1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-22 02:51:32 +03:00
kakoune/src/keys.cc

76 lines
2.1 KiB
C++
Raw Normal View History

2011-12-20 23:21:11 +04:00
#include "keys.hh"
#include <unordered_map>
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;
}
static std::unordered_map<String, Codepoint> keynamemap = {
2012-09-03 21:21:11 +04:00
{ "ret", '\r' },
{ "space", ' ' },
2012-09-17 15:46:34 +04:00
{ "esc", Key::Escape }
2011-12-20 23:21:11 +04:00
};
KeyList parse_keys(const String& str)
2011-12-20 23:21:11 +04:00
{
KeyList result;
for (CharCount pos = 0; pos < str.length(); ++pos)
2011-12-20 23:21:11 +04:00
{
if (str[pos] == '<')
{
CharCount 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())
{
Key::Modifiers modifier = Key::Modifiers::None;
String keyname = str.substr(pos+1, end_pos - pos - 1);
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 22:03:18 +04:00
modifier = Key::Modifiers::Alt;
keyname = keyname.substr(2);
}
}
if (keyname.length() == 1)
{
result.push_back(Key{ modifier, Codepoint(keyname[0]) });
pos = end_pos;
continue;
}
2011-12-20 23:21:11 +04:00
auto it = keynamemap.find(keyname);
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;
}
}
}
result.push_back({Key::Modifiers::None, Codepoint(str[pos])});
2011-12-20 23:21:11 +04:00
}
return result;
}
}