2011-09-07 22:16:56 +04:00
|
|
|
#include "command_manager.hh"
|
|
|
|
|
2014-10-30 02:22:54 +03:00
|
|
|
#include "alias_registry.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"
|
2015-08-27 22:48:51 +03:00
|
|
|
#include "optional.hh"
|
2016-06-19 19:01:56 +03:00
|
|
|
#include "containers.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
|
|
|
|
{
|
2014-10-30 02:22:54 +03:00
|
|
|
return m_commands.find(command_name) != m_commands.end();
|
2012-06-02 19:49:35 +04:00
|
|
|
}
|
|
|
|
|
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,
|
2014-02-12 13:02:09 +04:00
|
|
|
String docstring,
|
2014-02-08 05:02:58 +04:00
|
|
|
ParameterDesc param_desc,
|
2013-11-12 23:38:19 +04:00
|
|
|
CommandFlags flags,
|
2015-02-08 22:04:20 +03:00
|
|
|
CommandHelper helper,
|
2012-11-22 17:28:14 +04:00
|
|
|
CommandCompleter completer)
|
2011-09-07 22:16:56 +04:00
|
|
|
{
|
2014-04-08 22:54:32 +04:00
|
|
|
m_commands[command_name] = { std::move(command),
|
|
|
|
std::move(docstring),
|
|
|
|
std::move(param_desc),
|
|
|
|
flags,
|
2015-02-08 22:04:20 +03:00
|
|
|
std::move(helper),
|
2014-04-08 22:54:32 +04:00
|
|
|
std::move(completer) };
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|
|
|
|
|
2013-07-28 18:40:02 +04:00
|
|
|
struct parse_error : runtime_error
|
|
|
|
{
|
2014-11-15 21:45:56 +03:00
|
|
|
parse_error(StringView error)
|
2015-03-31 01:06:02 +03:00
|
|
|
: runtime_error{format("parse error: {}", error)} {}
|
2013-07-28 18:40:02 +04:00
|
|
|
};
|
2012-11-21 16:56:52 +04:00
|
|
|
|
2012-08-02 08:41:55 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
struct Reader
|
|
|
|
{
|
|
|
|
public:
|
2017-01-29 16:49:45 +03:00
|
|
|
Reader(StringView s) : str{s}, pos{}, coord{} {}
|
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
[[gnu::always_inline]]
|
|
|
|
char operator*() const { return str[pos]; }
|
|
|
|
|
|
|
|
Reader& operator++()
|
|
|
|
{
|
|
|
|
if (str[pos++] == '\n')
|
|
|
|
{
|
|
|
|
++coord.line;
|
|
|
|
coord.column = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++coord.column;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
explicit operator bool() const { return pos < str.length(); }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
StringView substr_from(ByteCount start) const
|
|
|
|
{
|
|
|
|
return str.substr(start, pos - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<char> peek_next() const
|
|
|
|
{
|
|
|
|
if (pos+1 != str.length())
|
|
|
|
return str[pos+1];
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView str;
|
|
|
|
ByteCount pos;
|
2016-09-22 22:36:26 +03:00
|
|
|
DisplayCoord coord;
|
2015-08-27 22:48:51 +03: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';
|
|
|
|
}
|
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
template<typename Func>
|
|
|
|
String get_until_delimiter(Reader& reader, Func is_delimiter)
|
2013-06-27 22:07:26 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
auto beg = reader.pos;
|
2013-06-27 22:07:26 +04:00
|
|
|
String str;
|
2015-08-27 22:48:51 +03:00
|
|
|
bool was_antislash = false;
|
|
|
|
|
|
|
|
while (reader)
|
2013-06-27 22:07:26 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
const char c = *reader;
|
|
|
|
if (is_delimiter(c))
|
2013-06-27 22:07:26 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
str += reader.substr_from(beg);
|
|
|
|
if (was_antislash)
|
2015-07-25 10:56:27 +03:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
str.back() = c;
|
|
|
|
beg = reader.pos+1;
|
2015-07-25 10:56:27 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return str;
|
2013-06-27 22:07:26 +04:00
|
|
|
}
|
2015-08-27 22:48:51 +03:00
|
|
|
was_antislash = c == '\\';
|
|
|
|
++reader;
|
2013-06-27 22:07:26 +04:00
|
|
|
}
|
2015-08-27 22:48:51 +03:00
|
|
|
if (beg < reader.str.length())
|
|
|
|
str += reader.substr_from(beg);
|
2014-03-06 00:57:12 +04:00
|
|
|
return str;
|
2013-06-27 22:07:26 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline String get_until_delimiter(Reader& reader, char c)
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
return get_until_delimiter(reader, [c](char ch) { return c == ch; });
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView get_until_closing_delimiter(Reader& reader, char opening_delimiter,
|
|
|
|
char closing_delimiter)
|
|
|
|
{
|
|
|
|
kak_assert(reader.str[reader.pos-1] == opening_delimiter);
|
2013-12-29 22:37:48 +04:00
|
|
|
int level = 0;
|
2015-08-27 22:48:51 +03:00
|
|
|
auto start = reader.pos;
|
|
|
|
while (reader)
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
const char c = *reader;
|
|
|
|
if (c == opening_delimiter)
|
2013-12-29 22:37:48 +04:00
|
|
|
++level;
|
2015-08-27 22:48:51 +03:00
|
|
|
else if (c == closing_delimiter)
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2015-08-27 22:48:51 +03:00
|
|
|
++reader;
|
2013-12-29 22:37:48 +04:00
|
|
|
}
|
2015-08-27 22:48:51 +03:00
|
|
|
return reader.substr_from(start);
|
2013-12-29 22:37:48 +04:00
|
|
|
}
|
|
|
|
|
2014-11-04 16:40:35 +03:00
|
|
|
struct unknown_expand : parse_error
|
|
|
|
{
|
2014-11-15 21:45:56 +03:00
|
|
|
unknown_expand(StringView name)
|
2015-03-31 01:06:02 +03:00
|
|
|
: parse_error{format("unknown expand '{}'", name)} {}
|
2014-11-04 16:40:35 +03:00
|
|
|
};
|
|
|
|
|
2014-02-27 10:41:58 +04:00
|
|
|
template<bool throw_on_invalid>
|
2014-04-22 00:49:25 +04:00
|
|
|
Token::Type token_type(StringView type_name)
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
|
|
|
if (type_name == "")
|
2015-09-20 13:19:10 +03:00
|
|
|
return Token::Type::RawQuoted;
|
2013-12-29 22:37:48 +04:00
|
|
|
else if (type_name == "sh")
|
|
|
|
return Token::Type::ShellExpand;
|
|
|
|
else if (type_name == "reg")
|
|
|
|
return Token::Type::RegisterExpand;
|
|
|
|
else if (type_name == "opt")
|
|
|
|
return Token::Type::OptionExpand;
|
2014-06-18 22:28:48 +04:00
|
|
|
else if (type_name == "val")
|
|
|
|
return Token::Type::ValExpand;
|
2015-12-01 23:07:14 +03:00
|
|
|
else if (type_name == "arg")
|
|
|
|
return Token::Type::ArgExpand;
|
2014-02-27 10:41:58 +04:00
|
|
|
else if (throw_on_invalid)
|
2013-12-29 22:37:48 +04:00
|
|
|
throw unknown_expand{type_name};
|
2014-02-27 10:41:58 +04:00
|
|
|
else
|
2015-09-20 13:19:10 +03:00
|
|
|
return Token::Type::RawQuoted;
|
2013-12-29 22:37:48 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
void skip_blanks_and_comments(Reader& reader)
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
while (reader)
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
const char c = *reader;
|
|
|
|
if (is_horizontal_blank(c))
|
|
|
|
++reader;
|
|
|
|
else if (c == '\\' and reader.peek_next().value_or('\0') == '\n')
|
|
|
|
++(++reader);
|
|
|
|
else if (c == '#')
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
for (bool eol = false; reader and not eol; ++reader)
|
|
|
|
eol = *reader == '\n';
|
2013-12-29 22:37:48 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 02:57:14 +04:00
|
|
|
template<bool throw_on_unterminated>
|
2015-08-27 22:48:51 +03:00
|
|
|
Token parse_percent_token(Reader& reader)
|
2014-04-04 02:57:14 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
++reader;
|
|
|
|
const ByteCount type_start = reader.pos;
|
|
|
|
while (reader and isalpha(*reader))
|
|
|
|
++reader;
|
|
|
|
StringView type_name = reader.substr_from(type_start);
|
2014-04-04 02:57:14 +04:00
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
if (throw_on_unterminated and not reader)
|
2015-03-31 01:06:02 +03:00
|
|
|
throw parse_error{format("expected a string delimiter after '%{}'",
|
|
|
|
type_name)};
|
2014-04-04 02:57:14 +04:00
|
|
|
|
|
|
|
Token::Type type = token_type<throw_on_unterminated>(type_name);
|
2015-08-27 23:36:49 +03:00
|
|
|
|
|
|
|
constexpr struct CharPair { char opening; char closing; } matching_pairs[] = {
|
2014-04-04 02:57:14 +04:00
|
|
|
{ '(', ')' }, { '[', ']' }, { '{', '}' }, { '<', '>' }
|
|
|
|
};
|
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
char opening_delimiter = *reader;
|
2015-08-29 23:39:23 +03:00
|
|
|
auto coord = reader.coord;
|
2015-08-27 22:48:51 +03:00
|
|
|
++reader;
|
|
|
|
auto start = reader.pos;
|
2014-04-04 02:57:14 +04:00
|
|
|
|
2015-08-27 23:36:49 +03:00
|
|
|
auto it = find_if(matching_pairs, [opening_delimiter](const CharPair& cp)
|
|
|
|
{ return opening_delimiter == cp.opening; });
|
|
|
|
|
|
|
|
if (it != std::end(matching_pairs))
|
2014-04-04 02:57:14 +04:00
|
|
|
{
|
2015-08-27 23:36:49 +03:00
|
|
|
const char closing_delimiter = it->closing;
|
2015-08-27 22:48:51 +03:00
|
|
|
auto token = get_until_closing_delimiter(reader, opening_delimiter,
|
|
|
|
closing_delimiter);
|
|
|
|
if (throw_on_unterminated and not reader)
|
|
|
|
throw parse_error{format("{}:{}: unterminated string '%{}{}...{}'",
|
|
|
|
coord.line, coord.column, type_name,
|
|
|
|
opening_delimiter, closing_delimiter)};
|
|
|
|
|
|
|
|
return {type, start, reader.pos, coord, token.str()};
|
2014-04-04 02:57:14 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
String token = get_until_delimiter(reader, opening_delimiter);
|
|
|
|
|
|
|
|
if (throw_on_unterminated and not reader)
|
|
|
|
throw parse_error{format("{}:{}: unterminated string '%{}{}...{}'",
|
|
|
|
coord.line, coord.column, type_name,
|
|
|
|
opening_delimiter, opening_delimiter)};
|
|
|
|
|
|
|
|
return {type, start, reader.pos, coord, std::move(token)};
|
2014-04-04 02:57:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 09:50:58 +03:00
|
|
|
String expand_token(const Token& token, const Context& context,
|
|
|
|
const ShellContext& shell_context)
|
|
|
|
{
|
|
|
|
auto& content = token.content();
|
|
|
|
switch (token.type())
|
|
|
|
{
|
|
|
|
case Token::Type::ShellExpand:
|
2016-06-19 19:01:56 +03:00
|
|
|
{
|
|
|
|
auto str = ShellManager::instance().eval(
|
|
|
|
content, context, {}, ShellManager::Flags::WaitForStdout,
|
|
|
|
shell_context).first;
|
|
|
|
|
|
|
|
int trailing_eol_count = 0;
|
|
|
|
for (auto c : str | reverse())
|
|
|
|
{
|
|
|
|
if (c != '\n')
|
|
|
|
break;
|
|
|
|
++trailing_eol_count;
|
|
|
|
}
|
|
|
|
str.resize(str.length() - trailing_eol_count, 0);
|
|
|
|
return str;
|
|
|
|
|
|
|
|
}
|
2015-12-12 09:50:58 +03:00
|
|
|
case Token::Type::RegisterExpand:
|
|
|
|
return context.main_sel_register_value(content).str();
|
|
|
|
case Token::Type::OptionExpand:
|
|
|
|
return context.options()[content].get_as_string();
|
|
|
|
case Token::Type::ValExpand:
|
|
|
|
{
|
|
|
|
auto it = shell_context.env_vars.find(content);
|
|
|
|
if (it != shell_context.env_vars.end())
|
|
|
|
return it->value;
|
|
|
|
return ShellManager::instance().get_val(content, context);
|
|
|
|
}
|
|
|
|
case Token::Type::ArgExpand:
|
|
|
|
{
|
|
|
|
auto& params = shell_context.params;
|
|
|
|
if (content == '@')
|
|
|
|
return join(params, ' ');
|
|
|
|
|
|
|
|
const int arg = str_to_int(content)-1;
|
|
|
|
if (arg < 0)
|
|
|
|
throw runtime_error("invalid argument index");
|
|
|
|
return arg < params.size() ? params[arg] : String{};
|
|
|
|
}
|
|
|
|
case Token::Type::RawEval:
|
|
|
|
return expand(content, context, shell_context);
|
|
|
|
case Token::Type::Raw:
|
|
|
|
case Token::Type::RawQuoted:
|
|
|
|
return content;
|
|
|
|
default: kak_assert(false);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-30 18:22:18 +04:00
|
|
|
template<bool throw_on_unterminated>
|
2014-04-22 00:49:25 +04:00
|
|
|
TokenList parse(StringView line)
|
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
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
Reader reader{line};
|
|
|
|
while (reader)
|
2011-09-07 22:16:56 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
skip_blanks_and_comments(reader);
|
2011-09-07 22:16:56 +04:00
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
ByteCount start = reader.pos;
|
|
|
|
auto coord = reader.coord;
|
2012-02-14 01:38:07 +04:00
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
const char c = *reader;
|
|
|
|
if (c == '"' or c == '\'')
|
2011-11-26 21:20:02 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
start = (++reader).pos;
|
|
|
|
String token = get_until_delimiter(reader, c);
|
|
|
|
if (throw_on_unterminated and not reader)
|
|
|
|
throw parse_error{format("unterminated string {0}...{0}", c)};
|
|
|
|
result.emplace_back(c == '"' ? Token::Type::RawEval
|
2015-09-20 13:19:10 +03:00
|
|
|
: Token::Type::RawQuoted,
|
2015-08-27 22:48:51 +03:00
|
|
|
start, reader.pos, coord, std::move(token));
|
2012-07-31 16:22:57 +04:00
|
|
|
}
|
2015-08-27 22:48:51 +03:00
|
|
|
else if (c == '%')
|
2014-04-08 22:54:32 +04:00
|
|
|
result.push_back(
|
2015-08-27 22:48:51 +03:00
|
|
|
parse_percent_token<throw_on_unterminated>(reader));
|
2012-02-14 01:38:07 +04:00
|
|
|
else
|
2013-06-27 22:07:26 +04:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
String str = get_until_delimiter(reader, [](char c) {
|
|
|
|
return is_command_separator(c) or is_horizontal_blank(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (not str.empty())
|
|
|
|
result.emplace_back(Token::Type::Raw, start, reader.pos,
|
2015-10-23 15:34:37 +03:00
|
|
|
coord, unescape(str, "%", '\\'));
|
2012-07-31 02:06:50 +04:00
|
|
|
}
|
2011-11-26 21:20:02 +04:00
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
if (is_command_separator(*reader))
|
|
|
|
result.emplace_back(Token::Type::CommandSeparator,
|
|
|
|
reader.pos, reader.pos+1, coord);
|
2012-01-15 05:37:35 +04:00
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
++reader;
|
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
|
|
|
}
|
|
|
|
|
2016-12-07 23:07:32 +03:00
|
|
|
template<typename Postprocess>
|
|
|
|
String expand_impl(StringView str, const Context& context,
|
|
|
|
const ShellContext& shell_context,
|
|
|
|
Postprocess postprocess)
|
2015-05-04 19:08:57 +03:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
Reader reader{str};
|
2015-05-04 19:08:57 +03:00
|
|
|
String res;
|
2015-08-27 22:48:51 +03:00
|
|
|
auto beg = 0_byte;
|
|
|
|
while (reader)
|
2015-05-04 19:08:57 +03:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
char c = *reader;
|
|
|
|
if (c == '\\')
|
2015-05-04 19:08:57 +03:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
c = *++reader;
|
2015-07-25 10:56:27 +03:00
|
|
|
if (c == '%' or c == '\\')
|
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
res += reader.substr_from(beg);
|
2015-07-25 10:56:27 +03:00
|
|
|
res.back() = c;
|
2015-08-27 22:48:51 +03:00
|
|
|
beg = (++reader).pos;
|
2015-07-25 10:56:27 +03:00
|
|
|
}
|
2015-05-04 19:08:57 +03:00
|
|
|
}
|
2015-08-27 22:48:51 +03:00
|
|
|
else if (c == '%')
|
2015-05-04 19:08:57 +03:00
|
|
|
{
|
2015-08-27 22:48:51 +03:00
|
|
|
res += reader.substr_from(beg);
|
2016-12-07 23:07:32 +03:00
|
|
|
res += postprocess(expand_token(parse_percent_token<true>(reader),
|
|
|
|
context, shell_context));
|
2015-08-27 22:48:51 +03:00
|
|
|
beg = (++reader).pos;
|
2015-05-04 19:08:57 +03:00
|
|
|
}
|
|
|
|
else
|
2015-08-27 22:48:51 +03:00
|
|
|
++reader;
|
2015-05-04 19:08:57 +03:00
|
|
|
}
|
2015-08-27 22:48:51 +03:00
|
|
|
res += reader.substr_from(beg);
|
2015-05-04 19:08:57 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:07:32 +03:00
|
|
|
String expand(StringView str, const Context& context,
|
|
|
|
const ShellContext& shell_context)
|
|
|
|
{
|
2017-01-09 02:26:15 +03:00
|
|
|
return expand_impl(str, context, shell_context, [](String s){ return s; });
|
2016-12-07 23:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
String expand(StringView str, const Context& context,
|
|
|
|
const ShellContext& shell_context,
|
|
|
|
std::function<String (String)> postprocess)
|
|
|
|
{
|
|
|
|
return expand_impl(str, context, shell_context,
|
|
|
|
[&](String s) { return postprocess(std::move(s)); });
|
|
|
|
}
|
|
|
|
|
2012-08-01 16:27:34 +04:00
|
|
|
struct command_not_found : runtime_error
|
|
|
|
{
|
2014-11-15 21:45:56 +03:00
|
|
|
command_not_found(StringView command)
|
2012-08-01 16:27:34 +04:00
|
|
|
: runtime_error(command + " : no such command") {}
|
|
|
|
};
|
|
|
|
|
2014-04-08 22:54:32 +04:00
|
|
|
CommandManager::CommandMap::const_iterator
|
2014-10-30 02:22:54 +03:00
|
|
|
CommandManager::find_command(const Context& context, const String& name) const
|
2013-12-24 06:06:22 +04:00
|
|
|
{
|
2014-10-30 02:22:54 +03:00
|
|
|
auto alias = context.aliases()[name];
|
|
|
|
const String& cmd_name = alias.empty() ? name : alias.str();
|
2013-12-24 06:06:22 +04:00
|
|
|
|
|
|
|
return m_commands.find(cmd_name);
|
|
|
|
}
|
|
|
|
|
2013-07-26 03:17:12 +04:00
|
|
|
void CommandManager::execute_single_command(CommandParameters params,
|
2014-05-07 23:39:59 +04:00
|
|
|
Context& context,
|
2015-10-22 15:59:23 +03:00
|
|
|
const ShellContext& shell_context,
|
2017-01-29 16:56:05 +03:00
|
|
|
DisplayCoord pos)
|
2012-08-06 21:29:51 +04:00
|
|
|
{
|
|
|
|
if (params.empty())
|
|
|
|
return;
|
|
|
|
|
2017-01-29 16:56:05 +03:00
|
|
|
constexpr int max_command_depth = 100;
|
|
|
|
if (m_command_depth > max_command_depth)
|
|
|
|
throw runtime_error("maximum nested command depth hit");
|
|
|
|
|
|
|
|
++m_command_depth;
|
|
|
|
auto pop_cmd = on_scope_end([this] { --m_command_depth; });
|
|
|
|
|
2016-12-25 04:42:31 +03:00
|
|
|
ParameterList param_view(params.begin()+1, params.end());
|
2014-10-30 02:22:54 +03:00
|
|
|
auto command_it = find_command(context, params[0]);
|
2012-08-06 21:29:51 +04:00
|
|
|
if (command_it == m_commands.end())
|
|
|
|
throw command_not_found(params[0]);
|
2014-04-08 22:54:32 +04:00
|
|
|
|
2014-05-07 23:39:59 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ParametersParser parameter_parser(param_view,
|
|
|
|
command_it->second.param_desc);
|
2015-10-22 15:59:23 +03:00
|
|
|
command_it->second.command(parameter_parser, context, shell_context);
|
2014-05-07 23:39:59 +04:00
|
|
|
}
|
|
|
|
catch (runtime_error& error)
|
|
|
|
{
|
2015-03-31 01:56:33 +03:00
|
|
|
throw runtime_error(format("{}:{}: '{}' {}", pos.line+1, pos.column+1,
|
|
|
|
command_it->first, error.what()));
|
2014-05-07 23:39:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:49:25 +04:00
|
|
|
void CommandManager::execute(StringView command_line,
|
2015-10-22 15:48:57 +03:00
|
|
|
Context& context, const ShellContext& shell_context)
|
2011-11-26 22:32:57 +04:00
|
|
|
{
|
2013-12-30 18:22:18 +04:00
|
|
|
TokenList tokens = parse<true>(command_line);
|
2012-08-01 16:27:34 +04:00
|
|
|
if (tokens.empty())
|
2012-01-14 18:02:54 +04:00
|
|
|
return;
|
|
|
|
|
2016-09-22 22:36:26 +03:00
|
|
|
DisplayCoord command_coord;
|
2015-01-12 16:58:41 +03:00
|
|
|
Vector<String> params;
|
2016-12-07 16:26:11 +03:00
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); )
|
2012-01-15 05:37:35 +04:00
|
|
|
{
|
2014-05-07 23:39:59 +04:00
|
|
|
if (params.empty())
|
2015-08-27 22:48:51 +03:00
|
|
|
command_coord = it->coord();
|
2014-05-07 23:39:59 +04:00
|
|
|
|
2014-04-04 02:57:14 +04:00
|
|
|
if (it->type() == Token::Type::CommandSeparator)
|
2012-01-15 05:37:35 +04:00
|
|
|
{
|
2015-10-22 15:59:23 +03:00
|
|
|
execute_single_command(params, context, shell_context, command_coord);
|
2014-04-04 02:57:14 +04:00
|
|
|
params.clear();
|
|
|
|
}
|
|
|
|
// Shell expand are retokenized
|
|
|
|
else if (it->type() == Token::Type::ShellExpand)
|
|
|
|
{
|
2016-12-07 16:26:11 +03:00
|
|
|
auto new_tokens = parse<true>(expand_token(*it, context,
|
|
|
|
shell_context));
|
|
|
|
it = tokens.insert(tokens.erase(it),
|
|
|
|
std::make_move_iterator(new_tokens.begin()),
|
|
|
|
std::make_move_iterator(new_tokens.end()));
|
|
|
|
continue; // skip incrementing, we already point to next token
|
2012-01-15 05:37:35 +04:00
|
|
|
}
|
2015-12-02 04:01:48 +03:00
|
|
|
else if (it->type() == Token::Type::ArgExpand and it->content() == '@')
|
2016-12-07 16:26:11 +03:00
|
|
|
params.insert(params.end(), shell_context.params.begin(),
|
|
|
|
shell_context.params.end());
|
2014-04-04 02:57:14 +04:00
|
|
|
else
|
2015-10-22 15:48:57 +03:00
|
|
|
params.push_back(expand_token(*it, context, shell_context));
|
2016-12-07 16:26:11 +03:00
|
|
|
|
|
|
|
++it;
|
2012-01-15 05:37:35 +04:00
|
|
|
}
|
2015-10-22 15:59:23 +03:00
|
|
|
execute_single_command(params, context, shell_context, command_coord);
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|
|
|
|
|
2016-12-23 19:23:31 +03:00
|
|
|
Optional<CommandInfo> CommandManager::command_info(const Context& context, StringView command_line) const
|
2014-02-12 02:16:17 +04:00
|
|
|
{
|
|
|
|
TokenList tokens = parse<false>(command_line);
|
|
|
|
size_t cmd_idx = 0;
|
|
|
|
for (size_t i = 0; i < tokens.size(); ++i)
|
|
|
|
{
|
|
|
|
if (tokens[i].type() == Token::Type::CommandSeparator)
|
|
|
|
cmd_idx = i+1;
|
|
|
|
}
|
|
|
|
|
2014-04-08 22:54:32 +04:00
|
|
|
if (cmd_idx == tokens.size() or
|
2015-09-20 13:19:10 +03:00
|
|
|
(tokens[cmd_idx].type() != Token::Type::Raw and
|
|
|
|
tokens[cmd_idx].type() != Token::Type::RawQuoted))
|
2016-12-23 19:23:31 +03:00
|
|
|
return {};
|
2014-02-12 02:16:17 +04:00
|
|
|
|
2014-10-30 02:22:54 +03:00
|
|
|
auto cmd = find_command(context, tokens[cmd_idx].content());
|
2014-02-12 02:16:17 +04:00
|
|
|
if (cmd == m_commands.end())
|
2016-12-23 19:23:31 +03:00
|
|
|
return {};
|
2014-02-12 02:16:17 +04:00
|
|
|
|
2016-12-23 19:23:31 +03:00
|
|
|
CommandInfo res;
|
|
|
|
res.name = cmd->first;
|
2014-02-12 13:02:09 +04:00
|
|
|
if (not cmd->second.docstring.empty())
|
2016-12-23 19:23:31 +03:00
|
|
|
res.info += cmd->second.docstring + "\n";
|
2014-05-03 18:54:51 +04:00
|
|
|
|
2015-02-08 22:04:20 +03:00
|
|
|
if (cmd->second.helper)
|
|
|
|
{
|
|
|
|
Vector<String> params;
|
|
|
|
for (auto it = tokens.begin() + cmd_idx + 1;
|
|
|
|
it != tokens.end() and it->type() != Token::Type::CommandSeparator;
|
|
|
|
++it)
|
|
|
|
{
|
2015-09-20 13:19:10 +03:00
|
|
|
if (it->type() == Token::Type::Raw or
|
|
|
|
it->type() == Token::Type::RawQuoted or
|
|
|
|
it->type() == Token::Type::RawEval)
|
2015-02-08 22:04:20 +03:00
|
|
|
params.push_back(it->content());
|
|
|
|
}
|
|
|
|
String helpstr = cmd->second.helper(context, params);
|
|
|
|
if (not helpstr.empty())
|
|
|
|
{
|
|
|
|
if (helpstr.back() != '\n')
|
|
|
|
helpstr += '\n';
|
2016-12-23 19:23:31 +03:00
|
|
|
res.info += helpstr;
|
2015-02-08 22:04:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-03 18:54:51 +04:00
|
|
|
String aliases;
|
2014-10-30 02:22:54 +03:00
|
|
|
for (auto& alias : context.aliases().aliases_for(cmd->first))
|
|
|
|
aliases += " " + alias;
|
2014-05-03 18:54:51 +04:00
|
|
|
if (not aliases.empty())
|
2016-12-23 19:23:31 +03:00
|
|
|
res.info += "Aliases:" + aliases + "\n";
|
2014-05-03 18:54:51 +04:00
|
|
|
|
|
|
|
|
2014-02-12 02:23:44 +04:00
|
|
|
auto& switches = cmd->second.param_desc.switches;
|
|
|
|
if (not switches.empty())
|
2014-02-12 02:16:17 +04:00
|
|
|
{
|
2016-12-23 19:23:31 +03:00
|
|
|
res.info += "Switches:\n";
|
|
|
|
res.info += generate_switches_doc(switches);
|
2014-02-12 02:16:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:56:37 +03:00
|
|
|
Completions CommandManager::complete_command_name(const Context& context,
|
2016-03-24 03:05:40 +03:00
|
|
|
StringView query, bool with_aliases) const
|
2016-03-08 16:56:37 +03:00
|
|
|
{
|
2016-03-24 03:05:40 +03:00
|
|
|
auto commands = m_commands
|
2016-03-09 00:35:56 +03:00
|
|
|
| filter([](const CommandMap::value_type& cmd) { return not (cmd.second.flags & CommandFlags::Hidden); })
|
2016-10-11 01:44:18 +03:00
|
|
|
| transform(std::mem_fn(&CommandMap::value_type::first));
|
2016-03-09 00:35:56 +03:00
|
|
|
|
2016-03-24 03:05:40 +03:00
|
|
|
if (not with_aliases)
|
|
|
|
return {0, query.length(), Kakoune::complete(query, query.length(), commands)};
|
|
|
|
|
2016-11-14 03:29:43 +03:00
|
|
|
auto candidates = Kakoune::complete(query, query.length(), commands);
|
2016-03-09 00:35:56 +03:00
|
|
|
return {0, query.length(), std::move(candidates)};
|
2016-03-08 16:56:37 +03:00
|
|
|
}
|
|
|
|
|
2014-04-08 22:54:32 +04:00
|
|
|
Completions CommandManager::complete(const Context& context,
|
|
|
|
CompletionFlags flags,
|
2014-04-22 00:49:25 +04:00
|
|
|
StringView command_line,
|
2014-04-08 22:54:32 +04:00
|
|
|
ByteCount cursor_pos)
|
2011-09-14 01:16:48 +04:00
|
|
|
{
|
2013-12-30 18:22:18 +04:00
|
|
|
TokenList tokens = parse<false>(command_line);
|
2011-09-14 01:16:48 +04:00
|
|
|
|
2014-01-04 00:07:40 +04:00
|
|
|
size_t cmd_idx = 0;
|
|
|
|
size_t tok_idx = tokens.size();
|
2011-09-14 01:16:48 +04:00
|
|
|
for (size_t i = 0; i < tokens.size(); ++i)
|
|
|
|
{
|
2014-01-04 00:07:40 +04:00
|
|
|
if (tokens[i].type() == Token::Type::CommandSeparator)
|
|
|
|
cmd_idx = i+1;
|
|
|
|
|
2013-12-29 22:37:48 +04:00
|
|
|
if (tokens[i].begin() <= cursor_pos and tokens[i].end() >= cursor_pos)
|
2011-09-14 01:16:48 +04:00
|
|
|
{
|
2014-01-04 00:07:40 +04:00
|
|
|
tok_idx = i;
|
2011-09-14 01:16:48 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:56:37 +03:00
|
|
|
const bool is_last_token = tok_idx == tokens.size();
|
2015-06-26 15:52:01 +03:00
|
|
|
// command name completion
|
2013-12-30 18:22:18 +04:00
|
|
|
if (tokens.empty() or
|
2016-03-08 16:56:37 +03:00
|
|
|
(tok_idx == cmd_idx and (is_last_token or
|
2015-09-20 13:19:10 +03:00
|
|
|
tokens[tok_idx].type() == Token::Type::Raw or
|
|
|
|
tokens[tok_idx].type() == Token::Type::RawQuoted)))
|
2011-09-14 01:16:48 +04:00
|
|
|
{
|
2016-03-08 16:56:37 +03:00
|
|
|
auto cmd_start = is_last_token ? cursor_pos : tokens[tok_idx].begin();
|
|
|
|
StringView query = command_line.substr(cmd_start, cursor_pos - cmd_start);
|
2016-03-24 03:05:40 +03:00
|
|
|
return offset_pos(complete_command_name(context, query, true), cmd_start);
|
2011-09-14 01:16:48 +04:00
|
|
|
}
|
2011-09-16 13:18:51 +04:00
|
|
|
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(not tokens.empty());
|
2011-09-16 13:18:51 +04:00
|
|
|
|
2014-01-04 00:07:40 +04:00
|
|
|
ByteCount start = tok_idx < tokens.size() ?
|
|
|
|
tokens[tok_idx].begin() : 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
|
|
|
|
2015-08-27 22:48:51 +03:00
|
|
|
const Token::Type type = tok_idx < tokens.size() ?
|
2015-09-20 13:19:10 +03:00
|
|
|
tokens[tok_idx].type() : Token::Type::Raw;
|
2015-08-27 22:48:51 +03:00
|
|
|
switch (type)
|
2013-12-30 18:22:18 +04:00
|
|
|
{
|
|
|
|
case Token::Type::OptionExpand:
|
2015-08-27 15:57:56 +03:00
|
|
|
return {start , cursor_pos,
|
|
|
|
GlobalScope::instance().option_registry().complete_option_name(
|
|
|
|
tokens[tok_idx].content(), cursor_pos_in_token) };
|
|
|
|
|
2013-12-30 18:22:18 +04:00
|
|
|
case Token::Type::ShellExpand:
|
2015-08-27 15:57:56 +03:00
|
|
|
return offset_pos(shell_complete(context, flags, tokens[tok_idx].content(),
|
|
|
|
cursor_pos_in_token), start);
|
|
|
|
|
2016-04-17 21:21:43 +03:00
|
|
|
case Token::Type::ValExpand:
|
|
|
|
return {start , cursor_pos,
|
|
|
|
ShellManager::instance().complete_env_var(
|
|
|
|
tokens[tok_idx].content(), cursor_pos_in_token) };
|
|
|
|
|
2013-12-30 18:22:18 +04:00
|
|
|
case Token::Type::Raw:
|
2015-09-20 13:19:10 +03:00
|
|
|
case Token::Type::RawQuoted:
|
|
|
|
case Token::Type::RawEval:
|
2013-12-30 18:22:18 +04:00
|
|
|
{
|
2014-01-04 00:07:40 +04:00
|
|
|
if (tokens[cmd_idx].type() != Token::Type::Raw)
|
2013-12-30 18:22:18 +04:00
|
|
|
return Completions{};
|
|
|
|
|
2014-01-04 00:07:40 +04:00
|
|
|
const String& command_name = tokens[cmd_idx].content();
|
2016-08-05 15:53:19 +03:00
|
|
|
if (command_name != m_last_complete_command)
|
|
|
|
{
|
|
|
|
m_last_complete_command = command_name;
|
|
|
|
flags |= CompletionFlags::Start;
|
|
|
|
}
|
2013-12-30 18:22:18 +04:00
|
|
|
|
2014-10-30 02:22:54 +03:00
|
|
|
auto command_it = find_command(context, command_name);
|
2014-04-08 22:54:32 +04:00
|
|
|
if (command_it == m_commands.end() or
|
|
|
|
not command_it->second.completer)
|
2013-12-30 18:22:18 +04:00
|
|
|
return Completions();
|
|
|
|
|
2015-01-12 16:58:41 +03:00
|
|
|
Vector<String> params;
|
2015-08-27 22:48:51 +03:00
|
|
|
for (auto it = tokens.begin() + cmd_idx + 1; it != tokens.end(); ++it)
|
|
|
|
params.push_back(it->content());
|
2014-01-04 00:07:40 +04:00
|
|
|
if (tok_idx == tokens.size())
|
2017-01-09 01:30:15 +03:00
|
|
|
params.emplace_back("");
|
2015-09-20 13:19:10 +03:00
|
|
|
Completions completions = offset_pos(command_it->second.completer(
|
2014-04-08 22:54:32 +04:00
|
|
|
context, flags, params, tok_idx - cmd_idx - 1,
|
2015-09-20 13:19:10 +03:00
|
|
|
cursor_pos_in_token), start);
|
2014-05-05 15:54:23 +04:00
|
|
|
|
2015-09-20 13:19:10 +03:00
|
|
|
if (type != Token::Type::RawQuoted)
|
|
|
|
{
|
|
|
|
StringView to_escape = type == Token::Type::Raw ? "% \t;" : "%";
|
|
|
|
for (auto& candidate : completions.candidates)
|
|
|
|
candidate = escape(candidate, to_escape, '\\');
|
|
|
|
}
|
2014-05-05 15:54:23 +04:00
|
|
|
|
2014-01-26 20:14:02 +04:00
|
|
|
return completions;
|
2013-12-30 18:22:18 +04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Completions{};
|
2011-09-16 13:18:51 +04:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:52:01 +03:00
|
|
|
Completions CommandManager::complete(const Context& context,
|
|
|
|
CompletionFlags flags,
|
|
|
|
CommandParameters params,
|
|
|
|
size_t token_to_complete,
|
|
|
|
ByteCount pos_in_token)
|
|
|
|
{
|
|
|
|
StringView prefix = params[token_to_complete].substr(0, pos_in_token);
|
|
|
|
|
|
|
|
if (token_to_complete == 0)
|
2016-03-24 03:05:40 +03:00
|
|
|
return complete_command_name(context, prefix, true);
|
2015-06-26 15:52:01 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const String& command_name = params[0];
|
2016-08-05 15:53:19 +03:00
|
|
|
if (command_name != m_last_complete_command)
|
|
|
|
{
|
|
|
|
m_last_complete_command = command_name;
|
|
|
|
flags |= CompletionFlags::Start;
|
|
|
|
}
|
2015-06-26 15:52:01 +03:00
|
|
|
|
|
|
|
auto command_it = find_command(context, command_name);
|
|
|
|
if (command_it != m_commands.end() and command_it->second.completer)
|
|
|
|
return command_it->second.completer(
|
2016-11-20 14:15:15 +03:00
|
|
|
context, flags, params.subrange(1),
|
2015-06-26 15:52:01 +03:00
|
|
|
token_to_complete-1, pos_in_token);
|
|
|
|
}
|
|
|
|
return Completions{};
|
|
|
|
}
|
|
|
|
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|