1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-21 18:41:29 +03:00
kakoune/src/keys.cc

130 lines
3.6 KiB
C++
Raw Normal View History

2011-12-20 23:21:11 +04:00
#include "keys.hh"
2013-04-09 22:05:40 +04:00
2013-01-30 22:03:11 +04:00
#include "utils.hh"
#include "utf8_iterator.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)
{
kak_assert(key.modifiers == Key::Modifiers::None);
2012-09-03 21:21:11 +04:00
key.modifiers = Key::Modifiers::Control;
key.key = key.key - 1 + 'a';
}
return key;
}
2014-03-20 23:52:11 +04:00
using KeyAndName = std::pair<const char*, Codepoint>;
static const KeyAndName keynamemap[] = {
2012-09-03 21:21:11 +04:00
{ "ret", '\r' },
{ "space", ' ' },
{ "tab", '\t' },
2013-04-02 20:41:45 +04:00
{ "lt", '<' },
{ "gt", '>' },
{ "backspace", Key::Backspace},
2013-01-30 22:03:11 +04:00
{ "esc", Key::Escape },
{ "up", Key::Up },
2013-02-18 21:58:07 +04:00
{ "down", Key::Down},
{ "left", Key::Left },
{ "right", Key::Right },
{ "pageup", Key::PageUp },
{ "pagedown", Key::PageDown },
{ "home", Key::Home },
{ "end", Key::End },
{ "backtab", Key::BackTab },
{ "del", Key::Delete },
2011-12-20 23:21:11 +04:00
};
2014-04-20 14:27:59 +04:00
KeyList parse_keys(StringView str)
2011-12-20 23:21:11 +04:00
{
KeyList result;
2014-11-09 16:02:01 +03:00
using Utf8It = utf8::iterator<const char*>;
for (Utf8It it = str.begin(), str_end = str.end(); it < str_end; ++it)
2011-12-20 23:21:11 +04:00
{
2014-11-09 16:02:01 +03:00
if (*it != '<')
2011-12-20 23:21:11 +04:00
{
2014-11-09 16:02:01 +03:00
result.push_back({Key::Modifiers::None, *it});
continue;
}
2011-12-20 23:21:11 +04:00
2014-11-09 16:02:01 +03:00
Utf8It end_it = it;
skip_while(end_it, str_end, [](char c) { return c != '>'; });
if (end_it == str_end)
{
result.push_back({Key::Modifiers::None, *it});
continue;
}
2014-11-09 16:02:01 +03:00
Key::Modifiers modifier = Key::Modifiers::None;
2013-11-09 15:12:55 +04:00
2014-11-09 16:02:01 +03:00
StringView desc{it.base()+1, end_it.base()};
if (desc.length() > 2 and desc[1] == '-')
{
switch(tolower(desc[0]))
{
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 23:21:11 +04:00
}
2014-11-09 16:02:01 +03:00
desc = desc.substr(2_byte);
}
auto name_it = find_if(keynamemap, [&desc](const KeyAndName& item)
{ return item.first == desc; });
if (name_it != end(keynamemap))
result.push_back(canonicalize_ifn({ modifier, name_it->second }));
else if (desc.char_length() == 1)
result.push_back(Key{ modifier, desc[0_char] });
else if (tolower(desc[0]) == 'f' and desc.length() <= 3)
{
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 23:21:11 +04:00
}
2014-11-09 16:02:01 +03:00
else
throw runtime_error("Failed to parse " +
StringView{it.base(), end_it.base()+1});
it = end_it;
2011-12-20 23:21:11 +04:00
}
return result;
}
2013-07-26 03:44:25 +04:00
String key_to_str(Key key)
2013-01-30 22:03:11 +04:00
{
bool named = false;
String res;
auto it = find_if(keynamemap, [&key](const KeyAndName& item)
{ return item.second == key.key; });
2014-03-20 23:52:11 +04:00
if (it != end(keynamemap))
2013-01-30 22:03:11 +04:00
{
named = true;
res = it->first;
}
2013-11-09 15:12:55 +04:00
else if (key.key >= Key::F1 and key.key < Key::F12)
{
named = true;
2014-11-09 14:42:06 +03:00
res = "F" + to_string((int)(key.key - Key::F1 + 1));
2013-11-09 15:12:55 +04:00
}
2013-01-30 22:03:11 +04: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)
2014-12-08 16:59:29 +03:00
res = StringView{'<'} + res + StringView{'>'};
2013-01-30 22:03:11 +04:00
return res;
}
2011-12-20 23:21:11 +04:00
}