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.
19 #include "kbsequence.h"
20 #include "LyXAction.h"
23 #include "frontends/LyXKeySym.h"
25 #include "support/filetools.h"
28 using lyx::support::i18nLibFileSearch;
30 string const kb_keymap::printKeysym(LyXKeySymPtr key,
31 key_modifier::state mod)
35 string const s = key->getSymbolName();
37 if (mod & key_modifier::shift)
39 if (mod & key_modifier::ctrl)
41 if (mod & key_modifier::alt)
49 string const kb_keymap::printKey(kb_key const & key) const
51 return printKeysym(key.code, key.mod.first);
55 string::size_type kb_keymap::bind(string const & seq, int action)
57 if (lyxerr.debugging(Debug::KBMAP)) {
58 lyxerr << "BIND: Sequence `"
59 << seq << "' Action `"
60 << action << '\'' << endl;
65 string::size_type const res = k.parse(seq);
66 if (res == string::npos) {
69 lyxerr[Debug::KBMAP] << "Parse error at position " << res
70 << " in key sequence '" << seq << "'."
85 keyword_item bindTags[] = {
86 { "\\bind", BN_BIND },
87 { "\\bind_file", BN_BINDFILE }
93 bool kb_keymap::read(string const & bind_file)
95 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
97 LyXLex lexrc(bindTags, bindCount);
98 if (lyxerr.debugging(Debug::PARSER))
99 lexrc.printTable(lyxerr);
101 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
104 lyxerr << "kb_keymap::read: cannot open bind file:"
109 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
112 while (!error && lexrc.isOK()) {
113 switch (lexrc.lex()) {
114 case LyXLex::LEX_UNDEF:
115 lexrc.printError("Unknown tag `$$Token'");
118 case LyXLex::LEX_FEOF:
125 seq = lexrc.getString();
127 lexrc.printError("BN_BIND: Missing key sequence");
132 if (lexrc.next(true)) {
133 cmd = lexrc.getString();
135 lexrc.printError("BN_BIND: missing command");
140 int action = lyxaction.LookupFunc(cmd);
141 if (!action == LFUN_UNKNOWN_ACTION) {
142 lexrc.printError("BN_BIND: Unknown LyX"
143 " function `$$Token'");
148 bind(seq, kb_action(action));
153 string const tmp(lexrc.getString());
156 lexrc.printError("BN_BINDFILE: Missing file name");
166 lyxerr << "kb_keymap::read: error while reading bind file:"
172 int kb_keymap::lookup(LyXKeySymPtr key,
173 key_modifier::state mod, kb_sequence * seq) const
176 seq->curmap = seq->stdmap;
178 return LFUN_UNKNOWN_ACTION;
181 Table::const_iterator end = table.end();
182 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
183 key_modifier::state mask(cit->mod.second);
184 key_modifier::state check =
185 static_cast<key_modifier::state>(mod & ~mask);
187 if (*(cit->code) == *key && cit->mod.first == check) {
189 if (cit->table.get()) {
190 // this is a prefix key - set new map
191 seq->curmap = cit->table.get();
194 // final key - reset map
195 seq->curmap = seq->stdmap;
202 // error - key not found:
203 seq->curmap = seq->stdmap;
205 return LFUN_UNKNOWN_ACTION;
209 string const kb_keymap::print() const
212 Table::const_iterator end = table.end();
213 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
214 buf += printKey((*cit));
221 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
223 LyXKeySymPtr code = seq->sequence[r];
227 key_modifier::state const mod1 = seq->modifiers[r].first;
228 key_modifier::state const mod2 = seq->modifiers[r].second;
230 // check if key is already there
231 Table::iterator end = table.end();
232 for (Table::iterator it = table.begin(); it != end; ++it) {
233 if (*(code) == *(it->code)
234 && mod1 == it->mod.first
235 && mod2 == it->mod.second) {
237 if (r + 1 == seq->length()) {
239 << "Warning: New binding for '"
241 << "' is overriding old binding..."
243 if (it->table.get()) {
248 } else if (!it->table.get()) {
249 lyxerr << "Error: New binding for '" << seq->print()
250 << "' is overriding old binding..."
254 it->table->defkey(seq, action, r + 1);
260 Table::iterator newone = table.insert(table.end(), kb_key());
262 newone->mod = seq->modifiers[r];
263 if (r + 1 == seq->length()) {
264 newone->action = action;
265 newone->table.reset();
268 newone->table.reset(new kb_keymap);
269 newone->table->defkey(seq, action, r + 1);
275 string const kb_keymap::findbinding(int act, string const & prefix) const
278 if (table.empty()) return res;
280 Table::const_iterator end = table.end();
281 for (Table::const_iterator cit = table.begin();
283 if (cit->table.get()) {
284 res += cit->table->findbinding(act,
288 } else if (cit->action == act) {
290 res += prefix + printKey((*cit));