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 "containers.hh"
|
|
|
|
#include "exception.hh"
|
|
|
|
#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"
|
2011-12-20 20:21:11 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:19:14 +02:00
|
|
|
Optional<Codepoint> Key::codepoint() const
|
|
|
|
{
|
|
|
|
if (*this == ctrl('m'))
|
|
|
|
return '\n';
|
|
|
|
if (*this == ctrl('i'))
|
|
|
|
return '\t';
|
|
|
|
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[] = {
|
2012-09-03 19:21:11 +02:00
|
|
|
{ "ret", '\r' },
|
2012-01-29 23:49:14 +01:00
|
|
|
{ "space", ' ' },
|
2013-02-19 13:50:27 +01:00
|
|
|
{ "tab", '\t' },
|
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 },
|
|
|
|
{ "backtab", Key::BackTab },
|
2014-05-25 18:41:28 +02:00
|
|
|
{ "del", Key::Delete },
|
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
|
|
|
{
|
2014-11-09 14:02:01 +01:00
|
|
|
result.push_back({Key::Modifiers::None, *it});
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
result.push_back({Key::Modifiers::None, *it});
|
|
|
|
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
|
|
|
|
2014-11-09 14:02:01 +01:00
|
|
|
StringView desc{it.base()+1, end_it.base()};
|
2015-03-10 20:33:46 +01:00
|
|
|
if (desc.length() > 2 and desc[1_byte] == '-')
|
2014-11-09 14:02:01 +01:00
|
|
|
{
|
2015-11-11 01:21:20 +01:00
|
|
|
switch(to_lower(desc[0_byte]))
|
2014-11-09 14:02:01 +01:00
|
|
|
{
|
|
|
|
case 'c': modifier = Key::Modifiers::Control; break;
|
|
|
|
case 'a': modifier = Key::Modifiers::Alt; break;
|
|
|
|
default:
|
|
|
|
throw runtime_error("unable to parse modifier in " +
|
|
|
|
StringView{it.base(), end_it.base()+1});
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
2014-11-09 14:02:01 +01:00
|
|
|
desc = desc.substr(2_byte);
|
|
|
|
}
|
|
|
|
auto name_it = find_if(keynamemap, [&desc](const KeyAndName& item)
|
2014-12-29 18:18:42 +01:00
|
|
|
{ return item.name == desc; });
|
2014-11-09 14:02:01 +01:00
|
|
|
if (name_it != 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)
|
|
|
|
result.push_back(Key{ modifier, desc[0_char] });
|
2015-11-11 01:21:20 +01:00
|
|
|
else if (to_lower(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)
|
|
|
|
result.push_back(Key{ modifier, Key::F1 + (val - 1) });
|
|
|
|
else
|
|
|
|
throw runtime_error("Only F1 through F12 are supported");
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
2014-11-09 14:02:01 +01:00
|
|
|
else
|
|
|
|
throw runtime_error("Failed to parse " +
|
|
|
|
StringView{it.base(), end_it.base()+1});
|
|
|
|
|
|
|
|
it = end_it;
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-07-26 01:44:25 +02:00
|
|
|
String key_to_str(Key key)
|
2013-01-30 19:03:11 +01:00
|
|
|
{
|
2015-03-28 19:18:28 +01:00
|
|
|
if (key.modifiers & Key::Modifiers::MouseEvent)
|
2016-03-23 14:39:52 +01:00
|
|
|
{
|
|
|
|
const auto coord = key.coord() + CharCoord{1,1};
|
|
|
|
switch (key.modifiers)
|
|
|
|
{
|
|
|
|
case Key::Modifiers::MousePos:
|
|
|
|
return format("<mouse:move:{}.{}>", coord.line, coord.column);
|
|
|
|
case Key::Modifiers::MousePress:
|
|
|
|
return format("<mouse:press:{}.{}>", coord.line, coord.column);
|
|
|
|
case Key::Modifiers::MouseRelease:
|
|
|
|
return format("<mouse:release:{}.{}>", coord.line, coord.column);
|
|
|
|
case Key::Modifiers::MouseWheelDown:
|
|
|
|
return "<mouse:wheel_down>";
|
|
|
|
case Key::Modifiers::MouseWheelUp:
|
|
|
|
return "<mouse:wheel_up>";
|
|
|
|
default: kak_assert(false);
|
|
|
|
}
|
|
|
|
}
|
2016-07-01 00:49:06 +02:00
|
|
|
else if (key.modifiers == Key::Modifiers::Resize)
|
|
|
|
{
|
|
|
|
auto size = key.coord() + CharCoord{1,1};
|
|
|
|
return format("<resize:{}.{}>", size.line, size.column);
|
|
|
|
}
|
2015-03-28 19:18:28 +01:00
|
|
|
|
2013-01-30 19:03:11 +01:00
|
|
|
bool named = false;
|
|
|
|
String res;
|
|
|
|
auto it = find_if(keynamemap, [&key](const KeyAndName& item)
|
2014-12-29 18:18:42 +01:00
|
|
|
{ return item.key == key.key; });
|
2014-03-20 20:52:11 +01:00
|
|
|
if (it != end(keynamemap))
|
2013-01-30 19:03:11 +01:00
|
|
|
{
|
|
|
|
named = true;
|
2014-12-29 18:18:42 +01:00
|
|
|
res = it->name;
|
2013-01-30 19:03:11 +01:00
|
|
|
}
|
2013-11-09 12:12:55 +01:00
|
|
|
else if (key.key >= Key::F1 and key.key < Key::F12)
|
|
|
|
{
|
|
|
|
named = true;
|
2014-11-09 12:42:06 +01:00
|
|
|
res = "F" + to_string((int)(key.key - Key::F1 + 1));
|
2013-11-09 12:12:55 +01:00
|
|
|
}
|
2013-01-30 19:03:11 +01:00
|
|
|
else
|
2016-02-05 10:13:07 +01:00
|
|
|
res = String{key.key};
|
2013-01-30 19:03:11 +01:00
|
|
|
|
|
|
|
switch (key.modifiers)
|
|
|
|
{
|
|
|
|
case Key::Modifiers::Control: res = "c-" + res; named = true; break;
|
|
|
|
case Key::Modifiers::Alt: res = "a-" + res; named = true; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
{ ' ' },
|
|
|
|
{ 'c' },
|
|
|
|
{ Key::Modifiers::Alt, 'j' },
|
|
|
|
{ Key::Modifiers::Control, 'r' }
|
|
|
|
};
|
|
|
|
String keys_as_str;
|
|
|
|
for (auto& key : keys)
|
|
|
|
keys_as_str += key_to_str(key);
|
|
|
|
auto parsed_keys = parse_keys(keys_as_str);
|
|
|
|
kak_assert(keys == parsed_keys);
|
|
|
|
}};
|
|
|
|
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|