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::FileName;
33 using support::i18nLibFileSearch;
39 string const kb_keymap::printKeySym(LyXKeySym const & key,
40 key_modifier::state mod)
44 string const s = key.getSymbolName();
46 if (mod & key_modifier::shift)
48 if (mod & key_modifier::ctrl)
50 if (mod & key_modifier::alt)
58 docstring const kb_keymap::printKey(kb_key const & key) const
60 return key.code->print(key.mod.first);
64 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
66 if (lyxerr.debugging(Debug::KBMAP)) {
67 lyxerr << "BIND: Sequence `"
68 << seq << "' Action `"
69 << func.action << '\'' << endl;
74 string::size_type const res = k.parse(seq);
75 if (res == string::npos) {
78 lyxerr[Debug::KBMAP] << "Parse error at position " << res
79 << " in key sequence '" << seq << "'."
94 keyword_item bindTags[] = {
95 { "\\bind", BN_BIND },
96 { "\\bind_file", BN_BINDFILE }
102 bool kb_keymap::read(string const & bind_file)
104 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
106 LyXLex lexrc(bindTags, bindCount);
107 if (lyxerr.debugging(Debug::PARSER))
108 lexrc.printTable(lyxerr);
110 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
113 lyxerr << "kb_keymap::read: cannot open bind file:"
118 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
121 while (lexrc.isOK()) {
122 switch (lexrc.lex()) {
123 case LyXLex::LEX_UNDEF:
124 lexrc.printError("Unknown tag `$$Token'");
127 case LyXLex::LEX_FEOF:
134 seq = lexrc.getString();
136 lexrc.printError("BN_BIND: Missing key sequence");
141 if (lexrc.next(true)) {
142 cmd = lexrc.getString();
144 lexrc.printError("BN_BIND: missing command");
149 FuncRequest func = lyxaction.lookupFunc(cmd);
150 if (func. action == LFUN_UNKNOWN_ACTION) {
151 lexrc.printError("BN_BIND: Unknown LyX"
152 " function `$$Token'");
162 string const tmp(lexrc.getString());
165 lexrc.printError("BN_BINDFILE: Missing file name");
175 lyxerr << "kb_keymap::read: error while reading bind file:"
182 kb_keymap::lookup(LyXKeySymPtr key,
183 key_modifier::state mod, kb_sequence * seq) const
185 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
188 seq->curmap = seq->stdmap;
193 Table::const_iterator end = table.end();
194 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
195 key_modifier::state mask(cit->mod.second);
196 key_modifier::state check =
197 static_cast<key_modifier::state>(mod & ~mask);
199 if (*(cit->code) == *key && cit->mod.first == check) {
201 if (cit->table.get()) {
202 // this is a prefix key - set new map
203 seq->curmap = cit->table.get();
204 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
207 // final key - reset map
208 seq->curmap = seq->stdmap;
215 // error - key not found:
216 seq->curmap = seq->stdmap;
223 docstring const kb_keymap::print() const
226 Table::const_iterator end = table.end();
227 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
228 buf += printKey((*cit));
235 void kb_keymap::defkey(kb_sequence * seq,
236 FuncRequest const & func, unsigned int r)
238 LyXKeySymPtr code = seq->sequence[r];
242 key_modifier::state const mod1 = seq->modifiers[r].first;
243 key_modifier::state const mod2 = seq->modifiers[r].second;
245 // check if key is already there
246 Table::iterator end = table.end();
247 for (Table::iterator it = table.begin(); it != end; ++it) {
248 if (*(code) == *(it->code)
249 && mod1 == it->mod.first
250 && mod2 == it->mod.second) {
252 if (r + 1 == seq->length()) {
254 << "Warning: New binding for '"
255 << to_utf8(seq->print())
256 << "' is overriding old binding..."
258 if (it->table.get()) {
262 it->func.origin = FuncRequest::KEYBOARD;
264 } else if (!it->table.get()) {
265 lyxerr << "Error: New binding for '"
266 << to_utf8(seq->print())
267 << "' is overriding old binding..."
271 it->table->defkey(seq, func, r + 1);
277 Table::iterator newone = table.insert(table.end(), kb_key());
279 newone->mod = seq->modifiers[r];
280 if (r + 1 == seq->length()) {
282 newone->func.origin = FuncRequest::KEYBOARD;
283 newone->table.reset();
286 newone->table.reset(new kb_keymap);
287 newone->table->defkey(seq, func, r + 1);
293 docstring const kb_keymap::printbindings(FuncRequest const & func) const
295 odocstringstream res;
296 Bindings bindings = findbindings(func);
297 for (Bindings::const_iterator cit = bindings.begin();
298 cit != bindings.end() ; ++cit)
299 res << '[' << cit->print() << ']';
305 kb_keymap::findbindings(FuncRequest const & func) const
307 return findbindings(func, kb_sequence(0, 0));
312 kb_keymap::findbindings(FuncRequest const & func,
313 kb_sequence const & prefix) const
316 if (table.empty()) return res;
318 Table::const_iterator end = table.end();
319 for (Table::const_iterator cit = table.begin();
321 if (cit->table.get()) {
322 kb_sequence seq = prefix;
323 seq.addkey(cit->code, cit->mod.first);
325 cit->table->findbindings(func, seq);
326 res.insert(res.end(), res2.begin(), res2.end());
327 } else if (cit->func == func) {
328 kb_sequence seq = prefix;
329 seq.addkey(cit->code, cit->mod.first);
338 std::pair<LyXKeySym const *, key_modifier::state>
339 kb_keymap::find1keybinding(FuncRequest const & func) const
341 Table::const_iterator end = table.end();
342 for (Table::const_iterator cit = table.begin();
344 if (!cit->table.get() && cit->func == func)
345 return std::make_pair(cit->code.get(), cit->mod.first);
348 return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);