xaizek / libvle (License: GPLv3+) (since 2019-04-21)
Library for building Vim-like applications.
Commit 7256c7fddee7c117f0a84a5a29438d13e155d67c

Support adding shortcuts with multiple LHSes
Author: xaizek
Author date (UTC): 2019-05-06 14:34
Committer name: xaizek
Committer date (UTC): 2019-05-06 14:34
Parent(s): 32bc9c91577dc939193d80455d5413838783e262
Signing key: 99DC5E4DB05F6BE2
Tree: e7cb0ad124422f34b72713b9bd706c20f46a1274
File Lines added Lines deleted
Mode.cpp 23 9
Mode.hpp 10 3
Modes.cpp 6 2
File Mode.cpp changed (mode: 100644) (index 4711dc9..be9c7f8)
... ... using namespace vle;
26 26
27 27 Shortcut::Shortcut(std::wstring shortcut, std::function<void(void)> handler, Shortcut::Shortcut(std::wstring shortcut, std::function<void(void)> handler,
28 28 std::string descr) std::string descr)
29 : shortcut(std::move(shortcut)), descr(std::move(descr))
29 : Shortcut(std::vector<std::wstring>{ std::move(shortcut) },
30 std::move(handler), std::move(descr))
31 { }
32
33 Shortcut::Shortcut(std::wstring shortcut, std::function<void(int)> handler,
34 std::string descr)
35 : Shortcut(std::vector<std::wstring>{ std::move(shortcut) },
36 std::move(handler), std::move(descr))
37 { }
38
39 Shortcut::Shortcut(std::vector<std::wstring> shortcuts,
40 std::function<void(void)> handler, std::string descr)
41 : shortcuts(std::move(shortcuts)), descr(std::move(descr))
30 42 { {
31 43 this->handler = [handler](int /*count*/) { this->handler = [handler](int /*count*/) {
32 44 handler(); handler();
33 45 }; };
34 46 } }
35 47
36 Shortcut::Shortcut(std::wstring shortcut, std::function<void(int)> handler,
37 std::string descr)
38 : shortcut(std::move(shortcut)), handler(std::move(handler)),
48 Shortcut::Shortcut(std::vector<std::wstring> shortcuts,
49 std::function<void(int)> handler, std::string descr)
50 : shortcuts(std::move(shortcuts)), handler(std::move(handler)),
39 51 descr(std::move(descr)) descr(std::move(descr))
40 52 { } { }
41 53
 
... ... Mode::Mode(std::string id) : id(std::move(id)), useCount(false)
45 57 void void
46 58 Mode::addShortcut(Shortcut shortcut) Mode::addShortcut(Shortcut shortcut)
47 59 { {
48 if (shortcut.shortcut.length() > 4U) {
49 throw std::invalid_argument("Shortcut value is too long");
50 }
60 for (const std::wstring &lhs : shortcut.shortcuts) {
61 if (lhs.length() > 4U) {
62 throw std::invalid_argument("Shortcut value is too long");
63 }
51 64
52 if (!seenShortcuts.emplace(shortcut.shortcut).second) {
53 throw std::invalid_argument("Duplicated shortcut");
65 if (!seenShortcuts.emplace(lhs).second) {
66 throw std::invalid_argument("Duplicated shortcut");
67 }
54 68 } }
55 69
56 70 shortcuts.emplace_back(std::move(shortcut)); shortcuts.emplace_back(std::move(shortcut));
File Mode.hpp changed (mode: 100644) (index e95ec1b..e73eed3)
23 23 #include <functional> #include <functional>
24 24 #include <string> #include <string>
25 25 #include <unordered_set> #include <unordered_set>
26 #include <vector>
26 27
27 28 namespace vle { namespace vle {
28 29
 
... ... struct Shortcut
33 34 // it's not available. // it's not available.
34 35 using handler_t = void(int count); using handler_t = void(int count);
35 36
36 std::wstring shortcut; // Shortcut itself.
37 std::function<handler_t> handler; // Handler to be called on match.
38 std::string descr; // Description of the shortcut.
37 std::vector<std::wstring> shortcuts; // Shortcuts.
38 std::function<handler_t> handler; // Handler to be called on match.
39 std::string descr; // Description of the shortcut.
39 40
40 41 // Creates a shortcut that doesn't use count. // Creates a shortcut that doesn't use count.
41 42 Shortcut(std::wstring shortcut, std::function<void(void)> handler, Shortcut(std::wstring shortcut, std::function<void(void)> handler,
 
... ... struct Shortcut
43 44 // Creates a shortcut that uses count. // Creates a shortcut that uses count.
44 45 Shortcut(std::wstring shortcut, std::function<void(int)> handler, Shortcut(std::wstring shortcut, std::function<void(int)> handler,
45 46 std::string descr); std::string descr);
47 // Creates a set of shortcuts that don't use count.
48 Shortcut(std::vector<std::wstring> shortcuts,
49 std::function<void(void)> handler, std::string descr);
50 // Creates a set of shortcuts that use count.
51 Shortcut(std::vector<std::wstring> shortcuts,
52 std::function<void(int)> handler, std::string descr);
46 53 }; };
47 54
48 55 // Contains mode configuration. // Contains mode configuration.
File Modes.cpp changed (mode: 100644) (index 253b647..e95b5ff)
19 19 #include "Modes.hpp" #include "Modes.hpp"
20 20
21 21 #include <cassert> #include <cassert>
22 #include <cwchar>
22 23
23 24 #include <iterator> #include <iterator>
24 25 #include <stdexcept> #include <stdexcept>
 
... ... Modes::init(std::vector<Mode> &&modesInfo)
93 94 std::vector<keys_add_info_t> keys; std::vector<keys_add_info_t> keys;
94 95 for (const Shortcut &sh : entry.second.getShortcuts()) { for (const Shortcut &sh : entry.second.getShortcuts()) {
95 96 keys_add_info_t addInfo = {}; keys_add_info_t addInfo = {};
96 wcscpy(const_cast<wchar_t *>(addInfo.keys), sh.shortcut.c_str());
97 97 addInfo.info.data.handler = &keyHandler; addInfo.info.data.handler = &keyHandler;
98 98 addInfo.info.descr = sh.descr.c_str(); addInfo.info.descr = sh.descr.c_str();
99 99 addInfo.info.context = const_cast<void *>( addInfo.info.context = const_cast<void *>(
100 100 static_cast<const void *>(&sh.handler) static_cast<const void *>(&sh.handler)
101 101 ); );
102 keys.push_back(addInfo);
102
103 for (const std::wstring &lhs : sh.shortcuts) {
104 std::wcscpy(const_cast<wchar_t *>(addInfo.keys), lhs.c_str());
105 keys.push_back(addInfo);
106 }
103 107 } }
104 108
105 109 int ret_code = vle_keys_add(&keys[0], keys.size(), modeId++); int ret_code = vle_keys_add(&keys[0], keys.size(), modeId++);
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/libvle

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/libvle

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master