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;
40 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
44 string const s = key.getSymbolName();
46 if (mod & ControlModifier)
48 #if defined(USE_MACOSX_PACKAGING) || defined(USE_META_KEYBINDING)
49 if (mod & MetaModifier)
51 if (mod & AltModifier)
54 if (mod & AltModifier)
57 if (mod & ShiftModifier)
65 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
67 LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
68 << func.action() << '\'');
72 string::size_type const res = k.parse(seq);
73 if (res == string::npos) {
76 LYXERR(Debug::KBMAP, "Parse error at position " << res
77 << " in key sequence '" << seq << "'.");
80 return res == string::npos ? 0 : res;
84 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
88 string::size_type const res = k.parse(seq);
89 if (res == string::npos)
92 LYXERR(Debug::KBMAP, "Parse error at position " << res
93 << " in key sequence '" << seq << "'.");
94 return res == string::npos ? 0 : res;
98 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
100 KeySymbol code = seq->sequence[r];
104 KeyModifier const mod1 = seq->modifiers[r].first;
105 KeyModifier const mod2 = seq->modifiers[r].second;
107 // check if key is already there
108 Table::iterator end = table.end();
109 for (Table::iterator it = table.begin(); it != end; ++it) {
111 && mod1 == it->mod.first
112 && mod2 == it->mod.second) {
114 if (r + 1 == seq->length()) {
115 LYXERR(Debug::KBMAP, "Warning: New binding for '"
116 << to_utf8(seq->print(KeySequence::Portable))
117 << "' is overriding old binding...");
119 it->prefixes.reset();
121 it->func.setOrigin(FuncRequest::KEYBOARD);
123 } else if (!it->prefixes) {
124 lyxerr << "Error: New binding for '"
125 << to_utf8(seq->print(KeySequence::Portable))
126 << "' is overriding old binding..."
130 it->prefixes->bind(seq, func, r + 1);
136 Table::iterator newone = table.insert(table.end(), Key());
138 newone->mod = seq->modifiers[r];
139 if (r + 1 == seq->length()) {
141 newone->func.setOrigin(FuncRequest::KEYBOARD);
142 newone->prefixes.reset();
144 newone->prefixes.reset(new KeyMap);
145 newone->prefixes->bind(seq, func, r + 1);
150 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
152 KeySymbol code = seq->sequence[r];
156 KeyModifier const mod1 = seq->modifiers[r].first;
157 KeyModifier const mod2 = seq->modifiers[r].second;
159 // check if key is already there
160 Table::iterator end = table.end();
161 Table::iterator remove = end;
162 for (Table::iterator it = table.begin(); it != end; ++it) {
164 && mod1 == it->mod.first
165 && mod2 == it->mod.second) {
167 if (r + 1 == seq->length()) {
168 if (it->func == func) {
171 it->prefixes.reset();
173 } else if (it->prefixes) {
174 it->prefixes->unbind(seq, func, r + 1);
175 if (it->prefixes->empty())
186 FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
188 KeySymbol code = seq.sequence[r];
190 return FuncRequest::unknown;
192 KeyModifier const mod1 = seq.modifiers[r].first;
193 KeyModifier const mod2 = seq.modifiers[r].second;
195 // check if key is already there
196 Table::iterator end = table.end();
197 for (Table::iterator it = table.begin(); it != end; ++it) {
199 && mod1 == it->mod.first
200 && mod2 == it->mod.second) {
201 if (r + 1 == seq.length())
203 else if (it->prefixes)
204 return it->prefixes->getBinding(seq, r + 1);
207 return FuncRequest::unknown;
217 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map, BindReadType rt)
219 FileName bf = i18nLibFileSearch("bind", bind_file, "bind");
224 lyxerr << "Could not find bind file: " << bind_file;
226 frontend::Alert::warning(_("Could not find bind file"),
227 bformat(_("Unable to find the bind file\n%1$s.\n"
228 "Please check your installation."), from_utf8(bind_file)));
232 static string const defaultBindfile = "cua";
233 if (bind_file == defaultBindfile) {
234 frontend::Alert::warning(_("Could not find `cua.bind' file"),
235 _("Unable to find the default bind file `cua.bind'.\n"
236 "Please check your installation."));
240 // Try it with the default file.
241 frontend::Alert::warning(_("Could not find bind file"),
242 bformat(_("Unable to find the bind file\n%1$s.\n"
243 "Falling back to default."), from_utf8(bind_file)));
244 return read(defaultBindfile, unbind_map);
246 return read(bf, unbind_map);
250 bool KeyMap::read(FileName const & bind_file, KeyMap * unbind_map)
252 ReturnValues retval = readWithoutConv(bind_file, unbind_map);
253 if (retval != FormatMismatch)
254 return retval == ReadOK;
256 LYXERR(Debug::FILES, "Converting bind file to " << LFUN_FORMAT);
257 FileName const tempfile = FileName::tempName("convert_bind");
258 bool const success = prefs2prefs(bind_file, tempfile, true);
260 LYXERR0 ("Unable to convert " << bind_file <<
261 " to format " << LFUN_FORMAT);
264 retval = readWithoutConv(tempfile, unbind_map);
265 tempfile.removeFile();
266 return retval == ReadOK;
270 KeyMap::ReturnValues KeyMap::readWithoutConv(FileName const & bind_file, KeyMap * unbind_map)
279 LexerKeyword bindTags[] = {
280 { "\\bind", BN_BIND },
281 { "\\bind_file", BN_BINDFILE },
282 { "\\unbind", BN_UNBIND },
283 { "format", BN_FORMAT }
286 Lexer lexrc(bindTags);
287 if (lyxerr.debugging(Debug::PARSER))
288 lexrc.printTable(lyxerr);
290 lexrc.setFile(bind_file);
292 LYXERR0("KeyMap::read: cannot open bind file:" << bind_file.absFileName());
296 LYXERR(Debug::KBMAP, "Reading bind file:" << bind_file.absFileName());
298 // format of pre-2.0 bind files, before this tag was introduced.
299 unsigned int format = 0;
301 while (lexrc.isOK()) {
302 switch (lexrc.lex()) {
304 case Lexer::LEX_UNDEF:
305 lexrc.printError("Unknown tag `$$Token'");
309 case Lexer::LEX_FEOF:
314 format = lexrc.getInteger();
319 lexrc.printError("BN_BIND: Missing key sequence");
323 string seq = lexrc.getString();
325 if (!lexrc.next(true)) {
326 lexrc.printError("BN_BIND: missing command");
330 string cmd = lexrc.getString();
332 FuncRequest func = lyxaction.lookupFunc(cmd);
333 if (func.action() == LFUN_UNKNOWN_ACTION) {
334 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
345 lexrc.printError("BN_UNBIND: Missing key sequence");
349 string seq = lexrc.getString();
351 if (!lexrc.next(true)) {
352 lexrc.printError("BN_UNBIND: missing command");
356 string cmd = lexrc.getString();
358 FuncRequest func = lyxaction.lookupFunc(cmd);
359 if (func.action() == LFUN_UNKNOWN_ACTION) {
360 lexrc.printError("BN_UNBIND: Unknown LyX"
361 " function `$$Token'");
367 unbind_map->bind(seq, func);
375 lexrc.printError("BN_BINDFILE: Missing file name");
379 string const tmp = lexrc.getString();
380 error |= !read(tmp, unbind_map);
384 // This is triggered the first time through the loop unless
385 // we hit a format tag.
386 if (format != LFUN_FORMAT)
387 return FormatMismatch;
391 LYXERR0("KeyMap::read: error while reading bind file:" << bind_file.absFileName());
398 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
400 ofstream os(bind_file.c_str(),
401 append ? (ios::app | ios::out) : ios::out);
404 os << "## This file is automatically generated by lyx\n"
405 << "## All modifications will be lost\n\n"
406 << "Format " << LFUN_FORMAT << "\n\n";
408 string tag = unbind ? "\\unbind" : "\\bind";
409 BindingList const list = listBindings(false);
410 BindingList::const_iterator it = list.begin();
411 BindingList::const_iterator it_end = list.end();
412 for (; it != it_end; ++it) {
413 FuncCode action = it->request.action();
414 string arg = to_utf8(it->request.argument());
416 string const cmd = lyxaction.getActionName(action)
417 + (arg.empty() ? string() : " " + arg) ;
419 << to_utf8(it->sequence.print(KeySequence::BindFile))
420 << "\" " << Lexer::quoteString(cmd)
428 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
429 KeyModifier mod, KeySequence * seq) const
433 return FuncRequest::unknown;
436 Table::const_iterator end = table.end();
437 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
438 KeyModifier mask = cit->mod.second;
439 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
441 if (cit->code == key && cit->mod.first == check) {
444 // this is a prefix key - set new map
445 seq->curmap = cit->prefixes.get();
446 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
449 // final key - reset map
456 // error - key not found:
459 return FuncRequest::unknown;
463 docstring const KeyMap::print(bool forgui) const
466 Table::const_iterator end = table.end();
467 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
468 buf += cit->code.print(cit->mod.first, forgui);
475 docstring KeyMap::printBindings(FuncRequest const & func,
476 KeySequence::outputFormat format) const
478 Bindings bindings = findBindings(func);
479 if (bindings.empty())
482 odocstringstream res;
483 Bindings::const_iterator cit = bindings.begin();
484 Bindings::const_iterator cit_end = bindings.end();
485 // print the first item
486 res << cit->print(format);
487 // more than one shortcuts?
488 for (++cit; cit != cit_end; ++cit)
489 res << ", " << cit->print(format);
494 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
496 return findBindings(func, KeySequence(0, 0));
500 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
501 KeySequence const & prefix) const
507 Table::const_iterator end = table.end();
508 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
510 KeySequence seq = prefix;
511 seq.addkey(cit->code, cit->mod.first);
512 Bindings res2 = cit->prefixes->findBindings(func, seq);
513 res.insert(res.end(), res2.begin(), res2.end());
514 } else if (cit->func == func) {
515 KeySequence seq = prefix;
516 seq.addkey(cit->code, cit->mod.first);
525 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
528 listBindings(list, KeySequence(0, 0), tag);
530 LyXAction::const_iterator fit = lyxaction.func_begin();
531 LyXAction::const_iterator const fen = lyxaction.func_end();
532 for (; fit != fen; ++fit) {
533 FuncCode action = fit->second;
534 bool has_action = false;
535 BindingList::const_iterator bit = list.begin();
536 BindingList::const_iterator const ben = list.end();
537 for (; bit != ben; ++bit)
538 if (bit->request.action() == action) {
543 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
550 void KeyMap::listBindings(BindingList & list,
551 KeySequence const & prefix, KeyMap::ItemType tag) const
553 Table::const_iterator it = table.begin();
554 Table::const_iterator it_end = table.end();
555 for (; it != it_end; ++it) {
556 // a LFUN_COMMAND_PREFIX
558 KeySequence seq = prefix;
559 seq.addkey(it->code, it->mod.first);
560 it->prefixes->listBindings(list, seq, tag);
562 KeySequence seq = prefix;
563 seq.addkey(it->code, it->mod.first);
564 list.push_back(Binding(it->func, seq, tag));