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';
|
|
|
|
}
|
|
|
|
|
2011-09-13 23:16:48 +02:00
|
|
|
typedef std::vector<std::pair<size_t, size_t>> TokenList;
|
2012-04-14 03:17:09 +02:00
|
|
|
static TokenList split(const String& line)
|
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;
|
|
|
|
|
|
|
|
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];
|
2011-11-26 18:20:02 +01:00
|
|
|
++pos;
|
2012-02-13 22:38:07 +01:00
|
|
|
token_start = delimiter == '`' ? pos - 1 : 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
|
|
|
if (delimiter == '`' and line[pos] == '`')
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
result.push_back(std::make_pair(token_start, pos));
|
2011-11-26 18:20:02 +01:00
|
|
|
|
2012-01-15 02:37:35 +01:00
|
|
|
if (line[pos] == ';')
|
|
|
|
result.push_back(std::make_pair(pos, pos+1));
|
|
|
|
|
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
|
|
|
{
|
2011-09-13 23:16:48 +02:00
|
|
|
TokenList tokens = split(command_line);
|
2011-09-07 20:16:56 +02:00
|
|
|
if (tokens.empty())
|
|
|
|
return;
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
std::vector<String> params;
|
2012-01-14 15:02:54 +01:00
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); ++it)
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
|
|
|
params.push_back(command_line.substr(it->first,
|
|
|
|
it->second - it->first));
|
|
|
|
}
|
2011-09-07 20:16:56 +02:00
|
|
|
|
2012-05-29 07:42:26 +02:00
|
|
|
execute(params, context, env_vars);
|
2011-11-26 19:32:57 +01:00
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
static void shell_eval(std::vector<String>& params,
|
|
|
|
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-02-13 22:38:07 +01:00
|
|
|
TokenList tokens = split(output);
|
|
|
|
|
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); ++it)
|
|
|
|
{
|
|
|
|
params.push_back(output.substr(it->first,
|
|
|
|
it->second - it->first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
while (end != params.end() and *end != ";")
|
|
|
|
++end;
|
2011-11-26 19:32:57 +01:00
|
|
|
|
2012-01-15 02:37:35 +01:00
|
|
|
if (end != begin)
|
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
std::vector<String> expanded_params;
|
2012-01-15 02:37:35 +01:00
|
|
|
auto command_it = m_commands.find(*begin);
|
2012-02-15 15:19:57 +01:00
|
|
|
|
|
|
|
if (command_it == m_commands.end() and
|
|
|
|
begin->front() == '`' and begin->back() == '`')
|
|
|
|
{
|
|
|
|
shell_eval(expanded_params,
|
|
|
|
begin->substr(1, begin->length() - 2),
|
2012-05-29 07:42:26 +02:00
|
|
|
context, env_vars);
|
2012-02-15 15:19:57 +01:00
|
|
|
if (not expanded_params.empty())
|
|
|
|
{
|
|
|
|
command_it = m_commands.find(expanded_params[0]);
|
|
|
|
expanded_params.erase(expanded_params.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-15 02:37:35 +01:00
|
|
|
if (command_it == m_commands.end())
|
|
|
|
throw command_not_found(*begin);
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
for (auto param = begin+1; param != end; ++param)
|
|
|
|
{
|
|
|
|
if (param->front() == '`' and param->back() == '`')
|
|
|
|
shell_eval(expanded_params,
|
|
|
|
param->substr(1, param->length() - 2),
|
2012-05-29 07:42:26 +02:00
|
|
|
context, env_vars);
|
2012-02-13 22:38:07 +01:00
|
|
|
else
|
|
|
|
expanded_params.push_back(*param);
|
|
|
|
}
|
|
|
|
command_it->second.command(expanded_params, context);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
TokenList tokens = split(command_line);
|
|
|
|
|
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)
|
|
|
|
{
|
2011-10-19 20:50:28 +02:00
|
|
|
if (tokens[i].first <= cursor_pos and tokens[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
|
|
|
{
|
2011-10-19 20:50:28 +02:00
|
|
|
size_t cmd_start = tokens.empty() ? 0 : tokens[0].first;
|
|
|
|
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-04-14 03:17:09 +02:00
|
|
|
String command_name =
|
2011-09-16 11:18:51 +02:00
|
|
|
command_line.substr(tokens[0].first,
|
|
|
|
tokens[0].second - tokens[0].first);
|
|
|
|
|
|
|
|
auto command_it = m_commands.find(command_name);
|
|
|
|
if (command_it == m_commands.end() or not command_it->second.completer)
|
|
|
|
return Completions();
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
std::vector<String> params;
|
2011-09-16 11:18:51 +02:00
|
|
|
for (auto it = tokens.begin() + 1; it != tokens.end(); ++it)
|
2011-09-14 17:41:56 +02:00
|
|
|
{
|
2011-09-16 11:18:51 +02:00
|
|
|
params.push_back(command_line.substr(it->first,
|
|
|
|
it->second - it->first));
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
2011-11-26 20:05:49 +01:00
|
|
|
|
|
|
|
size_t start = token_to_complete < tokens.size() ?
|
|
|
|
tokens[token_to_complete].first : cursor_pos;
|
|
|
|
Completions result(start , cursor_pos);
|
|
|
|
size_t cursor_pos_in_token = cursor_pos - start;
|
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
|
|
|
|
{
|
|
|
|
if (token_to_complete >= m_completers.size())
|
|
|
|
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() ?
|
|
|
|
params[token_to_complete] : 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
|
|
|
}
|