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.
18 #include "KeySequence.h"
19 #include "LyXAction.h"
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
32 using namespace lyx::support;
37 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
41 string const s = key.getSymbolName();
43 if (mod & ControlModifier)
45 if (mod & AltModifier)
47 if (mod & ShiftModifier)
55 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
57 LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
58 << func.action << '\'');
62 string::size_type const res = k.parse(seq);
63 if (res == string::npos) {
66 LYXERR(Debug::KBMAP, "Parse error at position " << res
67 << " in key sequence '" << seq << "'.");
70 return res == string::npos ? 0 : res;
74 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
78 string::size_type const res = k.parse(seq);
79 if (res == string::npos)
82 LYXERR(Debug::KBMAP, "Parse error at position " << res
83 << " in key sequence '" << seq << "'.");
84 return res == string::npos ? 0 : res;
88 bool KeyMap::hasBinding(KeySequence const & seq, FuncRequest const & func,
91 KeySymbol code = seq.sequence[r];
95 KeyModifier const mod1 = seq.modifiers[r].first;
96 KeyModifier const mod2 = seq.modifiers[r].second;
98 // check if key is already there
99 Table::iterator end = table.end();
100 for (Table::iterator it = table.begin(); it != end; ++it) {
102 && mod1 == it->mod.first
103 && mod2 == it->mod.second) {
104 if (r + 1 == seq.length())
105 return it->func == func;
106 else if (it->table.get())
107 return it->table->hasBinding(seq, func, r + 1);
128 LexerKeyword bindTags[] = {
129 { "\\bind", BN_BIND },
130 { "\\bind_file", BN_BINDFILE },
131 { "\\unbind", BN_UNBIND },
137 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
139 Lexer lexrc(bindTags);
140 if (lyxerr.debugging(Debug::PARSER))
141 lexrc.printTable(lyxerr);
143 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
146 lyxerr << "KeyMap::read: cannot open bind file:"
151 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
154 while (lexrc.isOK()) {
155 switch (lexrc.lex()) {
156 case Lexer::LEX_UNDEF:
157 lexrc.printError("Unknown tag `$$Token'");
160 case Lexer::LEX_FEOF:
167 seq = lexrc.getString();
169 lexrc.printError("BN_BIND: Missing key sequence");
174 if (lexrc.next(true)) {
175 cmd = lexrc.getString();
177 lexrc.printError("BN_BIND: missing command");
182 FuncRequest func = lyxaction.lookupFunc(cmd);
183 if (func. action == LFUN_UNKNOWN_ACTION) {
184 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
197 seq = lexrc.getString();
199 lexrc.printError("BN_UNBIND: Missing key sequence");
204 if (lexrc.next(true)) {
205 cmd = lexrc.getString();
207 lexrc.printError("BN_UNBIND: missing command");
212 FuncRequest func = lyxaction.lookupFunc(cmd);
213 if (func. action == LFUN_UNKNOWN_ACTION) {
214 lexrc.printError("BN_UNBIND: Unknown LyX"
215 " function `$$Token'");
221 unbind_map->bind(seq, func);
228 string const tmp(lexrc.getString());
229 error |= !read(tmp, unbind_map);
231 lexrc.printError("BN_BINDFILE: Missing file name");
241 lyxerr << "KeyMap::read: error while reading bind file:"
247 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
249 ofstream os(bind_file.c_str(),
250 append ? (ios::app | ios::out) : ios::out);
253 os << "## This file is automatically generated by lyx\n"
254 << "## All modifications will be lost\n\n";
256 string tag = unbind ? "\\unbind" : "\\bind";
257 BindingList const list = listBindings(false);
258 BindingList::const_iterator it = list.begin();
259 BindingList::const_iterator it_end = list.end();
260 for (; it != it_end; ++it) {
261 FuncCode action = it->request.action;
262 string arg = to_utf8(it->request.argument());
265 << to_utf8(it->sequence.print(KeySequence::BindFile))
267 << lyxaction.getActionName(action)
268 << (arg.empty() ? "" : " ") << arg
276 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
277 KeyModifier mod, KeySequence * seq) const
279 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
282 seq->curmap = seq->stdmap;
287 Table::const_iterator end = table.end();
288 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
289 KeyModifier mask = cit->mod.second;
290 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
292 if (cit->code == key && cit->mod.first == check) {
294 if (cit->table.get()) {
295 // this is a prefix key - set new map
296 seq->curmap = cit->table.get();
297 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
300 // final key - reset map
301 seq->curmap = seq->stdmap;
308 // error - key not found:
309 seq->curmap = seq->stdmap;
316 docstring const KeyMap::print(bool forgui) const
319 Table::const_iterator end = table.end();
320 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
321 buf += cit->code.print(cit->mod.first, forgui);
328 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
330 KeySymbol code = seq->sequence[r];
334 KeyModifier const mod1 = seq->modifiers[r].first;
335 KeyModifier const mod2 = seq->modifiers[r].second;
337 // check if key is already there
338 Table::iterator end = table.end();
339 for (Table::iterator it = table.begin(); it != end; ++it) {
341 && mod1 == it->mod.first
342 && mod2 == it->mod.second) {
344 if (r + 1 == seq->length()) {
345 LYXERR(Debug::KBMAP, "Warning: New binding for '"
346 << to_utf8(seq->print(KeySequence::Portable))
347 << "' is overriding old binding...");
348 if (it->table.get()) {
352 it->func.origin = FuncRequest::KEYBOARD;
354 } else if (!it->table.get()) {
355 lyxerr << "Error: New binding for '"
356 << to_utf8(seq->print(KeySequence::Portable))
357 << "' is overriding old binding..."
361 it->table->bind(seq, func, r + 1);
367 Table::iterator newone = table.insert(table.end(), Key());
369 newone->mod = seq->modifiers[r];
370 if (r + 1 == seq->length()) {
372 newone->func.origin = FuncRequest::KEYBOARD;
373 newone->table.reset();
375 newone->table.reset(new KeyMap);
376 newone->table->bind(seq, func, r + 1);
381 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
383 KeySymbol code = seq->sequence[r];
387 KeyModifier const mod1 = seq->modifiers[r].first;
388 KeyModifier const mod2 = seq->modifiers[r].second;
390 // check if key is already there
391 Table::iterator end = table.end();
392 Table::iterator remove = end;
393 for (Table::iterator it = table.begin(); it != end; ++it) {
395 && mod1 == it->mod.first
396 && mod2 == it->mod.second) {
398 if (r + 1 == seq->length()) {
399 if (it->func == func) {
404 } else if (it->table.get()) {
405 it->table->unbind(seq, func, r + 1);
406 if (it->table->empty())
419 docstring KeyMap::printBindings(FuncRequest const & func) const
421 Bindings bindings = findBindings(func);
422 if (bindings.empty())
425 odocstringstream res;
426 Bindings::const_iterator cit = bindings.begin();
427 Bindings::const_iterator cit_end = bindings.end();
428 // prin the first item
429 res << cit->print(KeySequence::ForGui);
430 // more than one shortcuts?
431 for (++cit; cit != cit_end; ++cit)
432 res << ", " << cit->print(KeySequence::ForGui);
437 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
439 return findBindings(func, KeySequence(0, 0));
443 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
444 KeySequence const & prefix) const
450 Table::const_iterator end = table.end();
451 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
452 if (cit->table.get()) {
453 KeySequence seq = prefix;
454 seq.addkey(cit->code, cit->mod.first);
455 Bindings res2 = cit->table->findBindings(func, seq);
456 res.insert(res.end(), res2.begin(), res2.end());
457 } else if (cit->func == func) {
458 KeySequence seq = prefix;
459 seq.addkey(cit->code, cit->mod.first);
468 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
471 listBindings(list, KeySequence(0, 0), tag);
473 LyXAction::const_func_iterator fit = lyxaction.func_begin();
474 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
475 for (; fit != fit_end; ++fit) {
476 FuncCode action = fit->second;
477 bool has_action = false;
478 BindingList::const_iterator it = list.begin();
479 BindingList::const_iterator it_end = list.end();
480 for (; it != it_end; ++it)
481 if (it->request.action == action) {
486 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
493 void KeyMap::listBindings(BindingList & list,
494 KeySequence const & prefix, int tag) const
496 Table::const_iterator it = table.begin();
497 Table::const_iterator it_end = table.end();
498 for (; it != it_end; ++it) {
499 // a LFUN_COMMAND_PREFIX
500 if (it->table.get()) {
501 KeySequence seq = prefix;
502 seq.addkey(it->code, it->mod.first);
503 it->table->listBindings(list, seq, tag);
505 KeySequence seq = prefix;
506 seq.addkey(it->code, it->mod.first);
507 list.push_back(Binding(it->func, seq, tag));