X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=common%2Fmodules%2Fcontexts.jsm;h=f5d976cae6dc030069d379599edd318ee66abf63;hb=70740024f9c028c1fd63e1a1850ab062ff956054;hp=15549e37cede5a2d10de6f1bb818571bd77bed00;hpb=eeed0be1a8abf7e3c97f43b63c1d595e940fef21;p=dactyl.git diff --git a/common/modules/contexts.jsm b/common/modules/contexts.jsm index 15549e3..f5d976c 100644 --- a/common/modules/contexts.jsm +++ b/common/modules/contexts.jsm @@ -109,6 +109,9 @@ var Contexts = Module("contexts", { completer: function (context) modules.completion.group(context) }); + + memoize(modules, "userContext", function () contexts.Context(modules.io.getRCFile("~", true), contexts.user, [modules, true])); + memoize(modules, "_userContext", function () contexts.Context(modules.io.getRCFile("~", true), contexts.user, [modules.userContext])); }, cleanup: function () { @@ -121,7 +124,7 @@ var Contexts = Module("contexts", { util.trapErrors("destroy", hive); for (let [name, plugin] in iter(this.modules.plugins.contexts)) - if (plugin && "onUnload" in plugin) + if (plugin && "onUnload" in plugin && callable(plugin.onUnload)) util.trapErrors("onUnload", plugin); }, @@ -159,7 +162,7 @@ var Contexts = Module("contexts", { { _hive: { value: name } }))); memoize(contexts.groupsProto, name, - function () [group[name] for (group in values(this.groups)) if (set.has(group, name))]); + function () [group[name] for (group in values(this.groups)) if (Set.has(group, name))]); }, get toStringParams() [this.name, this.Hive] @@ -177,17 +180,18 @@ var Contexts = Module("contexts", { 0); let contextPath = file.path; - let self = set.has(plugins, contextPath) && plugins.contexts[contextPath]; + let self = Set.has(plugins, contextPath) && plugins.contexts[contextPath]; if (self) { - if (set.has(self, "onUnload")) + if (Set.has(self, "onUnload")) self.onUnload(); } else { let name = isPlugin ? file.getRelativeDescriptor(isPlugin).replace(File.PATH_SEP, "-") : file.leafName; - self = update(newContext.apply(null, args || [userContext]), { + self = args && !isArray(args) ? args : newContext.apply(null, args || [userContext]); + update(self, { NAME: Const(name.replace(/\.[^.]*$/, "").replace(/-([a-z])/g, function (m, n1) n1.toUpperCase())), PATH: Const(file.path), @@ -212,10 +216,12 @@ var Contexts = Module("contexts", { contexts.removeGroup(this.GROUP); }) }); - Class.replaceProperty(plugins, file.path, self); + + if (group !== this.user) + Class.replaceProperty(plugins, file.path, self); // This belongs elsewhere - if (isPlugin && args) + if (isPlugin) Object.defineProperty(plugins, self.NAME, { configurable: true, enumerable: true, @@ -233,7 +239,7 @@ var Contexts = Module("contexts", { group = this.addGroup(commands.nameRegexp .iterate(name.replace(/\.[^.]*$/, "")) .join("-").replace(/--+/g, "-"), - "Script group for " + file.path, + _("context.scriptGroup", file.path), null, false); Class.replaceProperty(self, "GROUP", group); @@ -272,7 +278,7 @@ var Contexts = Module("contexts", { })), matchingGroups: function (uri) Object.create(this.groupsProto, { - groups: { value: this.activeGroups(uri) }, + groups: { value: this.activeGroups(uri) } }), activeGroups: function (uri, doc) { @@ -288,7 +294,7 @@ var Contexts = Module("contexts", { initializedGroups: function (hive) let (need = hive ? [hive] : Object.keys(this.hives)) - this.groupList.filter(function (group) need.some(set.has(group))), + this.groupList.filter(function (group) need.some(Set.has(group))), addGroup: function addGroup(name, description, filter, persist, replace) { let group = this.getGroup(name); @@ -307,7 +313,7 @@ var Contexts = Module("contexts", { if (description) group.description = description; if (filter) - group.filter = filter + group.filter = filter; group.persist = persist; } @@ -344,7 +350,7 @@ var Contexts = Module("contexts", { getGroup: function getGroup(name, hive) { if (name === "default") var group = this.context && this.context.context && this.context.context.GROUP; - else if (set.has(this.groupMap, name)) + else if (Set.has(this.groupMap, name)) group = this.groupMap[name]; if (group && hive) @@ -352,15 +358,35 @@ var Contexts = Module("contexts", { return group; }, + getDocs: function getDocs(context) { + try { + if (isinstance(context, ["Sandbox"])) { + let info = "INFO" in context && Cu.evalInSandbox("this.INFO instanceof XML && INFO.toXMLString()", context); + return info && XML(info); + } + if (typeof context.INFO == "xml") + return context.INFO; + } + catch (e) {} + return null; + }, + bindMacro: function (args, default_, params) { const { dactyl, events, modules } = this.modules; + function Proxy(obj, key) Class.Property({ + configurable: true, + enumerable: true, + get: function Proxy_get() process(obj[key]), + set: function Proxy_set(val) obj[key] = val + }) + let process = util.identity; if (callable(params)) var makeParams = function makeParams(self, args) - iter.toObject([k, process(v)] - for ([k, v] in iter(params.apply(self, args)))); + let (obj = params.apply(self, args)) + iter.toObject([k, Proxy(obj, k)] for (k in properties(obj))); else if (params) makeParams = function makeParams(self, args) iter.toObject([name, process(args[i])] @@ -368,6 +394,7 @@ var Contexts = Module("contexts", { let rhs = args.literalArg; let type = ["-builtin", "-ex", "-javascript", "-keys"].reduce(function (a, b) args[b] ? b : a, default_); + switch (type) { case "-builtin": let noremap = true; @@ -378,9 +405,10 @@ var Contexts = Module("contexts", { var action = function action() { events.feedkeys(action.macro(makeParams(this, arguments)), noremap, silent); - } + }; action.macro = util.compileMacro(rhs, true); break; + case "-ex": action = function action() modules.commands .execute(action.macro, makeParams(this, arguments), @@ -388,6 +416,7 @@ var Contexts = Module("contexts", { action.macro = util.compileMacro(rhs, true); action.context = this.context && update({}, this.context); break; + case "-javascript": if (callable(params)) action = dactyl.userEval("(function action() { with (action.makeParams(this, arguments)) {" + args.literalArg + "} })"); @@ -398,6 +427,7 @@ var Contexts = Module("contexts", { action.makeParams = makeParams; break; } + action.toString = function toString() (type === default_ ? "" : type + " ") + rhs; args = null; return action; @@ -466,9 +496,9 @@ var Contexts = Module("contexts", { if (args.has("-locations")) group.filter = filter; if (args.has("-description")) - group.description = args["-description"] + group.description = args["-description"]; if (args.has("-nopersist")) - group.persist = !args["-nopersist"] + group.persist = !args["-nopersist"]; } if (!group.builtin && args.has("-args")) { @@ -482,7 +512,7 @@ var Contexts = Module("contexts", { util.assert(!group.builtin || !["-description", "-locations", "-nopersist"] - .some(set.has(args.explicitOpts)), + .some(Set.has(args.explicitOpts)), _("group.cantModifyBuiltin")); }, { @@ -540,14 +570,23 @@ var Contexts = Module("contexts", { commands.add(["delg[roup]"], "Delete a group", function (args) { - util.assert(contexts.getGroup(args[0]), _("group.noSuch", args[0])); - contexts.removeGroup(args[0]); + util.assert(args.bang ^ !!args[0], _("error.argumentOrBang")); + + if (args.bang) + contexts.groupList = contexts.groupList.filter(function (g) g.builtin); + else { + util.assert(contexts.getGroup(args[0]), _("group.noSuch", args[0])); + contexts.removeGroup(args[0]); + } }, { - argCount: "1", + argCount: "?", + bang: true, completer: function (context, args) { - modules.completion.group(context); + if (args.bang) + return; context.filters.push(function ({ item }) !item.builtin); + modules.completion.group(context); } });