1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 08:17:59 +01:00

make use of our argsParser to handle commands with a fixed number of arguments.

Also added args.string to access the original string before it was parsed.
TODO: Always pass a parseArgs-like structure to commands, instead of only
when options or argCount is specified?
This commit is contained in:
Martin Stubenschrott
2008-08-13 21:53:56 +00:00
parent c49ed07af9
commit 1035a01ace
10 changed files with 76 additions and 146 deletions

View File

@@ -833,20 +833,15 @@ liberator.QuickMarks = function () //{{{
"Mark a URL with a letter for quick access", "Mark a URL with a letter for quick access",
function (args) function (args)
{ {
if (!args) var matches = args.string.match(/^([a-zA-Z0-9])(?:\s+(.+))?$/);
{
liberator.echoerr("E471: Argument required");
return;
}
var matches = args.match(/^([a-zA-Z0-9])(?:\s+(.+))?$/);
if (!matches) if (!matches)
liberator.echoerr("E488: Trailing characters"); liberator.echoerr("E488: Trailing characters");
else if (!matches[2]) else if (!matches[2])
liberator.quickmarks.add(matches[1], liberator.buffer.URL); liberator.quickmarks.add(matches[1], liberator.buffer.URL);
else else
liberator.quickmarks.add(matches[1], matches[2]); liberator.quickmarks.add(matches[1], matches[2]);
}); },
{ argCount: "+" });
liberator.commands.add(["qmarks"], liberator.commands.add(["qmarks"],
"Show all QuickMarks", "Show all QuickMarks",

View File

@@ -1535,24 +1535,21 @@ liberator.Marks = function () //{{{
"Mark current location within the web page", "Mark current location within the web page",
function (args) function (args)
{ {
if (!args) var mark = args.arguments[0];
{ if (mark.length > 1)
liberator.echoerr("E471: Argument required");
return;
}
if (args.length > 1)
{ {
liberator.echoerr("E488: Trailing characters"); liberator.echoerr("E488: Trailing characters");
return; return;
} }
if (!/[a-zA-Z]/.test(args)) if (!/[a-zA-Z]/.test(mark))
{ {
liberator.echoerr("E191: Argument must be a letter or forward/backward quote"); liberator.echoerr("E191: Argument must be a letter or forward/backward quote");
return; return;
} }
liberator.marks.add(args); liberator.marks.add(mark);
}); },
{ argCount: "1" });
liberator.commands.add(["marks"], liberator.commands.add(["marks"],
"Show all location marks of current web page", "Show all location marks of current web page",

View File

@@ -397,6 +397,7 @@ liberator.Commands = function () //{{{
var args = {}; // parsed options var args = {}; // parsed options
args.arguments = []; // remaining arguments args.arguments = []; // remaining arguments
args.string = str; // for access to the unparsed string
var invalid = false; var invalid = false;
var onlyArgumentsRemaining = allowUnknownOptions || false; // after a -- has been found var onlyArgumentsRemaining = allowUnknownOptions || false; // after a -- has been found
@@ -571,11 +572,15 @@ liberator.Commands = function () //{{{
} }
// check for correct number of arguments // check for correct number of arguments
if ((args.arguments.length == 0 && (argCount == "1" || argCount == "+")) || if (args.arguments.length == 0 && (argCount == "1" || argCount == "+"))
(args.arguments.length == 1 && (argCount == "0")) ||
(args.arguments.length > 1 && (argCount == "0" || argCount == "1" || argCount == "?")))
{ {
liberator.echoerr("Invalid number of arguments: " + args.arguments.length); liberator.echoerr("E471: Argument required");
return null;
}
else if (args.arguments.length == 1 && (argCount == "0") ||
args.arguments.length > 1 && (argCount == "0" || argCount == "1" || argCount == "?"))
{
liberator.echoerr("E488: Trailing characters");
return null; return null;
} }
@@ -685,41 +690,26 @@ liberator.Commands = function () //{{{
"Delete all user-defined commands", "Delete all user-defined commands",
function (args) function (args)
{ {
if (args)
{
liberator.echoerr("E488: Trailing characters");
return;
}
var commands = getUserCommands(); var commands = getUserCommands();
for (var i = 0; i < commands.length; i++) for (var i = 0; i < commands.length; i++)
removeUserCommand(commands[i].name); removeUserCommand(commands[i].name);
}); },
{ argCount: "0" });
// TODO: complete with user-defined commands // TODO: complete with user-defined commands
commandManager.add(["delc[ommand]"], commandManager.add(["delc[ommand]"],
"Delete the specified user-defined command", "Delete the specified user-defined command",
function (args) function (args)
{ {
if (!args)
{
liberator.echoerr("E471: Argument required");
return;
}
// TODO: add getUserCommand, removeUserCommands, or similar, and make them 'public'? // TODO: add getUserCommand, removeUserCommands, or similar, and make them 'public'?
var commands = getUserCommands(args); var cmd = args.arguments[0];
var commands = getUserCommands(cmd);
if (commands.length == 1 && args == commands[0].name) if (commands.length == 1 && cmd == commands[0].name)
removeUserCommand(commands[0].name); removeUserCommand(commands[0].name);
else else
liberator.echoerr("E184: No such user-defined command: " + args); liberator.echoerr("E184: No such user-defined command: " + cmd);
}); },
{ argCount: "1" });
// TODO: remove preview window, or change it at least
// commandManager.add(["pc[lose]"],
// "Close preview window on bottom of screen",
// function () { liberator.previewwindow.hide(); });
//}}} //}}}

