From 03d030db9e404a121d37c8c4ba0f82e246a38a84 Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Fri, 8 Aug 2008 06:45:14 +0000 Subject: [PATCH] change :command to behave like Vim's and add :comclear and :delcommand --- NEWS | 6 ++++++ content/commands.js | 45 ++++++++++++++++++++++++++++++++++++++++++-- locale/en-US/map.txt | 43 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index d80c0bbe..c9085178 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,11 @@
 2008-06-xx:
     * version 1.2
+    * IMPORTANT: changed :command to behave like Vim's version.
+      Eg. An alert command specified previously as
+          :command AlertMe alert(args)
+      should now be defined as
+          :command AlertMe :js alert()
     * IMPORTANT: changed the default keybinding to temporarily disable all
       Vimperator keybindings from  to 
     * IMPORTANT: removed old :buffers! buffer window, as it was ugly and slightly broken
@@ -10,6 +15,7 @@
       generous donation which made this behavior possible)
     * IMPORTANT: ctrl-x/a never take possible negative URLs into account, it was just
       too unpredictable
+    * add :comclear and :delcommand
     * add a special version to :hardcopy to skip the Print dialog
     * add :bl[ast], :bf[irst], :br[ewind] to go to first/last tab
     * add :bn[ext], :bp[revious], :bN[ext] to switch to next/previous tab
diff --git a/content/commands.js b/content/commands.js
index fedc035e..72ddcf5a 100644
--- a/content/commands.js
+++ b/content/commands.js
@@ -646,7 +646,11 @@ liberator.Commands = function () //{{{
             {
                 if (!liberator.commands.addUserCommand([cmd],
                         "User defined command",
-                        function (args, special, count, modifiers) { eval(rep); },
+                        function (args, special, count, modifiers)
+                        {
+                            var replaced = rep.replace("", args).replace("", "<");
+                            liberator.execute(replaced);
+                        },
                         null, special))
                 {
                     liberator.echoerr("E174: Command already exists: add ! to replace it");
@@ -660,12 +664,14 @@ liberator.Commands = function () //{{{
                     var str = ":" + liberator.util.escapeHTML(liberator.commandline.getCommand()) + "
" + ""; for (var i = 0; i < cmdlist.length; i++) - str += ""; + str += ""; str += "
NameArgsDefinition
" + cmdlist[i].name + "" + "*" + "" + cmdlist[i].isUserCommand + "
" + cmdlist[i].name + "" + "*" + "" + "...definition not implemented yet" + "
"; liberator.commandline.echo(str, liberator.commandline.HL_NORMAL, liberator.commandline.FORCE_MULTILINE); } else + { liberator.echo("No user-defined commands found"); + } } }, { @@ -674,6 +680,41 @@ liberator.Commands = function () //{{{ [["-bar"], OPTION_NOARG]] */ }); + commandManager.add(["comc[lear]"], + "Delete all user-defined commands", + function (args) + { + if (args) + { + liberator.echoerr("E488: Trailing characters"); + return; + } + + var commands = getUserCommands(); + for (var i = 0; i < commands.length; i++) + removeUserCommand(commands[i].name); + }); + + // TODO: complete with user-defined commands + commandManager.add(["delc[ommand]"], + "Delete the specified user-defined command", + function (args) + { + if (!args) + { + liberator.echoerr("E471: Argument required"); + return; + } + + // TODO: add getUserCommand, removeUserCommands, or similar, and make them 'public'? + var commands = getUserCommands(args); + + if (commands.length == 1 && args == commands[0].name) + removeUserCommand(commands[0].name); + else + liberator.echoerr("E184: No such user-defined command: " + args); + }); + // TODO: remove preview window, or change it at least // commandManager.add(["pc[lose]"], // "Close preview window on bottom of screen", diff --git a/locale/en-US/map.txt b/locale/en-US/map.txt index aca3fc87..25728e95 100644 --- a/locale/en-US/map.txt +++ b/locale/en-US/map.txt @@ -200,9 +200,50 @@ ________________________________________________________________________________ section:User-defined{nbsp}commands[user-commands] |:com| |:command| +||:com[mand]|| + +________________________________________________________________________________ +List all user-defined commands. +________________________________________________________________________________ + +||:com[mand] {cmd}|| + +________________________________________________________________________________ +List all user-defined commands that start with {cmd}. +________________________________________________________________________________ + + ||:com[mand][!] [{attr}...] {cmd} {rep}|| + ________________________________________________________________________________ -Lists and defines commands. To be written - but it works similar to Vim's :command +Define a new user command. The name of the command is {cmd} and its relacement +text is {rep}. The command's attributes are {attr}. If a command with this +name already exists an error is reported unless [!] is specified, in which case +the command is redefined. Unlike Vim, the command may start with a lowercase +letter. + +The replacement text {rep} is scanned for escape sequences and these are +replaced with values from the user entered command line. The resulting string +is then executed as an Ex command. + +The valid escape sequences are: +`------`------------------------------------------------------------------------ + The command arguments exactly as supplied + A literal '<' character to allow for a literal copy of one of the escape sequences. Eg. args> will expand to a literal +-------------------------------------------------------------------------------- + +Note: {attr} not implemented yet. +________________________________________________________________________________ + + +|:comc| |:comclear| +||:comc[lear]|| + +________________________________________________________________________________ +Delete all user-defined commands. +________________________________________________________________________________ + + +|:delc| |:delcommand| +||:delc[ommand] {cmd}|| + +________________________________________________________________________________ +Delete the user-defined command {cmd}. ________________________________________________________________________________ // vim: set syntax=asciidoc: