3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation
17 #include "commandtags.h"
18 #include "kbsequence.h"
23 string const kb_keymap::printKeysym(LyXKeySymPtr key,
24 key_modifier::state mod)
28 string const s = key->getSymbolName();
30 if (mod & key_modifier::shift) buf += "S-";
31 if (mod & key_modifier::ctrl) buf += "C-";
32 if (mod & key_modifier::alt) buf += "M-";
37 string const kb_keymap::printKey(kb_key const & key) const
39 return printKeysym(key.code, key.mod.first);
43 string::size_type kb_keymap::bind(string const & seq, int action)
45 if (lyxerr.debugging(Debug::KBMAP)) {
46 lyxerr << "BIND: Sequence `"
47 << seq << "' Action `"
48 << action << '\'' << endl;
53 string::size_type const res = k.parse(seq);
54 if (res == string::npos) {
57 lyxerr[Debug::KBMAP] << "Parse error at position " << res
58 << " in key sequence '" << seq << "'."
66 int kb_keymap::lookup(LyXKeySymPtr key,
67 key_modifier::state mod, kb_sequence * seq) const
70 seq->curmap = seq->stdmap;
72 return LFUN_UNKNOWN_ACTION;
75 for (Table::const_iterator cit = table.begin();
76 cit != table.end(); ++cit) {
77 key_modifier::state mask(cit->mod.second);
78 key_modifier::state check =
79 static_cast<key_modifier::state>(mod & ~mask);
81 if (*(cit->code) == *key && cit->mod.first == check) {
83 if (cit->table.get()) {
84 // this is a prefix key - set new map
85 seq->curmap = cit->table.get();
88 // final key - reset map
89 seq->curmap = seq->stdmap;
96 // error - key not found:
97 seq->curmap = seq->stdmap;
99 return LFUN_UNKNOWN_ACTION;
103 string const kb_keymap::print() const
106 for (Table::const_iterator cit = table.begin();
107 cit != table.end(); ++cit) {
108 buf += printKey((*cit));
115 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
117 LyXKeySymPtr code = seq->sequence[r];
121 key_modifier::state const mod1 = seq->modifiers[r].first;
122 key_modifier::state const mod2 = seq->modifiers[r].second;
124 // check if key is already there
125 for (Table::iterator it = table.begin(); it != table.end(); ++it) {
126 if (*(code) == *(it->code)
127 && mod1 == it->mod.first
128 && mod2 == it->mod.second) {
130 if (r + 1 == seq->length()) {
132 << "Warning: New binding for '"
134 << "' is overriding old binding..."
136 if (it->table.get()) {
141 } else if (!it->table.get()) {
142 lyxerr << "Error: New binding for '" << seq->print()
143 << "' is overriding old binding..."
147 it->table->defkey(seq, action, r + 1);
153 Table::iterator newone = table.insert(table.end(), kb_key());
155 newone->mod = seq->modifiers[r];
156 if (r + 1 == seq->length()) {
157 newone->action = action;
158 newone->table.reset();
161 newone->table.reset(new kb_keymap);
162 newone->table->defkey(seq, action, r + 1);
168 string const kb_keymap::findbinding(int act, string const & prefix) const
171 if (table.empty()) return res;
173 Table::const_iterator end = table.end();
174 for (Table::const_iterator cit = table.begin();
176 if (cit->table.get()) {
177 res += cit->table->findbinding(act,
181 } else if (cit->action == act) {
183 res += prefix + printKey((*cit));