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.
19 #include "KeySequence.h"
20 #include "LyXAction.h"
23 #include "support/filetools.h"
35 using support::FileName;
36 using support::i18nLibFileSearch;
39 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
43 string const s = key.getSymbolName();
45 if (mod & ShiftModifier)
47 if (mod & ControlModifier)
49 if (mod & AltModifier)
57 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
59 LYXERR(Debug::KBMAP) << "BIND: Sequence `"
60 << seq << "' Action `"
61 << func.action << '\'' << endl;
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 << "'."
85 keyword_item bindTags[] = {
86 { "\\bind", BN_BIND },
87 { "\\bind_file", BN_BINDFILE }
93 bool KeyMap::read(string const & bind_file)
95 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
97 Lexer lexrc(bindTags, bindCount);
98 if (lyxerr.debugging(Debug::PARSER))
99 lexrc.printTable(lyxerr);
101 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
104 lyxerr << "KeyMap::read: cannot open bind file:"
109 LYXERR(Debug::KBMAP) << "Reading bind file:" << tmp << endl;
112 while (lexrc.isOK()) {
113 switch (lexrc.lex()) {
114 case Lexer::LEX_UNDEF:
115 lexrc.printError("Unknown tag `$$Token'");
118 case Lexer::LEX_FEOF:
125 seq = lexrc.getString();
127 lexrc.printError("BN_BIND: Missing key sequence");
132 if (lexrc.next(true)) {
133 cmd = lexrc.getString();
135 lexrc.printError("BN_BIND: missing command");
140 FuncRequest func = lyxaction.lookupFunc(cmd);
141 if (func. action == LFUN_UNKNOWN_ACTION) {
142 lexrc.printError("BN_BIND: Unknown LyX"
143 " function `$$Token'");
153 string const tmp(lexrc.getString());
156 lexrc.printError("BN_BINDFILE: Missing file name");
166 lyxerr << "KeyMap::read: error while reading bind file:"
172 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
173 KeyModifier mod, KeySequence * seq) const
175 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
178 seq->curmap = seq->stdmap;
183 Table::const_iterator end = table.end();
184 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
185 KeyModifier mask = cit->mod.second;
186 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
188 if (cit->code == key && cit->mod.first == check) {
190 if (cit->table.get()) {
191 // this is a prefix key - set new map
192 seq->curmap = cit->table.get();
193 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
196 // final key - reset map
197 seq->curmap = seq->stdmap;
204 // error - key not found:
205 seq->curmap = seq->stdmap;
212 docstring const KeyMap::print(bool forgui) const
215 Table::const_iterator end = table.end();
216 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
217 buf += cit->code.print(cit->mod.first, forgui);
224 void KeyMap::defkey(KeySequence * seq, FuncRequest const & func, unsigned int r)
226 KeySymbol code = seq->sequence[r];
230 KeyModifier const mod1 = seq->modifiers[r].first;
231 KeyModifier const mod2 = seq->modifiers[r].second;
233 // check if key is already there
234 Table::iterator end = table.end();
235 for (Table::iterator it = table.begin(); it != end; ++it) {
237 && mod1 == it->mod.first
238 && mod2 == it->mod.second) {
240 if (r + 1 == seq->length()) {
242 << "Warning: New binding for '"
243 << to_utf8(seq->print(false))
244 << "' is overriding old binding..."
246 if (it->table.get()) {
250 it->func.origin = FuncRequest::KEYBOARD;
252 } else if (!it->table.get()) {
253 lyxerr << "Error: New binding for '"
254 << to_utf8(seq->print(false))
255 << "' is overriding old binding..."
259 it->table->defkey(seq, func, r + 1);
265 Table::iterator newone = table.insert(table.end(), Key());
267 newone->mod = seq->modifiers[r];
268 if (r + 1 == seq->length()) {
270 newone->func.origin = FuncRequest::KEYBOARD;
271 newone->table.reset();
273 newone->table.reset(new KeyMap);
274 newone->table->defkey(seq, func, r + 1);
279 docstring const KeyMap::printbindings(FuncRequest const & func) const
281 Bindings bindings = findbindings(func);
282 if (bindings.empty())
285 odocstringstream res;
286 Bindings::const_iterator cit = bindings.begin();
287 Bindings::const_iterator cit_end = bindings.end();
288 // prin the first item
289 res << cit->print(true);
290 // more than one shortcuts?
291 for (++cit; cit != cit_end; ++cit)
292 res << ", " << cit->print(true);
297 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func) const
299 return findbindings(func, KeySequence(0, 0));
303 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func,
304 KeySequence const & prefix) const
307 if (table.empty()) return res;
309 Table::const_iterator end = table.end();
310 for (Table::const_iterator cit = table.begin();
312 if (cit->table.get()) {
313 KeySequence seq = prefix;
314 seq.addkey(cit->code, cit->mod.first);
316 cit->table->findbindings(func, seq);
317 res.insert(res.end(), res2.begin(), res2.end());
318 } else if (cit->func == func) {
319 KeySequence seq = prefix;
320 seq.addkey(cit->code, cit->mod.first);
329 KeyMap::BindingList const KeyMap::listBindings(bool unbound) const
332 listBindings(list, KeySequence(0, 0));
334 LyXAction::const_func_iterator fit = lyxaction.func_begin();
335 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
336 for (; fit != fit_end; ++fit) {
337 kb_action action = fit->second;
338 bool has_action = false;
339 BindingList::const_iterator it = list.begin();
340 BindingList::const_iterator it_end = list.end();
341 for (; it != it_end; ++it)
342 if (it->first.action == action) {
347 list.push_back(make_pair(action, KeySequence(0, 0)));
354 void KeyMap::listBindings(BindingList & list,
355 KeySequence const & prefix) const
357 Table::const_iterator it = table.begin();
358 Table::const_iterator it_end = table.end();
359 for (; it != it_end; ++it) {
360 // a LFUN_COMMAND_PREFIX
361 if (it->table.get()) {
362 KeySequence seq = prefix;
363 seq.addkey(it->code, it->mod.first);
364 it->table->listBindings(list, seq);
366 KeySequence seq = prefix;
367 seq.addkey(it->code, it->mod.first);
368 list.push_back(make_pair(it->func, seq));