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"
26 #include "support/std_sstream.h"
28 using lyx::support::i18nLibFileSearch;
34 string const kb_keymap::printKeySym(LyXKeySym const & key,
35 key_modifier::state mod)
39 string const s = key.getSymbolName();
41 if (mod & key_modifier::shift)
43 if (mod & key_modifier::ctrl)
45 if (mod & key_modifier::alt)
53 string const kb_keymap::printKey(kb_key const & key) const
55 return key.code->print(key.mod.first);
59 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
61 if (lyxerr.debugging(Debug::KBMAP)) {
62 lyxerr << "BIND: Sequence `"
63 << seq << "' Action `"
64 << func.action << '\'' << endl;
69 string::size_type const res = k.parse(seq);
70 if (res == string::npos) {
73 lyxerr[Debug::KBMAP] << "Parse error at position " << res
74 << " in key sequence '" << seq << "'."
89 keyword_item bindTags[] = {
90 { "\\bind", BN_BIND },
91 { "\\bind_file", BN_BINDFILE }
97 bool kb_keymap::read(string const & bind_file)
99 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
101 LyXLex lexrc(bindTags, bindCount);
102 if (lyxerr.debugging(Debug::PARSER))
103 lexrc.printTable(lyxerr);
105 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
108 lyxerr << "kb_keymap::read: cannot open bind file:"
113 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
116 while (lexrc.isOK()) {
117 switch (lexrc.lex()) {
118 case LyXLex::LEX_UNDEF:
119 lexrc.printError("Unknown tag `$$Token'");
122 case LyXLex::LEX_FEOF:
129 seq = lexrc.getString();
131 lexrc.printError("BN_BIND: Missing key sequence");
136 if (lexrc.next(true)) {
137 cmd = lexrc.getString();
139 lexrc.printError("BN_BIND: missing command");
144 FuncRequest func = lyxaction.lookupFunc(cmd);
145 if (func. action == LFUN_UNKNOWN_ACTION) {
146 lexrc.printError("BN_BIND: Unknown LyX"
147 " function `$$Token'");
157 string const tmp(lexrc.getString());
160 lexrc.printError("BN_BINDFILE: Missing file name");
170 lyxerr << "kb_keymap::read: error while reading bind file:"
177 kb_keymap::lookup(LyXKeySymPtr key,
178 key_modifier::state mod, kb_sequence * seq) const
180 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
183 seq->curmap = seq->stdmap;
188 Table::const_iterator end = table.end();
189 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
190 key_modifier::state mask(cit->mod.second);
191 key_modifier::state check =
192 static_cast<key_modifier::state>(mod & ~mask);
194 if (*(cit->code) == *key && cit->mod.first == check) {
196 if (cit->table.get()) {
197 // this is a prefix key - set new map
198 seq->curmap = cit->table.get();
199 static FuncRequest prefix(LFUN_PREFIX);
202 // final key - reset map
203 seq->curmap = seq->stdmap;
210 // error - key not found:
211 seq->curmap = seq->stdmap;
218 string const kb_keymap::print() const
221 Table::const_iterator end = table.end();
222 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
223 buf += printKey((*cit));
230 void kb_keymap::defkey(kb_sequence * seq,
231 FuncRequest const & func, unsigned int r)
233 LyXKeySymPtr code = seq->sequence[r];
237 key_modifier::state const mod1 = seq->modifiers[r].first;
238 key_modifier::state const mod2 = seq->modifiers[r].second;
240 // check if key is already there
241 Table::iterator end = table.end();
242 for (Table::iterator it = table.begin(); it != end; ++it) {
243 if (*(code) == *(it->code)
244 && mod1 == it->mod.first
245 && mod2 == it->mod.second) {
247 if (r + 1 == seq->length()) {
249 << "Warning: New binding for '"
251 << "' is overriding old binding..."
253 if (it->table.get()) {
258 } else if (!it->table.get()) {
259 lyxerr << "Error: New binding for '" << seq->print()
260 << "' is overriding old binding..."
264 it->table->defkey(seq, func, r + 1);
270 Table::iterator newone = table.insert(table.end(), kb_key());
272 newone->mod = seq->modifiers[r];
273 if (r + 1 == seq->length()) {
275 newone->table.reset();
278 newone->table.reset(new kb_keymap);
279 newone->table->defkey(seq, func, r + 1);
285 string const kb_keymap::printbindings(FuncRequest const & func) const
287 std::ostringstream res;
288 Bindings bindings = findbindings(func);
289 for (Bindings::const_iterator cit = bindings.begin();
290 cit != bindings.end() ; ++cit)
291 res << '[' << cit->print() << ']';
297 kb_keymap::findbindings(FuncRequest const & func) const
299 return findbindings(func, kb_sequence(0, 0));
304 kb_keymap::findbindings(FuncRequest const & func,
305 kb_sequence const & prefix) const
308 if (table.empty()) return res;
310 Table::const_iterator end = table.end();
311 for (Table::const_iterator cit = table.begin();
313 if (cit->table.get()) {
314 kb_sequence seq = prefix;
315 seq.addkey(cit->code, cit->mod.first);
317 cit->table->findbindings(func, seq);
318 res.insert(res.end(), res2.begin(), res2.end());
319 } else if (cit->func == func) {
320 kb_sequence seq = prefix;
321 seq.addkey(cit->code, cit->mod.first);
330 std::pair<LyXKeySym const *, key_modifier::state>
331 kb_keymap::find1keybinding(FuncRequest const & func) const
333 Table::const_iterator end = table.end();
334 for (Table::const_iterator cit = table.begin();
336 if (!cit->table.get() && cit->func == func)
337 return std::make_pair(cit->code.get(), cit->mod.first);
340 return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);