mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-22 09:07:58 +01:00
merge improved :echo, :echoerr and :javascript commands
This commit is contained in:
@@ -483,18 +483,62 @@ function Commands() //{{{
|
||||
"Here, downloads can be paused, canceled and resumed."
|
||||
}
|
||||
));
|
||||
addDefaultCommand(new Command(["ec[ho]"],
|
||||
function(args) { vimperator.echo(args); } ,
|
||||
|
||||
function argToString(arg, color)
|
||||
{
|
||||
if (!arg)
|
||||
return "";
|
||||
|
||||
try
|
||||
{
|
||||
// TODO: move to vimperator.eval()?
|
||||
arg = eval(arg);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
vimperator.echoerr(e.toString());
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof arg === "object")
|
||||
arg = vimperator.objectToString(arg, color);
|
||||
else if (typeof arg === "function")
|
||||
arg = arg.toString().replace(/</g, "<").replace(/>/, ">");
|
||||
else if (typeof arg === "number" || typeof arg === "boolean")
|
||||
arg = "" + arg;
|
||||
else if (typeof arg === "undefined")
|
||||
arg = "undefined";
|
||||
|
||||
return arg;
|
||||
}
|
||||
addDefaultCommand(new Command(["ec[ho]"],
|
||||
function(args)
|
||||
{
|
||||
var res = argToString(args, true);
|
||||
if (res != null)
|
||||
vimperator.echo(res);
|
||||
},
|
||||
{
|
||||
usage: ["ec[ho] {expr}"],
|
||||
short_help: "Display a string at the bottom of the window",
|
||||
help: "Echo all arguments of this command. Useful for showing informational messages.<br/>Multiple lines can be separated by \\n."
|
||||
help: "Useful for showing informational messages. Multiple lines can be separated by \\n.<br/>" +
|
||||
"<code class=\"argument\">{expr}</code> can either be a quoted string, or any expression which can be fed to eval() like 4+5. " +
|
||||
"You can also view the source code of objects and functions if the return value of <code class=\"argument\">{expr}</code> is an object or function.",
|
||||
completer: function(filter) { return vimperator.completion.javascript(filter); }
|
||||
}
|
||||
));
|
||||
addDefaultCommand(new Command(["echoe[rr]"],
|
||||
function(args) { vimperator.echoerr(args); } ,
|
||||
function(args)
|
||||
{
|
||||
var res = argToString(args, false);
|
||||
if (res != null)
|
||||
vimperator.echoerr(res);
|
||||
},
|
||||
{
|
||||
usage: ["echoe[rr] {expr}"],
|
||||
short_help: "Display an error string at the bottom of the window",
|
||||
help: "Echo all arguments of this command highlighted in red. Useful for showing important messages."
|
||||
help: "Just like <code class=\"command\">:ec[ho]</code>, but echoes the result highlighted in red. Useful for showing important messages.",
|
||||
completer: function(filter) { return vimperator.completion.javascript(filter); }
|
||||
}
|
||||
));
|
||||
addDefaultCommand(new Command(["exe[cute]"],
|
||||
@@ -601,7 +645,11 @@ function Commands() //{{{
|
||||
help: "Acts as a JavaScript interpreter by passing the argument to <code>eval()</code>.<br/>" +
|
||||
"<code class=\"command\">:javascript alert('Hello world')</code> would show a dialog box with the text \"Hello world\".<br/>" +
|
||||
"<code class=\"command\">:javascript <<EOF</code> would read all the lines until a line starting with 'EOF' is found, and will <code>eval()</code> them.<br/>" +
|
||||
"The special version <code class=\"command\">:javascript!</code> will open the JavaScript console of Firefox."
|
||||
"The special version <code class=\"command\">:javascript!</code> will open the JavaScript console of Firefox.<br/>" +
|
||||
"Rudimentary <code class=\"mapping\"><Tab></code> completion is available for <code class=\"command\">:javascript {cmd}<Tab></code> (but not yet for the " +
|
||||
"<code class=\"command\">:js <<EOF</code> multiline widget). Be aware that Vimperator needs to run {cmd} through eval() " +
|
||||
"to get the completions, which could have unwanted side effects.",
|
||||
completer: function(filter) { return vimperator.completion.javascript(filter); }
|
||||
}
|
||||
));
|
||||
addDefaultCommand(new Command(["let"],
|
||||
@@ -915,7 +963,7 @@ function Commands() //{{{
|
||||
"</ol>" +
|
||||
"You WILL be able to use <code class=\"command\">:open [-T \"linux\"] torvalds<Tab></code> to complete bookmarks " +
|
||||
"with tag \"linux\" and which contain \"torvalds\". Note that -T support is only available for tab completion, not for the actual command.<br/>" +
|
||||
"The items which are completed on <code><Tab></code> are specified in the <code class=\"option\">'complete'</code> option.<br/>" +
|
||||
"The items which are completed on <code class=\"mapping\"><Tab></code> are specified in the <code class=\"option\">'complete'</code> option.<br/>" +
|
||||
"Without argument, reloads the current page.<br/>" +
|
||||
"Without argument but with <code class=\"command\">!</code>, reloads the current page skipping the cache.",
|
||||
completer: function(filter) { return vimperator.completion.get_url_completions(filter); }
|
||||
|
||||
@@ -473,6 +473,73 @@ vimperator.completion = (function() // {{{
|
||||
return build_longest_common_substring(mapped, filter);
|
||||
}, //}}}
|
||||
|
||||
javascript: function(str)
|
||||
{
|
||||
g_substrings = [];
|
||||
var matches = str.match(/^(.*?)(\s*\.\s*)?(\w*)$/);
|
||||
var object = "window";
|
||||
var filter = matches[3] || "";
|
||||
var start = matches[1].length-1;
|
||||
if (matches[2])
|
||||
{
|
||||
var brackets = 0, parentheses = 0;
|
||||
outer:
|
||||
for (; start >= 0; start--)
|
||||
{
|
||||
switch (matches[1][start])
|
||||
{
|
||||
case ";":
|
||||
case "{":
|
||||
break outer;
|
||||
|
||||
case "]":
|
||||
brackets--;
|
||||
break;
|
||||
case "[":
|
||||
brackets++;
|
||||
break;
|
||||
case ")":
|
||||
parentheses--;
|
||||
break;
|
||||
case "(":
|
||||
parentheses++;
|
||||
break;
|
||||
}
|
||||
if (brackets > 0 || parentheses > 0)
|
||||
break outer;
|
||||
}
|
||||
}
|
||||
object = matches[1].substr(start+1) || "window";
|
||||
|
||||
var completions = [];
|
||||
try
|
||||
{
|
||||
completions = eval(
|
||||
"var comp = [];" +
|
||||
"var type = '';" +
|
||||
"var value = '';" +
|
||||
"var obj = eval(" + object + ");" +
|
||||
"for (var i in obj) {" +
|
||||
" try { type = typeof(obj[i]); } catch (e) { type = 'unknown type'; };" +
|
||||
" if (type == 'number' || type == 'string' || type == 'boolean') {" +
|
||||
" value = obj[i];" +
|
||||
" comp.push([[i], type + ': ' + value]); }" +
|
||||
// The problem with that is that you complete vimperator.
|
||||
// but can't press <Tab> to complete sub items
|
||||
// so it's better to complete vimperator and the user can do
|
||||
// .<tab> to get the sub items
|
||||
//" else if (type == 'function') {" +
|
||||
//" comp.push([[i+'('], type]); }" +
|
||||
//" else if (type == 'object') {" +
|
||||
//" comp.push([[i+'.'], type]); }" +
|
||||
" else {" +
|
||||
" comp.push([[i], type]); }" +
|
||||
"} comp;");
|
||||
} catch (e) { completions = []; };
|
||||
|
||||
return build_longest_starting_substring(completions, filter);
|
||||
},
|
||||
|
||||
exTabCompletion: function(str) //{{{
|
||||
{
|
||||
var [count, cmd, special, args] = vimperator.commands.parseCommand(str);
|
||||
|
||||
@@ -434,36 +434,87 @@ const vimperator = (function() //{{{
|
||||
}
|
||||
},
|
||||
|
||||
// logs a message to the javascript error console
|
||||
log: function(msg, level)
|
||||
// if color = true it uses HTML markup to color certain items
|
||||
objectToString: function(object, color)
|
||||
{
|
||||
// if (Options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst
|
||||
console_service.logStringMessage('vimperator: ' + msg);
|
||||
},
|
||||
|
||||
// logs an object to the javascript error console also prints all
|
||||
// properties of the object
|
||||
logObject: function(object, level)
|
||||
{
|
||||
if (typeof object != 'object')
|
||||
if (object === null)
|
||||
return "null";
|
||||
if (typeof object != "object")
|
||||
return false;
|
||||
|
||||
var string = object + '::\n';
|
||||
var string = "";
|
||||
var obj = "";
|
||||
try { // for window.JSON
|
||||
obj = object.toString();
|
||||
} catch (e) {
|
||||
obj = "<Object>";
|
||||
}
|
||||
|
||||
if (color)
|
||||
string += "<span style=\"color: magenta; font-weight: bold;\">" + obj + "</span>::\n";
|
||||
else
|
||||
string += obj + "::\n";
|
||||
|
||||
for (var i in object)
|
||||
{
|
||||
var value;
|
||||
try
|
||||
{
|
||||
var value = object[i];
|
||||
if (i == "JSON") // without this ugly hack, ":echo window" does not work
|
||||
value = "[object JSON]";
|
||||
else
|
||||
value = object[i];
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
value = "";
|
||||
}
|
||||
|
||||
string += i + ": " + value + "\n";
|
||||
if (color)
|
||||
{
|
||||
// syntax highlighting for special items
|
||||
if (typeof value === "number")
|
||||
value = "<span style=\"color: red;\">" + value + "</span>";
|
||||
else if (typeof value === "string")
|
||||
{
|
||||
value = value.replace(/\n/, "\\n").replace(/</, "<");
|
||||
value = "<span style=\"color: green;\">\"" + value + "\"</span>";
|
||||
}
|
||||
else if (typeof value === "boolean")
|
||||
value = "<span style=\"color: blue;\">" + value + "</span>";
|
||||
else if (value == null || value == "undefined")
|
||||
value = "<span style=\"color: blue;\">" + value + "</span>";
|
||||
else if (typeof value === "object" || typeof value === "function")
|
||||
{
|
||||
// for java packages value.toString() would crash so badly
|
||||
// that we cannot even try/catch it
|
||||
if (/^\[JavaPackage.*\]$/.test(value))
|
||||
value = "[JavaPackage]";
|
||||
else
|
||||
{
|
||||
var str = value.toString();
|
||||
if (typeof str == "string") // can be "undefined"
|
||||
value = str.replace(/</g, "<").replace(/>/g, ">");
|
||||
}
|
||||
}
|
||||
|
||||
string += "<span style=\"font-weight: bold;\">" + i + "</span>: " + value + "\n";
|
||||
}
|
||||
else
|
||||
string += i + ": " + value + "\n";
|
||||
}
|
||||
vimperator.log(string, level);
|
||||
return string;
|
||||
},
|
||||
|
||||
// logs a message to the javascript error console
|
||||
// if msg is an object, it is beautified
|
||||
log: function(msg, level)
|
||||
{
|
||||
//if (Options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst
|
||||
if (typeof msg == "object")
|
||||
msg = this.objectToString(msg, false);
|
||||
|
||||
console_service.logStringMessage('vimperator: ' + msg);
|
||||
},
|
||||
|
||||
// open one or more URLs
|
||||
|
||||
Reference in New Issue
Block a user