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"
27 using lyx::support::i18nLibFileSearch;
33 string const kb_keymap::printKeysym(LyXKeySymPtr key,
34 key_modifier::state mod)
38 string const s = key->getSymbolName();
40 if (mod & key_modifier::shift)
42 if (mod & key_modifier::ctrl)
44 if (mod & key_modifier::alt)
52 string const kb_keymap::printKey(kb_key const & key) const
54 return printKeysym(key.code, key.mod.first);
58 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
60 if (lyxerr.debugging(Debug::KBMAP)) {
61 lyxerr << "BIND: Sequence `"
62 << seq << "' Action `"
63 << func.action << '\'' << endl;
68 string::size_type const res = k.parse(seq);
69 if (res == string::npos) {
72 lyxerr[Debug::KBMAP] << "Parse error at position " << res
73 << " in key sequence '" << seq << "'."
88 keyword_item bindTags[] = {
89 { "\\bind", BN_BIND },
90 { "\\bind_file", BN_BINDFILE }
96 bool kb_keymap::read(string const & bind_file)
98 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
100 LyXLex lexrc(bindTags, bindCount);
101 if (lyxerr.debugging(Debug::PARSER))
102 lexrc.printTable(lyxerr);
104 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
107 lyxerr << "kb_keymap::read: cannot open bind file:"
112 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
115 while (!error && lexrc.isOK()) {
116 switch (lexrc.lex()) {
117 case LyXLex::LEX_UNDEF:
118 lexrc.printError("Unknown tag `$$Token'");
121 case LyXLex::LEX_FEOF:
128 seq = lexrc.getString();
130 lexrc.printError("BN_BIND: Missing key sequence");
135 if (lexrc.next(true)) {
136 cmd = lexrc.getString();
138 lexrc.printError("BN_BIND: missing command");
143 FuncRequest func = lyxaction.lookupFunc(cmd);
144 if (func. action == LFUN_UNKNOWN_ACTION) {
145 lexrc.printError("BN_BIND: Unknown LyX"
146 " function `$$Token'");
156 string const tmp(lexrc.getString());
159 lexrc.printError("BN_BINDFILE: Missing file name");
169 lyxerr << "kb_keymap::read: error while reading bind file:"
176 kb_keymap::lookup(LyXKeySymPtr key,
177 key_modifier::state mod, kb_sequence * seq) const
179 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
182 seq->curmap = seq->stdmap;
187 Table::const_iterator end = table.end();
188 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
189 key_modifier::state mask(cit->mod.second);
190 key_modifier::state check =
191 static_cast<key_modifier::state>(mod & ~mask);
193 if (*(cit->code) == *key && cit->mod.first == check) {
195 if (cit->table.get()) {
196 // this is a prefix key - set new map
197 seq->curmap = cit->table.get();
198 static FuncRequest prefix(LFUN_PREFIX);
201 // final key - reset map
202 seq->curmap = seq->stdmap;
209 // error - key not found:
210 seq->curmap = seq->stdmap;
217 string const kb_keymap::print() const
220 Table::const_iterator end = table.end();
221 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
222 buf += printKey((*cit));
229 void kb_keymap::defkey(kb_sequence * seq,
230 FuncRequest const & func, unsigned int r)
232 LyXKeySymPtr code = seq->sequence[r];
236 key_modifier::state const mod1 = seq->modifiers[r].first;
237 key_modifier::state const mod2 = seq->modifiers[r].second;
239 // check if key is already there
240 Table::iterator end = table.end();
241 for (Table::iterator it = table.begin(); it != end; ++it) {
242 if (*(code) == *(it->code)
243 && mod1 == it->mod.first
244 && mod2 == it->mod.second) {
246 if (r + 1 == seq->length()) {
248 << "Warning: New binding for '"
250 << "' is overriding old binding..."
252 if (it->table.get()) {
257 } else if (!it->table.get()) {
258 lyxerr << "Error: New binding for '" << seq->print()
259 << "' is overriding old binding..."
263 it->table->defkey(seq, func, r + 1);
269 Table::iterator newone = table.insert(table.end(), kb_key());
271 newone->mod = seq->modifiers[r];
272 if (r + 1 == seq->length()) {
274 newone->table.reset();
277 newone->table.reset(new kb_keymap);
278 newone->table->defkey(seq, func, r + 1);
284 string const kb_keymap::findbinding(FuncRequest const & func,
285 string const & prefix) const
288 if (table.empty()) return res;
290 Table::const_iterator end = table.end();
291 for (Table::const_iterator cit = table.begin();
293 if (cit->table.get()) {
294 res += cit->table->findbinding(func,
298 } else if (cit->func == func) {
300 res += prefix + printKey((*cit));