2011-12-20 23:21:11 +04:00
|
|
|
#include "keys.hh"
|
2013-01-30 22:03:11 +04:00
|
|
|
#include "utils.hh"
|
2011-12-20 23:21:11 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-09-03 21:21:11 +04:00
|
|
|
Key canonicalize_ifn(Key key)
|
|
|
|
{
|
|
|
|
if (key.key > 0 and key.key < 27)
|
|
|
|
{
|
|
|
|
assert(key.modifiers == Key::Modifiers::None);
|
|
|
|
key.modifiers = Key::Modifiers::Control;
|
|
|
|
key.key = key.key - 1 + 'a';
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2013-01-30 22:03:11 +04:00
|
|
|
using KeyAndName = std::pair<String, Codepoint>;
|
|
|
|
static std::vector<KeyAndName> keynamemap = {
|
2012-09-03 21:21:11 +04:00
|
|
|
{ "ret", '\r' },
|
2012-01-30 02:49:14 +04:00
|
|
|
{ "space", ' ' },
|
2013-01-30 22:03:11 +04:00
|
|
|
{ "esc", Key::Escape },
|
|
|
|
{ "left", Key::Left },
|
|
|
|
{ "right", Key::Right },
|
|
|
|
{ "up", Key::Up },
|
|
|
|
{ "down", Key::Down}
|
2011-12-20 23:21:11 +04:00
|
|
|
};
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
KeyList parse_keys(const String& str)
|
2011-12-20 23:21:11 +04:00
|
|
|
{
|
|
|
|
KeyList result;
|
2012-10-11 02:41:48 +04:00
|
|
|
for (ByteCount pos = 0; pos < str.length(); ++pos)
|
2011-12-20 23:21:11 +04:00
|
|
|
{
|
|
|
|
if (str[pos] == '<')
|
|
|
|
{
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount end_pos = pos;
|
2011-12-20 23:21:11 +04:00
|
|
|
while (end_pos < str.length() and str[end_pos] != '>')
|
|
|
|
++end_pos;
|
|
|
|
|
|
|
|
if (end_pos < str.length())
|
|
|
|
{
|
2012-01-30 02:49:14 +04:00
|
|
|
Key::Modifiers modifier = Key::Modifiers::None;
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
String keyname = str.substr(pos+1, end_pos - pos - 1);
|
2012-01-30 02:49:14 +04:00
|
|
|
if (keyname.length() > 2)
|
|
|
|
{
|
|
|
|
if (tolower(keyname[0]) == 'c' and keyname[1] == '-')
|
|
|
|
{
|
|
|
|
modifier = Key::Modifiers::Control;
|
2012-10-11 02:41:48 +04:00
|
|
|
keyname = keyname.substr(2_byte);
|
2012-01-30 02:49:14 +04:00
|
|
|
}
|
|
|
|
if (tolower(keyname[0]) == 'a' and keyname[1] == '-')
|
|
|
|
{
|
2012-08-21 22:03:18 +04:00
|
|
|
modifier = Key::Modifiers::Alt;
|
2012-10-11 02:41:48 +04:00
|
|
|
keyname = keyname.substr(2_byte);
|
2012-01-30 02:49:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keyname.length() == 1)
|
|
|
|
{
|
2012-10-09 21:15:05 +04:00
|
|
|
result.push_back(Key{ modifier, Codepoint(keyname[0]) });
|
2012-01-30 02:49:14 +04:00
|
|
|
pos = end_pos;
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-30 22:03:11 +04:00
|
|
|
auto it = find_if(keynamemap, [&keyname](const KeyAndName& item)
|
|
|
|
{ return item.first == keyname; });
|
2011-12-20 23:21:11 +04:00
|
|
|
if (it != keynamemap.end())
|
|
|
|
{
|
2012-09-03 21:21:11 +04:00
|
|
|
Key key = canonicalize_ifn(Key{ modifier, it->second });
|
|
|
|
result.push_back(key);
|
2011-12-20 23:21:11 +04:00
|
|
|
pos = end_pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-09 21:15:05 +04:00
|
|
|
result.push_back({Key::Modifiers::None, Codepoint(str[pos])});
|
2011-12-20 23:21:11 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-30 22:03:11 +04:00
|
|
|
String key_to_str(const Key& key)
|
|
|
|
{
|
|
|
|
bool named = false;
|
|
|
|
String res;
|
|
|
|
auto it = find_if(keynamemap, [&key](const KeyAndName& item)
|
|
|
|
{ return item.second == key.key; });
|
|
|
|
if (it != keynamemap.end())
|
|
|
|
{
|
|
|
|
named = true;
|
|
|
|
res = it->first;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = codepoint_to_str(key.key);
|
|
|
|
|
|
|
|
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)
|
|
|
|
res = '<' + res + '>';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-12-20 23:21:11 +04:00
|
|
|
}
|