[Vimperator] patch autocommands

calmar mac at calmar.ws
Mon Dec 17 23:04:38 PST 2007


hi all,

initial patch for autocommands.

I'm wondering why there is the ba[ck] command in the diff?
I didn't add it really, did I? What made I wrong. maybe a new
fresh checkout and adding the autocommands stuff?

cheers
marco

PS: it won't work yet - that means there will be a popup - it
should 'execute' it so. Won't happen to know right now - and
didn't feel like figuring out :)

-- 
   (o_  It rocks: LINUX + Command-Line-Interface
   //\
   V_/_                     http://www.calmar.ws
-------------- next part --------------
? autocommands.patch
? patch.patch
Index: commands.js
===================================================================
RCS file: /cvs/vimperator/src/content/commands.js,v
retrieving revision 1.93
diff -u -r1.93 commands.js
--- commands.js	12 Dec 2007 13:53:50 -0000	1.93
+++ commands.js	18 Dec 2007 06:59:51 -0000
@@ -443,6 +443,62 @@
         throw StopIteration;
     }
 
+    function deleteAutoCommands(auEvent, regs) // args are filters
+    {
+        if (!auEvent && !regs)
+        {
+            vimperator.events.autoCommands = {}; // delete all TODO: rather delete.. or something?
+        }
+        else
+        {
+            for (var item in vimperator.events.autoCommands)
+            {
+                if (item == auEvent)
+                {
+                    delete vimperator.events.autoCommands[item];
+                }
+                else if (!auEvent) // delete: regs
+                {
+                    for (var i = 0; i < vimperator.events.autoCommands[item].length; i++)
+                        if (!regs || regs == vimperator.events.autoCommands[item][i][0])
+                            vimperator.events.autoCommands[item].splice(i, 1); // remove array
+                }
+            }
+        }
+    }
+    function printAutoCommands(auEvent, regs) // args are filters
+    {
+        var flag;
+        var list = "<table><tr><td style='font-weight: bold;'  colspan='2'>---- Auto-Commands ----</td></tr>";
+        for (var item in vimperator.events.autoCommands)
+        {
+            flag = true;
+            if (!auEvent || item == auEvent) // filter event
+            {
+                for (var i = 0; i < vimperator.events.autoCommands[item].length; i++)
+                {
+                    if (!regs || regs == vimperator.events.autoCommands[item][i][0]) // filter regs
+                    {
+                        if (flag == true)
+                        {
+                            list += "<tr><td style='font-weight: bold;'  colspan='2'>" + 
+                                    vimperator.util.escapeHTML(item) + "</td></tr>";
+                            flag = false;
+                        }
+
+                        list += "<tr>";
+                        list += "<td> &nbsp; " + vimperator.util.escapeHTML(vimperator.events.autoCommands[item][i][0]) + "</td>";
+                        list += "<td>" + vimperator.util.escapeHTML(vimperator.events.autoCommands[item][i][1]) + "</td>";
+                        list += "</tr>";
+                    }
+                }
+            }
+        }
+
+        list += "</table>";
+        vimperator.commandline.echo(list, vimperator.commandline.HL_NORMAL, vimperator.commandline.FORCE_MULTILINE);
+    }
+
     /////////////////////////////////////////////////////////////////////////////}}}
     ////////////////////// PUBLIC SECTION //////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////{{{
@@ -573,6 +629,108 @@
             }
         }
     ));
