]> git.lyx.org Git - lyx.git/blob - src/kbsequence.C
add bindings for latin charsets, use self-insert as intended from the beginning
[lyx.git] / src / kbsequence.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <cstring>
13 #include <X11/Xlib.h>
14
15 #include "gettext.h"
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "kbsequence.h"
22 #include "kbmap.h"
23 #include "debug.h"
24
25 using std::endl;
26
27 // The only modifiers that we handle. We want to throw away things
28 // like NumLock. 
29 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
30
31
32 // === static functions =================================================== 
33
34
35 /* ---F+------------------------------------------------------------------ *\
36    Function  : printKeysym
37    Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
38    Purpose   : prints a keysym, including modifiers.
39    Parameters: key    - keysym
40                mod    - modifiers
41                buf    - string where the result goes
42                maxlen - length of string (including '\0')
43    Returns   : length of printed string if ok, 0 otherwise.
44 \* ---F------------------------------------------------------------------- */
45 extern
46 void printKeysym(unsigned int key, unsigned int mod, string & buf);
47
48
49 // === kb_sequence methods ================================================ 
50
51 /* ---F+------------------------------------------------------------------ *\
52     Function  : kb_sequence::addkey
53     Called by : [user]
54     Purpose   : add a key to the sequence, look up in map and return action
55     Parameters: key  - keysym of key
56                 mod  - modifier mask
57                 nmod - modifier veto mask (unused now)
58     Returns   : action or -1 if error (no map defined or key not found)
59 \* ---F------------------------------------------------------------------- */
60
61 int kb_sequence::addkey(unsigned int key,
62                         unsigned int mod, unsigned int nmod /*= 0*/)
63 {
64         //lyxerr << "kb_sequence::addkey: length is [" << length << "]\n"
65         //       << "kb_sequence::addkey::key == [" << key << "]\n"
66         //       << "kb_sequence::addkey::mod == [" << mod << "]" << endl;
67         
68         if (length <= 0) {
69                 length = 0;
70                 sequence.clear();
71                 modifiers.clear();
72         }
73
74         modifiers.push_back(mod + (nmod << 16));
75         sequence.push_back(key);
76         ++length;
77
78         if (curmap)
79                 return curmap->lookup(key, mod, this);
80         
81         return -1;
82 }
83
84
85 /* ---F+------------------------------------------------------------------ *\
86     Function  : kb_sequence::parse
87     Called by : [user]
88     Purpose   : parse a string that holds a key sequence and add the keys
89     Parameters: s - string holding the key sequence
90     Returns   : 0 - if ok, error pos if error
91     Note      : Keys must be separated with whitespace;
92                 Use the keysym names used by XStringToKeysym
93                 Prefixes are S-, C-, M- for shift, control, meta
94 \* ---F------------------------------------------------------------------- */
95
96 int kb_sequence::parse(string const & s)
97 {
98         if (s.empty()) return 1;
99
100         string::size_type i = 0;
101         unsigned int mod = 0, nmod = 0;
102         while (i < s.length()) {
103                 if (s[i] && (s[i]) <= ' ') ++i;
104                 if (i >= s.length()) break;
105                 
106                 if (s[i + 1] == '-')    { // is implicit that s[i] == true
107                         switch (s[i]) {
108                         case 's': case 'S':
109                                 mod |= ShiftMask;
110                                 i += 2;
111                                 continue;
112                         case 'c': case 'C':
113                                 mod |= ControlMask;
114                                 i += 2;
115                                 continue;
116                         case 'm': case 'M':
117                                 mod |= Mod1Mask;
118                                 i += 2;
119                                 continue;
120                         default:
121                                 return i + 1;
122                         }
123                 } else if (s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
124                         switch (s[i + 1]) {
125                         case 's': case 'S':
126                                 nmod |= ShiftMask;
127                                 i += 3;
128                                 continue;
129                         case 'c': case 'C':
130                                 nmod |= ControlMask;
131                                 i += 3;
132                                 continue;
133                         case 'm': case 'M':
134                                 nmod |= Mod1Mask;
135                                 i += 3;
136                                 continue;
137                         default:
138                                 return i + 2;
139                         }
140                 } else {
141                         string tbuf;
142                         string::size_type j = i;
143                         for (; j < s.length() && s[j] > ' '; ++j)
144                                 tbuf += s[j];    // (!!!check bounds :-)
145                         
146                         KeySym key = XStringToKeysym(tbuf.c_str());
147                         if (key == NoSymbol) {
148                                 lyxerr[Debug::KBMAP]
149                                         << "kbmap.C: No such keysym: "
150                                         << tbuf << endl;
151                                 return j;
152                         }
153                         i = j;
154                         
155                         addkey(key, mod, nmod);
156                         mod = 0;
157                         nmod = 0;
158                 }
159         }
160         return 0;
161 }
162
163
164 /* ---F+------------------------------------------------------------------ *\
165     Function  : kb_sequence::print
166     Called by : [user]
167     Purpose   : print the currently defined sequence into a string
168     Parameters: buf           - string where the result goes
169                 when_defined  - only  print when sequence is real: length > 0.
170     Returns   : 0, if ok, -1 if string too long
171 \* ---F------------------------------------------------------------------- */
172
173 int kb_sequence::print(string & buf, bool when_defined) const
174 {
175         //lyxerr << "kb_sequence::print: length is [" << length << "]" << endl;
176         
177         KeySym key;
178         unsigned int mod;
179         int l = length;
180         if (l < 0 && !when_defined ) l = -l;
181         
182         for (int i = 0; i < l; ++i) {
183                 key = sequence[i];
184                 mod = modifiers[i] & 0xffff;
185                 //lyxerr << "kb_sequence::sequence[" << i << "] == [" << key << "]\n"
186                 //       << "kb_sequence::modifiers[" << i << "] == [" << mod << "]"
187                 //       << endl;
188
189                 printKeysym(key, mod, buf);  // RVDK_PATCH_5
190
191                 if (i + 1 < l) {  // append a blank
192                         buf += ' ';
193                 }
194         }
195         return 0;
196 }
197
198
199 /* ---F+------------------------------------------------------------------ *\
200     Function  : kb_sequence::printOptions
201     Called by : [user]
202     Purpose   : print the available key options from the current state in the
203                 sequence. RVDK_PATCH_5
204     Parameters: buf    - string where the result goes
205                 maxlen - length of string (including '\0')
206     Returns   : 0, if ok, -1 if string too long
207 \* ---F------------------------------------------------------------------- */
208
209 int kb_sequence::printOptions(string & buf) const
210 {
211         print(buf, true);
212         
213         if (!curmap) return -1;
214         buf += _("   options: ");
215         curmap->print(buf);
216         return 0;
217 }
218
219
220 /* ---F+------------------------------------------------------------------ *\
221     Function  : kb_sequence::delseq
222     Called by : [user]
223     Purpose   : mark the sequence as deleted
224     Parameters: none
225     Returns   : nothing
226 \* ---F------------------------------------------------------------------- */
227
228 void kb_sequence::delseq()
229 {
230         // negative length marks sequence as deleted, but we can still
231         // print() it or retrieve the last char using getiso()
232         length = -length;
233 }
234
235
236 /* ---F+------------------------------------------------------------------ *\
237    Function  : kb_sequence::getsym
238    Called by : [user], getiso
239    Purpose   : get the keysym of the last key in sequence
240    Parameters: none
241    Returns   : keysym
242 \* ---F------------------------------------------------------------------- */
243
244 unsigned int kb_sequence::getsym() const
245 {
246         int l = length;
247         if (l == 0) return NoSymbol;
248         if (l < 0) l = -l;
249         return sequence[l - 1];
250 }
251
252
253 /* ---F+------------------------------------------------------------------ *\
254     Function  : kb_sequence::getiso
255     Called by : [user]
256     Purpose   : return iso character code of last key, if any
257     Parameters: none
258     Returns   : iso code or 0 if none
259 \* ---F------------------------------------------------------------------- */
260
261 char kb_sequence::getiso() const
262 {
263         unsigned int const c = getsym();
264
265         lyxerr << "Raw keysym: " << hex << c << dec << endl;
266         lyxerr << "byte 3: " << hex << (c & 0x0000FF00) << dec << endl;
267         
268         switch (c & 0x0000FF00) {
269                 // latin 1 byte 3 = 0
270         case 0x00000000:
271                 return c;
272                 // latin 2 byte 3 = 1
273         case 0x00000100:
274                 // latin 3 byte 3 = 2
275         case 0x00000200:
276                 // latin 4 byte 3 = 3
277         case 0x00000300:
278                 // latin 8 byte 3 = 18 (0x12)
279         case 0x00001200:
280                 // latin 9 byte 3 = 19 (0x13)
281         case 0x00001300:
282                 return c & 0x000000FF;
283         default:
284                 return '\0';
285         }
286
287         // not a latin char we know of
288         return '\0';
289 }
290
291
292 /* ---F+------------------------------------------------------------------ *\
293     Function  : kb_sequence::reset
294     Called by : [user]
295     Purpose   : reset sequence to initial state. RVDK_PATCH_5
296     Parameters: none
297     Returns   : void
298 \* ---F------------------------------------------------------------------- */
299
300 void kb_sequence::reset()
301 {
302         delseq();
303         curmap = stdmap;
304         if (length > 0) length = -length;
305 }
306
307 /* === End of File: kbmap.C ============================================== */