2011-09-07 20:16:56 +02:00
|
|
|
#include "command_manager.hh"
|
|
|
|
|
|
|
|
#include "utils.hh"
|
2011-09-16 11:18:51 +02:00
|
|
|
#include "assert.hh"
|
2012-02-13 22:38:07 +01:00
|
|
|
#include "context.hh"
|
2012-05-03 09:25:13 +02:00
|
|
|
#include "shell_manager.hh"
|
2011-09-16 11:18:51 +02:00
|
|
|
|
2011-09-07 20:16:56 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-06-02 17:49:35 +02:00
|
|
|
bool CommandManager::command_defined(const String& command_name) const
|
|
|
|
{
|
|
|
|
return m_commands.find(command_name) != m_commands.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandManager::register_command(const String& command_name,
|
|
|
|
Command command,
|
2012-01-15 04:02:08 +01:00
|
|
|
unsigned flags,
|
2011-09-16 11:18:51 +02:00
|
|
|
const CommandCompleter& completer)
|
2011-09-07 20:16:56 +02:00
|
|
|
{
|
2012-01-15 04:02:08 +01:00
|
|
|
m_commands[command_name] = CommandDescriptor { command, flags, completer };
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-02 17:49:35 +02:00
|
|
|
void CommandManager::register_commands(const memoryview<String>& command_names,
|
|
|
|
Command command,
|
2012-02-03 20:14:35 +01:00
|
|
|
unsigned flags,
|
|
|
|
const CommandCompleter& completer)
|
2011-09-07 20:16:56 +02:00
|
|
|
{
|
|
|
|
for (auto command_name : command_names)
|
2012-01-15 04:02:08 +01:00
|
|
|
register_command(command_name, command, flags, completer);
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|
|
|
|
|
2012-02-13 22:38:07 +01:00
|
|
|
static bool is_blank(char c)
|
|
|
|
{
|
|
|
|
return c == ' ' or c == '\t' or c == '\n';
|
|
|
|
}
|
|
|
|
|
2012-07-31 00:06:50 +02:00
|
|
|
using TokenList = std::vector<Token>;
|
|
|
|
using TokenPosList = std::vector<std::pair<size_t, size_t>>;
|
|
|
|
|
|
|
|
static TokenList parse(const String& line,
|
|
|
|
TokenPosList* opt_token_pos_info = NULL)
|
2011-09-07 20:16:56 +02:00
|
|
|
{
|
2011-09-13 23:16:48 +02:00
|
|
|
TokenList result;
|
2011-09-07 20:16:56 +02:00
|
|
|
|
|
|
|
size_t pos = 0;
|
2011-11-26 18:20:02 +01:00
|
|
|
while (pos < line.length())
|
2011-09-07 20:16:56 +02:00
|
|
|
{
|
2012-02-13 22:38:07 +01:00
|
|
|
while(is_blank(line[pos]) and pos != line.length())
|
2011-09-07 20:16:56 +02:00
|
|
|
++pos;
|
|
|
|
|
2012-02-13 22:38:07 +01:00
|
|
|
size_t token_start = pos;
|
|
|
|
|
2012-07-31 00:06:50 +02:00
|
|
|
Token::Type type = Token::Type::Raw;
|
2012-02-13 22:38:07 +01:00
|
|
|
if (line[pos] == '"' or line[pos] == '\'' or line[pos] == '`')
|
2011-11-26 18:20:02 +01:00
|
|
|
{
|
2012-02-13 22:38:07 +01:00
|
|
|
char delimiter = line[pos];
|
2012-07-31 00:06:50 +02:00
|
|
|
|
|
|
|
if (delimiter == '`')
|
|
|
|
type = Token::Type::ShellExpand;
|
|
|
|
|
|
|
|
token_start = ++pos;
|
2011-11-26 18:20:02 +01:00
|
|
|
|
2012-02-13 22:38:07 +01:00
|
|
|
while ((line[pos] != delimiter or line[pos-1] == '\\') and
|
|
|
|
pos != line.length())
|
|
|
|
++pos;
|
2011-09-07 20:16:56 +02:00
|
|
|
|
2012-02-13 22:38:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
while (not is_blank(line[pos]) and pos != line.length() and
|
|
|
|
(line[pos] != ';' or line[pos-1] == '\\'))
|
|
|
|
++pos;
|
2011-09-07 20:16:56 +02:00
|
|
|
|
2012-02-13 22:38:07 +01:00
|
|
|
if (token_start != pos)
|
2012-07-31 00:06:50 +02:00
|
|
|
{
|
|
|
|
if (opt_token_pos_info)
|
|
|
|
opt_token_pos_info->push_back({token_start, pos});
|
|
|
|
result.push_back({type, line.substr(token_start, pos - token_start)});
|
|
|
|
}
|
2011-11-26 18:20:02 +01:00
|
|
|
|
2012-01-15 02:37:35 +01:00
|
|
|
if (line[pos] == ';')
|
2012-07-31 00:06:50 +02:00
|
|
|
{
|
|
|
|
if (opt_token_pos_info)
|
|
|
|
opt_token_pos_info->push_back({pos, pos+1});
|
|
|
|
result.push_back(Token{ Token::Type::CommandSeparator });
|
|
|
|
}
|
2012-01-15 02:37:35 +01:00
|
|
|
|
2011-11-26 18:20:02 +01:00
|
|
|
++pos;
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-09 20:40:59 +02:00
|
|
|
struct command_not_found : runtime_error
|
2011-09-07 20:16:56 +02:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
command_not_found(const String& command)
|
2011-09-09 20:40:59 +02:00
|
|
|
: runtime_error(command + " : no such command") {}
|
2011-09-07 20:16:56 +02:00
|
|
|
};
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
void CommandManager::execute(const String& command_line,
|
2012-05-29 07:42:26 +02:00
|
|
|
const Context& context,
|
|
|
|
const EnvVarMap& env_vars)
|
2011-09-07 20:16:56 +02:00
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
TokenList tokens = parse(command_line);
|
|
|
|
execute(tokens, context, env_vars);
|
2011-11-26 19:32:57 +01:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:06:50 +02:00
|
|
|
static void shell_eval(TokenList& params,
|
2012-04-14 03:17:09 +02:00
|
|
|
const String& cmdline,
|
2012-05-29 07:42:26 +02:00
|
|
|
const Context& context,
|
|
|
|
const EnvVarMap& env_vars)
|
2012-02-13 22:38:07 +01:00
|
|
|
{
|
2012-05-29 07:42:26 +02:00
|
|
|
String output = ShellManager::instance().eval(cmdline, context, env_vars);
|
2012-07-31 00:06:50 +02:00
|
|
|
TokenList tokens = parse(output);
|
2012-02-13 22:38:07 +01:00
|
|
|
|
2012-07-31 00:06:50 +02:00
|
|
|
for (auto& token : tokens)
|
|
|
|
params.push_back(std::move(token));
|
2012-02-13 22:38:07 +01:00
|
|
|
}
|
|
|
|
|
2012-01-14 15:02:54 +01:00
|
|
|
void CommandManager::execute(const CommandParameters& params,
|
2012-05-29 07:42:26 +02:00
|
|
|
const Context& context,
|
|
|
|
const EnvVarMap& env_vars)
|
2011-11-26 19:32:57 +01:00
|
|
|
{
|
2012-01-14 15:02:54 +01:00
|
|
|
if (params.empty())
|
|
|
|
return;
|
|
|
|
|
2012-01-15 02:37:35 +01:00
|
|
|
auto begin = params.begin();
|
|
|
|
auto end = begin;
|
|
|
|
while (true)
|
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
while (end != params.end() and end->type() != Token::Type::CommandSeparator)
|
2012-01-15 02:37:35 +01:00
|
|
|
++end;
|
2011-11-26 19:32:57 +01:00
|
|
|
|
2012-01-15 02:37:35 +01:00
|
|
|
if (end != begin)
|
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
if (begin->type() != Token::Type::Raw)
|
|
|
|
throw command_not_found("unable to parse command name");
|
|
|
|
auto command_it = m_commands.find(begin->content());
|
2012-01-15 02:37:35 +01:00
|
|
|
if (command_it == m_commands.end())
|
2012-07-31 00:06:50 +02:00
|
|
|
throw command_not_found(begin->content());
|
2012-01-15 02:37:35 +01:00
|
|
|
|
2012-01-15 04:02:08 +01:00
|
|
|
if (command_it->second.flags & IgnoreSemiColons)
|
|
|
|
end = params.end();
|
|
|
|
|
2012-02-13 22:38:07 +01:00
|
|
|
if (command_it->second.flags & DeferredShellEval)
|
|
|
|
command_it->second.command(CommandParameters(begin + 1, end), context);
|
|
|
|
else
|
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
TokenList expanded_tokens;
|
2012-02-13 22:38:07 +01:00
|
|
|
for (auto param = begin+1; param != end; ++param)
|
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
if (param->type() == Token::Type::ShellExpand)
|
|
|
|
shell_eval(expanded_tokens, param->content(),
|
2012-05-29 07:42:26 +02:00
|
|
|
context, env_vars);
|
2012-02-13 22:38:07 +01:00
|
|
|
else
|
2012-07-31 00:06:50 +02:00
|
|
|
expanded_tokens.push_back(*param);
|
2012-02-13 22:38:07 +01:00
|
|
|
}
|
2012-07-31 00:06:50 +02:00
|
|
|
command_it->second.command(expanded_tokens, context);
|
2012-02-13 22:38:07 +01:00
|
|
|
}
|
2012-01-15 02:37:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (end == params.end())
|
|
|
|
break;
|
|
|
|
|
|
|
|
begin = end+1;
|
|
|
|
end = begin;
|
|
|
|
}
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
Completions CommandManager::complete(const String& command_line, size_t cursor_pos)
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
TokenPosList pos_info;
|
|
|
|
TokenList tokens = parse(command_line, &pos_info);
|
2011-09-13 23:16:48 +02:00
|
|
|
|
2011-11-26 20:05:49 +01:00
|
|
|
size_t token_to_complete = tokens.size();
|
2011-09-13 23:16:48 +02:00
|
|
|
for (size_t i = 0; i < tokens.size(); ++i)
|
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
if (pos_info[i].first <= cursor_pos and pos_info[i].second >= cursor_pos)
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
|
|
|
token_to_complete = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 20:50:28 +02:00
|
|
|
if (token_to_complete == 0 or tokens.empty()) // command name completion
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
size_t cmd_start = tokens.empty() ? 0 : pos_info[0].first;
|
2011-10-19 20:50:28 +02:00
|
|
|
Completions result(cmd_start, cursor_pos);
|
2012-04-14 03:17:09 +02:00
|
|
|
String prefix = command_line.substr(cmd_start,
|
|
|
|
cursor_pos - cmd_start);
|
2011-09-13 23:16:48 +02:00
|
|
|
|
|
|
|
for (auto& command : m_commands)
|
|
|
|
{
|
|
|
|
if (command.first.substr(0, prefix.length()) == prefix)
|
|
|
|
result.candidates.push_back(command.first);
|
|
|
|
}
|
2012-06-02 17:49:56 +02:00
|
|
|
std::sort(result.candidates.begin(), result.candidates.end());
|
2011-09-13 23:16:48 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2011-09-16 11:18:51 +02:00
|
|
|
|
|
|
|
assert(not tokens.empty());
|
2012-07-31 00:06:50 +02:00
|
|
|
if (tokens[0].type() != Token::Type::Raw)
|
|
|
|
return Completions();
|
|
|
|
|
|
|
|
const String& command_name = tokens[0].content();
|
2011-09-16 11:18:51 +02:00
|
|
|
|
|
|
|
auto command_it = m_commands.find(command_name);
|
|
|
|
if (command_it == m_commands.end() or not command_it->second.completer)
|
|
|
|
return Completions();
|
|
|
|
|
2011-11-26 20:05:49 +01:00
|
|
|
size_t start = token_to_complete < tokens.size() ?
|
2012-07-31 00:06:50 +02:00
|
|
|
pos_info[token_to_complete].first : cursor_pos;
|
2011-11-26 20:05:49 +01:00
|
|
|
Completions result(start , cursor_pos);
|
|
|
|
size_t cursor_pos_in_token = cursor_pos - start;
|
2011-09-16 11:18:51 +02:00
|
|
|
|
2012-07-31 00:06:50 +02:00
|
|
|
CommandParameters params(tokens.begin()+1, tokens.end());
|
2011-09-16 11:18:51 +02:00
|
|
|
result.candidates = command_it->second.completer(params,
|
|
|
|
token_to_complete - 1,
|
|
|
|
cursor_pos_in_token);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CandidateList PerArgumentCommandCompleter::operator()(const CommandParameters& params,
|
|
|
|
size_t token_to_complete,
|
|
|
|
size_t pos_in_token) const
|
|
|
|
{
|
2012-07-31 00:06:50 +02:00
|
|
|
if (token_to_complete >= m_completers.size() or
|
|
|
|
(token_to_complete < params.size() and
|
|
|
|
params[token_to_complete].type() != Token::Type::Raw))
|
2011-09-16 11:18:51 +02:00
|
|
|
return CandidateList();
|
|
|
|
|
|
|
|
// it is possible to try to complete a new argument
|
|
|
|
assert(token_to_complete <= params.size());
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
const String& argument = token_to_complete < params.size() ?
|
2012-07-31 00:06:50 +02:00
|
|
|
params[token_to_complete].content() : String();
|
2011-09-16 11:18:51 +02:00
|
|
|
return m_completers[token_to_complete](argument, pos_in_token);
|
2011-09-13 23:16:48 +02:00
|
|
|
}
|
|
|
|
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|