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 "KeySequence.h"
20 #include "LyXAction.h"
23 #include "support/docstream.h"
24 #include "support/filetools.h"
34 using boost::make_tuple;
39 using support::FileName;
40 using support::i18nLibFileSearch;
43 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
47 string const s = key.getSymbolName();
49 if (mod & ControlModifier)
51 if (mod & AltModifier)
53 if (mod & ShiftModifier)
61 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
63 LYXERR(Debug::KBMAP) << "BIND: Sequence `"
64 << seq << "' Action `"
65 << 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 << "'."
78 return res == string::npos ? 0 : res;
82 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
86 string::size_type const res = k.parse(seq);
87 if (res == string::npos)
90 LYXERR(Debug::KBMAP) << "Parse error at position " << res
91 << " in key sequence '" << seq << "'."
93 return res == string::npos ? 0 : res;
97 bool KeyMap::hasBinding(KeySequence const & seq, FuncRequest const & func,
100 KeySymbol code = seq.sequence[r];
104 KeyModifier const mod1 = seq.modifiers[r].first;
105 KeyModifier const mod2 = seq.modifiers[r].second;
107 // check if key is already there
108 Table::iterator end = table.end();
109 for (Table::iterator it = table.begin(); it != end; ++it) {
111 && mod1 == it->mod.first
112 && mod2 == it->mod.second) {
113 if (r + 1 == seq.length())
114 return it->func == func;
115 else if (it->table.get())
116 return it->table->hasBinding(seq, func, r + 1);
137 keyword_item bindTags[] = {
138 { "\\bind", BN_BIND },
139 { "\\bind_file", BN_BINDFILE },
140 { "\\unbind", BN_UNBIND },
146 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
148 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
150 Lexer lexrc(bindTags, bindCount);
151 if (lyxerr.debugging(Debug::PARSER))
152 lexrc.printTable(lyxerr);
154 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
157 lyxerr << "KeyMap::read: cannot open bind file:"
162 LYXERR(Debug::KBMAP) << "Reading bind file:" << tmp << endl;
165 while (lexrc.isOK()) {
166 switch (lexrc.lex()) {
167 case Lexer::LEX_UNDEF:
168 lexrc.printError("Unknown tag `$$Token'");
171 case Lexer::LEX_FEOF:
178 seq = lexrc.getString();
180 lexrc.printError("BN_BIND: Missing key sequence");
185 if (lexrc.next(true)) {
186 cmd = lexrc.getString();
188 lexrc.printError("BN_BIND: missing command");
193 FuncRequest func = lyxaction.lookupFunc(cmd);
194 if (func. action == LFUN_UNKNOWN_ACTION) {
195 lexrc.printError("BN_BIND: Unknown LyX"
196 " function `$$Token'");
209 seq = lexrc.getString();
211 lexrc.printError("BN_UNBIND: Missing key sequence");
216 if (lexrc.next(true)) {
217 cmd = lexrc.getString();
219 lexrc.printError("BN_UNBIND: missing command");
224 FuncRequest func = lyxaction.lookupFunc(cmd);
225 if (func. action == LFUN_UNKNOWN_ACTION) {
226 lexrc.printError("BN_UNBIND: Unknown LyX"
227 " function `$$Token'");
233 unbind_map->bind(seq, func);
240 string const tmp(lexrc.getString());
241 error |= !read(tmp, unbind_map);
243 lexrc.printError("BN_BINDFILE: Missing file name");
253 lyxerr << "KeyMap::read: error while reading bind file:"
259 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
261 ofstream os(bind_file.c_str(),
262 append ? (ios::app | ios:: out): ios::out);
265 os << "## This file is automatically generated by lyx\n"
266 << "## All modifications will be lost\n\n";
268 string tag = unbind ? "\\unbind" : "\\bind";
269 BindingList const list = listBindings(false);
270 BindingList::const_iterator it = list.begin();
271 BindingList::const_iterator it_end = list.end();
272 for (; it != it_end; ++it) {
273 kb_action action = it->get<0>().action;
274 string arg = to_utf8(it->get<0>().argument());
277 << to_utf8(it->get<1>().print(KeySequence::BindFile))
279 << lyxaction.getActionName(action)
280 << (arg.empty() ? "" : " ") << arg
288 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
289 KeyModifier mod, KeySequence * seq) const
291 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
294 seq->curmap = seq->stdmap;
299 Table::const_iterator end = table.end();
300 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
301 KeyModifier mask = cit->mod.second;
302 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
304 if (cit->code == key && cit->mod.first == check) {
306 if (cit->table.get()) {
307 // this is a prefix key - set new map
308 seq->curmap = cit->table.get();
309 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
312 // final key - reset map
313 seq->curmap = seq->stdmap;
320 // error - key not found:
321 seq->curmap = seq->stdmap;
328 docstring const KeyMap::print(bool forgui) const
331 Table::const_iterator end = table.end();
332 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
333 buf += cit->code.print(cit->mod.first, forgui);
340 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
342 KeySymbol code = seq->sequence[r];
346 KeyModifier const mod1 = seq->modifiers[r].first;
347 KeyModifier const mod2 = seq->modifiers[r].second;
349 // check if key is already there
350 Table::iterator end = table.end();
351 for (Table::iterator it = table.begin(); it != end; ++it) {
353 && mod1 == it->mod.first
354 && mod2 == it->mod.second) {
356 if (r + 1 == seq->length()) {
358 << "Warning: New binding for '"
359 << to_utf8(seq->print(KeySequence::Portable))
360 << "' is overriding old binding..."
362 if (it->table.get()) {
366 it->func.origin = FuncRequest::KEYBOARD;
368 } else if (!it->table.get()) {
369 lyxerr << "Error: New binding for '"
370 << to_utf8(seq->print(KeySequence::Portable))
371 << "' is overriding old binding..."
375 it->table->bind(seq, func, r + 1);
381 Table::iterator newone = table.insert(table.end(), Key());
383 newone->mod = seq->modifiers[r];
384 if (r + 1 == seq->length()) {
386 newone->func.origin = FuncRequest::KEYBOARD;
387 newone->table.reset();
389 newone->table.reset(new KeyMap);
390 newone->table->bind(seq, func, r + 1);
395 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
397 KeySymbol code = seq->sequence[r];
401 KeyModifier const mod1 = seq->modifiers[r].first;
402 KeyModifier const mod2 = seq->modifiers[r].second;
404 // check if key is already there
405 Table::iterator end = table.end();
406 Table::iterator remove = end;
407 for (Table::iterator it = table.begin(); it != end; ++it) {
409 && mod1 == it->mod.first
410 && mod2 == it->mod.second) {
412 if (r + 1 == seq->length()) {
413 if (it->func == func) {
418 } else if (it->table.get()) {
419 it->table->unbind(seq, func, r + 1);
420 if (it->table->empty())
433 docstring const KeyMap::printbindings(FuncRequest const & func) const
435 Bindings bindings = findbindings(func);
436 if (bindings.empty())
439 odocstringstream res;
440 Bindings::const_iterator cit = bindings.begin();
441 Bindings::const_iterator cit_end = bindings.end();
442 // prin the first item
443 res << cit->print(KeySequence::ForGui);
444 // more than one shortcuts?
445 for (++cit; cit != cit_end; ++cit)
446 res << ", " << cit->print(KeySequence::ForGui);
451 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func) const
453 return findbindings(func, KeySequence(0, 0));
457 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func,
458 KeySequence const & prefix) const
461 if (table.empty()) return res;
463 Table::const_iterator end = table.end();
464 for (Table::const_iterator cit = table.begin();
466 if (cit->table.get()) {
467 KeySequence seq = prefix;
468 seq.addkey(cit->code, cit->mod.first);
470 cit->table->findbindings(func, seq);
471 res.insert(res.end(), res2.begin(), res2.end());
472 } else if (cit->func == func) {
473 KeySequence seq = prefix;
474 seq.addkey(cit->code, cit->mod.first);
483 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
486 listBindings(list, KeySequence(0, 0), tag);
488 LyXAction::const_func_iterator fit = lyxaction.func_begin();
489 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
490 for (; fit != fit_end; ++fit) {
491 kb_action action = fit->second;
492 bool has_action = false;
493 BindingList::const_iterator it = list.begin();
494 BindingList::const_iterator it_end = list.end();
495 for (; it != it_end; ++it)
496 if (it->get<0>().action == action) {
501 list.push_back(make_tuple(action, KeySequence(0, 0), tag));
508 void KeyMap::listBindings(BindingList & list,
509 KeySequence const & prefix, int tag) const
511 Table::const_iterator it = table.begin();
512 Table::const_iterator it_end = table.end();
513 for (; it != it_end; ++it) {
514 // a LFUN_COMMAND_PREFIX
515 if (it->table.get()) {
516 KeySequence seq = prefix;
517 seq.addkey(it->code, it->mod.first);
518 it->table->listBindings(list, seq, tag);
520 KeySequence seq = prefix;
521 seq.addkey(it->code, it->mod.first);
522 list.push_back(make_tuple(it->func, seq, tag));