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");
97 lyxerr << "kb_keymap::read: cannot open bind file:"
102 lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
105 while (!error && lexrc.isOK()) {
106 switch (lexrc.lex()) {
107 case LyXLex::LEX_UNDEF:
108 lexrc.printError("Unknown tag `$$Token'");
111 case LyXLex::LEX_FEOF:
118 seq = lexrc.getString();
120 lexrc.printError("BN_BIND: Missing key sequence");
125 if (lexrc.next(true)) {
126 cmd = lexrc.getString();
128 lexrc.printError("BN_BIND: missing command");
133 int action = lyxaction.LookupFunc(cmd);
134 if (!action == LFUN_UNKNOWN_ACTION) {
135 lexrc.printError("BN_BIND: Unknown LyX"
136 " function `$$Token'");
141 bind(seq, kb_action(action));
146 string const tmp(lexrc.getString());
149 lexrc.printError("BN_BINDFILE: Missing file name");
159 lyxerr << "kb_keymap::read: error while reading bind file:"
165 int kb_keymap::lookup(LyXKeySymPtr key,
166 key_modifier::state mod, kb_sequence * seq) const
169 seq->curmap = seq->stdmap;
171 return LFUN_UNKNOWN_ACTION;
174 Table::const_iterator end = table.end();
175 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
176 key_modifier::state mask(cit->mod.second);
177 key_modifier::state check =
178 static_cast<key_modifier::state>(mod & ~mask);
180 if (*(cit->code) == *key && cit->mod.first == check) {
182 if (cit->table.get()) {
183 // this is a prefix key - set new map
184 seq->curmap = cit->table.get();
187 // final key - reset map
188 seq->curmap = seq->stdmap;
195 // error - key not found:
196 seq->curmap = seq->stdmap;
198 return LFUN_UNKNOWN_ACTION;
202 string const kb_keymap::print() const
205 Table::const_iterator end = table.end();
206 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
207 buf += printKey((*cit));
214 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
216 LyXKeySymPtr code = seq->sequence[r];
220 key_modifier::state const mod1 = seq->modifiers[r].first;
221 key_modifier::state const mod2 = seq->modifiers[r].second;
223 // check if key is already there
224 Table::iterator end = table.end();
225 for (Table::iterator it = table.begin(); it != end; ++it) {
226 if (*(code) == *(it->code)
227 && mod1 == it->mod.first
228 && mod2 == it->mod.second) {
230 if (r + 1 == seq->length()) {
232 << "Warning: New binding for '"
234 << "' is overriding old binding..."
236 if (it->table.get()) {
241 } else if (!it->table.get()) {
242 lyxerr << "Error: New binding for '" << seq->print()
243 << "' is overriding old binding..."
247 it->table->defkey(seq, action, r + 1);
253 Table::iterator newone = table.insert(table.end(), kb_key());
255 newone->mod = seq->modifiers[r];
256 if (r + 1 == seq->length()) {
257 newone->action = action;
258 newone->table.reset();
261 newone->table.reset(new kb_keymap);
262 newone->table->defkey(seq, action, r + 1);
268 string const kb_keymap::findbinding(int act, string const & prefix) const
271 if (table.empty()) return res;
273 Table::const_iterator end = table.end();
274 for (Table::const_iterator cit = table.begin();
276 if (cit->table.get()) {
277 res += cit->table->findbinding(act,
281 } else if (cit->action == act) {
283 res += prefix + printKey((*cit));