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"
32 using namespace lyx::support;
37 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
41 string const s = key.getSymbolName();
43 if (mod & ControlModifier)
45 if (mod & AltModifier)
47 if (mod & ShiftModifier)
55 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
57 LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
58 << func.action << '\'');
62 string::size_type const res = k.parse(seq);
63 if (res == string::npos) {
66 LYXERR(Debug::KBMAP, "Parse error at position " << res
67 << " in key sequence '" << seq << "'.");
70 return res == string::npos ? 0 : res;
74 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
78 string::size_type const res = k.parse(seq);
79 if (res == string::npos)
82 LYXERR(Debug::KBMAP, "Parse error at position " << res
83 << " in key sequence '" << seq << "'.");
84 return res == string::npos ? 0 : res;
88 FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
90 KeySymbol code = seq.sequence[r];
92 return FuncRequest::unknown;
94 KeyModifier const mod1 = seq.modifiers[r].first;
95 KeyModifier const mod2 = seq.modifiers[r].second;
97 // check if key is already there
98 Table::iterator end = table.end();
99 for (Table::iterator it = table.begin(); it != end; ++it) {
101 && mod1 == it->mod.first
102 && mod2 == it->mod.second) {
103 if (r + 1 == seq.length())
105 else if (it->table.get())
106 return it->table->getBinding(seq, r + 1);
109 return FuncRequest::unknown;
119 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
127 LexerKeyword bindTags[] = {
128 { "\\bind", BN_BIND },
129 { "\\bind_file", BN_BINDFILE },
130 { "\\unbind", BN_UNBIND },
133 Lexer lexrc(bindTags);
134 if (lyxerr.debugging(Debug::PARSER))
135 lexrc.printTable(lyxerr);
137 FileName const tmp = i18nLibFileSearch("bind", bind_file, "bind");
140 LYXERR0("KeyMap::read: cannot open bind file:" << tmp);
144 LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
147 while (lexrc.isOK()) {
148 switch (lexrc.lex()) {
150 case Lexer::LEX_UNDEF:
151 lexrc.printError("Unknown tag `$$Token'");
155 case Lexer::LEX_FEOF:
160 lexrc.printError("BN_BIND: Missing key sequence");
164 string seq = lexrc.getString();
166 if (!lexrc.next(true)) {
167 lexrc.printError("BN_BIND: missing command");
171 string cmd = lexrc.getString();
173 FuncRequest func = lyxaction.lookupFunc(cmd);
174 if (func.action == LFUN_UNKNOWN_ACTION) {
175 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
186 lexrc.printError("BN_UNBIND: Missing key sequence");
190 string seq = lexrc.getString();
192 if (!lexrc.next(true)) {
193 lexrc.printError("BN_UNBIND: missing command");
197 string cmd = lexrc.getString();
199 FuncRequest func = lyxaction.lookupFunc(cmd);
200 if (func.action == LFUN_UNKNOWN_ACTION) {
201 lexrc.printError("BN_UNBIND: Unknown LyX"
202 " function `$$Token'");
208 unbind_map->bind(seq, func);
216 lexrc.printError("BN_BINDFILE: Missing file name");
220 string const tmp = lexrc.getString();
221 error |= !read(tmp, unbind_map);
227 LYXERR0("KeyMap::read: error while reading bind file:" << tmp);
232 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
234 ofstream os(bind_file.c_str(),
235 append ? (ios::app | ios::out) : ios::out);
238 os << "## This file is automatically generated by lyx\n"
239 << "## All modifications will be lost\n\n";
241 string tag = unbind ? "\\unbind" : "\\bind";
242 BindingList const list = listBindings(false);
243 BindingList::const_iterator it = list.begin();
244 BindingList::const_iterator it_end = list.end();
245 for (; it != it_end; ++it) {
246 FuncCode action = it->request.action;
247 string arg = to_utf8(it->request.argument());
250 << to_utf8(it->sequence.print(KeySequence::BindFile))
252 << lyxaction.getActionName(action)
253 << (arg.empty() ? "" : " ") << arg
261 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
262 KeyModifier mod, KeySequence * seq) const
265 seq->curmap = seq->stdmap;
267 return FuncRequest::unknown;
270 Table::const_iterator end = table.end();
271 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
272 KeyModifier mask = cit->mod.second;
273 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
275 if (cit->code == key && cit->mod.first == check) {
277 if (cit->table.get()) {
278 // this is a prefix key - set new map
279 seq->curmap = cit->table.get();
280 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
283 // final key - reset map
284 seq->curmap = seq->stdmap;
291 // error - key not found:
292 seq->curmap = seq->stdmap;
295 return FuncRequest::unknown;
299 docstring const KeyMap::print(bool forgui) const
302 Table::const_iterator end = table.end();
303 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
304 buf += cit->code.print(cit->mod.first, forgui);
311 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
313 KeySymbol code = seq->sequence[r];
317 KeyModifier const mod1 = seq->modifiers[r].first;
318 KeyModifier const mod2 = seq->modifiers[r].second;
320 // check if key is already there
321 Table::iterator end = table.end();
322 for (Table::iterator it = table.begin(); it != end; ++it) {
324 && mod1 == it->mod.first
325 && mod2 == it->mod.second) {
327 if (r + 1 == seq->length()) {
328 LYXERR(Debug::KBMAP, "Warning: New binding for '"
329 << to_utf8(seq->print(KeySequence::Portable))
330 << "' is overriding old binding...");
331 if (it->table.get()) {
335 it->func.origin = FuncRequest::KEYBOARD;
337 } else if (!it->table.get()) {
338 lyxerr << "Error: New binding for '"
339 << to_utf8(seq->print(KeySequence::Portable))
340 << "' is overriding old binding..."
344 it->table->bind(seq, func, r + 1);
350 Table::iterator newone = table.insert(table.end(), Key());
352 newone->mod = seq->modifiers[r];
353 if (r + 1 == seq->length()) {
355 newone->func.origin = FuncRequest::KEYBOARD;
356 newone->table.reset();
358 newone->table.reset(new KeyMap);
359 newone->table->bind(seq, func, r + 1);
364 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
366 KeySymbol code = seq->sequence[r];
370 KeyModifier const mod1 = seq->modifiers[r].first;
371 KeyModifier const mod2 = seq->modifiers[r].second;
373 // check if key is already there
374 Table::iterator end = table.end();
375 Table::iterator remove = end;
376 for (Table::iterator it = table.begin(); it != end; ++it) {
378 && mod1 == it->mod.first
379 && mod2 == it->mod.second) {
381 if (r + 1 == seq->length()) {
382 if (it->func == func) {
387 } else if (it->table.get()) {
388 it->table->unbind(seq, func, r + 1);
389 if (it->table->empty())
400 docstring KeyMap::printBindings(FuncRequest const & func) const
402 Bindings bindings = findBindings(func);
403 if (bindings.empty())
406 odocstringstream res;
407 Bindings::const_iterator cit = bindings.begin();
408 Bindings::const_iterator cit_end = bindings.end();
409 // prin the first item
410 res << cit->print(KeySequence::ForGui);
411 // more than one shortcuts?
412 for (++cit; cit != cit_end; ++cit)
413 res << ", " << cit->print(KeySequence::ForGui);
418 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
420 return findBindings(func, KeySequence(0, 0));
424 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
425 KeySequence const & prefix) const
431 Table::const_iterator end = table.end();
432 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
433 if (cit->table.get()) {
434 KeySequence seq = prefix;
435 seq.addkey(cit->code, cit->mod.first);
436 Bindings res2 = cit->table->findBindings(func, seq);
437 res.insert(res.end(), res2.begin(), res2.end());
438 } else if (cit->func == func) {
439 KeySequence seq = prefix;
440 seq.addkey(cit->code, cit->mod.first);
449 KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
452 listBindings(list, KeySequence(0, 0), tag);
454 LyXAction::const_func_iterator fit = lyxaction.func_begin();
455 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
456 for (; fit != fit_end; ++fit) {
457 FuncCode action = fit->second;
458 bool has_action = false;
459 BindingList::const_iterator it = list.begin();
460 BindingList::const_iterator it_end = list.end();
461 for (; it != it_end; ++it)
462 if (it->request.action == action) {
467 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
474 void KeyMap::listBindings(BindingList & list,
475 KeySequence const & prefix, KeyMap::ItemType tag) const
477 Table::const_iterator it = table.begin();
478 Table::const_iterator it_end = table.end();
479 for (; it != it_end; ++it) {
480 // a LFUN_COMMAND_PREFIX
481 if (it->table.get()) {
482 KeySequence seq = prefix;
483 seq.addkey(it->code, it->mod.first);
484 it->table->listBindings(list, seq, tag);
486 KeySequence seq = prefix;
487 seq.addkey(it->code, it->mod.first);
488 list.push_back(Binding(it->func, seq, tag));