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"
32 using support::FileName;
33 using support::i18nLibFileSearch;
39 string const kb_keymap::printKeySym(LyXKeySym const & key,
40 key_modifier::state mod)
44 string const s = key.getSymbolName();
46 if (mod & key_modifier::shift)
48 if (mod & key_modifier::ctrl)
50 if (mod & key_modifier::alt)
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 FileName 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 (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_COMMAND_PREFIX);
201 // final key - reset map
202 seq->curmap = seq->stdmap;
209 // error - key not found:
210 seq->curmap = seq->stdmap;
217 docstring const kb_keymap::print(bool forgui) const
220 Table::const_iterator end = table.end();
221 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
222 buf += cit->code->print(cit->mod.first, forgui);
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 '"
249 << to_utf8(seq->print(false))
250 << "' is overriding old binding..."
252 if (it->table.get()) {
256 it->func.origin = FuncRequest::KEYBOARD;
258 } else if (!it->table.get()) {
259 lyxerr << "Error: New binding for '"
260 << to_utf8(seq->print(false))
261 << "' is overriding old binding..."
265 it->table->defkey(seq, func, r + 1);
271 Table::iterator newone = table.insert(table.end(), kb_key());
273 newone->mod = seq->modifiers[r];
274 if (r + 1 == seq->length()) {
276 newone->func.origin = FuncRequest::KEYBOARD;
277 newone->table.reset();
279 newone->table.reset(new kb_keymap);
280 newone->table->defkey(seq, func, r + 1);
285 docstring const kb_keymap::printbindings(FuncRequest const & func) const
287 odocstringstream res;
288 Bindings bindings = findbindings(func);
289 for (Bindings::const_iterator cit = bindings.begin();
290 cit != bindings.end() ; ++cit)
291 res << '[' << cit->print(true) << ']';
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);