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

Allow options to have type Key

This commit is contained in:
Devin J. Pohly 2024-02-24 21:16:35 -06:00
parent 3d14582d0f
commit a7a3451ea7
4 changed files with 27 additions and 1 deletions

View File

@ -79,6 +79,11 @@ are exclusively available to built-in options.
as a string but the set commands will complain if the entered text
is not a valid regex
*key*::
a single keypress using the same syntax as `map` (see
<<mapping#mappable-keys,`:doc mapping mappable-keys`>>). If
multiple keys are entered, only the first will be used.
*coord*::
a line, column pair (separated by a comma)
Cannot be used with `declare-option`

View File

@ -1827,6 +1827,7 @@ const CommandDesc declare_option_cmd = {
" bool: boolean (true/false or yes/no)\n"
" str: character string\n"
" regex: regular expression\n"
" key: keystroke specifier\n"
" int-list: list of integers\n"
" str-list: list of character strings\n"
" completions: list of completion candidates\n"
@ -1843,7 +1844,7 @@ const CommandDesc declare_option_cmd = {
make_completer(
[](const Context& context, CompletionFlags flags,
StringView prefix, ByteCount cursor_pos) -> Completions {
auto c = {"int", "bool", "str", "regex", "int-list", "str-list", "completions", "line-specs", "range-specs", "str-to-str-map"};
auto c = {"int", "bool", "str", "regex", "key", "int-list", "str-list", "completions", "line-specs", "range-specs", "str-to-str-map"};
return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c), Completions::Flags::Menu };
}),
[](const ParametersParser& parser, Context& context, const ShellContext&)
@ -1866,6 +1867,8 @@ const CommandDesc declare_option_cmd = {
opt = &reg.declare_option<String>(parser[1], docstring, "", flags);
else if (parser[0] == "regex")
opt = &reg.declare_option<Regex>(parser[1], docstring, Regex{}, flags);
else if (parser[0] == "key")
opt = &reg.declare_option<Key>(parser[1], docstring, Key(Key::Invalid), flags);
else if (parser[0] == "int-list")
opt = &reg.declare_option<Vector<int, MemoryDomain::Options>>(parser[1], docstring, {}, flags);
else if (parser[0] == "str-list")

View File

@ -221,6 +221,20 @@ String to_string(Key key)
return res;
}
String option_to_string(const Key& key)
{
return to_string(key);
}
Key option_from_string(Meta::Type<Key>, StringView str)
{
auto keys = parse_keys(str);
if (keys.empty())
return Key(Key::Invalid);
return keys.front();
}
UnitTest test_keys{[]()
{
KeyList keys{

View File

@ -94,6 +94,8 @@ struct Key
static Modifiers to_modifier(MouseButton button) { return Key::Modifiers{((int)button << 6) & (int)Modifiers::MouseButtonMask}; }
Optional<Codepoint> codepoint() const;
static constexpr const char* option_type_name = "key";
};
constexpr bool with_bit_ops(Meta::Type<Key::Modifiers>) { return true; }
@ -107,6 +109,8 @@ KeyList parse_keys(StringView str);
String to_string(Key key);
StringView to_string(Key::MouseButton button);
Key::MouseButton str_to_button(StringView str);
String option_to_string(const Key& key);
Key option_from_string(Meta::Type<Key>, StringView str);
constexpr Key shift(Key key)
{