Add Key struct

This commit is contained in:
Maxime Coste 2011-12-20 19:21:11 +00:00
parent 91606850fd
commit 42a24895de
2 changed files with 73 additions and 0 deletions

40
src/keys.cc Normal file
View File

@ -0,0 +1,40 @@
#include "keys.hh"
#include <unordered_map>
namespace Kakoune
{
static std::unordered_map<std::string, Key> keynamemap = {
{ "ret", { Key::Modifiers::None, '\n' } }
};
KeyList parse_keys(const std::string& str)
{
KeyList result;
for (size_t pos = 0; pos < str.length(); ++pos)
{
if (str[pos] == '<')
{
size_t end_pos = pos;
while (end_pos < str.length() and str[end_pos] != '>')
++end_pos;
if (end_pos < str.length())
{
std::string keyname = str.substr(pos+1, end_pos - pos - 1);
auto it = keynamemap.find(keyname);
if (it != keynamemap.end())
{
result.push_back(it->second);
pos = end_pos;
continue;
}
}
}
result.push_back({Key::Modifiers::None, str[pos]});
}
return result;
}
}

33
src/keys.hh Normal file
View File

@ -0,0 +1,33 @@
#ifndef keys_hh_INCLUDED
#define keys_hh_INCLUDED
#include <vector>
#include <string>
namespace Kakoune
{
struct Key
{
enum class Modifiers
{
None = 0,
Control = 1,
Alt = 2,
ControlAlt = 3
};
Modifiers modifiers;
char key;
Key(Modifiers modifiers, char key)
: modifiers(modifiers), key(key) {}
};
typedef std::vector<Key> KeyList;
KeyList parse_keys(const std::string& str);
}
#endif // keys_hh_INCLUDED