2011-12-20 20:21:11 +01:00
|
|
|
#include "keys.hh"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-09-03 19:21:11 +02: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;
|
|
|
|
}
|
|
|
|
|
2012-10-08 19:33:53 +02:00
|
|
|
static std::unordered_map<String, utf8::Codepoint> keynamemap = {
|
2012-09-03 19:21:11 +02:00
|
|
|
{ "ret", '\r' },
|
2012-01-29 23:49:14 +01:00
|
|
|
{ "space", ' ' },
|
2012-09-17 13:46:34 +02:00
|
|
|
{ "esc", Key::Escape }
|
2011-12-20 20:21:11 +01:00
|
|
|
};
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
KeyList parse_keys(const String& str)
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
|
|
|
KeyList result;
|
2012-08-23 23:56:35 +02:00
|
|
|
for (CharCount pos = 0; pos < str.length(); ++pos)
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
|
|
|
if (str[pos] == '<')
|
|
|
|
{
|
2012-08-23 23:56:35 +02:00
|
|
|
CharCount end_pos = pos;
|
2011-12-20 20:21:11 +01:00
|
|
|
while (end_pos < str.length() and str[end_pos] != '>')
|
|
|
|
++end_pos;
|
|
|
|
|
|
|
|
if (end_pos < str.length())
|
|
|
|
{
|
2012-01-29 23:49:14 +01:00
|
|
|
Key::Modifiers modifier = Key::Modifiers::None;
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
String keyname = str.substr(pos+1, end_pos - pos - 1);
|
2012-01-29 23:49:14 +01:00
|
|
|
if (keyname.length() > 2)
|
|
|
|
{
|
|
|
|
if (tolower(keyname[0]) == 'c' and keyname[1] == '-')
|
|
|
|
{
|
|
|
|
modifier = Key::Modifiers::Control;
|
|
|
|
keyname = keyname.substr(2);
|
|
|
|
}
|
|
|
|
if (tolower(keyname[0]) == 'a' and keyname[1] == '-')
|
|
|
|
{
|
2012-08-21 20:03:18 +02:00
|
|
|
modifier = Key::Modifiers::Alt;
|
2012-01-29 23:49:14 +01:00
|
|
|
keyname = keyname.substr(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keyname.length() == 1)
|
|
|
|
{
|
2012-10-08 19:33:53 +02:00
|
|
|
result.push_back(Key{ modifier, utf8::Codepoint(keyname[0]) });
|
2012-01-29 23:49:14 +01:00
|
|
|
pos = end_pos;
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-20 20:21:11 +01:00
|
|
|
auto it = keynamemap.find(keyname);
|
|
|
|
if (it != keynamemap.end())
|
|
|
|
{
|
2012-09-03 19:21:11 +02:00
|
|
|
Key key = canonicalize_ifn(Key{ modifier, it->second });
|
|
|
|
result.push_back(key);
|
2011-12-20 20:21:11 +01:00
|
|
|
pos = end_pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-08 19:33:53 +02:00
|
|
|
result.push_back({Key::Modifiers::None, utf8::Codepoint(str[pos])});
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|