From 7cf6eddc30f8d0f704e92fe13c447df3aa664ddd Mon Sep 17 00:00:00 2001 From: Jason Felice Date: Thu, 14 Feb 2019 09:53:36 -0500 Subject: [PATCH] Add object mode expansions --- doc/pages/expansions.asciidoc | 15 +++++++++++++-- doc/pages/keys.asciidoc | 4 ++-- src/normal.cc | 34 +++++++++++++++++++++++++++------- src/selectors.hh | 10 ++++++++++ 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/doc/pages/expansions.asciidoc b/doc/pages/expansions.asciidoc index 36e5d020..754cf573 100644 --- a/doc/pages/expansions.asciidoc +++ b/doc/pages/expansions.asciidoc @@ -215,7 +215,7 @@ The following expansions are supported (with required context _in italics_): directory containing the user configuration *%val{count}*:: - _in `map` command parameter_ + + _in `map` command parameter and `` from object menu_ + current count when the mapping was triggered, defaults to 0 if no count given @@ -262,14 +262,25 @@ The following expansions are supported (with required context _in italics_): _in buffer, window scope_ + `true` if the buffer has modifications not saved, otherwise `false` +*%val{object_flags}*:: + _for commands executed from the object menu's `` only_ + + a pipe-separted list of words including `inner` if the user wants + an inner selection, `to_begin` if the user wants to select to the + beginning, and `to_end` if the user wants to select to the end + *%val{register}*:: - _in `map` command parameter_ + + _in `map` command parameter and `` from the object menu_ + current register when the mapping was triggered *%val{runtime}*:: directory containing the kak support files, determined from Kakoune's binary location +*%val{select_mode}*:: + _for commands executed from the object menu's `` only_ + + `replace` if the new selection should replace the existing, `extend` + otherwise + *%val{selection}*:: _in window scope_ + content of the main selection diff --git a/doc/pages/keys.asciidoc b/doc/pages/keys.asciidoc index 012f142a..c144e34e 100644 --- a/doc/pages/keys.asciidoc +++ b/doc/pages/keys.asciidoc @@ -682,8 +682,8 @@ in order to specify the wanted object: select user defined object, will prompt for open and close text **:: - run a command in object context. The expansions `%val{count}` and - `%val{register}` are available here. + run a command with additional expansions describing the selection + context (See <>) == Prompt commands diff --git a/src/normal.cc b/src/normal.cc index 66eec914..9be1cd4a 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -9,6 +9,7 @@ #include "commands.hh" #include "context.hh" #include "diff.hh" +#include "enum.hh" #include "face_registry.hh" #include "file.hh" #include "flags.hh" @@ -37,6 +38,14 @@ enum class SelectMode Append, }; +constexpr auto enum_desc(Meta::Type) +{ + return make_array, 3>({ + { SelectMode::Replace, "replace" }, + { SelectMode::Extend, "extend" }, + { SelectMode::Append, "append" }, + }); +} void merge_selections(Selection& sel, const Selection& new_sel) { const bool forward = sel.cursor() >= sel.anchor(); @@ -437,7 +446,7 @@ void for_each_codepoint(Context& context, NormalParams) selections.insert(strings, InsertMode::Replace); } -void command(Context& context, NormalParams params) +void command(Context& context, EnvVarMap env_vars) { if (not CommandManager::has_instance()) throw runtime_error{"commands are not supported"}; @@ -451,7 +460,7 @@ void command(Context& context, NormalParams params) StringView cmd_line, ByteCount pos) { return CommandManager::instance().complete(context, flags, cmd_line, pos); }, - [params](StringView cmdline, PromptEvent event, Context& context) { + [env_vars = std::move(env_vars)](StringView cmdline, PromptEvent event, Context& context) { if (context.has_client()) { context.client().info_hide(); @@ -479,16 +488,21 @@ void command(Context& context, NormalParams params) else if (not is_blank(cmdline[0])) RegisterManager::instance()[':'].set(context, cmdline.str()); - EnvVarMap env_vars = { - { "count", to_string(params.count) }, - { "register", String{¶ms.reg, 1} } - }; CommandManager::instance().execute( cmdline, context, { {}, env_vars }); } }); } +void command(Context& context, NormalParams params) +{ + EnvVarMap env_vars = { + { "count", to_string(params.count) }, + { "register", String{¶ms.reg, 1} } + }; + command(context, std::move(env_vars)); +} + BufferCoord apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView after) { const auto lines_before = before | split_after('\n') | gather>(); @@ -1313,7 +1327,13 @@ void select_object(Context& context, NormalParams params) if (key == alt(';')) { - command(context, params); + EnvVarMap env_vars = { + { "count", to_string(params.count) }, + { "register", String{¶ms.reg, 1} }, + { "select_mode", option_to_string(mode) }, + { "object_flags", option_to_string(flags) } + }; + command(context, std::move(env_vars)); return; } diff --git a/src/selectors.hh b/src/selectors.hh index a82e29a2..d2cbd040 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -1,6 +1,7 @@ #ifndef selectors_hh_INCLUDED #define selectors_hh_INCLUDED +#include "enum.hh" #include "optional.hh" #include "meta.hh" #include "unicode.hh" @@ -62,6 +63,15 @@ enum class ObjectFlags constexpr bool with_bit_ops(Meta::Type) { return true; } +constexpr auto enum_desc(Meta::Type) +{ + return make_array, 3>({ + { ObjectFlags::ToBegin, "to_begin" }, + { ObjectFlags::ToEnd, "to_end" }, + { ObjectFlags::Inner, "inner" }, + }); +} + template Optional select_word(const Context& context, const Selection& selection,