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"
|
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)
|
2013-07-28 18:40:02 +04:00
|
|
|
: runtime_error{"parse error: " + error} {}
|
|
|
|
};
|
2012-11-21 16:56:52 +04:00
|
|
|
|
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,
|
2014-04-04 02:57:14 +04:00
|
|
|
RawEval,
|
2012-08-01 16:27:34 +04:00
|
|
|
ShellExpand,
|
2012-08-11 14:13:48 +04:00
|
|
|
RegisterExpand,
|
|
|
|
OptionExpand,
|
2014-06-18 22:28:48 +04:00
|
|
|
ValExpand,
|
2012-08-01 16:27:34 +04:00
|
|
|
CommandSeparator
|
|
|
|
};
|
|
|
|
Token() : m_type(Type::Raw) {}
|
|
|
|
|
2013-12-29 22:37:48 +04:00
|
|
|
Token(Type type, ByteCount b, ByteCount e, String str = "")
|
|
|
|
: m_type(type), m_begin(b), m_end(e), m_content(str) {}
|
2012-08-01 16:27:34 +04:00
|
|
|
|
|
|
|
Type type() const { return m_type; }
|
2013-12-29 22:37:48 +04:00
|
|
|
ByteCount begin() const { return m_begin; }
|
|
|
|
ByteCount end() const { return m_end; }
|
2012-08-01 16:27:34 +04:00
|
|
|
const String& content() const { return m_content; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type m_type;
|
2013-12-29 22:37:48 +04:00
|
|
|
ByteCount m_begin;
|
|
|
|
ByteCount m_end;
|
2012-08-01 16:27:34 +04:00
|
|
|
String m_content;
|
|
|
|
};
|
|
|
|
|
2012-02-14 01:38:07 +04:00
|
|
|
|
2015-01-12 16:58:41 +03:00
|
|
|
using TokenList = Vector<Token>;
|
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';
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:49:25 +04:00
|
|
|
String get_until_delimiter(StringView base, ByteCount& pos, char delimiter)
|
2013-06-27 22:07:26 +04:00
|
|
|
{
|
|
|
|
const ByteCount length = base.length();
|
|
|
|
String str;
|
2014-03-06 00:57:12 +04:00
|
|
|
while (pos < length)
|
2013-06-27 22:07:26 +04:00
|
|
|
{
|
|
|
|
char c = base[pos];
|
|
|
|
if (c == delimiter)
|
|
|
|
{
|
|
|
|
if (base[pos-1] != '\\')
|
2014-03-06 00:57:12 +04:00
|
|
|
break;
|
2013-06-27 22:07:26 +04:00
|
|
|
str.back() = delimiter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str += c;
|
2014-03-06 00:57:12 +04:00
|
|
|
++pos;
|
2013-06-27 22:07:26 +04:00
|
|
|
}
|
2014-03-06 00:57:12 +04:00
|
|
|
return str;
|
2013-06-27 22:07:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 00:49:25 +04:00
|
|
|
String get_until_delimiter(StringView base, ByteCount& pos,
|
2013-12-29 22:37:48 +04:00
|
|
|
char opening_delimiter, char closing_delimiter)
|
|
|
|
{
|
|
|
|
kak_assert(base[pos-1] == opening_delimiter);
|
|
|
|
const ByteCount length = base.length();
|
|
|
|
int level = 0;
|
|
|
|
ByteCount start = pos;
|
|
|
|
while (pos != length)
|
|
|
|
{
|
|
|
|
if (base[pos] == opening_delimiter)
|
|
|
|
++level;
|
|
|
|
else if (base[pos] == closing_delimiter)
|
|
|
|
{
|
|
|
|
if (level > 0)
|
|
|
|
--level;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
return base.substr(start, pos - start);
|
|
|
|
}
|
|
|
|
|
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)
|
2014-11-04 16:40:35 +03:00
|
|
|
: parse_error{"unknown expand '" + name + "'"} {}
|
|
|
|
};
|
|
|
|
|
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 == "")
|
|
|
|
return Token::Type::Raw;
|
|
|
|
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;
|
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
|
|
|
|
return Token::Type::Raw;
|
2013-12-29 22:37:48 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 00:49:25 +04:00
|
|
|
void skip_blanks_and_comments(StringView base, ByteCount& pos)
|
2013-12-29 22:37:48 +04:00
|
|
|
{
|
|
|
|
const ByteCount length = base.length();
|
|
|
|
while (pos != length)
|
|
|
|
{
|
|
|
|
if (is_horizontal_blank(base[pos]))
|
|
|
|
++pos;
|
|
|
|
else if (base[pos] == '\\' and pos+1 < length and base[pos+1] == '\n')
|
|
|
|
pos += 2;
|
|
|
|
else if (base[pos] == '#')
|
|
|
|
{
|
2014-07-15 16:15:21 +04:00
|
|
|
while (pos != length)
|
|
|
|
{
|
|
|
|
if (base[pos++] == '\n')
|
|
|
|
break;
|
|
|
|
}
|
2013-12-29 22:37:48 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-04 16:40:35 +03:00
|
|
|
struct unterminated_string : parse_error
|
|
|
|
{
|
2014-11-15 21:45:56 +03:00
|
|
|
unterminated_string(StringView open, StringView close, int nest = 0)
|
2014-11-04 16:40:35 +03:00
|
|
|
: parse_error{"unterminated string '" + open + "..." + close + "'" +
|
|
|
|
(nest > 0 ? "(nesting: " + to_string(nest) + ")" : "")}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2014-04-04 02:57:14 +04:00
|
|
|
template<bool throw_on_unterminated>
|
2014-04-22 00:49:25 +04:00
|
|
|
Token parse_percent_token(StringView line, ByteCount& pos)
|
2014-04-04 02:57:14 +04:00
|
|
|
{
|
|
|
|
const ByteCount length = line.length();
|
|
|
|
const ByteCount type_start = ++pos;
|
|
|
|
while (isalpha(line[pos]))
|
|
|
|
++pos;
|
2014-04-22 00:49:25 +04:00
|
|
|
StringView type_name = line.substr(type_start, pos - type_start);
|
2014-04-04 02:57:14 +04:00
|
|
|
|
|
|
|
if (throw_on_unterminated and pos == length)
|
2014-04-08 22:54:32 +04:00
|
|
|
throw parse_error{"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);
|
2014-12-16 21:57:19 +03:00
|
|
|
static const UnorderedMap<char, char> matching_delimiters = {
|
2014-04-04 02:57:14 +04:00
|
|
|
{ '(', ')' }, { '[', ']' }, { '{', '}' }, { '<', '>' }
|
|
|
|
};
|
|
|
|
|
|
|
|
char opening_delimiter = line[pos];
|
|
|
|
ByteCount token_start = ++pos;
|
|
|
|
|
|
|
|
auto delim_it = matching_delimiters.find(opening_delimiter);
|
|
|
|
if (delim_it != matching_delimiters.end())
|
|
|
|
{
|
|
|
|
char closing_delimiter = delim_it->second;
|
|
|
|
String token = get_until_delimiter(line, pos, opening_delimiter,
|
|
|
|
closing_delimiter);
|
|
|
|
if (throw_on_unterminated and pos == length)
|
2014-12-08 16:59:29 +03:00
|
|
|
throw unterminated_string("%" + type_name + StringView{opening_delimiter},
|
2014-11-15 21:45:56 +03:00
|
|
|
closing_delimiter, 0);
|
2014-04-04 02:57:14 +04:00
|
|
|
return {type, token_start, pos, std::move(token)};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String token = get_until_delimiter(line, pos, opening_delimiter);
|
|
|
|
return {type, token_start, pos, std::move(token)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-04-04 02:57:14 +04:00
|
|
|
const ByteCount length = line.length();
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount pos = 0;
|
2012-07-31 16:22:57 +04:00
|
|
|
while (pos < length)
|
2011-09-07 22:16:56 +04:00
|
|
|
{
|
2013-12-29 22:37:48 +04:00
|
|
|
skip_blanks_and_comments(line, pos);
|
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 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;
|
2013-06-27 22:07:26 +04:00
|
|
|
String token = get_until_delimiter(line, pos, delimiter);
|
2013-12-30 18:22:18 +04:00
|
|
|
if (throw_on_unterminated and pos == length)
|
2014-11-15 21:45:56 +03:00
|
|
|
throw unterminated_string(delimiter, delimiter);
|
2014-11-11 16:53:57 +03:00
|
|
|
result.emplace_back(delimiter == '"' ? Token::Type::RawEval
|
|
|
|
: Token::Type::Raw,
|
|
|
|
token_start, pos, std::move(token));
|
2012-07-31 16:22:57 +04:00
|
|
|
}
|
|
|
|
else if (line[pos] == '%')
|
2014-04-08 22:54:32 +04:00
|
|
|
result.push_back(
|
|
|
|
parse_percent_token<throw_on_unterminated>(line, pos));
|
2012-02-14 01:38:07 +04:00
|
|
|
else
|
2013-06-27 22:07:26 +04:00
|
|
|
{
|
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;
|
2013-06-27 22:07:26 +04:00
|
|
|
if (start_pos != pos)
|
|
|
|
{
|
2014-11-04 16:35:54 +03:00
|
|
|
result.emplace_back(
|
|
|
|
Token::Type::Raw, token_start, pos,
|
|
|
|
unescape(line.substr(token_start, pos - token_start),
|
|
|
|
" \t;\n", '\\'));
|
2013-06-27 22:07:26 +04:00
|
|
|
}
|
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]))
|
2014-04-08 22:54:32 +04:00
|
|
|
result.emplace_back(Token::Type::CommandSeparator, pos, pos+1);
|
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
|
|
|
return result;
|
2011-11-26 22:32:57 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 02:57:14 +04:00
|
|
|
String eval_token(const Token& token, Context& context,
|
2015-01-06 16:40:56 +03:00
|
|
|
ArrayView<String> shell_params,
|
2014-04-04 02:57:14 +04:00
|
|
|
const EnvVarMap& env_vars);
|
|
|
|
|
2014-12-08 00:01:42 +03:00
|
|
|
String eval(StringView str, Context& context,
|
2015-01-06 16:40:56 +03:00
|
|
|
ArrayView<String> shell_params,
|
2014-04-04 02:57:14 +04:00
|
|
|
const EnvVarMap& env_vars)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
auto pos = 0_byte;
|
|
|
|
auto length = str.length();
|
|
|
|
while (pos < length)
|
|
|
|
{
|
|
|
|
if (str[pos] == '\\')
|
|
|
|
{
|
|
|
|
char c = str[++pos];
|
|
|
|
if (c != '%' and c != '\\')
|
|
|
|
res += '\\';
|
|
|
|
res += c;
|
2014-06-26 21:57:51 +04:00
|
|
|
++pos;
|
2014-04-04 02:57:14 +04:00
|
|
|
}
|
|
|
|
else if (str[pos] == '%')
|
|
|
|
{
|
|
|
|
Token token = parse_percent_token<true>(str, pos);
|
|
|
|
res += eval_token(token, context, shell_params, env_vars);
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res += str[pos++];
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String eval_token(const Token& token, Context& context,
|
2015-01-06 16:40:56 +03:00
|
|
|
ArrayView<String> shell_params,
|
2014-04-04 02:57:14 +04:00
|
|
|
const EnvVarMap& env_vars)
|
|
|
|
{
|
2014-04-08 22:54:32 +04:00
|
|
|
auto& content = token.content();
|
2014-04-04 02:57:14 +04:00
|
|
|
switch (token.type())
|
|
|
|
{
|
|
|
|
case Token::Type::ShellExpand:
|
2014-04-08 22:54:32 +04:00
|
|
|
return ShellManager::instance().eval(content, context, shell_params,
|
|
|
|
env_vars);
|
2014-04-04 02:57:14 +04:00
|
|
|
case Token::Type::RegisterExpand:
|
2014-06-21 14:31:08 +04:00
|
|
|
return context.main_sel_register_value(content);
|
2014-04-04 02:57:14 +04:00
|
|
|
case Token::Type::OptionExpand:
|
2014-04-08 22:54:32 +04:00
|
|
|
return context.options()[content].get_as_string();
|
2014-06-18 22:28:48 +04:00
|
|
|
case Token::Type::ValExpand:
|
2014-07-30 22:17:04 +04:00
|
|
|
{
|
|
|
|
auto it = env_vars.find(content);
|
|
|
|
if (it != env_vars.end())
|
|
|
|
return it->second;
|
2014-06-18 22:28:48 +04:00
|
|
|
return ShellManager::instance().get_val(content, context);
|
2014-07-30 22:17:04 +04:00
|
|
|
}
|
2014-04-04 02:57:14 +04:00
|
|
|
case Token::Type::RawEval:
|
2014-04-08 22:54:32 +04:00
|
|
|
return eval(content, context, shell_params, env_vars);
|
2014-04-04 02:57:14 +04:00
|
|
|
case Token::Type::Raw:
|
2014-04-08 22:54:32 +04:00
|
|
|
return content;
|
2014-04-04 02:57:14 +04:00
|
|
|
default: kak_assert(false);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2012-08-02 08:41:55 +04:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
CharCoord pos) const
|
2012-08-06 21:29:51 +04:00
|
|
|
{
|
|
|
|
if (params.empty())
|
|
|
|
return;
|
|
|
|
|
2015-01-06 16:40:56 +03:00
|
|
|
ArrayView<String> 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);
|
|
|
|
command_it->second.command(parameter_parser, context);
|
|
|
|
}
|
|
|
|
catch (runtime_error& error)
|
|
|
|
{
|
|
|
|
String info = to_string(pos.line+1) + ":" + to_string(pos.column+1) +
|
|
|
|
": '" + command_it->first + "' " + error.what();
|
|
|
|
throw runtime_error(std::move(info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static CharCoord find_coord(StringView str, ByteCount offset)
|
|
|
|
{
|
|
|
|
CharCoord res;
|
|
|
|
auto it = str.begin();
|
|
|
|
auto line_start = it;
|
|
|
|
while (it != str.end() and offset > 0)
|
|
|
|
{
|
|
|
|
if (*it == '\n')
|
|
|
|
{
|
|
|
|
line_start = it + 1;
|
|
|
|
++res.line;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
--offset;
|
|
|
|
}
|
|
|
|
res.column = utf8::distance(line_start, it);
|
|
|
|
return res;
|
2012-08-06 21:29:51 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 00:49:25 +04:00
|
|
|
void CommandManager::execute(StringView command_line,
|
2012-08-07 00:02:11 +04:00
|
|
|
Context& context,
|
2015-01-06 16:40:56 +03:00
|
|
|
ArrayView<String> shell_params,
|
2012-05-29 09:42:26 +04:00
|
|
|
const EnvVarMap& env_vars)
|
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;
|
|
|
|
|
2014-05-07 23:39:59 +04:00
|
|
|
CharCoord command_coord;
|
2015-01-12 16:58:41 +03:00
|
|
|
Vector<String> params;
|
2012-08-06 21:29:51 +04:00
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); ++it)
|
2012-01-15 05:37:35 +04:00
|
|
|
{
|
2014-05-07 23:39:59 +04:00
|
|
|
if (params.empty())
|
|
|
|
command_coord = find_coord(command_line, it->begin());
|
|
|
|
|
2014-04-04 02:57:14 +04:00
|
|
|
if (it->type() == Token::Type::CommandSeparator)
|
2012-01-15 05:37:35 +04:00
|
|
|
{
|
2014-05-07 23:39:59 +04:00
|
|
|
execute_single_command(params, context, command_coord);
|
2014-04-04 02:57:14 +04:00
|
|
|
params.clear();
|
|
|
|
}
|
|
|
|
// Shell expand are retokenized
|
|
|
|
else if (it->type() == Token::Type::ShellExpand)
|
|
|
|
{
|
|
|
|
auto shell_tokens = parse<true>(eval_token(*it, context,
|
2014-04-08 22:54:32 +04:00
|
|
|
shell_params,
|
|
|
|
env_vars));
|
2012-08-06 21:29:51 +04:00
|
|
|
it = tokens.erase(it);
|
|
|
|
for (auto& token : shell_tokens)
|
|
|
|
it = ++tokens.insert(it, std::move(token));
|
|
|
|
|
2014-04-04 02:57:14 +04:00
|
|
|
if (tokens.empty())
|
2012-08-06 21:29:51 +04:00
|
|
|
break;
|
2014-04-04 02:57:14 +04:00
|
|
|
|
|
|
|
it -= shell_tokens.size() + 1;
|
2012-01-15 05:37:35 +04:00
|
|
|
}
|
2014-04-04 02:57:14 +04:00
|
|
|
else
|
2014-04-08 22:54:32 +04:00
|
|
|
params.push_back(eval_token(*it, context, shell_params,
|
|
|
|
env_vars));
|
2012-01-15 05:37:35 +04:00
|
|
|
}
|
2014-05-07 23:39:59 +04:00
|
|
|
execute_single_command(params, context, command_coord);
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|
|
|
|
|
2014-10-30 02:22:54 +03:00
|
|
|
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-22 00:49:25 +04:00
|
|
|
CommandInfo res;
|
2014-04-08 22:54:32 +04:00
|
|
|
if (cmd_idx == tokens.size() or
|
|
|
|
tokens[cmd_idx].type() != Token::Type::Raw)
|
2014-02-12 02:16:17 +04:00
|
|
|
return res;
|
|
|
|
|
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())
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res.first = cmd->first;
|
2014-02-12 13:02:09 +04:00
|
|
|
if (not cmd->second.docstring.empty())
|
|
|
|
res.second += 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)
|
|
|
|
{
|
|
|
|
if (it->type() == Token::Type::Raw or it->type() == Token::Type::RawEval)
|
|
|
|
params.push_back(it->content());
|
|
|
|
}
|
|
|
|
String helpstr = cmd->second.helper(context, params);
|
|
|
|
if (not helpstr.empty())
|
|
|
|
{
|
|
|
|
if (helpstr.back() != '\n')
|
|
|
|
helpstr += '\n';
|
|
|
|
res.second += helpstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
res.second += "Aliases:" + aliases + "\n";
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2014-02-12 02:23:44 +04:00
|
|
|
res.second += "Switches:\n";
|
|
|
|
res.second += generate_switches_doc(switches);
|
2014-02-12 02:16:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 00:07:40 +04:00
|
|
|
// command name completion
|
2013-12-30 18:22:18 +04:00
|
|
|
if (tokens.empty() or
|
2014-01-04 00:07:40 +04:00
|
|
|
(tok_idx == cmd_idx and (tok_idx == tokens.size() or
|
|
|
|
tokens[tok_idx].type() == Token::Type::Raw)))
|
2011-09-14 01:16:48 +04:00
|
|
|
{
|
2014-04-08 22:54:32 +04:00
|
|
|
const bool is_end_token = tok_idx == tokens.size();
|
|
|
|
ByteCount cmd_start = is_end_token ? cursor_pos
|
|
|
|
: tokens[tok_idx].begin();
|
2011-10-19 22:50:28 +04:00
|
|
|
Completions result(cmd_start, cursor_pos);
|
2014-04-22 00:49:25 +04:00
|
|
|
StringView prefix = command_line.substr(cmd_start,
|
|
|
|
cursor_pos - cmd_start);
|
2011-09-14 01:16:48 +04:00
|
|
|
|
|
|
|
for (auto& command : m_commands)
|
|
|
|
{
|
2013-11-12 23:38:19 +04:00
|
|
|
if (command.second.flags & CommandFlags::Hidden)
|
|
|
|
continue;
|
|
|
|
if ( prefix_match(command.first, prefix))
|
2011-09-14 01:16:48 +04:00
|
|
|
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());
|
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
|
|
|
|
2014-01-04 00:07:40 +04:00
|
|
|
const Token::Type token_type = tok_idx < tokens.size() ?
|
|
|
|
tokens[tok_idx].type() : Token::Type::Raw;
|
2013-12-30 18:22:18 +04:00
|
|
|
switch (token_type)
|
|
|
|
{
|
|
|
|
case Token::Type::OptionExpand:
|
2014-01-26 20:14:02 +04:00
|
|
|
{
|
|
|
|
Completions result(start , cursor_pos);
|
2014-04-08 22:54:32 +04:00
|
|
|
result.candidates = context.options().complete_option_name(
|
|
|
|
tokens[tok_idx].content(), cursor_pos_in_token);
|
2013-12-30 18:22:18 +04:00
|
|
|
return result;
|
2014-01-26 20:14:02 +04:00
|
|
|
}
|
2013-12-30 18:22:18 +04:00
|
|
|
case Token::Type::ShellExpand:
|
|
|
|
{
|
2014-04-08 22:54:32 +04:00
|
|
|
Completions shell_completions = shell_complete(
|
|
|
|
context, flags, tokens[tok_idx].content(), cursor_pos_in_token);
|
2014-01-26 20:14:02 +04:00
|
|
|
shell_completions.start += start;
|
|
|
|
shell_completions.end += start;
|
|
|
|
return shell_completions;
|
2013-12-30 18:22:18 +04:00
|
|
|
}
|
|
|
|
case Token::Type::Raw:
|
|
|
|
{
|
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();
|
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;
|
2014-04-08 22:54:32 +04:00
|
|
|
for (auto token_it = tokens.begin() + cmd_idx + 1;
|
|
|
|
token_it != tokens.end(); ++token_it)
|
2013-12-30 18:22:18 +04:00
|
|
|
params.push_back(token_it->content());
|
2014-01-04 00:07:40 +04:00
|
|
|
if (tok_idx == tokens.size())
|
2013-12-30 18:22:18 +04:00
|
|
|
params.push_back("");
|
2014-04-08 22:54:32 +04:00
|
|
|
Completions completions = command_it->second.completer(
|
|
|
|
context, flags, params, tok_idx - cmd_idx - 1,
|
|
|
|
cursor_pos_in_token);
|
2014-01-26 20:14:02 +04:00
|
|
|
completions.start += start;
|
|
|
|
completions.end += start;
|
2014-05-05 15:54:23 +04:00
|
|
|
|
|
|
|
for (auto& candidate : completions.candidates)
|
2014-08-03 13:03:14 +04:00
|
|
|
candidate = escape(candidate, " \t;", '\\');
|
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
|
|
|
}
|
|
|
|
|
2014-01-26 20:14:02 +04:00
|
|
|
Completions PerArgumentCommandCompleter::operator()(const Context& context,
|
|
|
|
CompletionFlags flags,
|
|
|
|
CommandParameters params,
|
|
|
|
size_t token_to_complete,
|
2014-04-08 22:54:32 +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())
|
2014-01-26 20:14:02 +04:00
|
|
|
return Completions{};
|
2011-09-16 13:18:51 +04:00
|
|
|
|
|
|
|
// 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();
|
2014-04-08 22:54:32 +04:00
|
|
|
return m_completers[token_to_complete](context, flags, argument,
|
|
|
|
pos_in_token);
|
2011-09-14 01:16:48 +04:00
|
|
|
}
|
|
|
|
|
2011-09-07 22:16:56 +04:00
|
|
|
}
|