]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
John's Layout Tabular UI improvements and Martins fixes to clearing the
[lyx.git] / src / kbmap.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *      
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include <X11/Xlib.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "kbmap.h"
20 #include "kbsequence.h"
21 #include "debug.h"
22
23 using std::endl;
24
25 // The only modifiers that we handle. We want to throw away things
26 // like NumLock.
27 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
28
29
30 string const kb_keymap::printKeysym(unsigned int key, unsigned int mod)
31 {
32         string buf;
33         mod &= ModsMask;
34
35         char const * const s = XKeysymToString(key);
36         
37         if (mod & ShiftMask) buf += "S-";
38         if (mod & ControlMask) buf += "C-";
39         if (mod & Mod1Mask) buf += "M-";
40         if (s) buf += s;
41         return buf;
42 }
43
44
45 char kb_keymap::getiso(unsigned int c)
46 {
47         switch (c & 0x0000FF00) {
48                 // latin 1 byte 3 = 0
49         case 0x00000000: break;
50                 // latin 2 byte 3 = 1
51         case 0x00000100:
52                 // latin 3 byte 3 = 2
53         case 0x00000200:
54                 // latin 4 byte 3 = 3
55         case 0x00000300:
56                 // latin 8 byte 3 = 18 (0x12)
57         case 0x00001200:
58                 // latin 9 byte 3 = 19 (0x13)
59         case 0x00001300:
60                 c &= 0x000000FF;
61                 break;
62         default:
63                 c = 0;
64         }
65         return c;
66 }
67
68 string const kb_keymap::printKey(kb_key const & key) const
69 {
70         return printKeysym(key.code, key.mod & 0xffff);
71 }
72
73
74 string::size_type kb_keymap::bind(string const & seq, kb_action action)
75 {
76         if (lyxerr.debugging(Debug::KBMAP)) {
77                 lyxerr << "BIND: Sequence `"
78                        << seq << "' Action `"
79                        << action << "'" << endl;
80         }
81         
82         kb_sequence k(0, 0);
83
84         string::size_type const res = k.parse(seq);
85         if (res == string::npos) {
86                 defkey(&k, action);
87         } else {
88                 lyxerr[Debug::KBMAP] << "Parse error at position " << res
89                                      << " in key sequence '" << seq << "'."
90                                      << endl;
91         }
92
93         return res;
94 }
95
96
97 kb_action kb_keymap::lookup(unsigned int key,
98                       unsigned int mod, kb_sequence * seq) const
99 {
100         if (table.empty()) {
101                 seq->curmap = seq->stdmap;
102                 seq->mark_deleted();
103                 return LFUN_UNKNOWN_ACTION;
104         }
105
106         //suppress modifier bits we do not handle
107         mod &= ModsMask;
108
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) {
114                         // match found
115                         if (cit->table.get()) {
116                                 // this is a prefix key - set new map
117                                 seq->curmap = cit->table.get();
118                                 return LFUN_PREFIX;
119                         } else {
120                                 // final key - reset map
121                                 seq->curmap = seq->stdmap;
122                                 seq->mark_deleted();
123                                 return cit->action;
124                         }
125                 }
126         }
127
128         // error - key not found:
129         seq->curmap = seq->stdmap;
130         seq->mark_deleted();
131         return LFUN_UNKNOWN_ACTION;
132 }
133
134
135 string const kb_keymap::print() const
136 {
137         string buf;
138         for (Table::const_iterator cit = table.begin();
139              cit != table.end(); ++cit) {
140                 buf += printKey((*cit));
141                 buf += ' ';
142         }
143         return buf;
144 }
145
146
147 void kb_keymap::defkey(kb_sequence * seq, kb_action action, unsigned int r)
148 {
149         unsigned int const code = seq->sequence[r];
150         if (code == NoSymbol) return;
151
152         unsigned int const modmsk = seq->modifiers[r];
153
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) {
157                         // overwrite binding
158                         if (r + 1 == seq->length()) {
159                                 lyxerr[Debug::KBMAP]
160                                         << "Warning: New binding for '"
161                                         << seq->print()
162                                         << "' is overriding old binding..."
163                                         << endl;
164                                         if (it->table.get()) {
165                                         it->table.reset(0);
166                                 }
167                                 it->action = action;
168                                 return;
169                         } else if (!it->table.get()) {
170                                 lyxerr << "Error: New binding for '" << seq->print()
171                                        << "' is overriding old binding..."
172                                                << endl;
173                                 return;
174                         } else {
175                                 it->table->defkey(seq, action, r + 1);
176                                 return;
177                         }
178                 }
179         }
180         
181         Table::iterator newone = table.insert(table.end(), kb_key());
182         newone->code = code;
183         newone->mod = modmsk;
184         if (r + 1 == seq->length()) {
185                 newone->action = action;
186                 newone->table.reset(0);
187                 return;
188         } else {
189                 newone->table.reset(new kb_keymap);
190                 newone->table->defkey(seq, action, r + 1);
191                 return;
192         }
193 }
194
195
196 string const kb_keymap::findbinding(kb_action act, string const & prefix) const
197 {
198         string res;
199         if (table.empty()) return res;
200
201         Table::const_iterator end = table.end();
202         for (Table::const_iterator cit = table.begin();
203             cit != end; ++cit) {
204                 if (cit->table.get()) {
205                         res += cit->table->findbinding(act,
206                                                        prefix
207                                                        + printKey((*cit))
208                                                        + " ");
209                 } else if (cit->action == act) {
210                         res += "[";
211                         res += prefix + printKey((*cit));
212                         res += "] ";
213                 }
214         }
215         return res;
216 }