Merge pull request #5249 from lawrencetheabhorrence/unbind-recursive

`:unbind --recursive`
This commit is contained in:
Oliver Blanthorn 2025-09-13 13:07:04 +00:00 committed by GitHub
commit bc6d051591
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 3 deletions

View file

@ -4373,6 +4373,10 @@ export function comclear(name: string) {
*/
//#background
export async function bind(...args: string[]) {
if (args.includes("--recursive")) {
throw new Error("`--recursive` can only be called on unbind.")
}
const args_obj = parse_bind_args(...args)
let p = Promise.resolve()
if (args_obj.excmd !== "") {
@ -4830,7 +4834,10 @@ export function blacklistadd(url: string) {
return autocmd("DocStart", url, "mode ignore")
}
/** Unbind a sequence of keys so that they do nothing at all.
/**
Unbind a sequence of keys so that they do nothing at all.
Accepts the flag `--recursive` to unbind all binds that start with the specified key sequence, e.g. `:unbind --recursive ;` unbinds all the binds like `;f` `;F` `;;` etc.
See also:
@ -4840,6 +4847,17 @@ export function blacklistadd(url: string) {
//#background
export async function unbind(...args: string[]) {
const args_obj = parse_bind_args(...args)
if (args_obj.isRecursive) {
const prefix = args_obj.key
const maps = config.get(args_obj.configName as keyof config.default_config)
for (const binding in maps) {
if (binding.startsWith(prefix)) {
config.set(args_obj.configName, binding, null)
}
}
}
if (args_obj.excmd !== "") throw new Error("unbind syntax: `unbind key`")
if (args_obj.mode == "browser") {
const commands = await browser.commands.getAll()

View file

@ -27,6 +27,7 @@ interface bind_args {
configName: string
key: string
excmd: string
isRecursive: boolean
}
export function parse_bind_args(...args: string[]): bind_args {
@ -35,9 +36,21 @@ export function parse_bind_args(...args: string[]): bind_args {
const result = {} as bind_args
result.mode = "normal"
if (args[0].startsWith("--mode=")) {
result.mode = args.shift().replace("--mode=", "")
const flags = []; // --mode and --recursive
while (args[0].startsWith("--")) {
flags.push(args.shift());
}
for (const flag of flags) {
if (flag.startsWith("--mode")) {
result.mode = flag.replace("--mode=", "");
} else if (flag == "--recursive") {
result.isRecursive = true;
} else {
throw new Error("Invalid bind/unbind arguments.");
}
}
if (!mode2maps.has(result.mode)) {
result.configName = result.mode + "maps"
} else {