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 )
51 // calc required length;
53 if ( mod & ShiftMask ) len += 2;
54 if ( mod & ControlMask ) len += 2;
55 if ( mod & Mod1Mask ) len += 2;
57 s = XKeysymToString( key );
58 if ( s ) len += strlen( s );
60 if ( mod & ShiftMask ) {
61 *buf++ = 'S'; *buf++ = '-'; }
62 if ( mod & ControlMask ) {
63 *buf++ = 'C'; *buf++ = '-'; }
64 if ( mod & Mod1Mask ) {
65 *buf++ = 'M'; *buf++ = '-'; }
66 if ( s ) strcpy( buf, s );
73 /* ---F+------------------------------------------------------------------ *\
74 Function : printKeyTab
75 Called by : kb_keymap::print
76 Purpose : print the keysyms found in the given key table. RVDK_PATCH_5
77 Parameters: tabPt - keytable pointer
78 buf - string where the result goes
79 maxLen - length of string (including '\0')
80 Returns : length of printed string.
81 \* ---F------------------------------------------------------------------- */
84 int printKeyTab( kb_key *tabPt, char *buf, int maxLen )
87 unsigned int ksym, mod;
89 /* -------> Print each of the slots into buf. */
90 for( ; (tabPt->code & 0xffff) != NoSymbol; tabPt++) {
91 if ( maxLen <= 0 ) break;
94 mod = tabPt->mod & 0xffff;
96 len = printKeysym( ksym, mod, buf, maxLen );
97 if ( len <= 0 ) break;
102 /* -------> Add space when possible. */
115 // === kb_sequence methods ================================================
119 /* ---F+------------------------------------------------------------------ *\
120 Function : kb_sequence::addkey
122 Purpose : add a key to the sequence, look up in map and return action
123 Parameters: key - keysym of key
125 nmod - modifier veto mask (unused now)
126 Returns : action or -1 if error (no map defined or key not found)
127 \* ---F------------------------------------------------------------------- */
129 int kb_sequence::addkey(KeySym key, unsigned int mod, unsigned int nmod /*=0*/)
131 if(length<0) length=0;
133 if(length+1 >= size) {
134 unsigned int *nseq = new unsigned int[size+KB_PREALLOC];
136 memcpy(nseq, sequence, length*sizeof(unsigned int));
137 if(sequence != staticseq) delete sequence;
139 nseq = new unsigned int[size];
140 memcpy(nseq, modifiers, length*sizeof(unsigned int));
141 if(modifiers != staticmod) delete modifiers;
145 modifiers[length] = mod + (nmod<<16);
146 sequence[length++] = key;
149 return curmap->lookup(key, mod, this);
155 /* ---F+------------------------------------------------------------------ *\
156 Function : kb_sequence::parse
158 Purpose : parse a string that holds a key sequence and add the keys
159 Parameters: s - string holding the key sequence
160 Returns : 0 - if ok, error pos if error
161 Note : Keys must be separated with whitespace;
162 Use the keysym names used by XStringToKeysym
163 Prefixes are S-, C-, M- for shift, control, meta
164 \* ---F------------------------------------------------------------------- */
166 int kb_sequence::parse(char const*s)
169 unsigned int mod = 0, nmod = 0;
176 if(s[i] && ((unsigned char) s[i]) <= ' ') i++;
179 if(s[i+1]=='-') { // is implicit that s[i]==true
196 } else if(s[i]=='~' && s[i+1] && s[i+2]=='-') {
215 for(j = i; s[j] && ((unsigned char)s[j])>' '; j++)
216 tbuf[j-i] = s[j]; // (!!!check bounds :-)
220 key = XStringToKeysym(tbuf);
221 if(key == NoSymbol) {
222 lyxerr.debug("kbmap.C: No such keysym: "
223 + string(tbuf),Error::KBMAP);
228 addkey(key, mod, nmod);
237 /* ---F+------------------------------------------------------------------ *\
238 Function : kb_sequence::print
240 Purpose : print the currently defined sequence into a string
241 Parameters: buf - string where the result goes
242 maxlen - length of string (including '\0')
243 when_defined - only print when sequence is real: length > 0.
244 Returns : 0, if ok, -1 if string too long
245 \* ---F------------------------------------------------------------------- */
247 int kb_sequence::print(char *buf, int maxlen, bool when_defined) const
253 if ( l<0 && !when_defined ) l = -l;
255 for(int i = 0; i < l; i++) {
257 mod = modifiers[i] & 0xffff;
259 len = printKeysym( key, mod, buf, maxlen ); // RVDK_PATCH_5
268 if(i+1<l && maxlen>1) { // append a blank
278 /* ---F+------------------------------------------------------------------ *\
279 Function : kb_sequence::printOptions
281 Purpose : print the available key options from the current state in the
282 sequence. RVDK_PATCH_5
283 Parameters: buf - string where the result goes
284 maxlen - length of string (including '\0')
285 Returns : 0, if ok, -1 if string too long
286 \* ---F------------------------------------------------------------------- */
288 int kb_sequence::printOptions(char *buf, int maxlen) const
292 print( buf, maxlen, true );
297 if ( maxlen < 20 || !curmap ) return -1;
299 #warning reimplement kb_sequence using string
302 strcpy(s,_(" options: "));
307 curmap->print(buf, maxlen);
312 /* ---F+------------------------------------------------------------------ *\
313 Function : kb_sequence::delseq
315 Purpose : mark the sequence as deleted
318 \* ---F------------------------------------------------------------------- */
320 void kb_sequence::delseq()
322 // negative length marks sequence as deleted, but we can still
323 // print() it or retrieve the last char using getiso()
328 /* ---F+------------------------------------------------------------------ *\
329 Function : kb_sequence::getsym
330 Called by : [user], getiso
331 Purpose : get the keysym of the last key in sequence
334 \* ---F------------------------------------------------------------------- */
336 KeySym kb_sequence::getsym()
339 if(l==0) return NoSymbol;
341 return sequence[l-1];
345 /* ---F+------------------------------------------------------------------ *\
346 Function : kb_sequence::getiso
348 Purpose : return iso character code of last key, if any
350 Returns : iso code or 0 if none
351 \* ---F------------------------------------------------------------------- */
353 char kb_sequence::getiso()
363 /* ---F+------------------------------------------------------------------ *\
364 Function : kb_sequence::reset
366 Purpose : reset sequence to initial state. RVDK_PATCH_5
369 \* ---F------------------------------------------------------------------- */
371 void kb_sequence::reset()
375 if ( length > 0 ) length = -length;
379 // === kb_keymap methods ==================================================
381 // This binds a key to an action
382 int kb_keymap::bind(char const *seq, int action)
386 int res = k.parse(seq);
390 lyxerr.debug(string("Parse error at position ") + tostr(res) +
391 " in key sequence '" + seq + "'.", Error::KBMAP);
396 /* ---F+------------------------------------------------------------------ *\
397 Function : kb_keymap::lookup
398 Called by : [user], kb_sequence::add()
399 Purpose : look up a key press in a given keymap
400 Parameters: key - the keysym of the key press
401 mod - the modifier mask of the keypress
402 seq - the key-sequence retrieved so far
403 Returns : user defined action; 0 for prefix key, -1 if key not found
404 \* ---F------------------------------------------------------------------- */
406 int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence *seq)
408 unsigned int hashval, ksym, msk1, msk0;
411 //suppress modifier bits we do not handle
415 // error - no keymap defined:
416 seq->curmap = seq->stdmap;
421 if(size < 0) { // --- if hash table ---
422 hashval = ((key&0xff) ^ ((key>>8)&0xff)) % KB_HASHSIZE;
423 tab = htable[hashval];
425 seq->curmap = seq->stdmap;
429 } else // --- else: linear list ---
432 // --- now search the list of keys ---
434 for( ; (tab->code & 0xffff) != NoSymbol; tab++) {
436 msk1 = tab->mod & 0xffff;
437 msk0 = (tab->mod>>16) & 0xffff;
439 if(ksym == key && (mod&~msk0) == msk1) {
442 // this is a prefix key - set new map
443 seq->curmap = tab->table;
446 // final key - reset map
447 seq->curmap = seq->stdmap;
449 return tab->action; // ... and return action
454 // error - key not found:
455 seq->curmap = seq->stdmap;
461 /* ---F+------------------------------------------------------------------ *\
462 Function : kb_keymap::print
464 Purpose : Prints all the available keysyms. RVDK_PATCH_5
465 Parameters: buf - string where output goes.
466 maxLen - available length in string, including `\0'.
467 Returns : updated maxLen.
468 \* ---F------------------------------------------------------------------- */
470 int kb_keymap::print(char *buf, int maxLen) const
474 /* -----> Return when running out of string space or when keymap has no table.
475 Else, place a terminating newline in case no other output is generated. */
477 if ( maxLen <= 3 || !buf ) return maxLen;
478 if ( !table ) return maxLen;
481 /* -------> Hash table. Process each of its slots recursively and return. */
483 for ( int ix = 0; (ix < KB_HASHSIZE) && (maxLen > 1); ix++ ) {
485 len = printKeyTab( htable[ix], buf, maxLen );
491 /* -------> Normal table. */
492 len = printKeyTab( table, buf, maxLen );
500 /* ---F+------------------------------------------------------------------ *\
501 Function : kb_keymap::defkey
503 Purpose : define an action for a key sequence
504 Parameters: seq - the key sequence
505 action - the action to be defined
506 idx - recursion depth
508 \* ---F------------------------------------------------------------------- */
510 int kb_keymap::defkey(kb_sequence *seq, int action, int idx /*=0*/)
513 unsigned int code, modmsk;
516 code = seq->sequence[idx];
517 modmsk = seq->modifiers[idx];
518 if(code == NoSymbol) return -1;
520 // --- get list------------------------------------------------------
522 // If we don't have any yet, make an empty one
523 table = new kb_key[KB_PREALLOC];
524 table[0].code = NoSymbol;
530 int hashval = (code&0xffff);
531 hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
532 tab = htable[hashval];
533 ptab = htable+hashval;
535 tab = new kb_key[KB_PREALLOC];
536 tab[0].code = NoSymbol;
544 // --- check if key is already there --------------------------------
547 for(t = tab, tsize=1; t->code != NoSymbol; t++, tsize++) {
548 if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
549 if(idx+1 == seq->length) {
550 char buf[20]; buf[0] = 0;
551 seq->print(buf, 20, true);
552 lyxerr.debug(string("Warning: New binding for '") + buf +
553 "' is overriding old binding...", Error::KEY);
561 } else if (!t->table) {
562 char buf[20]; buf[0] = 0;
563 seq->print(buf, 20, true);
564 lyxerr.print(string("Error: New binding for '") + buf +
565 "' is overriding old binding...");
568 return t->table->defkey(seq, action, idx+1);
572 // --- extend list if necessary -------------------------------------
574 if(tsize % KB_PREALLOC == 0) {
575 kb_key *nt = new kb_key[tsize+KB_PREALLOC];
576 // Set to 0 as table is used uninitialised later (thornley)
578 memcpy(nt, tab, tsize*sizeof(kb_key));
582 if(size>=0) size = tsize+KB_PREALLOC;
585 // --- add action ---------------------------------------------------
587 tab[tsize--].code = NoSymbol;
588 tab[tsize].code = code;
589 tab[tsize].mod = modmsk;
590 kb_key *newone = &tab[tsize];
592 // --- convert list to hash table if necessary ----------------------
594 if(size>=0 && tsize>=32) {
595 kb_key *oldtab = tab;
596 kb_key **nht = new kb_key*[KB_HASHSIZE];
597 for(int i = 0; i < KB_HASHSIZE; i++)
602 // --- copy old keys to new hash table ---
604 for(kb_key *tu = oldtab; tu->code != NoSymbol; tu++){
605 // copy values from oldtab to htable
606 hashval = (tu->code&0xffff);
607 hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
608 tab = htable[hashval];
611 htable[hashval] = tab = new kb_key[KB_PREALLOC];
612 tab->code = NoSymbol;
615 for(kb_key *tt = tab; tt->code != NoSymbol; tt++)
617 if(ts % KB_PREALLOC == 0){
619 kb_key *nt = new kb_key[ts+KB_PREALLOC];
620 memcpy(nt, tab, ts*sizeof(kb_key));
621 htable[hashval] = nt;
625 tab[ts--].code = NoSymbol;
626 tab[ts].code = tu->code;
627 tab[ts].mod = tu->mod;
628 tab[ts].action = tu->action;
629 tab[ts].table = tu->table;
637 // --- define rest of sequence --------------------------------------
639 if(idx+1 == seq->length) {
640 newone->action = action;
644 newone->table = new kb_keymap;
645 int res = newone->table->defkey(seq, action, idx+1);
651 /* ---F+------------------------------------------------------------------ *\
652 Function : kb_keymap::~kb_keymap
653 Called by : [destructor]
654 Purpose : free keymap and its descendents
657 \* ---F------------------------------------------------------------------- */
659 kb_keymap::~kb_keymap()
663 for(int i=0; i < KB_HASHSIZE; i++) {
665 for(kb_key *t = htable[i]; t->code != NoSymbol; t++)
673 for(kb_key *t = table; t->code != NoSymbol; t++)
680 string keyname(kb_key k) {
682 printKeysym(k.code, k.mod, buf, 100);
686 // Finds a key for a keyaction, if possible
687 string kb_keymap::findbinding(int act) const {
693 for(int i=0; i < KB_HASHSIZE; i++) {
695 for(kb_key *t = htable[i]; t->code != NoSymbol; t++) {
697 string suffix = t->table->findbinding(act);
698 suffix = strip(suffix, ' ');
699 suffix = strip(suffix, ']');
700 suffix = frontStrip(suffix, '[');
701 if (!suffix.empty()) {
702 res += "[" + keyname(*t) + " " + suffix + "] ";
704 } else if (t->action == act) {
705 res += "[" + keyname(*t) + "] ";
711 for(kb_key *t = table; t->code != NoSymbol; t++) {
713 string suffix = t->table->findbinding(act);
714 suffix = strip(suffix, ' ');
715 suffix = strip(suffix, ']');
716 suffix = frontStrip(suffix, '[');
717 if (!suffix.empty()) {
718 res += "[" + keyname(*t) + " " + suffix + "] ";
720 } else if (t->action == act) {
721 res += "[" + keyname(*t) + "] ";
729 /* === End of File: kbmap.C ============================================== */