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;
55 // latin 8 byte 3 = 18 (0x12)
57 // latin 9 byte 3 = 19 (0x13)
67 string const kb_keymap::printKey(kb_key const & key) const
69 return printKeysym(key.code, key.mod.first);
73 string::size_type kb_keymap::bind(string const & seq, int action)
75 if (lyxerr.debugging(Debug::KBMAP)) {
76 lyxerr << "BIND: Sequence `"
77 << seq << "' Action `"
78 << action << "'" << endl;
83 string::size_type const res = k.parse(seq);
84 if (res == string::npos) {
87 lyxerr[Debug::KBMAP] << "Parse error at position " << res
88 << " in key sequence '" << seq << "'."
96 int kb_keymap::lookup(unsigned int key,
97 key_modifier::state mod, kb_sequence * seq) const
100 seq->curmap = seq->stdmap;
102 return LFUN_UNKNOWN_ACTION;
105 for (Table::const_iterator cit = table.begin();
106 cit != table.end(); ++cit) {
107 key_modifier::state mask(cit->mod.second);
108 key_modifier::state check =
109 static_cast<key_modifier::state>(mod & ~mask);
111 if (cit->code == key && cit->mod.first == check) {
113 if (cit->table.get()) {
114 // this is a prefix key - set new map
115 seq->curmap = cit->table.get();
118 // final key - reset map
119 seq->curmap = seq->stdmap;
126 // error - key not found:
127 seq->curmap = seq->stdmap;
129 return LFUN_UNKNOWN_ACTION;
133 string const kb_keymap::print() const
136 for (Table::const_iterator cit = table.begin();
137 cit != table.end(); ++cit) {
138 buf += printKey((*cit));
145 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
147 unsigned int const code = seq->sequence[r];
148 if (code == NoSymbol) return;
150 key_modifier::state const mod1 = seq->modifiers[r].first;
151 key_modifier::state const mod2 = seq->modifiers[r].second;
153 // check if key is already there
154 for (Table::iterator it = table.begin(); it != table.end(); ++it) {
155 if (code == it->code && mod1 == it->mod.first && mod2 == it->mod.second) {
157 if (r + 1 == seq->length()) {
159 << "Warning: New binding for '"
161 << "' is overriding old binding..."
163 if (it->table.get()) {
168 } else if (!it->table.get()) {
169 lyxerr << "Error: New binding for '" << seq->print()
170 << "' is overriding old binding..."
174 it->table->defkey(seq, action, r + 1);
180 Table::iterator newone = table.insert(table.end(), kb_key());
182 newone->mod = seq->modifiers[r];
183 if (r + 1 == seq->length()) {
184 newone->action = action;
185 newone->table.reset();
188 newone->table.reset(new kb_keymap);
189 newone->table->defkey(seq, action, r + 1);
195 string const kb_keymap::findbinding(int act, string const & prefix) const
198 if (table.empty()) return res;
200 Table::const_iterator end = table.end();
201 for (Table::const_iterator cit = table.begin();
203 if (cit->table.get()) {
204 res += cit->table->findbinding(act,
208 } else if (cit->action == act) {
210 res += prefix + printKey((*cit));