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...");
118 if (it->prefixes.get()) {
119 it->prefixes.reset();
122 it->func.setOrigin(FuncRequest::KEYBOARD);
124 } else if (!it->prefixes.get()) {
125 lyxerr << "Error: New binding for '"
126 << to_utf8(seq->print(KeySequence::Portable))
127 << "' is overriding old binding..."
131 it->prefixes->bind(seq, func, r + 1);
137 Table::iterator newone = table.insert(table.end(), Key());
139 newone->mod = seq->modifiers[r];
140 if (r + 1 == seq->length()) {
142 newone->func.setOrigin(FuncRequest::KEYBOARD);
143 newone->prefixes.reset();
145 newone->prefixes.reset(new KeyMap);
146 newone->prefixes->bind(seq, func, r + 1);
151 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
153 KeySymbol code = seq->sequence[r];
157 KeyModifier const mod1 = seq->modifiers[r].first;
158 KeyModifier const mod2 = seq->modifiers[r].second;
160 // check if key is already there
161 Table::iterator end = table.end();
162 Table::iterator remove = end;
163 for (Table::iterator it = table.begin(); it != end; ++it) {
165 && mod1 == it->mod.first
166 && mod2 == it->mod.second) {
168 if (r + 1 == seq->length()) {
169 if (it->func == func) {
171 if (it->prefixes.get())
172 it->prefixes.reset();
174 } else if (it->prefixes.get()) {
175 it->prefixes->unbind(seq, func, r + 1);
176 if (it->prefixes->empty())
187 FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
189 KeySymbol code = seq.sequence[r];
191 return FuncRequest::unknown;
193 KeyModifier const mod1 = seq.modifiers[r].first;
194 KeyModifier const mod2 = seq.modifiers[r].second;
196 // check if key is already there
197 Table::iterator end = table.end();
198 for (Table::iterator it = table.begin(); it != end; ++it) {
200 && mod1 == it->mod.first
201 && mod2 == it->mod.second) {
202 if (r + 1 == seq.length())
204 else if (it->prefixes.get())
205 return it->prefixes->getBinding(seq, r + 1);
208 return FuncRequest::unknown;
218 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map, BindReadType rt)
220 FileName bf = i18nLibFileSearch("bind", bind_file, "bind");
225 lyxerr << "Could not find bind file: " << bind_file;
227 frontend::Alert::warning(_("Could not find bind file"),
228 bformat(_("Unable to find the bind file\n%1$s.\n"
229 "Please check your installation."), from_utf8(bind_file)));
233 static string const defaultBindfile = "cua";
234 if (bind_file == defaultBindfile) {
235 frontend::Alert::warning(_("Could not find `cua.bind' file"),
236 _("Unable to find the default bind file `cua.bind'.\n"
237 "Please check your installation."));
241 // Try it with the default file.
242 frontend::Alert::warning(_("Could not find bind file"),
243 bformat(_("Unable to find the bind file\n%1$s.\n"
244 "Falling back to default."), from_utf8(bind_file)));
245 return read(defaultBindfile, unbind_map);
247 return read(bf, unbind_map);
251 bool KeyMap::read(FileName const & bind_file, KeyMap * unbind_map)
253 ReturnValues retval = readWithoutConv(bind_file, unbind_map);
254 if (retval != FormatMismatch)
255 return retval == ReadOK;
257 LYXERR(Debug::FILES, "Converting bind file to " << LFUN_FORMAT);
258 FileName const tempfile = FileName::tempName("convert_bind");
259 bool const success = prefs2prefs(bind_file, tempfile, true);
261 LYXERR0 ("Unable to convert " << bind_file <<
262 " to format " << LFUN_FORMAT);
265 retval = readWithoutConv(tempfile, unbind_map);
266 tempfile.removeFile();
267 return retval == ReadOK;
271 KeyMap::ReturnValues KeyMap::readWithoutConv(FileName const & bind_file, KeyMap * unbind_map)
280 LexerKeyword bindTags[] = {
281 { "\\bind", BN_BIND },
282 { "\\bind_file", BN_BINDFILE },
283 { "\\unbind", BN_UNBIND },
284 { "format", BN_FORMAT }
287 Lexer lexrc(bindTags);
288 if (lyxerr.debugging(Debug::PARSER))
289 lexrc.printTable(lyxerr);
291 lexrc.setFile(bind_file);
293 LYXERR0("KeyMap::read: cannot open bind file:" << bind_file.absFileName());
297 LYXERR(Debug::KBMAP, "Reading bind file:" << bind_file.absFileName());
299 // format of pre-2.0 bind files, before this tag was introduced.
300 unsigned int format = 0;
302 while (lexrc.isOK()) {
303 switch (lexrc.lex()) {
305 case Lexer::LEX_UNDEF:
306 lexrc.printError("Unknown tag `$$Token'");
310 case Lexer::LEX_FEOF:
315 format = lexrc.getInteger();
320 lexrc.printError("BN_BIND: Missing key sequence");
324 string seq = lexrc.getString();
326 if (!lexrc.next(true)) {
327 lexrc.printError("BN_BIND: missing command");
331 string cmd = lexrc.getString();
333 FuncRequest func = lyxaction.lookupFunc(cmd);
334 if (func.action() == LFUN_UNKNOWN_ACTION) {
335 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
346 lexrc.printError("BN_UNBIND: Missing key sequence");
350 string seq = lexrc.getString();
352 if (!lexrc.next(true)) {
353 lexrc.printError("BN_UNBIND: missing command");
357 string cmd = lexrc.getString();
359 FuncRequest func = lyxaction.lookupFunc(cmd);
360 if (func.action() == LFUN_UNKNOWN_ACTION) {
361 lexrc.printError("BN_UNBIND: Unknown LyX"
362 " function `$$Token'");
368 unbind_map->bind(seq, func);
376 lexrc.printError("BN_BINDFILE: Missing file name");
380 string const tmp = lexrc.getString();
381 error |= !read(tmp, unbind_map);
385 // This is triggered the first time through the loop unless
386 // we hit a format tag.
387 if (format != LFUN_FORMAT)
388 return FormatMismatch;
392 LYXERR0("KeyMap::read: error while reading bind file:" << bind_file.absFileName());
399 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
401 ofstream os(bind_file.c_str(),
402 append ? (ios::app | ios::out) : ios::out);
405 os << "## This file is automatically generated by lyx\n"
406 << "## All modifications will be lost\n\n"
407 << "Format " << LFUN_FORMAT << "\n\n";
409 string tag = unbind ? "\\unbind" : "\\bind";
410 BindingList const list = listBindings(false);
411 BindingList::const_iterator it = list.begin();
412 BindingList::const_iterator it_end = list.end();
413 for (; it != it_end; ++it) {
414 FuncCode action = it->request.action();
415 string arg = to_utf8(it->request.argument());
417 string const cmd = lyxaction.getActionName(action)
418 + (arg.empty() ? string() : " " + arg) ;
420 << to_utf8(it->sequence.print(KeySequence::BindFile))
421 << "\" " << Lexer::quoteString(cmd)
429 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
430 KeyModifier mod, KeySequence * seq) const
434 return FuncRequest::unknown;
437 Table::const_iterator end = table.end();
438 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
439 KeyModifier mask = cit->mod.second;
440 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
442 if (cit->code == key && cit->mod.first == check) {
444 if (cit->prefixes.get()) {
445 // this is a prefix key - set new map
446 seq->curmap = cit->prefixes.get();
447 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
450 // final key - reset map
457 // error - key not found:
460 return FuncRequest::unknown;
464 docstring const KeyMap::print(bool forgui) const
467 Table::const_iterator end = table.end();
468 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
469 buf += cit->code.print(cit->mod.first, forgui);
476 docstring KeyMap::printBindings(FuncRequest const & func,
477 KeySequence::outputFormat format) const
479 Bindings bindings = findBindings(func);
480 if (bindings.empty())
483 odocstringstream res;
484 Bindings::const_iterator cit = bindings.begin();
485 Bindings::const_iterator cit_end = bindings.end();
486 // print the first item
487 res << cit->print(format);
488 // more than one shortcuts?
489 for (++cit; cit != cit_end; ++cit)
490 res << ", " << cit->print(format);
495 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
497 return findBindings(func, KeySequence(0, 0));
501 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
502 KeySequence const & prefix) const
508 Table::const_iterator end = table.end();
509 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
510 if (cit->prefixes.get()) {
511 KeySequence seq = prefix;
512 seq.addkey(cit->code, cit->mod.first);
513 Bindings res2 = cit->prefixes->findBindings(func, seq);
514 res.insert(res.end(), res2.begin(), res2.end());
515 } else if (cit->func == func) {
516 KeySequence seq = prefix;
517 seq.addkey(cit->code, cit->mod.first);
526 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
529 listBindings(list, KeySequence(0, 0), tag);
531 LyXAction::const_iterator fit = lyxaction.func_begin();
532 LyXAction::const_iterator const fen = lyxaction.func_end();
533 for (; fit != fen; ++fit) {
534 FuncCode action = fit->second;
535 bool has_action = false;
536 BindingList::const_iterator bit = list.begin();
537 BindingList::const_iterator const ben = list.end();
538 for (; bit != ben; ++bit)
539 if (bit->request.action() == action) {
544 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
551 void KeyMap::listBindings(BindingList & list,
552 KeySequence const & prefix, KeyMap::ItemType tag) const
554 Table::const_iterator it = table.begin();
555 Table::const_iterator it_end = table.end();
556 for (; it != it_end; ++it) {
557 // a LFUN_COMMAND_PREFIX
558 if (it->prefixes.get()) {
559 KeySequence seq = prefix;
560 seq.addkey(it->code, it->mod.first);
561 it->prefixes->listBindings(list, seq, tag);
563 KeySequence seq = prefix;
564 seq.addkey(it->code, it->mod.first);
565 list.push_back(Binding(it->func, seq, tag));