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);
120 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
128 LexerKeyword bindTags[] = {
129 { "\\bind", BN_BIND },
130 { "\\bind_file", BN_BINDFILE },
131 { "\\unbind", BN_UNBIND },
134 Lexer lexrc(bindTags);
135 if (lyxerr.debugging(Debug::PARSER))
136 lexrc.printTable(lyxerr);
138 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
141 lyxerr << "KeyMap::read: cannot open bind file:"
146 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
149 while (lexrc.isOK()) {
150 switch (lexrc.lex()) {
151 case Lexer::LEX_UNDEF:
152 lexrc.printError("Unknown tag `$$Token'");
155 case Lexer::LEX_FEOF:
162 seq = lexrc.getString();
164 lexrc.printError("BN_BIND: Missing key sequence");
169 if (lexrc.next(true)) {
170 cmd = lexrc.getString();
172 lexrc.printError("BN_BIND: missing command");
177 FuncRequest func = lyxaction.lookupFunc(cmd);
178 if (func. action == LFUN_UNKNOWN_ACTION) {
179 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
192 seq = lexrc.getString();
194 lexrc.printError("BN_UNBIND: Missing key sequence");
199 if (lexrc.next(true)) {
200 cmd = lexrc.getString();
202 lexrc.printError("BN_UNBIND: missing command");
207 FuncRequest func = lyxaction.lookupFunc(cmd);
208 if (func. action == LFUN_UNKNOWN_ACTION) {
209 lexrc.printError("BN_UNBIND: Unknown LyX"
210 " function `$$Token'");
216 unbind_map->bind(seq, func);
223 string const tmp(lexrc.getString());
224 error |= !read(tmp, unbind_map);
226 lexrc.printError("BN_BINDFILE: Missing file name");
236 lyxerr << "KeyMap::read: error while reading bind file:"
242 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
244 ofstream os(bind_file.c_str(),
245 append ? (ios::app | ios::out) : ios::out);
248 os << "## This file is automatically generated by lyx\n"
249 << "## All modifications will be lost\n\n";
251 string tag = unbind ? "\\unbind" : "\\bind";
252 BindingList const list = listBindings(false);
253 BindingList::const_iterator it = list.begin();
254 BindingList::const_iterator it_end = list.end();
255 for (; it != it_end; ++it) {
256 FuncCode action = it->request.action;
257 string arg = to_utf8(it->request.argument());
260 << to_utf8(it->sequence.print(KeySequence::BindFile))
262 << lyxaction.getActionName(action)
263 << (arg.empty() ? "" : " ") << arg
271 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
272 KeyModifier mod, KeySequence * seq) const
274 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
277 seq->curmap = seq->stdmap;
282 Table::const_iterator end = table.end();
283 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
284 KeyModifier mask = cit->mod.second;
285 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
287 if (cit->code == key && cit->mod.first == check) {
289 if (cit->table.get()) {
290 // this is a prefix key - set new map
291 seq->curmap = cit->table.get();
292 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
295 // final key - reset map
296 seq->curmap = seq->stdmap;
303 // error - key not found:
304 seq->curmap = seq->stdmap;
311 docstring const KeyMap::print(bool forgui) const
314 Table::const_iterator end = table.end();
315 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
316 buf += cit->code.print(cit->mod.first, forgui);
323 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
325 KeySymbol code = seq->sequence[r];
329 KeyModifier const mod1 = seq->modifiers[r].first;
330 KeyModifier const mod2 = seq->modifiers[r].second;
332 // check if key is already there
333 Table::iterator end = table.end();
334 for (Table::iterator it = table.begin(); it != end; ++it) {
336 && mod1 == it->mod.first
337 && mod2 == it->mod.second) {
339 if (r + 1 == seq->length()) {
340 LYXERR(Debug::KBMAP, "Warning: New binding for '"
341 << to_utf8(seq->print(KeySequence::Portable))
342 << "' is overriding old binding...");
343 if (it->table.get()) {
347 it->func.origin = FuncRequest::KEYBOARD;
349 } else if (!it->table.get()) {
350 lyxerr << "Error: New binding for '"
351 << to_utf8(seq->print(KeySequence::Portable))
352 << "' is overriding old binding..."
356 it->table->bind(seq, func, r + 1);
362 Table::iterator newone = table.insert(table.end(), Key());
364 newone->mod = seq->modifiers[r];
365 if (r + 1 == seq->length()) {
367 newone->func.origin = FuncRequest::KEYBOARD;
368 newone->table.reset();
370 newone->table.reset(new KeyMap);
371 newone->table->bind(seq, func, r + 1);
376 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
378 KeySymbol code = seq->sequence[r];
382 KeyModifier const mod1 = seq->modifiers[r].first;
383 KeyModifier const mod2 = seq->modifiers[r].second;
385 // check if key is already there
386 Table::iterator end = table.end();
387 Table::iterator remove = end;
388 for (Table::iterator it = table.begin(); it != end; ++it) {
390 && mod1 == it->mod.first
391 && mod2 == it->mod.second) {
393 if (r + 1 == seq->length()) {
394 if (it->func == func) {
399 } else if (it->table.get()) {
400 it->table->unbind(seq, func, r + 1);
401 if (it->table->empty())
414 docstring KeyMap::printBindings(FuncRequest const & func) const
416 Bindings bindings = findBindings(func);
417 if (bindings.empty())
420 odocstringstream res;
421 Bindings::const_iterator cit = bindings.begin();
422 Bindings::const_iterator cit_end = bindings.end();
423 // prin the first item
424 res << cit->print(KeySequence::ForGui);
425 // more than one shortcuts?
426 for (++cit; cit != cit_end; ++cit)
427 res << ", " << cit->print(KeySequence::ForGui);
432 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
434 return findBindings(func, KeySequence(0, 0));
438 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
439 KeySequence const & prefix) const
445 Table::const_iterator end = table.end();
446 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
447 if (cit->table.get()) {
448 KeySequence seq = prefix;
449 seq.addkey(cit->code, cit->mod.first);
450 Bindings res2 = cit->table->findBindings(func, seq);
451 res.insert(res.end(), res2.begin(), res2.end());
452 } else if (cit->func == func) {
453 KeySequence seq = prefix;
454 seq.addkey(cit->code, cit->mod.first);
463 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
466 listBindings(list, KeySequence(0, 0), tag);
468 LyXAction::const_func_iterator fit = lyxaction.func_begin();
469 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
470 for (; fit != fit_end; ++fit) {
471 FuncCode action = fit->second;
472 bool has_action = false;
473 BindingList::const_iterator it = list.begin();
474 BindingList::const_iterator it_end = list.end();
475 for (; it != it_end; ++it)
476 if (it->request.action == action) {
481 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
488 void KeyMap::listBindings(BindingList & list,
489 KeySequence const & prefix, int tag) const
491 Table::const_iterator it = table.begin();
492 Table::const_iterator it_end = table.end();
493 for (; it != it_end; ++it) {
494 // a LFUN_COMMAND_PREFIX
495 if (it->table.get()) {
496 KeySequence seq = prefix;
497 seq.addkey(it->code, it->mod.first);
498 it->table->listBindings(list, seq, tag);
500 KeySequence seq = prefix;
501 seq.addkey(it->code, it->mod.first);
502 list.push_back(Binding(it->func, seq, tag));