mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-03-28 13:43:33 +01:00
'Option' refactoring.
This commit is contained in:
@@ -35,7 +35,7 @@ const Browser = Module("browser", {
|
|||||||
"Sets the current buffer's character encoding",
|
"Sets the current buffer's character encoding",
|
||||||
"string", "UTF-8",
|
"string", "UTF-8",
|
||||||
{
|
{
|
||||||
scope: options.OPTION_SCOPE_LOCAL,
|
scope: Option.SCOPE_LOCAL,
|
||||||
getter: function () getBrowser().docShell.QueryInterface(Ci.nsIDocCharset).charset,
|
getter: function () getBrowser().docShell.QueryInterface(Ci.nsIDocCharset).charset,
|
||||||
setter: function (val) {
|
setter: function (val) {
|
||||||
if (options["encoding"] == val)
|
if (options["encoding"] == val)
|
||||||
|
|||||||
@@ -31,84 +31,19 @@ const Option = Class("Option", {
|
|||||||
if (!extraInfo)
|
if (!extraInfo)
|
||||||
extraInfo = {};
|
extraInfo = {};
|
||||||
|
|
||||||
/** @property {string} The option's canonical name. */
|
|
||||||
this.name = names[0];
|
this.name = names[0];
|
||||||
/** @property {string[]} All names by which this option is identified. */
|
|
||||||
this.names = names;
|
this.names = names;
|
||||||
/**
|
|
||||||
* @property {string} The option's data type. One of:
|
|
||||||
* "boolean" - Boolean E.g. true
|
|
||||||
* "number" - Integer E.g. 1
|
|
||||||
* "string" - String E.g. "Vimperator"
|
|
||||||
* "charlist" - Character list E.g. "rb"
|
|
||||||
* "stringlist" - String list E.g. "homepage,quickmark,tabopen,paste"
|
|
||||||
*/
|
|
||||||
this.type = type;
|
this.type = type;
|
||||||
/**
|
this.description = description;
|
||||||
* @property {number} The scope of the option. This can be local, global,
|
|
||||||
* or both.
|
|
||||||
* @see Options#OPTION_SCOPE_LOCAL
|
|
||||||
* @see Options#OPTION_SCOPE_GLOBAL
|
|
||||||
* @see Options#OPTION_SCOPE_BOTH
|
|
||||||
*/
|
|
||||||
this.scope = (extraInfo.scope & options.OPTION_SCOPE_BOTH) || options.OPTION_SCOPE_GLOBAL; // XXX set to BOTH by default someday? - kstep
|
|
||||||
/**
|
|
||||||
* @property {string} This option's description, as shown in :optionusage.
|
|
||||||
*/
|
|
||||||
this.description = description || "";
|
|
||||||
|
|
||||||
/**
|
if (arguments.length > 3)
|
||||||
* @property {value} The option's default value. This value will be used
|
this.defaultValue = defaultValue;
|
||||||
* unless the option is explicitly set either interactively or in an RC
|
|
||||||
* file or plugin.
|
|
||||||
*/
|
|
||||||
this.defaultValue = (defaultValue === undefined) ? null : defaultValue; // "", 0 are valid default values
|
|
||||||
|
|
||||||
/**
|
update(this, extraInfo);
|
||||||
* @property {function} The function called when the option value is set.
|
|
||||||
*/
|
|
||||||
this.setter = extraInfo.setter || null;
|
|
||||||
/**
|
|
||||||
* @property {function} The function called when the option value is read.
|
|
||||||
*/
|
|
||||||
this.getter = extraInfo.getter || null;
|
|
||||||
/**
|
|
||||||
* @property {function(CompletionContext, Args)} This option's completer.
|
|
||||||
* @see CompletionContext
|
|
||||||
*/
|
|
||||||
this.completer = extraInfo.completer || null;
|
|
||||||
/**
|
|
||||||
* @property {function} The function called to validate the option's value
|
|
||||||
* when set.
|
|
||||||
*/
|
|
||||||
this.validator = extraInfo.validator || null;
|
|
||||||
/**
|
|
||||||
* @property The function called to determine whether the option already
|
|
||||||
* contains a specified value.
|
|
||||||
* @see #has
|
|
||||||
*/
|
|
||||||
this.checkHas = extraInfo.checkHas || null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @property {boolean} Set to true whenever the option is first set. This
|
|
||||||
* is useful to see whether it was changed from its default value
|
|
||||||
* interactively or by some RC file.
|
|
||||||
*/
|
|
||||||
this.hasChanged = false;
|
|
||||||
/**
|
|
||||||
* @property {nsIFile} The script in which this option was last set. null
|
|
||||||
* implies an interactive command.
|
|
||||||
*/
|
|
||||||
this.setFrom = null;
|
|
||||||
|
|
||||||
// add no{option} variant of boolean {option} to this.names
|
// add no{option} variant of boolean {option} to this.names
|
||||||
if (this.type == "boolean") {
|
if (this.type == "boolean")
|
||||||
this.names = []; // reset since order is important
|
this.names = array([name, "no" + name] for (name in values(names))).flatten();
|
||||||
for (let [, name] in Iterator(names)) {
|
|
||||||
this.names.push(name);
|
|
||||||
this.names.push("no" + name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.globalValue == undefined)
|
if (this.globalValue == undefined)
|
||||||
this.globalValue = this.defaultValue;
|
this.globalValue = this.defaultValue;
|
||||||
@@ -192,9 +127,9 @@ const Option = Class("Option", {
|
|||||||
|
|
||||||
let value;
|
let value;
|
||||||
|
|
||||||
if (liberator.has("tabs") && (scope & options.OPTION_SCOPE_LOCAL))
|
if (liberator.has("tabs") && (scope & Option.SCOPE_LOCAL))
|
||||||
value = tabs.options[this.name];
|
value = tabs.options[this.name];
|
||||||
if ((scope & options.OPTION_SCOPE_GLOBAL) && (value == undefined))
|
if ((scope & Option.SCOPE_GLOBAL) && (value == undefined))
|
||||||
value = this.globalValue;
|
value = this.globalValue;
|
||||||
|
|
||||||
if (this.getter)
|
if (this.getter)
|
||||||
@@ -220,9 +155,9 @@ const Option = Class("Option", {
|
|||||||
if (this.setter)
|
if (this.setter)
|
||||||
newValue = liberator.trapErrors(this.setter, this, newValue);
|
newValue = liberator.trapErrors(this.setter, this, newValue);
|
||||||
|
|
||||||
if (liberator.has("tabs") && (scope & options.OPTION_SCOPE_LOCAL))
|
if (liberator.has("tabs") && (scope & Option.SCOPE_LOCAL))
|
||||||
tabs.options[this.name] = newValue;
|
tabs.options[this.name] = newValue;
|
||||||
if ((scope & options.OPTION_SCOPE_GLOBAL) && newValue != this.globalValue)
|
if ((scope & Option.SCOPE_GLOBAL) && newValue != this.globalValue)
|
||||||
this.globalValue = newValue;
|
this.globalValue = newValue;
|
||||||
|
|
||||||
this.hasChanged = true;
|
this.hasChanged = true;
|
||||||
@@ -378,8 +313,104 @@ const Option = Class("Option", {
|
|||||||
if (!this.isValidValue(newValue))
|
if (!this.isValidValue(newValue))
|
||||||
return "E474: Invalid argument: " + values;
|
return "E474: Invalid argument: " + values;
|
||||||
this.setValues(newValue, scope);
|
this.setValues(newValue, scope);
|
||||||
}
|
},
|
||||||
|
|
||||||
|
// Properties {{{2
|
||||||
|
|
||||||
|
/** @property {string} The option's canonical name. */
|
||||||
|
name: null,
|
||||||
|
/** @property {string[]} All names by which this option is identified. */
|
||||||
|
names: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {string} The option's data type. One of:
|
||||||
|
* "boolean" - Boolean E.g. true
|
||||||
|
* "number" - Integer E.g. 1
|
||||||
|
* "string" - String E.g. "Vimperator"
|
||||||
|
* "charlist" - Character list E.g. "rb"
|
||||||
|
* "stringlist" - String list E.g. "homepage,quickmark,tabopen,paste"
|
||||||
|
*/
|
||||||
|
type: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {number} The scope of the option. This can be local, global,
|
||||||
|
* or both.
|
||||||
|
* @see Option#SCOPE_LOCAL
|
||||||
|
* @see Option#SCOPE_GLOBAL
|
||||||
|
* @see Option#SCOPE_BOTH
|
||||||
|
*/
|
||||||
|
scope: 1, // Option.SCOPE_GLOBAL // XXX set to BOTH by default someday? - kstep
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {string} This option's description, as shown in :optionusage.
|
||||||
|
*/
|
||||||
|
description: "",
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {value} The option's default value. This value will be used
|
||||||
|
* unless the option is explicitly set either interactively or in an RC
|
||||||
|
* file or plugin.
|
||||||
|
*/
|
||||||
|
defaultValue: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {function} The function called when the option value is set.
|
||||||
|
*/
|
||||||
|
setter: null,
|
||||||
|
/**
|
||||||
|
* @property {function} The function called when the option value is read.
|
||||||
|
*/
|
||||||
|
getter: null,
|
||||||
|
/**
|
||||||
|
* @property {function(CompletionContext, Args)} This option's completer.
|
||||||
|
* @see CompletionContext
|
||||||
|
*/
|
||||||
|
completer: null,
|
||||||
|
/**
|
||||||
|
* @property {function} The function called to validate the option's value
|
||||||
|
* when set.
|
||||||
|
*/
|
||||||
|
validator: null,
|
||||||
|
/**
|
||||||
|
* @property The function called to determine whether the option already
|
||||||
|
* contains a specified value.
|
||||||
|
* @see #has
|
||||||
|
*/
|
||||||
|
checkHas: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {boolean} Set to true whenever the option is first set. This
|
||||||
|
* is useful to see whether it was changed from its default value
|
||||||
|
* interactively or by some RC file.
|
||||||
|
*/
|
||||||
|
hasChanged: false,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {nsIFile} The script in which this option was last set. null
|
||||||
|
* implies an interactive command.
|
||||||
|
*/
|
||||||
|
setFrom: null
|
||||||
|
|
||||||
}, {
|
}, {
|
||||||
|
/**
|
||||||
|
* @property {number} Global option scope.
|
||||||
|
* @final
|
||||||
|
*/
|
||||||
|
SCOPE_GLOBAL: 1,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {number} Local option scope. Options in this scope only
|
||||||
|
* apply to the current tab/buffer.
|
||||||
|
* @final
|
||||||
|
*/
|
||||||
|
SCOPE_LOCAL: 2,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property {number} Both local and global option scope.
|
||||||
|
* @final
|
||||||
|
*/
|
||||||
|
SCOPE_BOTH: 3,
|
||||||
|
|
||||||
// TODO: Run this by default?
|
// TODO: Run this by default?
|
||||||
/**
|
/**
|
||||||
* Validates the specified <b>values</b> against values generated by the
|
* Validates the specified <b>values</b> against values generated by the
|
||||||
@@ -432,7 +463,7 @@ const Options = Module("options", {
|
|||||||
// Trigger any setters.
|
// Trigger any setters.
|
||||||
let opt = options.get(option);
|
let opt = options.get(option);
|
||||||
if (event == "change" && opt)
|
if (event == "change" && opt)
|
||||||
opt.set(opt.value, options.OPTION_SCOPE_GLOBAL);
|
opt.set(opt.value, Option.SCOPE_GLOBAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
storage.newMap("options", { store: false });
|
storage.newMap("options", { store: false });
|
||||||
@@ -445,25 +476,6 @@ const Options = Module("options", {
|
|||||||
this.prefObserver.unregister();
|
this.prefObserver.unregister();
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* @property {number} Global option scope.
|
|
||||||
* @final
|
|
||||||
*/
|
|
||||||
OPTION_SCOPE_GLOBAL: 1,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @property {number} Local option scope. Options in this scope only
|
|
||||||
* apply to the current tab/buffer.
|
|
||||||
* @final
|
|
||||||
*/
|
|
||||||
OPTION_SCOPE_LOCAL: 2,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @property {number} Both local and global option scope.
|
|
||||||
* @final
|
|
||||||
*/
|
|
||||||
OPTION_SCOPE_BOTH: 3,
|
|
||||||
|
|
||||||
/** @property {Iterator(Option)} @private */
|
/** @property {Iterator(Option)} @private */
|
||||||
__iterator__: function () {
|
__iterator__: function () {
|
||||||
let sorted = [o for ([i, o] in Iterator(this._optionHash))].sort(function (a, b) String.localeCompare(a.name, b.name));
|
let sorted = [o for ([i, o] in Iterator(this._optionHash))].sort(function (a, b) String.localeCompare(a.name, b.name));
|
||||||
@@ -551,7 +563,7 @@ const Options = Module("options", {
|
|||||||
*/
|
*/
|
||||||
get: function (name, scope) {
|
get: function (name, scope) {
|
||||||
if (!scope)
|
if (!scope)
|
||||||
scope = options.OPTION_SCOPE_BOTH;
|
scope = Option.SCOPE_BOTH;
|
||||||
|
|
||||||
if (name in this._optionHash)
|
if (name in this._optionHash)
|
||||||
return (this._optionHash[name].scope & scope) && this._optionHash[name];
|
return (this._optionHash[name].scope & scope) && this._optionHash[name];
|
||||||
@@ -575,7 +587,7 @@ const Options = Module("options", {
|
|||||||
*/
|
*/
|
||||||
list: function (onlyNonDefault, scope) {
|
list: function (onlyNonDefault, scope) {
|
||||||
if (!scope)
|
if (!scope)
|
||||||
scope = options.OPTION_SCOPE_BOTH;
|
scope = Option.SCOPE_BOTH;
|
||||||
|
|
||||||
function opts(opt) {
|
function opts(opt) {
|
||||||
for (let opt in Iterator(options)) {
|
for (let opt in Iterator(options)) {
|
||||||
@@ -1141,14 +1153,14 @@ const Options = Module("options", {
|
|||||||
commands.add(["setl[ocal]"],
|
commands.add(["setl[ocal]"],
|
||||||
"Set local option",
|
"Set local option",
|
||||||
function (args, modifiers) {
|
function (args, modifiers) {
|
||||||
modifiers.scope = options.OPTION_SCOPE_LOCAL;
|
modifiers.scope = Option.SCOPE_LOCAL;
|
||||||
setAction(args, modifiers);
|
setAction(args, modifiers);
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
bang: true,
|
bang: true,
|
||||||
count: true,
|
count: true,
|
||||||
completer: function (context, args) {
|
completer: function (context, args) {
|
||||||
return setCompleter(context, args, { scope: options.OPTION_SCOPE_LOCAL });
|
return setCompleter(context, args, { scope: Option.SCOPE_LOCAL });
|
||||||
},
|
},
|
||||||
literal: 0
|
literal: 0
|
||||||
}
|
}
|
||||||
@@ -1157,14 +1169,14 @@ const Options = Module("options", {
|
|||||||
commands.add(["setg[lobal]"],
|
commands.add(["setg[lobal]"],
|
||||||
"Set global option",
|
"Set global option",
|
||||||
function (args, modifiers) {
|
function (args, modifiers) {
|
||||||
modifiers.scope = options.OPTION_SCOPE_GLOBAL;
|
modifiers.scope = Option.SCOPE_GLOBAL;
|
||||||
setAction(args, modifiers);
|
setAction(args, modifiers);
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
bang: true,
|
bang: true,
|
||||||
count: true,
|
count: true,
|
||||||
completer: function (context, args) {
|
completer: function (context, args) {
|
||||||
return setCompleter(context, args, { scope: options.OPTION_SCOPE_GLOBAL });
|
return setCompleter(context, args, { scope: Option.SCOPE_GLOBAL });
|
||||||
},
|
},
|
||||||
literal: 0
|
literal: 0
|
||||||
}
|
}
|
||||||
@@ -1185,7 +1197,7 @@ const Options = Module("options", {
|
|||||||
: opt.name + "=" + opt.value]
|
: opt.name + "=" + opt.value]
|
||||||
}
|
}
|
||||||
for (opt in options)
|
for (opt in options)
|
||||||
if (!opt.getter && opt.value != opt.defaultValue && (opt.scope & options.OPTION_SCOPE_GLOBAL))
|
if (!opt.getter && opt.value != opt.defaultValue && (opt.scope & Option.SCOPE_GLOBAL))
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user