1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 11:58:00 +01:00

Integrate :se setter and completion parsing code.

This commit is contained in:
Kris Maglione
2008-10-08 04:58:27 +00:00
parent 17dcbd142c
commit 12e1b2e89e

View File

@@ -420,52 +420,37 @@ liberator.Options = function () //{{{
{ {
let ret = {}; let ret = {};
ret.args = args;
ret.onlyNonDefault = false; // used for :set to print non-default options ret.onlyNonDefault = false; // used for :set to print non-default options
if (!args) if (!args)
{ {
args = "all"; ret.args = "all";
ret.onlyNonDefault = true; ret.onlyNonDefault = true;
} }
// 1 2 3 4 5 6 [matches, prefix, ret.name, postfix, valueGiven, ret.operator, ret.value] =
var matches = args.match(/^\s*(no|inv)?([a-z_]+)([?&!])?\s*(([-+^]?)=(.*))?\s*$/); args.match(/^\s*(no|inv)?([a-z_]+)([?&!])?\s*(([-+^]?)=(.*))?\s*$/) || [];
ret.prefix = prefix;
ret.postfix = postfix;
if (!matches) if (!matches)
return null; return null;
ret.unsetBoolean = false;
if (matches[1] == "no")
ret.unsetBoolean = true;
ret.name = matches[2];
ret.all = false;
if (ret.name == "all")
ret.all = true;
ret.scope = modifiers && modifiers.scope || liberator.options.OPTION_SCOPE_BOTH; ret.scope = modifiers && modifiers.scope || liberator.options.OPTION_SCOPE_BOTH;
ret.option = liberator.options.get(ret.name, ret.scope); ret.option = liberator.options.get(ret.name, ret.scope);
if (!ret.option && !ret.all) ret.all = (ret.name == "all")
return ret; ret.get = (ret.all || postfix == "?" || (ret.option && ret.option.type != "boolean" && !valueGiven))
ret.invert = (prefix == "inv" || postfix == "!");
ret.reset = (postfix == "&");
ret.unsetBoolean = (prefix == "no");
let valueGiven = !!matches[4];
ret.get = false;
if (ret.all || matches[3] == "?" || (ret.option.type != "boolean" && !valueGiven))
ret.get = true;
ret.reset = false;
if (matches[3] == "&")
ret.reset = true;
ret.invert = false;
if (matches[1] == "inv" || matches[3] == "!")
ret.invert = true;
ret.operator = matches[5];
ret.value = matches[6];
if (ret.value === undefined) if (ret.value === undefined)
ret.value = ""; ret.value = "";
liberator.dump(ret);
return ret; return ret;
} }
@@ -483,16 +468,10 @@ liberator.Options = function () //{{{
onlyNonDefault = true; onlyNonDefault = true;
} }
// 1 2 3 4 5 let [matches, name, postfix, valueGiven, operator, value] =
var matches = args.match(/^\s*?([a-zA-Z0-9\.\-_{}]+)([?&!])?\s*(([-+^]?)=(.*))?\s*$/); args.match(/^\s*?([a-zA-Z0-9\.\-_{}]+)([?&!])?\s*(([-+^]?)=(.*))?\s*$/);
var name = matches[1]; let reset = (postfix == "&");
var reset = false; let invertBoolean = (postfix == "!");
var invertBoolean = false;
if (matches[2] == "&")
reset = true;
else if (matches[2] == "!")
invertBoolean = true;
if (name == "all" && reset) if (name == "all" && reset)
liberator.echoerr("You can't reset all options, it could make " + liberator.config.hostApplication + " unusable."); liberator.echoerr("You can't reset all options, it could make " + liberator.config.hostApplication + " unusable.");
@@ -502,9 +481,8 @@ liberator.Options = function () //{{{
liberator.options.resetPref(name); liberator.options.resetPref(name);
else if (invertBoolean) else if (invertBoolean)
liberator.options.invertPref(name); liberator.options.invertPref(name);
else if (matches[3]) else if (valueGiven)
{ {
var value = matches[5];
switch (value) switch (value)
{ {
case undefined: case undefined:
@@ -529,63 +507,13 @@ liberator.Options = function () //{{{
return; return;
} }
var onlyNonDefault = false; // used for :set to print non-default options let opt = parseOpt(args, modifiers);
if (!args) let option = opt.option;
{
args = "all";
onlyNonDefault = true;
}
// 1 2 3 4 5 6
var matches = args.match(/^\s*(no|inv)?([a-z_]+)([?&!])?\s*(([-+^]?)=(.*))?$/);
if (!matches)
{
liberator.echoerr("E518: Unknown option: " + args);
return;
}
var unsetBoolean = false;
if (matches[1] == "no")
unsetBoolean = true;
var name = matches[2];
var all = false;
if (name == "all")
all = true;
var scope = modifiers.scope || liberator.options.OPTION_SCOPE_BOTH;
var option = liberator.options.get(name, scope);
if (!option && !all)
{
liberator.echoerr("E518: Unknown option: " + args);
return;
}
var valueGiven = !!matches[4];
var get = false;
if (all || matches[3] == "?" || (option.type != "boolean" && !valueGiven))
get = true;
var reset = false;
if (matches[3] == "&")
reset = true;
var invertBoolean = false;
if (matches[1] == "inv" || matches[3] == "!")
invertBoolean = true;
var operator = matches[5];
var value = matches[6];
if (value === undefined)
value = "";
// reset a variable to its default value // reset a variable to its default value
if (reset) if (opt.reset)
{ {
if (all) if (opt.all)
{ {
for (let option in liberator.options) for (let option in liberator.options)
option.reset(); option.reset();
@@ -596,18 +524,18 @@ liberator.Options = function () //{{{
} }
} }
// read access // read access
else if (get) else if (opt.get)
{ {
if (all) if (opt.all)
{ {
liberator.options.list(onlyNonDefault, scope); liberator.options.list(opt.onlyNonDefault, scope);
} }
else else
{ {
if (option.type == "boolean") if (option.type == "boolean")
liberator.echo((option.get(scope) ? " " : "no") + option.name); liberator.echo((option.get(opt.scope) ? " " : "no") + option.name);
else else
liberator.echo(" " + option.name + "=" + option.get(scope)); liberator.echo(" " + option.name + "=" + option.get(opt.scope));
} }
} }
// write access // write access
@@ -615,28 +543,27 @@ liberator.Options = function () //{{{
// improved. i.e. Vim's behavior is pretty sloppy to no real benefit // improved. i.e. Vim's behavior is pretty sloppy to no real benefit
else else
{ {
var currentValue = option.get(scope); let currentValue = option.get(opt.scope);
let newValue;
var newValue;
switch (option.type) switch (option.type)
{ {
case "boolean": case "boolean":
if (valueGiven) if (opt.valueGiven)
{ {
liberator.echoerr("E474: Invalid argument: " + args); liberator.echoerr("E474: Invalid argument: " + args);
return; return;
} }
if (invertBoolean) if (opt.invert)
newValue = !currentValue; newValue = !currentValue;
else else
newValue = !unsetBoolean; newValue = !opt.unsetBoolean;
break; break;
case "number": case "number":
value = parseInt(value); // deduce radix let value = parseInt(opt.value); // deduce radix
if (isNaN(value)) if (isNaN(value))
{ {
@@ -644,65 +571,85 @@ liberator.Options = function () //{{{
return; return;
} }
if (operator == "+") switch (opt.operator)
newValue = currentValue + value; {
else if (operator == "-") case "+":
newValue = currentValue - value; newValue = currentValue + value;
else if (operator == "^") break;
newValue = currentValue * value; case "-":
else newValue = currentValue - value;
newValue = value; break;
case "^":
newValue = currentValue * value;
break;
default:
newValue = value;
break;
}
break; break;
case "charlist": case "charlist":
if (operator == "+") switch (opt.operator)
newValue = currentValue.replace(new RegExp("[" + value + "]", "g"), "") + value; {
else if (operator == "-") case "+":
newValue = currentValue.replace(value, ""); newValue = currentValue.replace(new RegExp("[" + opt.value + "]", "g"), "") + opt.value;
else if (operator == "^") break;
// NOTE: Vim doesn't prepend if there's a match in the current value case "-":
newValue = value + currentValue.replace(new RegExp("[" + value + "]", "g"), ""); newValue = currentValue.replace(new RegExp("[" + opt.value + "]", "g"), "");
else break;
newValue = value; case "^":
// NOTE: Vim doesn't prepend if there's a match in the current value
newValue = opt.value + currentValue.replace(new RegExp("[" + opt.value + "]", "g"), "");
break;
default:
newValue = opt.value;
break;
}
break; break;
case "stringlist": case "stringlist":
if (operator == "+") switch (opt.operator)
{ {
if (!currentValue.match(value)) case "+":
newValue = (currentValue ? currentValue + "," : "") + value; if (!currentValue.match(opt.value))
else newValue = (currentValue ? currentValue + "," : "") + opt.value;
newValue = currentValue; else
} newValue = currentValue;
else if (operator == "-") break;
{ case "-":
newValue = currentValue.replace(new RegExp("^" + value + ",?|," + value), ""); newValue = currentValue.replace(new RegExp("^" + opt.value + ",?|," + opt.value), "");
} break;
else if (operator == "^") case "^":
{ if (!currentValue.match(opt.value))
if (!currentValue.match(value)) newValue = opt.value + (currentValue ? "," : "") + currentValue;
newValue = value + (currentValue ? "," : "") + currentValue; else
else newValue = currentValue;
newValue = currentValue; break;
} default:
else newValue = opt.value;
{ break;
newValue = value;
} }
break; break;
case "string": case "string":
if (operator == "+") switch (opt.operator)
newValue = currentValue + value; {
else if (operator == "-") case "+":
newValue = currentValue.replace(value, ""); newValue = currentValue + opt.value;
else if (operator == "^") break;
newValue = value + currentValue; case "-":
else newValue = currentValue.replace(opt.value, "");
newValue = value; break;
case "^":
newValue = opt.value + currentValue;
break;
default:
newValue = opt.value;
break;
}
break; break;
@@ -712,7 +659,7 @@ liberator.Options = function () //{{{
if (option.isValidValue(newValue)) if (option.isValidValue(newValue))
{ {
option.set(newValue, scope); option.set(newValue, opt.scope);
} }
else else
// FIXME: need to be able to specify more specific errors // FIXME: need to be able to specify more specific errors