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 "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, unsigned int mod)
35 char const * const s = XKeysymToString(key);
37 if (mod & ShiftMask) buf += "S-";
38 if (mod & ControlMask) buf += "C-";
39 if (mod & Mod1Mask) 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 & 0xffff);
74 string::size_type kb_keymap::bind(string const & seq, kb_action 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 kb_action kb_keymap::lookup(unsigned int key,
98 unsigned int mod, kb_sequence * seq) const
101 seq->curmap = seq->stdmap;
103 return LFUN_UNKNOWN_ACTION;
106 //suppress modifier bits we do not handle
109 for (Table::const_iterator cit = table.begin();
110 cit != table.end(); ++cit) {
111 unsigned int const msk1 = cit->mod & 0xffff;
112 unsigned int const msk0 = (cit->mod >> 16) & 0xffff;
113 if (cit->code == key && (mod & ~msk0) == msk1) {
115 if (cit->table.get()) {
116 // this is a prefix key - set new map
117 seq->curmap = cit->table.get();
120 // final key - reset map
121 seq->curmap = seq->stdmap;
128 // error - key not found:
129 seq->curmap = seq->stdmap;
131 return LFUN_UNKNOWN_ACTION;
135 string const kb_keymap::print() const
138 for (Table::const_iterator cit = table.begin();
139 cit != table.end(); ++cit) {
140 buf += printKey((*cit));
147 void kb_keymap::defkey(kb_sequence * seq, kb_action action, unsigned int r)
149 unsigned int const code = seq->sequence[r];
150 if (code == NoSymbol) return;
152 unsigned int const modmsk = seq->modifiers[r];
154 // check if key is already there
155 for (Table::iterator it = table.begin(); it != table.end(); ++it) {
156 if (code == it->code && modmsk == it->mod) {
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 = modmsk;
184 if (r + 1 == seq->length()) {
185 newone->action = action;
186 newone->table.reset(0);
189 newone->table.reset(new kb_keymap);
190 newone->table->defkey(seq, action, r + 1);
196 string const kb_keymap::findbinding(kb_action 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));