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.
18 #include "kbsequence.h"
19 #include "LyXAction.h"
20 #include "support/filetools.h"
25 using lyx::support::i18nLibFileSearch;
27 string const kb_keymap::printKeysym(LyXKeySymPtr key,
28 key_modifier::state mod)
32 string const s = key->getSymbolName();
34 if (mod & key_modifier::shift)
36 if (mod & key_modifier::ctrl)
38 if (mod & key_modifier::alt)
46 string const kb_keymap::printKey(kb_key const & key) const
48 return printKeysym(key.code, key.mod.first);
52 string::size_type kb_keymap::bind(string const & seq, int action)
54 if (lyxerr.debugging(Debug::KBMAP)) {
55 lyxerr << "BIND: Sequence `"
56 << seq << "' Action `"
57 << action << '\'' << endl;
62 string::size_type const res = k.parse(seq);
63 if (res == string::npos) {
66 lyxerr[Debug::KBMAP] << "Parse error at position " << res
67 << " in key sequence '" << seq << "'."
82 keyword_item bindTags[] = {
83 { "\\bind", BN_BIND },
84 { "\\bind_file", BN_BINDFILE }
90 bool kb_keymap::read(string const & bind_file)
92 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
94 LyXLex lexrc(bindTags, bindCount);
95 if (lyxerr.debugging(Debug::PARSER))
96 lexrc.printTable(lyxerr);
98 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
101 lyxerr << "kb_keymap::read: cannot open bind file:"
106 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
109 while (!error && lexrc.isOK()) {
110 switch (lexrc.lex()) {
111 case LyXLex::LEX_UNDEF:
112 lexrc.printError("Unknown tag `$$Token'");
115 case LyXLex::LEX_FEOF:
122 seq = lexrc.getString();
124 lexrc.printError("BN_BIND: Missing key sequence");
129 if (lexrc.next(true)) {
130 cmd = lexrc.getString();
132 lexrc.printError("BN_BIND: missing command");
137 int action = lyxaction.LookupFunc(cmd);
138 if (!action == LFUN_UNKNOWN_ACTION) {
139 lexrc.printError("BN_BIND: Unknown LyX"
140 " function `$$Token'");
145 bind(seq, kb_action(action));
150 string const tmp(lexrc.getString());
153 lexrc.printError("BN_BINDFILE: Missing file name");
163 lyxerr << "kb_keymap::read: error while reading bind file:"
169 int kb_keymap::lookup(LyXKeySymPtr key,
170 key_modifier::state mod, kb_sequence * seq) const
173 seq->curmap = seq->stdmap;
175 return LFUN_UNKNOWN_ACTION;
178 Table::const_iterator end = table.end();
179 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
180 key_modifier::state mask(cit->mod.second);
181 key_modifier::state check =
182 static_cast<key_modifier::state>(mod & ~mask);
184 if (*(cit->code) == *key && cit->mod.first == check) {
186 if (cit->table.get()) {
187 // this is a prefix key - set new map
188 seq->curmap = cit->table.get();
191 // final key - reset map
192 seq->curmap = seq->stdmap;
199 // error - key not found:
200 seq->curmap = seq->stdmap;
202 return LFUN_UNKNOWN_ACTION;
206 string const kb_keymap::print() const
209 Table::const_iterator end = table.end();
210 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
211 buf += printKey((*cit));
218 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
220 LyXKeySymPtr code = seq->sequence[r];
224 key_modifier::state const mod1 = seq->modifiers[r].first;
225 key_modifier::state const mod2 = seq->modifiers[r].second;
227 // check if key is already there
228 Table::iterator end = table.end();
229 for (Table::iterator it = table.begin(); it != end; ++it) {
230 if (*(code) == *(it->code)
231 && mod1 == it->mod.first
232 && mod2 == it->mod.second) {
234 if (r + 1 == seq->length()) {
236 << "Warning: New binding for '"
238 << "' is overriding old binding..."
240 if (it->table.get()) {
245 } else if (!it->table.get()) {
246 lyxerr << "Error: New binding for '" << seq->print()
247 << "' is overriding old binding..."
251 it->table->defkey(seq, action, r + 1);
257 Table::iterator newone = table.insert(table.end(), kb_key());
259 newone->mod = seq->modifiers[r];
260 if (r + 1 == seq->length()) {
261 newone->action = action;
262 newone->table.reset();
265 newone->table.reset(new kb_keymap);
266 newone->table->defkey(seq, action, r + 1);
272 string const kb_keymap::findbinding(int act, string const & prefix) const
275 if (table.empty()) return res;
277 Table::const_iterator end = table.end();
278 for (Table::const_iterator cit = table.begin();
280 if (cit->table.get()) {
281 res += cit->table->findbinding(act,
285 } else if (cit->action == act) {
287 res += prefix + printKey((*cit));