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, unsigned int mod)
36 char const * const s = XKeysymToString(key);
38 if (mod & ShiftMask) buf += "S-";
39 if (mod & ControlMask) buf += "C-";
40 if (mod & Mod1Mask) buf += "M-";
46 char kb_keymap::getiso(unsigned int c)
48 switch (c & 0x0000FF00) {
50 case 0x00000000: break;
57 // latin 8 byte 3 = 18 (0x12)
59 // latin 9 byte 3 = 19 (0x13)
69 string const kb_keymap::printKey(kb_key const & key) const
71 return printKeysym(key.code, key.mod & 0xffff);
75 string::size_type kb_keymap::bind(string const & seq, int action)
77 if (lyxerr.debugging(Debug::KBMAP)) {
78 lyxerr << "BIND: Sequence `"
79 << seq << "' Action `"
80 << action << "'" << endl;
85 string::size_type const res = k.parse(seq);
86 if (res == string::npos) {
89 lyxerr[Debug::KBMAP] << "Parse error at position " << res
90 << " in key sequence '" << seq << "'."
98 int kb_keymap::lookup(unsigned int key,
99 unsigned int mod, kb_sequence * seq) const
102 seq->curmap = seq->stdmap;
104 return LFUN_UNKNOWN_ACTION;
107 //suppress modifier bits we do not handle
110 for (Table::const_iterator cit = table.begin();
111 cit != table.end(); ++cit) {
112 unsigned int const msk1 = cit->mod & 0xffff;
113 unsigned int const msk0 = (cit->mod >> 16) & 0xffff;
114 if (cit->code == key && (mod & ~msk0) == msk1) {
116 if (cit->table.get()) {
117 // this is a prefix key - set new map
118 seq->curmap = cit->table.get();
121 // final key - reset map
122 seq->curmap = seq->stdmap;
129 // error - key not found:
130 seq->curmap = seq->stdmap;
132 return LFUN_UNKNOWN_ACTION;
136 string const kb_keymap::print() const
139 for (Table::const_iterator cit = table.begin();
140 cit != table.end(); ++cit) {
141 buf += printKey((*cit));
148 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
150 unsigned int const code = seq->sequence[r];
151 if (code == NoSymbol) return;
153 unsigned int const modmsk = seq->modifiers[r];
155 // check if key is already there
156 for (Table::iterator it = table.begin(); it != table.end(); ++it) {
157 if (code == it->code && modmsk == it->mod) {
159 if (r + 1 == seq->length()) {
161 << "Warning: New binding for '"
163 << "' is overriding old binding..."
165 if (it->table.get()) {
170 } else if (!it->table.get()) {
171 lyxerr << "Error: New binding for '" << seq->print()
172 << "' is overriding old binding..."
176 it->table->defkey(seq, action, r + 1);
182 Table::iterator newone = table.insert(table.end(), kb_key());
184 newone->mod = modmsk;
185 if (r + 1 == seq->length()) {
186 newone->action = action;
187 newone->table.reset(0);
190 newone->table.reset(new kb_keymap);
191 newone->table->defkey(seq, action, r + 1);
197 string const kb_keymap::findbinding(int act, string const & prefix) const
200 if (table.empty()) return res;
202 Table::const_iterator end = table.end();
203 for (Table::const_iterator cit = table.begin();
205 if (cit->table.get()) {
206 res += cit->table->findbinding(act,
210 } else if (cit->action == act) {
212 res += prefix + printKey((*cit));