2011-12-20 20:21:11 +01:00
|
|
|
#include "keys.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2013-01-30 19:03:11 +01:00
|
|
|
#include "utils.hh"
|
2014-06-04 12:51:20 +02:00
|
|
|
#include "utf8_iterator.hh"
|
2011-12-20 20:21:11 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-09-03 19:21:11 +02:00
|
|
|
Key canonicalize_ifn(Key key)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:52:11 +01:00
|
|
|
using KeyAndName = std::pair<const char*, Codepoint>;
|
2014-04-03 02:19:49 +02:00
|
|
|
static const 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-06-04 12:51:20 +02:00
|
|
|
using PassPolicy = utf8::InvalidBytePolicy::Pass;
|
|
|
|
using Utf8It = utf8::utf8_iterator<const char*, PassPolicy>;
|
|
|
|
for (Utf8It it = str.begin(), str_end = str.end(); it < str_end; ++it)
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
2014-06-04 12:51:20 +02:00
|
|
|
if (*it == '<')
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
2014-06-04 12:51:20 +02:00
|
|
|
Utf8It end_it = it;
|
|
|
|
while (end_it < str_end and *end_it != '>')
|
|
|
|
++end_it;
|
2011-12-20 20:21:11 +01:00
|
|
|
|
2014-06-04 12:51:20 +02:00
|
|
|
if (end_it < str_end)
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
2012-01-29 23:49:14 +01:00
|
|
|
Key::Modifiers modifier = Key::Modifiers::None;
|
|
|
|
|
2014-06-04 12:51:20 +02:00
|
|
|
StringView keyname{it.base()+1, end_it.base()};
|
2012-01-29 23:49:14 +01:00
|
|
|
if (keyname.length() > 2)
|
|
|
|
{
|
|
|
|
if (tolower(keyname[0]) == 'c' and keyname[1] == '-')
|
|
|
|
{
|
|
|
|
modifier = Key::Modifiers::Control;
|
2012-10-11 00:41:48 +02:00
|
|
|
keyname = keyname.substr(2_byte);
|
2012-01-29 23:49:14 +01:00
|
|
|
}
|
|
|
|
if (tolower(keyname[0]) == 'a' and keyname[1] == '-')
|
|
|
|
{
|
2012-08-21 20:03:18 +02:00
|
|
|
modifier = Key::Modifiers::Alt;
|
2012-10-11 00:41:48 +02:00
|
|
|
keyname = keyname.substr(2_byte);
|
2012-01-29 23:49:14 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-04 12:51:20 +02:00
|
|
|
if (keyname.char_length() == 1)
|
2012-01-29 23:49:14 +01:00
|
|
|
{
|
2014-06-04 12:51:20 +02:00
|
|
|
result.push_back(Key{ modifier, utf8::codepoint<PassPolicy>(keyname.begin()) });
|
|
|
|
it = end_it;
|
2012-01-29 23:49:14 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-04 12:51:20 +02:00
|
|
|
auto name_it = find_if(keynamemap, [&keyname](const KeyAndName& item)
|
|
|
|
{ return item.first == keyname; });
|
|
|
|
if (name_it != end(keynamemap))
|
2011-12-20 20:21:11 +01:00
|
|
|
{
|
2014-06-04 12:51:20 +02:00
|
|
|
Key key = canonicalize_ifn(Key{ modifier, name_it->second });
|
2012-09-03 19:21:11 +02:00
|
|
|
result.push_back(key);
|
2014-06-04 12:51:20 +02:00
|
|
|
it = end_it;
|
2011-12-20 20:21:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-09 12:12:55 +01:00
|
|
|
if ((keyname[0] == 'f' or keyname[0] == 'F') and
|
|
|
|
keyname.length() <= 3)
|
|
|
|
{
|
|
|
|
|
|
|
|
int val = 0;
|
|
|
|
for (auto i = 1_byte; i < keyname.length(); ++i)
|
|
|
|
{
|
|
|
|
char c = keyname[i];
|
|
|
|
if (c >= '0' and c <= '9')
|
|
|
|
val = val*10 + c - '0';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (val >= 1 and val <= 12)
|
|
|
|
{
|
|
|
|
result.push_back(Key{ modifier, Key::F1 + (val - 1) });
|
2014-06-04 12:51:20 +02:00
|
|
|
it = end_it;
|
2013-11-09 12:12:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 20:21:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-04 12:51:20 +02:00
|
|
|
result.push_back({Key::Modifiers::None, *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
|
|
|
{
|
|
|
|
bool named = false;
|
|
|
|
String res;
|
|
|
|
auto it = find_if(keynamemap, [&key](const KeyAndName& item)
|
|
|
|
{ return item.second == key.key; });
|
2014-03-20 20:52:11 +01:00
|
|
|
if (it != end(keynamemap))
|
2013-01-30 19:03:11 +01:00
|
|
|
{
|
|
|
|
named = true;
|
|
|
|
res = it->first;
|
|
|
|
}
|
2013-11-09 12:12:55 +01:00
|
|
|
else if (key.key >= Key::F1 and key.key < Key::F12)
|
|
|
|
{
|
|
|
|
named = true;
|
|
|
|
res = "F" + to_string((int)(Codepoint)key.key - (int)(Codepoint)Key::F1 + 1);
|
|
|
|
}
|
2013-01-30 19:03:11 +01:00
|
|
|
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 20:21:11 +01:00
|
|
|
}
|