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)
349 unsigned int hashval;
351 unsigned int ksym, msk1, msk0;
354 //suppress modifier bits we do not handle
358 // error - no keymap defined:
359 seq->curmap = seq->stdmap;
365 if(size < 0) { // --- if hash table ---
366 hashval = ((key & 0xff) ^ ((key >> 8) & 0xff)) % KB_HASHSIZE;
367 tab = htable[hashval];
369 seq->curmap = seq->stdmap;
373 } else // --- else: linear list ---
377 // --- now search the list of keys ---
379 for(; (tab->code & 0xffff) != NoSymbol; ++tab) {
381 msk1 = tab->mod & 0xffff;
382 msk0 = (tab->mod >> 16) & 0xffff;
384 if(ksym == key && (mod & ~msk0) == msk1) {
387 // this is a prefix key - set new map
388 seq->curmap = tab->table;
391 // final key - reset map
392 seq->curmap = seq->stdmap;
394 return tab->action; // ... and return action
399 // error - key not found:
400 seq->curmap = seq->stdmap;
406 /* ---F+------------------------------------------------------------------ *\
407 Function : kb_keymap::print
409 Purpose : Prints all the available keysyms. RVDK_PATCH_5
410 Parameters: buf - string where output goes.
411 maxLen - available length in string, including `\0'.
412 Returns : updated maxLen.
413 \* ---F------------------------------------------------------------------- */
415 void kb_keymap::print(string & buf) const
417 // Return when keymap has no table.
420 // Process each of its slots recursively and return.
422 if ( size < 0 ) { // Hash table
423 for ( int ix = 0; ix < KB_HASHSIZE; ++ix ) {
425 printKeyTab(htable[ix], buf);
428 } else // Normal table
430 printKeyTab(table, buf);
434 /* ---F+------------------------------------------------------------------ *\
435 Function : kb_keymap::defkey
437 Purpose : define an action for a key sequence
438 Parameters: seq - the key sequence
439 action - the action to be defined
440 idx - recursion depth
442 \* ---F------------------------------------------------------------------- */
444 int kb_keymap::defkey(kb_sequence * seq, int action, int idx /*= 0*/)
446 unsigned int code = seq->sequence[idx];
447 if(code == NoSymbol) return -1;
449 unsigned int modmsk = seq->modifiers[idx];
450 kb_key * tab, ** ptab;
451 // --- get list------------------------------------------------------
453 // If we don't have any yet, make an empty one
454 table = new kb_key[KB_PREALLOC];
455 table[0].code = NoSymbol;
460 } else if(size < 0) {
462 int hashval = code & 0xffff;
463 hashval = ((hashval & 0xff) ^ ((hashval >> 8) & 0xff)) % KB_HASHSIZE;
464 tab = htable[hashval];
465 ptab = htable+hashval;
467 tab = new kb_key[KB_PREALLOC];
468 tab[0].code = NoSymbol;
477 // --- check if key is already there --------------------------------
481 for(t = tab, tsize = 1; t->code != NoSymbol; ++t, ++tsize) {
482 if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
483 if(idx + 1 == seq->length) {
485 seq->print(buf, true);
487 << "Warning: New binding for '"
489 << "' is overriding old binding..."
498 } else if (!t->table) {
500 seq->print(buf, true);
501 lyxerr << "Error: New binding for '" << buf
502 << "' is overriding old binding..."
506 return t->table->defkey(seq, action, idx + 1);
510 // --- extend list if necessary -------------------------------------
512 if(tsize % KB_PREALLOC == 0) {
513 kb_key * nt = new kb_key[tsize + KB_PREALLOC];
514 // Set to 0 as table is used uninitialised later (thornley)
516 memcpy(nt, tab, tsize * sizeof(kb_key));
520 if(size >= 0) size = tsize + KB_PREALLOC;
523 // --- add action ---------------------------------------------------
525 tab[tsize--].code = NoSymbol;
526 tab[tsize].code = code;
527 tab[tsize].mod = modmsk;
528 kb_key * newone = &tab[tsize];
530 // --- convert list to hash table if necessary ----------------------
533 if(size >= 0 && tsize >= 32) {
534 kb_key * oldtab = tab;
535 kb_key ** nht = new kb_key*[KB_HASHSIZE];
536 for(int i = 0; i < KB_HASHSIZE; ++i)
541 // --- copy old keys to new hash table ---
543 for(kb_key * tu = oldtab; tu->code != NoSymbol; ++tu) {
544 // copy values from oldtab to htable
545 hashval = (tu->code & 0xffff);
546 hashval = ((hashval & 0xff) ^ ((hashval>>8) & 0xff)) % KB_HASHSIZE;
547 tab = htable[hashval];
550 htable[hashval] = tab = new kb_key[KB_PREALLOC];
551 tab->code = NoSymbol;
554 for(kb_key * tt = tab; tt->code != NoSymbol; ++tt)
556 if(ts % KB_PREALLOC == 0){
558 kb_key * nt = new kb_key[ts+KB_PREALLOC];
559 memcpy(nt, tab, ts * sizeof(kb_key));
560 htable[hashval] = nt;
564 tab[ts--].code = NoSymbol;
565 tab[ts].code = tu->code;
566 tab[ts].mod = tu->mod;
567 tab[ts].action = tu->action;
568 tab[ts].table = tu->table;
576 // --- define rest of sequence --------------------------------------
578 if(idx+1 == seq->length) {
579 newone->action = action;
583 newone->table = new kb_keymap;
584 int res = newone->table->defkey(seq, action, idx+1);
590 /* ---F+------------------------------------------------------------------ *\
591 Function : kb_keymap::~kb_keymap
592 Called by : [destructor]
593 Purpose : free keymap and its descendents
596 \* ---F------------------------------------------------------------------- */
598 kb_keymap::~kb_keymap()
603 for(int i = 0; i < KB_HASHSIZE; ++i) {
605 for(kb_key * t = htable[i];
606 t->code != NoSymbol; ++t)
615 for(kb_key * t = table; t->code != NoSymbol; ++t)
625 string keyname(kb_key k)
628 printKeysym(k.code, k.mod, buf);
633 // Finds a key for a keyaction, if possible
634 string kb_keymap::findbinding(int act) const
637 if (!table) return res;
641 for(int i = 0; i < KB_HASHSIZE; ++i) {
643 for(kb_key * t = htable[i];
644 t->code != NoSymbol; ++t) {
646 string suffix = t->table->findbinding(act);
647 suffix = strip(suffix, ' ');
648 suffix = strip(suffix, ']');
649 suffix = frontStrip(suffix, '[');
650 if (!suffix.empty()) {
651 res += "[" + keyname(*t) + " " + suffix + "] ";
653 } else if (t->action == act) {
654 res += "[" + keyname(*t) + "] ";
661 for(kb_key * t = table; t->code != NoSymbol; ++t) {
663 string suffix = t->table->findbinding(act);
664 suffix = strip(suffix, ' ');
665 suffix = strip(suffix, ']');
666 suffix = frontStrip(suffix, '[');
667 if (!suffix.empty()) {
668 res += "[" + keyname(*t) + " " + suffix + "] ";
670 } else if (t->action == act) {
671 res += "[" + keyname(*t) + "] ";
681 /* === End of File: kbmap.C ============================================== */