]> git.lyx.org Git - lyx.git/blobdiff - src/kbmap.C
Make the shortcuts work correctly with the mac.
[lyx.git] / src / kbmap.C
index b95f8aeaee05b3c6574221cf0d97ce807cce60d6..b026b8630fee0a19c8565bfd159533b79080bc18 100644 (file)
-/* This file is part of
- * ====================================================== 
- * 
- *           LyX, The Document Processor
- *      
- *           Copyright 1995 Matthias Ettrich
- *           Copyright 1995-1999 The LyX Team.
+/**
+ * \file kbmap.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * ====================================================== */
+ * \author Lars Gullik Bjønnes
+ * \author Jean-Marc Lasgouttes
+ * \author John Levon
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
-#include <cstring>
-#include "support/lstrings.h"
-#include "gettext.h"
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
 #include "kbmap.h"
-#include "debug.h"
-
-// The only modifiers that we handle. We want to throw away things
-// like NumLock. 
-enum { ModsMask = ShiftMask | ControlMask | Mod1Mask};
 
+#include "debug.h"
+#include "kbsequence.h"
+#include "LyXAction.h"
+#include "lyxlex.h"
 
-// === static functions =================================================== 
-
+#include "frontends/LyXKeySym.h"
 
-/* ---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
-void printKeysym(KeySym key, unsigned int mod, string & buf)
-{
-       mod &= ModsMask;
-
-       char * s = XKeysymToString(key);
-       
-       if (mod & ShiftMask) buf += "S-";
-       if (mod & ControlMask) buf += "C-";
-       if (mod & Mod1Mask) buf += "M-";
-       if (s) buf += s;
-}
+#include "support/filetools.h"
 
+#include <sstream>
 
-/* ---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
-void printKeyTab(kb_key * tabPt, string & buf)
-{
-       unsigned int ksym, mod;
-       
-       /* -------> Print each of the slots into buf. */
-       for( ; (tabPt->code & 0xffff) != NoSymbol; ++tabPt) {
-               ksym =  tabPt->code;
-               mod  =  tabPt->mod & 0xffff;
-               
-               printKeysym(ksym, mod, buf);
-               buf += ' ';
-       }
-}
+namespace lyx {
 
+using support::FileName;
+using support::i18nLibFileSearch;
 
-// === kb_sequence methods ================================================ 
+using std::endl;
+using std::string;
 
-/* ---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------------------------------------------------------------------- */
 
-int kb_sequence::addkey(KeySym key,
-                       unsigned int mod, unsigned int nmod /*= 0*/)
+string const kb_keymap::printKeySym(LyXKeySym const & key,
+                                   key_modifier::state mod)
 {
-       if(length < 0) length = 0;
-
-       if(length + 1 >= size) {
-               unsigned int * nseq = new unsigned int[size + KB_PREALLOC];
-               size += KB_PREALLOC;
-               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));
-               if(modifiers != staticmod) delete modifiers;
-               modifiers = nseq;
-       }
-
-       modifiers[length]  = mod + (nmod << 16);
-       sequence[length++] = key;
-   
-       if(curmap)
-               return curmap->lookup(key, mod, this);
-       
-       return -1;
-}
+       string buf;
 
+       string const s = key.getSymbolName();
 
-/* ---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------------------------------------------------------------------- */
+       if (mod & key_modifier::shift)
+               buf += "S-";
+       if (mod & key_modifier::ctrl)
+               buf += "C-";
+       if (mod & key_modifier::alt)
+               buf += "M-";
 
-int kb_sequence::parse(char const * s)
-{
-       if(!s[0]) return 1;
-
-       int i = 0;
-       unsigned int mod = 0, nmod = 0;
-       while(s[i]) {
-               if(s[i] && (s[i]) <= ' ') ++i;
-               if(!s[i]) break;
-               
-               if(s[i + 1] == '-')     { // is implicit that s[i] == true
-                       switch(s[i]) {
-                       case 's': case 'S':
-                               mod |= ShiftMask;
-                               i += 2;
-                               continue;
-                       case 'c': case 'C':
-                               mod |= ControlMask;
-                               i += 2;
-                               continue;
-                       case 'm': case 'M':
-                               mod |= Mod1Mask;
-                               i += 2;
-                               continue;
-                       default:
-                               return i + 1;
-                       }
-               } else if(s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
-                       switch(s[i + 1]) {
-                       case 's': case 'S':
-                               nmod |= ShiftMask;
-                               i += 3;
-                               continue;
-                       case 'c': case 'C':
-                               nmod |= ControlMask;
-                               i += 3;
-                               continue;
-                       case 'm': case 'M':
-                               nmod |= Mod1Mask;
-                               i += 3;
-                               continue;
-                       default:
-                               return i + 2;
-                       }
-               } else {
-                       string tbuf;
-                       int j = i;
-                       for(; s[j] && s[j] > ' '; ++j)
-                               tbuf += s[j];    // (!!!check bounds :-)
-                       
-                       KeySym key = XStringToKeysym(tbuf.c_str());
-                       if(key == NoSymbol) {
-                               lyxerr[Debug::KBMAP]
-                                       << "kbmap.C: No such keysym: "
-                                       << tbuf << endl;
-                               return j;
-                       }
-                       i = j;
-                       
-                       addkey(key, mod, nmod);
-                       mod = 0;
-                       nmod = 0;
-               }
-       }
-       return 0;
+       buf += s;
+       return buf;
 }
 
 
-/* ---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------------------------------------------------------------------- */
-
-int kb_sequence::print(string & buf, bool when_defined) const
+string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
 {
-       KeySym key;
-       unsigned int mod;
-       int l = length;
-       if ( l < 0 && !when_defined ) l = -l;
-       
-       for(int i = 0; i < l; ++i) {
-               key = sequence[i];
-               mod = modifiers[i] & 0xffff;
-
-               printKeysym(key, mod, buf);  // RVDK_PATCH_5
-
-               if(i + 1 < l) {  // append a blank
-                       buf += ' ';
-               }
+       if (lyxerr.debugging(Debug::KBMAP)) {
+               lyxerr << "BIND: Sequence `"
+                      << seq << "' Action `"
+                      << func.action << '\'' << endl;
        }
-       return 0;
-}
 
+       kb_sequence k(0, 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------------------------------------------------------------------- */
+       string::size_type const res = k.parse(seq);
+       if (res == string::npos) {
+               defkey(&k, func);
+       } else {
+               lyxerr[Debug::KBMAP] << "Parse error at position " << res
+                                    << " in key sequence '" << seq << "'."
+                                    << endl;
+       }
 
-int kb_sequence::printOptions(string & buf) const
-{
-       print(buf, true);
-       
-       if (!curmap) return -1;
-       buf += _("   options: ");
-       curmap->print(buf);
-       return 0;
+       return res;
 }
 
 
-/* ---F+------------------------------------------------------------------ *\
-    Function  : kb_sequence::delseq
-    Called by : [user]
-    Purpose   : mark the sequence as deleted
-    Parameters: none
-    Returns   : nothing
-\* ---F------------------------------------------------------------------- */
+namespace {
 
-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;
-}
+enum BindTags {
+       BN_BIND,
+       BN_BINDFILE
+};
 
+keyword_item bindTags[] = {
+       { "\\bind", BN_BIND },
+       { "\\bind_file", BN_BINDFILE }
+};
 
-/* ---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;
-       if(l == 0) return NoSymbol;
-       if(l < 0) l = -l;
-       return sequence[l - 1];
 }
 
 
-/* ---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()
+bool kb_keymap::read(string const & bind_file)
 {
-       int c = getsym();
-       
-       if(c > 0xff)
-               return '\0';
-       return c;
-}
-
+       const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
+
+       LyXLex lexrc(bindTags, bindCount);
+       if (lyxerr.debugging(Debug::PARSER))
+               lexrc.printTable(lyxerr);
+
+       FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
+       lexrc.setFile(tmp);
+       if (!lexrc.isOK()) {
+               lyxerr << "kb_keymap::read: cannot open bind file:"
+                      << tmp << endl;
+               return false;
+       }
 
-/* ---F+------------------------------------------------------------------ *\
-    Function  : kb_sequence::reset
-    Called by : [user]
-    Purpose   : reset sequence to initial state. RVDK_PATCH_5
-    Parameters: none
-    Returns   : void
-\* ---F------------------------------------------------------------------- */
+       lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
+
+       bool error = false;
+       while (lexrc.isOK()) {
+               switch (lexrc.lex()) {
+               case LyXLex::LEX_UNDEF:
+                       lexrc.printError("Unknown tag `$$Token'");
+                       error = true;
+                       continue;
+               case LyXLex::LEX_FEOF:
+                       continue;
+               case BN_BIND:
+               {
+                       string seq, cmd;
+
+                       if (lexrc.next()) {
+                               seq = lexrc.getString();
+                       } else {
+                               lexrc.printError("BN_BIND: Missing key sequence");
+                               error = true;
+                               break;
+                       }
 
-void kb_sequence::reset()
-{
-       delseq();
-       curmap = stdmap;
-       if (length > 0) length = -length;
-}
+                       if (lexrc.next(true)) {
+                               cmd = lexrc.getString();
+                       } else {
+                               lexrc.printError("BN_BIND: missing command");
+                               error = true;
+                               break;
+                       }
 
+                       FuncRequest func = lyxaction.lookupFunc(cmd);
+                       if (func. action == LFUN_UNKNOWN_ACTION) {
+                               lexrc.printError("BN_BIND: Unknown LyX"
+                                                " function `$$Token'");
+                               error = true;
+                               break;
+                       }
 
-// === kb_keymap methods ================================================== 
+                       bind(seq, func);
+                       break;
+               }
+               case BN_BINDFILE:
+                       if (lexrc.next()) {
+                               string const tmp(lexrc.getString());
+                               error |= !read(tmp);
+                       } else {
+                               lexrc.printError("BN_BINDFILE: Missing file name");
+                               error = true;
+                               break;
 
-// This binds a key to an action
-int kb_keymap::bind(char const * seq, int action)
-{
-       kb_sequence k;
+                       }
+                       break;
+               }
+       }
 
-       int res = k.parse(seq);
-       if (!res) {
-               defkey(&k, action);
-       } else
-               lyxerr[Debug::KBMAP] << "Parse error at position " << res
-                                    << " in key sequence '" << seq << "'."
-                                    << endl;
-       return res;
+       if (error)
+               lyxerr << "kb_keymap::read: error while reading bind file:"
+                      << tmp << endl;
+       return !error;
 }
 
 
-/* ---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------------------------------------------------------------------- */
-
-int kb_keymap::lookup(KeySym key, unsigned int mod, kb_sequence * seq)
+FuncRequest const &
+kb_keymap::lookup(LyXKeySymPtr key,
+                 key_modifier::state mod, kb_sequence * seq) const
 {
-#ifndef NO_HASH
-       unsigned int hashval;
-#endif
-       unsigned int ksym, msk1, msk0;
-       kb_key * tab;
-
-       //suppress modifier bits we do not handle
-       mod &= ModsMask;
+       static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
 
-       if(!table) {
-               // error - no keymap defined:
+       if (table.empty()) {
                seq->curmap = seq->stdmap;
-               seq->delseq();
-               return -1;
+               seq->mark_deleted();
+               return unknown;
        }
 
-#ifndef NO_HASH
-       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 ---
-#endif
-               tab = table;
-
-       // --- now search the list of keys ---
+       Table::const_iterator end = table.end();
+       for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
+               key_modifier::state mask(cit->mod.second);
+               key_modifier::state check =
+                       static_cast<key_modifier::state>(mod & ~mask);
 
-       for(; (tab->code & 0xffff) != NoSymbol; ++tab) {
-               ksym =  tab->code;
-               msk1 =  tab->mod      & 0xffff;
-               msk0 = (tab->mod >> 16) & 0xffff;
-
-               if(ksym == key && (mod & ~msk0) == msk1) {
-                       // match found:
-                       if(tab->table) {
+               if (*(cit->code) == *key && cit->mod.first == check) {
+                       // match found
+                       if (cit->table.get()) {
                                // this is a prefix key - set new map
-                               seq->curmap = tab->table;
-                               return 0;
+                               seq->curmap = cit->table.get();
+                               static FuncRequest prefix(LFUN_COMMAND_PREFIX);
+                               return prefix;
                        } else {
                                // final key - reset map
                                seq->curmap = seq->stdmap;
-                               seq->delseq();
-                               return tab->action; // ... and return action
+                               seq->mark_deleted();
+                               return cit->func;
                        }
                }
        }
-       
+
        // 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------------------------------------------------------------------- */
+       seq->mark_deleted();
 
-void kb_keymap::print(string & buf) const
-{
-       // Return when keymap has no table.
-       if (!table) return;
-   
-       // Process each of its slots recursively and return.
-#ifndef NO_HASH
-       if ( size < 0 ) {   // Hash table
-               for ( int ix = 0; ix < KB_HASHSIZE; ++ix ) {
-                       if ( htable[ix] ) {
-                               printKeyTab(htable[ix], buf);
-                       }
-               }
-       } else // Normal table
-#endif
-               printKeyTab(table, buf);
+       return unknown;
 }
 
 
-/* ---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------------------------------------------------------------------- */
-
-int kb_keymap::defkey(kb_sequence * seq, int action, int idx /*= 0*/)
+docstring const kb_keymap::print(bool forgui) const
 {
-       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
-               table = new kb_key[KB_PREALLOC];
-               table[0].code = NoSymbol;
-               tab   =  table;
-               ptab  = &table;
-               size  = KB_PREALLOC;
-#ifndef NO_HASH
-       } 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;
-               }
-#endif
-       } else {
-               tab  =  table;
-               ptab = &table;
+       docstring buf;
+       Table::const_iterator end = table.end();
+       for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
+               buf += cit->code->print(cit->mod.first, forgui);
+               buf += ' ';
        }
+       return buf;
+}
 
-       // --- check if key is already there --------------------------------
 
-       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) {
-                               string buf;
-                               seq->print(buf, true);
-                               lyxerr[Debug::KEY]
+void kb_keymap::defkey(kb_sequence * seq,
+                      FuncRequest const & func, unsigned int r)
+{
+       LyXKeySymPtr code = seq->sequence[r];
+       if (!code->isOK())
+               return;
+
+       key_modifier::state const mod1 = seq->modifiers[r].first;
+       key_modifier::state const mod2 = seq->modifiers[r].second;
+
+       // check if key is already there
+       Table::iterator end = table.end();
+       for (Table::iterator it = table.begin(); it != end; ++it) {
+               if (*(code) == *(it->code)
+                   && mod1 == it->mod.first
+                   && mod2 == it->mod.second) {
+                       // overwrite binding
+                       if (r + 1 == seq->length()) {
+                               lyxerr[Debug::KBMAP]
                                        << "Warning: New binding for '"
-                                       << buf 
+                                       << to_utf8(seq->print(false))
                                        << "' is overriding old binding..."
                                        << endl;
-
-                               if(t->table) {
-                                       delete t->table;
-                                       t->table = 0;
+                               if (it->table.get()) {
+                                       it->table.reset();
                                }
-                               t->action = action;
-                               return 0;
-                       } else if (!t->table) {
-                               string buf;
-                               seq->print(buf, true);
-                               lyxerr << "Error: New binding for '" << buf
+                               it->func = func;
+                               it->func.origin = FuncRequest::KEYBOARD;
+                               return;
+                       } else if (!it->table.get()) {
+                               lyxerr << "Error: New binding for '" 
+                                      << to_utf8(seq->print(false))
                                       << "' is overriding old binding..."
-                                      << endl;
-                               return -1;
-                       } else
-                               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];
-               // Set to 0 as table is used uninitialised later (thornley)
-               nt[tsize].table = 0;
-               memcpy(nt, tab, tsize * sizeof(kb_key));
-               *ptab = nt;
-               delete[] tab;
-               tab = nt;
-               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 ----------------------
-
-#ifndef NO_HASH
-       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;
+                                              << endl;
+                               return;
+                       } else {
+                               it->table->defkey(seq, func, r + 1);
+                               return;
                        }
-                       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;
        }
-#endif
-       // --- define rest of sequence --------------------------------------
 
-       if(idx+1 == seq->length) {
-               newone->action = action;
-               newone->table  = 0;
-               return 0;
+       Table::iterator newone = table.insert(table.end(), kb_key());
+       newone->code = code;
+       newone->mod = seq->modifiers[r];
+       if (r + 1 == seq->length()) {
+               newone->func = func;
+               newone->func.origin = FuncRequest::KEYBOARD;
+               newone->table.reset();
        } else {
-               newone->table = new kb_keymap;
-               int res = newone->table->defkey(seq, action, idx+1);
-               return res;
+               newone->table.reset(new kb_keymap);
+               newone->table->defkey(seq, func, r + 1);
        }
 }
 
 
-/* ---F+------------------------------------------------------------------ *\
-    Function  : kb_keymap::~kb_keymap
-    Called by : [destructor]
-    Purpose   : free keymap and its descendents
-    Parameters: none
-    Returns   : nothing
-\* ---F------------------------------------------------------------------- */
-
-kb_keymap::~kb_keymap()
+docstring const kb_keymap::printbindings(FuncRequest const & func) const
 {
-       if(!table) return;
-#ifndef NO_HASH
-       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 {
-#endif
-               for(kb_key * t = table; t->code != NoSymbol; ++t)
-                       if(t->table)
-                               delete t->table;
-               delete table;
-#ifndef NO_HASH
-       }
-#endif
+       odocstringstream res;
+       Bindings bindings = findbindings(func);
+       for (Bindings::const_iterator cit = bindings.begin();
+            cit != bindings.end() ; ++cit)
+               res << '[' << cit->print(true) << ']';
+       return res.str();
 }
 
 
-string keyname(kb_key k)
+kb_keymap::Bindings
+kb_keymap::findbindings(FuncRequest const & func) const
 {
-       string buf;
-       printKeysym(k.code, k.mod, buf);
-       return buf;
+       return findbindings(func, kb_sequence(0, 0));
 }
 
 
-// Finds a key for a keyaction, if possible
-string kb_keymap::findbinding(int act) const
+kb_keymap::Bindings
+kb_keymap::findbindings(FuncRequest const & func,
+                       kb_sequence const & prefix) const
 {
-       string res;
-       if (!table) return res;
-
-#ifndef NO_HASH
-       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) + "] ";
-                                       }
-                               }
-                       }
+       Bindings res;
+       if (table.empty()) return res;
+
+       Table::const_iterator end = table.end();
+       for (Table::const_iterator cit = table.begin();
+           cit != end; ++cit) {
+               if (cit->table.get()) {
+                       kb_sequence seq = prefix;
+                       seq.addkey(cit->code, cit->mod.first);
+                       Bindings res2 =
+                               cit->table->findbindings(func, seq);
+                       res.insert(res.end(), res2.begin(), res2.end());
+               } else if (cit->func == func) {
+                       kb_sequence seq = prefix;
+                       seq.addkey(cit->code, cit->mod.first);
+                       res.push_back(seq);
                }
-       } else {
-#endif
-               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) + "] ";
-                       }
-               }
-#ifndef NO_HASH
        }
-#endif
+
        return res;
 }
 
 
-/* === End of File: kbmap.C ============================================== */
+std::pair<LyXKeySym const *, key_modifier::state>
+kb_keymap::find1keybinding(FuncRequest const & func) const
+{
+       Table::const_iterator end = table.end();
+       for (Table::const_iterator cit = table.begin();
+           cit != end; ++cit) {
+               if (!cit->table.get() && cit->func == func)
+                       return std::make_pair(cit->code.get(), cit->mod.first);
+       }
+
+       return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);
+}
+
+
+} // namespace lyx