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"
21 #include "support/debug.h"
22 #include "support/docstream.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/Lexer.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 for (auto & key : table) {
113 && mod1 == key.mod.first
114 && mod2 == key.mod.second) {
116 if (r + 1 == seq->length()) {
117 LYXERR(Debug::KBMAP, "Warning: New binding for '"
118 << to_utf8(seq->print(KeySequence::Portable))
119 << "' is overriding old binding...");
121 key.prefixes.reset();
123 key.func.setOrigin(FuncRequest::KEYBOARD);
125 } else if (!key.prefixes) {
126 lyxerr << "Error: New binding for '"
127 << to_utf8(seq->print(KeySequence::Portable))
128 << "' is overriding old binding..."
132 key.prefixes->bind(seq, func, r + 1);
138 Table::iterator newone = table.insert(table.end(), Key());
140 newone->mod = seq->modifiers[r];
141 if (r + 1 == seq->length()) {
143 newone->func.setOrigin(FuncRequest::KEYBOARD);
144 newone->prefixes.reset();
146 newone->prefixes.reset(new KeyMap);
147 newone->prefixes->bind(seq, func, r + 1);
152 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
154 KeySymbol code = seq->sequence[r];
158 KeyModifier const mod1 = seq->modifiers[r].first;
159 KeyModifier const mod2 = seq->modifiers[r].second;
161 // check if key is already there
162 vector <Table::iterator> removes;
163 Table::iterator end = table.end();
164 for (Table::iterator it = table.begin(); it != end; ++it) {
166 && mod1 == it->mod.first
167 && mod2 == it->mod.second) {
169 if (r + 1 == seq->length()) {
170 if (it->func == func || func == FuncRequest::unknown) {
171 removes.push_back(it);
173 it->prefixes.reset();
175 } else if (it->prefixes) {
176 it->prefixes->unbind(seq, func, r + 1);
177 if (it->prefixes->empty())
178 removes.push_back(it);
184 for (unsigned i = removes.size(); i > 0; --i)
185 table.erase(removes[i-1]);
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 for (auto const & key : table) {
201 && mod1 == key.mod.first
202 && mod2 == key.mod.second) {
203 if (r + 1 == seq.length())
204 return (key.prefixes) ? FuncRequest::prefix : key.func;
205 else if (key.prefixes)
206 return key.prefixes->getBinding(seq, r + 1);
209 return FuncRequest::unknown;
219 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map, BindReadType rt, bool i18n)
221 FileName bf = i18n ? i18nLibFileSearch("bind", bind_file, "bind")
222 : libFileSearch("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 == FuncRequest::unknown) {
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();
362 func = FuncRequest::unknown;
364 func = lyxaction.lookupFunc(cmd);
365 if (func == FuncRequest::unknown) {
366 lexrc.printError("BN_UNBIND: Unknown LyX function `$$Token'");
373 unbind_map->bind(seq, func);
381 lexrc.printError("BN_BINDFILE: Missing file name");
385 string tmp = lexrc.getString();
386 if (prefixIs(tmp, "../")) {
387 tmp = split(tmp, '/');
389 error |= !read(tmp, unbind_map, Default, false);
392 error |= !read(tmp, unbind_map);
396 // This is triggered the first time through the loop unless
397 // we hit a format tag.
398 if (format != LFUN_FORMAT)
399 return FormatMismatch;
403 LYXERR0("KeyMap::read: error while reading bind file:" << bind_file.absFileName());
410 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
412 ofstream os(bind_file.c_str(),
413 append ? (ios::app | ios::out) : ios::out);
416 os << "## This file is automatically generated by lyx\n"
417 << "## All modifications will be lost\n\n"
418 << "Format " << LFUN_FORMAT << "\n\n";
420 string tag = unbind ? "\\unbind" : "\\bind";
421 for (auto const & bnd : listBindings(false)) {
422 FuncCode const action = bnd.request.action();
423 string const arg = to_utf8(bnd.request.argument());
426 if (unbind && bnd.request == FuncRequest::unknown)
429 cmd = lyxaction.getActionName(action) + (arg.empty() ? string() : " " + arg);
431 << to_utf8(bnd.sequence.print(KeySequence::BindFile))
432 << "\" " << Lexer::quoteString(cmd)
440 FuncRequest const & KeyMap::lookup(KeySymbol const & keysym,
441 KeyModifier mod, KeySequence * seq) const
445 return FuncRequest::unknown;
448 for (auto const & key : table) {
449 KeyModifier mask = key.mod.second;
450 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
452 if (key.code == keysym && key.mod.first == check) {
455 // this is a prefix key - set new map
456 seq->curmap = key.prefixes.get();
457 return FuncRequest::prefix;
459 // final key - reset map
466 // error - key not found:
469 return FuncRequest::unknown;
473 docstring const KeyMap::print(bool forgui) const
476 for (auto const & key : table) {
477 buf += key.code.print(key.mod.first, forgui);
484 docstring KeyMap::printBindings(FuncRequest const & func,
485 KeySequence::outputFormat format,
486 bool const untranslated) const
488 Bindings bindings = findBindings(func);
489 if (bindings.empty())
492 odocstringstream res;
493 bool firstone = true;
494 for (auto const & key : bindings) {
497 res << key.print(format, untranslated);
504 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
506 return findBindings(func, KeySequence(nullptr, nullptr));
510 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
511 KeySequence const & prefix) const
517 for (auto const & key : table) {
519 KeySequence seq = prefix;
520 seq.addkey(key.code, key.mod.first);
521 Bindings res2 = key.prefixes->findBindings(func, seq);
522 res.insert(res.end(), res2.begin(), res2.end());
523 } else if (key.func == func) {
524 KeySequence seq = prefix;
525 seq.addkey(key.code, key.mod.first);
534 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
537 listBindings(list, KeySequence(nullptr, nullptr), tag);
539 for (auto const & name_code : lyxaction) {
540 FuncCode action = name_code.second;
541 bool has_action = false;
542 for (auto const & item : list)
543 if (item.request.action() == action) {
548 list.push_back(Binding(FuncRequest(action), KeySequence(nullptr, nullptr), tag));
555 void KeyMap::listBindings(BindingList & list,
556 KeySequence const & prefix, KeyMap::ItemType tag) const
558 for (auto const & key : table) {
559 // a LFUN_COMMAND_PREFIX
561 KeySequence seq = prefix;
562 seq.addkey(key.code, key.mod.first);
563 key.prefixes->listBindings(list, seq, tag);
565 KeySequence seq = prefix;
566 seq.addkey(key.code, key.mod.first);
567 list.push_back(Binding(key.func, seq, tag));