2011-09-07 22:16:56 +04:00
|
|
|
#include "command_manager.hh"
|
|
|
|
|
2011-09-16 13:18:51 +04:00
|
|
|
#include "assert.hh"
|
2012-02-14 01:38:07 +04:00
|
|
|
#include "context.hh"
|
2012-08-11 14:13:48 +04:00
|
|
|
#include "register_manager.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "shell_manager.hh"
|
|
|
|
#include "utils.hh"
|
2011-09-16 13:18:51 +04:00
|
|
|
|
2011-09-07 22:16:56 +04:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-06-02 19:49:35 +04:00
|
|
|
bool CommandManager::command_defined(const String& command_name) const
|
|
|
|
{
|
|
|
|
return m_commands.find(command_name) != m_commands.end();
|
|
|
|
}
|
|
|
|
|
2012-11-22 17:28:14 +04:00
|
|
|
void CommandManager::register_command(String command_name,
|
2012-06-02 19:49:35 +04:00
|
|
|
Command command,
|
2012-11-22 17:28:14 +04:00
|
|
|
CommandCompleter completer)
|
2011-09-07 22:16:56 +04:00
|
|
|
{
|
2012-11-22 17:28:14 +04:00
|
|
|
m_commands[command_name] = { std::move(command), std::move(completer) };
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|
|
|
|
|
2012-06-02 19:49:35 +04:00
|
|
|
void CommandManager::register_commands(const memoryview<String>& command_names,
|
|
|
|
Command command,
|
2012-11-22 17:28:14 +04:00
|
|
|
CommandCompleter completer)
|
2011-09-07 22:16:56 +04:00
|
|
|
{
|
|
|
|
for (auto command_name : command_names)
|
2012-11-22 17:28:14 +04:00
|
|
|
m_commands[command_name] = { command, completer };
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 16:56:52 +04:00
|
|
|
parse_error::parse_error(const String& error)
|
|
|
|
: runtime_error{"parse error: " + error} {}
|
|
|
|
|
|
|
|
|
2012-08-02 08:41:55 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2012-08-01 16:27:34 +04:00
|
|
|
struct Token
|
2012-02-14 01:38:07 +04:00
|
|
|
{
|
2012-08-01 16:27:34 +04:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
Raw,
|
|
|
|
ShellExpand,
|
2012-08-11 14:13:48 +04:00
|
|
|
RegisterExpand,
|
|
|
|
OptionExpand,
|
2012-08-01 16:27:34 +04:00
|
|
|
CommandSeparator
|
|
|
|
};
|
|
|
|
Token() : m_type(Type::Raw) {}
|
|
|
|
|
|
|
|
explicit Token(const String& string) : m_content(string), m_type(Type::Raw) {}
|
|
|
|
explicit Token(Type type) : m_type(type) {}
|
|
|
|
Token(Type type, String str) : m_content(str), m_type(type) {}
|
|
|
|
|
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
|
|
|
const String& content() const { return m_content; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type m_type;
|
|
|
|
String m_content;
|
|
|
|
};
|
|
|
|
|
2012-02-14 01:38:07 +04:00
|
|
|
|
2012-07-31 02:06:50 +04:00
|
|
|
using TokenList = std::vector<Token>;
|
2012-10-11 02:41:48 +04:00
|
|
|
using TokenPosList = std::vector<std::pair<ByteCount, ByteCount>>;
|
2012-07-31 02:06:50 +04:00
|
|
|
|
2012-10-08 21:33:53 +04:00
|
|
|
bool is_command_separator(char c)
|
2012-07-31 16:22:57 +04:00
|
|
|
{
|
|
|
|
return c == ';' or c == '\n';
|
|
|
|
}
|
|
|
|
|
2012-08-02 08:41:55 +04:00
|
|
|
bool is_horizontal_blank(char c)
|
2012-08-01 16:27:34 +04:00
|
|
|
{
|
|
|
|
return c == ' ' or c == '\t';
|
|
|
|
}
|
|
|
|
|
2012-11-21 16:56:52 +04:00
|
|
|
struct unterminated_string : parse_error
|
|
|
|
{
|
2013-05-13 16:23:07 +04:00
|
|
|
unterminated_string(const std::string& open, const std::string& close, int nest = 0)
|
2013-01-04 21:39:13 +04:00
|
|
|
: parse_error{"unterminated string '" + open + "..." + close + "'" +
|
2013-05-13 16:23:07 +04:00
|
|
|
(nest > 0 ? "(nesting: " + to_string(nest) + ")" : "")}
|
2012-11-21 16:56:52 +04:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2013-03-19 21:58:21 +04:00
|
|
|
struct unknown_expand : parse_error
|
|
|
|
{
|
|
|
|
unknown_expand(const String& name)
|
|
|
|
: parse_error{"unknown expand '" + name + "'"} {}
|
|
|
|
};
|
|
|
|
|
2012-08-02 08:41:55 +04:00
|
|
|
TokenList parse(const String& line,
|
2012-11-21 16:56:52 +04:00
|
|
|
TokenPosList* opt_token_pos_info = nullptr)
|
2011-09-07 22:16:56 +04:00
|
|
|
{
|
2011-09-14 01:16:48 +04:00
|
|
|
TokenList result;
|
2011-09-07 22:16:56 +04:00
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount length = line.length();
|
|
|
|
ByteCount pos = 0;
|
2012-07-31 16:22:57 +04:00
|
|
|
while (pos < length)
|
2011-09-07 22:16:56 +04:00
|
|
|
{
|
2012-07-31 16:22:57 +04:00
|
|
|
while (pos != length)
|
|
|
|
{
|
|
|
|
if (is_horizontal_blank(line[pos]))
|
|
|
|
++pos;
|
|
|
|
else if (line[pos] == '\\' and pos+1 < length and line[pos+1] == '\n')
|
|
|
|
pos += 2;
|
2012-09-04 02:04:49 +04:00
|
|
|
else if (line[pos] == '#')
|
|
|
|
{
|
|
|
|
while (pos != length and line[pos] != '\n')
|
|
|
|
++pos;
|
|
|
|
}
|
2012-07-31 16:22:57 +04:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2011-09-07 22:16:56 +04:00
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount token_start = pos;
|
2012-12-15 20:47:50 +04:00
|
|
|
ByteCount start_pos = pos;
|
2012-02-14 01:38:07 +04:00
|
|
|
|
2012-07-31 02:06:50 +04:00
|
|
|
Token::Type type = Token::Type::Raw;
|
2012-07-31 16:22:57 +04:00
|
|
|
if (line[pos] == '"' or line[pos] == '\'')
|
2011-11-26 21:20:02 +04:00
|
|
|
{
|
2012-02-14 01:38:07 +04:00
|
|
|
char delimiter = line[pos];
|
2012-07-31 02:06:50 +04:00
|
|
|
|
|
|
|
token_start = ++pos;
|
2011-11-26 21:20:02 +04:00
|
|
|
|
2012-02-14 01:38:07 +04:00
|
|
|
while ((line[pos] != delimiter or line[pos-1] == '\\') and
|
2012-07-31 16:22:57 +04:00
|
|
|
pos != length)
|
2012-02-14 01:38:07 +04:00
|
|
|
++pos;
|
2012-11-21 16:56:52 +04:00
|
|
|
if (pos == length)
|
|
|
|
throw unterminated_string(String{delimiter}, String{delimiter});
|
2012-07-31 16:22:57 +04:00
|
|
|
}
|
|
|
|
else if (line[pos] == '%')
|
|
|
|
{
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount type_start = ++pos;
|
2012-07-31 16:22:57 +04:00
|
|
|
while (isalpha(line[pos]))
|
|
|
|
++pos;
|
|
|
|
String type_name = line.substr(type_start, pos - type_start);
|
|
|
|
|
2013-03-19 21:58:21 +04:00
|
|
|
if (pos == length)
|
|
|
|
throw parse_error{"expected a string delimiter after '%" + type_name + "'"};
|
|
|
|
|
2012-07-31 16:22:57 +04:00
|
|
|
if (type_name == "sh")
|
|
|
|
type = Token::Type::ShellExpand;
|
2013-03-19 21:58:21 +04:00
|
|
|
else if (type_name == "reg")
|
2012-08-11 14:13:48 +04:00
|
|
|
type = Token::Type::RegisterExpand;
|
2013-03-19 21:58:21 +04:00
|
|
|
else if (type_name == "opt")
|
2012-08-11 14:13:48 +04:00
|
|
|
type = Token::Type::OptionExpand;
|
2013-03-19 21:58:21 +04:00
|
|
|
else if (type_name != "")
|
|
|
|
throw unknown_expand{type_name};
|
2011-09-07 22:16:56 +04:00
|
|
|
|
2012-10-08 21:33:53 +04:00
|
|
|
static const std::unordered_map<char, char> matching_delimiters = {
|
2012-07-31 16:22:57 +04:00
|
|
|
{ '(', ')' }, { '[', ']' }, { '{', '}' }, { '<', '>' }
|
|
|
|
};
|
|
|
|
|
2012-10-08 21:33:53 +04:00
|
|
|
char opening_delimiter = line[pos];
|
2012-07-31 16:22:57 +04:00
|
|
|
token_start = ++pos;
|
|
|
|
|
|
|
|
auto delim_it = matching_delimiters.find(opening_delimiter);
|
|
|
|
if (delim_it != matching_delimiters.end())
|
|
|
|
{
|
2012-10-08 21:33:53 +04:00
|
|
|
char closing_delimiter = delim_it->second;
|
2012-07-31 16:22:57 +04:00
|
|
|
int level = 0;
|
|
|
|
while (pos != length)
|
|
|
|
{
|
|
|
|
if (line[pos-1] != '\\' and line[pos] == opening_delimiter)
|
|
|
|
++level;
|
|
|
|
if (line[pos-1] != '\\' and line[pos] == closing_delimiter)
|
|
|
|
{
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
2012-11-21 16:56:52 +04:00
|
|
|
if (pos == length)
|
|
|
|
throw unterminated_string("%" + type_name + opening_delimiter,
|
|
|
|
String{closing_delimiter}, level);
|
2012-07-31 16:22:57 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-15 19:34:01 +04:00
|
|
|
while (pos != length and
|
|
|
|
(line[pos] != opening_delimiter or line[pos-1] == '\\'))
|
2012-07-31 16:22:57 +04:00
|
|
|
++pos;
|
2012-11-21 16:56:52 +04:00
|
|
|
if (pos == length)
|
|
|
|
throw unterminated_string("%" + type_name + opening_delimiter,
|
|
|
|
String{opening_delimiter});
|
2012-07-31 16:22:57 +04:00
|
|
|
}
|
2012-02-14 01:38:07 +04:00
|
|
|
}
|
|
|
|
else
|
2012-08-29 23:50:48 +04:00
|
|
|
while (pos != length and
|
|
|
|
((not is_command_separator(line[pos]) and
|
|
|
|
not is_horizontal_blank(line[pos]))
|
|
|
|
or (pos != 0 and line[pos-1] == '\\')))
|
2012-02-14 01:38:07 +04:00
|
|
|
++pos;
|
2011-09-07 22:16:56 +04:00
|
|
|
|
2012-12-15 20:47:50 +04:00
|
|
|
if (start_pos != pos)
|
2012-07-31 02:06:50 +04:00
|
|
|
{
|
|
|
|
if (opt_token_pos_info)
|
|
|
|
opt_token_pos_info->push_back({token_start, pos});
|
2012-08-29 23:50:48 +04:00
|
|
|
String token = line.substr(token_start, pos - token_start);
|
2013-04-12 21:16:55 +04:00
|
|
|
static const Regex regex{R"(\\([ \t;\n]))"};
|
|
|
|
token = boost::regex_replace(token, regex, "\\1");
|
2012-08-29 23:50:48 +04:00
|
|
|
result.push_back({type, token});
|
2012-07-31 02:06:50 +04:00
|
|
|
}
|
2011-11-26 21:20:02 +04:00
|
|
|
|
2012-07-31 16:22:57 +04:00
|
|
|
if (is_command_separator(line[pos]))
|
2012-07-31 02:06:50 +04: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 05:37:35 +04:00
|
|
|
|
2011-11-26 21:20:02 +04:00
|
|
|
++pos;
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|
2012-08-01 16:27:34 +04:00
|
|
|
if (not result.empty() and result.back().type() == Token::Type::CommandSeparator)
|
|
|
|
result.pop_back();
|
2011-09-07 22:16:56 +04:00
|
|
|
|
2012-08-01 16:27:34 +04:00
|
|
|
return result;
|
2011-11-26 22:32:57 +04:00
|
|
|
}
|
|
|
|
|
2012-08-02 08:41:55 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 16:27:34 +04:00
|
|
|
struct command_not_found : runtime_error
|
|
|
|
{
|
|
|
|
command_not_found(const String& command)
|
|
|
|
: runtime_error(command + " : no such command") {}
|
|
|
|
};
|
|
|
|
|
2012-08-06 21:29:51 +04:00
|
|
|
void CommandManager::execute_single_command(const CommandParameters& params,
|
2012-08-07 00:02:11 +04:00
|
|
|
Context& context) const
|
2012-08-06 21:29:51 +04:00
|
|
|
{
|
|
|
|
if (params.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto command_it = m_commands.find(params[0]);
|
|
|
|
if (command_it == m_commands.end())
|
|
|
|
throw command_not_found(params[0]);
|
|
|
|
memoryview<String> param_view(params.begin()+1, params.end());
|
|
|
|
command_it->second.command(param_view, context);
|
|
|
|
}
|
|
|
|
|
2012-08-01 16:27:34 +04:00
|
|
|
void CommandManager::execute(const String& command_line,
|
2012-08-07 00:02:11 +04:00
|
|
|
Context& context,
|
2012-09-09 19:10:53 +04:00
|
|
|
const memoryview<String>& shell_params,
|
2012-05-29 09:42:26 +04:00
|
|
|
const EnvVarMap& env_vars)
|
2011-11-26 22:32:57 +04:00
|
|
|
{
|
2012-08-01 16:27:34 +04:00
|
|
|
TokenList tokens = parse(command_line);
|
|
|
|
if (tokens.empty())
|
2012-01-14 18:02:54 +04:00
|
|
|
return;
|
|
|
|
|
2012-08-06 21:29:51 +04:00
|
|
|
std::vector<String> params;
|
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); ++it)
|
2012-01-15 05:37:35 +04:00
|
|
|
{
|
2012-08-06 21:29:51 +04:00
|
|
|
if (it->type() == Token::Type::ShellExpand)
|
2012-01-15 05:37:35 +04:00
|
|
|
{
|
2012-08-06 21:29:51 +04:00
|
|
|
String output = ShellManager::instance().eval(it->content(),
|
2012-09-09 19:10:53 +04:00
|
|
|
context, shell_params,
|
|
|
|
env_vars);
|
2012-08-06 21:29:51 +04:00
|
|
|
TokenList shell_tokens = parse(output);
|
|
|
|
it = tokens.erase(it);
|
|
|
|
for (auto& token : shell_tokens)
|
|
|
|
it = ++tokens.insert(it, std::move(token));
|
|
|
|
it -= shell_tokens.size();
|
|
|
|
|
|
|
|
// when last token is a ShellExpand which produces no output
|
|
|
|
if (it == tokens.end())
|
|
|
|
break;
|
2012-01-15 05:37:35 +04:00
|
|
|
}
|
2012-08-11 14:13:48 +04:00
|
|
|
if (it->type() == Token::Type::RegisterExpand)
|
|
|
|
{
|
|
|
|
if (it->content().length() != 1)
|
|
|
|
throw runtime_error("wrong register name: " + it->content());
|
|
|
|
Register& reg = RegisterManager::instance()[it->content()[0]];
|
|
|
|
params.push_back(reg.values(context)[0]);
|
|
|
|
}
|
|
|
|
if (it->type() == Token::Type::OptionExpand)
|
|
|
|
{
|
2012-11-22 16:50:29 +04:00
|
|
|
const Option& option = context.options()[it->content()];
|
2013-03-03 20:25:40 +04:00
|
|
|
params.push_back(option.get_as_string());
|
2012-08-11 14:13:48 +04:00
|
|
|
}
|
2012-08-06 21:29:51 +04:00
|
|
|
if (it->type() == Token::Type::CommandSeparator)
|
|
|
|
{
|
|
|
|
execute_single_command(params, context);
|
|
|
|
params.clear();
|
|
|
|
}
|
|
|
|
if (it->type() == Token::Type::Raw)
|
|
|
|
params.push_back(it->content());
|
2012-01-15 05:37:35 +04:00
|
|
|
}
|
2012-08-06 21:29:51 +04:00
|
|
|
execute_single_command(params, context);
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|
|
|
|
|
2012-08-06 23:37:43 +04:00
|
|
|
Completions CommandManager::complete(const Context& context,
|
2012-10-11 02:41:48 +04:00
|
|
|
const String& command_line, ByteCount cursor_pos)
|
2011-09-14 01:16:48 +04:00
|
|
|
{
|
2012-07-31 02:06:50 +04:00
|
|
|
TokenPosList pos_info;
|
|
|
|
TokenList tokens = parse(command_line, &pos_info);
|
2011-09-14 01:16:48 +04:00
|
|
|
|
2011-11-26 23:05:49 +04:00
|
|
|
size_t token_to_complete = tokens.size();
|
2011-09-14 01:16:48 +04:00
|
|
|
for (size_t i = 0; i < tokens.size(); ++i)
|
|
|
|
{
|
2012-07-31 02:06:50 +04:00
|
|
|
if (pos_info[i].first <= cursor_pos and pos_info[i].second >= cursor_pos)
|
2011-09-14 01:16:48 +04:00
|
|
|
{
|
|
|
|
token_to_complete = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 22:50:28 +04:00
|
|
|
if (token_to_complete == 0 or tokens.empty()) // command name completion
|
2011-09-14 01:16:48 +04:00
|
|
|
{
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount cmd_start = tokens.empty() ? 0 : pos_info[0].first;
|
2011-10-19 22:50:28 +04:00
|
|
|
Completions result(cmd_start, cursor_pos);
|
2012-04-14 05:17:09 +04:00
|
|
|
String prefix = command_line.substr(cmd_start,
|
|
|
|
cursor_pos - cmd_start);
|
2011-09-14 01:16:48 +04:00
|
|
|
|
|
|
|
for (auto& command : m_commands)
|
|
|
|
{
|
|
|
|
if (command.first.substr(0, prefix.length()) == prefix)
|
|
|
|
result.candidates.push_back(command.first);
|
|
|
|
}
|
2012-06-02 19:49:56 +04:00
|
|
|
std::sort(result.candidates.begin(), result.candidates.end());
|
2011-09-14 01:16:48 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2011-09-16 13:18:51 +04:00
|
|
|
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(not tokens.empty());
|
2012-07-31 02:06:50 +04:00
|
|
|
if (tokens[0].type() != Token::Type::Raw)
|
|
|
|
return Completions();
|
|
|
|
|
|
|
|
const String& command_name = tokens[0].content();
|
2011-09-16 13:18:51 +04:00
|
|
|
|
|
|
|
auto command_it = m_commands.find(command_name);
|
|
|
|
if (command_it == m_commands.end() or not command_it->second.completer)
|
|
|
|
return Completions();
|
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount start = token_to_complete < tokens.size() ?
|
2012-07-31 02:06:50 +04:00
|
|
|
pos_info[token_to_complete].first : cursor_pos;
|
2011-11-26 23:05:49 +04:00
|
|
|
Completions result(start , cursor_pos);
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount cursor_pos_in_token = cursor_pos - start;
|
2011-09-16 13:18:51 +04:00
|
|
|
|
2012-08-01 16:27:34 +04:00
|
|
|
std::vector<String> params;
|
|
|
|
for (auto token_it = tokens.begin()+1; token_it != tokens.end(); ++token_it)
|
|
|
|
params.push_back(token_it->content());
|
2012-08-06 23:37:43 +04:00
|
|
|
result.candidates = command_it->second.completer(context, params,
|
2011-09-16 13:18:51 +04:00
|
|
|
token_to_complete - 1,
|
|
|
|
cursor_pos_in_token);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-06 23:37:43 +04:00
|
|
|
CandidateList PerArgumentCommandCompleter::operator()(const Context& context,
|
|
|
|
const CommandParameters& params,
|
2011-09-16 13:18:51 +04:00
|
|
|
size_t token_to_complete,
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount pos_in_token) const
|
2011-09-16 13:18:51 +04:00
|
|
|
{
|
2012-08-01 16:27:34 +04:00
|
|
|
if (token_to_complete >= m_completers.size())
|
2011-09-16 13:18:51 +04:00
|
|
|
return CandidateList();
|
|
|
|
|
|
|
|
// it is possible to try to complete a new argument
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(token_to_complete <= params.size());
|
2011-09-16 13:18:51 +04:00
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
const String& argument = token_to_complete < params.size() ?
|
2012-08-01 16:27:34 +04:00
|
|
|
params[token_to_complete] : String();
|
2012-08-06 23:37:43 +04:00
|
|
|
return m_completers[token_to_complete](context, argument, pos_in_token);
|
2011-09-14 01:16:48 +04:00
|
|
|
}
|
|
|
|
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|