keys.cc (9130B)
1 #include "keys.hh" 2 3 #include "exception.hh" 4 #include "ranges.hh" 5 #include "string.hh" 6 #include "unit_tests.hh" 7 #include "utf8_iterator.hh" 8 #include "format.hh" 9 #include "string_utils.hh" 10 11 namespace Kakoune 12 { 13 14 struct key_parse_error : runtime_error 15 { 16 using runtime_error::runtime_error; 17 }; 18 19 static Key canonicalize_ifn(Key key) 20 { 21 if (key.key > 0 and key.key < 27) 22 { 23 kak_assert(key.modifiers == Key::Modifiers::None); 24 key.modifiers = Key::Modifiers::Control; 25 key.key = key.key - 1 + 'a'; 26 } 27 28 if (key.modifiers & Key::Modifiers::Shift) 29 { 30 if (is_basic_alpha(key.key)) 31 { 32 // Shift + ASCII letters is just the uppercase letter. 33 key.modifiers &= ~Key::Modifiers::Shift; 34 key.key = to_upper(key.key); 35 } 36 else if (key.key < 0xD800 or key.key > 0xDFFF) 37 { 38 // Shift + any other printable character is not allowed. 39 throw key_parse_error(format("Shift modifier only works on special keys and lowercase ASCII, not '{}'", key.key)); 40 } 41 } 42 43 return key; 44 } 45 46 Optional<Codepoint> Key::codepoint() const 47 { 48 if (*this == Key::Return) 49 return '\n'; 50 if (*this == Key::Tab) 51 return '\t'; 52 if (*this == Key::Space or *this == shift(Key::Space)) 53 return ' '; 54 if (*this == Key::Escape) 55 return 0x1B; 56 if (modifiers == Modifiers::None and key > 27 and 57 (key < 0xD800 or key > 0xDFFF)) // avoid surrogates 58 return key; 59 return {}; 60 } 61 62 struct KeyAndName { const char* name; Codepoint key; }; 63 static constexpr KeyAndName keynamemap[] = { 64 { "ret", Key::Return }, 65 { "space", Key::Space }, 66 { "tab", Key::Tab }, 67 { "lt", '<' }, 68 { "gt", '>' }, 69 { "backspace", Key::Backspace}, 70 { "esc", Key::Escape }, 71 { "up", Key::Up }, 72 { "down", Key::Down}, 73 { "left", Key::Left }, 74 { "right", Key::Right }, 75 { "pageup", Key::PageUp }, 76 { "pagedown", Key::PageDown }, 77 { "home", Key::Home }, 78 { "end", Key::End }, 79 { "ins", Key::Insert }, 80 { "del", Key::Delete }, 81 { "plus", '+' }, 82 { "minus", '-' }, 83 { "semicolon", ';' }, 84 { "percent", '%' }, 85 { "quote", '\'' }, 86 { "dquote", '"' }, 87 { "focus_in", Key::FocusIn }, 88 { "focus_out", Key::FocusOut }, 89 }; 90 91 KeyList parse_keys(StringView str) 92 { 93 KeyList result; 94 using Utf8It = utf8::iterator<const char*>; 95 for (Utf8It it{str.begin(), str}, str_end{str.end(), str}; it < str_end; ++it) 96 { 97 if (*it != '<') 98 { 99 auto convert = [](Codepoint cp) -> Codepoint { 100 switch (cp) 101 { 102 case '\n': return Key::Return; 103 case '\r': return Key::Return; 104 case '\b': return Key::Backspace; 105 case '\t': return Key::Tab; 106 case ' ': return Key::Space; 107 case '\033': return Key::Escape; 108 default: return cp; 109 } 110 }; 111 result.emplace_back(Key::Modifiers::None, convert(*it)); 112 continue; 113 } 114 115 Utf8It end_it = std::find(it, str_end, '>'); 116 if (end_it == str_end) 117 { 118 result.emplace_back(Key::Modifiers::None, *it); 119 continue; 120 } 121 122 Key::Modifiers modifier = Key::Modifiers::None; 123 124 StringView full_desc{it.base(), end_it.base()+1}; 125 StringView desc{it.base()+1, end_it.base()}; 126 for (auto dash = find(desc, '-'); dash != desc.end(); dash = find(desc, '-')) 127 { 128 if (dash != desc.begin() + 1) 129 throw key_parse_error(format("unable to parse modifier in '{}'", 130 full_desc)); 131 132 switch(to_lower(desc[0_byte])) 133 { 134 case 'c': modifier |= Key::Modifiers::Control; break; 135 case 'a': modifier |= Key::Modifiers::Alt; break; 136 case 's': modifier |= Key::Modifiers::Shift; break; 137 default: 138 throw key_parse_error(format("unable to parse modifier in '{}'", 139 full_desc)); 140 } 141 desc = StringView{dash+1, desc.end()}; 142 } 143 144 auto name_it = find_if(keynamemap, [&desc](const KeyAndName& item) 145 { return item.name == desc; }); 146 if (name_it != std::end(keynamemap)) 147 result.push_back(canonicalize_ifn({ modifier, name_it->key })); 148 else if (desc.char_length() == 1) 149 result.push_back(canonicalize_ifn({ modifier, desc[0_char] })); 150 else if (desc[0_byte] == 'F' and desc.length() <= 3) 151 { 152 int val = str_to_int(desc.substr(1_byte)); 153 if (val >= 1 and val <= 12) 154 result.emplace_back(modifier, Key::F1 + (val - 1)); 155 else 156 throw key_parse_error(format("only F1 through F12 are supported, not '{}'", desc)); 157 } 158 else 159 throw key_parse_error("unable to parse " + 160 StringView{it.base(), end_it.base()+1}); 161 162 it = end_it; 163 } 164 return result; 165 } 166 167 StringView to_string(Key::MouseButton button) 168 { 169 switch (button) 170 { 171 case Key::MouseButton::Left: return "left"; 172 case Key::MouseButton::Middle: return "middle"; 173 case Key::MouseButton::Right: return "right"; 174 default: kak_assert(false); throw logic_error{}; 175 } 176 } 177 178 Key::MouseButton str_to_button(StringView str) 179 { 180 if (str == "left") return Key::MouseButton::Left; 181 if (str == "middle") return Key::MouseButton::Middle; 182 if (str == "right") return Key::MouseButton::Right; 183 throw runtime_error(format("invalid mouse button name {}", str)); 184 } 185 186 String to_string(Key key) 187 { 188 const auto coord = key.coord() + DisplayCoord{1,1}; 189 bool named = true; 190 String res; 191 192 if (key.modifiers & Key::Modifiers::MousePos) 193 res = format("mouse:move:{}.{}", coord.line, coord.column); 194 else if (key.modifiers & Key::Modifiers::MousePress) 195 res = format("mouse:press:{}:{}.{}", key.mouse_button(), coord.line, coord.column); 196 else if (key.modifiers & Key::Modifiers::MouseRelease) 197 res = format("mouse:release:{}:{}.{}", key.mouse_button(), coord.line, coord.column); 198 else if (key.modifiers & Key::Modifiers::Scroll) 199 res = format("scroll:{}:{}.{}", key.scroll_amount(), coord.line, coord.column); 200 else if (key.modifiers & Key::Modifiers::Resize) 201 res = format("resize:{}.{}", coord.line, coord.column); 202 else 203 { 204 auto it = find_if(keynamemap, [&key](const KeyAndName& item) 205 { return item.key == key.key; }); 206 if (it != std::end(keynamemap)) 207 res = it->name; 208 else if (key.key >= Key::F1 and key.key <= Key::F12) 209 res = "F" + to_string((int)(key.key - Key::F1 + 1)); 210 else 211 { 212 named = false; 213 res = String{key.key}; 214 } 215 } 216 217 if (key.modifiers & Key::Modifiers::Shift) { res = "s-" + res; named = true; } 218 if (key.modifiers & Key::Modifiers::Alt) { res = "a-" + res; named = true; } 219 if (key.modifiers & Key::Modifiers::Control) { res = "c-" + res; named = true; } 220 221 if (named) 222 res = StringView{'<'} + res + StringView{'>'}; 223 return res; 224 } 225 226 UnitTest test_keys{[]() 227 { 228 KeyList keys{ 229 {Key::Space}, 230 { 'c' }, 231 {Key::Up}, 232 alt('j'), 233 ctrl('r'), 234 shift(Key::Up), 235 ctrl('['), 236 ctrl('\\'), 237 ctrl(']'), 238 ctrl('_'), 239 }; 240 String keys_as_str; 241 for (auto& key : keys) 242 keys_as_str += to_string(key); 243 auto parsed_keys = parse_keys(keys_as_str); 244 kak_assert(keys == parsed_keys); 245 kak_assert(ConstArrayView<Key>{parse_keys("a<c-a-b>c")} == 246 ConstArrayView<Key>{'a', ctrl(alt({'b'})), 'c'}); 247 248 kak_assert(parse_keys("x") == KeyList{ {'x'} }); 249 kak_assert(parse_keys("<x>") == KeyList{ {'x'} }); 250 kak_assert(parse_keys("<s-x>") == KeyList{ {'X'} }); 251 kak_assert(parse_keys("<s-X>") == KeyList{ {'X'} }); 252 kak_assert(parse_keys("<X>") == KeyList{ {'X'} }); 253 kak_assert(parse_keys("X") == KeyList{ {'X'} }); 254 kak_assert(parse_keys("<s-up>") == KeyList{ shift({Key::Up}) }); 255 kak_assert(parse_keys("<s-tab>") == KeyList{ shift({Key::Tab}) }); 256 kak_assert(parse_keys("\n") == KeyList{ Key::Return }); 257 258 kak_assert(to_string(shift({Key::Tab})) == "<s-tab>"); 259 260 kak_expect_throw(key_parse_error, parse_keys("<-x>")); 261 kak_expect_throw(key_parse_error, parse_keys("<xy-z>")); 262 kak_expect_throw(key_parse_error, parse_keys("<x-y>")); 263 kak_expect_throw(key_parse_error, parse_keys("<s-/>")); 264 kak_expect_throw(key_parse_error, parse_keys("<s-ë>")); 265 kak_expect_throw(key_parse_error, parse_keys("<s-lt>")); 266 kak_expect_throw(key_parse_error, parse_keys("<f99>")); 267 kak_expect_throw(key_parse_error, parse_keys("<backtab>")); 268 kak_expect_throw(key_parse_error, parse_keys("<invalidkey>")); 269 }}; 270 271 }