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"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
29 #include "frontends/alert.h"
36 using namespace lyx::support;
41 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
45 string const s = key.getSymbolName();
47 if (mod & ControlModifier)
49 if (mod & AltModifier)
51 if (mod & ShiftModifier)
59 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
61 LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
62 << func.action << '\'');
66 string::size_type const res = k.parse(seq);
67 if (res == string::npos) {
70 LYXERR(Debug::KBMAP, "Parse error at position " << res
71 << " in key sequence '" << seq << "'.");
74 return res == string::npos ? 0 : res;
78 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
82 string::size_type const res = k.parse(seq);
83 if (res == string::npos)
86 LYXERR(Debug::KBMAP, "Parse error at position " << res
87 << " in key sequence '" << seq << "'.");
88 return res == string::npos ? 0 : res;
92 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
94 KeySymbol code = seq->sequence[r];
98 KeyModifier const mod1 = seq->modifiers[r].first;
99 KeyModifier const mod2 = seq->modifiers[r].second;
101 // check if key is already there
102 Table::iterator end = table.end();
103 for (Table::iterator it = table.begin(); it != end; ++it) {
105 && mod1 == it->mod.first
106 && mod2 == it->mod.second) {
108 if (r + 1 == seq->length()) {
109 LYXERR(Debug::KBMAP, "Warning: New binding for '"
110 << to_utf8(seq->print(KeySequence::Portable))
111 << "' is overriding old binding...");
112 if (it->prefixes.get()) {
113 it->prefixes.reset();
116 it->func.origin = FuncRequest::KEYBOARD;
118 } else if (!it->prefixes.get()) {
119 lyxerr << "Error: New binding for '"
120 << to_utf8(seq->print(KeySequence::Portable))
121 << "' is overriding old binding..."
125 it->prefixes->bind(seq, func, r + 1);
131 Table::iterator newone = table.insert(table.end(), Key());
133 newone->mod = seq->modifiers[r];
134 if (r + 1 == seq->length()) {
136 newone->func.origin = FuncRequest::KEYBOARD;
137 newone->prefixes.reset();
139 newone->prefixes.reset(new KeyMap);
140 newone->prefixes->bind(seq, func, r + 1);
145 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
147 KeySymbol code = seq->sequence[r];
151 KeyModifier const mod1 = seq->modifiers[r].first;
152 KeyModifier const mod2 = seq->modifiers[r].second;
154 // check if key is already there
155 Table::iterator end = table.end();
156 Table::iterator remove = end;
157 for (Table::iterator it = table.begin(); it != end; ++it) {
159 && mod1 == it->mod.first
160 && mod2 == it->mod.second) {
162 if (r + 1 == seq->length()) {
163 if (it->func == func) {
165 if (it->prefixes.get())
166 it->prefixes.reset();
168 } else if (it->prefixes.get()) {
169 it->prefixes->unbind(seq, func, r + 1);
170 if (it->prefixes->empty())
181 FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
183 KeySymbol code = seq.sequence[r];
185 return FuncRequest::unknown;
187 KeyModifier const mod1 = seq.modifiers[r].first;
188 KeyModifier const mod2 = seq.modifiers[r].second;
190 // check if key is already there
191 Table::iterator end = table.end();
192 for (Table::iterator it = table.begin(); it != end; ++it) {
194 && mod1 == it->mod.first
195 && mod2 == it->mod.second) {
196 if (r + 1 == seq.length())
198 else if (it->prefixes.get())
199 return it->prefixes->getBinding(seq, r + 1);
202 return FuncRequest::unknown;
212 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map, BindReadType rt)
214 FileName bf = i18nLibFileSearch("bind", bind_file, "bind");
218 lyxerr << "Could not find bind file: " << bind_file;
220 frontend::Alert::warning(_("Could not find bind file"),
221 bformat(_("Unable to find the bind file\n%1$s.\n"
222 "Please check your installation."), from_utf8(bind_file)));
225 frontend::Alert::warning(_("Could not find bind file"),
226 bformat(_("Unable to find the bind file\n%1$s.\n"
227 "Falling back to default."), from_utf8(bind_file)));
228 // So try it with the default file.
229 if (read("cua", unbind_map))
231 lyxerr << "Could not find cua bind file!";
232 frontend::Alert::warning(_("Could not find cua bind file"),
233 _("Unable to find the default bind file `cua'.\n"
234 "Please check your installation."));
237 return read(bf, unbind_map);
241 bool KeyMap::read(FileName const & bind_file, KeyMap * unbind_map)
249 LexerKeyword bindTags[] = {
250 { "\\bind", BN_BIND },
251 { "\\bind_file", BN_BINDFILE },
252 { "\\unbind", BN_UNBIND },
255 Lexer lexrc(bindTags);
256 if (lyxerr.debugging(Debug::PARSER))
257 lexrc.printTable(lyxerr);
259 lexrc.setFile(bind_file);
261 LYXERR0("KeyMap::read: cannot open bind file:" << bind_file.absFilename());
265 LYXERR(Debug::KBMAP, "Reading bind file:" << bind_file.absFilename());
268 while (lexrc.isOK()) {
269 switch (lexrc.lex()) {
271 case Lexer::LEX_UNDEF:
272 lexrc.printError("Unknown tag `$$Token'");
276 case Lexer::LEX_FEOF:
281 lexrc.printError("BN_BIND: Missing key sequence");
285 string seq = lexrc.getString();
287 if (!lexrc.next(true)) {
288 lexrc.printError("BN_BIND: missing command");
292 string cmd = lexrc.getString();
294 FuncRequest func = lyxaction.lookupFunc(cmd);
295 if (func.action == LFUN_UNKNOWN_ACTION) {
296 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
307 lexrc.printError("BN_UNBIND: Missing key sequence");
311 string seq = lexrc.getString();
313 if (!lexrc.next(true)) {
314 lexrc.printError("BN_UNBIND: missing command");
318 string cmd = lexrc.getString();
320 FuncRequest func = lyxaction.lookupFunc(cmd);
321 if (func.action == LFUN_UNKNOWN_ACTION) {
322 lexrc.printError("BN_UNBIND: Unknown LyX"
323 " function `$$Token'");
329 unbind_map->bind(seq, func);
337 lexrc.printError("BN_BINDFILE: Missing file name");
341 string const tmp = lexrc.getString();
342 error |= !read(tmp, unbind_map);
348 LYXERR0("KeyMap::read: error while reading bind file:" << bind_file.absFilename());
353 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
355 ofstream os(bind_file.c_str(),
356 append ? (ios::app | ios::out) : ios::out);
359 os << "## This file is automatically generated by lyx\n"
360 << "## All modifications will be lost\n\n";
362 string tag = unbind ? "\\unbind" : "\\bind";
363 BindingList const list = listBindings(false);
364 BindingList::const_iterator it = list.begin();
365 BindingList::const_iterator it_end = list.end();
366 for (; it != it_end; ++it) {
367 FuncCode action = it->request.action;
368 string arg = to_utf8(it->request.argument());
371 << to_utf8(it->sequence.print(KeySequence::BindFile))
373 << lyxaction.getActionName(action)
374 << (arg.empty() ? "" : " ") << arg
382 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
383 KeyModifier mod, KeySequence * seq) const
387 return FuncRequest::unknown;
390 Table::const_iterator end = table.end();
391 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
392 KeyModifier mask = cit->mod.second;
393 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
395 if (cit->code == key && cit->mod.first == check) {
397 if (cit->prefixes.get()) {
398 // this is a prefix key - set new map
399 seq->curmap = cit->prefixes.get();
400 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
403 // final key - reset map
410 // error - key not found:
413 return FuncRequest::unknown;
417 docstring const KeyMap::print(bool forgui) const
420 Table::const_iterator end = table.end();
421 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
422 buf += cit->code.print(cit->mod.first, forgui);
429 docstring KeyMap::printBindings(FuncRequest const & func,
430 KeySequence::outputFormat format) const
432 Bindings bindings = findBindings(func);
433 if (bindings.empty())
436 odocstringstream res;
437 Bindings::const_iterator cit = bindings.begin();
438 Bindings::const_iterator cit_end = bindings.end();
439 // print the first item
440 res << cit->print(format);
441 // more than one shortcuts?
442 for (++cit; cit != cit_end; ++cit)
443 res << ", " << cit->print(format);
448 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
450 return findBindings(func, KeySequence(0, 0));
454 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
455 KeySequence const & prefix) const
461 Table::const_iterator end = table.end();
462 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
463 if (cit->prefixes.get()) {
464 KeySequence seq = prefix;
465 seq.addkey(cit->code, cit->mod.first);
466 Bindings res2 = cit->prefixes->findBindings(func, seq);
467 res.insert(res.end(), res2.begin(), res2.end());
468 } else if (cit->func == func) {
469 KeySequence seq = prefix;
470 seq.addkey(cit->code, cit->mod.first);
479 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
482 listBindings(list, KeySequence(0, 0), tag);
484 LyXAction::const_func_iterator fit = lyxaction.func_begin();
485 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
486 for (; fit != fit_end; ++fit) {
487 FuncCode action = fit->second;
488 bool has_action = false;
489 BindingList::const_iterator it = list.begin();
490 BindingList::const_iterator it_end = list.end();
491 for (; it != it_end; ++it)
492 if (it->request.action == action) {
497 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
504 void KeyMap::listBindings(BindingList & list,
505 KeySequence const & prefix, KeyMap::ItemType tag) const
507 Table::const_iterator it = table.begin();
508 Table::const_iterator it_end = table.end();
509 for (; it != it_end; ++it) {
510 // a LFUN_COMMAND_PREFIX
511 if (it->prefixes.get()) {
512 KeySequence seq = prefix;
513 seq.addkey(it->code, it->mod.first);
514 it->prefixes->listBindings(list, seq, tag);
516 KeySequence seq = prefix;
517 seq.addkey(it->code, it->mod.first);
518 list.push_back(Binding(it->func, seq, tag));