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;
31 string const kb_keymap::printKeysym(LyXKeySymPtr key,
32 key_modifier::state mod)
36 string const s = key->getSymbolName();
38 if (mod & key_modifier::shift)
40 if (mod & key_modifier::ctrl)
42 if (mod & key_modifier::alt)
50 string const kb_keymap::printKey(kb_key const & key) const
52 return printKeysym(key.code, key.mod.first);
56 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
58 if (lyxerr.debugging(Debug::KBMAP)) {
59 lyxerr << "BIND: Sequence `"
60 << seq << "' Action `"
61 << func.action << '\'' << endl;
66 string::size_type const res = k.parse(seq);
67 if (res == string::npos) {
70 lyxerr[Debug::KBMAP] << "Parse error at position " << res
71 << " in key sequence '" << seq << "'."
86 keyword_item bindTags[] = {
87 { "\\bind", BN_BIND },
88 { "\\bind_file", BN_BINDFILE }
94 bool kb_keymap::read(string const & bind_file)
96 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
98 LyXLex lexrc(bindTags, bindCount);
99 if (lyxerr.debugging(Debug::PARSER))
100 lexrc.printTable(lyxerr);
102 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
105 lyxerr << "kb_keymap::read: cannot open bind file:"
110 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
113 while (!error && lexrc.isOK()) {
114 switch (lexrc.lex()) {
115 case LyXLex::LEX_UNDEF:
116 lexrc.printError("Unknown tag `$$Token'");
119 case LyXLex::LEX_FEOF:
126 seq = lexrc.getString();
128 lexrc.printError("BN_BIND: Missing key sequence");
133 if (lexrc.next(true)) {
134 cmd = lexrc.getString();
136 lexrc.printError("BN_BIND: missing command");
141 FuncRequest func = lyxaction.lookupFunc(cmd);
142 if (func. action == LFUN_UNKNOWN_ACTION) {
143 lexrc.printError("BN_BIND: Unknown LyX"
144 " function `$$Token'");
154 string const tmp(lexrc.getString());
157 lexrc.printError("BN_BINDFILE: Missing file name");
167 lyxerr << "kb_keymap::read: error while reading bind file:"
174 kb_keymap::lookup(LyXKeySymPtr key,
175 key_modifier::state mod, kb_sequence * seq) const
177 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
180 seq->curmap = seq->stdmap;
185 Table::const_iterator end = table.end();
186 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
187 key_modifier::state mask(cit->mod.second);
188 key_modifier::state check =
189 static_cast<key_modifier::state>(mod & ~mask);
191 if (*(cit->code) == *key && cit->mod.first == check) {
193 if (cit->table.get()) {
194 // this is a prefix key - set new map
195 seq->curmap = cit->table.get();
196 static FuncRequest prefix(LFUN_PREFIX);
199 // final key - reset map
200 seq->curmap = seq->stdmap;
207 // error - key not found:
208 seq->curmap = seq->stdmap;
215 string const kb_keymap::print() const
218 Table::const_iterator end = table.end();
219 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
220 buf += printKey((*cit));
227 void kb_keymap::defkey(kb_sequence * seq,
228 FuncRequest const & func, unsigned int r)
230 LyXKeySymPtr code = seq->sequence[r];
234 key_modifier::state const mod1 = seq->modifiers[r].first;
235 key_modifier::state const mod2 = seq->modifiers[r].second;
237 // check if key is already there
238 Table::iterator end = table.end();
239 for (Table::iterator it = table.begin(); it != end; ++it) {
240 if (*(code) == *(it->code)
241 && mod1 == it->mod.first
242 && mod2 == it->mod.second) {
244 if (r + 1 == seq->length()) {
246 << "Warning: New binding for '"
248 << "' is overriding old binding..."
250 if (it->table.get()) {
255 } else if (!it->table.get()) {
256 lyxerr << "Error: New binding for '" << seq->print()
257 << "' is overriding old binding..."
261 it->table->defkey(seq, func, r + 1);
267 Table::iterator newone = table.insert(table.end(), kb_key());
269 newone->mod = seq->modifiers[r];
270 if (r + 1 == seq->length()) {
272 newone->table.reset();
275 newone->table.reset(new kb_keymap);
276 newone->table->defkey(seq, func, r + 1);
282 string const kb_keymap::findbinding(FuncRequest const & func,
283 string const & prefix) const
286 if (table.empty()) return res;
288 Table::const_iterator end = table.end();
289 for (Table::const_iterator cit = table.begin();
291 if (cit->table.get()) {
292 res += cit->table->findbinding(func,
296 } else if (cit->func == func) {
298 res += prefix + printKey((*cit));