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 * ====================================================== */
15 #include "support/lstrings.h"
19 #pragma implementation
25 // The only modifiers that we handle. We want to throw away things
27 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask};
30 // === static functions ===================================================
33 /* ---F+------------------------------------------------------------------ *\
34 Function : printKeysym
35 Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
36 Purpose : prints a keysym, including modifiers.
37 Parameters: key - keysym
39 buf - string where the result goes
40 maxlen - length of string (including '\0')
41 Returns : length of printed string if ok, 0 otherwise.
42 \* ---F------------------------------------------------------------------- */
44 void printKeysym(unsigned long key, unsigned int mod, string & buf)
48 char * s = XKeysymToString(key);
50 if (mod & ShiftMask) buf += "S-";
51 if (mod & ControlMask) buf += "C-";
52 if (mod & Mod1Mask) buf += "M-";
57 /* ---F+------------------------------------------------------------------ *\
58 Function : printKeyTab
59 Called by : kb_keymap::print
60 Purpose : print the keysyms found in the given key table. RVDK_PATCH_5
61 Parameters: tabPt - keytable pointer
62 buf - string where the result goes
63 maxLen - length of string (including '\0')
64 Returns : length of printed string.
65 \* ---F------------------------------------------------------------------- */
68 void printKeyTab(kb_key * tabPt, string & buf)
70 unsigned int ksym, mod;
72 /* -------> Print each of the slots into buf. */
73 for( ; (tabPt->code & 0xffff) != NoSymbol; ++tabPt) {
75 mod = tabPt->mod & 0xffff;
77 printKeysym(ksym, mod, buf);
83 // === kb_sequence methods ================================================
85 /* ---F+------------------------------------------------------------------ *\
86 Function : kb_sequence::addkey
88 Purpose : add a key to the sequence, look up in map and return action
89 Parameters: key - keysym of key
91 nmod - modifier veto mask (unused now)
92 Returns : action or -1 if error (no map defined or key not found)
93 \* ---F------------------------------------------------------------------- */
95 int kb_sequence::addkey(unsigned long key,
96 unsigned int mod, unsigned int nmod /*= 0*/)
98 if(length < 0) length = 0;
100 if(length + 1 >= size) {
101 unsigned int * nseq = new unsigned int[size + KB_PREALLOC];
103 memcpy(nseq, sequence, length * sizeof(unsigned int));
104 if(sequence != staticseq) delete sequence;
106 nseq = new unsigned int[size];
107 memcpy(nseq, modifiers, length * sizeof(unsigned int));
108 if(modifiers != staticmod) delete modifiers;
112 modifiers[length] = mod + (nmod << 16);
113 sequence[length++] = key;
116 return curmap->lookup(key, mod, this);
122 /* ---F+------------------------------------------------------------------ *\
123 Function : kb_sequence::parse
125 Purpose : parse a string that holds a key sequence and add the keys
126 Parameters: s - string holding the key sequence
127 Returns : 0 - if ok, error pos if error
128 Note : Keys must be separated with whitespace;
129 Use the keysym names used by XStringToKeysym
130 Prefixes are S-, C-, M- for shift, control, meta
131 \* ---F------------------------------------------------------------------- */
133 int kb_sequence::parse(char const * s)
138 unsigned int mod = 0, nmod = 0;
140 if(s[i] && (s[i]) <= ' ') ++i;
143 if(s[i + 1] == '-') { // is implicit that s[i] == true
160 } else if(s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
180 for(; s[j] && s[j] > ' '; ++j)
181 tbuf += s[j]; // (!!!check bounds :-)
183 KeySym key = XStringToKeysym(tbuf.c_str());
184 if(key == NoSymbol) {
186 << "kbmap.C: No such keysym: "
192 addkey(key, mod, nmod);
201 /* ---F+------------------------------------------------------------------ *\
202 Function : kb_sequence::print
204 Purpose : print the currently defined sequence into a string
205 Parameters: buf - string where the result goes
206 maxlen - length of string (including '\0')
207 when_defined - only print when sequence is real: length > 0.
208 Returns : 0, if ok, -1 if string too long
209 \* ---F------------------------------------------------------------------- */
211 int kb_sequence::print(string & buf, bool when_defined) const
216 if ( l < 0 && !when_defined ) l = -l;
218 for(int i = 0; i < l; ++i) {
220 mod = modifiers[i] & 0xffff;
222 printKeysym(key, mod, buf); // RVDK_PATCH_5
224 if(i + 1 < l) { // append a blank
232 /* ---F+------------------------------------------------------------------ *\
233 Function : kb_sequence::printOptions
235 Purpose : print the available key options from the current state in the
236 sequence. RVDK_PATCH_5
237 Parameters: buf - string where the result goes
238 maxlen - length of string (including '\0')
239 Returns : 0, if ok, -1 if string too long
240 \* ---F------------------------------------------------------------------- */
242 int kb_sequence::printOptions(string & buf) const
246 if (!curmap) return -1;
247 buf += _(" options: ");
253 /* ---F+------------------------------------------------------------------ *\
254 Function : kb_sequence::delseq
256 Purpose : mark the sequence as deleted
259 \* ---F------------------------------------------------------------------- */
261 void kb_sequence::delseq()
263 // negative length marks sequence as deleted, but we can still
264 // print() it or retrieve the last char using getiso()
269 /* ---F+------------------------------------------------------------------ *\
270 Function : kb_sequence::getsym
271 Called by : [user], getiso
272 Purpose : get the keysym of the last key in sequence
275 \* ---F------------------------------------------------------------------- */
277 KeySym kb_sequence::getsym()
280 if(l == 0) return NoSymbol;
282 return sequence[l - 1];
286 /* ---F+------------------------------------------------------------------ *\
287 Function : kb_sequence::getiso
289 Purpose : return iso character code of last key, if any
291 Returns : iso code or 0 if none
292 \* ---F------------------------------------------------------------------- */
294 char kb_sequence::getiso()
304 /* ---F+------------------------------------------------------------------ *\
305 Function : kb_sequence::reset
307 Purpose : reset sequence to initial state. RVDK_PATCH_5
310 \* ---F------------------------------------------------------------------- */
312 void kb_sequence::reset()
316 if (length > 0) length = -length;
320 // === kb_keymap methods ==================================================
322 // This binds a key to an action
323 int kb_keymap::bind(char const * seq, int action)
327 int res = k.parse(seq);
331 lyxerr[Debug::KBMAP] << "Parse error at position " << res
332 << " in key sequence '" << seq << "'."
338 /* ---F+------------------------------------------------------------------ *\
339 Function : kb_keymap::lookup
340 Called by : [user], kb_sequence::add()
341 Purpose : look up a key press in a given keymap
342 Parameters: key - the keysym of the key press
343 mod - the modifier mask of the keypress
344 seq - the key-sequence retrieved so far
345 Returns : user defined action; 0 for prefix key, -1 if key not found
346 \* ---F------------------------------------------------------------------- */
348 int kb_keymap::lookup(unsigned long key, unsigned int mod, kb_sequence * seq)
350 unsigned int ksym, msk1, msk0;
353 //suppress modifier bits we do not handle
357 // error - no keymap defined:
358 seq->curmap = seq->stdmap;
365 // --- now search the list of keys ---
367 for(; (tab->code & 0xffff) != NoSymbol; ++tab) {
369 msk1 = tab->mod & 0xffff;
370 msk0 = (tab->mod >> 16) & 0xffff;
372 if(ksym == key && (mod & ~msk0) == msk1) {
375 // this is a prefix key - set new map
376 seq->curmap = tab->table;
379 // final key - reset map
380 seq->curmap = seq->stdmap;
382 return tab->action; // ... and return action
387 // error - key not found:
388 seq->curmap = seq->stdmap;
394 /* ---F+------------------------------------------------------------------ *\
395 Function : kb_keymap::print
397 Purpose : Prints all the available keysyms. RVDK_PATCH_5
398 Parameters: buf - string where output goes.
399 maxLen - available length in string, including `\0'.
400 Returns : updated maxLen.
401 \* ---F------------------------------------------------------------------- */
403 void kb_keymap::print(string & buf) const
405 // Return when keymap has no table.
408 // Process each of its slots recursively and return.
409 printKeyTab(table, buf);
413 /* ---F+------------------------------------------------------------------ *\
414 Function : kb_keymap::defkey
416 Purpose : define an action for a key sequence
417 Parameters: seq - the key sequence
418 action - the action to be defined
419 idx - recursion depth
421 \* ---F------------------------------------------------------------------- */
423 int kb_keymap::defkey(kb_sequence * seq, int action, int idx /*= 0*/)
425 unsigned int code = seq->sequence[idx];
426 if(code == NoSymbol) return -1;
428 unsigned int modmsk = seq->modifiers[idx];
429 kb_key * tab, ** ptab;
430 // --- get list------------------------------------------------------
432 // If we don't have any yet, make an empty one
433 table = new kb_key[KB_PREALLOC];
434 table[0].code = NoSymbol;
443 // --- check if key is already there --------------------------------
447 for(t = tab, tsize = 1; t->code != NoSymbol; ++t, ++tsize) {
448 if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
449 if(idx + 1 == seq->length) {
451 seq->print(buf, true);
453 << "Warning: New binding for '"
455 << "' is overriding old binding..."
464 } else if (!t->table) {
466 seq->print(buf, true);
467 lyxerr << "Error: New binding for '" << buf
468 << "' is overriding old binding..."
472 return t->table->defkey(seq, action, idx + 1);
476 // --- extend list if necessary -------------------------------------
478 if(tsize % KB_PREALLOC == 0) {
479 kb_key * nt = new kb_key[tsize + KB_PREALLOC];
480 // Set to 0 as table is used uninitialised later (thornley)
482 memcpy(nt, tab, tsize * sizeof(kb_key));
486 if(size >= 0) size = tsize + KB_PREALLOC;
489 // --- add action ---------------------------------------------------
491 tab[tsize--].code = NoSymbol;
492 tab[tsize].code = code;
493 tab[tsize].mod = modmsk;
494 kb_key * newone = &tab[tsize];
496 // --- define rest of sequence --------------------------------------
498 if(idx + 1 == seq->length) {
499 newone->action = action;
503 newone->table = new kb_keymap;
504 int res = newone->table->defkey(seq, action, idx + 1);
510 /* ---F+------------------------------------------------------------------ *\
511 Function : kb_keymap::~kb_keymap
512 Called by : [destructor]
513 Purpose : free keymap and its descendents
516 \* ---F------------------------------------------------------------------- */
518 kb_keymap::~kb_keymap()
521 for(kb_key * t = table; t->code != NoSymbol; ++t)
528 string keyname(kb_key k)
531 printKeysym(k.code, k.mod, buf);
536 // Finds a key for a keyaction, if possible
537 string kb_keymap::findbinding(int act) const
540 if (!table) return res;
542 for(kb_key * t = table; t->code != NoSymbol; ++t) {
544 string suffix = t->table->findbinding(act);
545 suffix = strip(suffix, ' ');
546 suffix = strip(suffix, ']');
547 suffix = frontStrip(suffix, '[');
548 if (!suffix.empty()) {
549 res += "[" + keyname(*t) + " " + suffix + "] ";
551 } else if (t->action == act) {
552 res += "[" + keyname(*t) + "] ";
559 /* === End of File: kbmap.C ============================================== */