3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation
17 #include "commandtags.h"
18 #include "kbsequence.h"
25 // The only modifiers that we handle. We want to throw away things
27 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
30 string const kb_keymap::printKeysym(unsigned int key, key_modifier::state mod)
34 char const * const s = XKeysymToString(key);
36 if (mod & key_modifier::shift) buf += "S-";
37 if (mod & key_modifier::ctrl) buf += "C-";
38 if (mod & key_modifier::alt) buf += "M-";
44 char kb_keymap::getiso(unsigned int c)
46 switch (c & 0x0000FF00) {
48 case 0x00000000: break;
59 // latin 8 byte 3 = 18 (0x12)
61 // latin 9 byte 3 = 19 (0x13)
71 string const kb_keymap::printKey(kb_key const & key) const
73 return printKeysym(key.code, key.mod.first);
77 string::size_type kb_keymap::bind(string const & seq, int action)
79 if (lyxerr.debugging(Debug::KBMAP)) {
80 lyxerr << "BIND: Sequence `"
81 << seq << "' Action `"
82 << action << "'" << endl;
87 string::size_type const res = k.parse(seq);
88 if (res == string::npos) {
91 lyxerr[Debug::KBMAP] << "Parse error at position " << res
92 << " in key sequence '" << seq << "'."
100 int kb_keymap::lookup(unsigned int key,
101 key_modifier::state mod, kb_sequence * seq) const
104 seq->curmap = seq->stdmap;
106 return LFUN_UNKNOWN_ACTION;
109 for (Table::const_iterator cit = table.begin();
110 cit != table.end(); ++cit) {
111 key_modifier::state mask(cit->mod.second);
112 key_modifier::state check =
113 static_cast<key_modifier::state>(mod & ~mask);
115 if (cit->code == key && cit->mod.first == check) {
117 if (cit->table.get()) {
118 // this is a prefix key - set new map
119 seq->curmap = cit->table.get();
122 // final key - reset map
123 seq->curmap = seq->stdmap;
130 // error - key not found:
131 seq->curmap = seq->stdmap;
133 return LFUN_UNKNOWN_ACTION;
137 string const kb_keymap::print() const
140 for (Table::const_iterator cit = table.begin();
141 cit != table.end(); ++cit) {
142 buf += printKey((*cit));
149 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
151 unsigned int const code = seq->sequence[r];
152 if (code == NoSymbol) return;
154 key_modifier::state const mod1 = seq->modifiers[r].first;
155 key_modifier::state const mod2 = seq->modifiers[r].second;
157 // check if key is already there
158 for (Table::iterator it = table.begin(); it != table.end(); ++it) {
159 if (code == it->code && mod1 == it->mod.first && mod2 == it->mod.second) {
161 if (r + 1 == seq->length()) {
163 << "Warning: New binding for '"
165 << "' is overriding old binding..."
167 if (it->table.get()) {
172 } else if (!it->table.get()) {
173 lyxerr << "Error: New binding for '" << seq->print()
174 << "' is overriding old binding..."
178 it->table->defkey(seq, action, r + 1);
184 Table::iterator newone = table.insert(table.end(), kb_key());
186 newone->mod = seq->modifiers[r];
187 if (r + 1 == seq->length()) {
188 newone->action = action;
189 newone->table.reset();
192 newone->table.reset(new kb_keymap);
193 newone->table->defkey(seq, action, r + 1);
199 string const kb_keymap::findbinding(int act, string const & prefix) const
202 if (table.empty()) return res;
204 Table::const_iterator end = table.end();
205 for (Table::const_iterator cit = table.begin();
207 if (cit->table.get()) {
208 res += cit->table->findbinding(act,
212 } else if (cit->action == act) {
214 res += prefix + printKey((*cit));