From 8184b1109f8d3ccfdf6d6a38053b8907c9d70d55 Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Thu, 28 Feb 2008 23:55:02 +0000 Subject: [PATCH] moved more commands. the end of all the moving is near! --- content/commands.js | 363 -------------------------------------------- content/events.js | 92 +++++++++++ content/find.js | 9 ++ content/options.js | 117 ++++++++++++++ content/tabs.js | 4 + content/vim.js | 129 ++++++++++++++++ 6 files changed, 351 insertions(+), 363 deletions(-) diff --git a/content/commands.js b/content/commands.js index 368f8ea8..be8bb32a 100644 --- a/content/commands.js +++ b/content/commands.js @@ -596,98 +596,6 @@ vimperator.Commands = function () //{{{ // move to vim.js: - commandManager.addUserCommand(new vimperator.Command(["let"], - function (args) - { - if (!args) - { - var str = ""; - for (var i in vimperator.globalVariables) - { - var value = vimperator.globalVariables[i]; - if (typeof value == "number") - var prefix = "#"; - else if (typeof value == "function") - var prefix = "*"; - else - var prefix = ""; - - str += "" + i + "" + prefix + value + "\n"; - } - if (str) - vimperator.echo("" + str + "
", vimperator.commandline.FORCE_MULTILINE); - else - vimperator.echo("No variables found"); - return; - } - - var matches; - // 1 - type, 2 - name, 3 - +-., 4 - expr - if (matches = args.match(/([$@&])?([\w:]+)\s*([+-.])?=\s*(.+)/)) - { - if (!matches[1]) - { - var reference = vimperator.variableReference(matches[2]); - if (!reference[0] && matches[3]) - { - vimperator.echoerr("E121: Undefined variable: " + matches[2]); - return; - } - - var expr = vimperator.eval(matches[4]); - if (typeof expr === undefined) - { - vimperator.echoerr("E15: Invalid expression: " + matches[4]); - return; - } - else - { - if (!reference[0]) - { - if (reference[2] == "g") - reference[0] = vimperator.globalVariables; - else - return; // for now - } - - if (matches[3]) - { - if (matches[3] == "+") - reference[0][reference[1]] += expr; - else if (matches[3] == "-") - reference[0][reference[1]] -= expr; - else if (matches[3] == ".") - reference[0][reference[1]] += expr.toString(); - } - else - reference[0][reference[1]] = expr; - } - } - } - // 1 - name - else if (matches = args.match(/^\s*([\w:]+)\s*$/)) - { - var reference = vimperator.variableReference(matches[1]); - if (!reference[0]) - { - vimperator.echoerr("E121: Undefined variable: " + matches[1]); - return; - } - - var value = reference[0][reference[1]]; - if (typeof value == "number") - var prefix = "#"; - else if (typeof value == "function") - var prefix = "*"; - else - var prefix = ""; - vimperator.echo(reference[1] + "\t\t" + prefix + value); - } - }, - { - shortHelp: "Sets or lists a variable" - } - )); commandManager.addUserCommand(new vimperator.Command(["q[uit]"], function () { vimperator.tabs.remove(getBrowser().mCurrentTab, 1, false, 1); }, { shortHelp: "Quit current tab" } @@ -697,158 +605,7 @@ vimperator.Commands = function () //{{{ function () { vimperator.quit(false); }, { shortHelp: "Quit Vimperator", } )); - commandManager.addUserCommand(new vimperator.Command(["res[tart]"], - function () { vimperator.restart(); }, - { shortHelp: "Force the browser to restart" } - )); - commandManager.addUserCommand(new vimperator.Command(["time"], - function (args, special, count) - { - try - { - if (count > 1) - { - var i = count; - var beforeTime = Date.now(); - if (args && args[0] == ":") - { - while (i--) - vimperator.execute(args); - } - else - { - while (i--) - eval("with(vimperator){" + args + "}"); - } - - if (special) - return; - - var afterTime = Date.now(); - - if ((afterTime - beforeTime) / count >= 100) - { - var each = ((afterTime - beforeTime) / 1000.0 / count); - var eachUnits = "sec"; - } - else - { - var each = ((afterTime - beforeTime) / count); - var eachUnits = "msec"; - } - - if (afterTime - beforeTime >= 100) - { - var total = ((afterTime - beforeTime) / 1000.0); - var totalUnits = "sec"; - } - else - { - var total = (afterTime - beforeTime); - var totalUnits = "msec"; - } - - var str = ":" + vimperator.util.escapeHTML(vimperator.commandline.getCommand()) + "
" + - "" + - "" + - "" + - "" + - "" + - "
Code execution summary
Executed:" + count + "times
Average time:" + each.toFixed(2) + "" + eachUnits + "
Total time:" + total.toFixed(2) + "" + totalUnits + "
"; - - vimperator.commandline.echo(str, vimperator.commandline.HL_NORMAL, vimperator.commandline.FORCE_MULTILINE); - } - else - { - var beforeTime = Date.now(); - if (args && args[0] == ":") - vimperator.execute(args); - else - eval("with(vimperator){" + args + "}"); - - if (special) - return; - - var afterTime = Date.now(); - - if (afterTime - beforeTime >= 100) - vimperator.echo("Total time: " + ((afterTime - beforeTime) / 1000.0).toFixed(2) + " sec"); - else - vimperator.echo("Total time: " + (afterTime - beforeTime) + " msec"); - } - } - catch (e) - { - vimperator.echoerr(e); - } - }, - { - shortHelp: "Profile a piece of code or a command" - } - )); - commandManager.addUserCommand(new vimperator.Command(["unl[et]"], - function (args, special) - { - if (!args) - { - vimperator.echoerr("E471: Argument required"); - return; - } - - var names = args.split(/ /); - if (typeof names == "string") names = [names]; - var length = names.length; - for (var i = 0, name = names[i]; i < length; name = names[++i]) - { - var reference = vimperator.variableReference(name); - if (!reference[0]) - { - if (!special) - vimperator.echoerr("E108: No such variable: " + name); - return; - } - - delete reference[0][reference[1]]; - } - }, - { - shortHelp: "Deletes a variable." - } - )); - commandManager.addUserCommand(new vimperator.Command(["ve[rsion]"], - function (args, special) - { - if (special) - vimperator.open("about:"); - else - vimperator.echo(":" + vimperator.util.escapeHTML(vimperator.commandline.getCommand()) + - "\nVimperator " + vimperator.version + " running on:\n" + navigator.userAgent); - }, - { - shortHelp: "Show version information" - } - )); - commandManager.addUserCommand(new vimperator.Command(["viu[sage]"], - function (args, special, count, modifiers) - { - var usage = ""; - for (let mapping in vimperator.mappings) - { - usage += ""; - } - usage += "
" + - vimperator.util.escapeHTML(mapping.names[0]) + "" + - vimperator.util.escapeHTML(mapping.shortHelp || "") + "
"; - - vimperator.echo(usage, vimperator.commandline.FORCE_MULTILINE); - }, - { - shortHelp: "Show help for normal mode commands" - } - )); - - // move to io.js: // TODO: move where? (commands.js would be fine, but timing issue) commandManager.addUserCommand(new vimperator.Command(["com[mand]"], @@ -1000,98 +757,6 @@ vimperator.Commands = function () //{{{ { shortHelp: "Remove all abbreviations for Insert mode" } )); - // move to v.AutoCommands: - commandManager.addUserCommand(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, regex, cmds] = args.match(/^(\*)?(?:\s+)?(\S+)(?:\s+)?(\S+)?(?:\s+)?(.+)?$/); - - if (cmds) - { - vimperator.autocommands.add(auEvent, regex, cmds); - } - else if (regex) // e.g. no cmds provided - { - if (special) - vimperator.autocommands.remove(auEvent, regex); - else - vimperator.autocommands.list(auEvent, regex); - } - 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", - completer: function (filter) { return vimperator.completion.autocommands(filter); } - } - )); - - // move to events.js: - commandManager.addUserCommand(new vimperator.Command(["macros"], - function (arg) - { - var str = ""; - var macroRef = vimperator.events.getMacros(arg); - for (var item in macroRef) - str += ""; - - str += "
" + item + "   " + - vimperator.util.escapeHTML(macroRef[item]) + "
"; - - vimperator.echo(str, vimperator.commandline.FORCE_MULTILINE); - }, - { - shortHelp: "List macros matching a regex" - } - )); - commandManager.addUserCommand(new vimperator.Command(["delmac[ros]"], - function (arg) - { - if (!arg) - vimperator.echoerr("E474: Invalid argument"); - else - vimperator.events.deleteMacros(arg); - }, - { - shortHelp: "Delete macros matching a regex" - } - )); - commandManager.addUserCommand(new vimperator.Command(["pl[ay]"], - function (arg) - { - if (!arg) - vimperator.echoerr("E474: Invalid argument"); - else - vimperator.events.playMacro(arg); - }, - { - shortHelp: "Play a macro", - completer: function (filter) { return vimperator.completion.macros(filter); } - } - )); - // TODO: add helper method: addMappingCommand // 0 args -> list all maps // 1 arg -> list the maps starting with args @@ -1268,40 +933,12 @@ vimperator.Commands = function () //{{{ } )); - // move to find.js: - commandManager.addUserCommand(new vimperator.Command(["noh[lsearch]"], - function (args) { vimperator.search.clear(); }, - { shortHelp: "Remove the search highlighting" } - )); - - // move to either events.js or vim.js? - commandManager.addUserCommand(new vimperator.Command(["norm[al]"], - function (args, special) - { - if (!args) - { - vimperator.echoerr("E471: Argument required"); - return; - } - - vimperator.events.feedkeys(args, special); - }, - { - shortHelp: "Execute Normal mode commands" - } - )); // TODO: remove/change preview window commandManager.addUserCommand(new vimperator.Command(["pc[lose]"], function () { vimperator.previewwindow.hide(); }, { shortHelp: "Close preview window on bottom of screen" } )); - // move to tabs.js - commandManager.addUserCommand(new vimperator.Command(["reloada[ll]"], - function (args, special) { vimperator.tabs.reloadAll(special); }, - { shortHelp: "Reload all pages" } - )); - // TODO: check for v.has("windows") commandManager.addUserCommand(new vimperator.Command(["winc[lose]", "wc[lose]"], function (args) { window.close(); }, diff --git a/content/events.js b/content/events.js index e8a8a1b0..b9a4d8cf 100644 --- a/content/events.js +++ b/content/events.js @@ -42,6 +42,56 @@ vimperator.AutoCommands = function() //{{{ throw StopIteration; } + /////////////////////////////////////////////////////////////////////////////}}} + ////////////////////// COMMANDS //////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////{{{ + + vimperator.commands.add(["au[tocmd]"], + "Execute commands automatically on events", + 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, regex, cmds] = args.match(/^(\*)?(?:\s+)?(\S+)(?:\s+)?(\S+)?(?:\s+)?(.+)?$/); + + if (cmds) + { + vimperator.autocommands.add(auEvent, regex, cmds); + } + else if (regex) // e.g. no cmds provided + { + if (special) + vimperator.autocommands.remove(auEvent, regex); + else + vimperator.autocommands.list(auEvent, regex); + } + 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); + } + } + }, + { + completer: function (filter) { return vimperator.completion.autocommands(filter); } + }); + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ @@ -514,6 +564,48 @@ vimperator.Events = function () //{{{ }, { flags: vimperator.Mappings.flags.ARGUMENT | vimperator.Mappings.flags.COUNT }); + /////////////////////////////////////////////////////////////////////////////}}} + ////////////////////// COMMANDS //////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////{{{ + + vimperator.commands.add(["delmac[ros]"], + "Delete macros", + function (arg) + { + if (!arg) + vimperator.echoerr("E474: Invalid argument"); + else + vimperator.events.deleteMacros(arg); + }); + + vimperator.commands.add(["macros"], + "List all macros", + function (arg) + { + var str = ""; + var macroRef = vimperator.events.getMacros(arg); + for (var item in macroRef) + str += ""; + + str += "
" + item + "   " + + vimperator.util.escapeHTML(macroRef[item]) + "
"; + + vimperator.echo(str, vimperator.commandline.FORCE_MULTILINE); + }); + + vimperator.commands.add(["pl[ay]"], + "Replay a recorded macro", + function (arg) + { + if (!arg) + vimperator.echoerr("E474: Invalid argument"); + else + vimperator.events.playMacro(arg); + }, + { + completer: function (filter) { return vimperator.completion.macros(filter); } + }); + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ diff --git a/content/find.js b/content/find.js index 1c28c9dc..55a909f4 100644 --- a/content/find.js +++ b/content/find.js @@ -144,6 +144,7 @@ vimperator.Search = function () //{{{ /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// MAPPINGS //////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ + var modes = vimperator.config.browserModes || [vimperator.modes.NORMAL]; vimperator.mappings.add(modes, @@ -178,6 +179,14 @@ vimperator.Search = function () //{{{ vimperator.search.findAgain(); }); + /////////////////////////////////////////////////////////////////////////////}}} + ////////////////////// COMMANDS //////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////{{{ + + vimperator.commands.add(["noh[lsearch]"], + "Remove the search highlighting", + function (args) { vimperator.search.clear(); }); + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ diff --git a/content/options.js b/content/options.js index c4c25d10..cff6dfed 100644 --- a/content/options.js +++ b/content/options.js @@ -200,6 +200,96 @@ vimperator.Options = function () //{{{ ////////////////////// COMMANDS //////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ + vimperator.commands.add(["let"], + "Set or list a variable", + function (args) + { + if (!args) + { + var str = ""; + for (var i in vimperator.globalVariables) + { + var value = vimperator.globalVariables[i]; + if (typeof value == "number") + var prefix = "#"; + else if (typeof value == "function") + var prefix = "*"; + else + var prefix = ""; + + str += "" + i + "" + prefix + value + "\n"; + } + if (str) + vimperator.echo("" + str + "
", vimperator.commandline.FORCE_MULTILINE); + else + vimperator.echo("No variables found"); + return; + } + + var matches; + // 1 - type, 2 - name, 3 - +-., 4 - expr + if (matches = args.match(/([$@&])?([\w:]+)\s*([+-.])?=\s*(.+)/)) + { + if (!matches[1]) + { + var reference = vimperator.variableReference(matches[2]); + if (!reference[0] && matches[3]) + { + vimperator.echoerr("E121: Undefined variable: " + matches[2]); + return; + } + + var expr = vimperator.eval(matches[4]); + if (typeof expr === undefined) + { + vimperator.echoerr("E15: Invalid expression: " + matches[4]); + return; + } + else + { + if (!reference[0]) + { + if (reference[2] == "g") + reference[0] = vimperator.globalVariables; + else + return; // for now + } + + if (matches[3]) + { + if (matches[3] == "+") + reference[0][reference[1]] += expr; + else if (matches[3] == "-") + reference[0][reference[1]] -= expr; + else if (matches[3] == ".") + reference[0][reference[1]] += expr.toString(); + } + else + reference[0][reference[1]] = expr; + } + } + } + // 1 - name + else if (matches = args.match(/^\s*([\w:]+)\s*$/)) + { + var reference = vimperator.variableReference(matches[1]); + if (!reference[0]) + { + vimperator.echoerr("E121: Undefined variable: " + matches[1]); + return; + } + + var value = reference[0][reference[1]]; + if (typeof value == "number") + var prefix = "#"; + else if (typeof value == "function") + var prefix = "*"; + else + var prefix = ""; + vimperator.echo(reference[1] + "\t\t" + prefix + value); + } + }); + vimperator.commands.add(["pref[erences]", "prefs"], "Show " + vimperator.config.appName + " Preferences", function (args, special, count, modifiers) @@ -476,6 +566,33 @@ vimperator.Options = function () //{{{ completer: function (filter, special) { return vimperator.completion.option(filter, special); } }); + vimperator.commands.add(["unl[et]"], + "Delete a variable", + function (args, special) + { + if (!args) + { + vimperator.echoerr("E471: Argument required"); + return; + } + + var names = args.split(/ /); + if (typeof names == "string") names = [names]; + var length = names.length; + for (var i = 0, name = names[i]; i < length; name = names[++i]) + { + var reference = vimperator.variableReference(name); + if (!reference[0]) + { + if (!special) + vimperator.echoerr("E108: No such variable: " + name); + return; + } + + delete reference[0][reference[1]]; + } + }); + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ diff --git a/content/tabs.js b/content/tabs.js index b4d84b0b..14eff3ac 100644 --- a/content/tabs.js +++ b/content/tabs.js @@ -238,6 +238,10 @@ vimperator.Tabs = function () //{{{ vimperator.buffer.list(special); }); + vimperator.commands.add(["reloada[ll]"], + "Reload all tab pages", + function (args, special) { vimperator.tabs.reloadAll(special); }); + vimperator.commands.add(["tab"], "Execute a command and tell it to output in a new tab", function (args) { vimperator.execute(args, { inTab: true }); }, diff --git a/content/vim.js b/content/vim.js index 72d885a8..bcd063cd 100644 --- a/content/vim.js +++ b/content/vim.js @@ -254,6 +254,135 @@ const vimperator = (function () //{{{ { completer: function (filter) { return vimperator.completion.javascript(filter); } }); + + vimperator.commands.add(["norm[al]"], + "Execute Normal mode commands", + function (args, special) + { + if (!args) + { + vimperator.echoerr("E471: Argument required"); + return; + } + + vimperator.events.feedkeys(args, special); + }); + + vimperator.commands.add(["res[tart]"], + "Force " + vimperator.config.appName + " to restart", + function () { vimperator.restart(); }); + + vimperator.commands.add(["time"], + "Profile a piece of code or run a command multiple times", + function (args, special, count) + { + try + { + if (count > 1) + { + var i = count; + var beforeTime = Date.now(); + + if (args && args[0] == ":") + { + while (i--) + vimperator.execute(args); + } + else + { + while (i--) + eval("with(vimperator){" + args + "}"); + } + + if (special) + return; + + var afterTime = Date.now(); + + if ((afterTime - beforeTime) / count >= 100) + { + var each = ((afterTime - beforeTime) / 1000.0 / count); + var eachUnits = "sec"; + } + else + { + var each = ((afterTime - beforeTime) / count); + var eachUnits = "msec"; + } + + if (afterTime - beforeTime >= 100) + { + var total = ((afterTime - beforeTime) / 1000.0); + var totalUnits = "sec"; + } + else + { + var total = (afterTime - beforeTime); + var totalUnits = "msec"; + } + + var str = ":" + vimperator.util.escapeHTML(vimperator.commandline.getCommand()) + "
" + + "" + + "" + + "" + + "" + + "" + + "
Code execution summary
Executed:" + count + "times
Average time:" + each.toFixed(2) + "" + eachUnits + "
Total time:" + total.toFixed(2) + "" + totalUnits + "
"; + + vimperator.commandline.echo(str, vimperator.commandline.HL_NORMAL, vimperator.commandline.FORCE_MULTILINE); + } + else + { + var beforeTime = Date.now(); + if (args && args[0] == ":") + vimperator.execute(args); + else + eval("with(vimperator){" + args + "}"); + + if (special) + return; + + var afterTime = Date.now(); + + if (afterTime - beforeTime >= 100) + vimperator.echo("Total time: " + ((afterTime - beforeTime) / 1000.0).toFixed(2) + " sec"); + else + vimperator.echo("Total time: " + (afterTime - beforeTime) + " msec"); + } + } + catch (e) + { + vimperator.echoerr(e); + } + }); + + vimperator.commands.add(["ve[rsion]"], + "Show version information", + function (args, special) + { + if (special) + vimperator.open("about:"); + else + vimperator.echo(":" + vimperator.util.escapeHTML(vimperator.commandline.getCommand()) + "\n" + + vimperator.config.appName + " " + vimperator.version + + " running on:\n" + navigator.userAgent); + }); + + vimperator.commands.add(["viu[sage]"], + "List all mappings with a short description", + function (args, special, count, modifiers) + { + var usage = ""; + for (let mapping in vimperator.mappings) + { + usage += ""; + } + usage += "
" + + vimperator.util.escapeHTML(mapping.names[0]) + "" + + vimperator.util.escapeHTML(mapping.shortHelp || "") + "
"; + + vimperator.echo(usage, vimperator.commandline.FORCE_MULTILINE); + }); } // initially hide all GUI, it is later restored unless the user has :set go= or something