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) {
223 << "kbmap.C: No such keysym: "
229 addkey(key, mod, nmod);
238 /* ---F+------------------------------------------------------------------ *\
239 Function : kb_sequence::print
241 Purpose : print the currently defined sequence into a string
242 Parameters: buf - string where the result goes
243 maxlen - length of string (including '\0')
244 when_defined - only print when sequence is real: length > 0.
245 Returns : 0, if ok, -1 if string too long
246 \* ---F------------------------------------------------------------------- */
248 int kb_sequence::print(char *buf, int maxlen, bool when_defined) const
254 if ( l<0 && !when_defined ) l = -l;
256 for(int i = 0; i < l; i++) {
258 mod = modifiers[i] & 0xffff;
260 len = printKeysym( key, mod, buf, maxlen ); // RVDK_PATCH_5
269 if(i+1<l && maxlen>1) { // append a blank
279 /* ---F+------------------------------------------------------------------ *\
280 Function : kb_sequence::printOptions
282 Purpose : print the available key options from the current state in the
283 sequence. RVDK_PATCH_5
284 Parameters: buf - string where the result goes
285 maxlen - length of string (including '\0')
286 Returns : 0, if ok, -1 if string too long
287 \* ---F------------------------------------------------------------------- */
289 int kb_sequence::printOptions(char *buf, int maxlen) const
293 print( buf, maxlen, true );
298 if ( maxlen < 20 || !curmap ) return -1;
300 #warning reimplement kb_sequence using string
303 strcpy(s,_(" options: "));
308 curmap->print(buf, maxlen);
313 /* ---F+------------------------------------------------------------------ *\
314 Function : kb_sequence::delseq
316 Purpose : mark the sequence as deleted
319 \* ---F------------------------------------------------------------------- */
321 void kb_sequence::delseq()
323 // negative length marks sequence as deleted, but we can still
324 // print() it or retrieve the last char using getiso()
329 /* ---F+------------------------------------------------------------------ *\
330 Function : kb_sequence::getsym
331 Called by : [user], getiso
332 Purpose : get the keysym of the last key in sequence
335 \* ---F------------------------------------------------------------------- */
337 KeySym kb_sequence::getsym()
340 if(l==0) return NoSymbol;
342 return sequence[l-1];
346 /* ---F+------------------------------------------------------------------ *\
347 Function : kb_sequence::getiso
349 Purpose : return iso character code of last key, if any
351 Returns : iso code or 0 if none
352 \* ---F------------------------------------------------------------------- */
354 char kb_sequence::getiso()
364 /* ---F+------------------------------------------------------------------ *\
365 Function : kb_sequence::reset
367 Purpose : reset sequence to initial state. RVDK_PATCH_5
370 \* ---F------------------------------------------------------------------- */
372 void kb_sequence::reset()
376 if ( length > 0 ) length = -length;
380 // === kb_keymap methods ==================================================
382 // This binds a key to an action
383 int kb_keymap::bind(char const *seq, int action)
387 int res = k.parse(seq);
391 lyxerr[Debug::KBMAP] << "Parse error at position " << res
392 << " in key sequence '" << seq << "'."
398 /* ---F+------------------------------------------------------------------ *\
399 Function : kb_keymap::lookup
400 Called by : [user], kb_sequence::add()
401 Purpose : look up a key press in a given keymap
402 Parameters: key - the keysym of the key press
403 mod - the modifier mask of the keypress
404 seq - the key-sequence retrieved so far
405 Returns : user defined action; 0 for prefix key, -1 if key not found
406 \* ---F------------------------------------------------------------------- */
408 int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence *seq)
410 unsigned int hashval, ksym, msk1, msk0;
413 //suppress modifier bits we do not handle
417 // error - no keymap defined:
418 seq->curmap = seq->stdmap;
423 if(size < 0) { // --- if hash table ---
424 hashval = ((key&0xff) ^ ((key>>8)&0xff)) % KB_HASHSIZE;
425 tab = htable[hashval];
427 seq->curmap = seq->stdmap;
431 } else // --- else: linear list ---
434 // --- now search the list of keys ---
436 for( ; (tab->code & 0xffff) != NoSymbol; tab++) {
438 msk1 = tab->mod & 0xffff;
439 msk0 = (tab->mod>>16) & 0xffff;
441 if(ksym == key && (mod&~msk0) == msk1) {
444 // this is a prefix key - set new map
445 seq->curmap = tab->table;
448 // final key - reset map
449 seq->curmap = seq->stdmap;
451 return tab->action; // ... and return action
456 // error - key not found:
457 seq->curmap = seq->stdmap;
463 /* ---F+------------------------------------------------------------------ *\
464 Function : kb_keymap::print
466 Purpose : Prints all the available keysyms. RVDK_PATCH_5
467 Parameters: buf - string where output goes.
468 maxLen - available length in string, including `\0'.
469 Returns : updated maxLen.
470 \* ---F------------------------------------------------------------------- */
472 int kb_keymap::print(char *buf, int maxLen) const
476 /* -----> Return when running out of string space or when keymap has no table.
477 Else, place a terminating newline in case no other output is generated. */
479 if ( maxLen <= 3 || !buf ) return maxLen;
480 if ( !table ) return maxLen;
483 /* -------> Hash table. Process each of its slots recursively and return. */
485 for ( int ix = 0; (ix < KB_HASHSIZE) && (maxLen > 1); ix++ ) {
487 len = printKeyTab( htable[ix], buf, maxLen );
493 /* -------> Normal table. */
494 len = printKeyTab( table, buf, maxLen );
502 /* ---F+------------------------------------------------------------------ *\
503 Function : kb_keymap::defkey
505 Purpose : define an action for a key sequence
506 Parameters: seq - the key sequence
507 action - the action to be defined
508 idx - recursion depth
510 \* ---F------------------------------------------------------------------- */
512 int kb_keymap::defkey(kb_sequence *seq, int action, int idx /*=0*/)
515 unsigned int code, modmsk;
518 code = seq->sequence[idx];
519 modmsk = seq->modifiers[idx];
520 if(code == NoSymbol) return -1;
522 // --- get list------------------------------------------------------
524 // If we don't have any yet, make an empty one
525 table = new kb_key[KB_PREALLOC];
526 table[0].code = NoSymbol;
532 int hashval = (code&0xffff);
533 hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
534 tab = htable[hashval];
535 ptab = htable+hashval;
537 tab = new kb_key[KB_PREALLOC];
538 tab[0].code = NoSymbol;
546 // --- check if key is already there --------------------------------
549 for(t = tab, tsize=1; t->code != NoSymbol; t++, tsize++) {
550 if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
551 if(idx+1 == seq->length) {
552 char buf[20]; buf[0] = 0;
553 seq->print(buf, 20, true);
555 << "Warning: New binding for '"
557 << "' is overriding old binding..."
566 } else if (!t->table) {
567 char buf[20]; buf[0] = 0;
568 seq->print(buf, 20, true);
569 lyxerr << "Error: New binding for '" << buf
570 << "' is overriding old binding..."
574 return t->table->defkey(seq, action, idx+1);
578 // --- extend list if necessary -------------------------------------
580 if(tsize % KB_PREALLOC == 0) {
581 kb_key *nt = new kb_key[tsize+KB_PREALLOC];
582 // Set to 0 as table is used uninitialised later (thornley)
584 memcpy(nt, tab, tsize*sizeof(kb_key));
588 if(size>=0) size = tsize+KB_PREALLOC;
591 // --- add action ---------------------------------------------------
593 tab[tsize--].code = NoSymbol;
594 tab[tsize].code = code;
595 tab[tsize].mod = modmsk;
596 kb_key *newone = &tab[tsize];
598 // --- convert list to hash table if necessary ----------------------
600 if(size>=0 && tsize>=32) {
601 kb_key *oldtab = tab;
602 kb_key **nht = new kb_key*[KB_HASHSIZE];
603 for(int i = 0; i < KB_HASHSIZE; i++)
608 // --- copy old keys to new hash table ---
610 for(kb_key *tu = oldtab; tu->code != NoSymbol; tu++){
611 // copy values from oldtab to htable
612 hashval = (tu->code&0xffff);
613 hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
614 tab = htable[hashval];
617 htable[hashval] = tab = new kb_key[KB_PREALLOC];
618 tab->code = NoSymbol;
621 for(kb_key *tt = tab; tt->code != NoSymbol; tt++)
623 if(ts % KB_PREALLOC == 0){
625 kb_key *nt = new kb_key[ts+KB_PREALLOC];
626 memcpy(nt, tab, ts*sizeof(kb_key));
627 htable[hashval] = nt;
631 tab[ts--].code = NoSymbol;
632 tab[ts].code = tu->code;
633 tab[ts].mod = tu->mod;
634 tab[ts].action = tu->action;
635 tab[ts].table = tu->table;
643 // --- define rest of sequence --------------------------------------
645 if(idx+1 == seq->length) {
646 newone->action = action;
650 newone->table = new kb_keymap;
651 int res = newone->table->defkey(seq, action, idx+1);
657 /* ---F+------------------------------------------------------------------ *\
658 Function : kb_keymap::~kb_keymap
659 Called by : [destructor]
660 Purpose : free keymap and its descendents
663 \* ---F------------------------------------------------------------------- */
665 kb_keymap::~kb_keymap()
669 for(int i=0; i < KB_HASHSIZE; i++) {
671 for(kb_key *t = htable[i]; t->code != NoSymbol; t++)
679 for(kb_key *t = table; t->code != NoSymbol; t++)
686 string keyname(kb_key k) {
688 printKeysym(k.code, k.mod, buf, 100);
692 // Finds a key for a keyaction, if possible
693 string kb_keymap::findbinding(int act) const {
699 for(int i=0; i < KB_HASHSIZE; i++) {
701 for(kb_key *t = htable[i]; t->code != NoSymbol; t++) {
703 string suffix = t->table->findbinding(act);
704 suffix = strip(suffix, ' ');
705 suffix = strip(suffix, ']');
706 suffix = frontStrip(suffix, '[');
707 if (!suffix.empty()) {
708 res += "[" + keyname(*t) + " " + suffix + "] ";
710 } else if (t->action == act) {
711 res += "[" + keyname(*t) + "] ";
717 for(kb_key *t = table; t->code != NoSymbol; t++) {
719 string suffix = t->table->findbinding(act);
720 suffix = strip(suffix, ' ');
721 suffix = strip(suffix, ']');
722 suffix = frontStrip(suffix, '[');
723 if (!suffix.empty()) {
724 res += "[" + keyname(*t) + " " + suffix + "] ";
726 } else if (t->action == act) {
727 res += "[" + keyname(*t) + "] ";
735 /* === End of File: kbmap.C ============================================== */