]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/content/autocommands.js
Import r6948 from upstream hg supporting Firefox up to 24.*
[dactyl.git] / common / content / autocommands.js
index 2d960c835be66b75f59a9c4fb274eea449fe38d3..eaaa1c785e1613b2c04ab535fc1f34d5d7f2d19f 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
-// Copyright (c) 2008-2011 by Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2008-2012 Kris Maglione <maglione.k@gmail.com>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
 
 var AutoCommand = Struct("event", "filter", "command");
 update(AutoCommand.prototype, {
-    eventName: Class.memoize(function () this.event.toLowerCase()),
+    eventName: Class.Memoize(function () this.event.toLowerCase()),
 
     match: function (event, pattern) {
-        return (!event || this.eventName == event.toLowerCase()) && (!pattern || String(this.filter) === pattern);
+        return (!event || this.eventName == event.toLowerCase()) && (!pattern || String(this.filter) === String(pattern));
     }
 });
 
@@ -43,24 +43,26 @@ var AutoCmdHive = Class("AutoCmdHive", Contexts.Hive, {
     },
 
     /**
-     * Returns all autocommands with a matching *event* and *regexp*.
+     * Returns all autocommands with a matching *event* and *filter*.
      *
      * @param {string} event The event name filter.
-     * @param {string} pattern The URL pattern filter.
-     * @returns {AutoCommand[]}
+     * @param {string} filter The URL pattern filter.
+     * @returns {[AutoCommand]}
      */
-    get: function (event, pattern) {
-        return this._store.filter(function (autoCmd) autoCmd.match(event, regexp));
+    get: function (event, filter) {
+        filter = filter && String(Group.compileFilter(filter));
+        return this._store.filter(function (autoCmd) autoCmd.match(event, filter));
     },
 
     /**
-     * Deletes all autocommands with a matching *event* and *regexp*.
+     * Deletes all autocommands with a matching *event* and *filter*.
      *
      * @param {string} event The event name filter.
-     * @param {string} regexp The URL pattern filter.
+     * @param {string} filter The URL pattern filter.
      */
-    remove: function (event, regexp) {
-        this._store = this._store.filter(function (autoCmd) !autoCmd.match(event, regexp));
+    remove: function (event, filter) {
+        filter = filter && String(Group.compileFilter(filter));
+        this._store = this._store.filter(function (autoCmd) !autoCmd.match(event, filter));
     },
 });
 
@@ -69,12 +71,6 @@ var AutoCmdHive = Class("AutoCmdHive", Contexts.Hive, {
  */
 var AutoCommands = Module("autocommands", {
     init: function () {
-        update(this, {
-            hives: contexts.Hives("autocmd", AutoCmdHive),
-            user: contexts.hives.autocmd.user,
-            allHives: contexts.allGroups.autocmd,
-            matchingHives: function matchingHives(uri, doc) contexts.matchingGroups(uri, doc).autocmd
-        });
     },
 
     get activeHives() contexts.allGroups.autocmd.filter(function (h) h._store.length),
@@ -84,12 +80,17 @@ var AutoCommands = Module("autocommands", {
     remove: deprecated("group.autocmd.remove", { get: function remove() autocommands.user.closure.remove }),
 
     /**
-     * Lists all autocommands with a matching *event* and *regexp*.
+     * Lists all autocommands with a matching *event*, *regexp* and optionally
+     * *hives*.
      *
      * @param {string} event The event name filter.
      * @param {string} regexp The URL pattern filter.
+     * @param {[Hive]} hives List of hives.
+     * @optional
      */
-    list: function (event, regexp) {
+    list: function (event, regexp, hives) {
+
+        let hives = hives || this.activeHives;
 
         function cmds(hive) {
             let cmds = {};
@@ -102,29 +103,28 @@ var AutoCommands = Module("autocommands", {
             return cmds;
         }
 
-        commandline.commandOutput(
-            <table>
-                <tr highlight="Title">
-                    <td colspan="3">----- Auto Commands -----</td>
-                </tr>
-                {
-                    template.map(this.activeHives, function (hive)
-                        <tr highlight="Title">
-                            <td colspan="3">{hive.name}</td>
-                        </tr> +
-                        <tr style="height: .5ex;"/> +
-                        template.map(cmds(hive), function ([event, items])
-                            <tr style="height: .5ex;"/> +
-                            template.map(items, function (item, i)
-                                <tr>
-                                    <td highlight="Title" style="padding-right: 1em;">{i == 0 ? event : ""}</td>
-                                    <td>{item.filter.toXML ? item.filter.toXML() : item.filter}</td>
-                                    <td>{item.command}</td>
-                                </tr>) +
-                            <tr style="height: .5ex;"/>) +
-                        <tr style="height: .5ex;"/>)
-                }
-            </table>);
+        let table = (
+            ["table", {},
+                ["tr", { highlight: "Title" },
+                    ["td", { colspan: "3" }, "----- Auto Commands -----"]],
+                hives.map(function (hive) [
+                    ["tr", {},
+                        ["td", { colspan: "3" },
+                            ["span", { highlight: "Title" }, hive.name],
+                            " ", hive.filter.toJSONXML(modules)]],
+                    ["tr", { style: "height: .5ex;" }],
+                    iter(cmds(hive)).map(function ([event, items]) [
+                        ["tr", { style: "height: .5ex;" }],
+                        items.map(function (item, i)
+                            ["tr", {},
+                                ["td", { highlight: "Title", style: "padding-left: 1em; padding-right: 1em;" },
+                                    i == 0 ? event : ""],
+                                ["td", {}, item.filter.toJSONXML ? item.filter.toJSONXML(modules) : String(item.filter)],
+                                ["td", {}, String(item.command)]]),
+                        ["tr", { style: "height: .5ex;" }]]).toArray(),
+                    ["tr", { style: "height: .5ex;" }]
+                ])]);
+        commandline.commandOutput(table);
     },
 
     /**
@@ -143,15 +143,13 @@ var AutoCommands = Module("autocommands", {
         let lastPattern = null;
         var { url, doc } = args;
         if (url)
-            uri = util.newURI(url);
+            uri = util.createURI(url);
         else
             var { uri, doc } = buffer;
 
         event = event.toLowerCase();
         for (let hive in values(this.matchingHives(uri, doc))) {
-            let args = update({},
-                              hive.argsExtra(arguments[1]),
-                              arguments[1]);
+            let args = hive.makeArgs(doc, null, arguments[1]);
 
             for (let autoCmd in values(hive._store))
                 if (autoCmd.eventName === event && autoCmd.filter(uri, doc)) {
@@ -167,11 +165,19 @@ var AutoCommands = Module("autocommands", {
     }
 }, {
 }, {
-    commands: function () {
+    contexts: function initContexts() {
+        update(AutoCommands.prototype, {
+            hives: contexts.Hives("autocmd", AutoCmdHive),
+            user: contexts.hives.autocmd.user,
+            allHives: contexts.allGroups.autocmd,
+            matchingHives: function matchingHives(uri, doc) contexts.matchingGroups(uri, doc).autocmd
+        });
+    },
+    commands: function initCommands() {
         commands.add(["au[tocmd]"],
             "Execute commands automatically on events",
             function (args) {
-                let [event, regexp, cmd] = args;
+                let [event, filter, cmd] = args;
                 let events = [];
 
                 if (event) {
@@ -186,9 +192,9 @@ var AutoCommands = Module("autocommands", {
 
                 if (args.length > 2) { // add new command, possibly removing all others with the same event/pattern
                     if (args.bang)
-                        args["-group"].remove(event, regexp);
+                        args["-group"].remove(event, filter);
                     cmd = contexts.bindMacro(args, "-ex", function (params) params);
-                    args["-group"].add(events, regexp, cmd);
+                    args["-group"].add(events, filter, cmd);
                 }
                 else {
                     if (event == "*")
@@ -197,10 +203,10 @@ var AutoCommands = Module("autocommands", {
                     if (args.bang) {
                         // TODO: "*" only appears to work in Vim when there is a {group} specified
                         if (args[0] != "*" || args.length > 1)
-                            args["-group"].remove(event, regexp); // remove all
+                            args["-group"].remove(event, filter); // remove all
                     }
                     else
-                        autocommands.list(event, regexp); // list all
+                        autocommands.list(event, filter, args.explicitOpts["-group"] ? [args["-group"]] : null); // list all
                 }
             }, {
                 bang: true,
@@ -240,7 +246,7 @@ var AutoCommands = Module("autocommands", {
                         return void dactyl.echomsg(_("autocmd.noMatching"));
 
                     let [event, url] = args;
-                    let defaultURL = url || buffer.uri.spec;
+                    let uri = util.createURI(url) || buffer.uri;
                     let validEvents = Object.keys(config.autocommands);
 
                     // TODO: add command validators
@@ -248,7 +254,7 @@ var AutoCommands = Module("autocommands", {
                                   _("autocmd.cantExecuteAll"));
                     dactyl.assert(validEvents.indexOf(event) >= 0,
                                   _("autocmd.noGroup", args));
-                    dactyl.assert(autocommands.get(event).some(function (c) c.patterns.some(function (re) re.test(defaultURL) ^ !re.result)),
+                    dactyl.assert(autocommands.get(event).some(function (c) c.filter(uri)),
                                   _("autocmd.noMatching"));
 
                     if (this.name == "doautoall" && dactyl.has("tabs")) {
@@ -257,13 +263,13 @@ var AutoCommands = Module("autocommands", {
                         for (let i = 0; i < tabs.count; i++) {
                             tabs.select(i);
                             // if no url arg is specified use the current buffer's URL
-                            autocommands.trigger(event, { url: url || buffer.uri.spec });
+                            autocommands.trigger(event, { url: uri.spec });
                         }
 
                         tabs.select(current);
                     }
                     else
-                        autocommands.trigger(event, { url: defaultURL });
+                        autocommands.trigger(event, { url: uri.spec });
                 }, {
                     argCount: "*", // FIXME: kludged for proper error message should be "1".
                     completer: function (context) completion.autocmdEvent(context),
@@ -271,15 +277,15 @@ var AutoCommands = Module("autocommands", {
                 });
         });
     },
-    completion: function () {
+    completion: function initCompletion() {
         completion.autocmdEvent = function autocmdEvent(context) {
             context.completions = Iterator(config.autocommands);
         };
     },
-    javascript: function () {
-        JavaScript.setCompleter(autocommands.user.get, [function () Iterator(config.autocommands)]);
+    javascript: function initJavascript() {
+        JavaScript.setCompleter(AutoCmdHive.prototype.get, [function () Iterator(config.autocommands)]);
     },
-    options: function () {
+    options: function initOptions() {
         options.add(["eventignore", "ei"],
             "List of autocommand event names which should be ignored",
             "stringlist", "",
@@ -290,4 +296,4 @@ var AutoCommands = Module("autocommands", {
     }
 });
 
-// vim: set fdm=marker sw=4 ts=4 et:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et: