/* This file is part of
- * ======================================================
+ * ======================================================
*
- * LyX, The Document Processor
+ * LyX, The Document Processor
*
- * Copyright 1995 Matthias Ettrich
- * Copyright 1995-1999 The LyX Team.
+ * Copyright 1995 Matthias Ettrich
+ * Copyright 1995-1999 The LyX Team.
*
- * ======================================================*/
+ * ====================================================== */
#include <config.h>
#include <cstring>
-#include <cstdio>
+#include <X11/Xlib.h>
+
#include "support/lstrings.h"
#include "gettext.h"
#endif
#include "kbmap.h"
-#include "error.h"
+#include "debug.h"
// The only modifiers that we handle. We want to throw away things
// like NumLock.
enum { ModsMask = ShiftMask | ControlMask | Mod1Mask};
-// === static functions ===================================================
+// === static functions ===================================================
/* ---F+------------------------------------------------------------------ *\
maxlen - length of string (including '\0')
Returns : length of printed string if ok, 0 otherwise.
\* ---F------------------------------------------------------------------- */
-
static
-int printKeysym( KeySym key, unsigned int mod, char *buf, int maxlen )
+void printKeysym(unsigned long key, unsigned int mod, string & buf)
{
- int len;
- char *s;
-
mod &= ModsMask;
- // calc required length;
- len = 0;
- if ( mod & ShiftMask ) len += 2;
- if ( mod & ControlMask ) len += 2;
- if ( mod & Mod1Mask ) len += 2;
+ char * s = XKeysymToString(key);
- s = XKeysymToString( key );
- if ( s ) len += strlen( s );
- if ( len < maxlen ) {
- if ( mod & ShiftMask ) {
- *buf++ = 'S'; *buf++ = '-'; }
- if ( mod & ControlMask ) {
- *buf++ = 'C'; *buf++ = '-'; }
- if ( mod & Mod1Mask ) {
- *buf++ = 'M'; *buf++ = '-'; }
- if ( s ) strcpy( buf, s );
- return len;
- } else
- return 0;
+ if (mod & ShiftMask) buf += "S-";
+ if (mod & ControlMask) buf += "C-";
+ if (mod & Mod1Mask) buf += "M-";
+ if (s) buf += s;
}
\* ---F------------------------------------------------------------------- */
static
-int printKeyTab( kb_key *tabPt, char *buf, int maxLen )
+void printKeyTab(kb_key * tabPt, string & buf)
{
- int len, doneLen = 0;
unsigned int ksym, mod;
/* -------> Print each of the slots into buf. */
- for( ; (tabPt->code & 0xffff) != NoSymbol; tabPt++) {
- if ( maxLen <= 0 ) break;
-
+ for( ; (tabPt->code & 0xffff) != NoSymbol; ++tabPt) {
ksym = tabPt->code;
mod = tabPt->mod & 0xffff;
- len = printKeysym( ksym, mod, buf, maxLen );
- if ( len <= 0 ) break;
- buf += len;
- maxLen -= len;
- doneLen += len;
-
- /* -------> Add space when possible. */
- if ( maxLen > 0 ) {
- *buf++ = ' ';
- *buf = '\0';
- maxLen--;
- doneLen++;
- }
+ printKeysym(ksym, mod, buf);
+ buf += ' ';
}
- return doneLen;
}
-
-// === kb_sequence methods ================================================
-
-
+// === kb_sequence methods ================================================
/* ---F+------------------------------------------------------------------ *\
Function : kb_sequence::addkey
Returns : action or -1 if error (no map defined or key not found)
\* ---F------------------------------------------------------------------- */
-int kb_sequence::addkey(KeySym key, unsigned int mod, unsigned int nmod /*=0*/)
+int kb_sequence::addkey(unsigned long key,
+ unsigned int mod, unsigned int nmod /*= 0*/)
{
- if(length<0) length=0;
+ if(length < 0) length = 0;
- if(length+1 >= size) {
- unsigned int *nseq = new unsigned int[size+KB_PREALLOC];
+ if(length + 1 >= size) {
+ unsigned int * nseq = new unsigned int[size + KB_PREALLOC];
size += KB_PREALLOC;
- memcpy(nseq, sequence, length*sizeof(unsigned int));
+ memcpy(nseq, sequence, length * sizeof(unsigned int));
if(sequence != staticseq) delete sequence;
sequence = nseq;
nseq = new unsigned int[size];
- memcpy(nseq, modifiers, length*sizeof(unsigned int));
+ memcpy(nseq, modifiers, length * sizeof(unsigned int));
if(modifiers != staticmod) delete modifiers;
modifiers = nseq;
}
- modifiers[length] = mod + (nmod<<16);
+ modifiers[length] = mod + (nmod << 16);
sequence[length++] = key;
if(curmap)
Prefixes are S-, C-, M- for shift, control, meta
\* ---F------------------------------------------------------------------- */
-int kb_sequence::parse(char const*s)
+int kb_sequence::parse(char const * s)
{
+ if(!s[0]) return 1;
+
int i = 0;
unsigned int mod = 0, nmod = 0;
- KeySym key = 0;
- char tbuf[100];
-
- if(!s[0]) return 1;
-
while(s[i]) {
- if(s[i] && ((unsigned char) s[i]) <= ' ') i++;
+ if(s[i] && (s[i]) <= ' ') ++i;
if(!s[i]) break;
- if(s[i+1]=='-') { // is implicit that s[i]==true
+ if(s[i + 1] == '-') { // is implicit that s[i] == true
switch(s[i]) {
case 's': case 'S':
mod |= ShiftMask;
- i+=2;
+ i += 2;
continue;
case 'c': case 'C':
mod |= ControlMask;
- i+=2;
+ i += 2;
continue;
case 'm': case 'M':
mod |= Mod1Mask;
- i+=2;
+ i += 2;
continue;
default:
- return i+1;
+ return i + 1;
}
- } else if(s[i]=='~' && s[i+1] && s[i+2]=='-') {
- switch(s[i+1]) {
+ } else if(s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
+ switch(s[i + 1]) {
case 's': case 'S':
nmod |= ShiftMask;
- i+=3;
+ i += 3;
continue;
case 'c': case 'C':
nmod |= ControlMask;
- i+=3;
+ i += 3;
continue;
case 'm': case 'M':
nmod |= Mod1Mask;
- i+=3;
+ i += 3;
continue;
default:
- return i+2;
+ return i + 2;
}
} else {
- int j = 0;
- for(j = i; s[j] && ((unsigned char)s[j])>' '; j++)
- tbuf[j-i] = s[j]; // (!!!check bounds :-)
+ string tbuf;
+ int j = i;
+ for(; s[j] && s[j] > ' '; ++j)
+ tbuf += s[j]; // (!!!check bounds :-)
- tbuf[j-i] = '\0';
-
- key = XStringToKeysym(tbuf);
+ KeySym key = XStringToKeysym(tbuf.c_str());
if(key == NoSymbol) {
- lyxerr.debug("kbmap.C: No such keysym: "
- + string(tbuf),Error::KBMAP);
+ lyxerr[Debug::KBMAP]
+ << "kbmap.C: No such keysym: "
+ << tbuf << endl;
return j;
}
i = j;
Returns : 0, if ok, -1 if string too long
\* ---F------------------------------------------------------------------- */
-int kb_sequence::print(char *buf, int maxlen, bool when_defined) const
+int kb_sequence::print(string & buf, bool when_defined) const
{
KeySym key;
unsigned int mod;
- int len;
int l = length;
- if ( l<0 && !when_defined ) l = -l;
+ if ( l < 0 && !when_defined ) l = -l;
- for(int i = 0; i < l; i++) {
+ for(int i = 0; i < l; ++i) {
key = sequence[i];
mod = modifiers[i] & 0xffff;
- len = printKeysym( key, mod, buf, maxlen ); // RVDK_PATCH_5
- buf += len;
- maxlen -= len;
-
- if ( len == 0 ) {
- *buf = '\0';
- return -1;
- }
+ printKeysym(key, mod, buf); // RVDK_PATCH_5
- if(i+1<l && maxlen>1) { // append a blank
- *buf++ = ' ';
- maxlen--;
+ if(i + 1 < l) { // append a blank
+ buf += ' ';
}
}
- *buf = '\0';
return 0;
}
Returns : 0, if ok, -1 if string too long
\* ---F------------------------------------------------------------------- */
-int kb_sequence::printOptions(char *buf, int maxlen) const
+int kb_sequence::printOptions(string & buf) const
{
- int len;
-
- print( buf, maxlen, true );
- len = strlen( buf );
- maxlen -= len;
- buf += len;
+ print(buf, true);
- if ( maxlen < 20 || !curmap ) return -1;
-#ifdef WITH_WARNINGS
-#warning reimplement kb_sequence using string
-#endif
- char s[20];
- strcpy(s,_(" options: "));
- strcpy( buf, s);
- buf += strlen(s);
- maxlen -= strlen(s);
-
- curmap->print(buf, maxlen);
+ if (!curmap) return -1;
+ buf += _(" options: ");
+ curmap->print(buf);
return 0;
}
KeySym kb_sequence::getsym()
{
int l = length;
- if(l==0) return NoSymbol;
- if(l<0) l = -l;
- return sequence[l-1];
+ if(l == 0) return NoSymbol;
+ if(l < 0) l = -l;
+ return sequence[l - 1];
}
if(c > 0xff)
return '\0';
- return (char)c;
+ return c;
}
{
delseq();
curmap = stdmap;
- if ( length > 0 ) length = -length;
+ if (length > 0) length = -length;
}
-// === kb_keymap methods ==================================================
+// === kb_keymap methods ==================================================
// This binds a key to an action
-int kb_keymap::bind(char const *seq, int action)
+int kb_keymap::bind(char const * seq, int action)
{
kb_sequence k;
if (!res) {
defkey(&k, action);
} else
- lyxerr.debug(string("Parse error at position ") + tostr(res) +
- " in key sequence '" + seq + "'.", Error::KBMAP);
+ lyxerr[Debug::KBMAP] << "Parse error at position " << res
+ << " in key sequence '" << seq << "'."
+ << endl;
return res;
}
Returns : user defined action; 0 for prefix key, -1 if key not found
\* ---F------------------------------------------------------------------- */
-int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence *seq)
+int kb_keymap::lookup(unsigned long key, unsigned int mod, kb_sequence * seq)
{
- unsigned int hashval, ksym, msk1, msk0;
- kb_key *tab;
+ unsigned int ksym, msk1, msk0;
+ kb_key * tab;
//suppress modifier bits we do not handle
mod &= ModsMask;
return -1;
}
- if(size < 0) { // --- if hash table ---
- hashval = ((key&0xff) ^ ((key>>8)&0xff)) % KB_HASHSIZE;
- tab = htable[hashval];
- if(!tab) {
- seq->curmap = seq->stdmap;
- seq->delseq();
- return -1;
- }
- } else // --- else: linear list ---
- tab = table;
+ tab = table;
// --- now search the list of keys ---
- for( ; (tab->code & 0xffff) != NoSymbol; tab++) {
+ for(; (tab->code & 0xffff) != NoSymbol; ++tab) {
ksym = tab->code;
msk1 = tab->mod & 0xffff;
- msk0 = (tab->mod>>16) & 0xffff;
+ msk0 = (tab->mod >> 16) & 0xffff;
- if(ksym == key && (mod&~msk0) == msk1) {
+ if(ksym == key && (mod & ~msk0) == msk1) {
// match found:
if(tab->table) {
- // this is a prefix key - set new map
+ // this is a prefix key - set new map
seq->curmap = tab->table;
return 0;
} else {
- // final key - reset map
+ // final key - reset map
seq->curmap = seq->stdmap;
seq->delseq();
return tab->action; // ... and return action
Returns : updated maxLen.
\* ---F------------------------------------------------------------------- */
-int kb_keymap::print(char *buf, int maxLen) const
+void kb_keymap::print(string & buf) const
{
- int len;
-
- /* -----> Return when running out of string space or when keymap has no table.
- Else, place a terminating newline in case no other output is generated. */
-
- if ( maxLen <= 3 || !buf ) return maxLen;
- if ( !table ) return maxLen;
- *buf = '\0';
+ // Return when keymap has no table.
+ if (!table) return;
- /* -------> Hash table. Process each of its slots recursively and return. */
- if ( size < 0 ) {
- for ( int ix = 0; (ix < KB_HASHSIZE) && (maxLen > 1); ix++ ) {
- if ( htable[ix] ) {
- len = printKeyTab( htable[ix], buf, maxLen );
- maxLen -= len;
- buf += len;
- }
- }
- } else {
- /* -------> Normal table. */
- len = printKeyTab( table, buf, maxLen );
- maxLen -= len;
- buf += len;
- }
- return maxLen;
+ // Process each of its slots recursively and return.
+ printKeyTab(table, buf);
}
Returns : 0 if ok.
\* ---F------------------------------------------------------------------- */
-int kb_keymap::defkey(kb_sequence *seq, int action, int idx /*=0*/)
+int kb_keymap::defkey(kb_sequence * seq, int action, int idx /*= 0*/)
{
- int tsize;
- unsigned int code, modmsk;
- kb_key *tab, **ptab;
-
- code = seq->sequence[idx];
- modmsk = seq->modifiers[idx];
+ unsigned int code = seq->sequence[idx];
if(code == NoSymbol) return -1;
+ unsigned int modmsk = seq->modifiers[idx];
+ kb_key * tab, ** ptab;
// --- get list------------------------------------------------------
if(!table) {
// If we don't have any yet, make an empty one
tab = table;
ptab = &table;
size = KB_PREALLOC;
- } else if(size<0) {
- // Hash table.
- int hashval = (code&0xffff);
- hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
- tab = htable[hashval];
- ptab = htable+hashval;
- if(!tab) {
- tab = new kb_key[KB_PREALLOC];
- tab[0].code = NoSymbol;
- *ptab = tab;
- }
} else {
tab = table;
ptab = &table;
// --- check if key is already there --------------------------------
- kb_key *t;
- for(t = tab, tsize=1; t->code != NoSymbol; t++, tsize++) {
+ kb_key * t;
+ int tsize;
+ for(t = tab, tsize = 1; t->code != NoSymbol; ++t, ++tsize) {
if(code == t->code && modmsk == t->mod) { // -- overwrite binding ---
- if(idx+1 == seq->length) {
- char buf[20]; buf[0] = 0;
- seq->print(buf, 20, true);
- lyxerr.debug(string("Warning: New binding for '") + buf +
- "' is overriding old binding...", Error::KEY);
+ if(idx + 1 == seq->length) {
+ string buf;
+ seq->print(buf, true);
+ lyxerr[Debug::KEY]
+ << "Warning: New binding for '"
+ << buf
+ << "' is overriding old binding..."
+ << endl;
if(t->table) {
delete t->table;
t->action = action;
return 0;
} else if (!t->table) {
- char buf[20]; buf[0] = 0;
- seq->print(buf, 20, true);
- lyxerr.print(string("Error: New binding for '") + buf +
- "' is overriding old binding...");
+ string buf;
+ seq->print(buf, true);
+ lyxerr << "Error: New binding for '" << buf
+ << "' is overriding old binding..."
+ << endl;
return -1;
} else
- return t->table->defkey(seq, action, idx+1);
+ return t->table->defkey(seq, action, idx + 1);
}
}
// --- extend list if necessary -------------------------------------
if(tsize % KB_PREALLOC == 0) {
- kb_key *nt = new kb_key[tsize+KB_PREALLOC];
+ kb_key * nt = new kb_key[tsize + KB_PREALLOC];
// Set to 0 as table is used uninitialised later (thornley)
nt[tsize].table = 0;
- memcpy(nt, tab, tsize*sizeof(kb_key));
+ memcpy(nt, tab, tsize * sizeof(kb_key));
*ptab = nt;
delete[] tab;
tab = nt;
- if(size>=0) size = tsize+KB_PREALLOC;
+ if(size >= 0) size = tsize + KB_PREALLOC;
}
// --- add action ---------------------------------------------------
tab[tsize--].code = NoSymbol;
tab[tsize].code = code;
tab[tsize].mod = modmsk;
- kb_key *newone = &tab[tsize];
-
- // --- convert list to hash table if necessary ----------------------
-
- 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++)
- nht[i] = 0;
- htable = nht;
- size = -KB_HASHSIZE;
-
- // --- copy old keys to new hash table ---
- int hashval;
- for(kb_key *tu = oldtab; tu->code != NoSymbol; tu++){
- // copy values from oldtab to htable
- hashval = (tu->code&0xffff);
- hashval = ((hashval&0xff) ^ ((hashval>>8)&0xff)) % KB_HASHSIZE;
- tab = htable[hashval];
-
- if(!tab){
- htable[hashval] = tab = new kb_key[KB_PREALLOC];
- tab->code = NoSymbol;
- }
- int ts = 1;
- for(kb_key *tt = tab; tt->code != NoSymbol; tt++)
- ts++;
- if(ts % KB_PREALLOC == 0){
- // extend table
- kb_key *nt = new kb_key[ts+KB_PREALLOC];
- memcpy(nt, tab, ts*sizeof(kb_key));
- 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;
- }
+ kb_key * newone = &tab[tsize];
// --- define rest of sequence --------------------------------------
- if(idx+1 == seq->length) {
+ 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);
+ int res = newone->table->defkey(seq, action, idx + 1);
return res;
}
}
kb_keymap::~kb_keymap()
{
if(!table) return;
- if(size<0) {
- for(int i=0; i < KB_HASHSIZE; i++) {
- if(htable[i]) {
- for(kb_key *t = htable[i]; t->code != NoSymbol; t++)
- if(t->table)
- delete t->table;
- delete htable[i];
- }
- }
- delete htable;
- } else {
- for(kb_key *t = table; t->code != NoSymbol; t++)
- if(t->table)
- delete t->table;
- delete table;
- }
+ for(kb_key * t = table; t->code != NoSymbol; ++t)
+ if(t->table)
+ delete t->table;
+ delete table;
}
-string keyname(kb_key k) {
- char buf[100];
- printKeysym(k.code, k.mod, buf, 100);
+
+string keyname(kb_key k)
+{
+ string buf;
+ printKeysym(k.code, k.mod, buf);
return buf;
}
+
// Finds a key for a keyaction, if possible
-string kb_keymap::findbinding(int act) const {
+string kb_keymap::findbinding(int act) const
+{
string res;
- if (!table)
- return res;
-
- if (size<0) {
- for(int i=0; i < KB_HASHSIZE; i++) {
- if(htable[i]) {
- for(kb_key *t = htable[i]; t->code != NoSymbol; t++) {
- if(t->table) {
- string suffix = t->table->findbinding(act);
- suffix = strip(suffix, ' ');
- suffix = strip(suffix, ']');
- suffix = frontStrip(suffix, '[');
- if (!suffix.empty()) {
- res += "[" + keyname(*t) + " " + suffix + "] ";
- }
- } else if (t->action == act) {
- res += "[" + keyname(*t) + "] ";
- }
- }
- }
- }
- } else {
- for(kb_key *t = table; t->code != NoSymbol; t++) {
- if(t->table) {
- string suffix = t->table->findbinding(act);
- suffix = strip(suffix, ' ');
- suffix = strip(suffix, ']');
- suffix = frontStrip(suffix, '[');
- if (!suffix.empty()) {
- res += "[" + keyname(*t) + " " + suffix + "] ";
- }
- } else if (t->action == act) {
- res += "[" + keyname(*t) + "] ";
+ if (!table) return res;
+
+ for(kb_key * t = table; t->code != NoSymbol; ++t) {
+ if(t->table) {
+ string suffix = t->table->findbinding(act);
+ suffix = strip(suffix, ' ');
+ suffix = strip(suffix, ']');
+ suffix = frontStrip(suffix, '[');
+ if (!suffix.empty()) {
+ res += "[" + keyname(*t) + " " + suffix + "] ";
}
+ } else if (t->action == act) {
+ res += "[" + keyname(*t) + "] ";
}
}
return res;