1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 *======================================================*/
16 // $Id: kbmap.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $
18 #if !defined(lint) && !defined(WITH_WARNINGS)
19 static char vcid[] = "$Id: kbmap.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $";
23 #pragma implementation
29 // The only modifiers that we handle. We want to throw away things
31 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask};
34 // === static functions ===================================================
37 /* ---F+------------------------------------------------------------------ *\
38 Function : printKeysym
39 Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
40 Purpose : prints a keysym, including modifiers.
41 Parameters: key - keysym
43 buf - string where the result goes
44 maxlen - length of string (including '\0')
45 Returns : length of printed string if ok, 0 otherwise.
46 \* ---F------------------------------------------------------------------- */
49 int printKeysym( KeySym key, unsigned int mod, char *buf, int maxlen )
56 // calc required length;
58 if ( mod & ShiftMask ) len += 2;
59 if ( mod & ControlMask ) len += 2;
60 if ( mod & Mod1Mask ) len += 2;
62 s = XKeysymToString( key );
63 if ( s ) len += strlen( s );
65 if ( mod & ShiftMask ) {
66 *buf++ = 'S'; *buf++ = '-'; }
67 if ( mod & ControlMask ) {
68 *buf++ = 'C'; *buf++ = '-'; }
69 if ( mod & Mod1Mask ) {
70 *buf++ = 'M'; *buf++ = '-'; }
71 if ( s ) strcpy( buf, s );
78 /* ---F+------------------------------------------------------------------ *\
79 Function : printKeyTab
80 Called by : kb_keymap::print
81 Purpose : print the keysyms found in the given key table. RVDK_PATCH_5
82 Parameters: tabPt - keytable pointer
83 buf - string where the result goes
84 maxLen - length of string (including '\0')
85 Returns : length of printed string.
86 \* ---F------------------------------------------------------------------- */
89 int printKeyTab( kb_key *tabPt, char *buf, int maxLen )
92 unsigned int ksym, mod;
94 /* -------> Print each of the slots into buf. */
95 for( ; (tabPt->code & 0xffff) != NoSymbol; tabPt++) {
96 if ( maxLen <= 0 ) break;
99 mod = tabPt->mod & 0xffff;
101 len = printKeysym( ksym, mod, buf, maxLen );
102 if ( len <= 0 ) break;
107 /* -------> Add space when possible. */
120 // === kb_sequence methods ================================================
124 /* ---F+------------------------------------------------------------------ *\
125 Function : kb_sequence::addkey
127 Purpose : add a key to the sequence, look up in map and return action
128 Parameters: key - keysym of key
130 nmod - modifier veto mask (unused now)
131 Returns : action or -1 if error (no map defined or key not found)
132 \* ---F------------------------------------------------------------------- */
134 int kb_sequence::addkey(KeySym key, unsigned int mod, unsigned int nmod /*=0*/)
136 if(length<0) length=0;
138 if(length+1 >= size) {
139 unsigned int *nseq = new unsigned int[size+KB_PREALLOC];
141 memcpy(nseq, sequence, length*sizeof(unsigned int));
142 if(sequence != staticseq) delete sequence;
144 nseq = new unsigned int[size];
145 memcpy(nseq, modifiers, length*sizeof(unsigned int));
146 if(modifiers != staticmod) delete modifiers;
150 modifiers[length] = mod + (nmod<<16);
151 sequence[length++] = key;
154 return curmap->lookup(key, mod, this);
160 /* ---F+------------------------------------------------------------------ *\
161 Function : kb_sequence::parse
163 Purpose : parse a string that holds a key sequence and add the keys
164 Parameters: s - string holding the key sequence
165 Returns : 0 - if ok, error pos if error
166 Note : Keys must be separated with whitespace;
167 Use the keysym names used by XStringToKeysym
168 Prefixes are S-, C-, M- for shift, control, meta
169 \* ---F------------------------------------------------------------------- */
171 int kb_sequence::parse(char const*s)
174 unsigned int mod = 0, nmod = 0;
181 if(s[i] && ((unsigned char) s[i]) <= ' ') i++;
184 if(s[i+1]=='-') { // is implicit that s[i]==true
201 } else if(s[i]=='~' && s[i+1] && s[i+2]=='-') {
220 for(j = i; s[j] && ((unsigned char)s[j])>' '; j++)
221 tbuf[j-i] = s[j]; // (!!!check bounds :-)
225 key = XStringToKeysym(tbuf);
226 if(key == NoSymbol) {
227 lyxerr.debug("kbmap.C: No such keysym: "
228 + LString(tbuf),Error::KBMAP);
233 addkey(key, mod, nmod);
242 /* ---F+------------------------------------------------------------------ *\
243 Function : kb_sequence::print
245 Purpose : print the currently defined sequence into a string
246 Parameters: buf - string where the result goes
247 maxlen - length of string (including '\0')
248 when_defined - only print when sequence is real: length > 0.
249 Returns : 0, if ok, -1 if string too long
250 \* ---F------------------------------------------------------------------- */
252 int kb_sequence::print(char *buf, int maxlen, bool when_defined) const
258 if ( l<0 && !when_defined ) l = -l;
260 for(int i = 0; i < l; i++) {
262 mod = modifiers[i] & 0xffff;
264 len = printKeysym( key, mod, buf, maxlen ); // RVDK_PATCH_5
273 if(i+1<l && maxlen>1) { // append a blank
283 /* ---F+------------------------------------------------------------------ *\
284 Function : kb_sequence::printOptions
286 Purpose : print the available key options from the current state in the
287 sequence. RVDK_PATCH_5
288 Parameters: buf - string where the result goes
289 maxlen - length of string (including '\0')
290 Returns : 0, if ok, -1 if string too long
291 \* ---F------------------------------------------------------------------- */
293 int kb_sequence::printOptions(char *buf, int maxlen) const
297 print( buf, maxlen, true );
302 if ( maxlen < 20 || !curmap ) return -1;
304 #warning reimplement kb_sequence using LString
307 strcpy(s,_(" options: "));
312 curmap->print(buf, maxlen);
317 /* ---F+------------------------------------------------------------------ *\
318 Function : kb_sequence::delseq
320 Purpose : mark the sequence as deleted
323 \* ---F------------------------------------------------------------------- */
325 void kb_sequence::delseq()
327 // negative length marks sequence as deleted, but we can still
328 // print() it or retrieve the last char using getiso()
333 /* ---F+------------------------------------------------------------------ *\
334 Function : kb_sequence::getsym
335 Called by : [user], getiso
336 Purpose : get the keysym of the last key in sequence
339 \* ---F------------------------------------------------------------------- */
341 KeySym kb_sequence::getsym()
344 if(l==0) return NoSymbol;
346 return sequence[l-1];
350 /* ---F+------------------------------------------------------------------ *\
351 Function : kb_sequence::getiso
353 Purpose : return iso character code of last key, if any
355 Returns : iso code or 0 if none
356 \* ---F------------------------------------------------------------------- */
358 char kb_sequence::getiso()
368 /* ---F+------------------------------------------------------------------ *\
369 Function : kb_sequence::reset
371 Purpose : reset sequence to initial state. RVDK_PATCH_5
374 \* ---F------------------------------------------------------------------- */
376 void kb_sequence::reset()
380 if ( length > 0 ) length = -length;
384 // === kb_keymap methods ==================================================
386 // This binds a key to an action
387 int kb_keymap::bind(char const *seq, int action)
391 int res = k.parse(seq);
395 lyxerr.debug(LString("Parse error at position ") + res +
396 " in key sequence '" + seq + "'.", Error::KBMAP);
401 /* ---F+------------------------------------------------------------------ *\
402 Function : kb_keymap::lookup
403 Called by : [user], kb_sequence::add()
404 Purpose : look up a key press in a given keymap
405 Parameters: key - the keysym of the key press
406 mod - the modifier mask of the keypress
407 seq - the key-sequence retrieved so far
408 Returns : user defined action; 0 for prefix key, -1 if key not found
409 \* ---F------------------------------------------------------------------- */
411 int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence *seq)
413 unsigned int hashval, ksym, msk1, msk0;
416 //suppress modifier bits we do not handle
420 // error - no keymap defined:
421 seq->curmap = seq->stdmap;
426 if(size < 0) { // --- if hash table ---
427 hashval = ((key&0xff) ^ ((key>>8)&0xff)) % KB_HASHSIZE;
428 tab = htable[hashval];
430 seq->curmap = seq->stdmap;
434 } else // --- else: linear list ---
437 // --- now search the list of keys ---
439 for( ; (tab->code & 0xffff) != NoSymbol; tab++) {
441 msk1 = tab->mod & 0xffff;
442 msk0 = (tab->mod>>16) & 0xffff;
444 if(ksym == key && (mod&~msk0) == msk1) {
447 // this is a prefix key - set new map
448 seq->curmap = tab->table;
451 // final key - reset map
452 seq->curmap = seq->stdmap;
454 return tab->action; // ... and return action
459 // error - key not found:
460 seq->curmap = seq->stdmap;
466 /* ---F+------------------------------------------------------------------ *\
467 Function : kb_keymap::print
469 Purpose : Prints all the available keysyms. RVDK_PATCH_5
470 Parameters: buf - string where output goes.
471 maxLen - available length in string, including `\0'.
472 Returns : updated maxLen.
473 \* ---F------------------------------------------------------------------- */
475 int kb_keymap::print(char *buf, int maxLen) const
479 /* -----> Return when running out of string space or when keymap has no table.
480 Else, place a terminating newline in case no other output is generated. */
482 if ( maxLen <= 3 || !buf ) return maxLen;
483 if ( !table ) return maxLen;
486 /* -------> Hash table. Process each of its slots recursively and return. */
488 for ( int ix = 0; (ix < KB_HASHSIZE) && (maxLen > 1); ix++ ) {
490 len = printKeyTab( htable[ix], buf, maxLen );
496 /* -------> Normal table. */
497 len = printKeyTab( table, buf, maxLen );
505 /* ---F+------------------------------------------------------------------ *\
506 Function : kb_keymap::defkey
508 Purpose : define an action for a key sequence
509 Parameters: seq - the key sequence
510 action - the action to be defined
511 idx - recursion depth
513 \* ---F------------------------------------------------------------------- */
515 int kb_keymap::defkey(kb_sequence *seq, int action, int idx /*=0*/)
518 unsigned int code, modmsk;
521 code = seq->sequence[idx];
522 modmsk = seq->modifiers[idx];
523 if(code == NoSymbol) return -1;
525 // --- get list------------------------------------------------------
527 // If we don't have any yet, make an empty one
528 table = new kb_key[KB_PREALLOC];
529 table[0].code = NoSymbol;
535 int hashval = (code&0xffff);
536 hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
537 tab = htable[hashval];
538 ptab = htable+hashval;
540 tab = new kb_key[KB_PREALLOC];
541 tab[0].code = NoSymbol;
549 // --- check if key is already there --------------------------------
552 for(t = tab, tsize=1; t->code != NoSymbol; t++, tsize++) {
553 if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
554 if(idx+1 == seq->length) {
555 char buf[20]; buf[0] = 0;
556 seq->print(buf, 20, true);
557 lyxerr.debug(LString("Warning: New binding for '") + buf +
558 "' is overriding old binding...", Error::KEY);
566 } else if (!t->table) {
567 char buf[20]; buf[0] = 0;
568 seq->print(buf, 20, true);
569 lyxerr.print(LString("Error: New binding for '") + buf +
570 "' is overriding old binding...");
573 return t->table->defkey(seq, action, idx+1);
577 // --- extend list if necessary -------------------------------------
579 if(tsize % KB_PREALLOC == 0) {
580 kb_key *nt = new kb_key[tsize+KB_PREALLOC];
581 // Set to NULL as table is used uninitialised later (thornley)
582 nt[tsize].table = NULL;
583 memcpy(nt, tab, tsize*sizeof(kb_key));
587 if(size>=0) size = tsize+KB_PREALLOC;
590 // --- add action ---------------------------------------------------
592 tab[tsize--].code = NoSymbol;
593 tab[tsize].code = code;
594 tab[tsize].mod = modmsk;
595 kb_key *newone = &tab[tsize];
597 // --- convert list to hash table if necessary ----------------------
599 if(size>=0 && tsize>=32) {
600 kb_key *oldtab = tab;
601 kb_key **nht = new kb_key*[KB_HASHSIZE];
602 for(int i = 0; i < KB_HASHSIZE; i++)
607 // --- copy old keys to new hash table ---
609 for(kb_key *tu = oldtab; tu->code != NoSymbol; tu++){
610 // copy values from oldtab to htable
611 hashval = (tu->code&0xffff);
612 hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
613 tab = htable[hashval];
616 htable[hashval] = tab = new kb_key[KB_PREALLOC];
617 tab->code = NoSymbol;
620 for(kb_key *tt = tab; tt->code != NoSymbol; tt++)
622 if(ts % KB_PREALLOC == 0){
624 kb_key *nt = new kb_key[ts+KB_PREALLOC];
625 memcpy(nt, tab, ts*sizeof(kb_key));
626 htable[hashval] = nt;
630 tab[ts--].code = NoSymbol;
631 tab[ts].code = tu->code;
632 tab[ts].mod = tu->mod;
633 tab[ts].action = tu->action;
634 tab[ts].table = tu->table;
642 // --- define rest of sequence --------------------------------------
644 if(idx+1 == seq->length) {
645 newone->action = action;
649 newone->table = new kb_keymap;
650 int res = newone->table->defkey(seq, action, idx+1);
656 /* ---F+------------------------------------------------------------------ *\
657 Function : kb_keymap::~kb_keymap
658 Called by : [destructor]
659 Purpose : free keymap and its descendents
662 \* ---F------------------------------------------------------------------- */
664 kb_keymap::~kb_keymap()
668 for(int i=0; i < KB_HASHSIZE; i++) {
670 for(kb_key *t = htable[i]; t->code != NoSymbol; t++)
678 for(kb_key *t = table; t->code != NoSymbol; t++)
685 LString keyname(kb_key k) {
687 printKeysym(k.code, k.mod, buf, 100);
691 // Finds a key for a keyaction, if possible
692 LString kb_keymap::findbinding(int act) const {
698 for(int i=0; i < KB_HASHSIZE; i++) {
700 for(kb_key *t = htable[i]; t->code != NoSymbol; t++) {
702 LString suffix = t->table->findbinding(act);
705 suffix.frontStrip('[');
706 if (!suffix.empty()) {
707 res += "[" + keyname(*t) + " " + suffix + "] ";
709 } else if (t->action == act) {
710 res += "[" + keyname(*t) + "] ";
716 for(kb_key *t = table; t->code != NoSymbol; t++) {
718 LString suffix = t->table->findbinding(act);
721 suffix.frontStrip('[');
722 if (!suffix.empty()) {
723 res += "[" + keyname(*t) + " " + suffix + "] ";
725 } else if (t->action == act) {
726 res += "[" + keyname(*t) + "] ";
734 /* === End of File: kbmap.C ============================================== */