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 "kbsequence.h"
20 #include "LyXAction.h"
23 #include "frontends/LyXKeySym.h"
25 #include "support/filetools.h"
32 using support::i18nLibFileSearch;
38 string const kb_keymap::printKeySym(LyXKeySym const & key,
39 key_modifier::state mod)
43 string const s = key.getSymbolName();
45 if (mod & key_modifier::shift)
47 if (mod & key_modifier::ctrl)
49 if (mod & key_modifier::alt)
57 string const kb_keymap::printKey(kb_key const & key) const
59 return key.code->print(key.mod.first);
63 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
65 if (lyxerr.debugging(Debug::KBMAP)) {
66 lyxerr << "BIND: Sequence `"
67 << seq << "' Action `"
68 << func.action << '\'' << endl;
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 << "'."
93 keyword_item bindTags[] = {
94 { "\\bind", BN_BIND },
95 { "\\bind_file", BN_BINDFILE }
101 bool kb_keymap::read(string const & bind_file)
103 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
105 LyXLex lexrc(bindTags, bindCount);
106 if (lyxerr.debugging(Debug::PARSER))
107 lexrc.printTable(lyxerr);
109 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
112 lyxerr << "kb_keymap::read: cannot open bind file:"
117 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
120 while (lexrc.isOK()) {
121 switch (lexrc.lex()) {
122 case LyXLex::LEX_UNDEF:
123 lexrc.printError("Unknown tag `$$Token'");
126 case LyXLex::LEX_FEOF:
133 seq = lexrc.getString();
135 lexrc.printError("BN_BIND: Missing key sequence");
140 if (lexrc.next(true)) {
141 cmd = lexrc.getString();
143 lexrc.printError("BN_BIND: missing command");
148 FuncRequest func = lyxaction.lookupFunc(cmd);
149 if (func. action == LFUN_UNKNOWN_ACTION) {
150 lexrc.printError("BN_BIND: Unknown LyX"
151 " function `$$Token'");
161 string const tmp(lexrc.getString());
164 lexrc.printError("BN_BINDFILE: Missing file name");
174 lyxerr << "kb_keymap::read: error while reading bind file:"
181 kb_keymap::lookup(LyXKeySymPtr key,
182 key_modifier::state mod, kb_sequence * seq) const
184 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
187 seq->curmap = seq->stdmap;
192 Table::const_iterator end = table.end();
193 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
194 key_modifier::state mask(cit->mod.second);
195 key_modifier::state check =
196 static_cast<key_modifier::state>(mod & ~mask);
198 if (*(cit->code) == *key && cit->mod.first == check) {
200 if (cit->table.get()) {
201 // this is a prefix key - set new map
202 seq->curmap = cit->table.get();
203 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
206 // final key - reset map
207 seq->curmap = seq->stdmap;
214 // error - key not found:
215 seq->curmap = seq->stdmap;
222 string const kb_keymap::print() const
225 Table::const_iterator end = table.end();
226 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
227 buf += printKey((*cit));
234 void kb_keymap::defkey(kb_sequence * seq,
235 FuncRequest const & func, unsigned int r)
237 LyXKeySymPtr code = seq->sequence[r];
241 key_modifier::state const mod1 = seq->modifiers[r].first;
242 key_modifier::state const mod2 = seq->modifiers[r].second;
244 // check if key is already there
245 Table::iterator end = table.end();
246 for (Table::iterator it = table.begin(); it != end; ++it) {
247 if (*(code) == *(it->code)
248 && mod1 == it->mod.first
249 && mod2 == it->mod.second) {
251 if (r + 1 == seq->length()) {
253 << "Warning: New binding for '"
255 << "' is overriding old binding..."
257 if (it->table.get()) {
261 it->func.origin = FuncRequest::KEYBOARD;
263 } else if (!it->table.get()) {
264 lyxerr << "Error: New binding for '" << seq->print()
265 << "' is overriding old binding..."
269 it->table->defkey(seq, func, r + 1);
275 Table::iterator newone = table.insert(table.end(), kb_key());
277 newone->mod = seq->modifiers[r];
278 if (r + 1 == seq->length()) {
280 newone->func.origin = FuncRequest::KEYBOARD;
281 newone->table.reset();
284 newone->table.reset(new kb_keymap);
285 newone->table->defkey(seq, func, r + 1);
291 string const kb_keymap::printbindings(FuncRequest const & func) const
293 std::ostringstream res;
294 Bindings bindings = findbindings(func);
295 for (Bindings::const_iterator cit = bindings.begin();
296 cit != bindings.end() ; ++cit)
297 res << '[' << cit->print() << ']';
303 kb_keymap::findbindings(FuncRequest const & func) const
305 return findbindings(func, kb_sequence(0, 0));
310 kb_keymap::findbindings(FuncRequest const & func,
311 kb_sequence const & prefix) const
314 if (table.empty()) return res;
316 Table::const_iterator end = table.end();
317 for (Table::const_iterator cit = table.begin();
319 if (cit->table.get()) {
320 kb_sequence seq = prefix;
321 seq.addkey(cit->code, cit->mod.first);
323 cit->table->findbindings(func, seq);
324 res.insert(res.end(), res2.begin(), res2.end());
325 } else if (cit->func == func) {
326 kb_sequence seq = prefix;
327 seq.addkey(cit->code, cit->mod.first);
336 std::pair<LyXKeySym const *, key_modifier::state>
337 kb_keymap::find1keybinding(FuncRequest const & func) const
339 Table::const_iterator end = table.end();
340 for (Table::const_iterator cit = table.begin();
342 if (!cit->table.get() && cit->func == func)
343 return std::make_pair(cit->code.get(), cit->mod.first);
346 return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);