3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
14 #include "kbsequence.h"
19 string const kb_keymap::printKeysym(LyXKeySymPtr key,
20 key_modifier::state mod)
24 string const s = key->getSymbolName();
26 if (mod & key_modifier::shift)
28 if (mod & key_modifier::ctrl)
30 if (mod & key_modifier::alt)
38 string const kb_keymap::printKey(kb_key const & key) const
40 return printKeysym(key.code, key.mod.first);
44 string::size_type kb_keymap::bind(string const & seq, int action)
46 if (lyxerr.debugging(Debug::KBMAP)) {
47 lyxerr << "BIND: Sequence `"
48 << seq << "' Action `"
49 << action << '\'' << endl;
54 string::size_type const res = k.parse(seq);
55 if (res == string::npos) {
58 lyxerr[Debug::KBMAP] << "Parse error at position " << res
59 << " in key sequence '" << seq << "'."
67 int kb_keymap::lookup(LyXKeySymPtr key,
68 key_modifier::state mod, kb_sequence * seq) const
71 seq->curmap = seq->stdmap;
73 return LFUN_UNKNOWN_ACTION;
76 Table::const_iterator end = table.end();
77 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
78 key_modifier::state mask(cit->mod.second);
79 key_modifier::state check =
80 static_cast<key_modifier::state>(mod & ~mask);
82 if (*(cit->code) == *key && cit->mod.first == check) {
84 if (cit->table.get()) {
85 // this is a prefix key - set new map
86 seq->curmap = cit->table.get();
89 // final key - reset map
90 seq->curmap = seq->stdmap;
97 // error - key not found:
98 seq->curmap = seq->stdmap;
100 return LFUN_UNKNOWN_ACTION;
104 string const kb_keymap::print() const
107 Table::const_iterator end = table.end();
108 for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
109 buf += printKey((*cit));
116 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
118 LyXKeySymPtr code = seq->sequence[r];
122 key_modifier::state const mod1 = seq->modifiers[r].first;
123 key_modifier::state const mod2 = seq->modifiers[r].second;
125 // check if key is already there
126 Table::iterator end = table.end();
127 for (Table::iterator it = table.begin(); it != end; ++it) {
128 if (*(code) == *(it->code)
129 && mod1 == it->mod.first
130 && mod2 == it->mod.second) {
132 if (r + 1 == seq->length()) {
134 << "Warning: New binding for '"
136 << "' is overriding old binding..."
138 if (it->table.get()) {
143 } else if (!it->table.get()) {
144 lyxerr << "Error: New binding for '" << seq->print()
145 << "' is overriding old binding..."
149 it->table->defkey(seq, action, r + 1);
155 Table::iterator newone = table.insert(table.end(), kb_key());
157 newone->mod = seq->modifiers[r];
158 if (r + 1 == seq->length()) {
159 newone->action = action;
160 newone->table.reset();
163 newone->table.reset(new kb_keymap);
164 newone->table->defkey(seq, action, r + 1);
170 string const kb_keymap::findbinding(int act, string const & prefix) const
173 if (table.empty()) return res;
175 Table::const_iterator end = table.end();
176 for (Table::const_iterator cit = table.begin();
178 if (cit->table.get()) {
179 res += cit->table->findbinding(act,
183 } else if (cit->action == act) {
185 res += prefix + printKey((*cit));