3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
14 #include "kbsequence.h"
15 #include "LyXAction.h"
16 #include "support/filetools.h"
21 using lyx::support::i18nLibFileSearch;
23 string const kb_keymap::printKeysym(LyXKeySymPtr key,
24 key_modifier::state mod)
28 string const s = key->getSymbolName();
30 if (mod & key_modifier::shift)
32 if (mod & key_modifier::ctrl)
34 if (mod & key_modifier::alt)
42 string const kb_keymap::printKey(kb_key const & key) const
44 return printKeysym(key.code, key.mod.first);
48 string::size_type kb_keymap::bind(string const & seq, int action)
50 if (lyxerr.debugging(Debug::KBMAP)) {
51 lyxerr << "BIND: Sequence `"
52 << seq << "' Action `"
53 << action << '\'' << endl;
58 string::size_type const res = k.parse(seq);
59 if (res == string::npos) {
62 lyxerr[Debug::KBMAP] << "Parse error at position " << res
63 << " in key sequence '" << seq << "'."
78 keyword_item bindTags[] = {
79 { "\\bind", BN_BIND },
80 { "\\bind_file", BN_BINDFILE }
86 bool kb_keymap::read(string const & bind_file)
88 const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
90 LyXLex lexrc(bindTags, bindCount);
91 if (lyxerr.debugging(Debug::PARSER))
92 lexrc.printTable(lyxerr);
94 string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
96 if (!lexrc.isOK()) return false;
98 lyxerr[Debug::KBMAP] << "Reading bindfile:" << tmp << endl;
101 while (lexrc.isOK()) {
102 switch (lexrc.lex()) {
103 case LyXLex::LEX_UNDEF:
104 lexrc.printError("Unknown tag `$$Token'");
106 case LyXLex::LEX_FEOF:
113 seq = lexrc.getString();
115 lexrc.printError("BN_BIND: Missing key sequence");
119 if (lexrc.next(true)) {
120 cmd = lexrc.getString();
122 lexrc.printError("BN_BIND: missing command");
126 int action = lyxaction.LookupFunc(cmd);
127 if (!action == LFUN_UNKNOWN_ACTION) {
128 lexrc.printError("BN_BIND: Unknown LyX"
129 " function `$$Token'");
133 error = (bind(seq, kb_action(action)) != string::npos);
138 string const tmp(lexrc.getString());
141 lexrc.printError("BN_BINDFILE: Missing file name");
151 lyxerr << "Error reading bind file: " << tmp << endl;
158 int kb_keymap::lookup(LyXKeySymPtr key,
159 key_modifier::state mod, kb_sequence * seq) const
162 seq->curmap = seq->stdmap;
164 return LFUN_UNKNOWN_ACTION;
167 Table::const_iterator end = table.end();
168 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
169 key_modifier::state mask(cit->mod.second);
170 key_modifier::state check =
171 static_cast<key_modifier::state>(mod & ~mask);
173 if (*(cit->code) == *key && cit->mod.first == check) {
175 if (cit->table.get()) {
176 // this is a prefix key - set new map
177 seq->curmap = cit->table.get();
180 // final key - reset map
181 seq->curmap = seq->stdmap;
188 // error - key not found:
189 seq->curmap = seq->stdmap;
191 return LFUN_UNKNOWN_ACTION;
195 string const kb_keymap::print() const
198 Table::const_iterator end = table.end();
199 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
200 buf += printKey((*cit));
207 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
209 LyXKeySymPtr code = seq->sequence[r];
213 key_modifier::state const mod1 = seq->modifiers[r].first;
214 key_modifier::state const mod2 = seq->modifiers[r].second;
216 // check if key is already there
217 Table::iterator end = table.end();
218 for (Table::iterator it = table.begin(); it != end; ++it) {
219 if (*(code) == *(it->code)
220 && mod1 == it->mod.first
221 && mod2 == it->mod.second) {
223 if (r + 1 == seq->length()) {
225 << "Warning: New binding for '"
227 << "' is overriding old binding..."
229 if (it->table.get()) {
234 } else if (!it->table.get()) {
235 lyxerr << "Error: New binding for '" << seq->print()
236 << "' is overriding old binding..."
240 it->table->defkey(seq, action, r + 1);
246 Table::iterator newone = table.insert(table.end(), kb_key());
248 newone->mod = seq->modifiers[r];
249 if (r + 1 == seq->length()) {
250 newone->action = action;
251 newone->table.reset();
254 newone->table.reset(new kb_keymap);
255 newone->table->defkey(seq, action, r + 1);
261 string const kb_keymap::findbinding(int act, string const & prefix) const
264 if (table.empty()) return res;
266 Table::const_iterator end = table.end();
267 for (Table::const_iterator cit = table.begin();
269 if (cit->table.get()) {
270 res += cit->table->findbinding(act,
274 } else if (cit->action == act) {
276 res += prefix + printKey((*cit));