diff --git a/src/completion.cc b/src/completion.cc new file mode 100644 index 000000000..b2579ff3b --- /dev/null +++ b/src/completion.cc @@ -0,0 +1,38 @@ +#include "completion.hh" +#include "file.hh" +#include "context.hh" + +namespace Kakoune +{ + +Completions shell_complete(const Context& context, CompletionFlags flags, + const String& prefix, ByteCount cursor_pos) +{ + ByteCount word_start = 0; + ByteCount word_end = 0; + + bool first = true; + const ByteCount len = prefix.length(); + for (ByteCount pos = 0; pos < cursor_pos;) + { + if (pos != 0) + first = false; + while (pos != len and is_blank(prefix[pos])) + ++pos; + word_start = pos; + while (pos != len and not is_blank(prefix[pos])) + ++pos; + word_end = pos; + } + Completions completions{word_start, word_end}; + if (first) + completions.candidates = complete_command(prefix.substr(word_start, word_end), + cursor_pos - word_start); + else + completions.candidates = complete_filename(prefix.substr(word_start, word_end), + context.options()["ignored_files"].get(), + cursor_pos - word_start); + return completions; +} + +} diff --git a/src/completion.hh b/src/completion.hh index 44726dd39..84e08dcba 100644 --- a/src/completion.hh +++ b/src/completion.hh @@ -40,5 +40,8 @@ inline Completions complete_nothing(const Context& context, CompletionFlags, return Completions(cursor_pos, cursor_pos); } +Completions shell_complete(const Context& context, CompletionFlags, + const String&, ByteCount cursor_pos); + } #endif // completion_hh_INCLUDED diff --git a/src/normal.cc b/src/normal.cc index 14ff7b40d..b89dc3954 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -421,38 +421,8 @@ void command(Context& context, int) template void pipe(Context& context, int) { - auto completer = [](const Context& context, CompletionFlags flags, - const String& prefix, ByteCount cursor_pos) - { - ByteCount word_start = 0; - ByteCount word_end = 0; - - bool first = true; - const ByteCount len = prefix.length(); - for (ByteCount pos = 0; pos < cursor_pos;) - { - if (pos != 0) - first = false; - while (pos != len and is_blank(prefix[pos])) - ++pos; - word_start = pos; - while (pos != len and not is_blank(prefix[pos])) - ++pos; - word_end = pos; - } - Completions completions{word_start, word_end}; - if (first) - completions.candidates = complete_command(prefix.substr(word_start, word_end), - cursor_pos - word_start); - else - completions.candidates = complete_filename(prefix.substr(word_start, word_end), - context.options()["ignored_files"].get(), - cursor_pos - word_start); - return completions; - }; - const char* prompt = mode == InsertMode::Replace ? "pipe:" : "pipe (ins):"; - context.input_handler().prompt(prompt, get_color("Prompt"), completer, + context.input_handler().prompt(prompt, get_color("Prompt"), shell_complete, [](const String& cmdline, PromptEvent event, Context& context) { if (event != PromptEvent::Validate)