1 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2011 by Kris Maglione <maglione.k@gmail.com>
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
11 Components.utils.import("resource://dactyl/bootstrap.jsm");
12 defineModule("options", {
13 exports: ["Option", "Options", "ValueError", "options"],
14 require: ["contexts", "messages", "storage"]
17 this.lazyRequire("config", ["config"]);
21 let ValueError = Class("ValueError", ErrorBase);
23 // do NOT create instances of this class yourself, use the helper method
24 // options.add() instead
26 * A class representing configuration options. Instances are created by the
27 * {@link Options} class.
29 * @param {[string]} names The names by which this option is identified.
30 * @param {string} description A short one line description of the option.
31 * @param {string} type The option's value data type (see {@link Option#type}).
32 * @param {string} defaultValue The default value for this option.
33 * @param {Object} extraInfo An optional extra configuration hash. The
34 * following properties are supported.
35 * completer - see {@link Option#completer}
36 * domains - see {@link Option#domains}
37 * getter - see {@link Option#getter}
38 * initialValue - Initial value is loaded from getter
39 * persist - see {@link Option#persist}
40 * privateData - see {@link Option#privateData}
41 * scope - see {@link Option#scope}
42 * setter - see {@link Option#setter}
43 * validator - see {@link Option#validator}
47 var Option = Class("Option", {
48 init: function init(modules, names, description, defaultValue, extraInfo) {
49 this.modules = modules;
51 this.realNames = names;
52 this.description = description;
55 this.update(extraInfo);
57 this._defaultValue = defaultValue;
59 if (this.globalValue == undefined && !this.initialValue)
60 this.globalValue = this.defaultValue;
63 magicalProperties: Set(["cleanupValue"]),
66 * @property {string} This option's description, as shown in :listoptions.
68 description: Messages.Localized(""),
70 get helpTag() "'" + this.name + "'",
72 initValue: function initValue() {
73 util.trapErrors(function () this.value = this.value, this);
76 get isDefault() this.stringValue === this.stringDefaultValue,
78 /** @property {value} The value to reset this option to at cleanup time. */
79 get cleanupValue() options.cleanupPrefs.get(this.name),
80 set cleanupValue(value) {
81 if (options.cleanupPrefs.get(this.name) == null)
82 options.cleanupPrefs.set(this.name, value);
85 /** @property {value} The option's global value. @see #scope */
87 let val = options.store.get(this.name, {}).value;
90 return this.globalValue = this.defaultValue;
92 set globalValue(val) {
93 options.store.set(this.name, { value: val, time: Date.now() });
97 * Returns *value* as an array of parsed values if the option type is
98 * "charlist" or "stringlist" or else unchanged.
100 * @param {value} value The option value.
101 * @returns {value|[string]}
103 parse: function parse(value) Option.dequote(value),
106 * Returns *values* packed in the appropriate format for the option type.
108 * @param {value|[string]} values The option value.
111 stringify: function stringify(vals) Commands.quote(vals),
114 * Returns the option's value as an array of parsed values if the option
115 * type is "charlist" or "stringlist" or else the simple value.
117 * @param {number} scope The scope to return these values from (see
118 * {@link Option#scope}).
119 * @returns {value|[string]}
121 get: function get(scope) {
123 if ((scope & this.scope) == 0) // option doesn't exist in this scope
132 if (config.has("tabs") && (scope & Option.SCOPE_LOCAL))
133 values = tabs.options[this.name];
135 if ((scope & Option.SCOPE_GLOBAL) && (values == undefined))
136 values = this.globalValue;
139 return util.trapErrors(this.getter, this, values);
145 * Sets the option's value from an array of values if the option type is
146 * "charlist" or "stringlist" or else the simple value.
148 * @param {number} scope The scope to apply these values to (see
149 * {@link Option#scope}).
151 set: function set(newValues, scope, skipGlobal) {
152 scope = scope || this.scope;
153 if ((scope & this.scope) == 0) // option doesn't exist in this scope
157 newValues = this.setter(newValues);
158 if (newValues === undefined)
162 if (config.has("tabs") && (scope & Option.SCOPE_LOCAL))
163 tabs.options[this.name] = newValues;
165 if ((scope & Option.SCOPE_GLOBAL) && !skipGlobal)
166 this.globalValue = newValues;
168 this.hasChanged = true;
171 // dactyl.triggerObserver("options." + this.name, newValues);
174 getValues: deprecated("Option#get", "get"),
175 setValues: deprecated("Option#set", "set"),
176 joinValues: deprecated("Option#stringify", "stringify"),
177 parseValues: deprecated("Option#parse", "parse"),
180 * @property {value} The option's current value. The option's local value,
181 * or if no local value is set, this is equal to the
182 * (@link #globalValue).
184 get value() this.get(),
185 set value(val) this.set(val),
187 get stringValue() this.stringify(this.value),
188 set stringValue(value) this.value = this.parse(value),
190 get stringDefaultValue() this.stringify(this.defaultValue),
191 set stringDefaultValue(val) this.defaultValue = this.parse(val),
193 getKey: function getKey(key) undefined,
196 * Returns whether the option value contains one or more of the specified
201 has: function has() Array.some(arguments, function (val) this.value.indexOf(val) >= 0, this),
204 * Returns whether this option is identified by *name*.
206 * @param {string} name
209 hasName: function hasName(name) this.names.indexOf(name) >= 0,
212 * Returns whether the specified *values* are valid for this option.
213 * @see Option#validator
215 isValidValue: function isValidValue(values) this.validator(values),
217 invalidArgument: function invalidArgument(arg, op) _("error.invalidArgument",
218 this.name + (op || "").replace(/=?$/, "=") + arg),
221 * Resets the option to its default value.
223 reset: function reset() {
224 this.value = this.defaultValue;
228 * Sets the option's value using the specified set *operator*.
230 * @param {string} operator The set operator.
231 * @param {value|[string]} values The value (or values) to apply.
232 * @param {number} scope The scope to apply this value to (see
234 * @param {boolean} invert Whether this is an invert boolean operation.
236 op: function op(operator, values, scope, invert, str) {
239 var newValues = this._op(operator, values, scope, invert);
240 if (newValues == null)
241 return _("option.operatorNotSupported", operator, this.type);
243 if (!this.isValidValue(newValues))
244 return this.invalidArgument(str || this.stringify(values), operator);
246 this.set(newValues, scope);
249 if (!(e instanceof ValueError))
251 return this.invalidArgument(str || this.stringify(values), operator) + ": " + e.message;
258 /** @property {string} The option's canonical name. */
261 /** @property {[string]} All names by which this option is identified. */
262 names: Class.Memoize(function () this.realNames),
265 * @property {string} The option's data type. One of:
266 * "boolean" - Boolean, e.g., true
267 * "number" - Integer, e.g., 1
268 * "string" - String, e.g., "Pentadactyl"
269 * "charlist" - Character list, e.g., "rb"
270 * "regexplist" - Regexp list, e.g., "^foo,bar$"
271 * "stringmap" - String map, e.g., "key:v,foo:bar"
272 * "regexpmap" - Regexp map, e.g., "^key:v,foo$:bar"
277 * @property {number} The scope of the option. This can be local, global,
279 * @see Option#SCOPE_LOCAL
280 * @see Option#SCOPE_GLOBAL
281 * @see Option#SCOPE_BOTH
283 scope: 1, // Option.SCOPE_GLOBAL // XXX set to BOTH by default someday? - kstep
286 * @property {function(CompletionContext, Args)} This option's completer.
287 * @see CompletionContext
289 completer: function completer(context, extra) {
290 if (/map$/.test(this.type) && extra.value == null)
294 context.completions = this.values;
298 * @property {[[string, string]]} This option's possible values.
299 * @see CompletionContext
301 values: Messages.Localized(null),
304 * @property {function(host, values)} A function which should return a list
305 * of domains referenced in the given values. Used in determining whether
306 * to purge the command from history when clearing private data.
307 * @see Command#domains
312 * @property {function(host, values)} A function which should strip
313 * references to a given domain from the given values.
315 filterDomain: function filterDomain(host, values)
316 Array.filter(values, function (val) !this.domains([val]).some(function (val) util.isSubdomain(val, host)), this),
319 * @property {value} The option's default value. This value will be used
320 * unless the option is explicitly set either interactively or in an RC
323 defaultValue: Class.Memoize(function () {
324 let defaultValue = this._defaultValue;
325 delete this._defaultValue;
327 if (Set.has(this.modules.config.optionDefaults, this.name))
328 defaultValue = this.modules.config.optionDefaults[this.name];
330 if (defaultValue == null && this.getter)
331 defaultValue = this.getter();
333 if (defaultValue == undefined)
336 if (this.type === "string")
337 defaultValue = Commands.quote(defaultValue);
339 if (isArray(defaultValue))
340 defaultValue = defaultValue.map(Option.quote).join(",");
341 else if (isObject(defaultValue))
342 defaultValue = iter(defaultValue).map(function (val) val.map(function (v) Option.quote(v, /:/))
346 if (isArray(defaultValue))
347 defaultValue = defaultValue.map(Option.quote).join(",");
349 return this.parse(defaultValue);
353 * @property {function} The function called when the option value is read.
358 * @property {boolean} When true, this options values will be saved
359 * when generating a configuration file.
365 * @property {boolean|function(values)} When true, values of this
366 * option may contain private data which should be purged from
367 * saved histories when clearing private data. If a function, it
368 * should return true if an invocation with the given values
369 * contains private data
374 * @property {function} The function called when the option value is set.
378 testValues: function testValues(values, validator) validator(values),
381 * @property {function} The function called to validate the option's value
384 validator: function validator() {
385 if (this.values || this.completer !== Option.prototype.completer)
386 return Option.validateCompleter.apply(this, arguments);
391 * @property {boolean} Set to true whenever the option is first set. This
392 * is useful to see whether it was changed from its default value
393 * interactively or by some RC file.
398 * Returns the timestamp when the option's value was last changed.
400 get lastSet() options.store.get(this.name).time,
401 set lastSet(val) { options.store.set(this.name, { value: this.globalValue, time: Date.now() }); },
404 * @property {nsIFile} The script in which this option was last set. null
405 * implies an interactive command.
411 * @property {number} Global option scope.
417 * @property {number} Local option scope. Options in this scope only
418 * apply to the current tab/buffer.
424 * @property {number} Both local and global option scope.
430 toggleAll: function toggleAll() toggleAll.supercall(this, "all") ^ !!toggleAll.superapply(this, arguments),
433 parseRegexp: function parseRegexp(value, result, flags) {
434 let keepQuotes = this && this.keepQuotes;
435 if (isArray(flags)) // Called by Array.map
436 result = flags = undefined;
439 flags = this && this.regexpFlags || "";
441 let [, bang, val] = /^(!?)(.*)/.exec(value);
442 let re = util.regexp(Option.dequote(val), flags);
444 re.result = result !== undefined ? result : !bang;
445 re.key = re.bang + Option.quote(util.regexp.getSource(re), /^!|:/);
446 re.toString = function () Option.unparseRegexp(this, keepQuotes);
450 unparseRegexp: function unparseRegexp(re, quoted) re.bang + Option.quote(util.regexp.getSource(re), /^!|:/) +
451 (typeof re.result === "boolean" ? "" : ":" + (quoted ? re.result : Option.quote(re.result, /:/))),
453 parseSite: function parseSite(pattern, result, rest) {
454 if (isArray(rest)) // Called by Array.map
457 let [, bang, filter] = /^(!?)(.*)/.exec(pattern);
458 filter = Option.dequote(filter).trim();
460 let quote = this.keepQuotes ? util.identity : function (v) Option.quote(v, /:/);
462 return update(Styles.matchFilter(filter), {
465 result: result !== undefined ? result : !bang,
466 toString: function toString() this.bang + Option.quote(this.filter, /:/) +
467 (typeof this.result === "boolean" ? "" : ":" + quote(this.result)),
472 stringlist: function stringlist(k) this.value.indexOf(k) >= 0,
473 get charlist() this.stringlist,
475 regexplist: function regexplist(k, default_) {
476 for (let re in values(this.value))
477 if ((re.test || re).call(re, k))
479 return arguments.length > 1 ? default_ : null;
481 get regexpmap() this.regexplist,
482 get sitelist() this.regexplist,
483 get sitemap() this.regexplist
487 sitelist: function (vals) array.compact(vals.map(function (site) util.getHost(site.filter))),
488 get sitemap() this.sitelist
492 charlist: function (vals) Commands.quote(vals.join("")),
494 stringlist: function (vals) vals.map(Option.quote).join(","),
496 stringmap: function (vals) [Option.quote(k, /:/) + ":" + Option.quote(v, /:/) for ([k, v] in Iterator(vals))].join(","),
498 regexplist: function (vals) vals.join(","),
499 get regexpmap() this.regexplist,
500 get sitelist() this.regexplist,
501 get sitemap() this.regexplist
505 number: function (value) let (val = Option.dequote(value))
506 Option.validIf(Number(val) % 1 == 0, _("option.intRequired")) && parseInt(val),
508 boolean: function boolean(value) Option.dequote(value) == "true" || value == true ? true : false,
510 charlist: function charlist(value) Array.slice(Option.dequote(value)),
512 stringlist: function stringlist(value) (value === "") ? [] : Option.splitList(value),
514 regexplist: function regexplist(value) (value === "") ? [] :
515 Option.splitList(value, true)
516 .map(function (re) Option.parseRegexp(re, undefined, this.regexpFlags), this),
518 sitelist: function sitelist(value) {
522 value = Option.splitList(value, true);
523 return value.map(Option.parseSite, this);
526 stringmap: function stringmap(value) array.toObject(
527 Option.splitList(value, true).map(function (v) {
528 let [count, key, quote] = Commands.parseArg(v, /:/);
529 return [key, Option.dequote(v.substr(count + 1))];
532 regexpmap: function regexpmap(value) Option.parse.list.call(this, value, Option.parseRegexp),
534 sitemap: function sitemap(value) Option.parse.list.call(this, value, Option.parseSite),
536 list: function list(value, parse) let (prev = null)
537 array.compact(Option.splitList(value, true).map(function (v) {
538 let [count, filter, quote] = Commands.parseArg(v, /:/, true);
540 let val = v.substr(count + 1);
541 if (!this.keepQuotes)
542 val = Option.dequote(val);
544 if (v.length > count)
545 return prev = parse.call(this, filter, val);
547 util.assert(prev, _("error.syntaxError"), false);
548 prev.result += "," + v;
554 regexpmap: function regexpmap(vals, validator) vals.every(function (re) validator(re.result)),
555 get sitemap() this.regexpmap,
556 stringlist: function stringlist(vals, validator) vals.every(validator, this),
557 stringmap: function stringmap(vals, validator) values(vals).every(validator, this)
560 dequote: function dequote(value) {
562 [, arg, Option._quote] = Commands.parseArg(String(value), "");
567 splitList: function splitList(value, keepQuotes) {
570 while (value.length) {
571 if (count !== undefined)
572 value = value.slice(1);
573 var [count, arg, quote] = Commands.parseArg(value, /,/, keepQuotes);
574 Option._quote = quote; // FIXME
576 if (value.length > count)
577 Option._splitAt += count + 1;
578 value = value.slice(count);
583 quote: function quote(str, re) isArray(str) ? str.map(function (s) quote(s, re)).join(",") :
584 Commands.quoteArg[/[\s|"'\\,]|^$/.test(str) || re && re.test && re.test(str)
585 ? (/[\b\f\n\r\t]/.test(str) ? '"' : "'")
589 boolean: function boolean(operator, values, scope, invert) {
597 number: function number(operator, values, scope, invert) {
599 values = values[(values.indexOf(String(this.value)) + 1) % values.length];
601 let value = parseInt(values);
602 util.assert(Number(values) % 1 == 0,
603 _("command.set.numberRequired", this.name, values));
607 return this.value + value;
609 return this.value - value;
611 return this.value * value;
618 string: function string(operator, values, scope, invert) {
620 return values[(values.indexOf(this.value) + 1) % values.length];
624 return this.value + values;
626 return this.value.replace(values, "");
628 return values + this.value;
635 stringmap: function stringmap(operator, values, scope, invert) {
636 let res = update({}, this.value);
639 // The result is the same.
642 return update(res, values);
644 for (let [k, v] in Iterator(values))
650 for (let [k, v] in Iterator(values))
662 stringlist: function stringlist(operator, values, scope, invert) {
663 values = Array.concat(values);
667 return ary.filter(function (elem) !Set.add(seen, elem));
672 return uniq(Array.concat(this.value, values), true);
674 // NOTE: Vim doesn't prepend if there's a match in the current value
675 return uniq(Array.concat(values, this.value), true);
677 return this.value.filter(function (item) !Set.has(this, item), Set(values));
680 let keepValues = this.value.filter(function (item) !Set.has(this, item), Set(values));
681 let addValues = values.filter(function (item) !Set.has(this, item), Set(this.value));
682 return addValues.concat(keepValues);
688 get charlist() this.stringlist,
689 get regexplist() this.stringlist,
690 get regexpmap() this.stringlist,
691 get sitelist() this.stringlist,
692 get sitemap() this.stringlist
695 validIf: function validIf(test, error) {
698 throw ValueError(error);
702 * Validates the specified *values* against values generated by the
703 * option's completer function.
705 * @param {value|[string]} values The value or array of values to validate.
708 validateCompleter: function validateCompleter(vals) {
709 function completions(extra) {
710 let context = CompletionContext("");
711 return context.fork("", 0, this, this.completer, extra) ||
712 context.allItems.items.map(function (item) [item.text]);
715 if (isObject(vals) && !isArray(vals)) {
716 let k = values(completions.call(this, { values: {} })).toObject();
717 let v = values(completions.call(this, { value: "" })).toObject();
718 return Object.keys(vals).every(Set.has(k)) && values(vals).every(Set.has(v));
722 var acceptable = this.values.array || this.values;
724 acceptable = completions.call(this);
726 if (isArray(acceptable))
727 acceptable = Set(acceptable.map(function ([k]) k));
729 if (this.type === "regexpmap" || this.type === "sitemap")
730 return Array.concat(vals).every(function (re) Set.has(acceptable, re.result));
732 return Array.concat(vals).every(Set.has(acceptable));
747 "StringMap"].forEach(function (name) {
748 let type = name.toLowerCase();
749 let class_ = Class(name + "Option", Option, {
752 _op: Option.ops[type]
755 if (type in Option.getKey)
756 class_.prototype.getKey = Option.getKey[type];
758 if (type in Option.parse)
759 class_.prototype.parse = Option.parse[type];
761 if (type in Option.stringify)
762 class_.prototype.stringify = Option.stringify[type];
764 if (type in Option.domains)
765 class_.prototype.domains = Option.domains[type];
767 if (type in Option.testValues)
768 class_.prototype.testValues = Option.testValues[type];
770 Option.types[type] = class_;
771 this[class_.className] = class_;
772 EXPORTED_SYMBOLS.push(class_.className);
775 update(BooleanOption.prototype, {
776 names: Class.Memoize(function ()
777 array.flatten([[name, "no" + name] for (name in values(this.realNames))]))
780 var OptionHive = Class("OptionHive", Contexts.Hive, {
781 init: function init(group) {
782 init.supercall(this, group);
784 this.has = Set.has(this.values);
787 add: function add(names, description, type, defaultValue, extraInfo) {
788 return this.modules.options.add(names, description, type, defaultValue, extraInfo);
795 var Options = Module("options", {
796 Local: function Local(dactyl, modules, window) let ({ contexts } = modules) ({
797 init: function init() {
801 hives: contexts.Hives("options", Class("OptionHive", OptionHive, { modules: modules })),
802 user: contexts.hives.options.user
807 this._optionMap = {};
809 storage.newMap("options", { store: false });
810 storage.addObserver("options", function optionObserver(key, event, option) {
811 // Trigger any setters.
812 let opt = self.get(option);
813 if (event == "change" && opt)
814 opt.set(opt.globalValue, Option.SCOPE_GLOBAL, true);
817 modules.cache.register("options.dtd", function ()
819 iter(([["option", o.name, "default"].join("."),
820 o.type === "string" ? o.defaultValue.replace(/'/g, "''") :
821 o.defaultValue === true ? "on" :
822 o.defaultValue === false ? "off" : o.stringDefaultValue]
825 ([["option", o.name, "type"].join("."), o.type] for (o in self)),
831 "io.source": function ioSource(context, file, modTime) {
832 cache.flushEntry("options.dtd", modTime);
839 * Lists all options in *scope* or only those with changed values if
840 * *onlyNonDefault* is specified.
842 * @param {function(Option)} filter Limit the list
843 * @param {number} scope Only list options in this scope (see
844 * {@link Option#scope}).
846 list: function list(filter, scope) {
848 scope = Option.SCOPE_BOTH;
851 for (let opt in Iterator(this)) {
854 isDefault: opt.isDefault,
855 default: opt.stringDefaultValue,
856 pre: "\u00a0\u00a0", // Unicode nonbreaking space.
860 if (filter && !filter(opt))
862 if (!(opt.scope & scope))
865 if (opt.type == "boolean") {
868 option.default = (opt.defaultValue ? "" : "no") + opt.name;
870 else if (isArray(opt.value) && opt.type != "charlist")
871 option.value = <>={template.map(opt.value,
872 function (v) template.highlight(String(v)),
873 <>,<span style="width: 0; display: inline-block"> </span></>)}</>;
875 option.value = <>={template.highlight(opt.stringValue)}</>;
880 modules.commandline.commandOutput(template.options("Options", opts.call(this), this["verbose"] > 0));
883 cleanup: function cleanup() {
884 for (let opt in this)
885 if (opt.cleanupValue != null)
886 opt.stringValue = opt.cleanupValue;
892 * @param {[string]} names All names for the option.
893 * @param {string} description A description of the option.
894 * @param {string} type The option type (see {@link Option#type}).
895 * @param {value} defaultValue The option's default value.
896 * @param {Object} extra An optional extra configuration hash (see
897 * {@link Map#extraInfo}).
900 add: function add(names, description, type, defaultValue, extraInfo) {
903 if (!util.isDactyl(Components.stack.caller))
904 deprecated.warn(add, "options.add", "group.options.add");
906 util.assert(type in Option.types, _("option.noSuchType", type),
912 extraInfo.definedAt = contexts.getCaller(Components.stack.caller);
915 if (name in this._optionMap) {
916 this.dactyl.log(_("option.replaceExisting", name.quote()), 1);
920 let closure = function () self._optionMap[name];
922 memoize(this._optionMap, name, function () Option.types[type](modules, names, description, defaultValue, extraInfo));
923 for (let alias in values(names.slice(1)))
924 memoize(this._optionMap, alias, closure);
926 if (extraInfo.setter && (!extraInfo.scope || extraInfo.scope & Option.SCOPE_GLOBAL))
927 if (this.dactyl.initialized)
928 closure().initValue();
930 memoize(this.needInit, this.needInit.length, closure);
932 this._floptions = (this._floptions || []).concat(name);
933 memoize(this._options, this._options.length, closure);
935 // quickly access options with options["wildmode"]:
936 this.__defineGetter__(name, function () this._optionMap[name].value);
937 this.__defineSetter__(name, function (value) { this._optionMap[name].value = value; });
941 /** @property {Iterator(Option)} @private */
942 __iterator__: function __iterator__()
943 values(this._options.sort(function (a, b) String.localeCompare(a.name, b.name))),
945 allPrefs: deprecated("prefs.getNames", function allPrefs() prefs.getNames.apply(prefs, arguments)),
946 getPref: deprecated("prefs.get", function getPref() prefs.get.apply(prefs, arguments)),
947 invertPref: deprecated("prefs.invert", function invertPref() prefs.invert.apply(prefs, arguments)),
948 listPrefs: deprecated("prefs.list", function listPrefs() { this.modules.commandline.commandOutput(prefs.list.apply(prefs, arguments)); }),
949 observePref: deprecated("prefs.observe", function observePref() prefs.observe.apply(prefs, arguments)),
950 popContext: deprecated("prefs.popContext", function popContext() prefs.popContext.apply(prefs, arguments)),
951 pushContext: deprecated("prefs.pushContext", function pushContext() prefs.pushContext.apply(prefs, arguments)),
952 resetPref: deprecated("prefs.reset", function resetPref() prefs.reset.apply(prefs, arguments)),
953 safeResetPref: deprecated("prefs.safeReset", function safeResetPref() prefs.safeReset.apply(prefs, arguments)),
954 safeSetPref: deprecated("prefs.safeSet", function safeSetPref() prefs.safeSet.apply(prefs, arguments)),
955 setPref: deprecated("prefs.set", function setPref() prefs.set.apply(prefs, arguments)),
956 withContext: deprecated("prefs.withContext", function withContext() prefs.withContext.apply(prefs, arguments)),
958 cleanupPrefs: Class.Memoize(function () config.prefs.Branch("cleanup.option.")),
960 cleanup: function cleanup(reason) {
961 if (~["disable", "uninstall"].indexOf(reason))
962 this.cleanupPrefs.resetBranch();
966 * Returns the option with *name* in the specified *scope*.
968 * @param {string} name The option's name.
969 * @param {number} scope The option's scope (see {@link Option#scope}).
971 * @returns {Option} The matching option.
973 get: function get(name, scope) {
975 scope = Option.SCOPE_BOTH;
977 if (this._optionMap[name] && (this._optionMap[name].scope & scope))
978 return this._optionMap[name];
983 * Parses a :set command's argument string.
985 * @param {string} args The :set command's argument string.
986 * @param {Object} modifiers A hash of parsing modifiers. These are:
987 * scope - see {@link Option#scope}
989 * @returns {Object} The parsed command object.
991 parseOpt: function parseOpt(args, modifiers) {
993 let matches, prefix, postfix;
995 [matches, prefix, res.name, postfix, res.valueGiven, res.operator, res.value] =
996 args.match(/^\s*(no|inv)?([^=]+?)([?&!])?\s*(([-+^]?)=(.*))?\s*$/) || [];
999 res.onlyNonDefault = false; // used for :set to print non-default options
1002 res.onlyNonDefault = true;
1006 if (res.option = this.get(res.name, res.scope)) {
1007 if (prefix === "no" && res.option.type !== "boolean")
1010 else if (res.option = this.get(prefix + res.name, res.scope)) {
1011 res.name = prefix + res.name;
1016 res.prefix = prefix;
1017 res.postfix = postfix;
1019 res.all = (res.name == "all");
1020 res.get = (res.all || postfix == "?" || (res.option && res.option.type != "boolean" && !res.valueGiven));
1021 res.invert = (prefix == "inv" || postfix == "!");
1022 res.reset = (postfix == "&");
1023 res.unsetBoolean = (prefix == "no");
1025 res.scope = modifiers && modifiers.scope;
1030 if (res.value === undefined)
1033 res.optionValue = res.option.get(res.scope);
1036 if (!res.invert || res.option.type != "number") // Hack.
1037 res.values = res.option.parse(res.value);
1047 * Remove the option with matching *name*.
1049 * @param {string} name The name of the option to remove. This can be
1050 * any of the option's names.
1052 remove: function remove(name) {
1053 let opt = this.get(name);
1054 this._options = this._options.filter(function (o) o != opt);
1055 for (let name in values(opt.names))
1056 delete this._optionMap[name];
1059 /** @property {Object} The options store. */
1060 get store() storage.options
1063 commands: function initCommands(dactyl, modules, window) {
1064 const { commands, contexts, options } = modules;
1067 getMode: function (args) findMode(args["-mode"]),
1068 iterate: function (args) {
1069 for (let map in mappings.iterate(this.getMode(args)))
1070 for (let name in values(map.names))
1071 yield { name: name, __proto__: map };
1074 description: function (map) (XML.ignoreWhitespace = false, XML.prettyPrinting = false, <>
1075 {options.get("passkeys").has(map.name)
1076 ? <span highlight="URLExtra">({
1077 tempate.linkifyHelp(_("option.passkeys.passedBy"))
1080 {template.linkifyHelp(map.description)}
1085 dactyl.addUsageCommand({
1086 name: ["listo[ptions]", "lo"],
1087 description: "List all options along with their short descriptions",
1089 iterate: function (args) options,
1091 description: function (opt) (XML.ignoreWhitespace = false, XML.prettyPrinting = false, <>
1092 {opt.scope == Option.SCOPE_LOCAL
1093 ? <span highlight="URLExtra">({_("option.bufferLocal")})</span> : ""}
1094 {template.linkifyHelp(opt.description)}
1096 help: function (opt) "'" + opt.name + "'"
1100 function setAction(args, modifiers) {
1101 let bang = args.bang;
1106 function flushList() {
1107 let names = Set(list.map(function (opt) opt.option ? opt.option.name : ""));
1109 if (list.some(function (opt) opt.all))
1110 options.list(function (opt) !(list[0].onlyNonDefault && opt.isDefault), list[0].scope);
1112 options.list(function (opt) Set.has(names, opt.name), list[0].scope);
1116 for (let [, arg] in args) {
1118 let onlyNonDefault = false;
1120 let invertBoolean = false;
1122 if (args[0] == "") {
1124 onlyNonDefault = true;
1127 var [matches, name, postfix, valueGiven, operator, value] =
1128 arg.match(/^\s*?((?:[^=\\']|\\.|'[^']*')+?)([?&!])?\s*(([-+^]?)=(.*))?\s*$/);
1129 reset = (postfix == "&");
1130 invertBoolean = (postfix == "!");
1133 name = Option.dequote(name);
1134 if (name == "all" && reset)
1135 modules.commandline.input(_("pref.prompt.resetAll", config.host) + " ",
1138 for (let pref in values(prefs.getNames()))
1141 { promptHighlight: "WarningMsg" });
1142 else if (name == "all")
1143 modules.commandline.commandOutput(prefs.list(onlyNonDefault, ""));
1146 else if (invertBoolean)
1148 else if (valueGiven) {
1149 if (value == undefined)
1151 else if (value == "true")
1153 else if (value == "false")
1155 else if (Number(value) % 1 == 0)
1156 value = parseInt(value);
1158 value = Option.dequote(value);
1161 value = Option.ops[typeof value].call({ value: prefs.get(name) }, operator, value);
1162 prefs.set(name, value);
1165 modules.commandline.commandOutput(prefs.list(onlyNonDefault, name));
1169 let opt = modules.options.parseOpt(arg, modifiers);
1170 util.assert(opt, _("command.set.errorParsing", arg));
1171 util.assert(!opt.error, _("command.set.errorParsing", opt.error));
1173 let option = opt.option;
1174 util.assert(option != null || opt.all, _("command.set.unknownOption", opt.name));
1176 // reset a variable to its default value
1180 for (let option in modules.options)
1193 if (opt.option.type === "boolean") {
1194 util.assert(!opt.valueGiven, _("error.invalidArgument", arg));
1195 opt.values = !opt.unsetBoolean;
1197 else if (/^(string|number)$/.test(opt.option.type) && opt.invert)
1198 opt.values = Option.splitList(opt.value);
1200 var res = opt.option.op(opt.operator || "=", opt.values, opt.scope, opt.invert,
1207 dactyl.echoerr(res);
1208 option.setFrom = contexts.getCaller(null);
1214 function setCompleter(context, args, modifiers) {
1215 const { completion } = modules;
1217 let filter = context.filter;
1219 if (args.bang) { // list completions for about:config entries
1220 if (filter[filter.length - 1] == "=") {
1221 context.advance(filter.length);
1222 filter = filter.substr(0, filter.length - 1);
1224 context.pushProcessor(0, function (item, text, next) next(item, text.substr(0, 100)));
1225 context.completions = [
1226 [prefs.get(filter), _("option.currentValue")],
1227 [prefs.defaults.get(filter), _("option.defaultValue")]
1228 ].filter(function (k) k[0] != null);
1232 return completion.preference(context);
1235 let opt = modules.options.parseOpt(filter, modifiers);
1236 let prefix = opt.prefix;
1238 context.highlight();
1239 if (context.filter.indexOf("=") == -1) {
1240 if (false && prefix)
1241 context.filters.push(function ({ item }) item.type == "boolean" || prefix == "inv" && isArray(item.values));
1242 return completion.option(context, opt.scope, opt.name == "inv" ? opt.name : prefix);
1245 function error(length, message) {
1246 context.message = message;
1247 context.highlight(0, length, "SPELLCHECK");
1250 let option = opt.option;
1252 return error(opt.name.length, _("option.noSuch", opt.name));
1254 context.advance(context.filter.indexOf("="));
1255 if (option.type == "boolean")
1256 return error(context.filter.length, _("error.trailingCharacters"));
1260 return error(context.filter.length, opt.error);
1262 if (opt.get || opt.reset || !option || prefix)
1265 if (!opt.value && !opt.operator && !opt.invert) {
1266 context.fork("default", 0, this, function (context) {
1267 context.title = ["Extra Completions"];
1268 context.pushProcessor(0, function (item, text, next) next(item, text.substr(0, 100)));
1269 context.completions = [
1270 [option.stringValue, _("option.currentValue")],
1271 [option.stringDefaultValue, _("option.defaultValue")]
1272 ].filter(function (f) f[0] !== "");
1273 context.quote = ["", util.identity, ""];
1277 let optcontext = context.fork("values");
1278 modules.completion.optionValue(optcontext, opt.name, opt.operator);
1280 // Fill in the current values if we're removing
1281 if (opt.operator == "-" && isArray(opt.values)) {
1282 let have = Set([i.text for (i in values(context.allItems.items))]);
1283 context = context.fork("current-values", 0);
1284 context.anchored = optcontext.anchored;
1285 context.maxItems = optcontext.maxItems;
1287 context.filters.push(function (i) !Set.has(have, i.text));
1288 modules.completion.optionValue(context, opt.name, opt.operator, null,
1289 function (context) {
1290 context.generate = function () option.value.map(function (o) [o, ""]);
1292 context.title = ["Current values"];
1296 // TODO: deprecated. This needs to support "g:"-prefixed globals at a
1297 // minimum for now. The coderepos plugins make extensive use of global
1299 commands.add(["let"],
1300 "Set or list a variable",
1302 let globalVariables = dactyl._globalVariables;
1303 args = (args[0] || "").trim();
1304 function fmt(value) (typeof value == "number" ? "#" :
1305 typeof value == "function" ? "*" :
1307 if (!args || args == "g:") {
1311 template.map(globalVariables, function ([i, value]) {
1313 <td style="width: 200px;">{i}</td>
1314 <td>{fmt(value)}</td>
1319 if (str.text().length() == str.*.length())
1320 dactyl.echomsg(_("variable.none"));
1322 dactyl.echo(str, modules.commandline.FORCE_MULTILINE);
1326 let matches = args.match(/^([a-z]:)?([\w]+)(?:\s*([-+.])?=\s*(.*)?)?$/);
1328 let [, scope, name, op, expr] = matches;
1329 let fullName = (scope || "") + name;
1331 util.assert(scope == "g:" || scope == null,
1332 _("command.let.illegalVar", scope + name));
1333 util.assert(Set.has(globalVariables, name) || (expr && !op),
1334 _("command.let.undefinedVar", fullName));
1337 dactyl.echo(fullName + "\t\t" + fmt(globalVariables[name]));
1340 var newValue = dactyl.userEval(expr);
1343 util.assert(newValue !== undefined,
1344 _("command.let.invalidExpression", expr));
1346 let value = newValue;
1348 value = globalVariables[name];
1354 value += String(newValue);
1356 globalVariables[name] = value;
1360 dactyl.echoerr(_("command.let.unexpectedChar"));
1363 deprecated: "the options system",
1370 names: ["setl[ocal]"],
1371 description: "Set local option",
1372 modifiers: { scope: Option.SCOPE_LOCAL }
1375 names: ["setg[lobal]"],
1376 description: "Set global option",
1377 modifiers: { scope: Option.SCOPE_GLOBAL }
1381 description: "Set an option",
1384 serialize: function () [
1387 literalArg: [opt.type == "boolean" ? (opt.value ? "" : "no") + opt.name
1388 : opt.name + "=" + opt.stringValue]
1390 for (opt in modules.options)
1391 if (!opt.getter && !opt.isDefault && (opt.scope & Option.SCOPE_GLOBAL))
1395 ].forEach(function (params) {
1396 commands.add(params.names, params.description,
1397 function (args, modifiers) {
1398 setAction(args, update(modifiers, params.modifiers));
1402 completer: setCompleter,
1403 domains: function domains(args) array.flatten(args.map(function (spec) {
1405 let opt = modules.options.parseOpt(spec);
1406 if (opt.option && opt.option.domains)
1407 return opt.option.domains(opt.values);
1410 util.reportError(e);
1415 privateData: function privateData(args) args.some(function (spec) {
1416 let opt = modules.options.parseOpt(spec);
1417 return opt.option && opt.option.privateData &&
1418 (!callable(opt.option.privateData) ||
1419 opt.option.privateData(opt.values));
1421 }, params.extra || {}));
1424 // TODO: deprecated. This needs to support "g:"-prefixed globals at a
1426 commands.add(["unl[et]"],
1427 "Delete a variable",
1429 for (let [, name] in args) {
1430 name = name.replace(/^g:/, ""); // throw away the scope prefix
1431 if (!Set.has(dactyl._globalVariables, name)) {
1433 dactyl.echoerr(_("command.let.noSuch", name));
1437 delete dactyl._globalVariables[name];
1443 deprecated: "the options system"
1446 completion: function initCompletion(dactyl, modules, window) {
1447 const { completion } = modules;
1449 completion.option = function option(context, scope, prefix) {
1450 context.title = ["Option"];
1451 context.keys = { text: "names", description: "description" };
1452 context.anchored = false;
1453 context.completions = modules.options;
1454 if (prefix == "inv")
1455 context.keys.text = function (opt)
1456 opt.type == "boolean" || isArray(opt.value) ? opt.names.map(function (n) "inv" + n)
1459 context.filters.push(function ({ item }) item.scope & scope);
1462 completion.optionValue = function (context, name, op, curValue, completer) {
1463 let opt = modules.options.get(name);
1464 completer = completer || opt.completer;
1465 if (!completer || !opt)
1469 var curValues = curValue != null ? opt.parse(curValue) : opt.value;
1470 var newValues = opt.parse(context.filter);
1473 context.message = _("error.error", e);
1474 context.completions = [];
1484 newValues = Option.splitList(context.filter);
1489 Option._splitAt = newValues.length;
1494 let vals = Option.splitList(context.filter);
1495 let target = vals.pop() || "";
1497 let [count, key, quote] = Commands.parseArg(target, /:/, true);
1498 let split = Option._splitAt;
1500 extra.key = Option.dequote(key);
1501 extra.value = count < target.length ? Option.dequote(target.substr(count + 1)) : null;
1502 extra.values = opt.parse(vals.join(","));
1504 Option._splitAt = split + (extra.value == null ? 0 : count + 1);
1507 // TODO: Highlight when invalid
1508 context.advance(Option._splitAt);
1509 context.filter = Option.dequote(context.filter);
1512 if (isArray(opt.defaultValue)) {
1513 let val = array.nth(obj, function (re) re.key == extra.key, 0);
1514 return val && val.result;
1516 if (Set.has(opt.defaultValue, extra.key))
1517 return obj[extra.key];
1520 if (extra.key && extra.value != null) {
1521 context.fork("default", 0, this, function (context) {
1522 context.completions = [
1523 [val(opt.value), _("option.currentValue")],
1524 [val(opt.defaultValue), _("option.defaultValue")]
1525 ].filter(function (f) f[0] !== "" && f[0] != null);
1527 context = context.fork("stuff", 0);
1530 context.title = ["Option Value"];
1531 context.quote = Commands.complQuote[Option._quote] || Commands.complQuote[""];
1532 // Not Vim compatible, but is a significant enough improvement
1533 // that it's worth breaking compatibility.
1534 if (isArray(newValues)) {
1535 context.filters.push(function (i) newValues.indexOf(i.text) == -1);
1537 context.filters.push(function (i) curValues.indexOf(i.text) == -1);
1539 context.filters.push(function (i) curValues.indexOf(i.text) > -1);
1541 memoize(extra, "values", function () {
1543 return curValues.concat(newValues);
1545 return curValues.filter(function (v) newValues.indexOf(val) == -1);
1550 let res = completer.call(opt, context, extra);
1552 context.completions = res;
1555 javascript: function initJavascript(dactyl, modules, window) {
1556 const { options, JavaScript } = modules;
1557 JavaScript.setCompleter(Options.prototype.get, [function () ([o.name, o.description] for (o in options))]);
1559 sanitizer: function initSanitizer(dactyl, modules, window) {
1560 const { sanitizer } = modules;
1562 sanitizer.addItem("options", {
1563 description: "Options containing hostname data",
1564 action: function sanitize_action(timespan, host) {
1566 for (let opt in values(modules.options._options))
1567 if (timespan.contains(opt.lastSet * 1000) && opt.domains)
1569 opt.value = opt.filterDomain(host, opt.value);
1572 dactyl.reportError(e);
1575 privateEnter: function privateEnter() {
1576 for (let opt in values(modules.options._options))
1577 if (opt.privateData && (!callable(opt.privateData) || opt.privateData(opt.value)))
1578 opt.oldValue = opt.value;
1580 privateLeave: function privateLeave() {
1581 for (let opt in values(modules.options._options))
1582 if (opt.oldValue != null) {
1583 opt.value = opt.oldValue;
1584 opt.oldValue = null;
1593 } catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
1595 // vim: set fdm=marker sw=4 ts=4 et ft=javascript: