mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-01-06 10:34:13 +01:00
simple tab-completion for :javascript and :echo, really just a quick time saver, but prone to errors
This commit is contained in:
@@ -523,7 +523,8 @@ function Commands() //{{{
|
||||
short_help: "Display a string at the bottom of the window",
|
||||
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."
|
||||
"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]"],
|
||||
@@ -536,7 +537,8 @@ function Commands() //{{{
|
||||
{
|
||||
usage: ["echoe[rr] {expr}"],
|
||||
short_help: "Display an error string at the bottom of the window",
|
||||
help: "Just like <code class=\"command\">:ec[ho]</code>, but echoes the result 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]"],
|
||||
@@ -643,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"],
|
||||
@@ -957,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); }
|
||||
|
||||
@@ -479,6 +479,74 @@ 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--)
|
||||
{
|
||||
dump(matches[1].substr(start) + ": " + start + "\n");
|
||||
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);
|
||||
@@ -498,14 +566,27 @@ vimperator.completion = (function() // {{{
|
||||
var command = vimperator.commands.get(cmd);
|
||||
if (command && command.completer)
|
||||
{
|
||||
matches = str.match(/^:*\d*\w+\s+/);
|
||||
start = matches ? matches[0].length : 0;
|
||||
|
||||
// TODO: maybe we should move these checks to the complete functions
|
||||
if (command.hasName("open") || command.hasName("tabopen") || command.hasName("winopen"))
|
||||
{
|
||||
var skip = args.match(/^(.*,\s+)(.*)/); // start after the last ", "
|
||||
if (skip)
|
||||
{
|
||||
start += skip[1].length;
|
||||
args = skip[2];
|
||||
}
|
||||
}
|
||||
else if (command.hasName("echo") || command.hasName("echoerr") || command.hasName("javascript"))
|
||||
{
|
||||
var skip = args.match(/^(.*?)(\w*)$/); // start after the last ", "
|
||||
if (skip)
|
||||
start += skip[1].length;
|
||||
}
|
||||
|
||||
completions = command.completer.call(this, args);
|
||||
// if (command[0][0] == "open" ||
|
||||
// command[0][0] == "tabopen" ||
|
||||
// command[0][0] == "winopen")
|
||||
// start = str.search(/^:*\d*\w+(\s+|.*\|)/); // up to the last | or the first space
|
||||
// else
|
||||
matches = str.match(/^:*\d*\w+\s+/); // up to the first spaces only
|
||||
start = matches[0].length;
|
||||
}
|
||||
}
|
||||
return [start, completions];
|
||||
|
||||
Reference in New Issue
Block a user