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 keyword_item 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 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
141 Lexer lexrc(bindTags, bindCount);
142 if (lyxerr.debugging(Debug::PARSER))
143 lexrc.printTable(lyxerr);
145 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
148 lyxerr << "KeyMap::read: cannot open bind file:"
153 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
156 while (lexrc.isOK()) {
157 switch (lexrc.lex()) {
158 case Lexer::LEX_UNDEF:
159 lexrc.printError("Unknown tag `$$Token'");
162 case Lexer::LEX_FEOF:
169 seq = lexrc.getString();
171 lexrc.printError("BN_BIND: Missing key sequence");
176 if (lexrc.next(true)) {
177 cmd = lexrc.getString();
179 lexrc.printError("BN_BIND: missing command");
184 FuncRequest func = lyxaction.lookupFunc(cmd);
185 if (func. action == LFUN_UNKNOWN_ACTION) {
186 lexrc.printError("BN_BIND: Unknown LyX"
187 " function `$$Token'");
200 seq = lexrc.getString();
202 lexrc.printError("BN_UNBIND: Missing key sequence");
207 if (lexrc.next(true)) {
208 cmd = lexrc.getString();
210 lexrc.printError("BN_UNBIND: missing command");
215 FuncRequest func = lyxaction.lookupFunc(cmd);
216 if (func. action == LFUN_UNKNOWN_ACTION) {
217 lexrc.printError("BN_UNBIND: Unknown LyX"
218 " function `$$Token'");
224 unbind_map->bind(seq, func);
231 string const tmp(lexrc.getString());
232 error |= !read(tmp, unbind_map);
234 lexrc.printError("BN_BINDFILE: Missing file name");
244 lyxerr << "KeyMap::read: error while reading bind file:"
250 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
252 ofstream os(bind_file.c_str(),
253 append ? (ios::app | ios::out) : ios::out);
256 os << "## This file is automatically generated by lyx\n"
257 << "## All modifications will be lost\n\n";
259 string tag = unbind ? "\\unbind" : "\\bind";
260 BindingList const list = listBindings(false);
261 BindingList::const_iterator it = list.begin();
262 BindingList::const_iterator it_end = list.end();
263 for (; it != it_end; ++it) {
264 FuncCode action = it->request.action;
265 string arg = to_utf8(it->request.argument());
268 << to_utf8(it->sequence.print(KeySequence::BindFile))
270 << lyxaction.getActionName(action)
271 << (arg.empty() ? "" : " ") << arg
279 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
280 KeyModifier mod, KeySequence * seq) const
282 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
285 seq->curmap = seq->stdmap;
290 Table::const_iterator end = table.end();
291 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
292 KeyModifier mask = cit->mod.second;
293 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
295 if (cit->code == key && cit->mod.first == check) {
297 if (cit->table.get()) {
298 // this is a prefix key - set new map
299 seq->curmap = cit->table.get();
300 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
303 // final key - reset map
304 seq->curmap = seq->stdmap;
311 // error - key not found:
312 seq->curmap = seq->stdmap;
319 docstring const KeyMap::print(bool forgui) const
322 Table::const_iterator end = table.end();
323 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
324 buf += cit->code.print(cit->mod.first, forgui);
331 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
333 KeySymbol code = seq->sequence[r];
337 KeyModifier const mod1 = seq->modifiers[r].first;
338 KeyModifier const mod2 = seq->modifiers[r].second;
340 // check if key is already there
341 Table::iterator end = table.end();
342 for (Table::iterator it = table.begin(); it != end; ++it) {
344 && mod1 == it->mod.first
345 && mod2 == it->mod.second) {
347 if (r + 1 == seq->length()) {
348 LYXERR(Debug::KBMAP, "Warning: New binding for '"
349 << to_utf8(seq->print(KeySequence::Portable))
350 << "' is overriding old binding...");
351 if (it->table.get()) {
355 it->func.origin = FuncRequest::KEYBOARD;
357 } else if (!it->table.get()) {
358 lyxerr << "Error: New binding for '"
359 << to_utf8(seq->print(KeySequence::Portable))
360 << "' is overriding old binding..."
364 it->table->bind(seq, func, r + 1);
370 Table::iterator newone = table.insert(table.end(), Key());
372 newone->mod = seq->modifiers[r];
373 if (r + 1 == seq->length()) {
375 newone->func.origin = FuncRequest::KEYBOARD;
376 newone->table.reset();
378 newone->table.reset(new KeyMap);
379 newone->table->bind(seq, func, r + 1);
384 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
386 KeySymbol code = seq->sequence[r];
390 KeyModifier const mod1 = seq->modifiers[r].first;
391 KeyModifier const mod2 = seq->modifiers[r].second;
393 // check if key is already there
394 Table::iterator end = table.end();
395 Table::iterator remove = end;
396 for (Table::iterator it = table.begin(); it != end; ++it) {
398 && mod1 == it->mod.first
399 && mod2 == it->mod.second) {
401 if (r + 1 == seq->length()) {
402 if (it->func == func) {
407 } else if (it->table.get()) {
408 it->table->unbind(seq, func, r + 1);
409 if (it->table->empty())
422 docstring KeyMap::printBindings(FuncRequest const & func) const
424 Bindings bindings = findBindings(func);
425 if (bindings.empty())
428 odocstringstream res;
429 Bindings::const_iterator cit = bindings.begin();
430 Bindings::const_iterator cit_end = bindings.end();
431 // prin the first item
432 res << cit->print(KeySequence::ForGui);
433 // more than one shortcuts?
434 for (++cit; cit != cit_end; ++cit)
435 res << ", " << cit->print(KeySequence::ForGui);
440 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
442 return findBindings(func, KeySequence(0, 0));
446 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
447 KeySequence const & prefix) const
453 Table::const_iterator end = table.end();
454 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
455 if (cit->table.get()) {
456 KeySequence seq = prefix;
457 seq.addkey(cit->code, cit->mod.first);
458 Bindings res2 = cit->table->findBindings(func, seq);
459 res.insert(res.end(), res2.begin(), res2.end());
460 } else if (cit->func == func) {
461 KeySequence seq = prefix;
462 seq.addkey(cit->code, cit->mod.first);
471 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
474 listBindings(list, KeySequence(0, 0), tag);
476 LyXAction::const_func_iterator fit = lyxaction.func_begin();
477 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
478 for (; fit != fit_end; ++fit) {
479 FuncCode action = fit->second;
480 bool has_action = false;
481 BindingList::const_iterator it = list.begin();
482 BindingList::const_iterator it_end = list.end();
483 for (; it != it_end; ++it)
484 if (it->request.action == action) {
489 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
496 void KeyMap::listBindings(BindingList & list,
497 KeySequence const & prefix, int tag) const
499 Table::const_iterator it = table.begin();
500 Table::const_iterator it_end = table.end();
501 for (; it != it_end; ++it) {
502 // a LFUN_COMMAND_PREFIX
503 if (it->table.get()) {
504 KeySequence seq = prefix;
505 seq.addkey(it->code, it->mod.first);
506 it->table->listBindings(list, seq, tag);
508 KeySequence seq = prefix;
509 seq.addkey(it->code, it->mod.first);
510 list.push_back(Binding(it->func, seq, tag));