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 LexerKeyword 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 Lexer lexrc(bindTags);
140 if (lyxerr.debugging(Debug::PARSER))
141 lexrc.printTable(lyxerr);
143 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
146 lyxerr << "KeyMap::read: cannot open bind file:"
151 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
154 while (lexrc.isOK()) {
155 switch (lexrc.lex()) {
156 case Lexer::LEX_UNDEF:
157 lexrc.printError("Unknown tag `$$Token'");
160 case Lexer::LEX_FEOF:
167 seq = lexrc.getString();
169 lexrc.printError("BN_BIND: Missing key sequence");
174 if (lexrc.next(true)) {
175 cmd = lexrc.getString();
177 lexrc.printError("BN_BIND: missing command");
182 FuncRequest func = lyxaction.lookupFunc(cmd);
183 if (func. action == LFUN_UNKNOWN_ACTION) {
184 lexrc.printError("BN_BIND: Unknown LyX"
185 " function `$$Token'");
198 seq = lexrc.getString();
200 lexrc.printError("BN_UNBIND: Missing key sequence");
205 if (lexrc.next(true)) {
206 cmd = lexrc.getString();
208 lexrc.printError("BN_UNBIND: missing command");
213 FuncRequest func = lyxaction.lookupFunc(cmd);
214 if (func. action == LFUN_UNKNOWN_ACTION) {
215 lexrc.printError("BN_UNBIND: Unknown LyX"
216 " function `$$Token'");
222 unbind_map->bind(seq, func);
229 string const tmp(lexrc.getString());
230 error |= !read(tmp, unbind_map);
232 lexrc.printError("BN_BINDFILE: Missing file name");
242 lyxerr << "KeyMap::read: error while reading bind file:"
248 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
250 ofstream os(bind_file.c_str(),
251 append ? (ios::app | ios::out) : ios::out);
254 os << "## This file is automatically generated by lyx\n"
255 << "## All modifications will be lost\n\n";
257 string tag = unbind ? "\\unbind" : "\\bind";
258 BindingList const list = listBindings(false);
259 BindingList::const_iterator it = list.begin();
260 BindingList::const_iterator it_end = list.end();
261 for (; it != it_end; ++it) {
262 FuncCode action = it->request.action;
263 string arg = to_utf8(it->request.argument());
266 << to_utf8(it->sequence.print(KeySequence::BindFile))
268 << lyxaction.getActionName(action)
269 << (arg.empty() ? "" : " ") << arg
277 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
278 KeyModifier mod, KeySequence * seq) const
280 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
283 seq->curmap = seq->stdmap;
288 Table::const_iterator end = table.end();
289 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
290 KeyModifier mask = cit->mod.second;
291 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
293 if (cit->code == key && cit->mod.first == check) {
295 if (cit->table.get()) {
296 // this is a prefix key - set new map
297 seq->curmap = cit->table.get();
298 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
301 // final key - reset map
302 seq->curmap = seq->stdmap;
309 // error - key not found:
310 seq->curmap = seq->stdmap;
317 docstring const KeyMap::print(bool forgui) const
320 Table::const_iterator end = table.end();
321 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
322 buf += cit->code.print(cit->mod.first, forgui);
329 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
331 KeySymbol code = seq->sequence[r];
335 KeyModifier const mod1 = seq->modifiers[r].first;
336 KeyModifier const mod2 = seq->modifiers[r].second;
338 // check if key is already there
339 Table::iterator end = table.end();
340 for (Table::iterator it = table.begin(); it != end; ++it) {
342 && mod1 == it->mod.first
343 && mod2 == it->mod.second) {
345 if (r + 1 == seq->length()) {
346 LYXERR(Debug::KBMAP, "Warning: New binding for '"
347 << to_utf8(seq->print(KeySequence::Portable))
348 << "' is overriding old binding...");
349 if (it->table.get()) {
353 it->func.origin = FuncRequest::KEYBOARD;
355 } else if (!it->table.get()) {
356 lyxerr << "Error: New binding for '"
357 << to_utf8(seq->print(KeySequence::Portable))
358 << "' is overriding old binding..."
362 it->table->bind(seq, func, r + 1);
368 Table::iterator newone = table.insert(table.end(), Key());
370 newone->mod = seq->modifiers[r];
371 if (r + 1 == seq->length()) {
373 newone->func.origin = FuncRequest::KEYBOARD;
374 newone->table.reset();
376 newone->table.reset(new KeyMap);
377 newone->table->bind(seq, func, r + 1);
382 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
384 KeySymbol code = seq->sequence[r];
388 KeyModifier const mod1 = seq->modifiers[r].first;
389 KeyModifier const mod2 = seq->modifiers[r].second;
391 // check if key is already there
392 Table::iterator end = table.end();
393 Table::iterator remove = end;
394 for (Table::iterator it = table.begin(); it != end; ++it) {
396 && mod1 == it->mod.first
397 && mod2 == it->mod.second) {
399 if (r + 1 == seq->length()) {
400 if (it->func == func) {
405 } else if (it->table.get()) {
406 it->table->unbind(seq, func, r + 1);
407 if (it->table->empty())
420 docstring KeyMap::printBindings(FuncRequest const & func) const
422 Bindings bindings = findBindings(func);
423 if (bindings.empty())
426 odocstringstream res;
427 Bindings::const_iterator cit = bindings.begin();
428 Bindings::const_iterator cit_end = bindings.end();
429 // prin the first item
430 res << cit->print(KeySequence::ForGui);
431 // more than one shortcuts?
432 for (++cit; cit != cit_end; ++cit)
433 res << ", " << cit->print(KeySequence::ForGui);
438 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
440 return findBindings(func, KeySequence(0, 0));
444 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
445 KeySequence const & prefix) const
451 Table::const_iterator end = table.end();
452 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
453 if (cit->table.get()) {
454 KeySequence seq = prefix;
455 seq.addkey(cit->code, cit->mod.first);
456 Bindings res2 = cit->table->findBindings(func, seq);
457 res.insert(res.end(), res2.begin(), res2.end());
458 } else if (cit->func == func) {
459 KeySequence seq = prefix;
460 seq.addkey(cit->code, cit->mod.first);
469 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
472 listBindings(list, KeySequence(0, 0), tag);
474 LyXAction::const_func_iterator fit = lyxaction.func_begin();
475 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
476 for (; fit != fit_end; ++fit) {
477 FuncCode action = fit->second;
478 bool has_action = false;
479 BindingList::const_iterator it = list.begin();
480 BindingList::const_iterator it_end = list.end();
481 for (; it != it_end; ++it)
482 if (it->request.action == action) {
487 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
494 void KeyMap::listBindings(BindingList & list,
495 KeySequence const & prefix, int tag) const
497 Table::const_iterator it = table.begin();
498 Table::const_iterator it_end = table.end();
499 for (; it != it_end; ++it) {
500 // a LFUN_COMMAND_PREFIX
501 if (it->table.get()) {
502 KeySequence seq = prefix;
503 seq.addkey(it->code, it->mod.first);
504 it->table->listBindings(list, seq, tag);
506 KeySequence seq = prefix;
507 seq.addkey(it->code, it->mod.first);
508 list.push_back(Binding(it->func, seq, tag));