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) buf += "S-";
27 if (mod & key_modifier::ctrl) buf += "C-";
28 if (mod & key_modifier::alt) buf += "M-";
33 string const kb_keymap::printKey(kb_key const & key) const
35 return printKeysym(key.code, key.mod.first);
39 string::size_type kb_keymap::bind(string const & seq, int action)
41 if (lyxerr.debugging(Debug::KBMAP)) {
42 lyxerr << "BIND: Sequence `"
43 << seq << "' Action `"
44 << action << '\'' << endl;
49 string::size_type const res = k.parse(seq);
50 if (res == string::npos) {
53 lyxerr[Debug::KBMAP] << "Parse error at position " << res
54 << " in key sequence '" << seq << "'."
62 int kb_keymap::lookup(LyXKeySymPtr key,
63 key_modifier::state mod, kb_sequence * seq) const
66 seq->curmap = seq->stdmap;
68 return LFUN_UNKNOWN_ACTION;
71 for (Table::const_iterator cit = table.begin();
72 cit != table.end(); ++cit) {
73 key_modifier::state mask(cit->mod.second);
74 key_modifier::state check =
75 static_cast<key_modifier::state>(mod & ~mask);
77 if (*(cit->code) == *key && cit->mod.first == check) {
79 if (cit->table.get()) {
80 // this is a prefix key - set new map
81 seq->curmap = cit->table.get();
84 // final key - reset map
85 seq->curmap = seq->stdmap;
92 // error - key not found:
93 seq->curmap = seq->stdmap;
95 return LFUN_UNKNOWN_ACTION;
99 string const kb_keymap::print() const
102 for (Table::const_iterator cit = table.begin();
103 cit != table.end(); ++cit) {
104 buf += printKey((*cit));
111 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
113 LyXKeySymPtr code = seq->sequence[r];
117 key_modifier::state const mod1 = seq->modifiers[r].first;
118 key_modifier::state const mod2 = seq->modifiers[r].second;
120 // check if key is already there
121 for (Table::iterator it = table.begin(); it != table.end(); ++it) {
122 if (*(code) == *(it->code)
123 && mod1 == it->mod.first
124 && mod2 == it->mod.second) {
126 if (r + 1 == seq->length()) {
128 << "Warning: New binding for '"
130 << "' is overriding old binding..."
132 if (it->table.get()) {
137 } else if (!it->table.get()) {
138 lyxerr << "Error: New binding for '" << seq->print()
139 << "' is overriding old binding..."
143 it->table->defkey(seq, action, r + 1);
149 Table::iterator newone = table.insert(table.end(), kb_key());
151 newone->mod = seq->modifiers[r];
152 if (r + 1 == seq->length()) {
153 newone->action = action;
154 newone->table.reset();
157 newone->table.reset(new kb_keymap);
158 newone->table->defkey(seq, action, r + 1);
164 string const kb_keymap::findbinding(int act, string const & prefix) const
167 if (table.empty()) return res;
169 Table::const_iterator end = table.end();
170 for (Table::const_iterator cit = table.begin();
172 if (cit->table.get()) {
173 res += cit->table->findbinding(act,
177 } else if (cit->action == act) {
179 res += prefix + printKey((*cit));