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 (mod & AltModifier)
50 if (mod & ShiftModifier)
58 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
60 LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
61 << func.action() << '\'');
65 string::size_type const res = k.parse(seq);
66 if (res == string::npos) {
69 LYXERR(Debug::KBMAP, "Parse error at position " << res
70 << " in key sequence '" << seq << "'.");
73 return res == string::npos ? 0 : res;
77 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
81 string::size_type const res = k.parse(seq);
82 if (res == string::npos)
85 LYXERR(Debug::KBMAP, "Parse error at position " << res
86 << " in key sequence '" << seq << "'.");
87 return res == string::npos ? 0 : res;
91 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
93 KeySymbol code = seq->sequence[r];
97 KeyModifier const mod1 = seq->modifiers[r].first;
98 KeyModifier const mod2 = seq->modifiers[r].second;
100 // check if key is already there
101 Table::iterator end = table.end();
102 for (Table::iterator it = table.begin(); it != end; ++it) {
104 && mod1 == it->mod.first
105 && mod2 == it->mod.second) {
107 if (r + 1 == seq->length()) {
108 LYXERR(Debug::KBMAP, "Warning: New binding for '"
109 << to_utf8(seq->print(KeySequence::Portable))
110 << "' is overriding old binding...");
111 if (it->prefixes.get()) {
112 it->prefixes.reset();
115 it->func.setOrigin(FuncRequest::KEYBOARD);
117 } else if (!it->prefixes.get()) {
118 lyxerr << "Error: New binding for '"
119 << to_utf8(seq->print(KeySequence::Portable))
120 << "' is overriding old binding..."
124 it->prefixes->bind(seq, func, r + 1);
130 Table::iterator newone = table.insert(table.end(), Key());
132 newone->mod = seq->modifiers[r];
133 if (r + 1 == seq->length()) {
135 newone->func.setOrigin(FuncRequest::KEYBOARD);
136 newone->prefixes.reset();
138 newone->prefixes.reset(new KeyMap);
139 newone->prefixes->bind(seq, func, r + 1);
144 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
146 KeySymbol code = seq->sequence[r];
150 KeyModifier const mod1 = seq->modifiers[r].first;
151 KeyModifier const mod2 = seq->modifiers[r].second;
153 // check if key is already there
154 Table::iterator end = table.end();
155 Table::iterator remove = end;
156 for (Table::iterator it = table.begin(); it != end; ++it) {
158 && mod1 == it->mod.first
159 && mod2 == it->mod.second) {
161 if (r + 1 == seq->length()) {
162 if (it->func == func) {
164 if (it->prefixes.get())
165 it->prefixes.reset();
167 } else if (it->prefixes.get()) {
168 it->prefixes->unbind(seq, func, r + 1);
169 if (it->prefixes->empty())
180 FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
182 KeySymbol code = seq.sequence[r];
184 return FuncRequest::unknown;
186 KeyModifier const mod1 = seq.modifiers[r].first;
187 KeyModifier const mod2 = seq.modifiers[r].second;
189 // check if key is already there
190 Table::iterator end = table.end();
191 for (Table::iterator it = table.begin(); it != end; ++it) {
193 && mod1 == it->mod.first
194 && mod2 == it->mod.second) {
195 if (r + 1 == seq.length())
197 else if (it->prefixes.get())
198 return it->prefixes->getBinding(seq, r + 1);
201 return FuncRequest::unknown;
211 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map, BindReadType rt)
213 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)));
226 static string const defaultBindfile = "cua";
227 if (bind_file == defaultBindfile) {
228 frontend::Alert::warning(_("Could not find `cua.bind' file"),
229 _("Unable to find the default bind file `cua.bind'.\n"
230 "Please check your installation."));
234 // Try it with the default file.
235 frontend::Alert::warning(_("Could not find bind file"),
236 bformat(_("Unable to find the bind file\n%1$s.\n"
237 "Falling back to default."), from_utf8(bind_file)));
238 return read(defaultBindfile, unbind_map);
240 return read(bf, unbind_map);
244 bool KeyMap::read(FileName const & bind_file, KeyMap * unbind_map)
246 ReturnValues retval = readWithoutConv(bind_file, unbind_map);
247 if (retval != FormatMismatch)
248 return retval == ReadOK;
250 LYXERR(Debug::FILES, "Converting bind file to " << LFUN_FORMAT);
251 FileName const tempfile = FileName::tempName("convert_bind");
252 bool const success = prefs2prefs(bind_file, tempfile, true);
254 LYXERR0 ("Unable to convert " << bind_file <<
255 " to format " << LFUN_FORMAT);
258 retval = readWithoutConv(tempfile, unbind_map);
259 tempfile.removeFile();
260 return retval == ReadOK;
264 KeyMap::ReturnValues KeyMap::readWithoutConv(FileName const & bind_file, KeyMap * unbind_map)
273 LexerKeyword bindTags[] = {
274 { "\\bind", BN_BIND },
275 { "\\bind_file", BN_BINDFILE },
276 { "\\unbind", BN_UNBIND },
277 { "format", BN_FORMAT }
280 Lexer lexrc(bindTags);
281 if (lyxerr.debugging(Debug::PARSER))
282 lexrc.printTable(lyxerr);
284 lexrc.setFile(bind_file);
286 LYXERR0("KeyMap::read: cannot open bind file:" << bind_file.absFileName());
290 LYXERR(Debug::KBMAP, "Reading bind file:" << bind_file.absFileName());
292 // format of pre-2.0 bind files, before this tag was introduced.
293 unsigned int format = 0;
295 while (lexrc.isOK()) {
296 switch (lexrc.lex()) {
298 case Lexer::LEX_UNDEF:
299 lexrc.printError("Unknown tag `$$Token'");
303 case Lexer::LEX_FEOF:
308 format = lexrc.getInteger();
313 lexrc.printError("BN_BIND: Missing key sequence");
317 string seq = lexrc.getString();
319 if (!lexrc.next(true)) {
320 lexrc.printError("BN_BIND: missing command");
324 string cmd = lexrc.getString();
326 FuncRequest func = lyxaction.lookupFunc(cmd);
327 if (func.action() == LFUN_UNKNOWN_ACTION) {
328 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
339 lexrc.printError("BN_UNBIND: Missing key sequence");
343 string seq = lexrc.getString();
345 if (!lexrc.next(true)) {
346 lexrc.printError("BN_UNBIND: missing command");
350 string cmd = lexrc.getString();
352 FuncRequest func = lyxaction.lookupFunc(cmd);
353 if (func.action() == LFUN_UNKNOWN_ACTION) {
354 lexrc.printError("BN_UNBIND: Unknown LyX"
355 " function `$$Token'");
361 unbind_map->bind(seq, func);
369 lexrc.printError("BN_BINDFILE: Missing file name");
373 string const tmp = lexrc.getString();
374 error |= !read(tmp, unbind_map);
378 // This is triggered the first time through the loop unless
379 // we hit a format tag.
380 if (format != LFUN_FORMAT)
381 return FormatMismatch;
385 LYXERR0("KeyMap::read: error while reading bind file:" << bind_file.absFileName());
392 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
394 ofstream os(bind_file.c_str(),
395 append ? (ios::app | ios::out) : ios::out);
398 os << "## This file is automatically generated by lyx\n"
399 << "## All modifications will be lost\n\n"
400 << "Format " << LFUN_FORMAT << "\n\n";
402 string tag = unbind ? "\\unbind" : "\\bind";
403 BindingList const list = listBindings(false);
404 BindingList::const_iterator it = list.begin();
405 BindingList::const_iterator it_end = list.end();
406 for (; it != it_end; ++it) {
407 FuncCode action = it->request.action();
408 string arg = to_utf8(it->request.argument());
410 string const cmd = lyxaction.getActionName(action)
411 + (arg.empty() ? string() : " " + arg) ;
413 << to_utf8(it->sequence.print(KeySequence::BindFile))
414 << "\" " << Lexer::quoteString(cmd)
422 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
423 KeyModifier mod, KeySequence * seq) const
427 return FuncRequest::unknown;
430 Table::const_iterator end = table.end();
431 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
432 KeyModifier mask = cit->mod.second;
433 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
435 if (cit->code == key && cit->mod.first == check) {
437 if (cit->prefixes.get()) {
438 // this is a prefix key - set new map
439 seq->curmap = cit->prefixes.get();
440 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
443 // final key - reset map
450 // error - key not found:
453 return FuncRequest::unknown;
457 docstring const KeyMap::print(bool forgui) const
460 Table::const_iterator end = table.end();
461 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
462 buf += cit->code.print(cit->mod.first, forgui);
469 docstring KeyMap::printBindings(FuncRequest const & func,
470 KeySequence::outputFormat format) const
472 Bindings bindings = findBindings(func);
473 if (bindings.empty())
476 odocstringstream res;
477 Bindings::const_iterator cit = bindings.begin();
478 Bindings::const_iterator cit_end = bindings.end();
479 // print the first item
480 res << cit->print(format);
481 // more than one shortcuts?
482 for (++cit; cit != cit_end; ++cit)
483 res << ", " << cit->print(format);
488 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
490 return findBindings(func, KeySequence(0, 0));
494 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
495 KeySequence const & prefix) const
501 Table::const_iterator end = table.end();
502 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
503 if (cit->prefixes.get()) {
504 KeySequence seq = prefix;
505 seq.addkey(cit->code, cit->mod.first);
506 Bindings res2 = cit->prefixes->findBindings(func, seq);
507 res.insert(res.end(), res2.begin(), res2.end());
508 } else if (cit->func == func) {
509 KeySequence seq = prefix;
510 seq.addkey(cit->code, cit->mod.first);
519 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
522 listBindings(list, KeySequence(0, 0), tag);
524 LyXAction::const_iterator fit = lyxaction.func_begin();
525 LyXAction::const_iterator const fen = lyxaction.func_end();
526 for (; fit != fen; ++fit) {
527 FuncCode action = fit->second;
528 bool has_action = false;
529 BindingList::const_iterator bit = list.begin();
530 BindingList::const_iterator const ben = list.end();
531 for (; bit != ben; ++bit)
532 if (bit->request.action() == action) {
537 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
544 void KeyMap::listBindings(BindingList & list,
545 KeySequence const & prefix, KeyMap::ItemType tag) const
547 Table::const_iterator it = table.begin();
548 Table::const_iterator it_end = table.end();
549 for (; it != it_end; ++it) {
550 // a LFUN_COMMAND_PREFIX
551 if (it->prefixes.get()) {
552 KeySequence seq = prefix;
553 seq.addkey(it->code, it->mod.first);
554 it->prefixes->listBindings(list, seq, tag);
556 KeySequence seq = prefix;
557 seq.addkey(it->code, it->mod.first);
558 list.push_back(Binding(it->func, seq, tag));