+    commandManager.add(new vimperator.Command(["au[tocmd]"],
+        function (args, special) 
+        {  
+            if (!args)
+            {
+                if (special) // :au!
+                    deleteAutoCommands(null, null);
+                else // :au
+                    printAutoCommands(null, null);
+            } 
+            else
+            {
+                // (?:  ) means don't store; (....)? <-> exclamation marks makes the group optional
+                var [all, asterix, auEvent, regs, cmds] =  args.match(/^(\*)?(?:\s+)?(\S+)(?:\s+)?(\S+)?(?:\s+)?(.+)?$/);
+
+                if (cmds)
+                {
+                    var eventsIter = auEvent.split(",") 
+                    for (var i = 0; i < eventsIter.length; i++)
+                    {
+                        if (!vimperator.events.autoCommands[eventsIter[i]])
+                            vimperator.events.autoCommands[eventsIter[i]] = [];
+
+                        vimperator.events.autoCommands[eventsIter[i]].push([regs, cmds]);
+                    }
+                }
+                else if (regs)
+                {
+                    if (special)
+                        deletAutoCommands(auEvent, regs);
+                    else
+                        printAutoCommands(auEvent, regs);
+                }
+                else if (auEvent)
+                {
+                    if (asterix)
+                        if (special)
+                            deleteAutoCommands(null, auEvent); // auEvent is a regex with a * (asterix)
+                        else
+                            printAutoCommands(null, auEvent);
+                    else
+                        if (special)
+                            deleteAutoCommands(auEvent, null);
+                        else
+                            printAutoCommands(auEvent, null);
+                }
+            }
+        // TODO: completer function for events?
+        },
+        {
+            shortHelp: "Execute commands automatically on events",
+            help: ":au[tocmd] {event} {pat} {cmd}<br/>" +
+                  "Add {cmd} to the list of commands Vimperator will execute on {event}<br/><br/>" +
+                  "autocmd[!] {events} {pat}   list/remove autocommands filtered be {events} and {pat}<br/>" +
+                  "autocmd[!] {events}         list/remove autocommands matching {events}<br/>" +  
+                  "autocmd[!] * {pat}          list/remove autocommands matching {pat}<br/>" +  
+                  "autocmd[!]                  list/remove all autocommands"  
+        }
+    ));
+    commandManager.add(new vimperator.Command(["ba[ck]"],
+        function (args, special, count)
+        {
+            if (special)
+                vimperator.history.goToStart();
+            else
+            {
+                if (args)
+                {
+                    var sh = getWebNavigation().sessionHistory;
+                    for (var i = sh.index - 1; i >= 0; i--)
+                    {
+                        if (sh.getEntryAtIndex(i, false).URI.spec == args)
+                        {
+                            getWebNavigation().gotoIndex(i);
+                            return;
+                        }
+                    }
+                }
+                vimperator.history.stepTo(count > 0 ? -1 * count : -1);
+            }
+        },
+        {
+            usage: ["[count]ba[ck][!] [url]"],
+            shortHelp: "Go back in the browser history",
+            help: "Count is supported, <code class=\"command\">:3back</code> goes back 3 pages in the browser history.<br/>" +
+                  "The special version <code class=\"command\">:back!</code> goes to the beginning of the browser history.",
+            completer: function (filter)
+            {
+                var sh = getWebNavigation().sessionHistory;
+                var completions = [];
+                for (var i = sh.index - 1; i >= 0; i--)
+                {
+                    var entry = sh.getEntryAtIndex(i, false);
+                    var url = entry.URI.spec;
+                    var title = entry.title;
+                    if (vimperator.completion.match([url, title], filter, false))
+                        completions.push([url, title]);
+                }
+                return [0, completions];
+            }
+        }
+    ));
     commandManager.add(new vimperator.Command(["bd[elete]", "bw[ipeout]", "bun[load]", "tabc[lose]"],
         function (args, special, count) { vimperator.tabs.remove(getBrowser().mCurrentTab, count > 0 ? count : 1, special, 0); },
         {
@@ -1498,6 +1656,13 @@
             for (var abbrCmd in vimperator.editor.abbreviations)
                 line += abbrCmd;
 
+            line += "\n\" Auto-Commands\n";
+            for (var item in vimperator.events.autoCommands)
+                    for (var i = 0; i < vimperator.events.autoCommands[item].length; i++)
+                        line += "autocmd " + item + " " + vimperator.events.autoCommands[item][i][0]  + 
+                                             " " + vimperator.events.autoCommands[item][i][1] + "\n";
+
+
             // if (vimperator.events.getMapLeader() != "\\")
             //    line += "\nlet mapleader = \"" + vimperator.events.getMapLeader() + "\"\n";
 
Index: events.js
===================================================================
RCS file: /cvs/vimperator/src/content/events.js,v
retrieving revision 1.42
diff -u -r1.42 events.js
--- events.js	17 Dec 2007 20:45:29 -0000	1.42
+++ events.js	18 Dec 2007 06:59:52 -0000
@@ -223,9 +223,24 @@
             // code which should happen for all (also background) newly loaded tabs goes here:
             vimperator.buffer.updateBufferList();
 
-            //update history
+
             var url = vimperator.buffer.URL;
             var title = vimperator.buffer.title;
+
+            if (vimperator.events.autoCommands["onPageLoad"]) // XXX: or this.autoCommands[...] ?
+            {
+                for (var i = 0; i < vimperator.events.autoCommands["onPageLoad"].length; i++)
+                {
+                    var regex = new RegExp(vimperator.events.autoCommands["onPageLoad"][i][0]);
+                    if (regex.test(url))
+                    {
+                        alert("events.js:237 " + vimperator.events.autoCommands["onPageLoad"][i][1]); //TODO: make it real
+                    }
+                }
+
+            }
+
+            //update history
             vimperator.history.add(url, title);
 
             // code which is only relevant if the page load is the current tab goes here:
@@ -259,6 +274,8 @@
 
     var eventManager = {
 
+        autoCommands:  {}, // keeps autocommands
+
         wantsModeReset: true, // used in onFocusChange since Firefox is so buggy here
 
         startRecording: function (macro)


More information about the Vimperator mailing list