summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2013-01-30 19:03:11 +0100
committerMaxime Coste <frrrwww@gmail.com>2013-01-30 19:03:11 +0100
commitde0f765498260baa987f8b6efefde83155891d86 (patch)
tree2521de59e5b68f5208b8c64e393b756968c996c1 /src
parent045aa0c7eccca5a51e6354fb29a8f8dcc1a9f4d7 (diff)
Keys: add key_to_str function
Diffstat (limited to 'src')
-rw-r--r--src/keys.cc40
-rw-r--r--src/keys.hh1
2 files changed, 36 insertions, 5 deletions
diff --git a/src/keys.cc b/src/keys.cc
index 6b2479ff..d9c304d6 100644
--- a/src/keys.cc
+++ b/src/keys.cc
@@ -1,6 +1,5 @@
#include "keys.hh"
-
-#include <unordered_map>
+#include "utils.hh"
namespace Kakoune
{
@@ -16,10 +15,15 @@ Key canonicalize_ifn(Key key)
return key;
}
-static std::unordered_map<String, Codepoint> keynamemap = {
+using KeyAndName = std::pair<String, Codepoint>;
+static std::vector<KeyAndName> keynamemap = {
{ "ret", '\r' },
{ "space", ' ' },
- { "esc", Key::Escape }
+ { "esc", Key::Escape },
+ { "left", Key::Left },
+ { "right", Key::Right },
+ { "up", Key::Up },
+ { "down", Key::Down}
};
KeyList parse_keys(const String& str)
@@ -57,7 +61,8 @@ KeyList parse_keys(const String& str)
pos = end_pos;
continue;
}
- auto it = keynamemap.find(keyname);
+ auto it = find_if(keynamemap, [&keyname](const KeyAndName& item)
+ { return item.first == keyname; });
if (it != keynamemap.end())
{
Key key = canonicalize_ifn(Key{ modifier, it->second });
@@ -72,4 +77,29 @@ KeyList parse_keys(const String& str)
return result;
}
+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;
+}
+
}
diff --git a/src/keys.hh b/src/keys.hh
index 19a83575..799aadee 100644
--- a/src/keys.hh
+++ b/src/keys.hh
@@ -51,6 +51,7 @@ struct Key
typedef std::vector<Key> KeyList;
KeyList parse_keys(const String& str);
+String key_to_str(const Key& key);
}