2011-12-20 20:21:11 +01:00
|
|
|
#include "keys.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2014-12-23 14:34:21 +01:00
|
|
|
#include "exception.hh"
|
2017-08-29 10:23:03 +02:00
|
|
|
#include "ranges.hh"
|
2014-12-23 14:34:21 +01:00
|
|
|
#include "string.hh"
|
2015-05-22 14:58:56 +02:00
|
|
|
#include "unit_tests.hh"
|
2014-06-04 12:51:20 +02:00
|
|
|
#include "utf8_iterator.hh"
|
2015-05-22 14:58:56 +02:00
|
|
|
#include "utils.hh"
|
2017-10-09 16:12:42 +02:00
|
|
|
#include "string_utils.hh"
|
2011-12-20 20:21:11 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2018-03-15 13:02:27 +01:00
|
|
|
struct key_parse_error : runtime_error
|
|
|
|
{
|
|
|
|
using runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2015-08-18 01:19:14 +02:00
|
|
|
static Key canonicalize_ifn(Key key)
|
2012-09-03 19:21:11 +02:00
|
|
|
{
|
|
|
|
if (key.key > 0 and key.key < 27)
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(key.modifiers == Key::Modifiers::None);
|
2012-09-03 19:21:11 +02:00
|
|
|
key.modifiers = Key::Modifiers::Control;
|
|
|
|
key.key = key.key - 1 + 'a';
|
|
|
|
}
|
2018-03-15 13:02:27 +01:00
|
|
|
|
|
|
|
if (key.modifiers & Key::Modifiers::Shift)
|
|
|
|
{
|
|
|
|
if (is_basic_alpha(key.key))
|
|
|
|
{
|
|
|
|
// Shift + ASCII letters is just the uppercase letter.
|
|
|
|
key.modifiers &= ~Key::Modifiers::Shift;
|
|
|
|
key.key = to_upper(key.key);
|
|
|
|
}
|
|
|
|
else if (key.key < 0xD800 || key.key > 0xDFFF)
|
|
|
|
{
|
|
|
|
// Shift + any other printable character is not allowed.
|
|
|
|
throw key_parse_error(format("Shift modifier only works on special keys and lowercase ASCII, not '{}'", key.key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-03 19:21:11 +02:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:19:14 +02:00
|
|
|
Optional<Codepoint> Key::codepoint() const
|
|
|
|
{
|
2016-07-05 20:21:15 +02:00
|
|
|
if (*this == Key::Return)
|
2015-08-18 01:19:14 +02:00
|
|
|
return '\n';
|
2016-07-05 20:21:15 +02:00
|
|
|
if (*this == Key::Tab)
|
2015-08-18 01:19:14 +02:00
|
|
|
return '\t';
|
2022-02-09 12:43:36 +01:00
|
|
|
if (*this == Key::Space or *this == shift(Key::Space))
|
2021-12-10 22:12:08 +01:00
|
|
|
return ' ';
|
2016-12-14 01:34:53 +01:00
|
|
|
if (*this == Key::Escape)
|
|
|
|
return 0x1B;
|
2015-08-18 01:19:14 +02:00
|
|
|
if (modifiers == Modifiers::None and key > 27 and
|
|
|
|
(key < 0xD800 or key > 0xDFFF)) // avoid surrogates
|
|
|
|
return key;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:18:42 +01:00
|
|
|
struct KeyAndName { const char* name; Codepoint key; };
|
|
|
|
static constexpr KeyAndName keynamemap[] = {
|
2016-07-05 20:21:15 +02:00
|
|
|
{ "ret", Key::Return },
|
2021-12-10 22:12:08 +01:00
|
|
|
{ "space", Key::Space },
|
2016-07-05 20:21:15 +02:00
|
|
|
{ "tab", Key::Tab },
|
2013-04-02 18:41:45 +02:00
|
|
|
{ "lt", '<' },
|
|
|
|
{ "gt", '>' },
|
2013-02-19 13:50:27 +01:00
|
|
|
{ "backspace", Key::Backspace},
|
2013-01-30 19:03:11 +01:00
|
|
|
{ "esc", Key::Escape },
|
|
|
|
{ "up", Key::Up },
|
2013-02-18 18:58:07 +01:00
|
|
|
{ "down", Key::Down},
|
2013-02-19 13:50:27 +01:00
|
|
|
{ "left", Key::Left },
|
|
|
|
{ "right", Key::Right },
|
|
|
|
{ "pageup", Key::PageUp },
|
|
|
|
{ "pagedown", Key::PageDown },
|
|
|
|
{ "home", Key::Home },
|
|
|
|
{ "end", Key::End },
|
2019-07-07 18:38:46 +02:00
|
|
|
{ "ins", Key::Insert },
|
2014-05-25 18:41:28 +02:00
|
|
|
{ "del", Key::Delete },
|
2017-04-11 11:47:15 +02:00
|
|
|
{ "plus", '+' },
|
|
|
|
{ "minus", '-' },
|
2019-10-22 11:02:06 +02:00
|
|
|
{ "semicolon", ';' },
|
2020-06-03 14:17:32 +02:00
|
|
|
{ "percent", '%' },
|
2021-05-28 12:32:12 +02:00
|
|
|
{ "focus_in", Key::FocusIn },
|
|
|
|
{ "focus_out", Key::FocusOut },
|
2011-12-20 20:21:11 +01:00
|
|
|
};
|
|
|
|
|
2014-04-20 12:27:59 +02:00
|
|
|
KeyList parse_keys(StringView str)
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
|
|
|
KeyList result;
|
2014-11-09 14:02:01 +01:00
|
|
|
using Utf8It = utf8::iterator<const char*>;
|
2015-09-23 20:39:21 +02:00
|
|
|
for (Utf8It it{str.begin(), str}, str_end{str.end(), str}; it < str_end; ++it)
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
2014-11-09 14:02:01 +01:00
|
|
|
if (*it != '<')
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
2020-04-13 04:44:15 +02:00
|
|
|
auto convert = [](Codepoint cp) -> Codepoint {
|
|
|
|
switch (cp)
|
|
|
|
{
|
|
|
|
case '\n': return Key::Return;
|
|
|
|
case '\r': return Key::Return;
|
|
|
|
case '\b': return Key::Backspace;
|
|
|
|
case '\t': return Key::Tab;
|
2021-12-10 22:12:08 +01:00
|
|
|
case ' ': return Key::Space;
|
2020-04-13 04:44:15 +02:00
|
|
|
case '\033': return Key::Escape;
|
|
|
|
default: return cp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.emplace_back(Key::Modifiers::None, convert(*it));
|
2014-11-09 14:02:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-20 20:21:11 +01:00
|
|
|
|
2015-03-27 14:18:52 +01:00
|
|
|
Utf8It end_it = std::find(it, str_end, '>');
|
2014-11-09 14:02:01 +01:00
|
|
|
if (end_it == str_end)
|
|
|
|
{
|
2017-01-08 23:30:15 +01:00
|
|
|
result.emplace_back(Key::Modifiers::None, *it);
|
2014-11-09 14:02:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
2012-01-29 23:49:14 +01:00
|
|
|
|
2014-11-09 14:02:01 +01:00
|
|
|
Key::Modifiers modifier = Key::Modifiers::None;
|
2013-11-09 12:12:55 +01:00
|
|
|
|
2017-04-11 11:44:14 +02:00
|
|
|
StringView full_desc{it.base(), end_it.base()+1};
|
2014-11-09 14:02:01 +01:00
|
|
|
StringView desc{it.base()+1, end_it.base()};
|
2017-04-11 11:44:14 +02:00
|
|
|
for (auto dash = find(desc, '-'); dash != desc.end(); dash = find(desc, '-'))
|
2014-11-09 14:02:01 +01:00
|
|
|
{
|
2017-04-11 11:44:14 +02:00
|
|
|
if (dash != desc.begin() + 1)
|
2018-03-15 13:02:27 +01:00
|
|
|
throw key_parse_error(format("unable to parse modifier in '{}'",
|
|
|
|
full_desc));
|
2017-04-10 22:19:56 +02:00
|
|
|
|
2017-04-11 11:44:14 +02:00
|
|
|
switch(to_lower(desc[0_byte]))
|
2014-11-09 14:02:01 +01:00
|
|
|
{
|
2017-04-11 11:44:14 +02:00
|
|
|
case 'c': modifier |= Key::Modifiers::Control; break;
|
|
|
|
case 'a': modifier |= Key::Modifiers::Alt; break;
|
2018-03-15 13:02:27 +01:00
|
|
|
case 's': modifier |= Key::Modifiers::Shift; break;
|
2017-04-11 11:44:14 +02:00
|
|
|
default:
|
2018-03-15 13:02:27 +01:00
|
|
|
throw key_parse_error(format("unable to parse modifier in '{}'",
|
|
|
|
full_desc));
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
2017-04-10 22:19:56 +02:00
|
|
|
desc = StringView{dash+1, desc.end()};
|
2014-11-09 14:02:01 +01:00
|
|
|
}
|
2017-04-10 22:19:56 +02:00
|
|
|
|
2014-11-09 14:02:01 +01:00
|
|
|
auto name_it = find_if(keynamemap, [&desc](const KeyAndName& item)
|
2014-12-29 18:18:42 +01:00
|
|
|
{ return item.name == desc; });
|
2016-03-14 10:56:08 +01:00
|
|
|
if (name_it != std::end(keynamemap))
|
2014-12-29 18:18:42 +01:00
|
|
|
result.push_back(canonicalize_ifn({ modifier, name_it->key }));
|
2014-11-09 14:02:01 +01:00
|
|
|
else if (desc.char_length() == 1)
|
2018-03-15 13:02:27 +01:00
|
|
|
result.push_back(canonicalize_ifn({ modifier, desc[0_char] }));
|
2019-06-24 17:17:49 +02:00
|
|
|
else if (desc[0_byte] == 'F' and desc.length() <= 3)
|
2014-11-09 14:02:01 +01:00
|
|
|
{
|
|
|
|
int val = str_to_int(desc.substr(1_byte));
|
|
|
|
if (val >= 1 and val <= 12)
|
2017-01-08 23:30:15 +01:00
|
|
|
result.emplace_back(modifier, Key::F1 + (val - 1));
|
2014-11-09 14:02:01 +01:00
|
|
|
else
|
2018-03-15 13:02:27 +01:00
|
|
|
throw key_parse_error(format("only F1 through F12 are supported, not '{}'", desc));
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
2014-11-09 14:02:01 +01:00
|
|
|
else
|
2018-03-15 13:02:27 +01:00
|
|
|
throw key_parse_error("unable to parse " +
|
2014-11-09 14:02:01 +01:00
|
|
|
StringView{it.base(), end_it.base()+1});
|
|
|
|
|
|
|
|
it = end_it;
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-04 10:54:55 +02:00
|
|
|
StringView to_string(Key::MouseButton button)
|
2020-06-28 11:48:55 +02:00
|
|
|
{
|
|
|
|
switch (button)
|
|
|
|
{
|
|
|
|
case Key::MouseButton::Left: return "left";
|
|
|
|
case Key::MouseButton::Middle: return "middle";
|
|
|
|
case Key::MouseButton::Right: return "right";
|
|
|
|
default: kak_assert(false); throw logic_error{};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Key::MouseButton str_to_button(StringView str)
|
|
|
|
{
|
|
|
|
if (str == "left") return Key::MouseButton::Left;
|
|
|
|
if (str == "middle") return Key::MouseButton::Middle;
|
|
|
|
if (str == "right") return Key::MouseButton::Right;
|
|
|
|
throw runtime_error(format("invalid mouse button name {}", str));
|
|
|
|
}
|
|
|
|
|
2022-08-04 10:51:10 +02:00
|
|
|
String to_string(Key key)
|
2013-01-30 19:03:11 +01:00
|
|
|
{
|
2019-08-19 14:16:39 +02:00
|
|
|
const auto coord = key.coord() + DisplayCoord{1,1};
|
2023-05-11 21:21:31 +02:00
|
|
|
bool named = true;
|
2013-01-30 19:03:11 +01:00
|
|
|
String res;
|
2023-05-11 21:21:31 +02:00
|
|
|
|
|
|
|
if (key.modifiers & Key::Modifiers::MousePos)
|
|
|
|
res = format("mouse:move:{}.{}", coord.line, coord.column);
|
|
|
|
else if (key.modifiers & Key::Modifiers::MousePress)
|
|
|
|
res = format("mouse:press:{}:{}.{}", key.mouse_button(), coord.line, coord.column);
|
|
|
|
else if (key.modifiers & Key::Modifiers::MouseRelease)
|
|
|
|
res = format("mouse:release:{}:{}.{}", key.mouse_button(), coord.line, coord.column);
|
|
|
|
else if (key.modifiers & Key::Modifiers::Scroll)
|
|
|
|
res = format("scroll:{}", static_cast<int>(key.key));
|
|
|
|
else if (key.modifiers & Key::Modifiers::Resize)
|
|
|
|
res = format("resize:{}.{}", coord.line, coord.column);
|
|
|
|
else
|
2013-11-09 12:12:55 +01:00
|
|
|
{
|
2023-05-11 21:21:31 +02:00
|
|
|
auto it = find_if(keynamemap, [&key](const KeyAndName& item)
|
|
|
|
{ return item.key == key.key; });
|
|
|
|
if (it != std::end(keynamemap))
|
|
|
|
res = it->name;
|
|
|
|
else if (key.key >= Key::F1 and key.key <= Key::F12)
|
|
|
|
res = "F" + to_string((int)(key.key - Key::F1 + 1));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
named = false;
|
|
|
|
res = String{key.key};
|
|
|
|
}
|
2013-11-09 12:12:55 +01:00
|
|
|
}
|
2013-01-30 19:03:11 +01:00
|
|
|
|
2018-03-15 13:02:27 +01:00
|
|
|
if (key.modifiers & Key::Modifiers::Shift) { res = "s-" + res; named = true; }
|
|
|
|
if (key.modifiers & Key::Modifiers::Alt) { res = "a-" + res; named = true; }
|
|
|
|
if (key.modifiers & Key::Modifiers::Control) { res = "c-" + res; named = true; }
|
|
|
|
|
2013-01-30 19:03:11 +01:00
|
|
|
if (named)
|
2014-12-08 14:59:29 +01:00
|
|
|
res = StringView{'<'} + res + StringView{'>'};
|
2013-01-30 19:03:11 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:58:56 +02:00
|
|
|
UnitTest test_keys{[]()
|
|
|
|
{
|
|
|
|
KeyList keys{
|
2021-12-10 22:12:08 +01:00
|
|
|
{Key::Space},
|
2015-05-22 14:58:56 +02:00
|
|
|
{ 'c' },
|
2021-12-10 22:12:08 +01:00
|
|
|
{Key::Up},
|
2018-03-15 13:02:27 +01:00
|
|
|
alt('j'),
|
|
|
|
ctrl('r'),
|
|
|
|
shift(Key::Up),
|
2020-10-30 14:43:34 +01:00
|
|
|
ctrl('['),
|
|
|
|
ctrl('\\'),
|
|
|
|
ctrl(']'),
|
|
|
|
ctrl('_'),
|
2015-05-22 14:58:56 +02:00
|
|
|
};
|
|
|
|
String keys_as_str;
|
|
|
|
for (auto& key : keys)
|
2022-08-04 10:51:10 +02:00
|
|
|
keys_as_str += to_string(key);
|
2015-05-22 14:58:56 +02:00
|
|
|
auto parsed_keys = parse_keys(keys_as_str);
|
|
|
|
kak_assert(keys == parsed_keys);
|
2017-04-11 11:44:14 +02:00
|
|
|
kak_assert(ConstArrayView<Key>{parse_keys("a<c-a-b>c")} ==
|
2018-03-15 13:02:27 +01:00
|
|
|
ConstArrayView<Key>{'a', ctrl(alt({'b'})), 'c'});
|
|
|
|
|
|
|
|
kak_assert(parse_keys("x") == KeyList{ {'x'} });
|
|
|
|
kak_assert(parse_keys("<x>") == KeyList{ {'x'} });
|
|
|
|
kak_assert(parse_keys("<s-x>") == KeyList{ {'X'} });
|
|
|
|
kak_assert(parse_keys("<s-X>") == KeyList{ {'X'} });
|
|
|
|
kak_assert(parse_keys("<X>") == KeyList{ {'X'} });
|
|
|
|
kak_assert(parse_keys("X") == KeyList{ {'X'} });
|
|
|
|
kak_assert(parse_keys("<s-up>") == KeyList{ shift({Key::Up}) });
|
|
|
|
kak_assert(parse_keys("<s-tab>") == KeyList{ shift({Key::Tab}) });
|
2020-04-13 04:44:15 +02:00
|
|
|
kak_assert(parse_keys("\n") == KeyList{ Key::Return });
|
2018-03-15 13:02:27 +01:00
|
|
|
|
2022-08-04 10:51:10 +02:00
|
|
|
kak_assert(to_string(shift({Key::Tab})) == "<s-tab>");
|
2018-03-15 13:02:27 +01:00
|
|
|
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<-x>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<xy-z>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<x-y>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<s-/>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<s-ë>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<s-lt>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<f99>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<backtab>"));
|
|
|
|
kak_expect_throw(key_parse_error, parse_keys("<invalidkey>"));
|
2015-05-22 14:58:56 +02:00
|
|
|
}};
|
|
|
|
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|