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 "support/debug.h"
19 #include "KeySequence.h"
20 #include "LyXAction.h"
23 #include "support/docstream.h"
24 #include "support/filetools.h"
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 & ControlModifier)
50 if (mod & AltModifier)
52 if (mod & ShiftModifier)
60 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
62 LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
63 << func.action << '\'');
67 string::size_type const res = k.parse(seq);
68 if (res == string::npos) {
71 LYXERR(Debug::KBMAP, "Parse error at position " << res
72 << " in key sequence '" << seq << "'.");
75 return res == string::npos ? 0 : res;
79 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
83 string::size_type const res = k.parse(seq);
84 if (res == string::npos)
87 LYXERR(Debug::KBMAP, "Parse error at position " << res
88 << " in key sequence '" << seq << "'.");
89 return res == string::npos ? 0 : res;
93 bool KeyMap::hasBinding(KeySequence const & seq, FuncRequest const & func,
96 KeySymbol code = seq.sequence[r];
100 KeyModifier const mod1 = seq.modifiers[r].first;
101 KeyModifier const mod2 = seq.modifiers[r].second;
103 // check if key is already there
104 Table::iterator end = table.end();
105 for (Table::iterator it = table.begin(); it != end; ++it) {
107 && mod1 == it->mod.first
108 && mod2 == it->mod.second) {
109 if (r + 1 == seq.length())
110 return it->func == func;
111 else if (it->table.get())
112 return it->table->hasBinding(seq, func, r + 1);
133 keyword_item bindTags[] = {
134 { "\\bind", BN_BIND },
135 { "\\bind_file", BN_BINDFILE },
136 { "\\unbind", BN_UNBIND },
142 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
144 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
146 Lexer lexrc(bindTags, bindCount);
147 if (lyxerr.debugging(Debug::PARSER))
148 lexrc.printTable(lyxerr);
150 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
153 lyxerr << "KeyMap::read: cannot open bind file:"
158 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
161 while (lexrc.isOK()) {
162 switch (lexrc.lex()) {
163 case Lexer::LEX_UNDEF:
164 lexrc.printError("Unknown tag `$$Token'");
167 case Lexer::LEX_FEOF:
174 seq = lexrc.getString();
176 lexrc.printError("BN_BIND: Missing key sequence");
181 if (lexrc.next(true)) {
182 cmd = lexrc.getString();
184 lexrc.printError("BN_BIND: missing command");
189 FuncRequest func = lyxaction.lookupFunc(cmd);
190 if (func. action == LFUN_UNKNOWN_ACTION) {
191 lexrc.printError("BN_BIND: Unknown LyX"
192 " function `$$Token'");
205 seq = lexrc.getString();
207 lexrc.printError("BN_UNBIND: Missing key sequence");
212 if (lexrc.next(true)) {
213 cmd = lexrc.getString();
215 lexrc.printError("BN_UNBIND: missing command");
220 FuncRequest func = lyxaction.lookupFunc(cmd);
221 if (func. action == LFUN_UNKNOWN_ACTION) {
222 lexrc.printError("BN_UNBIND: Unknown LyX"
223 " function `$$Token'");
229 unbind_map->bind(seq, func);
236 string const tmp(lexrc.getString());
237 error |= !read(tmp, unbind_map);
239 lexrc.printError("BN_BINDFILE: Missing file name");
249 lyxerr << "KeyMap::read: error while reading bind file:"
255 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
257 ofstream os(bind_file.c_str(),
258 append ? (ios::app | ios:: out): ios::out);
261 os << "## This file is automatically generated by lyx\n"
262 << "## All modifications will be lost\n\n";
264 string tag = unbind ? "\\unbind" : "\\bind";
265 BindingList const list = listBindings(false);
266 BindingList::const_iterator it = list.begin();
267 BindingList::const_iterator it_end = list.end();
268 for (; it != it_end; ++it) {
269 kb_action action = it->request.action;
270 string arg = to_utf8(it->request.argument());
273 << to_utf8(it->sequence.print(KeySequence::BindFile))
275 << lyxaction.getActionName(action)
276 << (arg.empty() ? "" : " ") << arg
284 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
285 KeyModifier mod, KeySequence * seq) const
287 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
290 seq->curmap = seq->stdmap;
295 Table::const_iterator end = table.end();
296 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
297 KeyModifier mask = cit->mod.second;
298 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
300 if (cit->code == key && cit->mod.first == check) {
302 if (cit->table.get()) {
303 // this is a prefix key - set new map
304 seq->curmap = cit->table.get();
305 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
308 // final key - reset map
309 seq->curmap = seq->stdmap;
316 // error - key not found:
317 seq->curmap = seq->stdmap;
324 docstring const KeyMap::print(bool forgui) const
327 Table::const_iterator end = table.end();
328 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
329 buf += cit->code.print(cit->mod.first, forgui);
336 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
338 KeySymbol code = seq->sequence[r];
342 KeyModifier const mod1 = seq->modifiers[r].first;
343 KeyModifier const mod2 = seq->modifiers[r].second;
345 // check if key is already there
346 Table::iterator end = table.end();
347 for (Table::iterator it = table.begin(); it != end; ++it) {
349 && mod1 == it->mod.first
350 && mod2 == it->mod.second) {
352 if (r + 1 == seq->length()) {
353 LYXERR(Debug::KBMAP, "Warning: New binding for '"
354 << to_utf8(seq->print(KeySequence::Portable))
355 << "' is overriding old binding...");
356 if (it->table.get()) {
360 it->func.origin = FuncRequest::KEYBOARD;
362 } else if (!it->table.get()) {
363 lyxerr << "Error: New binding for '"
364 << to_utf8(seq->print(KeySequence::Portable))
365 << "' is overriding old binding..."
369 it->table->bind(seq, func, r + 1);
375 Table::iterator newone = table.insert(table.end(), Key());
377 newone->mod = seq->modifiers[r];
378 if (r + 1 == seq->length()) {
380 newone->func.origin = FuncRequest::KEYBOARD;
381 newone->table.reset();
383 newone->table.reset(new KeyMap);
384 newone->table->bind(seq, func, r + 1);
389 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
391 KeySymbol code = seq->sequence[r];
395 KeyModifier const mod1 = seq->modifiers[r].first;
396 KeyModifier const mod2 = seq->modifiers[r].second;
398 // check if key is already there
399 Table::iterator end = table.end();
400 Table::iterator remove = end;
401 for (Table::iterator it = table.begin(); it != end; ++it) {
403 && mod1 == it->mod.first
404 && mod2 == it->mod.second) {
406 if (r + 1 == seq->length()) {
407 if (it->func == func) {
412 } else if (it->table.get()) {
413 it->table->unbind(seq, func, r + 1);
414 if (it->table->empty())
427 docstring KeyMap::printBindings(FuncRequest const & func) const
429 Bindings bindings = findBindings(func);
430 if (bindings.empty())
433 odocstringstream res;
434 Bindings::const_iterator cit = bindings.begin();
435 Bindings::const_iterator cit_end = bindings.end();
436 // prin the first item
437 res << cit->print(KeySequence::ForGui);
438 // more than one shortcuts?
439 for (++cit; cit != cit_end; ++cit)
440 res << ", " << cit->print(KeySequence::ForGui);
445 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
447 return findBindings(func, KeySequence(0, 0));
451 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
452 KeySequence const & prefix) const
458 Table::const_iterator end = table.end();
459 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
460 if (cit->table.get()) {
461 KeySequence seq = prefix;
462 seq.addkey(cit->code, cit->mod.first);
463 Bindings res2 = cit->table->findBindings(func, seq);
464 res.insert(res.end(), res2.begin(), res2.end());
465 } else if (cit->func == func) {
466 KeySequence seq = prefix;
467 seq.addkey(cit->code, cit->mod.first);
476 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
479 listBindings(list, KeySequence(0, 0), tag);
481 LyXAction::const_func_iterator fit = lyxaction.func_begin();
482 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
483 for (; fit != fit_end; ++fit) {
484 kb_action action = fit->second;
485 bool has_action = false;
486 BindingList::const_iterator it = list.begin();
487 BindingList::const_iterator it_end = list.end();
488 for (; it != it_end; ++it)
489 if (it->request.action == action) {
494 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
501 void KeyMap::listBindings(BindingList & list,
502 KeySequence const & prefix, int tag) const
504 Table::const_iterator it = table.begin();
505 Table::const_iterator it_end = table.end();
506 for (; it != it_end; ++it) {
507 // a LFUN_COMMAND_PREFIX
508 if (it->table.get()) {
509 KeySequence seq = prefix;
510 seq.addkey(it->code, it->mod.first);
511 it->table->listBindings(list, seq, tag);
513 KeySequence seq = prefix;
514 seq.addkey(it->code, it->mod.first);
515 list.push_back(Binding(it->func, seq, tag));