1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 14:02:28 +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",
function (args)
{
if (!args)
{
liberator.echoerr("E471: Argument required");
return;
}
var matches = args.match(/^([a-zA-Z0-9])(?:\s+(.+))?$/);
var matches = args.string.match(/^([a-zA-Z0-9])(?:\s+(.+))?$/);
if (!matches)
liberator.echoerr("E488: Trailing characters");
else if (!matches[2])
liberator.quickmarks.add(matches[1], liberator.buffer.URL);
else
liberator.quickmarks.add(matches[1], matches[2]);
});
},
{ argCount: "+" });
liberator.commands.add(["qmarks"],
"Show all QuickMarks",

View File

@@ -1535,24 +1535,21 @@ liberator.Marks = function () //{{{
"Mark current location within the web page",
function (args)
{
if (!args)
{
liberator.echoerr("E471: Argument required");
return;
}
if (args.length > 1)
var mark = args.arguments[0];
if (mark.length > 1)
{
liberator.echoerr("E488: Trailing characters");
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");
return;
}
liberator.marks.add(args);
});
liberator.marks.add(mark);
},
{ argCount: "1" });
liberator.commands.add(["marks"],
"Show all location marks of current web page",

View File

@@ -397,6 +397,7 @@ liberator.Commands = function () //{{{
var args = {}; // parsed options
args.arguments = []; // remaining arguments
args.string = str; // for access to the unparsed string
var invalid = false;
var onlyArgumentsRemaining = allowUnknownOptions || false; // after a -- has been found
@@ -571,11 +572,15 @@ liberator.Commands = function () //{{{
}
// check for correct number of arguments
if ((args.arguments.length == 0 && (argCount == "1" || argCount == "+")) ||
(args.arguments.length == 1 && (argCount == "0")) ||
(args.arguments.length > 1 && (argCount == "0" || argCount == "1" || argCount == "?")))
if (args.arguments.length == 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;
}
@@ -685,41 +690,26 @@ liberator.Commands = function () //{{{
"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);
});
},
{ argCount: "0" });
// 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)
var cmd = args.arguments[0];
var commands = getUserCommands(cmd);
if (commands.length == 1 && cmd == 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",
// function () { liberator.previewwindow.hide(); });
liberator.echoerr("E184: No such user-defined command: " + cmd);
},
{ argCount: "1" });
//}}}

View File

@@ -91,11 +91,9 @@ liberator.IO = function () //{{{
"Print the current directory name",
function (args)
{
if (args)
liberator.echoerr("E488: Trailing characters");
else
liberator.echo(liberator.io.getCurrentDirectory());
});
liberator.echo(liberator.io.getCurrentDirectory());
},
{ argCount: "0" });
// mkv[imperatorrc] or mkm[uttatorrc]
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"
var filename;
if (args)
{
filename = args;
}
else
{
filename = (navigator.platform == "Win32") ? "~/_" : "~/.";

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -292,45 +292,35 @@ liberator.config = { //{{{
"Close the sidebar window",
function (args)
{
if (args)
{
liberator.echoerr("E488: Trailing characters");
return;
}
if (document.getElementById("sidebar-box").hidden == false)
toggleSidebar();
});
},
{ argCount: "0" });
liberator.commands.add(["sideb[ar]", "sb[ar]", "sbope[n]"],
"Open the sidebar window",
function (args)
{
if (!args)
{
liberator.echoerr("E471: Argument required");
return;
}
// 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();
return;
}
var menu = document.getElementById("viewSidebarMenu");
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();
break;
return;
}
}
liberator.echoerr("No sidebar " + args.string + " found");
},
{
argCount: "+",
completer: function (filter)
{
var menu = document.getElementById("viewSidebarMenu");