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"
29 using lyx::support::i18nLibFileSearch;
35 string const kb_keymap::printKeySym(LyXKeySym const & key,
36 key_modifier::state mod)
40 string const s = key.getSymbolName();
42 if (mod & key_modifier::shift)
44 if (mod & key_modifier::ctrl)
46 if (mod & key_modifier::alt)
54 string const kb_keymap::printKey(kb_key const & key) const
56 return key.code->print(key.mod.first);
60 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
62 if (lyxerr.debugging(Debug::KBMAP)) {
63 lyxerr << "BIND: Sequence `"
64 << seq << "' Action `"
65 << func.action << '\'' << endl;
70 string::size_type const res = k.parse(seq);
71 if (res == string::npos) {
74 lyxerr[Debug::KBMAP] << "Parse error at position " << res
75 << " in key sequence '" << seq << "'."
90 keyword_item bindTags[] = {
91 { "\\bind", BN_BIND },
92 { "\\bind_file", BN_BINDFILE }
98 bool kb_keymap::read(string const & bind_file)
100 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
102 LyXLex lexrc(bindTags, bindCount);
103 if (lyxerr.debugging(Debug::PARSER))
104 lexrc.printTable(lyxerr);
106 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
109 lyxerr << "kb_keymap::read: cannot open bind file:"
114 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
117 while (lexrc.isOK()) {
118 switch (lexrc.lex()) {
119 case LyXLex::LEX_UNDEF:
120 lexrc.printError("Unknown tag `$$Token'");
123 case LyXLex::LEX_FEOF:
130 seq = lexrc.getString();
132 lexrc.printError("BN_BIND: Missing key sequence");
137 if (lexrc.next(true)) {
138 cmd = lexrc.getString();
140 lexrc.printError("BN_BIND: missing command");
145 FuncRequest func = lyxaction.lookupFunc(cmd);
146 if (func. action == LFUN_UNKNOWN_ACTION) {
147 lexrc.printError("BN_BIND: Unknown LyX"
148 " function `$$Token'");
158 string const tmp(lexrc.getString());
161 lexrc.printError("BN_BINDFILE: Missing file name");
171 lyxerr << "kb_keymap::read: error while reading bind file:"
178 kb_keymap::lookup(LyXKeySymPtr key,
179 key_modifier::state mod, kb_sequence * seq) const
181 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
184 seq->curmap = seq->stdmap;
189 Table::const_iterator end = table.end();
190 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
191 key_modifier::state mask(cit->mod.second);
192 key_modifier::state check =
193 static_cast<key_modifier::state>(mod & ~mask);
195 if (*(cit->code) == *key && cit->mod.first == check) {
197 if (cit->table.get()) {
198 // this is a prefix key - set new map
199 seq->curmap = cit->table.get();
200 static FuncRequest prefix(LFUN_PREFIX);
203 // final key - reset map
204 seq->curmap = seq->stdmap;
211 // error - key not found:
212 seq->curmap = seq->stdmap;
219 string const kb_keymap::print() const
222 Table::const_iterator end = table.end();
223 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
224 buf += printKey((*cit));
231 void kb_keymap::defkey(kb_sequence * seq,
232 FuncRequest const & func, unsigned int r)
234 LyXKeySymPtr code = seq->sequence[r];
238 key_modifier::state const mod1 = seq->modifiers[r].first;
239 key_modifier::state const mod2 = seq->modifiers[r].second;
241 // check if key is already there
242 Table::iterator end = table.end();
243 for (Table::iterator it = table.begin(); it != end; ++it) {
244 if (*(code) == *(it->code)
245 && mod1 == it->mod.first
246 && mod2 == it->mod.second) {
248 if (r + 1 == seq->length()) {
250 << "Warning: New binding for '"
252 << "' is overriding old binding..."
254 if (it->table.get()) {
258 it->func.origin = FuncRequest::KEYBOARD;
260 } else if (!it->table.get()) {
261 lyxerr << "Error: New binding for '" << seq->print()
262 << "' is overriding old binding..."
266 it->table->defkey(seq, func, r + 1);
272 Table::iterator newone = table.insert(table.end(), kb_key());
274 newone->mod = seq->modifiers[r];
275 if (r + 1 == seq->length()) {
277 newone->func.origin = FuncRequest::KEYBOARD;
278 newone->table.reset();
281 newone->table.reset(new kb_keymap);
282 newone->table->defkey(seq, func, r + 1);
288 string const kb_keymap::printbindings(FuncRequest const & func) const
290 std::ostringstream res;
291 Bindings bindings = findbindings(func);
292 for (Bindings::const_iterator cit = bindings.begin();
293 cit != bindings.end() ; ++cit)
294 res << '[' << cit->print() << ']';
300 kb_keymap::findbindings(FuncRequest const & func) const
302 return findbindings(func, kb_sequence(0, 0));
307 kb_keymap::findbindings(FuncRequest const & func,
308 kb_sequence const & prefix) const
311 if (table.empty()) return res;
313 Table::const_iterator end = table.end();
314 for (Table::const_iterator cit = table.begin();
316 if (cit->table.get()) {
317 kb_sequence seq = prefix;
318 seq.addkey(cit->code, cit->mod.first);
320 cit->table->findbindings(func, seq);
321 res.insert(res.end(), res2.begin(), res2.end());
322 } else if (cit->func == func) {
323 kb_sequence seq = prefix;
324 seq.addkey(cit->code, cit->mod.first);
333 std::pair<LyXKeySym const *, key_modifier::state>
334 kb_keymap::find1keybinding(FuncRequest const & func) const
336 Table::const_iterator end = table.end();
337 for (Table::const_iterator cit = table.begin();
339 if (!cit->table.get() && cit->func == func)
340 return std::make_pair(cit->code.get(), cit->mod.first);
343 return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);