[Vimperator] patch autocommands
calmar
mac at calmar.ws
Thu Dec 27 07:38:51 PST 2007
On Tue, Dec 18, 2007 at 04:18:40PM +0100, Martin Stubenschrott wrote:
Hi Martin,
seems to work about..
the completion function works only on the initial event (but
better than nothing I think. Maybe checking of the filter consist
of more than one word or so? If it's the first word, complete,
else do nothing?)
anyway, the next thing is allowing hintchars etc.. again. Any last
hint about it?
I'm actually thinking about tweak it into the current hint - if
not too complicate that is.. else as plugin or something..
cheers
marco
--
(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
-------------- next part --------------
? auto.diff
? 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 27 Dec 2007 15:32:17 -0000
@@ -1293,6 +1293,58 @@
function (args) { vimperator.editor.removeAllAbbreviations("i"); },
{ shortHelp: "Remove all abbreviations for Insert mode" }
));
+ commandManager.add(new vimperator.Command(["au[tocmd]"],
+ function (args, special)
+ {
+ if (!args)
+ {
+ if (special) // :au!
+ vimperator.autocommands.remove(null, null);
+ else // :au
+ vimperator.autocommands.list(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)
+ {
+ vimperator.autocommands.add(auEvent, regs, cmds);
+ }
+ else if (regs) // e.g. no cmds provide
+ {
+ if (special)
+ vimperator.autocommands.remove(auEvent, regs);
+ else
+ vimperator.autocommands.list(auEvent, regs);
+ }
+ else if (auEvent)
+ {
+ if (asterix)
+ if (special)
+ vimperator.autocommands.remove(null, auEvent); // ':au! * auEvent'
+ else
+ vimperator.autocommands.list(null, auEvent);
+ else
+ if (special)
+ vimperator.autocommands.remove(auEvent, null);
+ else
+ vimperator.autocommands.list(auEvent, null);
+ }
+ }
+ },
+ {
+ 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",
+ completer: function (filter) { return vimperator.completion.autocommands(filter); }
+ }
+ ));
// 0 args -> list all maps
// 1 arg -> list the maps starting with args
// 2 args -> map arg1 to arg*
@@ -1494,6 +1546,10 @@
}
}
+ line += "\n\" Auto-Commands\n";
+ for (var item in vimperator.autocommands)
+ line += "autocmd " + item + "\n";
+
line += "\n\" Abbreviations\n";
for (var abbrCmd in vimperator.editor.abbreviations)
line += abbrCmd;
Index: completion.js
===================================================================
RCS file: /cvs/vimperator/src/content/completion.js,v
retrieving revision 1.28
diff -u -r1.28 completion.js
--- completion.js 4 Dec 2007 08:21:38 -0000 1.28
+++ completion.js 27 Dec 2007 15:32:18 -0000
@@ -129,7 +129,22 @@
}
return longest;
},
+ autocommands: function (filter)
+ {
+ substrings = [];
+ var nodes = [
+ ["onPageLoad", "when a page gets (re)loaded/opened"]
+ ];
+
+ if (!filter)
+ return [0, nodes];
+ var mapped = nodes.map(function (node) {
+ return [[node[0]], node[1]];
+ });
+
+ return [0, buildLongestCommonSubstring(mapped, filter)];
+ },
dialog: function (filter)
{
substrings = [];
Index: events.js
===================================================================
RCS file: /cvs/vimperator/src/content/events.js,v
retrieving revision 1.44
diff -u -r1.44 events.js
--- events.js 20 Dec 2007 15:19:09 -0000 1.44
+++ events.js 27 Dec 2007 15:32:19 -0000
@@ -26,6 +26,121 @@
the terms of any one of the MPL, the GPL or the LGPL.
}}} ***** END LICENSE BLOCK *****/
+vimperator.AutoCommands = function()
+{
+ ////////////////////////////////////////////////////////////////////////////////
+ ////////////////////// PRIVATE SECTION /////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////////////////{{{
+
+ var autoCommands = {};
+
+ function autoCommandsInterator()
+ {
+ for (var item in autoCommands)
+ for (var i = 0; i < autoCommands[item].length; i++)
+ yield item + " " + autoCommands[item][i][0] + " " + autoCommands[item][i][1];
+ throw StopIteration;
+ }
+
+ /////////////////////////////////////////////////////////////////////////////}}}
+ ////////////////////// PUBLIC SECTION //////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////////////////{{{
+
+ //TODO: maybe this.function rather than v.autocommands.function...
+
+ return {
+
+ __iterator__: function ()
+ {
+ return autoCommandsInterator();
+ },
+
+ remove: function (auEvent, regs) // args are filters
+ {
+ if (!auEvent && !regs)
+ {
+ autoCommands = {}; // delete all TODO: rather delete.. or something?
+ }
+ else
+ {
+ for (var item in autoCommands)
+ {
+ if (item == auEvent)
+ {
+ delete autoCommands[item];
+ }
+ else if (!auEvent) // delete: regs
+ {
+ for (var i = 0; i < autoCommands[item].length; i++)
+ if (!regs || regs == autoCommands[item][i][0])
+ autoCommands[item].splice(i, 1); // remove array
+ }
+ }
+ }
+ },
+
+ list: function (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 autoCommands)
+ {
+ flag = true;
+ if (!auEvent || item == auEvent) // filter event
+ {
+ for (var i = 0; i < autoCommands[item].length; i++)
+ {
+ if (!regs || regs == 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> " + vimperator.util.escapeHTML(autoCommands[item][i][0]) + "</td>";
+ list += "<td>" + vimperator.util.escapeHTML(autoCommands[item][i][1]) + "</td>";
+ list += "</tr>";
+ }
+ }
+ }
+ }
+
+ list += "</table>";
+ vimperator.commandline.echo(list, vimperator.commandline.HL_NORMAL, vimperator.commandline.FORCE_MULTILINE);
+ },
+
+ add: function (auEvent, regs, cmds)
+ {
+ var eventsIter = auEvent.split(",");
+ for (var i = 0; i < eventsIter.length; i++)
+ {
+ if (!autoCommands[eventsIter[i]])
+ autoCommands[eventsIter[i]] = [];
+
+ autoCommands[eventsIter[i]].push([regs, cmds]);
+ }
+ },
+
+ trigger: function (auEvent, url)
+ {
+ if (autoCommands[auEvent])
+ {
+ for (var i = 0; i < autoCommands[auEvent].length; i++)
+ {
+ var regex = new RegExp(autoCommands[auEvent][i][0]);
+ if (regex.test(url))
+ {
+ vimperator.execute(autoCommands[auEvent][i][1]);
+ }
+ }
+ }
+ }
+ };
+}
+
vimperator.Events = function () //{{{
{
////////////////////////////////////////////////////////////////////////////////
@@ -229,13 +344,16 @@
}
// 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;
+
+ //update history
vimperator.history.add(url, title);
+ vimperator.buffer.updateBufferList();
+ vimperator.autocommands.trigger("onPageLoad", url);
+
// mark the buffer as loaded, we can't use vimperator.buffer.loaded
// since that always refers to the current buffer, while doc can be
// any buffer, even in a background tab
Index: vimperator.js
===================================================================
RCS file: /cvs/vimperator/src/content/vimperator.js,v
retrieving revision 1.51
diff -u -r1.51 vimperator.js
--- vimperator.js 20 Dec 2007 15:19:09 -0000 1.51
+++ vimperator.js 27 Dec 2007 15:32:19 -0000
@@ -615,6 +615,8 @@
vimperator.quickmarks = vimperator.QuickMarks();
vimperator.log("Loading module hints...", 3);
vimperator.hints = vimperator.Hints();
+ vimperator.log("Loading module autocommands...", 3); //XXX: what the 3 there, I didn't check
+ vimperator.autocommands = vimperator.AutoCommands();
vimperator.log("Loading module io...", 3);
vimperator.io = vimperator.IO();
vimperator.log("Loading module completion...", 3);
More information about the Vimperator
mailing list