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 * ====================================================== */
14 #include "support/lstrings.h"
18 #pragma implementation
24 // The only modifiers that we handle. We want to throw away things
26 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask};
29 // === static functions ===================================================
32 /* ---F+------------------------------------------------------------------ *\
33 Function : printKeysym
34 Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
35 Purpose : prints a keysym, including modifiers.
36 Parameters: key - keysym
38 buf - string where the result goes
39 maxlen - length of string (including '\0')
40 Returns : length of printed string if ok, 0 otherwise.
41 \* ---F------------------------------------------------------------------- */
44 int printKeysym( KeySym key, unsigned int mod, char *buf, int maxlen )
48 // calc required length;
50 if ( mod & ShiftMask ) len += 2;
51 if ( mod & ControlMask ) len += 2;
52 if ( mod & Mod1Mask ) len += 2;
54 char * s = XKeysymToString( key );
55 if ( s ) len += strlen( s );
57 if ( mod & ShiftMask ) {
58 *buf++ = 'S'; *buf++ = '-'; }
59 if ( mod & ControlMask ) {
60 *buf++ = 'C'; *buf++ = '-'; }
61 if ( mod & Mod1Mask ) {
62 *buf++ = 'M'; *buf++ = '-'; }
63 if ( s ) strcpy( buf, s );
70 /* ---F+------------------------------------------------------------------ *\
71 Function : printKeyTab
72 Called by : kb_keymap::print
73 Purpose : print the keysyms found in the given key table. RVDK_PATCH_5
74 Parameters: tabPt - keytable pointer
75 buf - string where the result goes
76 maxLen - length of string (including '\0')
77 Returns : length of printed string.
78 \* ---F------------------------------------------------------------------- */
81 int printKeyTab( kb_key * tabPt, char *buf, int maxLen )
84 unsigned int ksym, mod;
86 /* -------> Print each of the slots into buf. */
87 for( ; (tabPt->code & 0xffff) != NoSymbol; ++tabPt) {
88 if ( maxLen <= 0 ) break;
91 mod = tabPt->mod & 0xffff;
93 len = printKeysym( ksym, mod, buf, maxLen );
94 if ( len <= 0 ) break;
99 /* -------> Add space when possible. */
112 // === kb_sequence methods ================================================
116 /* ---F+------------------------------------------------------------------ *\
117 Function : kb_sequence::addkey
119 Purpose : add a key to the sequence, look up in map and return action
120 Parameters: key - keysym of key
122 nmod - modifier veto mask (unused now)
123 Returns : action or -1 if error (no map defined or key not found)
124 \* ---F------------------------------------------------------------------- */
126 int kb_sequence::addkey(KeySym key, unsigned int mod, unsigned int nmod /*= 0*/)
128 if(length < 0) length = 0;
130 if(length + 1 >= size) {
131 unsigned int * nseq = new unsigned int[size+KB_PREALLOC];
133 memcpy(nseq, sequence, length * sizeof(unsigned int));
134 if(sequence != staticseq) delete sequence;
136 nseq = new unsigned int[size];
137 memcpy(nseq, modifiers, length * sizeof(unsigned int));
138 if(modifiers != staticmod) delete modifiers;
142 modifiers[length] = mod + (nmod << 16);
143 sequence[length++] = key;
146 return curmap->lookup(key, mod, this);
152 /* ---F+------------------------------------------------------------------ *\
153 Function : kb_sequence::parse
155 Purpose : parse a string that holds a key sequence and add the keys
156 Parameters: s - string holding the key sequence
157 Returns : 0 - if ok, error pos if error
158 Note : Keys must be separated with whitespace;
159 Use the keysym names used by XStringToKeysym
160 Prefixes are S-, C-, M- for shift, control, meta
161 \* ---F------------------------------------------------------------------- */
163 int kb_sequence::parse(char const * s)
166 unsigned int mod = 0, nmod = 0;
173 if(s[i] && (s[i]) <= ' ') ++i;
176 if(s[i+1] == '-') { // is implicit that s[i] == true
193 } else if(s[i] == '~' && s[i+1] && s[i+2] == '-') {
212 for(j = i; s[j] && (s[j])>' '; ++j)
213 tbuf[j-i] = s[j]; // (!!!check bounds :-)
217 key = XStringToKeysym(tbuf);
218 if(key == NoSymbol) {
220 << "kbmap.C: No such keysym: "
226 addkey(key, mod, nmod);
235 /* ---F+------------------------------------------------------------------ *\
236 Function : kb_sequence::print
238 Purpose : print the currently defined sequence into a string
239 Parameters: buf - string where the result goes
240 maxlen - length of string (including '\0')
241 when_defined - only print when sequence is real: length > 0.
242 Returns : 0, if ok, -1 if string too long
243 \* ---F------------------------------------------------------------------- */
245 int kb_sequence::print(char * buf, int maxlen, bool when_defined) const
251 if ( l < 0 && !when_defined ) l = -l;
253 for(int i = 0; i < l; ++i) {
255 mod = modifiers[i] & 0xffff;
257 len = printKeysym( key, mod, buf, maxlen ); // RVDK_PATCH_5
266 if(i+1<l && maxlen>1) { // append a blank
276 /* ---F+------------------------------------------------------------------ *\
277 Function : kb_sequence::printOptions
279 Purpose : print the available key options from the current state in the
280 sequence. RVDK_PATCH_5
281 Parameters: buf - string where the result goes
282 maxlen - length of string (including '\0')
283 Returns : 0, if ok, -1 if string too long
284 \* ---F------------------------------------------------------------------- */
286 int kb_sequence::printOptions(char * buf, int maxlen) const
288 print(buf, maxlen, true);
289 int len = strlen(buf);
293 if ( maxlen < 20 || !curmap ) return -1;
295 #warning reimplement kb_sequence using string
298 strcpy(s, _(" options: "));
303 curmap->print(buf, maxlen);
308 /* ---F+------------------------------------------------------------------ *\
309 Function : kb_sequence::delseq
311 Purpose : mark the sequence as deleted
314 \* ---F------------------------------------------------------------------- */
316 void kb_sequence::delseq()
318 // negative length marks sequence as deleted, but we can still
319 // print() it or retrieve the last char using getiso()
324 /* ---F+------------------------------------------------------------------ *\
325 Function : kb_sequence::getsym
326 Called by : [user], getiso
327 Purpose : get the keysym of the last key in sequence
330 \* ---F------------------------------------------------------------------- */
332 KeySym kb_sequence::getsym()
335 if(l == 0) return NoSymbol;
337 return sequence[l-1];
341 /* ---F+------------------------------------------------------------------ *\
342 Function : kb_sequence::getiso
344 Purpose : return iso character code of last key, if any
346 Returns : iso code or 0 if none
347 \* ---F------------------------------------------------------------------- */
349 char kb_sequence::getiso()
359 /* ---F+------------------------------------------------------------------ *\
360 Function : kb_sequence::reset
362 Purpose : reset sequence to initial state. RVDK_PATCH_5
365 \* ---F------------------------------------------------------------------- */
367 void kb_sequence::reset()
371 if ( length > 0 ) length = -length;
375 // === kb_keymap methods ==================================================
377 // This binds a key to an action
378 int kb_keymap::bind(char const * seq, int action)
382 int res = k.parse(seq);
386 lyxerr[Debug::KBMAP] << "Parse error at position " << res
387 << " in key sequence '" << seq << "'."
393 /* ---F+------------------------------------------------------------------ *\
394 Function : kb_keymap::lookup
395 Called by : [user], kb_sequence::add()
396 Purpose : look up a key press in a given keymap
397 Parameters: key - the keysym of the key press
398 mod - the modifier mask of the keypress
399 seq - the key-sequence retrieved so far
400 Returns : user defined action; 0 for prefix key, -1 if key not found
401 \* ---F------------------------------------------------------------------- */
403 int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence * seq)
405 unsigned int hashval, ksym, msk1, msk0;
408 //suppress modifier bits we do not handle
412 // error - no keymap defined:
413 seq->curmap = seq->stdmap;
418 if(size < 0) { // --- if hash table ---
419 hashval = ((key&0xff) ^ ((key>>8)&0xff)) % KB_HASHSIZE;
420 tab = htable[hashval];
422 seq->curmap = seq->stdmap;
426 } else // --- else: linear list ---
429 // --- now search the list of keys ---
431 for( ; (tab->code & 0xffff) != NoSymbol; ++tab) {
433 msk1 = tab->mod & 0xffff;
434 msk0 = (tab->mod >> 16) & 0xffff;
436 if(ksym == key && (mod & ~msk0) == msk1) {
439 // this is a prefix key - set new map
440 seq->curmap = tab->table;
443 // final key - reset map
444 seq->curmap = seq->stdmap;
446 return tab->action; // ... and return action
451 // error - key not found:
452 seq->curmap = seq->stdmap;
458 /* ---F+------------------------------------------------------------------ *\
459 Function : kb_keymap::print
461 Purpose : Prints all the available keysyms. RVDK_PATCH_5
462 Parameters: buf - string where output goes.
463 maxLen - available length in string, including `\0'.
464 Returns : updated maxLen.
465 \* ---F------------------------------------------------------------------- */
467 int kb_keymap::print(char * buf, int maxLen) const
469 /* -----> Return when running out of string space or when keymap has no table.
470 Else, place a terminating newline in case no other output is generated. */
472 if ( maxLen <= 3 || !buf ) return maxLen;
473 if ( !table ) return maxLen;
476 /* -------> Hash table. Process each of its slots recursively and return. */
478 for ( int ix = 0; (ix < KB_HASHSIZE) && (maxLen > 1); ++ix ) {
480 int len = printKeyTab( htable[ix], buf, maxLen );
486 /* -------> Normal table. */
487 int len = printKeyTab( table, buf, maxLen );
495 /* ---F+------------------------------------------------------------------ *\
496 Function : kb_keymap::defkey
498 Purpose : define an action for a key sequence
499 Parameters: seq - the key sequence
500 action - the action to be defined
501 idx - recursion depth
503 \* ---F------------------------------------------------------------------- */
505 int kb_keymap::defkey(kb_sequence *seq, int action, int idx /*= 0*/)
507 unsigned int code = seq->sequence[idx];
508 if(code == NoSymbol) return -1;
510 unsigned int modmsk = seq->modifiers[idx];
512 // --- get list------------------------------------------------------
514 // If we don't have any yet, make an empty one
515 table = new kb_key[KB_PREALLOC];
516 table[0].code = NoSymbol;
522 int hashval = (code&0xffff);
523 hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
524 tab = htable[hashval];
525 ptab = htable+hashval;
527 tab = new kb_key[KB_PREALLOC];
528 tab[0].code = NoSymbol;
536 // --- check if key is already there --------------------------------
540 for(t = tab, tsize = 1; t->code != NoSymbol; ++t, ++tsize) {
541 if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
542 if(idx+1 == seq->length) {
543 char buf[20]; buf[0] = 0;
544 seq->print(buf, 20, true);
546 << "Warning: New binding for '"
548 << "' is overriding old binding..."
557 } else if (!t->table) {
558 char buf[20]; buf[0] = 0;
559 seq->print(buf, 20, true);
560 lyxerr << "Error: New binding for '" << buf
561 << "' is overriding old binding..."
565 return t->table->defkey(seq, action, idx+1);
569 // --- extend list if necessary -------------------------------------
571 if(tsize % KB_PREALLOC == 0) {
572 kb_key * nt = new kb_key[tsize+KB_PREALLOC];
573 // Set to 0 as table is used uninitialised later (thornley)
575 memcpy(nt, tab, tsize * sizeof(kb_key));
579 if(size>= 0) size = tsize + KB_PREALLOC;
582 // --- add action ---------------------------------------------------
584 tab[tsize--].code = NoSymbol;
585 tab[tsize].code = code;
586 tab[tsize].mod = modmsk;
587 kb_key * newone = &tab[tsize];
589 // --- convert list to hash table if necessary ----------------------
591 if(size >= 0 && tsize >= 32) {
592 kb_key * oldtab = tab;
593 kb_key ** nht = new kb_key*[KB_HASHSIZE];
594 for(int i = 0; i < KB_HASHSIZE; ++i)
599 // --- copy old keys to new hash table ---
601 for(kb_key * tu = oldtab; tu->code != NoSymbol; ++tu) {
602 // copy values from oldtab to htable
603 hashval = (tu->code & 0xffff);
604 hashval = ((hashval & 0xff) ^ ((hashval>>8) & 0xff)) % KB_HASHSIZE;
605 tab = htable[hashval];
608 htable[hashval] = tab = new kb_key[KB_PREALLOC];
609 tab->code = NoSymbol;
612 for(kb_key * tt = tab; tt->code != NoSymbol; ++tt)
614 if(ts % KB_PREALLOC == 0){
616 kb_key * nt = new kb_key[ts+KB_PREALLOC];
617 memcpy(nt, tab, ts * sizeof(kb_key));
618 htable[hashval] = nt;
622 tab[ts--].code = NoSymbol;
623 tab[ts].code = tu->code;
624 tab[ts].mod = tu->mod;
625 tab[ts].action = tu->action;
626 tab[ts].table = tu->table;
634 // --- define rest of sequence --------------------------------------
636 if(idx+1 == seq->length) {
637 newone->action = action;
641 newone->table = new kb_keymap;
642 int res = newone->table->defkey(seq, action, idx+1);
648 /* ---F+------------------------------------------------------------------ *\
649 Function : kb_keymap::~kb_keymap
650 Called by : [destructor]
651 Purpose : free keymap and its descendents
654 \* ---F------------------------------------------------------------------- */
656 kb_keymap::~kb_keymap()
660 for(int i = 0; i < KB_HASHSIZE; ++i) {
662 for(kb_key * t = htable[i];
663 t->code != NoSymbol; ++t)
671 for(kb_key * t = table; t->code != NoSymbol; ++t)
678 string keyname(kb_key k) {
680 printKeysym(k.code, k.mod, buf, 100);
684 // Finds a key for a keyaction, if possible
685 string kb_keymap::findbinding(int act) const {
691 for(int i = 0; i < KB_HASHSIZE; ++i) {
693 for(kb_key * t = htable[i];
694 t->code != NoSymbol; ++t) {
696 string suffix = t->table->findbinding(act);
697 suffix = strip(suffix, ' ');
698 suffix = strip(suffix, ']');
699 suffix = frontStrip(suffix, '[');
700 if (!suffix.empty()) {
701 res += "[" + keyname(*t) + " " + suffix + "] ";
703 } else if (t->action == act) {
704 res += "[" + keyname(*t) + "] ";
710 for(kb_key * t = table; t->code != NoSymbol; ++t) {
712 string suffix = t->table->findbinding(act);
713 suffix = strip(suffix, ' ');
714 suffix = strip(suffix, ']');
715 suffix = frontStrip(suffix, '[');
716 if (!suffix.empty()) {
717 res += "[" + keyname(*t) + " " + suffix + "] ";
719 } else if (t->action == act) {
720 res += "[" + keyname(*t) + "] ";
728 /* === End of File: kbmap.C ============================================== */