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 "frontends/KeySymbol.h"
25 #include "support/filetools.h"
32 using support::FileName;
33 using support::i18nLibFileSearch;
39 string const KeyMap::printKeySym(KeySymbol 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 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
60 LYXERR(Debug::KBMAP) << "BIND: Sequence `"
61 << seq << "' Action `"
62 << func.action << '\'' << endl;
66 string::size_type const res = k.parse(seq);
67 if (res == string::npos) {
70 LYXERR(Debug::KBMAP) << "Parse error at position " << res
71 << " in key sequence '" << seq << "'."
86 keyword_item bindTags[] = {
87 { "\\bind", BN_BIND },
88 { "\\bind_file", BN_BINDFILE }
94 bool KeyMap::read(string const & bind_file)
96 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
98 Lexer lexrc(bindTags, bindCount);
99 if (lyxerr.debugging(Debug::PARSER))
100 lexrc.printTable(lyxerr);
102 FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
105 lyxerr << "KeyMap::read: cannot open bind file:"
110 LYXERR(Debug::KBMAP) << "Reading bind file:" << tmp << endl;
113 while (lexrc.isOK()) {
114 switch (lexrc.lex()) {
115 case Lexer::LEX_UNDEF:
116 lexrc.printError("Unknown tag `$$Token'");
119 case Lexer::LEX_FEOF:
126 seq = lexrc.getString();
128 lexrc.printError("BN_BIND: Missing key sequence");
133 if (lexrc.next(true)) {
134 cmd = lexrc.getString();
136 lexrc.printError("BN_BIND: missing command");
141 FuncRequest func = lyxaction.lookupFunc(cmd);
142 if (func. action == LFUN_UNKNOWN_ACTION) {
143 lexrc.printError("BN_BIND: Unknown LyX"
144 " function `$$Token'");
154 string const tmp(lexrc.getString());
157 lexrc.printError("BN_BINDFILE: Missing file name");
167 lyxerr << "KeyMap::read: error while reading bind file:"
174 KeyMap::lookup(KeySymbolPtr key,
175 key_modifier::state mod, KeySequence * seq) const
177 static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
180 seq->curmap = seq->stdmap;
185 Table::const_iterator end = table.end();
186 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
187 key_modifier::state mask(cit->mod.second);
188 key_modifier::state check =
189 static_cast<key_modifier::state>(mod & ~mask);
191 if (*(cit->code) == *key && cit->mod.first == check) {
193 if (cit->table.get()) {
194 // this is a prefix key - set new map
195 seq->curmap = cit->table.get();
196 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
199 // final key - reset map
200 seq->curmap = seq->stdmap;
207 // error - key not found:
208 seq->curmap = seq->stdmap;
215 docstring const KeyMap::print(bool forgui) const
218 Table::const_iterator end = table.end();
219 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
220 buf += cit->code->print(cit->mod.first, forgui);
227 void KeyMap::defkey(KeySequence * seq, FuncRequest const & func, unsigned int r)
229 KeySymbolPtr code = seq->sequence[r];
233 key_modifier::state const mod1 = seq->modifiers[r].first;
234 key_modifier::state const mod2 = seq->modifiers[r].second;
236 // check if key is already there
237 Table::iterator end = table.end();
238 for (Table::iterator it = table.begin(); it != end; ++it) {
239 if (*(code) == *(it->code)
240 && mod1 == it->mod.first
241 && mod2 == it->mod.second) {
243 if (r + 1 == seq->length()) {
245 << "Warning: New binding for '"
246 << to_utf8(seq->print(false))
247 << "' is overriding old binding..."
249 if (it->table.get()) {
253 it->func.origin = FuncRequest::KEYBOARD;
255 } else if (!it->table.get()) {
256 lyxerr << "Error: New binding for '"
257 << to_utf8(seq->print(false))
258 << "' is overriding old binding..."
262 it->table->defkey(seq, func, r + 1);
268 Table::iterator newone = table.insert(table.end(), Key());
270 newone->mod = seq->modifiers[r];
271 if (r + 1 == seq->length()) {
273 newone->func.origin = FuncRequest::KEYBOARD;
274 newone->table.reset();
276 newone->table.reset(new KeyMap);
277 newone->table->defkey(seq, func, r + 1);
282 docstring const KeyMap::printbindings(FuncRequest const & func) const
284 odocstringstream res;
285 Bindings bindings = findbindings(func);
286 for (Bindings::const_iterator cit = bindings.begin();
287 cit != bindings.end() ; ++cit)
288 res << '[' << cit->print(true) << ']';
293 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func) const
295 return findbindings(func, KeySequence(0, 0));
299 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func,
300 KeySequence const & prefix) const
303 if (table.empty()) return res;
305 Table::const_iterator end = table.end();
306 for (Table::const_iterator cit = table.begin();
308 if (cit->table.get()) {
309 KeySequence seq = prefix;
310 seq.addkey(cit->code, cit->mod.first);
312 cit->table->findbindings(func, seq);
313 res.insert(res.end(), res2.begin(), res2.end());
314 } else if (cit->func == func) {
315 KeySequence seq = prefix;
316 seq.addkey(cit->code, cit->mod.first);
325 std::pair<KeySymbol const *, key_modifier::state>
326 KeyMap::find1keybinding(FuncRequest const & func) const
328 Table::const_iterator end = table.end();
329 for (Table::const_iterator cit = table.begin();
331 if (!cit->table.get() && cit->func == func)
332 return std::make_pair(cit->code.get(), cit->mod.first);
335 return std::make_pair<KeySymbol const *, key_modifier::state>(0, key_modifier::none);