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
266 seq->curmap = seq->stdmap;
268 return FuncRequest::unknown;
271 Table::const_iterator end = table.end();
272 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
273 KeyModifier mask = cit->mod.second;
274 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
276 if (cit->code == key && cit->mod.first == check) {
278 if (cit->table.get()) {
279 // this is a prefix key - set new map
280 seq->curmap = cit->table.get();
281 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
284 // final key - reset map
285 seq->curmap = seq->stdmap;
292 // error - key not found:
293 seq->curmap = seq->stdmap;
296 return FuncRequest::unknown;
300 docstring const KeyMap::print(bool forgui) const
303 Table::const_iterator end = table.end();
304 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
305 buf += cit->code.print(cit->mod.first, forgui);
312 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
314 KeySymbol code = seq->sequence[r];
318 KeyModifier const mod1 = seq->modifiers[r].first;
319 KeyModifier const mod2 = seq->modifiers[r].second;
321 // check if key is already there
322 Table::iterator end = table.end();
323 for (Table::iterator it = table.begin(); it != end; ++it) {
325 && mod1 == it->mod.first
326 && mod2 == it->mod.second) {
328 if (r + 1 == seq->length()) {
329 LYXERR(Debug::KBMAP, "Warning: New binding for '"
330 << to_utf8(seq->print(KeySequence::Portable))
331 << "' is overriding old binding...");
332 if (it->table.get()) {
336 it->func.origin = FuncRequest::KEYBOARD;
338 } else if (!it->table.get()) {
339 lyxerr << "Error: New binding for '"
340 << to_utf8(seq->print(KeySequence::Portable))
341 << "' is overriding old binding..."
345 it->table->bind(seq, func, r + 1);
351 Table::iterator newone = table.insert(table.end(), Key());
353 newone->mod = seq->modifiers[r];
354 if (r + 1 == seq->length()) {
356 newone->func.origin = FuncRequest::KEYBOARD;
357 newone->table.reset();
359 newone->table.reset(new KeyMap);
360 newone->table->bind(seq, func, r + 1);
365 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
367 KeySymbol code = seq->sequence[r];
371 KeyModifier const mod1 = seq->modifiers[r].first;
372 KeyModifier const mod2 = seq->modifiers[r].second;
374 // check if key is already there
375 Table::iterator end = table.end();
376 Table::iterator remove = end;
377 for (Table::iterator it = table.begin(); it != end; ++it) {
379 && mod1 == it->mod.first
380 && mod2 == it->mod.second) {
382 if (r + 1 == seq->length()) {
383 if (it->func == func) {
388 } else if (it->table.get()) {
389 it->table->unbind(seq, func, r + 1);
390 if (it->table->empty())
401 docstring KeyMap::printBindings(FuncRequest const & func) const
403 Bindings bindings = findBindings(func);
404 if (bindings.empty())
407 odocstringstream res;
408 Bindings::const_iterator cit = bindings.begin();
409 Bindings::const_iterator cit_end = bindings.end();
410 // prin the first item
411 res << cit->print(KeySequence::ForGui);
412 // more than one shortcuts?
413 for (++cit; cit != cit_end; ++cit)
414 res << ", " << cit->print(KeySequence::ForGui);
419 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
421 return findBindings(func, KeySequence(0, 0));
425 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
426 KeySequence const & prefix) const
432 Table::const_iterator end = table.end();
433 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
434 if (cit->table.get()) {
435 KeySequence seq = prefix;
436 seq.addkey(cit->code, cit->mod.first);
437 Bindings res2 = cit->table->findBindings(func, seq);
438 res.insert(res.end(), res2.begin(), res2.end());
439 } else if (cit->func == func) {
440 KeySequence seq = prefix;
441 seq.addkey(cit->code, cit->mod.first);
450 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
453 listBindings(list, KeySequence(0, 0), tag);
455 LyXAction::const_func_iterator fit = lyxaction.func_begin();
456 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
457 for (; fit != fit_end; ++fit) {
458 FuncCode action = fit->second;
459 bool has_action = false;
460 BindingList::const_iterator it = list.begin();
461 BindingList::const_iterator it_end = list.end();
462 for (; it != it_end; ++it)
463 if (it->request.action == action) {
468 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
475 void KeyMap::listBindings(BindingList & list,
476 KeySequence const & prefix, KeyMap::ItemType tag) const
478 Table::const_iterator it = table.begin();
479 Table::const_iterator it_end = table.end();
480 for (; it != it_end; ++it) {
481 // a LFUN_COMMAND_PREFIX
482 if (it->table.get()) {
483 KeySequence seq = prefix;
484 seq.addkey(it->code, it->mod.first);
485 it->table->listBindings(list, seq, tag);
487 KeySequence seq = prefix;
488 seq.addkey(it->code, it->mod.first);
489 list.push_back(Binding(it->func, seq, tag));