View File

@@ -91,11 +91,9 @@ liberator.IO = function () //{{{
"Print the current directory name", "Print the current directory name",
function (args) function (args)
{ {
if (args) liberator.echo(liberator.io.getCurrentDirectory());
liberator.echoerr("E488: Trailing characters"); },
else { argCount: "0" });
liberator.echo(liberator.io.getCurrentDirectory());
});
// mkv[imperatorrc] or mkm[uttatorrc] // mkv[imperatorrc] or mkm[uttatorrc]
liberator.commands.add(["mk" + extname.substr(0, 1) + "[" + extname.substr(1) + "rc]"], liberator.commands.add(["mk" + extname.substr(0, 1) + "[" + extname.substr(1) + "rc]"],
@@ -105,7 +103,9 @@ liberator.IO = function () //{{{
// TODO: "E172: Only one file name allowed" // TODO: "E172: Only one file name allowed"
var filename; var filename;
if (args) if (args)
{
filename = args; filename = args;
}
else else
{ {
filename = (navigator.platform == "Win32") ? "~/_" : "~/."; filename = (navigator.platform == "Win32") ? "~/_" : "~/.";

View File

@@ -230,14 +230,9 @@ const liberator = (function () //{{{
"Execute Normal mode commands", "Execute Normal mode commands",
function (args, special) function (args, special)
{ {
if (!args) liberator.events.feedkeys(args.string, special);
{ },
liberator.echoerr("E471: Argument required"); { argCount: "+" });
return;
}
liberator.events.feedkeys(args, special);
});
liberator.commands.add(["q[uit]"], liberator.commands.add(["q[uit]"],
liberator.has("tabs") ? "Quit current tab" : "Quit application", liberator.has("tabs") ? "Quit current tab" : "Quit application",

View File

@@ -727,27 +727,17 @@ liberator.Mail = function () //{{{
"Empty trash of the current account", "Empty trash of the current account",
function (args, special) function (args, special)
{ {
if (args)
{
liberator.echoerr("E488: Trailing characters");
return;
}
goDoCommand("cmd_emptyTrash"); goDoCommand("cmd_emptyTrash");
}); },
{ argCount: "0" });
liberator.commands.add(["get[messages]"], liberator.commands.add(["get[messages]"],
"Check for new messages", "Check for new messages",
function (args, special) function (args, special)
{ {
if (args)
{
liberator.echoerr("E488: Trailing characters");
return;
}
liberator.mail.getNewMessages(!special); liberator.mail.getNewMessages(!special);
}); },
{ argCount: "0" });
/////////////////////////////////////////////////////////////////////////////}}} /////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////

View File

@@ -213,38 +213,28 @@ liberator.Mappings = function () //{{{
"Remove all mappings" + modeDescription, "Remove all mappings" + modeDescription,
function (args) function (args)
{ {
if (args)
{
liberator.echoerr("E474: Invalid argument");
return;
}
for (let i = 0; i < modes.length; i++) for (let i = 0; i < modes.length; i++)
liberator.mappings.removeAll(modes[i]); liberator.mappings.removeAll(modes[i]);
}); },
{ argCount: "0" });
liberator.commands.add([ch + "unm[ap]"], liberator.commands.add([ch + "unm[ap]"],
"Remove a mapping" + modeDescription, "Remove a mapping" + modeDescription,
function (args) function (args)
{ {
if (!args) var found = false;
{
liberator.echoerr("E474: Invalid argument");
return;
}
var flag = false;
for (let i = 0; i < modes.length; i++) for (let i = 0; i < modes.length; i++)
{ {
if (liberator.mappings.hasMap(modes[i], args)) if (liberator.mappings.hasMap(modes[i], args.arguments[0]))
{ {
liberator.mappings.remove(modes[i], args); liberator.mappings.remove(modes[i], args.arguments[0]);
flag = true; found = true;
} }
} }
if (!flag) if (!found)
liberator.echoerr("E31: No such mapping"); liberator.echoerr("E31: No such mapping");
}); },
{ argCount: "1" });
} }
/////////////////////////////////////////////////////////////////////////////}}} /////////////////////////////////////////////////////////////////////////////}}}

