1999-09-27 18:44:28 +00:00
|
|
|
/* This file is part of
|
1999-11-15 12:01:38 +00:00
|
|
|
* ======================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
1999-12-13 00:05:34 +00:00
|
|
|
* LyX, The Document Processor
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
1999-12-13 00:05:34 +00:00
|
|
|
* Copyright 1995 Matthias Ettrich
|
|
|
|
* Copyright 1995-1999 The LyX Team.
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include "support/lstrings.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "gettext.h"
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "kbmap.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// The only modifiers that we handle. We want to throw away things
|
|
|
|
// like NumLock.
|
|
|
|
enum { ModsMask = ShiftMask | ControlMask | Mod1Mask};
|
|
|
|
|
|
|
|
|
1999-11-15 12:01:38 +00:00
|
|
|
// === static functions ===================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : printKeysym
|
|
|
|
Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
|
|
|
|
Purpose : prints a keysym, including modifiers.
|
|
|
|
Parameters: key - keysym
|
|
|
|
mod - modifiers
|
|
|
|
buf - string where the result goes
|
|
|
|
maxlen - length of string (including '\0')
|
|
|
|
Returns : length of printed string if ok, 0 otherwise.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
static
|
1999-12-21 06:10:21 +00:00
|
|
|
void printKeysym(KeySym key, unsigned int mod, string & buf)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
mod &= ModsMask;
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
char * s = XKeysymToString(key);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
if (mod & ShiftMask) buf += "S-";
|
|
|
|
if (mod & ControlMask) buf += "C-";
|
|
|
|
if (mod & Mod1Mask) buf += "M-";
|
|
|
|
if (s) buf += s;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : printKeyTab
|
|
|
|
Called by : kb_keymap::print
|
|
|
|
Purpose : print the keysyms found in the given key table. RVDK_PATCH_5
|
|
|
|
Parameters: tabPt - keytable pointer
|
|
|
|
buf - string where the result goes
|
|
|
|
maxLen - length of string (including '\0')
|
|
|
|
Returns : length of printed string.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static
|
1999-12-21 06:10:21 +00:00
|
|
|
void printKeyTab(kb_key * tabPt, string & buf)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
unsigned int ksym, mod;
|
|
|
|
|
|
|
|
/* -------> Print each of the slots into buf. */
|
1999-12-13 21:59:26 +00:00
|
|
|
for( ; (tabPt->code & 0xffff) != NoSymbol; ++tabPt) {
|
1999-09-27 18:44:28 +00:00
|
|
|
ksym = tabPt->code;
|
|
|
|
mod = tabPt->mod & 0xffff;
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
printKeysym(ksym, mod, buf);
|
|
|
|
buf += ' ';
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-15 12:01:38 +00:00
|
|
|
// === kb_sequence methods ================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::addkey
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : add a key to the sequence, look up in map and return action
|
|
|
|
Parameters: key - keysym of key
|
|
|
|
mod - modifier mask
|
|
|
|
nmod - modifier veto mask (unused now)
|
|
|
|
Returns : action or -1 if error (no map defined or key not found)
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
int kb_sequence::addkey(KeySym key,
|
|
|
|
unsigned int mod, unsigned int nmod /*= 0*/)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-13 21:59:26 +00:00
|
|
|
if(length < 0) length = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
if(length + 1 >= size) {
|
1999-12-16 06:43:25 +00:00
|
|
|
unsigned int * nseq = new unsigned int[size + KB_PREALLOC];
|
1999-09-27 18:44:28 +00:00
|
|
|
size += KB_PREALLOC;
|
1999-12-13 21:59:26 +00:00
|
|
|
memcpy(nseq, sequence, length * sizeof(unsigned int));
|
1999-09-27 18:44:28 +00:00
|
|
|
if(sequence != staticseq) delete sequence;
|
|
|
|
sequence = nseq;
|
|
|
|
nseq = new unsigned int[size];
|
1999-12-13 21:59:26 +00:00
|
|
|
memcpy(nseq, modifiers, length * sizeof(unsigned int));
|
1999-09-27 18:44:28 +00:00
|
|
|
if(modifiers != staticmod) delete modifiers;
|
|
|
|
modifiers = nseq;
|
|
|
|
}
|
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
modifiers[length] = mod + (nmod << 16);
|
1999-09-27 18:44:28 +00:00
|
|
|
sequence[length++] = key;
|
|
|
|
|
|
|
|
if(curmap)
|
|
|
|
return curmap->lookup(key, mod, this);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::parse
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : parse a string that holds a key sequence and add the keys
|
|
|
|
Parameters: s - string holding the key sequence
|
|
|
|
Returns : 0 - if ok, error pos if error
|
|
|
|
Note : Keys must be separated with whitespace;
|
|
|
|
Use the keysym names used by XStringToKeysym
|
|
|
|
Prefixes are S-, C-, M- for shift, control, meta
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
int kb_sequence::parse(char const * s)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-21 06:10:21 +00:00
|
|
|
if(!s[0]) return 1;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
int i = 0;
|
|
|
|
unsigned int mod = 0, nmod = 0;
|
|
|
|
while(s[i]) {
|
1999-12-13 21:59:26 +00:00
|
|
|
if(s[i] && (s[i]) <= ' ') ++i;
|
1999-09-27 18:44:28 +00:00
|
|
|
if(!s[i]) break;
|
|
|
|
|
1999-12-21 06:10:21 +00:00
|
|
|
if(s[i + 1] == '-') { // is implicit that s[i] == true
|
1999-09-27 18:44:28 +00:00
|
|
|
switch(s[i]) {
|
|
|
|
case 's': case 'S':
|
|
|
|
mod |= ShiftMask;
|
1999-12-21 06:10:21 +00:00
|
|
|
i += 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
|
|
|
case 'c': case 'C':
|
|
|
|
mod |= ControlMask;
|
1999-12-21 06:10:21 +00:00
|
|
|
i += 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
|
|
|
case 'm': case 'M':
|
|
|
|
mod |= Mod1Mask;
|
1999-12-21 06:10:21 +00:00
|
|
|
i += 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
|
|
|
default:
|
1999-12-21 06:10:21 +00:00
|
|
|
return i + 1;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-12-21 06:10:21 +00:00
|
|
|
} else if(s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
|
|
|
|
switch(s[i + 1]) {
|
1999-09-27 18:44:28 +00:00
|
|
|
case 's': case 'S':
|
|
|
|
nmod |= ShiftMask;
|
1999-12-21 06:10:21 +00:00
|
|
|
i += 3;
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
|
|
|
case 'c': case 'C':
|
|
|
|
nmod |= ControlMask;
|
1999-12-21 06:10:21 +00:00
|
|
|
i += 3;
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
|
|
|
case 'm': case 'M':
|
|
|
|
nmod |= Mod1Mask;
|
1999-12-21 06:10:21 +00:00
|
|
|
i += 3;
|
1999-09-27 18:44:28 +00:00
|
|
|
continue;
|
|
|
|
default:
|
1999-12-21 06:10:21 +00:00
|
|
|
return i + 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
1999-12-21 06:10:21 +00:00
|
|
|
string tbuf;
|
|
|
|
int j = i;
|
|
|
|
for(; s[j] && s[j] > ' '; ++j)
|
|
|
|
tbuf += s[j]; // (!!!check bounds :-)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-21 06:10:21 +00:00
|
|
|
KeySym key = XStringToKeysym(tbuf.c_str());
|
1999-09-27 18:44:28 +00:00
|
|
|
if(key == NoSymbol) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::KBMAP]
|
|
|
|
<< "kbmap.C: No such keysym: "
|
|
|
|
<< tbuf << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return j;
|
|
|
|
}
|
|
|
|
i = j;
|
|
|
|
|
|
|
|
addkey(key, mod, nmod);
|
|
|
|
mod = 0;
|
|
|
|
nmod = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::print
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : print the currently defined sequence into a string
|
|
|
|
Parameters: buf - string where the result goes
|
|
|
|
maxlen - length of string (including '\0')
|
|
|
|
when_defined - only print when sequence is real: length > 0.
|
|
|
|
Returns : 0, if ok, -1 if string too long
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
int kb_sequence::print(string & buf, bool when_defined) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
KeySym key;
|
|
|
|
unsigned int mod;
|
|
|
|
int l = length;
|
1999-12-13 21:59:26 +00:00
|
|
|
if ( l < 0 && !when_defined ) l = -l;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
for(int i = 0; i < l; ++i) {
|
1999-09-27 18:44:28 +00:00
|
|
|
key = sequence[i];
|
|
|
|
mod = modifiers[i] & 0xffff;
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
printKeysym(key, mod, buf); // RVDK_PATCH_5
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
if(i + 1 < l) { // append a blank
|
|
|
|
buf += ' ';
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::printOptions
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : print the available key options from the current state in the
|
|
|
|
sequence. RVDK_PATCH_5
|
|
|
|
Parameters: buf - string where the result goes
|
|
|
|
maxlen - length of string (including '\0')
|
|
|
|
Returns : 0, if ok, -1 if string too long
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
int kb_sequence::printOptions(string & buf) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-16 06:43:25 +00:00
|
|
|
print(buf, true);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
if (!curmap) return -1;
|
|
|
|
buf += _(" options: ");
|
|
|
|
curmap->print(buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::delseq
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : mark the sequence as deleted
|
|
|
|
Parameters: none
|
|
|
|
Returns : nothing
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void kb_sequence::delseq()
|
|
|
|
{
|
|
|
|
// negative length marks sequence as deleted, but we can still
|
|
|
|
// print() it or retrieve the last char using getiso()
|
|
|
|
length = -length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::getsym
|
|
|
|
Called by : [user], getiso
|
|
|
|
Purpose : get the keysym of the last key in sequence
|
|
|
|
Parameters: none
|
|
|
|
Returns : keysym
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
KeySym kb_sequence::getsym()
|
|
|
|
{
|
|
|
|
int l = length;
|
1999-11-15 12:01:38 +00:00
|
|
|
if(l == 0) return NoSymbol;
|
1999-12-13 21:59:26 +00:00
|
|
|
if(l < 0) l = -l;
|
1999-12-21 06:10:21 +00:00
|
|
|
return sequence[l - 1];
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::getiso
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : return iso character code of last key, if any
|
|
|
|
Parameters: none
|
|
|
|
Returns : iso code or 0 if none
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
char kb_sequence::getiso()
|
|
|
|
{
|
|
|
|
int c = getsym();
|
|
|
|
|
|
|
|
if(c > 0xff)
|
|
|
|
return '\0';
|
1999-12-13 00:05:34 +00:00
|
|
|
return c;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_sequence::reset
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : reset sequence to initial state. RVDK_PATCH_5
|
|
|
|
Parameters: none
|
|
|
|
Returns : void
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void kb_sequence::reset()
|
|
|
|
{
|
|
|
|
delseq();
|
|
|
|
curmap = stdmap;
|
1999-12-21 06:10:21 +00:00
|
|
|
if (length > 0) length = -length;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-15 12:01:38 +00:00
|
|
|
// === kb_keymap methods ==================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// This binds a key to an action
|
1999-12-13 21:59:26 +00:00
|
|
|
int kb_keymap::bind(char const * seq, int action)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
kb_sequence k;
|
|
|
|
|
|
|
|
int res = k.parse(seq);
|
|
|
|
if (!res) {
|
|
|
|
defkey(&k, action);
|
|
|
|
} else
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::KBMAP] << "Parse error at position " << res
|
|
|
|
<< " in key sequence '" << seq << "'."
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_keymap::lookup
|
|
|
|
Called by : [user], kb_sequence::add()
|
|
|
|
Purpose : look up a key press in a given keymap
|
|
|
|
Parameters: key - the keysym of the key press
|
|
|
|
mod - the modifier mask of the keypress
|
|
|
|
seq - the key-sequence retrieved so far
|
|
|
|
Returns : user defined action; 0 for prefix key, -1 if key not found
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence * seq)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
|
|
|
unsigned int hashval;
|
|
|
|
#endif
|
|
|
|
unsigned int ksym, msk1, msk0;
|
1999-12-13 21:59:26 +00:00
|
|
|
kb_key * tab;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
//suppress modifier bits we do not handle
|
|
|
|
mod &= ModsMask;
|
|
|
|
|
|
|
|
if(!table) {
|
|
|
|
// error - no keymap defined:
|
|
|
|
seq->curmap = seq->stdmap;
|
|
|
|
seq->delseq();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
1999-09-27 18:44:28 +00:00
|
|
|
if(size < 0) { // --- if hash table ---
|
1999-12-21 06:10:21 +00:00
|
|
|
hashval = ((key & 0xff) ^ ((key >> 8) & 0xff)) % KB_HASHSIZE;
|
1999-09-27 18:44:28 +00:00
|
|
|
tab = htable[hashval];
|
|
|
|
if(!tab) {
|
|
|
|
seq->curmap = seq->stdmap;
|
|
|
|
seq->delseq();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else // --- else: linear list ---
|
1999-12-16 06:43:25 +00:00
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
tab = table;
|
|
|
|
|
|
|
|
// --- now search the list of keys ---
|
|
|
|
|
1999-12-21 06:10:21 +00:00
|
|
|
for(; (tab->code & 0xffff) != NoSymbol; ++tab) {
|
1999-09-27 18:44:28 +00:00
|
|
|
ksym = tab->code;
|
|
|
|
msk1 = tab->mod & 0xffff;
|
1999-12-13 21:59:26 +00:00
|
|
|
msk0 = (tab->mod >> 16) & 0xffff;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
if(ksym == key && (mod & ~msk0) == msk1) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// match found:
|
|
|
|
if(tab->table) {
|
1999-12-16 06:43:25 +00:00
|
|
|
// this is a prefix key - set new map
|
1999-09-27 18:44:28 +00:00
|
|
|
seq->curmap = tab->table;
|
|
|
|
return 0;
|
|
|
|
} else {
|
1999-12-16 06:43:25 +00:00
|
|
|
// final key - reset map
|
1999-09-27 18:44:28 +00:00
|
|
|
seq->curmap = seq->stdmap;
|
|
|
|
seq->delseq();
|
|
|
|
return tab->action; // ... and return action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// error - key not found:
|
|
|
|
seq->curmap = seq->stdmap;
|
|
|
|
seq->delseq();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_keymap::print
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : Prints all the available keysyms. RVDK_PATCH_5
|
|
|
|
Parameters: buf - string where output goes.
|
|
|
|
maxLen - available length in string, including `\0'.
|
|
|
|
Returns : updated maxLen.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
void kb_keymap::print(string & buf) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-16 06:43:25 +00:00
|
|
|
// Return when keymap has no table.
|
|
|
|
if (!table) return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
// Process each of its slots recursively and return.
|
|
|
|
#ifndef NO_HASH
|
|
|
|
if ( size < 0 ) { // Hash table
|
|
|
|
for ( int ix = 0; ix < KB_HASHSIZE; ++ix ) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if ( htable[ix] ) {
|
1999-12-16 06:43:25 +00:00
|
|
|
printKeyTab(htable[ix], buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
1999-12-16 06:43:25 +00:00
|
|
|
} else // Normal table
|
|
|
|
#endif
|
|
|
|
printKeyTab(table, buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_keymap::defkey
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : define an action for a key sequence
|
|
|
|
Parameters: seq - the key sequence
|
|
|
|
action - the action to be defined
|
|
|
|
idx - recursion depth
|
|
|
|
Returns : 0 if ok.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
int kb_keymap::defkey(kb_sequence * seq, int action, int idx /*= 0*/)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-13 21:59:26 +00:00
|
|
|
unsigned int code = seq->sequence[idx];
|
1999-09-27 18:44:28 +00:00
|
|
|
if(code == NoSymbol) return -1;
|
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
unsigned int modmsk = seq->modifiers[idx];
|
1999-12-16 06:43:25 +00:00
|
|
|
kb_key * tab, ** ptab;
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- get list------------------------------------------------------
|
|
|
|
if(!table) {
|
|
|
|
// If we don't have any yet, make an empty one
|
|
|
|
table = new kb_key[KB_PREALLOC];
|
|
|
|
table[0].code = NoSymbol;
|
|
|
|
tab = table;
|
|
|
|
ptab = &table;
|
|
|
|
size = KB_PREALLOC;
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
|
|
|
} else if(size < 0) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// Hash table.
|
1999-12-16 06:43:25 +00:00
|
|
|
int hashval = code & 0xffff;
|
|
|
|
hashval = ((hashval & 0xff) ^ ((hashval >> 8) & 0xff)) % KB_HASHSIZE;
|
1999-09-27 18:44:28 +00:00
|
|
|
tab = htable[hashval];
|
|
|
|
ptab = htable+hashval;
|
|
|
|
if(!tab) {
|
|
|
|
tab = new kb_key[KB_PREALLOC];
|
|
|
|
tab[0].code = NoSymbol;
|
|
|
|
*ptab = tab;
|
|
|
|
}
|
1999-12-16 06:43:25 +00:00
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
|
|
|
tab = table;
|
|
|
|
ptab = &table;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- check if key is already there --------------------------------
|
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
kb_key * t;
|
|
|
|
int tsize;
|
|
|
|
for(t = tab, tsize = 1; t->code != NoSymbol; ++t, ++tsize) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
|
1999-12-21 06:10:21 +00:00
|
|
|
if(idx + 1 == seq->length) {
|
1999-12-16 06:43:25 +00:00
|
|
|
string buf;
|
|
|
|
seq->print(buf, true);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::KEY]
|
|
|
|
<< "Warning: New binding for '"
|
|
|
|
<< buf
|
|
|
|
<< "' is overriding old binding..."
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if(t->table) {
|
|
|
|
delete t->table;
|
|
|
|
t->table = 0;
|
|
|
|
}
|
|
|
|
t->action = action;
|
|
|
|
return 0;
|
|
|
|
} else if (!t->table) {
|
1999-12-16 06:43:25 +00:00
|
|
|
string buf;
|
|
|
|
seq->print(buf, true);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "Error: New binding for '" << buf
|
|
|
|
<< "' is overriding old binding..."
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return -1;
|
|
|
|
} else
|
1999-12-16 06:43:25 +00:00
|
|
|
return t->table->defkey(seq, action, idx + 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- extend list if necessary -------------------------------------
|
|
|
|
|
|
|
|
if(tsize % KB_PREALLOC == 0) {
|
1999-12-16 06:43:25 +00:00
|
|
|
kb_key * nt = new kb_key[tsize + KB_PREALLOC];
|
1999-10-02 16:21:10 +00:00
|
|
|
// Set to 0 as table is used uninitialised later (thornley)
|
|
|
|
nt[tsize].table = 0;
|
1999-12-13 21:59:26 +00:00
|
|
|
memcpy(nt, tab, tsize * sizeof(kb_key));
|
1999-09-27 18:44:28 +00:00
|
|
|
*ptab = nt;
|
|
|
|
delete[] tab;
|
|
|
|
tab = nt;
|
1999-12-16 06:43:25 +00:00
|
|
|
if(size >= 0) size = tsize + KB_PREALLOC;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- add action ---------------------------------------------------
|
|
|
|
|
|
|
|
tab[tsize--].code = NoSymbol;
|
|
|
|
tab[tsize].code = code;
|
|
|
|
tab[tsize].mod = modmsk;
|
1999-12-13 21:59:26 +00:00
|
|
|
kb_key * newone = &tab[tsize];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// --- convert list to hash table if necessary ----------------------
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
1999-12-13 21:59:26 +00:00
|
|
|
if(size >= 0 && tsize >= 32) {
|
|
|
|
kb_key * oldtab = tab;
|
|
|
|
kb_key ** nht = new kb_key*[KB_HASHSIZE];
|
|
|
|
for(int i = 0; i < KB_HASHSIZE; ++i)
|
1999-09-27 18:44:28 +00:00
|
|
|
nht[i] = 0;
|
|
|
|
htable = nht;
|
|
|
|
size = -KB_HASHSIZE;
|
|
|
|
|
|
|
|
// --- copy old keys to new hash table ---
|
|
|
|
int hashval;
|
1999-12-13 21:59:26 +00:00
|
|
|
for(kb_key * tu = oldtab; tu->code != NoSymbol; ++tu) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// copy values from oldtab to htable
|
1999-12-13 21:59:26 +00:00
|
|
|
hashval = (tu->code & 0xffff);
|
|
|
|
hashval = ((hashval & 0xff) ^ ((hashval>>8) & 0xff)) % KB_HASHSIZE;
|
1999-09-27 18:44:28 +00:00
|
|
|
tab = htable[hashval];
|
|
|
|
|
|
|
|
if(!tab){
|
|
|
|
htable[hashval] = tab = new kb_key[KB_PREALLOC];
|
|
|
|
tab->code = NoSymbol;
|
|
|
|
}
|
|
|
|
int ts = 1;
|
1999-12-13 21:59:26 +00:00
|
|
|
for(kb_key * tt = tab; tt->code != NoSymbol; ++tt)
|
|
|
|
++ts;
|
1999-09-27 18:44:28 +00:00
|
|
|
if(ts % KB_PREALLOC == 0){
|
|
|
|
// extend table
|
1999-12-13 21:59:26 +00:00
|
|
|
kb_key * nt = new kb_key[ts+KB_PREALLOC];
|
|
|
|
memcpy(nt, tab, ts * sizeof(kb_key));
|
1999-09-27 18:44:28 +00:00
|
|
|
htable[hashval] = nt;
|
|
|
|
delete[] tab;
|
|
|
|
tab = nt;
|
|
|
|
}
|
|
|
|
tab[ts--].code = NoSymbol;
|
|
|
|
tab[ts].code = tu->code;
|
|
|
|
tab[ts].mod = tu->mod;
|
|
|
|
tab[ts].action = tu->action;
|
|
|
|
tab[ts].table = tu->table;
|
|
|
|
|
|
|
|
if(tu == newone)
|
|
|
|
newone = &tab[ts];
|
|
|
|
}
|
|
|
|
delete[] oldtab;
|
|
|
|
}
|
1999-12-16 06:43:25 +00:00
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
// --- define rest of sequence --------------------------------------
|
|
|
|
|
|
|
|
if(idx+1 == seq->length) {
|
|
|
|
newone->action = action;
|
|
|
|
newone->table = 0;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
newone->table = new kb_keymap;
|
|
|
|
int res = newone->table->defkey(seq, action, idx+1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_keymap::~kb_keymap
|
|
|
|
Called by : [destructor]
|
|
|
|
Purpose : free keymap and its descendents
|
|
|
|
Parameters: none
|
|
|
|
Returns : nothing
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
kb_keymap::~kb_keymap()
|
|
|
|
{
|
|
|
|
if(!table) return;
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
1999-12-13 21:59:26 +00:00
|
|
|
if(size < 0) {
|
|
|
|
for(int i = 0; i < KB_HASHSIZE; ++i) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if(htable[i]) {
|
1999-12-13 21:59:26 +00:00
|
|
|
for(kb_key * t = htable[i];
|
|
|
|
t->code != NoSymbol; ++t)
|
1999-09-27 18:44:28 +00:00
|
|
|
if(t->table)
|
|
|
|
delete t->table;
|
|
|
|
delete htable[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete htable;
|
|
|
|
} else {
|
1999-12-16 06:43:25 +00:00
|
|
|
#endif
|
1999-12-13 21:59:26 +00:00
|
|
|
for(kb_key * t = table; t->code != NoSymbol; ++t)
|
1999-09-27 18:44:28 +00:00
|
|
|
if(t->table)
|
|
|
|
delete t->table;
|
|
|
|
delete table;
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-12-16 06:43:25 +00:00
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
|
|
|
|
string keyname(kb_key k)
|
|
|
|
{
|
|
|
|
string buf;
|
|
|
|
printKeysym(k.code, k.mod, buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Finds a key for a keyaction, if possible
|
1999-12-16 06:43:25 +00:00
|
|
|
string kb_keymap::findbinding(int act) const
|
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string res;
|
1999-12-16 06:43:25 +00:00
|
|
|
if (!table) return res;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
1999-12-13 21:59:26 +00:00
|
|
|
if (size < 0) {
|
|
|
|
for(int i = 0; i < KB_HASHSIZE; ++i) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if(htable[i]) {
|
1999-12-13 21:59:26 +00:00
|
|
|
for(kb_key * t = htable[i];
|
|
|
|
t->code != NoSymbol; ++t) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if(t->table) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string suffix = t->table->findbinding(act);
|
|
|
|
suffix = strip(suffix, ' ');
|
|
|
|
suffix = strip(suffix, ']');
|
|
|
|
suffix = frontStrip(suffix, '[');
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!suffix.empty()) {
|
|
|
|
res += "[" + keyname(*t) + " " + suffix + "] ";
|
|
|
|
}
|
|
|
|
} else if (t->action == act) {
|
|
|
|
res += "[" + keyname(*t) + "] ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
1999-12-16 06:43:25 +00:00
|
|
|
#endif
|
1999-12-13 21:59:26 +00:00
|
|
|
for(kb_key * t = table; t->code != NoSymbol; ++t) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if(t->table) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string suffix = t->table->findbinding(act);
|
|
|
|
suffix = strip(suffix, ' ');
|
|
|
|
suffix = strip(suffix, ']');
|
|
|
|
suffix = frontStrip(suffix, '[');
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!suffix.empty()) {
|
|
|
|
res += "[" + keyname(*t) + " " + suffix + "] ";
|
|
|
|
}
|
|
|
|
} else if (t->action == act) {
|
|
|
|
res += "[" + keyname(*t) + "] ";
|
|
|
|
}
|
|
|
|
}
|
1999-12-16 06:43:25 +00:00
|
|
|
#ifndef NO_HASH
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-12-16 06:43:25 +00:00
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* === End of File: kbmap.C ============================================== */
|