From 4211f6ee072b94b80e24cbcbd888b82dc858ed29 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sun, 6 Feb 2011 14:07:18 -0500 Subject: [PATCH] More groups work, including -args flag and proper serialization. --HG-- branch : groups --- common/content/autocommands.js | 7 +++++- common/content/commands.js | 8 +++++-- common/content/contexts.js | 42 ++++++++++++++++++++++++++++++---- common/content/mappings.js | 17 ++++++-------- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/common/content/autocommands.js b/common/content/autocommands.js index 76a45a41..3d1db6e2 100644 --- a/common/content/autocommands.js +++ b/common/content/autocommands.js @@ -146,7 +146,11 @@ var AutoCommands = Module("autocommands", { let uri = args.url ? util.newURI(args.url) : buffer.uri; event = event.toLowerCase(); - for (let hive in this.hives.iterValues()) + for (let hive in this.hives.iterValues()) { + let args = update({}, + hive.group.argsExtra(arguments[1]), + arguments[1]); + for (let autoCmd in values(hive._store)) if (autoCmd.eventName === event && autoCmd.filter(uri)) { if (!lastPattern || lastPattern !== String(autoCmd.filter)) @@ -157,6 +161,7 @@ var AutoCommands = Module("autocommands", { dactyl.trapErrors(autoCmd.command, autoCmd, args); } + } } }, { }, { diff --git a/common/content/commands.js b/common/content/commands.js index 8e577a9d..644a14a4 100644 --- a/common/content/commands.js +++ b/common/content/commands.js @@ -154,9 +154,12 @@ var Command = Class("Command", { throw FailedAssertion("E477: No ! allowed"); return !dactyl.trapErrors(function exec(command) { + // update({}, command.hive.group.argsExtra(args), args); + if (this.always) this.always(args, modifiers); - if (!contexts.context || !contexts.context.noExecute) + + if (!context || !context.noExecute) this.action(args, modifiers); }, this); }, @@ -569,12 +572,13 @@ var Commands = Module("commands", { string = string(tokens || {}); let lines = string.split(/\r\n|[\r\n]/); + let startLine = context.line; for (var i = 0; i < lines.length && !context.finished; i++) { // Deal with editors from Silly OSs. let line = lines[i].replace(/\r$/, ""); - context.line = context.line + i; + context.line = startLine + i; // Process escaped new lines while (i < lines.length && /^\s*\\/.test(lines[i + 1])) diff --git a/common/content/contexts.js b/common/content/contexts.js index 97e7f9d0..1233121d 100644 --- a/common/content/contexts.js +++ b/common/content/contexts.js @@ -9,7 +9,7 @@ var Group = Class("Group", { const self = this; this.name = name; this.description = description; - this.filter = filter || function (uri) true; + this.filter = filter || Group.defaultFilter; this.persist = persist || false; this.subGroups = []; }, @@ -20,6 +20,8 @@ var Group = Class("Group", { subGroup.cleanup(); }, + argsExtra: function argsExtra() ({}), + get toStringParams() [this.name], get builtin() contexts.builtinGroups.indexOf(this) >= 0, @@ -52,6 +54,8 @@ var Group = Class("Group", { groupsProto: {}, + defaultFilter: Class.memoize(function () this.compileFilter(["*"])), + subGroupMap: {}, SubGroup: Class("SubGroup", Class.Property, { @@ -91,7 +95,7 @@ var Contexts = Module("contexts", { this.groupMap = {}; this.subGroupProto = {}; - this.system = this.addGroup("builtin", "Builtin items"); + this.builtin = this.addGroup("builtin", "Builtin items"); this.user = this.addGroup("user", "User-defined items", null, true); this.builtinGroups = [this.system, this.user]; }, @@ -295,7 +299,7 @@ var Contexts = Module("contexts", { let filter = Group.compileFilter(args["-locations"]); if (!group) group = contexts.addGroup(name, args["-description"], filter, !args["-nopersist"]); - else { + else if (!group.builtin) { if (args.has("-locations")) group.filter = filter; if (args.has("-description")) @@ -304,6 +308,12 @@ var Contexts = Module("contexts", { group.persist = !args["-nopersist"] } + if (!group.builtin && args.has("-args")) { + group.argsExtra = contexts.bindMacro({ literalArg: "return " + args["-args"] }, + "-javascript", util.identity); + group.args = args["-args"]; + } + if (args.context) args.context.group = group; }, @@ -316,6 +326,11 @@ var Contexts = Module("contexts", { }, keepQuotes: true, options: [ + { + names: ["-args", "-a"], + description: "JavaScript Object which augments the arguments passed to commands, mappings, and autocommands", + type: CommandOption.STRING + }, { names: ["-description", "-desc", "-d"], description: "A description of this group", @@ -332,7 +347,26 @@ var Contexts = Module("contexts", { names: ["-nopersist", "-n"], description: "Do not save this group to an auto-generated RC file" } - ] + ], + serialGroup: 20, + serialize: function () [ + { + command: this.name, + bang: true, + options: iter([v, typeof group[k] == "boolean" ? null : group[k]] + // FIXME: this map is expressed multiple times + for ([k, v] in Iterator({ + args: "-args", + description: "-description", + filter: "-locations" + })) + if (group[k])).toObject(), + arguments: [group.name], + ignoreDefaults: true + } + for (group in values(contexts.activeGroups())) + if (!group.builtin && group.persist) + ].concat([{ command: this.name, arguments: ["user"] }]) }); commands.add(["delg[roup]"], diff --git a/common/content/mappings.js b/common/content/mappings.js index 7ca3b61e..e350a863 100644 --- a/common/content/mappings.js +++ b/common/content/mappings.js @@ -107,8 +107,9 @@ var Map = Class("Map", { .map(function ([i, prop]) [prop, this[i]], arguments) .toObject(); - if (!args.context) - args.context = contexts.context; + args = update({ context: contexts.context }, + this.hive.group.argsExtra(args), + args); let self = this; function repeat() self.action(args) @@ -531,20 +532,16 @@ var Mappings = Module("mappings", { serialize: function () { return this.name != "map" ? [] : array(mappings.userHives) - .filter(function (h) !h.noPersist) + .filter(function (h) h.group.persist) .map(function (hive) [ - { - command: "mapgroup", - bang: true, - arguments: [hive.name, String(hive.filter)].slice(0, hive.name == "user" ? 1 : 2) - } - ].concat([ { command: "map", options: array([ + hive !== mappings.user && ["-group", hive.group.name], ["-modes", uniqueModes(map.modes)], ["-description", map.description], map.silent && ["-silent"]]) + .filter(util.identity) .toObject(), arguments: [map.names[0]], @@ -553,7 +550,7 @@ var Mappings = Module("mappings", { } for (map in userMappings(hive)) if (map.persist) - ])) + ]) .flatten().array; } };