]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/content/autocommands.js
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / common / content / autocommands.js
index beb8fc52e2a199bd5fefab0ed1fa3f1e2d452679..31c3ad90826bcae04fddd387b0c39182311fb596 100644 (file)
@@ -4,16 +4,16 @@
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-"use strict";
+/* use strict */
 
 /** @scope modules */
 
 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.
+     * @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),
@@ -107,6 +103,7 @@ var AutoCommands = Module("autocommands", {
             return cmds;
         }
 
+        XML.prettyPrinting = XML.ignoreWhitespace = false;
         commandline.commandOutput(
             <table>
                 <tr highlight="Title">
@@ -114,15 +111,16 @@ var AutoCommands = Module("autocommands", {
                 </tr>
                 {
                     template.map(hives, function (hive)
-                        <tr highlight="Title">
-                            <td colspan="3">{hive.name}</td>
+                        <tr>
+                            <td colspan="3"><span highlight="Title">{hive.name}</span>
+                                            {hive.filter}</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 highlight="Title" style="padding-left: 1em; padding-right: 1em;">{i == 0 ? event : ""}</td>
                                     <td>{item.filter.toXML ? item.filter.toXML() : item.filter}</td>
                                     <td>{item.command}</td>
                                 </tr>) +
@@ -154,9 +152,7 @@ var AutoCommands = Module("autocommands", {
 
         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)) {
@@ -172,11 +168,19 @@ var AutoCommands = Module("autocommands", {
     }
 }, {
 }, {
+    contexts: function () {
+        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 () {
         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) {
@@ -191,9 +195,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 == "*")
@@ -202,10 +206,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, args.explicitOpts["-group"] ? [args["-group"]] : null); // list all
+                        autocommands.list(event, filter, args.explicitOpts["-group"] ? [args["-group"]] : null); // list all
                 }
             }, {
                 bang: true,
@@ -282,7 +286,7 @@ var AutoCommands = Module("autocommands", {
         };
     },
     javascript: function () {
-        JavaScript.setCompleter(autocommands.user.get, [function () Iterator(config.autocommands)]);
+        JavaScript.setCompleter(AutoCmdHive.prototype.get, [function () Iterator(config.autocommands)]);
     },
     options: function () {
         options.add(["eventignore", "ei"],