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 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
90 KeySymbol code = seq->sequence[r];
94 KeyModifier const mod1 = seq->modifiers[r].first;
95 KeyModifier const mod2 = seq->modifiers[r].second;
97 // check if key is already there
98 Table::iterator end = table.end();
99 for (Table::iterator it = table.begin(); it != end; ++it) {
101 && mod1 == it->mod.first
102 && mod2 == it->mod.second) {
104 if (r + 1 == seq->length()) {
105 LYXERR(Debug::KBMAP, "Warning: New binding for '"
106 << to_utf8(seq->print(KeySequence::Portable))
107 << "' is overriding old binding...");
108 if (it->prefixes.get()) {
109 it->prefixes.reset();
112 it->func.origin = FuncRequest::KEYBOARD;
114 } else if (!it->prefixes.get()) {
115 lyxerr << "Error: New binding for '"
116 << to_utf8(seq->print(KeySequence::Portable))
117 << "' is overriding old binding..."
121 it->prefixes->bind(seq, func, r + 1);
127 Table::iterator newone = table.insert(table.end(), Key());
129 newone->mod = seq->modifiers[r];
130 if (r + 1 == seq->length()) {
132 newone->func.origin = FuncRequest::KEYBOARD;
133 newone->prefixes.reset();
135 newone->prefixes.reset(new KeyMap);
136 newone->prefixes->bind(seq, func, r + 1);
141 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
143 KeySymbol code = seq->sequence[r];
147 KeyModifier const mod1 = seq->modifiers[r].first;
148 KeyModifier const mod2 = seq->modifiers[r].second;
150 // check if key is already there
151 Table::iterator end = table.end();
152 Table::iterator remove = end;
153 for (Table::iterator it = table.begin(); it != end; ++it) {
155 && mod1 == it->mod.first
156 && mod2 == it->mod.second) {
158 if (r + 1 == seq->length()) {
159 if (it->func == func) {
161 if (it->prefixes.get())
162 it->prefixes.reset();
164 } else if (it->prefixes.get()) {
165 it->prefixes->unbind(seq, func, r + 1);
166 if (it->prefixes->empty())
177 FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
179 KeySymbol code = seq.sequence[r];
181 return FuncRequest::unknown;
183 KeyModifier const mod1 = seq.modifiers[r].first;
184 KeyModifier const mod2 = seq.modifiers[r].second;
186 // check if key is already there
187 Table::iterator end = table.end();
188 for (Table::iterator it = table.begin(); it != end; ++it) {
190 && mod1 == it->mod.first
191 && mod2 == it->mod.second) {
192 if (r + 1 == seq.length())
194 else if (it->prefixes.get())
195 return it->prefixes->getBinding(seq, r + 1);
198 return FuncRequest::unknown;
208 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
216 LexerKeyword bindTags[] = {
217 { "\\bind", BN_BIND },
218 { "\\bind_file", BN_BINDFILE },
219 { "\\unbind", BN_UNBIND },
222 Lexer lexrc(bindTags);
223 if (lyxerr.debugging(Debug::PARSER))
224 lexrc.printTable(lyxerr);
226 FileName const tmp = i18nLibFileSearch("bind", bind_file, "bind");
229 LYXERR0("KeyMap::read: cannot open bind file:" << tmp);
233 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
236 while (lexrc.isOK()) {
237 switch (lexrc.lex()) {
239 case Lexer::LEX_UNDEF:
240 lexrc.printError("Unknown tag `$$Token'");
244 case Lexer::LEX_FEOF:
249 lexrc.printError("BN_BIND: Missing key sequence");
253 string seq = lexrc.getString();
255 if (!lexrc.next(true)) {
256 lexrc.printError("BN_BIND: missing command");
260 string cmd = lexrc.getString();
262 FuncRequest func = lyxaction.lookupFunc(cmd);
263 if (func.action == LFUN_UNKNOWN_ACTION) {
264 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
275 lexrc.printError("BN_UNBIND: Missing key sequence");
279 string seq = lexrc.getString();
281 if (!lexrc.next(true)) {
282 lexrc.printError("BN_UNBIND: missing command");
286 string cmd = lexrc.getString();
288 FuncRequest func = lyxaction.lookupFunc(cmd);
289 if (func.action == LFUN_UNKNOWN_ACTION) {
290 lexrc.printError("BN_UNBIND: Unknown LyX"
291 " function `$$Token'");
297 unbind_map->bind(seq, func);
305 lexrc.printError("BN_BINDFILE: Missing file name");
309 string const tmp = lexrc.getString();
310 error |= !read(tmp, unbind_map);
316 LYXERR0("KeyMap::read: error while reading bind file:" << tmp);
321 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
323 ofstream os(bind_file.c_str(),
324 append ? (ios::app | ios::out) : ios::out);
327 os << "## This file is automatically generated by lyx\n"
328 << "## All modifications will be lost\n\n";
330 string tag = unbind ? "\\unbind" : "\\bind";
331 BindingList const list = listBindings(false);
332 BindingList::const_iterator it = list.begin();
333 BindingList::const_iterator it_end = list.end();
334 for (; it != it_end; ++it) {
335 FuncCode action = it->request.action;
336 string arg = to_utf8(it->request.argument());
339 << to_utf8(it->sequence.print(KeySequence::BindFile))
341 << lyxaction.getActionName(action)
342 << (arg.empty() ? "" : " ") << arg
350 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
351 KeyModifier mod, KeySequence * seq) const
355 return FuncRequest::unknown;
358 Table::const_iterator end = table.end();
359 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
360 KeyModifier mask = cit->mod.second;
361 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
363 if (cit->code == key && cit->mod.first == check) {
365 if (cit->prefixes.get()) {
366 // this is a prefix key - set new map
367 seq->curmap = cit->prefixes.get();
368 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
371 // final key - reset map
378 // error - key not found:
381 return FuncRequest::unknown;
385 docstring const KeyMap::print(bool forgui) const
388 Table::const_iterator end = table.end();
389 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
390 buf += cit->code.print(cit->mod.first, forgui);
397 docstring KeyMap::printBindings(FuncRequest const & func,
398 KeySequence::outputFormat format) const
400 Bindings bindings = findBindings(func);
401 if (bindings.empty())
404 odocstringstream res;
405 Bindings::const_iterator cit = bindings.begin();
406 Bindings::const_iterator cit_end = bindings.end();
407 // print the first item
408 res << cit->print(format);
409 // more than one shortcuts?
410 for (++cit; cit != cit_end; ++cit)
411 res << ", " << cit->print(format);
416 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
418 return findBindings(func, KeySequence(0, 0));
422 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
423 KeySequence const & prefix) const
429 Table::const_iterator end = table.end();
430 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
431 if (cit->prefixes.get()) {
432 KeySequence seq = prefix;
433 seq.addkey(cit->code, cit->mod.first);
434 Bindings res2 = cit->prefixes->findBindings(func, seq);
435 res.insert(res.end(), res2.begin(), res2.end());
436 } else if (cit->func == func) {
437 KeySequence seq = prefix;
438 seq.addkey(cit->code, cit->mod.first);
447 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
450 listBindings(list, KeySequence(0, 0), tag);
452 LyXAction::const_func_iterator fit = lyxaction.func_begin();
453 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
454 for (; fit != fit_end; ++fit) {
455 FuncCode action = fit->second;
456 bool has_action = false;
457 BindingList::const_iterator it = list.begin();
458 BindingList::const_iterator it_end = list.end();
459 for (; it != it_end; ++it)
460 if (it->request.action == action) {
465 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
472 void KeyMap::listBindings(BindingList & list,
473 KeySequence const & prefix, KeyMap::ItemType tag) const
475 Table::const_iterator it = table.begin();
476 Table::const_iterator it_end = table.end();
477 for (; it != it_end; ++it) {
478 // a LFUN_COMMAND_PREFIX
479 if (it->prefixes.get()) {
480 KeySequence seq = prefix;
481 seq.addkey(it->code, it->mod.first);
482 it->prefixes->listBindings(list, seq, tag);
484 KeySequence seq = prefix;
485 seq.addkey(it->code, it->mod.first);
486 list.push_back(Binding(it->func, seq, tag));