1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * ====================================================== */
13 #include "support/lstrings.h"
17 #pragma implementation
23 // The only modifiers that we handle. We want to throw away things
25 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask};
28 // === static functions ===================================================
31 /* ---F+------------------------------------------------------------------ *\
32 Function : printKeysym
33 Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
34 Purpose : prints a keysym, including modifiers.
35 Parameters: key - keysym
37 buf - string where the result goes
38 maxlen - length of string (including '\0')
39 Returns : length of printed string if ok, 0 otherwise.
40 \* ---F------------------------------------------------------------------- */
42 void printKeysym(KeySym key, unsigned int mod, string & buf)
46 char * s = XKeysymToString(key);
48 if (mod & ShiftMask) buf += "S-";
49 if (mod & ControlMask) buf += "C-";
50 if (mod & Mod1Mask) buf += "M-";
55 /* ---F+------------------------------------------------------------------ *\
56 Function : printKeyTab
57 Called by : kb_keymap::print
58 Purpose : print the keysyms found in the given key table. RVDK_PATCH_5
59 Parameters: tabPt - keytable pointer
60 buf - string where the result goes
61 maxLen - length of string (including '\0')
62 Returns : length of printed string.
63 \* ---F------------------------------------------------------------------- */
66 void printKeyTab(kb_key * tabPt, string & buf)
68 unsigned int ksym, mod;
70 /* -------> Print each of the slots into buf. */
71 for( ; (tabPt->code & 0xffff) != NoSymbol; ++tabPt) {
73 mod = tabPt->mod & 0xffff;
75 printKeysym(ksym, mod, buf);
81 // === kb_sequence methods ================================================
83 /* ---F+------------------------------------------------------------------ *\
84 Function : kb_sequence::addkey
86 Purpose : add a key to the sequence, look up in map and return action
87 Parameters: key - keysym of key
89 nmod - modifier veto mask (unused now)
90 Returns : action or -1 if error (no map defined or key not found)
91 \* ---F------------------------------------------------------------------- */
93 int kb_sequence::addkey(KeySym key,
94 unsigned int mod, unsigned int nmod /*= 0*/)
96 if(length < 0) length = 0;
98 if(length + 1 >= size) {
99 unsigned int * nseq = new unsigned int[size + KB_PREALLOC];
101 memcpy(nseq, sequence, length * sizeof(unsigned int));
102 if(sequence != staticseq) delete sequence;
104 nseq = new unsigned int[size];
105 memcpy(nseq, modifiers, length * sizeof(unsigned int));
106 if(modifiers != staticmod) delete modifiers;
110 modifiers[length] = mod + (nmod << 16);
111 sequence[length++] = key;
114 return curmap->lookup(key, mod, this);
120 /* ---F+------------------------------------------------------------------ *\
121 Function : kb_sequence::parse
123 Purpose : parse a string that holds a key sequence and add the keys
124 Parameters: s - string holding the key sequence
125 Returns : 0 - if ok, error pos if error
126 Note : Keys must be separated with whitespace;
127 Use the keysym names used by XStringToKeysym
128 Prefixes are S-, C-, M- for shift, control, meta
129 \* ---F------------------------------------------------------------------- */
131 int kb_sequence::parse(char const * s)
136 unsigned int mod = 0, nmod = 0;
138 if(s[i] && (s[i]) <= ' ') ++i;
141 if(s[i + 1] == '-') { // is implicit that s[i] == true
158 } else if(s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
178 for(; s[j] && s[j] > ' '; ++j)
179 tbuf += s[j]; // (!!!check bounds :-)
181 KeySym key = XStringToKeysym(tbuf.c_str());
182 if(key == NoSymbol) {
184 << "kbmap.C: No such keysym: "
190 addkey(key, mod, nmod);
199 /* ---F+------------------------------------------------------------------ *\
200 Function : kb_sequence::print
202 Purpose : print the currently defined sequence into a string
203 Parameters: buf - string where the result goes
204 maxlen - length of string (including '\0')
205 when_defined - only print when sequence is real: length > 0.
206 Returns : 0, if ok, -1 if string too long
207 \* ---F------------------------------------------------------------------- */
209 int kb_sequence::print(string & buf, bool when_defined) const
214 if ( l < 0 && !when_defined ) l = -l;
216 for(int i = 0; i < l; ++i) {
218 mod = modifiers[i] & 0xffff;
220 printKeysym(key, mod, buf); // RVDK_PATCH_5
222 if(i + 1 < l) { // append a blank
230 /* ---F+------------------------------------------------------------------ *\
231 Function : kb_sequence::printOptions
233 Purpose : print the available key options from the current state in the
234 sequence. RVDK_PATCH_5
235 Parameters: buf - string where the result goes
236 maxlen - length of string (including '\0')
237 Returns : 0, if ok, -1 if string too long
238 \* ---F------------------------------------------------------------------- */
240 int kb_sequence::printOptions(string & buf) const
244 if (!curmap) return -1;
245 buf += _(" options: ");
251 /* ---F+------------------------------------------------------------------ *\
252 Function : kb_sequence::delseq
254 Purpose : mark the sequence as deleted
257 \* ---F------------------------------------------------------------------- */
259 void kb_sequence::delseq()
261 // negative length marks sequence as deleted, but we can still
262 // print() it or retrieve the last char using getiso()
267 /* ---F+------------------------------------------------------------------ *\
268 Function : kb_sequence::getsym
269 Called by : [user], getiso
270 Purpose : get the keysym of the last key in sequence
273 \* ---F------------------------------------------------------------------- */
275 KeySym kb_sequence::getsym()
278 if(l == 0) return NoSymbol;
280 return sequence[l - 1];
284 /* ---F+------------------------------------------------------------------ *\
285 Function : kb_sequence::getiso
287 Purpose : return iso character code of last key, if any
289 Returns : iso code or 0 if none
290 \* ---F------------------------------------------------------------------- */
292 char kb_sequence::getiso()
302 /* ---F+------------------------------------------------------------------ *\
303 Function : kb_sequence::reset
305 Purpose : reset sequence to initial state. RVDK_PATCH_5
308 \* ---F------------------------------------------------------------------- */
310 void kb_sequence::reset()
314 if (length > 0) length = -length;
318 // === kb_keymap methods ==================================================
320 // This binds a key to an action
321 int kb_keymap::bind(char const * seq, int action)
325 int res = k.parse(seq);
329 lyxerr[Debug::KBMAP] << "Parse error at position " << res
330 << " in key sequence '" << seq << "'."
336 /* ---F+------------------------------------------------------------------ *\
337 Function : kb_keymap::lookup
338 Called by : [user], kb_sequence::add()
339 Purpose : look up a key press in a given keymap
340 Parameters: key - the keysym of the key press
341 mod - the modifier mask of the keypress
342 seq - the key-sequence retrieved so far
343 Returns : user defined action; 0 for prefix key, -1 if key not found
344 \* ---F------------------------------------------------------------------- */
346 int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence * seq)
348 unsigned int ksym, msk1, msk0;
351 //suppress modifier bits we do not handle
355 // error - no keymap defined:
356 seq->curmap = seq->stdmap;
363 // --- now search the list of keys ---
365 for(; (tab->code & 0xffff) != NoSymbol; ++tab) {
367 msk1 = tab->mod & 0xffff;
368 msk0 = (tab->mod >> 16) & 0xffff;
370 if(ksym == key && (mod & ~msk0) == msk1) {
373 // this is a prefix key - set new map
374 seq->curmap = tab->table;
377 // final key - reset map
378 seq->curmap = seq->stdmap;
380 return tab->action; // ... and return action
385 // error - key not found:
386 seq->curmap = seq->stdmap;
392 /* ---F+------------------------------------------------------------------ *\
393 Function : kb_keymap::print
395 Purpose : Prints all the available keysyms. RVDK_PATCH_5
396 Parameters: buf - string where output goes.
397 maxLen - available length in string, including `\0'.
398 Returns : updated maxLen.
399 \* ---F------------------------------------------------------------------- */
401 void kb_keymap::print(string & buf) const
403 // Return when keymap has no table.
406 // Process each of its slots recursively and return.
407 printKeyTab(table, buf);
411 /* ---F+------------------------------------------------------------------ *\
412 Function : kb_keymap::defkey
414 Purpose : define an action for a key sequence
415 Parameters: seq - the key sequence
416 action - the action to be defined
417 idx - recursion depth
419 \* ---F------------------------------------------------------------------- */
421 int kb_keymap::defkey(kb_sequence * seq, int action, int idx /*= 0*/)
423 unsigned int code = seq->sequence[idx];
424 if(code == NoSymbol) return -1;
426 unsigned int modmsk = seq->modifiers[idx];
427 kb_key * tab, ** ptab;
428 // --- get list------------------------------------------------------
430 // If we don't have any yet, make an empty one
431 table = new kb_key[KB_PREALLOC];
432 table[0].code = NoSymbol;
441 // --- check if key is already there --------------------------------
445 for(t = tab, tsize = 1; t->code != NoSymbol; ++t, ++tsize) {
446 if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
447 if(idx + 1 == seq->length) {
449 seq->print(buf, true);
451 << "Warning: New binding for '"
453 << "' is overriding old binding..."
462 } else if (!t->table) {
464 seq->print(buf, true);
465 lyxerr << "Error: New binding for '" << buf
466 << "' is overriding old binding..."
470 return t->table->defkey(seq, action, idx + 1);
474 // --- extend list if necessary -------------------------------------
476 if(tsize % KB_PREALLOC == 0) {
477 kb_key * nt = new kb_key[tsize + KB_PREALLOC];
478 // Set to 0 as table is used uninitialised later (thornley)
480 memcpy(nt, tab, tsize * sizeof(kb_key));
484 if(size >= 0) size = tsize + KB_PREALLOC;
487 // --- add action ---------------------------------------------------
489 tab[tsize--].code = NoSymbol;
490 tab[tsize].code = code;
491 tab[tsize].mod = modmsk;
492 kb_key * newone = &tab[tsize];
494 // --- define rest of sequence --------------------------------------
496 if(idx+1 == seq->length) {
497 newone->action = action;
501 newone->table = new kb_keymap;
502 int res = newone->table->defkey(seq, action, idx+1);
508 /* ---F+------------------------------------------------------------------ *\
509 Function : kb_keymap::~kb_keymap
510 Called by : [destructor]
511 Purpose : free keymap and its descendents
514 \* ---F------------------------------------------------------------------- */
516 kb_keymap::~kb_keymap()
519 for(kb_key * t = table; t->code != NoSymbol; ++t)
526 string keyname(kb_key k)
529 printKeysym(k.code, k.mod, buf);
534 // Finds a key for a keyaction, if possible
535 string kb_keymap::findbinding(int act) const
538 if (!table) return res;
540 for(kb_key * t = table; t->code != NoSymbol; ++t) {
542 string suffix = t->table->findbinding(act);
543 suffix = strip(suffix, ' ');
544 suffix = strip(suffix, ']');
545 suffix = frontStrip(suffix, '[');
546 if (!suffix.empty()) {
547 res += "[" + keyname(*t) + " " + suffix + "] ";
549 } else if (t->action == act) {
550 res += "[" + keyname(*t) + "] ";
557 /* === End of File: kbmap.C ============================================== */