]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
missing entry
[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 "commandtags.h"
21 #include "kbsequence.h"
22 #include "debug.h"
23
24 using std::endl;
25
26 // The only modifiers that we handle. We want to throw away things
27 // like NumLock.
28 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
29
30
31 string const kb_keymap::printKeysym(unsigned int key, key_modifier::state mod)
32 {
33         string buf;
34
35         char const * const s = XKeysymToString(key);
36
37         if (mod & key_modifier::shift) buf += "S-";
38         if (mod & key_modifier::ctrl) buf += "C-";
39         if (mod & key_modifier::alt) 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.first);
71 }
72
73
74 string::size_type kb_keymap::bind(string const & seq, int 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 int kb_keymap::lookup(unsigned int key,
98                       key_modifier::state 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         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);
111  
112                 if (cit->code == key && cit->mod.first == check) {
113                         // match found
114                         if (cit->table.get()) {
115                                 // this is a prefix key - set new map
116                                 seq->curmap = cit->table.get();
117                                 return LFUN_PREFIX;
118                         } else {
119                                 // final key - reset map
120                                 seq->curmap = seq->stdmap;
121                                 seq->mark_deleted();
122                                 return cit->action;
123                         }
124                 }
125         }
126
127         // error - key not found:
128         seq->curmap = seq->stdmap;
129         seq->mark_deleted();
130         return LFUN_UNKNOWN_ACTION;
131 }
132
133
134 string const kb_keymap::print() const
135 {
136         string buf;
137         for (Table::const_iterator cit = table.begin();
138              cit != table.end(); ++cit) {
139                 buf += printKey((*cit));
140                 buf += ' ';
141         }
142         return buf;
143 }
144
145
146 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
147 {
148         unsigned int const code = seq->sequence[r];
149         if (code == NoSymbol) return;
150
151         key_modifier::state const mod1 = seq->modifiers[r].first;
152         key_modifier::state const mod2 = seq->modifiers[r].second;
153
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) {
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();
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 = seq->modifiers[r];
184         if (r + 1 == seq->length()) {
185                 newone->action = action;
186                 newone->table.reset();
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(int 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 }