1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-15 01:11:09 +03:00

Add object mode expansions

This commit is contained in:
Jason Felice 2019-02-14 09:53:36 -05:00
parent e169a1893b
commit 7cf6eddc30
4 changed files with 52 additions and 11 deletions

View File

@ -215,7 +215,7 @@ The following expansions are supported (with required context _in italics_):
directory containing the user configuration directory containing the user configuration
*%val{count}*:: *%val{count}*::
_in `map` command <keys> parameter_ + _in `map` command <keys> parameter and `<a-;>` from object menu_ +
current count when the mapping was triggered, defaults to 0 if no current count when the mapping was triggered, defaults to 0 if no
count given count given
@ -262,14 +262,25 @@ The following expansions are supported (with required context _in italics_):
_in buffer, window scope_ + _in buffer, window scope_ +
`true` if the buffer has modifications not saved, otherwise `false` `true` if the buffer has modifications not saved, otherwise `false`
*%val{object_flags}*::
_for commands executed from the object menu's `<a-;>` 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}*:: *%val{register}*::
_in `map` command <keys> parameter_ + _in `map` command <keys> parameter and `<a-;>` from the object menu_ +
current register when the mapping was triggered current register when the mapping was triggered
*%val{runtime}*:: *%val{runtime}*::
directory containing the kak support files, determined from Kakoune's directory containing the kak support files, determined from Kakoune's
binary location binary location
*%val{select_mode}*::
_for commands executed from the object menu's `<a-;>` only_ +
`replace` if the new selection should replace the existing, `extend`
otherwise
*%val{selection}*:: *%val{selection}*::
_in window scope_ + _in window scope_ +
content of the main selection content of the main selection

View File

@ -682,8 +682,8 @@ in order to specify the wanted object:
select user defined object, will prompt for open and close text select user defined object, will prompt for open and close text
*<a-;>*:: *<a-;>*::
run a command in object context. The expansions `%val{count}` and run a command with additional expansions describing the selection
`%val{register}` are available here. context (See <<expansions#,`:doc expansions`>>)
== Prompt commands == Prompt commands

View File

@ -9,6 +9,7 @@
#include "commands.hh" #include "commands.hh"
#include "context.hh" #include "context.hh"
#include "diff.hh" #include "diff.hh"
#include "enum.hh"
#include "face_registry.hh" #include "face_registry.hh"
#include "file.hh" #include "file.hh"
#include "flags.hh" #include "flags.hh"
@ -37,6 +38,14 @@ enum class SelectMode
Append, Append,
}; };
constexpr auto enum_desc(Meta::Type<SelectMode>)
{
return make_array<EnumDesc<SelectMode>, 3>({
{ SelectMode::Replace, "replace" },
{ SelectMode::Extend, "extend" },
{ SelectMode::Append, "append" },
});
}
void merge_selections(Selection& sel, const Selection& new_sel) void merge_selections(Selection& sel, const Selection& new_sel)
{ {
const bool forward = sel.cursor() >= sel.anchor(); const bool forward = sel.cursor() >= sel.anchor();
@ -437,7 +446,7 @@ void for_each_codepoint(Context& context, NormalParams)
selections.insert(strings, InsertMode::Replace); selections.insert(strings, InsertMode::Replace);
} }
void command(Context& context, NormalParams params) void command(Context& context, EnvVarMap env_vars)
{ {
if (not CommandManager::has_instance()) if (not CommandManager::has_instance())
throw runtime_error{"commands are not supported"}; throw runtime_error{"commands are not supported"};
@ -451,7 +460,7 @@ void command(Context& context, NormalParams params)
StringView cmd_line, ByteCount pos) { StringView cmd_line, ByteCount pos) {
return CommandManager::instance().complete(context, flags, cmd_line, 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()) if (context.has_client())
{ {
context.client().info_hide(); context.client().info_hide();
@ -479,16 +488,21 @@ void command(Context& context, NormalParams params)
else if (not is_blank(cmdline[0])) else if (not is_blank(cmdline[0]))
RegisterManager::instance()[':'].set(context, cmdline.str()); RegisterManager::instance()[':'].set(context, cmdline.str());
EnvVarMap env_vars = {
{ "count", to_string(params.count) },
{ "register", String{&params.reg, 1} }
};
CommandManager::instance().execute( CommandManager::instance().execute(
cmdline, context, { {}, env_vars }); cmdline, context, { {}, env_vars });
} }
}); });
} }
void command(Context& context, NormalParams params)
{
EnvVarMap env_vars = {
{ "count", to_string(params.count) },
{ "register", String{&params.reg, 1} }
};
command(context, std::move(env_vars));
}
BufferCoord apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView after) BufferCoord apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView after)
{ {
const auto lines_before = before | split_after<StringView>('\n') | gather<Vector<StringView>>(); const auto lines_before = before | split_after<StringView>('\n') | gather<Vector<StringView>>();
@ -1313,7 +1327,13 @@ void select_object(Context& context, NormalParams params)
if (key == alt(';')) if (key == alt(';'))
{ {
command(context, params); EnvVarMap env_vars = {
{ "count", to_string(params.count) },
{ "register", String{&params.reg, 1} },
{ "select_mode", option_to_string(mode) },
{ "object_flags", option_to_string(flags) }
};
command(context, std::move(env_vars));
return; return;
} }

View File

@ -1,6 +1,7 @@
#ifndef selectors_hh_INCLUDED #ifndef selectors_hh_INCLUDED
#define selectors_hh_INCLUDED #define selectors_hh_INCLUDED
#include "enum.hh"
#include "optional.hh" #include "optional.hh"
#include "meta.hh" #include "meta.hh"
#include "unicode.hh" #include "unicode.hh"
@ -62,6 +63,15 @@ enum class ObjectFlags
constexpr bool with_bit_ops(Meta::Type<ObjectFlags>) { return true; } constexpr bool with_bit_ops(Meta::Type<ObjectFlags>) { return true; }
constexpr auto enum_desc(Meta::Type<ObjectFlags>)
{
return make_array<EnumDesc<ObjectFlags>, 3>({
{ ObjectFlags::ToBegin, "to_begin" },
{ ObjectFlags::ToEnd, "to_end" },
{ ObjectFlags::Inner, "inner" },
});
}
template<WordType word_type> template<WordType word_type>
Optional<Selection> Optional<Selection>
select_word(const Context& context, const Selection& selection, select_word(const Context& context, const Selection& selection,