diff --git a/README.asciidoc b/README.asciidoc index d3f2448e..f41c9e78 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -819,6 +819,8 @@ Some options are built in Kakoune, and can be used to control it's behaviour: element of the list should follow the format: _.[+]@_ to define where the completion apply in the buffer, and the other strings are the candidates. + * `static_words` _str-list_: list of words that are always added to completion + candidates when completing words in insert mode. * `autoreload` _enum(yes|no|ask)_: auto reload the buffers when an external modification is detected. * `debug` _flags(hooks|shell|profile)_: dump various debug information in diff --git a/rc/c-family.kak b/rc/c-family.kak index bcf41105..c3ec29d5 100644 --- a/rc/c-family.kak +++ b/rc/c-family.kak @@ -123,6 +123,7 @@ hook global WinSetOption filetype=(c|cpp|objc) %[ alias window alt c-family-alternative-file set window formatcmd "astyle" + set window static_words "void:char:short:int:long:signed:unsigned:float:double:size_t:while:for:if:else:do:switch:case:default:goto:asm:break:continue:return:sizeof:const:auto:register:inline:static:volatile:struct:enum:union:typedef:extern:restrict" ] hook global WinSetOption filetype=(?!(c|cpp|objc)$).* %[ diff --git a/src/insert_completer.cc b/src/insert_completer.cc index 1718220d..77f87c13 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -74,7 +74,7 @@ WordDB& get_word_db(const Buffer& buffer) } template -InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos) +InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos, const OptionManager& options) { using Utf8It = utf8::iterator; Utf8It pos{buffer.iterator_at(cursor_pos), buffer}; @@ -129,6 +129,20 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos) add_matches(*buf); } } + + { + using StaticWords = Vector; + const StaticWords& static_words = options["static_words"].get(); + + for (auto& word : static_words) + { + if (prefix.empty()) + matches.push_back(RankedMatch{word, prefix}); + else if (RankedMatch match{word, prefix}) + matches.push_back(match); + } + } + unordered_erase(matches, StringView{prefix}); std::sort(matches.begin(), matches.end()); matches.erase(std::unique(matches.begin(), matches.end()), matches.end()); @@ -160,7 +174,7 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos) template InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos, - OptionManager& options) + const OptionManager& options) { auto pos = buffer.iterator_at(cursor_pos); auto begin = pos; @@ -204,7 +218,7 @@ InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos, } InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos, - OptionManager& options, StringView option_name) + const OptionManager& options, StringView option_name) { const StringList& opt = options[option_name].get(); if (opt.empty()) @@ -280,7 +294,7 @@ InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos, return {}; } -InsertCompletion complete_line(const Buffer& buffer, OptionManager& options, ByteCoord cursor_pos) +InsertCompletion complete_line(const Buffer& buffer, ByteCoord cursor_pos, const OptionManager& options) { const CharCount tabstop = options["tabstop"].get(); const CharCount column = get_column(buffer, tabstop, cursor_pos); @@ -395,15 +409,12 @@ bool InsertCompleter::setup_ifn() for (auto& completer : completers) { if (completer.mode == InsertCompleterDesc::Filename and - try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { - return complete_filename(buffer, cursor_pos, - m_options); - })) + try_complete(complete_filename)) return true; if (completer.mode == InsertCompleterDesc::Option and - try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos) { - return complete_option(buffer, cursor_pos, - m_options, *completer.param); + try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos, + const OptionManager& options) { + return complete_option(buffer, cursor_pos, options, *completer.param); })) return true; if (completer.mode == InsertCompleterDesc::Word and @@ -457,14 +468,14 @@ void InsertCompleter::on_option_changed(const Option& opt) } } -template -bool InsertCompleter::try_complete(CompleteFunc complete_func) +template +bool InsertCompleter::try_complete(Func complete_func) { auto& buffer = m_context.buffer(); ByteCoord cursor_pos = m_context.selections().main().cursor(); try { - m_completions = complete_func(buffer, cursor_pos); + m_completions = complete_func(buffer, cursor_pos, m_options); } catch (runtime_error& e) { @@ -484,11 +495,8 @@ bool InsertCompleter::try_complete(CompleteFunc complete_func) void InsertCompleter::explicit_file_complete() { - auto func = [this](const Buffer& buffer, ByteCoord cursor_pos) { - return complete_filename(buffer, cursor_pos, m_options); - }; - try_complete(func); - m_explicit_completer = func; + try_complete(complete_filename); + m_explicit_completer = complete_filename; } void InsertCompleter::explicit_word_complete() @@ -499,11 +507,8 @@ void InsertCompleter::explicit_word_complete() void InsertCompleter::explicit_line_complete() { - auto func = [this](const Buffer& buffer, ByteCoord cursor_pos) { - return complete_line(buffer, m_options, cursor_pos); - }; - try_complete(func); - m_explicit_completer = func; + try_complete(complete_line); + m_explicit_completer = complete_line; } } diff --git a/src/insert_completer.hh b/src/insert_completer.hh index 5aeade49..64c54758 100644 --- a/src/insert_completer.hh +++ b/src/insert_completer.hh @@ -76,8 +76,8 @@ public: private: bool setup_ifn(); - template - bool try_complete(CompleteFunc complete_func); + template + bool try_complete(Func complete_func); void on_option_changed(const Option& opt) override; void menu_show(); @@ -90,7 +90,8 @@ private: CandidateList m_matching_candidates; int m_current_candidate = -1; - std::function m_explicit_completer; + using CompleteFunc = InsertCompletion (const Buffer&, ByteCoord, const OptionManager& options); + std::function m_explicit_completer; }; } diff --git a/src/main.cc b/src/main.cc index 9f5f916e..144be901 100644 --- a/src/main.cc +++ b/src/main.cc @@ -220,6 +220,8 @@ void register_options() InsertCompleterDesc{ InsertCompleterDesc::Filename }, InsertCompleterDesc{ InsertCompleterDesc::Word, "all"_str } }), OptionFlags::None); + reg.declare_option("static_words", "list of words to always consider for insert word completion", + Vector{}); reg.declare_option("autoreload", "autoreload buffer when a filesystem modification is detected", Autoreload::Ask);