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/filetools.h"
33 using boost::make_tuple;
38 using support::FileName;
39 using support::i18nLibFileSearch;
42 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
46 string const s = key.getSymbolName();
48 if (mod & ShiftModifier)
50 if (mod & ControlModifier)
52 if (mod & AltModifier)
60 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
62 LYXERR(Debug::KBMAP) << "BIND: Sequence `"
63 << seq << "' Action `"
64 << 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 << "'."
77 return res == string::npos ? 0 : res;
81 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
85 string::size_type const res = k.parse(seq);
86 if (res == string::npos)
89 LYXERR(Debug::KBMAP) << "Parse error at position " << res
90 << " in key sequence '" << seq << "'."
92 return res == string::npos ? 0 : res;
96 bool KeyMap::hasBinding(KeySequence const & seq, FuncRequest const & func,
99 KeySymbol code = seq.sequence[r];
103 KeyModifier const mod1 = seq.modifiers[r].first;
104 KeyModifier const mod2 = seq.modifiers[r].second;
106 // check if key is already there
107 Table::iterator end = table.end();
108 for (Table::iterator it = table.begin(); it != end; ++it) {
110 && mod1 == it->mod.first
111 && mod2 == it->mod.second) {
112 if (r + 1 == seq.length())
113 return it->func == func;
114 else if (it->table.get())
115 return it->table->hasBinding(seq, func, r + 1);
136 keyword_item bindTags[] = {
137 { "\\bind", BN_BIND },
138 { "\\bind_file", BN_BINDFILE },
139 { "\\unbind", BN_UNBIND },
145 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
147 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
149 Lexer lexrc(bindTags, bindCount);
150 if (lyxerr.debugging(Debug::PARSER))
151 lexrc.printTable(lyxerr);
153 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
156 lyxerr << "KeyMap::read: cannot open bind file:"
161 LYXERR(Debug::KBMAP) << "Reading bind file:" << tmp << endl;
164 while (lexrc.isOK()) {
165 switch (lexrc.lex()) {
166 case Lexer::LEX_UNDEF:
167 lexrc.printError("Unknown tag `$$Token'");
170 case Lexer::LEX_FEOF:
177 seq = lexrc.getString();
179 lexrc.printError("BN_BIND: Missing key sequence");
184 if (lexrc.next(true)) {
185 cmd = lexrc.getString();
187 lexrc.printError("BN_BIND: missing command");
192 FuncRequest func = lyxaction.lookupFunc(cmd);
193 if (func. action == LFUN_UNKNOWN_ACTION) {
194 lexrc.printError("BN_BIND: Unknown LyX"
195 " function `$$Token'");
208 seq = lexrc.getString();
210 lexrc.printError("BN_UNBIND: Missing key sequence");
215 if (lexrc.next(true)) {
216 cmd = lexrc.getString();
218 lexrc.printError("BN_UNBIND: missing command");
223 FuncRequest func = lyxaction.lookupFunc(cmd);
224 if (func. action == LFUN_UNKNOWN_ACTION) {
225 lexrc.printError("BN_UNBIND: Unknown LyX"
226 " function `$$Token'");
232 unbind_map->bind(seq, func);
239 string const tmp(lexrc.getString());
240 error |= !read(tmp, unbind_map);
242 lexrc.printError("BN_BINDFILE: Missing file name");
252 lyxerr << "KeyMap::read: error while reading bind file:"
258 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
260 ofstream os(bind_file.c_str(),
261 append ? (ios::app | ios:: out): ios::out);
264 os << "## This file is automatically generated by lyx\n"
265 << "## All modifications will be lost\n\n";
267 string tag = unbind ? "\\unbind" : "\\bind";
268 BindingList const list = listBindings(false);
269 BindingList::const_iterator it = list.begin();
270 BindingList::const_iterator it_end = list.end();
271 for (; it != it_end; ++it) {
272 kb_action action = it->get<0>().action;
273 string arg = to_utf8(it->get<0>().argument());
276 << to_utf8(it->get<1>().print(KeySequence::BindFile))
278 << lyxaction.getActionName(action)
279 << (arg.empty() ? "" : " ") << arg
287 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
288 KeyModifier mod, KeySequence * seq) const
290 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
293 seq->curmap = seq->stdmap;
298 Table::const_iterator end = table.end();
299 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
300 KeyModifier mask = cit->mod.second;
301 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
303 if (cit->code == key && cit->mod.first == check) {
305 if (cit->table.get()) {
306 // this is a prefix key - set new map
307 seq->curmap = cit->table.get();
308 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
311 // final key - reset map
312 seq->curmap = seq->stdmap;
319 // error - key not found:
320 seq->curmap = seq->stdmap;
327 docstring const KeyMap::print(bool forgui) const
330 Table::const_iterator end = table.end();
331 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
332 buf += cit->code.print(cit->mod.first, forgui);
339 void KeyMap::defkey(KeySequence * seq, FuncRequest const & func, unsigned int r)
341 KeySymbol code = seq->sequence[r];
345 KeyModifier const mod1 = seq->modifiers[r].first;
346 KeyModifier const mod2 = seq->modifiers[r].second;
348 // check if key is already there
349 Table::iterator end = table.end();
350 for (Table::iterator it = table.begin(); it != end; ++it) {
352 && mod1 == it->mod.first
353 && mod2 == it->mod.second) {
355 if (r + 1 == seq->length()) {
357 << "Warning: New binding for '"
358 << to_utf8(seq->print(KeySequence::Portable))
359 << "' is overriding old binding..."
361 if (it->table.get()) {
365 it->func.origin = FuncRequest::KEYBOARD;
367 } else if (!it->table.get()) {
368 lyxerr << "Error: New binding for '"
369 << to_utf8(seq->print(KeySequence::Portable))
370 << "' is overriding old binding..."
374 it->table->defkey(seq, func, r + 1);
380 Table::iterator newone = table.insert(table.end(), Key());
382 newone->mod = seq->modifiers[r];
383 if (r + 1 == seq->length()) {
385 newone->func.origin = FuncRequest::KEYBOARD;
386 newone->table.reset();
388 newone->table.reset(new KeyMap);
389 newone->table->defkey(seq, func, r + 1);
394 void KeyMap::delkey(KeySequence * seq, FuncRequest const & func, unsigned int r)
396 KeySymbol code = seq->sequence[r];
400 KeyModifier const mod1 = seq->modifiers[r].first;
401 KeyModifier const mod2 = seq->modifiers[r].second;
403 // check if key is already there
404 Table::iterator end = table.end();
405 Table::iterator remove = end;
406 for (Table::iterator it = table.begin(); it != end; ++it) {
408 && mod1 == it->mod.first
409 && mod2 == it->mod.second) {
411 if (r + 1 == seq->length()) {
412 if (it->func == func) {
417 } else if (it->table.get()) {
418 it->table->delkey(seq, func, r + 1);
419 if (it->table->empty())
432 docstring const KeyMap::printbindings(FuncRequest const & func) const
434 Bindings bindings = findbindings(func);
435 if (bindings.empty())
438 odocstringstream res;
439 Bindings::const_iterator cit = bindings.begin();
440 Bindings::const_iterator cit_end = bindings.end();
441 // prin the first item
442 res << cit->print(KeySequence::ForGui);
443 // more than one shortcuts?
444 for (++cit; cit != cit_end; ++cit)
445 res << ", " << cit->print(KeySequence::ForGui);
450 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func) const
452 return findbindings(func, KeySequence(0, 0));
456 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func,
457 KeySequence const & prefix) const
460 if (table.empty()) return res;
462 Table::const_iterator end = table.end();
463 for (Table::const_iterator cit = table.begin();
465 if (cit->table.get()) {
466 KeySequence seq = prefix;
467 seq.addkey(cit->code, cit->mod.first);
469 cit->table->findbindings(func, seq);
470 res.insert(res.end(), res2.begin(), res2.end());
471 } else if (cit->func == func) {
472 KeySequence seq = prefix;
473 seq.addkey(cit->code, cit->mod.first);
482 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
485 listBindings(list, KeySequence(0, 0), tag);
487 LyXAction::const_func_iterator fit = lyxaction.func_begin();
488 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
489 for (; fit != fit_end; ++fit) {
490 kb_action action = fit->second;
491 bool has_action = false;
492 BindingList::const_iterator it = list.begin();
493 BindingList::const_iterator it_end = list.end();
494 for (; it != it_end; ++it)
495 if (it->get<0>().action == action) {
500 list.push_back(make_tuple(action, KeySequence(0, 0), tag));
507 void KeyMap::listBindings(BindingList & list,
508 KeySequence const & prefix, int tag) const
510 Table::const_iterator it = table.begin();
511 Table::const_iterator it_end = table.end();
512 for (; it != it_end; ++it) {
513 // a LFUN_COMMAND_PREFIX
514 if (it->table.get()) {
515 KeySequence seq = prefix;
516 seq.addkey(it->code, it->mod.first);
517 it->table->listBindings(list, seq, tag);
519 KeySequence seq = prefix;
520 seq.addkey(it->code, it->mod.first);
521 list.push_back(make_tuple(it->func, seq, tag));