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"
28 #include "support/TempFile.h"
30 #include "frontends/alert.h"
37 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 defined(USE_MACOSX_PACKAGING) || defined(USE_META_KEYBINDING)
50 if (mod & MetaModifier)
52 if (mod & AltModifier)
55 if (mod & AltModifier)
58 if (mod & ShiftModifier)
66 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
68 LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
69 << func.action() << '\'');
71 KeySequence k(nullptr, nullptr);
73 string::size_type const res = k.parse(seq);
74 if (res == string::npos) {
77 LYXERR(Debug::KBMAP, "Parse error at position " << res
78 << " in key sequence '" << seq << "'.");
81 return res == string::npos ? 0 : res;
85 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
87 KeySequence k(nullptr, nullptr);
89 string::size_type const res = k.parse(seq);
90 if (res == string::npos)
93 LYXERR(Debug::KBMAP, "Parse error at position " << res
94 << " in key sequence '" << seq << "'.");
95 return res == string::npos ? 0 : res;
99 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
101 KeySymbol code = seq->sequence[r];
105 KeyModifier const mod1 = seq->modifiers[r].first;
106 KeyModifier const mod2 = seq->modifiers[r].second;
108 // check if key is already there
109 // FIXME perf: Profiling shows that this is responsible of 99% of the
110 // cost of GuiPrefs::applyView()
111 Table::iterator end = table.end();
112 for (Table::iterator it = table.begin(); it != end; ++it) {
114 && mod1 == it->mod.first
115 && mod2 == it->mod.second) {
117 if (r + 1 == seq->length()) {
118 LYXERR(Debug::KBMAP, "Warning: New binding for '"
119 << to_utf8(seq->print(KeySequence::Portable))
120 << "' is overriding old binding...");
122 it->prefixes.reset();
124 it->func.setOrigin(FuncRequest::KEYBOARD);
126 } else if (!it->prefixes) {
127 lyxerr << "Error: New binding for '"
128 << to_utf8(seq->print(KeySequence::Portable))
129 << "' is overriding old binding..."
133 it->prefixes->bind(seq, func, r + 1);
139 Table::iterator newone = table.insert(table.end(), Key());
141 newone->mod = seq->modifiers[r];
142 if (r + 1 == seq->length()) {
144 newone->func.setOrigin(FuncRequest::KEYBOARD);
145 newone->prefixes.reset();
147 newone->prefixes.reset(new KeyMap);
148 newone->prefixes->bind(seq, func, r + 1);
153 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
155 KeySymbol code = seq->sequence[r];
159 KeyModifier const mod1 = seq->modifiers[r].first;
160 KeyModifier const mod2 = seq->modifiers[r].second;
162 // check if key is already there
163 Table::iterator end = table.end();
164 Table::iterator remove = end;
165 for (Table::iterator it = table.begin(); it != end; ++it) {
167 && mod1 == it->mod.first
168 && mod2 == it->mod.second) {
170 if (r + 1 == seq->length()) {
171 if (it->func == func) {
174 it->prefixes.reset();
176 } else if (it->prefixes) {
177 it->prefixes->unbind(seq, func, r + 1);
178 if (it->prefixes->empty())
189 FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
191 KeySymbol code = seq.sequence[r];
193 return FuncRequest::unknown;
195 KeyModifier const mod1 = seq.modifiers[r].first;
196 KeyModifier const mod2 = seq.modifiers[r].second;
198 // check if key is already there
199 Table::iterator end = table.end();
200 for (Table::iterator it = table.begin(); it != end; ++it) {
202 && mod1 == it->mod.first
203 && mod2 == it->mod.second) {
204 if (r + 1 == seq.length())
206 else if (it->prefixes)
207 return it->prefixes->getBinding(seq, r + 1);
210 return FuncRequest::unknown;
220 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map, BindReadType rt)
222 FileName bf = i18nLibFileSearch("bind", bind_file, "bind");
227 lyxerr << "Could not find bind file: " << bind_file;
229 frontend::Alert::warning(_("Could not find bind file"),
230 bformat(_("Unable to find the bind file\n%1$s.\n"
231 "Please check your installation."), from_utf8(bind_file)));
235 static string const defaultBindfile = "cua";
236 if (bind_file == defaultBindfile) {
237 frontend::Alert::warning(_("Could not find `cua.bind' file"),
238 _("Unable to find the default bind file `cua.bind'.\n"
239 "Please check your installation."));
243 // Try it with the default file.
244 frontend::Alert::warning(_("Could not find bind file"),
245 bformat(_("Unable to find the bind file\n%1$s.\n"
246 "Falling back to default."), from_utf8(bind_file)));
247 return read(defaultBindfile, unbind_map);
249 return read(bf, unbind_map);
253 bool KeyMap::read(FileName const & bind_file, KeyMap * unbind_map)
255 ReturnValues retval = readWithoutConv(bind_file, unbind_map);
256 if (retval != FormatMismatch)
257 return retval == ReadOK;
259 LYXERR(Debug::FILES, "Converting bind file to " << LFUN_FORMAT);
260 TempFile tmp("convert_bind");
261 FileName const tempfile = tmp.name();
262 bool const success = prefs2prefs(bind_file, tempfile, true);
264 LYXERR0 ("Unable to convert " << bind_file <<
265 " to format " << LFUN_FORMAT);
268 retval = readWithoutConv(tempfile, unbind_map);
269 return retval == ReadOK;
273 KeyMap::ReturnValues KeyMap::readWithoutConv(FileName const & bind_file, KeyMap * unbind_map)
282 LexerKeyword bindTags[] = {
283 { "\\bind", BN_BIND },
284 { "\\bind_file", BN_BINDFILE },
285 { "\\unbind", BN_UNBIND },
286 { "format", BN_FORMAT }
289 Lexer lexrc(bindTags);
290 if (lyxerr.debugging(Debug::PARSER))
291 lexrc.printTable(lyxerr);
293 lexrc.setFile(bind_file);
295 LYXERR0("KeyMap::read: cannot open bind file:" << bind_file.absFileName());
299 LYXERR(Debug::KBMAP, "Reading bind file:" << bind_file.absFileName());
301 // format of pre-2.0 bind files, before this tag was introduced.
304 while (lexrc.isOK()) {
305 switch (lexrc.lex()) {
307 case Lexer::LEX_UNDEF:
308 lexrc.printError("Unknown tag `$$Token'");
312 case Lexer::LEX_FEOF:
317 format = lexrc.getInteger();
322 lexrc.printError("BN_BIND: Missing key sequence");
326 string seq = lexrc.getString();
328 if (!lexrc.next(true)) {
329 lexrc.printError("BN_BIND: missing command");
333 string cmd = lexrc.getString();
335 FuncRequest func = lyxaction.lookupFunc(cmd);
336 if (func.action() == LFUN_UNKNOWN_ACTION) {
337 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
348 lexrc.printError("BN_UNBIND: Missing key sequence");
352 string seq = lexrc.getString();
354 if (!lexrc.next(true)) {
355 lexrc.printError("BN_UNBIND: missing command");
359 string cmd = lexrc.getString();
361 FuncRequest func = lyxaction.lookupFunc(cmd);
362 if (func.action() == LFUN_UNKNOWN_ACTION) {
363 lexrc.printError("BN_UNBIND: Unknown LyX"
364 " function `$$Token'");
370 unbind_map->bind(seq, func);
378 lexrc.printError("BN_BINDFILE: Missing file name");
382 string const tmp = lexrc.getString();
383 error |= !read(tmp, unbind_map);
387 // This is triggered the first time through the loop unless
388 // we hit a format tag.
389 if (format != LFUN_FORMAT)
390 return FormatMismatch;
394 LYXERR0("KeyMap::read: error while reading bind file:" << bind_file.absFileName());
401 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
403 ofstream os(bind_file.c_str(),
404 append ? (ios::app | ios::out) : ios::out);
407 os << "## This file is automatically generated by lyx\n"
408 << "## All modifications will be lost\n\n"
409 << "Format " << LFUN_FORMAT << "\n\n";
411 string tag = unbind ? "\\unbind" : "\\bind";
412 BindingList const list = listBindings(false);
413 BindingList::const_iterator it = list.begin();
414 BindingList::const_iterator it_end = list.end();
415 for (; it != it_end; ++it) {
416 FuncCode action = it->request.action();
417 string arg = to_utf8(it->request.argument());
419 string const cmd = lyxaction.getActionName(action)
420 + (arg.empty() ? string() : " " + arg) ;
422 << to_utf8(it->sequence.print(KeySequence::BindFile))
423 << "\" " << Lexer::quoteString(cmd)
431 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
432 KeyModifier mod, KeySequence * seq) const
436 return FuncRequest::unknown;
439 Table::const_iterator end = table.end();
440 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
441 KeyModifier mask = cit->mod.second;
442 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
444 if (cit->code == key && cit->mod.first == check) {
447 // this is a prefix key - set new map
448 seq->curmap = cit->prefixes.get();
449 static const FuncRequest prefix(LFUN_COMMAND_PREFIX);
452 // final key - reset map
459 // error - key not found:
462 return FuncRequest::unknown;
466 docstring const KeyMap::print(bool forgui) const
469 Table::const_iterator end = table.end();
470 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
471 buf += cit->code.print(cit->mod.first, forgui);
478 docstring KeyMap::printBindings(FuncRequest const & func,
479 KeySequence::outputFormat format,
480 bool const untranslated) const
482 Bindings bindings = findBindings(func);
483 if (bindings.empty())
486 odocstringstream res;
487 Bindings::const_iterator cit = bindings.begin();
488 Bindings::const_iterator cit_end = bindings.end();
489 // print the first item
490 res << cit->print(format, untranslated);
491 // more than one shortcuts?
492 for (++cit; cit != cit_end; ++cit)
493 res << ", " << cit->print(format, untranslated);
498 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
500 return findBindings(func, KeySequence(nullptr, nullptr));
504 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
505 KeySequence const & prefix) const
511 Table::const_iterator end = table.end();
512 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
514 KeySequence seq = prefix;
515 seq.addkey(cit->code, cit->mod.first);
516 Bindings res2 = cit->prefixes->findBindings(func, seq);
517 res.insert(res.end(), res2.begin(), res2.end());
518 } else if (cit->func == func) {
519 KeySequence seq = prefix;
520 seq.addkey(cit->code, cit->mod.first);
529 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
532 listBindings(list, KeySequence(nullptr, nullptr), tag);
534 LyXAction::const_iterator fit = lyxaction.func_begin();
535 LyXAction::const_iterator const fen = lyxaction.func_end();
536 for (; fit != fen; ++fit) {
537 FuncCode action = fit->second;
538 bool has_action = false;
539 BindingList::const_iterator bit = list.begin();
540 BindingList::const_iterator const ben = list.end();
541 for (; bit != ben; ++bit)
542 if (bit->request.action() == action) {
547 list.push_back(Binding(FuncRequest(action), KeySequence(nullptr, nullptr), tag));
554 void KeyMap::listBindings(BindingList & list,
555 KeySequence const & prefix, KeyMap::ItemType tag) const
557 Table::const_iterator it = table.begin();
558 Table::const_iterator it_end = table.end();
559 for (; it != it_end; ++it) {
560 // a LFUN_COMMAND_PREFIX
562 KeySequence seq = prefix;
563 seq.addkey(it->code, it->mod.first);
564 it->prefixes->listBindings(list, seq, tag);
566 KeySequence seq = prefix;
567 seq.addkey(it->code, it->mod.first);
568 list.push_back(Binding(it->func, seq, tag));