1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
16 #pragma implementation
20 #include "commandtags.h"
21 #include "kbsequence.h"
26 // The only modifiers that we handle. We want to throw away things
28 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
31 string const kb_keymap::printKeysym(unsigned int key, key_modifier::state mod)
35 char const * const s = XKeysymToString(key);
37 if (mod & key_modifier::shift) buf += "S-";
38 if (mod & key_modifier::ctrl) buf += "C-";
39 if (mod & key_modifier::alt) buf += "M-";
45 char kb_keymap::getiso(unsigned int c)
47 switch (c & 0x0000FF00) {
49 case 0x00000000: break;
56 // latin 8 byte 3 = 18 (0x12)
58 // latin 9 byte 3 = 19 (0x13)
68 string const kb_keymap::printKey(kb_key const & key) const
70 return printKeysym(key.code, key.mod.first);
74 string::size_type kb_keymap::bind(string const & seq, int action)
76 if (lyxerr.debugging(Debug::KBMAP)) {
77 lyxerr << "BIND: Sequence `"
78 << seq << "' Action `"
79 << action << "'" << endl;
84 string::size_type const res = k.parse(seq);
85 if (res == string::npos) {
88 lyxerr[Debug::KBMAP] << "Parse error at position " << res
89 << " in key sequence '" << seq << "'."
97 int kb_keymap::lookup(unsigned int key,
98 key_modifier::state mod, kb_sequence * seq) const
101 seq->curmap = seq->stdmap;
103 return LFUN_UNKNOWN_ACTION;
106 for (Table::const_iterator cit = table.begin();
107 cit != table.end(); ++cit) {
108 key_modifier::state mask(cit->mod.second);
109 key_modifier::state check =
110 static_cast<key_modifier::state>(mod & ~mask);
112 if (cit->code == key && cit->mod.first == check) {
114 if (cit->table.get()) {
115 // this is a prefix key - set new map
116 seq->curmap = cit->table.get();
119 // final key - reset map
120 seq->curmap = seq->stdmap;
127 // error - key not found:
128 seq->curmap = seq->stdmap;
130 return LFUN_UNKNOWN_ACTION;
134 string const kb_keymap::print() const
137 for (Table::const_iterator cit = table.begin();
138 cit != table.end(); ++cit) {
139 buf += printKey((*cit));
146 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
148 unsigned int const code = seq->sequence[r];
149 if (code == NoSymbol) return;
151 key_modifier::state const mod1 = seq->modifiers[r].first;
152 key_modifier::state const mod2 = seq->modifiers[r].second;
154 // check if key is already there
155 for (Table::iterator it = table.begin(); it != table.end(); ++it) {
156 if (code == it->code && mod1 == it->mod.first && mod2 == it->mod.second) {
158 if (r + 1 == seq->length()) {
160 << "Warning: New binding for '"
162 << "' is overriding old binding..."
164 if (it->table.get()) {
169 } else if (!it->table.get()) {
170 lyxerr << "Error: New binding for '" << seq->print()
171 << "' is overriding old binding..."
175 it->table->defkey(seq, action, r + 1);
181 Table::iterator newone = table.insert(table.end(), kb_key());
183 newone->mod = seq->modifiers[r];
184 if (r + 1 == seq->length()) {
185 newone->action = action;
186 newone->table.reset();
189 newone->table.reset(new kb_keymap);
190 newone->table->defkey(seq, action, r + 1);
196 string const kb_keymap::findbinding(int act, string const & prefix) const
199 if (table.empty()) return res;
201 Table::const_iterator end = table.end();
202 for (Table::const_iterator cit = table.begin();
204 if (cit->table.get()) {
205 res += cit->table->findbinding(act,
209 } else if (cit->action == act) {
211 res += prefix + printKey((*cit));