kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

command_manager.cc (31688B)


      1 #include "command_manager.hh"
      2 
      3 #include "alias_registry.hh"
      4 #include "assert.hh"
      5 #include "context.hh"
      6 #include "debug.hh"
      7 #include "flags.hh"
      8 #include "file.hh"
      9 #include "hook_manager.hh"
     10 #include "local_scope.hh"
     11 #include "optional.hh"
     12 #include "option_manager.hh"
     13 #include "option_types.hh"
     14 #include "profile.hh"
     15 #include "ranges.hh"
     16 #include "regex.hh"
     17 #include "register_manager.hh"
     18 #include "shell_manager.hh"
     19 #include "scope.hh"
     20 #include "utils.hh"
     21 #include "unit_tests.hh"
     22 
     23 #include <algorithm>
     24 
     25 namespace Kakoune
     26 {
     27 
     28 bool CommandManager::command_defined(StringView command_name) const
     29 {
     30     return m_commands.find(command_name) != m_commands.end();
     31 }
     32 
     33 void CommandManager::register_command(String command_name,
     34                                       CommandFunc func,
     35                                       String docstring,
     36                                       ParameterDesc param_desc,
     37                                       CommandFlags flags,
     38                                       CommandHelper helper,
     39                                       CommandCompleter completer)
     40 {
     41     m_commands[command_name] = { std::move(func),
     42                                  std::move(docstring),
     43                                  std::move(param_desc),
     44                                  flags,
     45                                  std::move(helper),
     46                                  std::move(completer) };
     47 }
     48 
     49 void CommandManager::set_command_completer(StringView command_name, CommandCompleter completer)
     50 {
     51     auto it = m_commands.find(command_name);
     52     if (it == m_commands.end())
     53         throw runtime_error(format("no such command '{}'", command_name));
     54 
     55     it->value.completer = std::move(completer);
     56 }
     57 
     58 bool CommandManager::module_defined(StringView module_name) const
     59 {
     60     return m_modules.find(module_name) != m_modules.end();
     61 }
     62 
     63 void CommandManager::register_module(String module_name, String commands)
     64 {
     65     auto module = m_modules.find(module_name);
     66     if (module != m_modules.end() and module->value.state != Module::State::Registered)
     67         throw runtime_error{format("module already loaded: '{}'", module_name)};
     68 
     69     m_modules[module_name] = { Module::State::Registered, std::move(commands) };
     70 }
     71 
     72 void CommandManager::load_module(StringView module_name, Context& context)
     73 {
     74     auto module = m_modules.find(module_name);
     75     if (module == m_modules.end())
     76         throw runtime_error{format("no such module: '{}'", module_name)};
     77     switch (module->value.state)
     78     {
     79         case Module::State::Loading:
     80             throw runtime_error(format("module '{}' loaded recursively", module_name));
     81         case Module::State::Loaded: return;
     82         case Module::State::Registered: default: break;
     83     }
     84 
     85     {
     86         module->value.state = Module::State::Loading;
     87         auto restore_state = OnScopeEnd([&] { module->value.state = Module::State::Registered; });
     88 
     89         Context empty_context{Context::EmptyContextFlag{}};
     90         execute(module->value.commands, empty_context);
     91     }
     92     module->value.commands.clear();
     93     module->value.state = Module::State::Loaded;
     94 
     95     context.hooks().run_hook(Hook::ModuleLoaded, module_name, context);
     96 }
     97 
     98 HashSet<String> CommandManager::loaded_modules() const
     99 {
    100     return m_modules | filter([](auto&& elem) { return elem.value.state == Module::State::Loaded; })
    101                      | transform([](auto&& elem) { return elem.key; })
    102                      | gather<HashSet<String>>();
    103 }
    104 
    105 struct parse_error : runtime_error
    106 {
    107     parse_error(StringView error)
    108         : runtime_error{format("parse error: {}", error)} {}
    109 };
    110 
    111 namespace
    112 {
    113 
    114 bool is_command_separator(char c)
    115 {
    116     return c == ';' or c == '\n';
    117 }
    118 
    119 struct ParseResult
    120 {
    121     String content;
    122     bool terminated;
    123 };
    124 
    125 template<typename Delimiter>
    126 ParseResult parse_quoted(ParseState& state, Delimiter delimiter)
    127 {
    128     static_assert(std::is_same_v<Delimiter, char> or std::is_same_v<Delimiter, Codepoint>);
    129     auto read = [](const char*& it, const char* end) {
    130         if constexpr (std::is_same_v<Delimiter, Codepoint>)
    131             return utf8::read_codepoint(it, end);
    132         else
    133             return *it++;
    134     };
    135 
    136     const char* beg = state.pos;
    137     const char* end = state.str.end();
    138     String str;
    139 
    140     while (state.pos != end)
    141     {
    142         const char* cur = state.pos;
    143         const auto c = read(state.pos, end);
    144         if (c == delimiter)
    145         {
    146             auto next = state.pos;
    147             if (next == end or read(next, end) != delimiter)
    148             {
    149                 if (str.empty())
    150                     return {String{String::NoCopy{}, {beg, cur}}, true};
    151 
    152                 str += StringView{beg, cur};
    153                 return {str, true};
    154             }
    155             str += StringView{beg, state.pos};
    156             state.pos = beg = next;
    157         }
    158     }
    159     if (beg < end)
    160         str += StringView{beg, end};
    161     return {str, false};
    162 }
    163 
    164 template<char opening_delimiter, char closing_delimiter>
    165 ParseResult parse_quoted_balanced(ParseState& state)
    166 {
    167     int level = 1;
    168     const char* pos = state.pos;
    169     const char* beg = pos;
    170     const char* end = state.str.end();
    171     while (pos != end)
    172     {
    173         const char c = *pos++;
    174         if (c == opening_delimiter)
    175             ++level;
    176         else if (c == closing_delimiter and --level == 0)
    177             break;
    178     }
    179     state.pos = pos;
    180     const bool terminated = (level == 0);
    181     return {String{String::NoCopy{}, {beg, pos - terminated}}, terminated};
    182 }
    183 
    184 bool is_ascii_horizontal_blank(char c)
    185 {
    186     return c == '\t'      or
    187            c == '\f'      or
    188            c == ' ';
    189 }
    190 
    191 String parse_unquoted(ParseState& state)
    192 {
    193     const char* beg = state.pos;
    194     const char* end = state.str.end();
    195 
    196     String str;
    197 
    198     while (state.pos != end)
    199     {
    200         const char c = *state.pos;
    201         if (is_command_separator(c) or is_ascii_horizontal_blank(c))
    202         {
    203             str += StringView{beg, state.pos};
    204             if (state.pos != beg and *(state.pos - 1) == '\\')
    205             {
    206                 str.back() = c;
    207                 beg = state.pos+1;
    208             }
    209             else
    210                 return str;
    211         }
    212         ++state.pos;
    213     }
    214     if (beg < end)
    215         str += StringView{beg, end};
    216     return str;
    217 }
    218 
    219 Token::Type token_type(StringView type_name, bool throw_on_invalid)
    220 {
    221     if (type_name == "")
    222         return Token::Type::RawQuoted;
    223     else if (type_name == "sh")
    224         return Token::Type::ShellExpand;
    225     else if (type_name == "reg")
    226         return Token::Type::RegisterExpand;
    227     else if (type_name == "opt")
    228         return Token::Type::OptionExpand;
    229     else if (type_name == "val")
    230         return Token::Type::ValExpand;
    231     else if (type_name == "arg")
    232         return Token::Type::ArgExpand;
    233     else if (type_name == "file")
    234         return Token::Type::FileExpand;
    235     else if (type_name == "exp")
    236         return Token::Type::Expand;
    237     else if (throw_on_invalid)
    238         throw parse_error{format("unknown expand '{}'", type_name)};
    239     else
    240         return Token::Type::RawQuoted;
    241 }
    242 
    243 void skip_blanks_and_comments(ParseState& state)
    244 {
    245     while (state)
    246     {
    247         const char c = *state.pos;
    248         if (is_ascii_horizontal_blank(c))
    249             ++state.pos;
    250         else if (c == '\\' and state.pos + 1 != state.str.end() and
    251                  state.pos[1] == '\n')
    252             state.pos += 2;
    253         else if (c == '#')
    254         {
    255             while (state and *state.pos != '\n')
    256                 ++state.pos;
    257         }
    258         else
    259             break;
    260     }
    261 }
    262 
    263 BufferCoord compute_coord(StringView s)
    264 {
    265     BufferCoord coord{0,0};
    266     for (auto c : s)
    267     {
    268         if (c == '\n')
    269         {
    270             ++coord.line;
    271             coord.column = 0;
    272         }
    273         else
    274             ++coord.column;
    275     }
    276     return coord;
    277 }
    278 
    279 Token parse_percent_token(ParseState& state, bool throw_on_unterminated)
    280 {
    281     kak_assert(state.pos[-1] == '%');
    282     const auto type_start = state.pos;
    283     while (state and *state.pos >= 'a' and *state.pos <= 'z')
    284         ++state.pos;
    285     StringView type_name{type_start, state.pos};
    286 
    287     bool at_end = state.pos == state.str.end();
    288     const Codepoint opening_delimiter = utf8::read_codepoint(state.pos, state.str.end());
    289     if (at_end or iswalpha(opening_delimiter))
    290     {
    291         if (throw_on_unterminated)
    292             throw parse_error{format("expected a string delimiter after '%{}'",
    293                                      type_name)};
    294         return {};
    295     }
    296 
    297     Token::Type type = token_type(type_name, throw_on_unterminated);
    298 
    299     constexpr struct CharPair { char opening; char closing; ParseResult (*parse_func)(ParseState&); } matching_pairs[] = {
    300         { '(', ')', parse_quoted_balanced<'(', ')'> },
    301         { '[', ']', parse_quoted_balanced<'[', ']'> },
    302         { '{', '}', parse_quoted_balanced<'{', '}'> },
    303         { '<', '>', parse_quoted_balanced<'<', '>'> }
    304     };
    305 
    306     auto start = state.pos;
    307     const ByteCount byte_pos = start - state.str.begin();
    308 
    309     if (auto it = find_if(matching_pairs, [=](const CharPair& cp) { return opening_delimiter == cp.opening; });
    310         it != std::end(matching_pairs))
    311     {
    312         auto quoted = it->parse_func(state);
    313         if (throw_on_unterminated and not quoted.terminated)
    314         {
    315             auto coord = compute_coord({state.str.begin(), start});
    316             throw parse_error{format("{}:{}: unterminated string '%{}{}...{}'",
    317                                      coord.line+1, coord.column+1, type_name,
    318                                      it->opening, it->closing)};
    319         }
    320 
    321         return {type, byte_pos, std::move(quoted.content), quoted.terminated};
    322     }
    323     else
    324     {
    325         const bool is_ascii = opening_delimiter < 128;
    326         auto quoted = is_ascii ? parse_quoted(state, (char)opening_delimiter) : parse_quoted(state, opening_delimiter);
    327 
    328         if (throw_on_unterminated and not quoted.terminated)
    329         {
    330             auto coord = compute_coord({state.str.begin(), start});
    331             throw parse_error{format("{}:{}: unterminated string '%{}{}...{}'",
    332                                      coord.line+1, coord.column+1, type_name,
    333                                      opening_delimiter, opening_delimiter)};
    334         }
    335 
    336         return {type, byte_pos, std::move(quoted.content), quoted.terminated};
    337     }
    338 }
    339 
    340 template<typename Target>
    341     requires (std::is_same_v<Target, Vector<String>> or std::is_same_v<Target, String>)
    342 void expand_token(Token&& token, const Context& context, const ShellContext& shell_context, Target& target)
    343 {
    344     constexpr bool single = std::is_same_v<Target, String>;
    345     auto set_target = [&](auto&& s) {
    346         if constexpr (single)
    347             target = std::move(s);
    348         else if constexpr (std::is_same_v<std::remove_cvref_t<decltype(s)>, String>)
    349             target.push_back(std::move(s));
    350         else if constexpr (std::is_same_v<decltype(s), Vector<String>&&>)
    351             target.insert(target.end(), std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
    352         else
    353             target.insert(target.end(), s.begin(), s.end());
    354     };
    355 
    356     auto&& content = token.content;
    357     switch (token.type)
    358     {
    359     case Token::Type::ShellExpand:
    360     {
    361         auto str = ShellManager::instance().eval(
    362             content, context, StringView{},
    363             ShellManager::Flags::WaitForStdout,
    364             shell_context).first;
    365 
    366         if (not str.empty() and str.back() == '\n')
    367             str.resize(str.length() - 1, 0);
    368 
    369         return set_target(std::move(str));
    370     }
    371     case Token::Type::RegisterExpand:
    372         if constexpr (single)
    373             return set_target(context.main_sel_register_value(content).str());
    374         else
    375             return set_target(RegisterManager::instance()[content].get(context));
    376     case Token::Type::OptionExpand:
    377     {
    378         auto& opt = context.options()[content];
    379         if constexpr (single)
    380             return set_target(opt.get_as_string(Quoting::Raw));
    381         else
    382             return set_target(opt.get_as_strings());
    383     }
    384     case Token::Type::ValExpand:
    385     {
    386         auto it = shell_context.env_vars.find(content);
    387         if (it != shell_context.env_vars.end())
    388             return set_target(it->value);
    389 
    390         auto val = ShellManager::instance().get_val(content, context);
    391         if constexpr (single)
    392             return set_target(join(val, ' ', false));
    393         else
    394             return set_target(std::move(val));
    395     }
    396     case Token::Type::ArgExpand:
    397     {
    398         auto& params = shell_context.params;
    399         if (content == '@')
    400         {
    401             if constexpr (single)
    402                 return set_target(join(params, ' ', false));
    403             else
    404                 return set_target(params);
    405         }
    406 
    407         const int arg = str_to_int(content);
    408         if (arg < 1)
    409             throw runtime_error("invalid argument index");
    410         return set_target(arg <= params.size() ? params[arg-1] : String{});
    411     }
    412     case Token::Type::FileExpand:
    413         return set_target(read_file(content));
    414     case Token::Type::Expand:
    415         return set_target(expand(content, context, shell_context));
    416     case Token::Type::Raw:
    417     case Token::Type::RawQuoted:
    418         return set_target(std::move(content));
    419     default: kak_assert(false);
    420     }
    421 }
    422 
    423 }
    424 
    425 CommandParser::CommandParser(StringView command_line) : m_state{command_line, command_line.begin()} {}
    426 
    427 Optional<Token> CommandParser::read_token(bool throw_on_unterminated)
    428 {
    429     skip_blanks_and_comments(m_state);
    430     if (not m_state)
    431         return {};
    432 
    433     const StringView line = m_state.str;
    434     const char* start = m_state.pos;
    435 
    436     const char c = *m_state.pos;
    437     if (c == '"' or c == '\'')
    438     {
    439         start = ++m_state.pos;
    440         ParseResult quoted = parse_quoted(m_state, c);
    441         if (throw_on_unterminated and not quoted.terminated)
    442             throw parse_error{format("unterminated string {0}...{0}", c)};
    443         return Token{c == '"' ? Token::Type::Expand
    444                               : Token::Type::RawQuoted,
    445                      start - line.begin(), std::move(quoted.content),
    446                      quoted.terminated};
    447     }
    448     else if (c == '%')
    449     {
    450         ++m_state.pos;
    451         return parse_percent_token(m_state, throw_on_unterminated);
    452     }
    453     else if (is_command_separator(c))
    454         return Token{Token::Type::CommandSeparator,
    455                      ++m_state.pos - line.begin(), {}};
    456     else
    457     {
    458         if (c == '\\' and m_state.pos + 1 != m_state.str.end())
    459         {
    460             const char next = m_state.pos[1];
    461             if (next == '%' or next == '\'' or next == '"')
    462                 ++m_state.pos;
    463         }
    464         return Token{Token::Type::Raw, start - line.begin(), parse_unquoted(m_state)};
    465     }
    466     return {};
    467 }
    468 
    469 template<typename Postprocess>
    470 String expand_impl(StringView str, const Context& context,
    471                    const ShellContext& shell_context,
    472                    Postprocess postprocess)
    473 {
    474     ParseState state{str, str.begin()};
    475     String res;
    476     auto beg = state.pos;
    477     while (state)
    478     {
    479         if (*state.pos++ == '%')
    480         {
    481             if (state and *state.pos == '%')
    482             {
    483                 res += StringView{beg, state.pos};
    484                 beg = ++state.pos;
    485             }
    486             else
    487             {
    488                 res += StringView{beg, state.pos-1};
    489                 String token;
    490                 expand_token(parse_percent_token(state, true), context, shell_context, token);
    491                 res += postprocess(token);
    492                 beg = state.pos;
    493             }
    494         }
    495     }
    496     res += StringView{beg, state.pos};
    497     return res;
    498 }
    499 
    500 String expand(StringView str, const Context& context,
    501               const ShellContext& shell_context)
    502 {
    503     return expand_impl(str, context, shell_context, [](String s){ return s; });
    504 }
    505 
    506 String expand(StringView str, const Context& context,
    507               const ShellContext& shell_context,
    508               const FunctionRef<String (String)>& postprocess)
    509 {
    510     return expand_impl(str, context, shell_context, postprocess);
    511 }
    512 
    513 StringView resolve_alias(const Context& context, StringView name)
    514 {
    515     auto alias = context.aliases()[name];
    516     return alias.empty() ? name : alias;
    517 }
    518 
    519 void CommandManager::execute_single_command(CommandParameters params,
    520                                             Context& context,
    521                                             const ShellContext& shell_context)
    522 {
    523     if (params.empty())
    524         return;
    525 
    526     constexpr int max_command_depth = 100;
    527     if (m_command_depth > max_command_depth)
    528         throw runtime_error("maximum nested command depth hit");
    529 
    530     ++m_command_depth;
    531     auto pop_depth = OnScopeEnd([this] { --m_command_depth; });
    532 
    533     auto command_it = m_commands.find(resolve_alias(context, params[0]));
    534     if (command_it == m_commands.end())
    535         throw runtime_error("no such command");
    536 
    537     auto debug_flags = context.options()["debug"].get<DebugFlags>();
    538     if (debug_flags & DebugFlags::Commands)
    539         write_to_debug_buffer(format("command {}", join(params, ' ')));
    540 
    541     ProfileScope profile{debug_flags, [&](std::chrono::microseconds duration) {
    542         write_to_debug_buffer(format("command {} took {} us", params[0], duration.count()));
    543     }};
    544 
    545     command_it->value.func({{params.begin()+1, params.end()}, command_it->value.param_desc},
    546                            context, shell_context);
    547 }
    548 
    549 void CommandManager::execute(StringView command_line,
    550                              Context& context, const ShellContext& shell_context)
    551 {
    552     CommandParser parser(command_line);
    553 
    554     LocalScope local_scope(context);
    555     ByteCount command_pos{};
    556     Vector<String> params;
    557     while (true)
    558     {
    559         Optional<Token> token = parser.read_token(true);
    560         if (not token or token->type == Token::Type::CommandSeparator)
    561         {
    562             try
    563             {
    564                 execute_single_command(params, context, shell_context);
    565             }
    566             catch (failure& error)
    567             {
    568                 throw;
    569             }
    570             catch (runtime_error& error)
    571             {
    572                 auto coord = compute_coord(command_line.substr(0_byte, command_pos));
    573                 error.set_what(format("{}:{}: '{}': {}", coord.line+1, coord.column+1,
    574                                       params[0], error.what()));
    575                 throw;
    576             }
    577 
    578             if (not token)
    579                 return;
    580 
    581             params.clear();
    582             continue;
    583         }
    584 
    585         if (params.empty())
    586             command_pos = token->pos;
    587 
    588         if (token->type == Token::Type::ArgExpand and token->content == '@')
    589             params.insert(params.end(), shell_context.params.begin(),
    590                           shell_context.params.end());
    591         else
    592             expand_token(*std::move(token), context, shell_context, params);
    593     }
    594 }
    595 
    596 Optional<CommandInfo> CommandManager::command_info(const Context& context, StringView command_line) const
    597 {
    598     CommandParser parser{command_line};
    599     Vector<Token> tokens;
    600     while (auto token = parser.read_token(false))
    601     {
    602         if (token->type == Token::Type::CommandSeparator)
    603             tokens.clear();
    604         else
    605             tokens.push_back(std::move(*token));
    606     }
    607 
    608     if (tokens.empty() or
    609         (tokens.front().type != Token::Type::Raw and
    610          tokens.front().type != Token::Type::RawQuoted))
    611         return {};
    612 
    613     auto cmd = m_commands.find(resolve_alias(context, tokens.front().content));
    614     if (cmd == m_commands.end())
    615         return {};
    616 
    617     CommandInfo res;
    618     res.name = cmd->key;
    619     if (not cmd->value.docstring.empty())
    620         res.info += cmd->value.docstring + "\n";
    621 
    622     if (cmd->value.helper)
    623     {
    624         Vector<String> params;
    625         for (auto it = tokens.begin() + 1; it != tokens.end(); ++it)
    626         {
    627             if (it->type == Token::Type::Raw or
    628                 it->type == Token::Type::RawQuoted or
    629                 it->type == Token::Type::Expand)
    630                 params.push_back(it->content);
    631         }
    632         String helpstr = cmd->value.helper(context, params);
    633         if (not helpstr.empty())
    634             res.info += format("{}\n", helpstr);
    635     }
    636 
    637     String aliases;
    638     for (auto& alias : context.aliases().aliases_for(cmd->key))
    639         aliases += " " + alias;
    640     if (not aliases.empty())
    641         res.info += format("Aliases:{}\n", aliases);
    642 
    643     auto& switches = cmd->value.param_desc.switches;
    644     if (not switches.empty())
    645         res.info += format("Switches:\n{}", indent(generate_switches_doc(switches)));
    646 
    647     return res;
    648 }
    649 
    650 Completions CommandManager::complete_command_name(const Context& context, StringView query) const
    651 {
    652     auto commands = m_commands
    653             | filter([](const CommandMap::Item& cmd) { return not (cmd.value.flags & CommandFlags::Hidden); })
    654             | transform(&CommandMap::Item::key);
    655 
    656     auto aliases = context.aliases().flatten_aliases()
    657             | transform(&HashItem<String, String>::key);
    658 
    659     return {0, query.length(),
    660             Kakoune::complete(query, query.length(), concatenated(commands, aliases)),
    661             Completions::Flags::Menu | Completions::Flags::NoEmpty};
    662 }
    663 
    664 Completions CommandManager::complete_module_name(StringView query) const
    665 {
    666     return {0, query.length(),
    667             Kakoune::complete(query, query.length(), m_modules | filter([](auto&& item) { return item.value.state == Module::State::Registered; })
    668                                                                | transform(&ModuleMap::Item::key))};
    669 }
    670 
    671 static Completions complete_expansion(const Context& context,
    672                                       Token token, ByteCount start,
    673                                       ByteCount cursor_pos, ByteCount pos_in_token)
    674 {
    675     switch (token.type) {
    676     case Token::Type::RegisterExpand:
    677         return { start, cursor_pos,
    678                  RegisterManager::instance().complete_register_name(
    679                      token.content, pos_in_token) };
    680 
    681     case Token::Type::OptionExpand:
    682         return { start, cursor_pos,
    683                  GlobalScope::instance().option_registry().complete_option_name(
    684                      token.content, pos_in_token) };
    685 
    686     case Token::Type::ShellExpand:
    687         return offset_pos(shell_complete(context, token.content,
    688                                          pos_in_token), start);
    689 
    690     case Token::Type::ValExpand:
    691         return { start, cursor_pos,
    692                  ShellManager::instance().complete_env_var(
    693                      token.content, pos_in_token) };
    694 
    695     case Token::Type::FileExpand:
    696     {
    697         const auto& ignored_files = context.options()["ignored_files"].get<Regex>();
    698         return { start, cursor_pos, complete_filename(
    699                  token.content, ignored_files, pos_in_token, FilenameFlags::Expand) };
    700     }
    701 
    702     default:
    703         kak_assert(false);
    704         throw runtime_error("unknown expansion");
    705     }
    706 }
    707 
    708 static Completions complete_expand(const Context& context,
    709                                         StringView prefix, ByteCount start,
    710                                         ByteCount cursor_pos, ByteCount pos_in_token)
    711 {
    712     ParseState state{prefix, prefix.begin()};
    713     while (state)
    714     {
    715         if (*state.pos++ == '%')
    716         {
    717             if (state and *state.pos == '%')
    718                 ++state.pos;
    719             else
    720             {
    721                 auto token = parse_percent_token(state, false);
    722                 if (token.terminated)
    723                     continue;
    724                 if (token.type == Token::Type::Raw or token.type == Token::Type::RawQuoted)
    725                     return {};
    726                 return complete_expansion(context, token,
    727                                           start + token.pos, cursor_pos,
    728                                           pos_in_token - token.pos);
    729             }
    730         }
    731     }
    732     return {};
    733 }
    734 
    735 
    736 static Completions requote(Completions completions, Token::Type token_type)
    737 {
    738     if (completions.flags & Completions::Flags::Quoted)
    739         return completions;
    740 
    741     if (token_type == Token::Type::Raw)
    742     {
    743         const bool at_token_start = completions.start == 0;
    744         for (auto& candidate : completions.candidates)
    745         {
    746             const StringView to_escape = ";\n \t";
    747             if ((at_token_start and candidate.substr(0_byte, 1_byte) == "%") or
    748                 any_of(candidate, [&](auto c) { return contains(to_escape, c); }))
    749                 candidate = at_token_start ? quote(candidate) : escape(candidate, to_escape, '\\');
    750         }
    751     }
    752     else if (token_type == Token::Type::RawQuoted)
    753         completions.flags |= Completions::Flags::Quoted;
    754     else
    755         kak_assert(false);
    756 
    757     return completions;
    758 }
    759 
    760 Completions CommandManager::Completer::operator()(
    761     const Context& context, StringView command_line, ByteCount cursor_pos)
    762 {
    763     auto prefix = command_line.substr(0_byte, cursor_pos);
    764     CommandParser parser{prefix};
    765     const char* cursor = prefix.begin() + cursor_pos;
    766     Vector<Token> tokens;
    767 
    768     bool is_last_token = true;
    769     while (auto token = parser.read_token(false))
    770     {
    771         if (token->type == Token::Type::CommandSeparator)
    772         {
    773             tokens.clear();
    774             continue;
    775         }
    776 
    777         tokens.push_back(std::move(*token));
    778         if (parser.pos() >= cursor)
    779         {
    780             is_last_token = false;
    781             break;
    782         }
    783     }
    784 
    785     if (is_last_token)
    786         tokens.push_back({Token::Type::Raw, prefix.length(), {}});
    787     kak_assert(not tokens.empty());
    788     const auto& token = tokens.back();
    789 
    790     if (token.terminated) // do not complete past explicit token close
    791         return Completions{};
    792 
    793     const ByteCount start = token.pos;
    794     const ByteCount pos_in_token = cursor_pos - start;
    795 
    796     // command name completion
    797     if (tokens.size() == 1 and (token.type == Token::Type::Raw or
    798                                 token.type == Token::Type::RawQuoted))
    799     {
    800         return offset_pos(requote(CommandManager::instance().complete_command_name(context, prefix), token.type), start);
    801     }
    802 
    803     auto& commands = CommandManager::instance().m_commands;
    804 
    805     switch (token.type)
    806     {
    807     case Token::Type::RegisterExpand:
    808     case Token::Type::OptionExpand:
    809     case Token::Type::ShellExpand:
    810     case Token::Type::ValExpand:
    811     case Token::Type::FileExpand:
    812         return complete_expansion(context, token, start, cursor_pos, pos_in_token);
    813 
    814     case Token::Type::Raw:
    815     case Token::Type::RawQuoted:
    816     {
    817         StringView command_name = tokens.front().content;
    818         auto command_it = commands.find(resolve_alias(context, command_name));
    819         if (command_it == commands.end())
    820             return Completions{};
    821 
    822         auto& command = command_it->value;
    823         if (command_name != m_last_complete_command)
    824         {
    825             m_last_complete_command = command_name.str();
    826             m_command_completer = command.completer;
    827         }
    828 
    829         auto raw_params = tokens | skip(1) | transform(&Token::content) | gather<Vector<String>>();
    830         ParametersParser parser{raw_params, command.param_desc, true};
    831 
    832         switch (parser.state())
    833         {
    834             case ParametersParser::State::Switch:
    835             {
    836                 auto switches = Kakoune::complete(token.content.substr(1_byte), pos_in_token,
    837                                                   concatenated(command.param_desc.switches
    838                                                                    | transform(&SwitchMap::Item::key)
    839                                                                    | filter([&](const auto& key) {
    840                                                                        return not parser.get_switch(key);
    841                                                                    }),
    842                                                                ConstArrayView<String>{"-"}));
    843                 return switches.empty()
    844                         ? Completions{}
    845                         : Completions{start+1, cursor_pos, std::move(switches), Completions::Flags::Menu};
    846             }
    847             case ParametersParser::State::SwitchArgument:
    848             {
    849                 const auto& switch_desc = command.param_desc.switches.get(raw_params.at(raw_params.size() - 2).substr(1_byte));
    850                 if (not *switch_desc.arg_completer)
    851                     return Completions{};
    852                 return offset_pos(requote((*switch_desc.arg_completer)(context, raw_params.back(), pos_in_token), token.type), start);
    853             }
    854             case ParametersParser::State::Positional:
    855                 break;
    856         }
    857 
    858         if (not m_command_completer)
    859             return Completions{};
    860 
    861         Vector<String> params{parser.begin(), parser.end()};
    862         auto index = params.size() - 1;
    863 
    864         return offset_pos(requote(m_command_completer(context, params, index, pos_in_token), token.type), start);
    865     }
    866     case Token::Type::Expand:
    867         return complete_expand(context, token.content, start, cursor_pos, pos_in_token);
    868     default:
    869         break;
    870     }
    871     return Completions{};
    872 }
    873 
    874 Completions CommandManager::NestedCompleter::operator()(
    875     const Context& context, CommandParameters params,
    876     size_t token_to_complete, ByteCount pos_in_token)
    877 {
    878     StringView prefix = params[token_to_complete].substr(0, pos_in_token);
    879     if (token_to_complete == 0)
    880         return CommandManager::instance().complete_command_name(context, prefix);
    881 
    882     StringView command_name = params[0];
    883     auto& commands = CommandManager::instance().m_commands;
    884     if (command_name != m_last_complete_command)
    885     {
    886         m_last_complete_command = command_name.str();
    887         auto it = commands.find(resolve_alias(context, command_name));
    888         if (it != commands.end())
    889             m_command_completer = it->value.completer;
    890     }
    891 
    892     return m_command_completer
    893         ? m_command_completer(context, params.subrange(1), token_to_complete-1, pos_in_token)
    894         : Completions{};
    895 }
    896 
    897 UnitTest test_command_parsing{[]
    898 {
    899     auto check_quoted = [](StringView str, bool terminated, StringView content)
    900     {
    901         auto check_quoted_impl = [&](auto type_hint) {
    902             ParseState state{str, str.begin()};
    903             const decltype(type_hint) delimiter = *state.pos++;
    904             auto quoted = parse_quoted(state, delimiter);
    905             kak_assert(quoted.terminated == terminated);
    906             kak_assert(quoted.content == content);
    907         };
    908         check_quoted_impl(Codepoint{});
    909         check_quoted_impl(char{});
    910     };
    911     check_quoted("'abc'", true, "abc");
    912     check_quoted("'abc''def", false, "abc'def");
    913     check_quoted("'abc''def'''", true, "abc'def'");
    914     check_quoted(StringView("'abc''def'", 5), true, "abc");
    915 
    916     auto check_balanced = [](StringView str, bool terminated, StringView content)
    917     {
    918         ParseState state{str, str.begin()+1};
    919         auto quoted = parse_quoted_balanced<'{', '}'>(state);
    920         kak_assert(quoted.terminated == terminated);
    921         kak_assert(quoted.content == content);
    922     };
    923     check_balanced("{abc}", true, "abc");
    924     check_balanced("{abc{def}}", true, "abc{def}");
    925     check_balanced("{{abc}{def}", false, "{abc}{def}");
    926 
    927     auto check_unquoted = [](StringView str, StringView content)
    928     {
    929         ParseState state{str, str.begin()};
    930         kak_assert(parse_unquoted(state) == content);
    931     };
    932     check_unquoted("abc def", "abc");
    933     check_unquoted("abc; def", "abc");
    934     check_unquoted("abc\\; def", "abc;");
    935     check_unquoted("abc\\;\\ def", "abc; def");
    936 
    937     {
    938         CommandParser parser(R"(foo 'bar' "baz" qux)");
    939         kak_assert(parser.read_token(false)->content == "foo");
    940         kak_assert(parser.read_token(false)->content == "bar");
    941         kak_assert(parser.read_token(false)->content == "baz");
    942         kak_assert(parser.read_token(false)->content == "qux");
    943         kak_assert(not parser.read_token(false));
    944     }
    945 }};
    946 
    947 }