3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
11 * Full author contact details are available in file CREDITS.
17 #include "kbsequence.h"
18 #include "LyXAction.h"
19 #include "support/filetools.h"
24 using lyx::support::i18nLibFileSearch;
26 string const kb_keymap::printKeysym(LyXKeySymPtr key,
27 key_modifier::state mod)
31 string const s = key->getSymbolName();
33 if (mod & key_modifier::shift)
35 if (mod & key_modifier::ctrl)
37 if (mod & key_modifier::alt)
45 string const kb_keymap::printKey(kb_key const & key) const
47 return printKeysym(key.code, key.mod.first);
51 string::size_type kb_keymap::bind(string const & seq, int action)
53 if (lyxerr.debugging(Debug::KBMAP)) {
54 lyxerr << "BIND: Sequence `"
55 << seq << "' Action `"
56 << action << '\'' << endl;
61 string::size_type const res = k.parse(seq);
62 if (res == string::npos) {
65 lyxerr[Debug::KBMAP] << "Parse error at position " << res
66 << " in key sequence '" << seq << "'."
81 keyword_item bindTags[] = {
82 { "\\bind", BN_BIND },
83 { "\\bind_file", BN_BINDFILE }
89 bool kb_keymap::read(string const & bind_file)
91 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
93 LyXLex lexrc(bindTags, bindCount);
94 if (lyxerr.debugging(Debug::PARSER))
95 lexrc.printTable(lyxerr);
97 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
100 lyxerr << "kb_keymap::read: cannot open bind file:"
105 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
108 while (!error && lexrc.isOK()) {
109 switch (lexrc.lex()) {
110 case LyXLex::LEX_UNDEF:
111 lexrc.printError("Unknown tag `$$Token'");
114 case LyXLex::LEX_FEOF:
121 seq = lexrc.getString();
123 lexrc.printError("BN_BIND: Missing key sequence");
128 if (lexrc.next(true)) {
129 cmd = lexrc.getString();
131 lexrc.printError("BN_BIND: missing command");
136 int action = lyxaction.LookupFunc(cmd);
137 if (!action == LFUN_UNKNOWN_ACTION) {
138 lexrc.printError("BN_BIND: Unknown LyX"
139 " function `$$Token'");
144 bind(seq, kb_action(action));
149 string const tmp(lexrc.getString());
152 lexrc.printError("BN_BINDFILE: Missing file name");
162 lyxerr << "kb_keymap::read: error while reading bind file:"
168 int kb_keymap::lookup(LyXKeySymPtr key,
169 key_modifier::state mod, kb_sequence * seq) const
172 seq->curmap = seq->stdmap;
174 return LFUN_UNKNOWN_ACTION;
177 Table::const_iterator end = table.end();
178 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
179 key_modifier::state mask(cit->mod.second);
180 key_modifier::state check =
181 static_cast<key_modifier::state>(mod & ~mask);
183 if (*(cit->code) == *key && cit->mod.first == check) {
185 if (cit->table.get()) {
186 // this is a prefix key - set new map
187 seq->curmap = cit->table.get();
190 // final key - reset map
191 seq->curmap = seq->stdmap;
198 // error - key not found:
199 seq->curmap = seq->stdmap;
201 return LFUN_UNKNOWN_ACTION;
205 string const kb_keymap::print() const
208 Table::const_iterator end = table.end();
209 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
210 buf += printKey((*cit));
217 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
219 LyXKeySymPtr code = seq->sequence[r];
223 key_modifier::state const mod1 = seq->modifiers[r].first;
224 key_modifier::state const mod2 = seq->modifiers[r].second;
226 // check if key is already there
227 Table::iterator end = table.end();
228 for (Table::iterator it = table.begin(); it != end; ++it) {
229 if (*(code) == *(it->code)
230 && mod1 == it->mod.first
231 && mod2 == it->mod.second) {
233 if (r + 1 == seq->length()) {
235 << "Warning: New binding for '"
237 << "' is overriding old binding..."
239 if (it->table.get()) {
244 } else if (!it->table.get()) {
245 lyxerr << "Error: New binding for '" << seq->print()
246 << "' is overriding old binding..."
250 it->table->defkey(seq, action, r + 1);
256 Table::iterator newone = table.insert(table.end(), kb_key());
258 newone->mod = seq->modifiers[r];
259 if (r + 1 == seq->length()) {
260 newone->action = action;
261 newone->table.reset();
264 newone->table.reset(new kb_keymap);
265 newone->table->defkey(seq, action, r + 1);
271 string const kb_keymap::findbinding(int act, string const & prefix) const
274 if (table.empty()) return res;
276 Table::const_iterator end = table.end();
277 for (Table::const_iterator cit = table.begin();
279 if (cit->table.get()) {
280 res += cit->table->findbinding(act,
284 } else if (cit->action == act) {
286 res += prefix + printKey((*cit));