From 3169a93825749b271849784dc5d52e722237761a Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Tue, 7 Oct 2008 03:19:40 +0000 Subject: [PATCH] Add Option.has(). Improve JS completion. --- content/completion.js | 3 ++- content/events.js | 2 +- content/liberator.js | 15 +++++------ content/options.js | 58 +++++++++++++++++++++---------------------- content/ui.js | 4 +-- content/vimperator.js | 4 +-- 6 files changed, 42 insertions(+), 44 deletions(-) diff --git a/content/completion.js b/content/completion.js index 096fe02c..67f2e754 100644 --- a/content/completion.js +++ b/content/completion.js @@ -308,6 +308,7 @@ liberator.Completion = function () //{{{ { try { + liberator.dump("eval(" + liberator.util.escapeString(arg) + ")"); return window.eval(arg); } catch(e) {} @@ -345,7 +346,7 @@ liberator.Completion = function () //{{{ if (["string", "number", "boolean"].indexOf(type) > -1) type += ": " + String(v).replace("\n", "\\n", "g"); if (type == "function") - type += ": " + v.toSource.replace(/\{.*/, "{ ... }"); + type += ": " + String(v).replace(/{(.|\n)*/, "{ ... }"); compl.push([k, type]); } diff --git a/content/events.js b/content/events.js index 5641ef8b..367c5648 100644 --- a/content/events.js +++ b/content/events.js @@ -253,7 +253,7 @@ liberator.AutoCommands = function () //{{{ { let events = liberator.options["eventignore"].split(","); - if (events.some(function (e) e == "all" || e == event)) + if (liberator.options.get("eventignore").has("all", event)) return; let autoCmds = store.filter(function (autoCmd) autoCmd.event == event); diff --git a/content/liberator.js b/content/liberator.js index ce13eb27..d064efcd 100644 --- a/content/liberator.js +++ b/content/liberator.js @@ -155,9 +155,8 @@ const liberator = (function () //{{{ function () { liberator.open("chrome://mozapps/content/extensions/extensions.xul", - (liberator.options["newtab"] && - (liberator.options["newtab"] == "all" || liberator.options["newtab"].split(",").indexOf("addons") != -1)) ? - liberator.NEW_TAB: liberator.CURRENT_TAB); + (liberator.options["newtab"] && liberator.options.get("newtab").has("all", "addons")) + ? liberator.NEW_TAB: liberator.CURRENT_TAB); }, { argCount: "0" }); @@ -329,9 +328,8 @@ const liberator = (function () //{{{ if (special) // open javascript console { liberator.open("chrome://global/content/console.xul", - (liberator.options["newtab"] && - (liberator.options["newtab"] == "all" || liberator.options["newtab"].split(",").indexOf("javascript") != -1)) ? - liberator.NEW_TAB : liberator.CURRENT_TAB); + (liberator.options["newtab"] && liberator.options.get("newtab").has("all", "javascript")) + ? liberator.NEW_TAB : liberator.CURRENT_TAB); } else { @@ -889,9 +887,8 @@ const liberator = (function () //{{{ help: function (topic) { - var where = (liberator.options["newtab"] && (liberator.options["newtab"] == "all" || - liberator.options["newtab"].split(",").indexOf("help") != -1)) ? - liberator.NEW_TAB : liberator.CURRENT_TAB; + var where = (liberator.options["newtab"] && liberator.options.get("newtab").has("all", "help")) + ? liberator.NEW_TAB : liberator.CURRENT_TAB; if (!topic) { diff --git a/content/options.js b/content/options.js index d4904ef4..cb00a97e 100644 --- a/content/options.js +++ b/content/options.js @@ -100,15 +100,9 @@ liberator.Option = function (names, description, type, defaultValue, extraInfo) this.set = function (newValue, scope) { - if (scope) - { - if ((scope & this.scope) == 0) // option doesn't exist in this scope - return null; - } - else - { - scope = this.scope; - } + scope = scope || this.scope; + if ((scope & this.scope) == 0) // option doesn't exist in this scope + return null; if (this.setter) { @@ -130,8 +124,16 @@ liberator.Option = function (names, description, type, defaultValue, extraInfo) this.hasChanged = true; }; - this.__defineGetter__("value", this.get); + this.has = function () + { + let value = this.value; + if (this.type == "stringlist") + value = this.value.split(","); + /* Return the number of matching arguments. */ + return Array.reduce(arguments, function (n, val) value.indexOf(val) >= 0, 0); + }; + this.__defineGetter__("value", this.get); this.__defineSetter__("value", this.set); this.hasName = function (name) @@ -162,7 +164,7 @@ liberator.Options = function () //{{{ var prefService = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefBranch); - var options = []; + var options = {}; function optionObserver(key, event, option) { @@ -368,9 +370,8 @@ liberator.Options = function () //{{{ if (special) // open Firefox settings GUI dialog { liberator.open("about:config", - (liberator.options.newtab && - (liberator.options.newtab == "all" || liberator.options.newtab.split(",").indexOf("prefs") != -1)) ? - liberator.NEW_TAB : liberator.CURRENT_TAB); + (liberator.options["newtab"] && liberator.options.get("newtab").has("all", "prefs")) + ? liberator.NEW_TAB : liberator.CURRENT_TAB); } else { @@ -881,9 +882,9 @@ liberator.Options = function () //{{{ __iterator__: function () { - let sorted = options.sort(function (opt1, opt2) opt1.name > opt2.name); - for (let i = 0; i < sorted.length; i++) - yield sorted[i]; + let sorted = [o for ([,o] in Iterator(options))] + .sort(function (a, b) String.localeCompare(a.name, b.name)); + return (v for ([k, v] in Iterator(sorted))); }, add: function (names, description, type, defaultValue, extraInfo) @@ -896,22 +897,18 @@ liberator.Options = function () //{{{ if (!option) return false; - for (let opt in Iterator(this)) + if (option.name in options) { - if (opt.name == option.name) - { - // never replace for now - liberator.log("Warning: '" + names[0] + "' already exists, NOT replacing existing option.", 1); - return false; - } + // never replace for now + liberator.log("Warning: '" + names[0] + "' already exists, NOT replacing existing option.", 1); + return false; } // quickly access options with liberator.options["wildmode"]: this.__defineGetter__(option.name, function () option.value); this.__defineSetter__(option.name, function (value) { option.value = value; }); - // TODO: sort option - options.push(option); + options[option.name] = option; return true; }, @@ -920,10 +917,13 @@ liberator.Options = function () //{{{ if (!scope) scope = liberator.options.OPTION_SCOPE_BOTH; - for (let i = 0; i < options.length; i++) + if (name in options && (options[name].scope & scope)) + return options[name]; + + for (let [,opt] in Iterator(options)) { - if (options[i].hasName(name) && (options[i].scope & scope)) - return options[i]; + if (opt.hasName(name) && (opt.scope & scope)) + return opt; } return null; diff --git a/content/ui.js b/content/ui.js index 74e4ab8f..c442f6b0 100644 --- a/content/ui.js +++ b/content/ui.js @@ -144,7 +144,7 @@ liberator.CommandLine = function () //{{{ var promptCompleter = null; liberator.registerCallback("change", liberator.modes.EX, function (command) { - if (liberator.options["wildoptions"].indexOf("auto") >= 0) + if (liberator.options.get("wildoptions").has("auto")) autocompleteTimer.tell(command); else completionIndex = UNINITIALIZED; @@ -570,7 +570,7 @@ liberator.CommandLine = function () //{{{ // open the completion list automatically if wanted if (/\s/.test(cmd) && - liberator.options["wildoptions"].indexOf("auto") >= 0 && + liberator.options.get("wildoptions").has("auto") >= 0 && extendedMode == liberator.modes.EX) { var [start, compl] = liberator.completion.ex(cmd); diff --git a/content/vimperator.js b/content/vimperator.js index a0b6d87d..08169b21 100644 --- a/content/vimperator.js +++ b/content/vimperator.js @@ -261,8 +261,8 @@ liberator.config = { //{{{ function () { liberator.open("chrome://mozapps/content/downloads/downloads.xul", - (liberator.options["newtab"] == "all" || liberator.options["newtab"].split(",").indexOf("downloads") != -1) ? - liberator.NEW_TAB : liberator.CURRENT_TAB); + liberator.options.get("newtab").has("all", "downloads") + ? liberator.NEW_TAB : liberator.CURRENT_TAB); }, { argCount: "0" });