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 LYXERR0("KeyMap::read: cannot open bind file:" << tmp);
145 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
148 while (lexrc.isOK()) {
149 switch (lexrc.lex()) {
151 case Lexer::LEX_UNDEF:
152 lexrc.printError("Unknown tag `$$Token'");
156 case Lexer::LEX_FEOF:
161 lexrc.printError("BN_BIND: Missing key sequence");
165 string seq = lexrc.getString();
167 if (!lexrc.next(true)) {
168 lexrc.printError("BN_BIND: missing command");
172 string cmd = lexrc.getString();
174 FuncRequest func = lyxaction.lookupFunc(cmd);
175 if (func.action == LFUN_UNKNOWN_ACTION) {
176 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
187 lexrc.printError("BN_UNBIND: Missing key sequence");
191 string seq = lexrc.getString();
193 if (!lexrc.next(true)) {
194 lexrc.printError("BN_UNBIND: missing command");
198 string cmd = lexrc.getString();
200 FuncRequest func = lyxaction.lookupFunc(cmd);
201 if (func.action == LFUN_UNKNOWN_ACTION) {
202 lexrc.printError("BN_UNBIND: Unknown LyX"
203 " function `$$Token'");
209 unbind_map->bind(seq, func);
217 lexrc.printError("BN_BINDFILE: Missing file name");
221 string const tmp = lexrc.getString();
222 error |= !read(tmp, unbind_map);
228 LYXERR0("KeyMap::read: error while reading bind file:" << tmp);
233 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
235 ofstream os(bind_file.c_str(),
236 append ? (ios::app | ios::out) : ios::out);
239 os << "## This file is automatically generated by lyx\n"
240 << "## All modifications will be lost\n\n";
242 string tag = unbind ? "\\unbind" : "\\bind";
243 BindingList const list = listBindings(false);
244 BindingList::const_iterator it = list.begin();
245 BindingList::const_iterator it_end = list.end();
246 for (; it != it_end; ++it) {
247 FuncCode action = it->request.action;
248 string arg = to_utf8(it->request.argument());
251 << to_utf8(it->sequence.print(KeySequence::BindFile))
253 << lyxaction.getActionName(action)
254 << (arg.empty() ? "" : " ") << arg
262 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
263 KeyModifier mod, KeySequence * seq) const
265 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
268 seq->curmap = seq->stdmap;
273 Table::const_iterator end = table.end();
274 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
275 KeyModifier mask = cit->mod.second;
276 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
278 if (cit->code == key && cit->mod.first == check) {
280 if (cit->table.get()) {
281 // this is a prefix key - set new map
282 seq->curmap = cit->table.get();
283 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
286 // final key - reset map
287 seq->curmap = seq->stdmap;
294 // error - key not found:
295 seq->curmap = seq->stdmap;
302 docstring const KeyMap::print(bool forgui) const
305 Table::const_iterator end = table.end();
306 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
307 buf += cit->code.print(cit->mod.first, forgui);
314 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
316 KeySymbol code = seq->sequence[r];
320 KeyModifier const mod1 = seq->modifiers[r].first;
321 KeyModifier const mod2 = seq->modifiers[r].second;
323 // check if key is already there
324 Table::iterator end = table.end();
325 for (Table::iterator it = table.begin(); it != end; ++it) {
327 && mod1 == it->mod.first
328 && mod2 == it->mod.second) {
330 if (r + 1 == seq->length()) {
331 LYXERR(Debug::KBMAP, "Warning: New binding for '"
332 << to_utf8(seq->print(KeySequence::Portable))
333 << "' is overriding old binding...");
334 if (it->table.get()) {
338 it->func.origin = FuncRequest::KEYBOARD;
340 } else if (!it->table.get()) {
341 lyxerr << "Error: New binding for '"
342 << to_utf8(seq->print(KeySequence::Portable))
343 << "' is overriding old binding..."
347 it->table->bind(seq, func, r + 1);
353 Table::iterator newone = table.insert(table.end(), Key());
355 newone->mod = seq->modifiers[r];
356 if (r + 1 == seq->length()) {
358 newone->func.origin = FuncRequest::KEYBOARD;
359 newone->table.reset();
361 newone->table.reset(new KeyMap);
362 newone->table->bind(seq, func, r + 1);
367 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
369 KeySymbol code = seq->sequence[r];
373 KeyModifier const mod1 = seq->modifiers[r].first;
374 KeyModifier const mod2 = seq->modifiers[r].second;
376 // check if key is already there
377 Table::iterator end = table.end();
378 Table::iterator remove = end;
379 for (Table::iterator it = table.begin(); it != end; ++it) {
381 && mod1 == it->mod.first
382 && mod2 == it->mod.second) {
384 if (r + 1 == seq->length()) {
385 if (it->func == func) {
390 } else if (it->table.get()) {
391 it->table->unbind(seq, func, r + 1);
392 if (it->table->empty())
403 docstring KeyMap::printBindings(FuncRequest const & func) const
405 Bindings bindings = findBindings(func);
406 if (bindings.empty())
409 odocstringstream res;
410 Bindings::const_iterator cit = bindings.begin();
411 Bindings::const_iterator cit_end = bindings.end();
412 // prin the first item
413 res << cit->print(KeySequence::ForGui);
414 // more than one shortcuts?
415 for (++cit; cit != cit_end; ++cit)
416 res << ", " << cit->print(KeySequence::ForGui);
421 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
423 return findBindings(func, KeySequence(0, 0));
427 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
428 KeySequence const & prefix) const
434 Table::const_iterator end = table.end();
435 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
436 if (cit->table.get()) {
437 KeySequence seq = prefix;
438 seq.addkey(cit->code, cit->mod.first);
439 Bindings res2 = cit->table->findBindings(func, seq);
440 res.insert(res.end(), res2.begin(), res2.end());
441 } else if (cit->func == func) {
442 KeySequence seq = prefix;
443 seq.addkey(cit->code, cit->mod.first);
452 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
455 listBindings(list, KeySequence(0, 0), tag);
457 LyXAction::const_func_iterator fit = lyxaction.func_begin();
458 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
459 for (; fit != fit_end; ++fit) {
460 FuncCode action = fit->second;
461 bool has_action = false;
462 BindingList::const_iterator it = list.begin();
463 BindingList::const_iterator it_end = list.end();
464 for (; it != it_end; ++it)
465 if (it->request.action == action) {
470 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
477 void KeyMap::listBindings(BindingList & list,
478 KeySequence const & prefix, int tag) const
480 Table::const_iterator it = table.begin();
481 Table::const_iterator it_end = table.end();
482 for (; it != it_end; ++it) {
483 // a LFUN_COMMAND_PREFIX
484 if (it->table.get()) {
485 KeySequence seq = prefix;
486 seq.addkey(it->code, it->mod.first);
487 it->table->listBindings(list, seq, tag);
489 KeySequence seq = prefix;
490 seq.addkey(it->code, it->mod.first);
491 list.push_back(Binding(it->func, seq, tag));