mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-21 21:47:59 +01:00
merge vimperator.execute and the execute_command() global function
This commit is contained in:
@@ -191,6 +191,52 @@ function Commands() //{{{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// FIXME: doesn't really belong here...
|
||||
// return [null, null, null, null, heredoc_tag || false];
|
||||
// [count, cmd, special, args] = match;
|
||||
this.parseCommand = function(string, tag)
|
||||
{
|
||||
// removing comments
|
||||
string.replace(/\s*".*$/, '');
|
||||
if (tag) // we already have a multiline heredoc construct
|
||||
{
|
||||
if (string == tag)
|
||||
return [null, null, null, null, false];
|
||||
else
|
||||
return [null, null, null, string, tag];
|
||||
}
|
||||
|
||||
// 0 - count, 1 - cmd, 2 - special, 3 - args, 4 - heredoc tag
|
||||
var matches = string.match(/^:*(\d+)?([a-zA-Z]+)(!)?(?:\s+(.*?)\s*)?$/);
|
||||
if (!matches)
|
||||
return [null, null, null, null, null];
|
||||
matches.shift();
|
||||
|
||||
// parse count
|
||||
if (matches[0])
|
||||
{
|
||||
matches[0] = parseInt(matches[0]);
|
||||
if (isNaN(matches[0]))
|
||||
matches[0] = 0; // 0 is the default if no count given
|
||||
}
|
||||
else
|
||||
matches[0] = 0;
|
||||
|
||||
matches[2] = !!matches[2];
|
||||
matches.push(null);
|
||||
if (matches[3])
|
||||
{
|
||||
tag = matches[3].match(/<<\s*(\w+)\s*$/);
|
||||
if (tag && tag[1])
|
||||
matches[4] = tag[1];
|
||||
}
|
||||
else
|
||||
matches[3] = '';
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////}}}
|
||||
////////////////////// DEFAULT COMMANDS ////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////{{{
|
||||
@@ -1119,82 +1165,6 @@ function Commands() //{{{
|
||||
//}}}
|
||||
} //}}}
|
||||
|
||||
function execute_command(count, cmd, special, args, modifiers) //{{{
|
||||
{
|
||||
if (!cmd)
|
||||
return;
|
||||
|
||||
if (!modifiers)
|
||||
modifiers = {};
|
||||
|
||||
var command = vimperator.commands.get(cmd);
|
||||
if (command === null)
|
||||
{
|
||||
vimperator.echoerr("E492: Not an editor command: " + cmd);
|
||||
vimperator.focusContent();
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: need to perform this test? -- djk
|
||||
if (command.action === null)
|
||||
{
|
||||
vimperator.echoerr("E666: Internal error: command.action === null");
|
||||
return;
|
||||
}
|
||||
|
||||
// valid command, call it:
|
||||
command.execute(args, special, count, modifiers);
|
||||
|
||||
} //}}}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////}}}
|
||||
// Ex command parsing and execution ////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////{{{
|
||||
|
||||
// return [null, null, null, null, heredoc_tag || false];
|
||||
// [count, cmd, special, args] = match;
|
||||
function tokenize_ex(string, tag)
|
||||
{
|
||||
// removing comments
|
||||
string.replace(/\s*".*$/, '');
|
||||
if (tag) // we already have a multiline heredoc construct
|
||||
{
|
||||
if (string == tag)
|
||||
return [null, null, null, null, false];
|
||||
else
|
||||
return [null, null, null, string, tag];
|
||||
}
|
||||
|
||||
// 0 - count, 1 - cmd, 2 - special, 3 - args, 4 - heredoc tag
|
||||
var matches = string.match(/^:*(\d+)?([a-zA-Z]+)(!)?(?:\s+(.*?)\s*)?$/);
|
||||
if (!matches)
|
||||
return [null, null, null, null, null];
|
||||
matches.shift();
|
||||
|
||||
// parse count
|
||||
if (matches[0])
|
||||
{
|
||||
matches[0] = parseInt(matches[0]);
|
||||
if (isNaN(matches[0]))
|
||||
matches[0] = 0; // 0 is the default if no count given
|
||||
}
|
||||
else
|
||||
matches[0] = 0;
|
||||
|
||||
matches[2] = !!matches[2];
|
||||
matches.push(null);
|
||||
if (matches[3])
|
||||
{
|
||||
tag = matches[3].match(/<<\s*(\w+)\s*$/);
|
||||
if (tag && tag[1])
|
||||
matches[4] = tag[1];
|
||||
}
|
||||
else
|
||||
matches[3] = '';
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
/* takes a string like 'google bla, www.osnews.com'
|
||||
* and returns an array ['www.google.com/search?q=bla', 'www.osnews.com']
|
||||
*/
|
||||
|
||||
@@ -448,7 +448,7 @@ function get_buffer_completions(filter) //{{{
|
||||
|
||||
function exTabCompletion(str) //{{{
|
||||
{
|
||||
var [count, cmd, special, args] = tokenize_ex(str);
|
||||
var [count, cmd, special, args] = vimperator.commands.parseCommand(str);
|
||||
var completions = new Array;
|
||||
var start = 0;
|
||||
|
||||
|
||||
@@ -26,48 +26,6 @@ the provisions above, a recipient may use your version of this file under
|
||||
the terms of any one of the MPL, the GPL or the LGPL.
|
||||
}}} ***** END LICENSE BLOCK *****/
|
||||
|
||||
// XXX: move somehere else!
|
||||
// function multiliner(line, prev_match, heredoc) //{{{
|
||||
// {
|
||||
// var end = true;
|
||||
// var match = tokenize_ex(line, prev_match[4]);
|
||||
// if (prev_match[3] === undefined) prev_match[3] = '';
|
||||
// if (match[4] === null)
|
||||
// {
|
||||
// vimperator.focusContent();
|
||||
// execute_command.apply(this, match);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (match[4] === false)
|
||||
// {
|
||||
// prev_match[3] = prev_match[3].replace(new RegExp('<<\s*' + prev_match[4]), heredoc.replace(/\n$/, ''));
|
||||
// vimperator.focusContent(); // also sets comp_tab_index to -1
|
||||
// execute_command.apply(this, prev_match);
|
||||
// prev_match = new Array(5);
|
||||
// prev_match[3] = '';
|
||||
// heredoc = '';
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// end = false;
|
||||
// if (!prev_match[3])
|
||||
// {
|
||||
// prev_match[0] = match[0];
|
||||
// prev_match[1] = match[1];
|
||||
// prev_match[2] = match[2];
|
||||
// prev_match[3] = match[3];
|
||||
// prev_match[4] = match[4];
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// heredoc += match[3] + '\n';
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return [prev_match, heredoc, end];
|
||||
// } //}}}
|
||||
|
||||
/*
|
||||
* This class is used for prompting of user input and echoing of messages
|
||||
*
|
||||
|
||||
@@ -209,13 +209,32 @@ const vimperator = (function() //{{{
|
||||
|
||||
execute: function(string, modifiers)
|
||||
{
|
||||
if (!string)
|
||||
// skip comments and blank lines
|
||||
if (/^\s*("|$)/.test(string))
|
||||
return;
|
||||
|
||||
var tokens = tokenize_ex(string.replace(/^'(.*)'$/, '$1'));
|
||||
tokens[4] = modifiers;
|
||||
if (!modifiers)
|
||||
modifiers = {};
|
||||
|
||||
return execute_command.apply(this, tokens);
|
||||
var [count, cmd, special, args] = vimperator.commands.parseCommand(string.replace(/^'(.*)'$/, '$1'));
|
||||
var command = vimperator.commands.get(cmd);
|
||||
|
||||
if (command === null)
|
||||
{
|
||||
vimperator.echoerr("E492: Not an editor command: " + cmd);
|
||||
vimperator.focusContent();
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: need to perform this test? -- djk
|
||||
if (command.action === null)
|
||||
{
|
||||
vimperator.echoerr("E666: Internal error: command.action === null");
|
||||
return;
|
||||
}
|
||||
|
||||
// valid command, call it:
|
||||
command.execute(args, special, count, modifiers);
|
||||
},
|
||||
|
||||
// after pressing Escape, put focus on a non-input field of the browser document
|
||||
@@ -509,7 +528,8 @@ const vimperator = (function() //{{{
|
||||
{
|
||||
var heredoc = "";
|
||||
var heredocEnd = null; // the string which ends the heredoc
|
||||
s.split("\n").forEach(function(line) {
|
||||
s.split("\n").forEach(function(line)
|
||||
{
|
||||
if (heredocEnd) // we already are in a heredoc
|
||||
{
|
||||
if (line.search(heredocEnd) != -1)
|
||||
@@ -524,7 +544,7 @@ const vimperator = (function() //{{{
|
||||
else
|
||||
{
|
||||
// check for a heredoc
|
||||
var [count, cmd, special, args] = tokenize_ex(line);
|
||||
var [count, cmd, special, args] = vimperator.commands.parseCommand(line);
|
||||
var command = vimperator.commands.get(cmd);
|
||||
if (command && command.name == "javascript")
|
||||
{
|
||||
@@ -537,7 +557,7 @@ const vimperator = (function() //{{{
|
||||
}
|
||||
}
|
||||
else // execute a normal vimperator command
|
||||
execute_command(count, cmd, special, args);
|
||||
vimperator.execute(line);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user