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 function `$$Token'");
199 seq = lexrc.getString();
201 lexrc.printError("BN_UNBIND: Missing key sequence");
206 if (lexrc.next(true)) {
207 cmd = lexrc.getString();
209 lexrc.printError("BN_UNBIND: missing command");
214 FuncRequest func = lyxaction.lookupFunc(cmd);
215 if (func. action == LFUN_UNKNOWN_ACTION) {
216 lexrc.printError("BN_UNBIND: Unknown LyX"
217 " function `$$Token'");
223 unbind_map->bind(seq, func);
230 string const tmp(lexrc.getString());
231 error |= !read(tmp, unbind_map);
233 lexrc.printError("BN_BINDFILE: Missing file name");
243 lyxerr << "KeyMap::read: error while reading bind file:"
249 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
251 ofstream os(bind_file.c_str(),
252 append ? (ios::app | ios::out) : ios::out);
255 os << "## This file is automatically generated by lyx\n"
256 << "## All modifications will be lost\n\n";
258 string tag = unbind ? "\\unbind" : "\\bind";
259 BindingList const list = listBindings(false);
260 BindingList::const_iterator it = list.begin();
261 BindingList::const_iterator it_end = list.end();
262 for (; it != it_end; ++it) {
263 FuncCode action = it->request.action;
264 string arg = to_utf8(it->request.argument());
267 << to_utf8(it->sequence.print(KeySequence::BindFile))
269 << lyxaction.getActionName(action)
270 << (arg.empty() ? "" : " ") << arg
278 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
279 KeyModifier mod, KeySequence * seq) const
281 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
284 seq->curmap = seq->stdmap;
289 Table::const_iterator end = table.end();
290 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
291 KeyModifier mask = cit->mod.second;
292 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
294 if (cit->code == key && cit->mod.first == check) {
296 if (cit->table.get()) {
297 // this is a prefix key - set new map
298 seq->curmap = cit->table.get();
299 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
302 // final key - reset map
303 seq->curmap = seq->stdmap;
310 // error - key not found:
311 seq->curmap = seq->stdmap;
318 docstring const KeyMap::print(bool forgui) const
321 Table::const_iterator end = table.end();
322 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
323 buf += cit->code.print(cit->mod.first, forgui);
330 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
332 KeySymbol code = seq->sequence[r];
336 KeyModifier const mod1 = seq->modifiers[r].first;
337 KeyModifier const mod2 = seq->modifiers[r].second;
339 // check if key is already there
340 Table::iterator end = table.end();
341 for (Table::iterator it = table.begin(); it != end; ++it) {
343 && mod1 == it->mod.first
344 && mod2 == it->mod.second) {
346 if (r + 1 == seq->length()) {
347 LYXERR(Debug::KBMAP, "Warning: New binding for '"
348 << to_utf8(seq->print(KeySequence::Portable))
349 << "' is overriding old binding...");
350 if (it->table.get()) {
354 it->func.origin = FuncRequest::KEYBOARD;
356 } else if (!it->table.get()) {
357 lyxerr << "Error: New binding for '"
358 << to_utf8(seq->print(KeySequence::Portable))
359 << "' is overriding old binding..."
363 it->table->bind(seq, func, r + 1);
369 Table::iterator newone = table.insert(table.end(), Key());
371 newone->mod = seq->modifiers[r];
372 if (r + 1 == seq->length()) {
374 newone->func.origin = FuncRequest::KEYBOARD;
375 newone->table.reset();
377 newone->table.reset(new KeyMap);
378 newone->table->bind(seq, func, r + 1);
383 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
385 KeySymbol code = seq->sequence[r];
389 KeyModifier const mod1 = seq->modifiers[r].first;
390 KeyModifier const mod2 = seq->modifiers[r].second;
392 // check if key is already there
393 Table::iterator end = table.end();
394 Table::iterator remove = end;
395 for (Table::iterator it = table.begin(); it != end; ++it) {
397 && mod1 == it->mod.first
398 && mod2 == it->mod.second) {
400 if (r + 1 == seq->length()) {
401 if (it->func == func) {
406 } else if (it->table.get()) {
407 it->table->unbind(seq, func, r + 1);
408 if (it->table->empty())
421 docstring KeyMap::printBindings(FuncRequest const & func) const
423 Bindings bindings = findBindings(func);
424 if (bindings.empty())
427 odocstringstream res;
428 Bindings::const_iterator cit = bindings.begin();
429 Bindings::const_iterator cit_end = bindings.end();
430 // prin the first item
431 res << cit->print(KeySequence::ForGui);
432 // more than one shortcuts?
433 for (++cit; cit != cit_end; ++cit)
434 res << ", " << cit->print(KeySequence::ForGui);
439 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
441 return findBindings(func, KeySequence(0, 0));
445 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
446 KeySequence const & prefix) const
452 Table::const_iterator end = table.end();
453 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
454 if (cit->table.get()) {
455 KeySequence seq = prefix;
456 seq.addkey(cit->code, cit->mod.first);
457 Bindings res2 = cit->table->findBindings(func, seq);
458 res.insert(res.end(), res2.begin(), res2.end());
459 } else if (cit->func == func) {
460 KeySequence seq = prefix;
461 seq.addkey(cit->code, cit->mod.first);
470 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
473 listBindings(list, KeySequence(0, 0), tag);
475 LyXAction::const_func_iterator fit = lyxaction.func_begin();
476 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
477 for (; fit != fit_end; ++fit) {
478 FuncCode action = fit->second;
479 bool has_action = false;
480 BindingList::const_iterator it = list.begin();
481 BindingList::const_iterator it_end = list.end();
482 for (; it != it_end; ++it)
483 if (it->request.action == action) {
488 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
495 void KeyMap::listBindings(BindingList & list,
496 KeySequence const & prefix, int tag) const
498 Table::const_iterator it = table.begin();
499 Table::const_iterator it_end = table.end();
500 for (; it != it_end; ++it) {
501 // a LFUN_COMMAND_PREFIX
502 if (it->table.get()) {
503 KeySequence seq = prefix;
504 seq.addkey(it->code, it->mod.first);
505 it->table->listBindings(list, seq, tag);
507 KeySequence seq = prefix;
508 seq.addkey(it->code, it->mod.first);
509 list.push_back(Binding(it->func, seq, tag));