View File

@@ -307,25 +307,19 @@ liberator.Options = function () //{{{
"Show " + liberator.config.hostApplication + " preferences", "Show " + liberator.config.hostApplication + " preferences",
function (args, special, count, modifiers) function (args, special, count, modifiers)
{ {
if (!args) if (special) // open Firefox settings GUI dialog
{ {
if (special) // open firefox settings gui dialog liberator.open("about:config",
{ (liberator.options.newtab &&
liberator.open("about:config", (liberator.options.newtab == "all" || liberator.options.newtab.split(",").indexOf("prefs") != -1)) ?
(liberator.options.newtab && liberator.NEW_TAB : liberator.CURRENT_TAB);
(liberator.options.newtab == "all" || liberator.options.newtab.split(",").indexOf("prefs") != -1)) ?
liberator.NEW_TAB : liberator.CURRENT_TAB);
}
else
{
openPreferences();
}
} }
else else
{ {
liberator.echoerr("E488: Trailing characters"); openPreferences();
} }
}); },
{ argCount: "0" });
liberator.commands.add(["setl[ocal]"], liberator.commands.add(["setl[ocal]"],
"Set local option", "Set local option",
@@ -334,12 +328,10 @@ liberator.Options = function () //{{{
liberator.commands.get("set").execute(args, special, count, { scope: liberator.options.OPTION_SCOPE_LOCAL }); liberator.commands.get("set").execute(args, special, count, { scope: liberator.options.OPTION_SCOPE_LOCAL });
}, },
{ {
completer: function (filter, special, count) completer: function (filter, special, count)
{ {
return liberator.commands.get("set").completer(filter, special, count, { scope: liberator.options.OPTION_SCOPE_LOCAL }); return liberator.commands.get("set").completer(filter, special, count, { scope: liberator.options.OPTION_SCOPE_LOCAL });
} }
} }
); );
@@ -350,12 +342,10 @@ liberator.Options = function () //{{{
liberator.commands.get("set").execute(args, special, count, { scope: liberator.options.OPTION_SCOPE_GLOBAL }); liberator.commands.get("set").execute(args, special, count, { scope: liberator.options.OPTION_SCOPE_GLOBAL });
}, },
{ {
completer: function (filter, special, count) completer: function (filter, special, count)
{ {
return liberator.commands.get("set").completer(filter, special, count, { scope: liberator.options.OPTION_SCOPE_GLOBAL }); return liberator.commands.get("set").completer(filter, special, count, { scope: liberator.options.OPTION_SCOPE_GLOBAL });
} }
} }
); );
@@ -372,6 +362,7 @@ liberator.Options = function () //{{{
args = "all"; args = "all";
onlyNonDefault = true; onlyNonDefault = true;
} }
// 1 2 3 4 5 // 1 2 3 4 5
var matches = args.match(/^\s*?([a-zA-Z0-9\.\-_{}]+)([?&!])?\s*(([-+^]?)=(.*))?\s*$/); var matches = args.match(/^\s*?([a-zA-Z0-9\.\-_{}]+)([?&!])?\s*(([-+^]?)=(.*))?\s*$/);
var name = matches[1]; var name = matches[1];
@@ -384,7 +375,7 @@ liberator.Options = function () //{{{
invertBoolean = true; invertBoolean = true;
if (name == "all" && reset) if (name == "all" && reset)
liberator.echoerr("You can't reset all the firefox options, it could make your browser unusable."); liberator.echoerr("You can't reset all options, it could make " + liberator.config.hostApplication + " unusable.");
else if (name == "all") else if (name == "all")
liberator.options.listPrefs(onlyNonDefault, ""); liberator.options.listPrefs(onlyNonDefault, "");
else if (reset) else if (reset)
@@ -510,9 +501,8 @@ liberator.Options = function () //{{{
} }
} }
// write access // write access
// NOTE: the behaviour is generally Vim compatible but could be // NOTE: the behavior is generally Vim compatible but could be
// improved. i.e. Vim's behaviour is pretty sloppy to no real // improved. i.e. Vim's behavior is pretty sloppy to no real benefit
// benefit
else else
{ {
option.scope = newOptionScope; option.scope = newOptionScope;
@@ -721,17 +711,14 @@ liberator.Options = function () //{{{
"Delete a variable", "Delete a variable",
function (args, special) function (args, special)
{ {
if (!args) //var names = args.split(/ /);
{ //if (typeof names == "string") names = [names];
liberator.echoerr("E471: Argument required");
return;
}
var names = args.split(/ /); //var length = names.length;
if (typeof names == "string") names = [names]; //for (var i = 0, name = names[i]; i < length; name = names[++i])
var length = names.length; for (var i = 0; i < args.arguments.length; i++)
for (var i = 0, name = names[i]; i < length; name = names[++i])
{ {
var name = args.arguments[i];
var reference = liberator.variableReference(name); var reference = liberator.variableReference(name);
if (!reference[0]) if (!reference[0])
{ {
@@ -742,7 +729,8 @@ liberator.Options = function () //{{{
delete reference[0][reference[1]]; delete reference[0][reference[1]];
} }
}); },
{ argCount: "+" });
/////////////////////////////////////////////////////////////////////////////}}} /////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////

View File

@@ -383,14 +383,9 @@ liberator.Tabs = function () //{{{
"Show a list of all buffers", "Show a list of all buffers",
function (args, special) function (args, special)
{ {
if (args)
{
liberator.echoerr("E488: Trailing characters");
return;
}
liberator.tabs.list(special); liberator.tabs.list(special);
}); },
{ argCount: "0" });
liberator.commands.add(["quita[ll]", "qa[ll]"], liberator.commands.add(["quita[ll]", "qa[ll]"],
"Quit " + liberator.config.appName, "Quit " + liberator.config.appName,

View File

@@ -292,45 +292,35 @@ liberator.config = { //{{{
"Close the sidebar window", "Close the sidebar window",
function (args) function (args)
{ {
if (args)
{
liberator.echoerr("E488: Trailing characters");
return;
}
if (document.getElementById("sidebar-box").hidden == false) if (document.getElementById("sidebar-box").hidden == false)
toggleSidebar(); toggleSidebar();
}); },
{ argCount: "0" });
liberator.commands.add(["sideb[ar]", "sb[ar]", "sbope[n]"], liberator.commands.add(["sideb[ar]", "sb[ar]", "sbope[n]"],
"Open the sidebar window", "Open the sidebar window",
function (args) function (args)
{ {
if (!args)
{
liberator.echoerr("E471: Argument required");
return;
}
// do nothing if the requested sidebar is already open // do nothing if the requested sidebar is already open
if (document.getElementById("sidebar-title").value == args) if (document.getElementById("sidebar-title").value == args.string)
{ {
document.getElementById("sidebar-box").contentWindow.focus(); document.getElementById("sidebar-box").contentWindow.focus();
return; return;
} }
var menu = document.getElementById("viewSidebarMenu"); var menu = document.getElementById("viewSidebarMenu");
for (var i = 0; i < menu.childNodes.length; i++) for (var i = 0; i < menu.childNodes.length; i++)
{ {
if (menu.childNodes[i].label == args) if (menu.childNodes[i].label == args.string)
{ {
menu.childNodes[i].doCommand(); menu.childNodes[i].doCommand();
break; return;
} }
} }
liberator.echoerr("No sidebar " + args.string + " found");
}, },
{ {
argCount: "+",
completer: function (filter) completer: function (filter)
{ {
var menu = document.getElementById("viewSidebarMenu"); var menu = document.getElementById("viewSidebarMenu");