]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
Continue to improve GtkLengthEntry
[lyx.git] / src / kbmap.C
1 /**
2  * \file kbmap.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author John Levon
9  * \author André Pönitz
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "kbmap.h"
17
18 #include "debug.h"
19 #include "kbsequence.h"
20 #include "LyXAction.h"
21 #include "lyxlex.h"
22
23 #include "frontends/LyXKeySym.h"
24
25 #include "support/filetools.h"
26
27 #include <sstream>
28
29 using lyx::support::i18nLibFileSearch;
30
31 using std::endl;
32 using std::string;
33
34
35 string const kb_keymap::printKeySym(LyXKeySym const & key,
36                                     key_modifier::state mod)
37 {
38         string buf;
39
40         string const s = key.getSymbolName();
41
42         if (mod & key_modifier::shift)
43                 buf += "S-";
44         if (mod & key_modifier::ctrl)
45                 buf += "C-";
46         if (mod & key_modifier::alt)
47                 buf += "M-";
48
49         buf += s;
50         return buf;
51 }
52
53
54 string const kb_keymap::printKey(kb_key const & key) const
55 {
56         return key.code->print(key.mod.first);
57 }
58
59
60 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
61 {
62         if (lyxerr.debugging(Debug::KBMAP)) {
63                 lyxerr << "BIND: Sequence `"
64                        << seq << "' Action `"
65                        << func.action << '\'' << endl;
66         }
67
68         kb_sequence k(0, 0);
69
70         string::size_type const res = k.parse(seq);
71         if (res == string::npos) {
72                 defkey(&k, func);
73         } else {
74                 lyxerr[Debug::KBMAP] << "Parse error at position " << res
75                                      << " in key sequence '" << seq << "'."
76                                      << endl;
77         }
78
79         return res;
80 }
81
82
83 namespace {
84
85 enum BindTags {
86         BN_BIND,
87         BN_BINDFILE
88 };
89
90 keyword_item bindTags[] = {
91         { "\\bind", BN_BIND },
92         { "\\bind_file", BN_BINDFILE }
93 };
94
95 }
96
97
98 bool kb_keymap::read(string const & bind_file)
99 {
100         const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
101
102         LyXLex lexrc(bindTags, bindCount);
103         if (lyxerr.debugging(Debug::PARSER))
104                 lexrc.printTable(lyxerr);
105
106         string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
107         lexrc.setFile(tmp);
108         if (!lexrc.isOK()) {
109                 lyxerr << "kb_keymap::read: cannot open bind file:"
110                        << tmp << endl;
111                 return false;
112         }
113
114         lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
115
116         bool error = false;
117         while (lexrc.isOK()) {
118                 switch (lexrc.lex()) {
119                 case LyXLex::LEX_UNDEF:
120                         lexrc.printError("Unknown tag `$$Token'");
121                         error = true;
122                         continue;
123                 case LyXLex::LEX_FEOF:
124                         continue;
125                 case BN_BIND:
126                 {
127                         string seq, cmd;
128
129                         if (lexrc.next()) {
130                                 seq = lexrc.getString();
131                         } else {
132                                 lexrc.printError("BN_BIND: Missing key sequence");
133                                 error = true;
134                                 break;
135                         }
136
137                         if (lexrc.next(true)) {
138                                 cmd = lexrc.getString();
139                         } else {
140                                 lexrc.printError("BN_BIND: missing command");
141                                 error = true;
142                                 break;
143                         }
144
145                         FuncRequest func = lyxaction.lookupFunc(cmd);
146                         if (func. action == LFUN_UNKNOWN_ACTION) {
147                                 lexrc.printError("BN_BIND: Unknown LyX"
148                                                  " function `$$Token'");
149                                 error = true;
150                                 break;
151                         }
152
153                         bind(seq, func);
154                         break;
155                 }
156                 case BN_BINDFILE:
157                         if (lexrc.next()) {
158                                 string const tmp(lexrc.getString());
159                                 error |= !read(tmp);
160                         } else {
161                                 lexrc.printError("BN_BINDFILE: Missing file name");
162                                 error = true;
163                                 break;
164
165                         }
166                         break;
167                 }
168         }
169
170         if (error)
171                 lyxerr << "kb_keymap::read: error while reading bind file:"
172                        << tmp << endl;
173         return !error;
174 }
175
176
177 FuncRequest const &
178 kb_keymap::lookup(LyXKeySymPtr key,
179                   key_modifier::state mod, kb_sequence * seq) const
180 {
181         static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
182
183         if (table.empty()) {
184                 seq->curmap = seq->stdmap;
185                 seq->mark_deleted();
186                 return unknown;
187         }
188
189         Table::const_iterator end = table.end();
190         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
191                 key_modifier::state mask(cit->mod.second);
192                 key_modifier::state check =
193                         static_cast<key_modifier::state>(mod & ~mask);
194
195                 if (*(cit->code) == *key && cit->mod.first == check) {
196                         // match found
197                         if (cit->table.get()) {
198                                 // this is a prefix key - set new map
199                                 seq->curmap = cit->table.get();
200                                 static FuncRequest prefix(LFUN_PREFIX);
201                                 return prefix;
202                         } else {
203                                 // final key - reset map
204                                 seq->curmap = seq->stdmap;
205                                 seq->mark_deleted();
206                                 return cit->func;
207                         }
208                 }
209         }
210
211         // error - key not found:
212         seq->curmap = seq->stdmap;
213         seq->mark_deleted();
214
215         return unknown;
216 }
217
218
219 string const kb_keymap::print() const
220 {
221         string buf;
222         Table::const_iterator end = table.end();
223         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
224                 buf += printKey((*cit));
225                 buf += ' ';
226         }
227         return buf;
228 }
229
230
231 void kb_keymap::defkey(kb_sequence * seq,
232                        FuncRequest const & func, unsigned int r)
233 {
234         LyXKeySymPtr code = seq->sequence[r];
235         if (!code->isOK())
236                 return;
237
238         key_modifier::state const mod1 = seq->modifiers[r].first;
239         key_modifier::state const mod2 = seq->modifiers[r].second;
240
241         // check if key is already there
242         Table::iterator end = table.end();
243         for (Table::iterator it = table.begin(); it != end; ++it) {
244                 if (*(code) == *(it->code)
245                     && mod1 == it->mod.first
246                     && mod2 == it->mod.second) {
247                         // overwrite binding
248                         if (r + 1 == seq->length()) {
249                                 lyxerr[Debug::KBMAP]
250                                         << "Warning: New binding for '"
251                                         << seq->print()
252                                         << "' is overriding old binding..."
253                                         << endl;
254                                 if (it->table.get()) {
255                                         it->table.reset();
256                                 }
257                                 it->func = func;
258                                 it->func.origin = FuncRequest::KEYBOARD;
259                                 return;
260                         } else if (!it->table.get()) {
261                                 lyxerr << "Error: New binding for '" << seq->print()
262                                        << "' is overriding old binding..."
263                                                << endl;
264                                 return;
265                         } else {
266                                 it->table->defkey(seq, func, r + 1);
267                                 return;
268                         }
269                 }
270         }
271
272         Table::iterator newone = table.insert(table.end(), kb_key());
273         newone->code = code;
274         newone->mod = seq->modifiers[r];
275         if (r + 1 == seq->length()) {
276                 newone->func = func;
277                 newone->func.origin = FuncRequest::KEYBOARD;
278                 newone->table.reset();
279                 return;
280         } else {
281                 newone->table.reset(new kb_keymap);
282                 newone->table->defkey(seq, func, r + 1);
283                 return;
284         }
285 }
286
287
288 string const kb_keymap::printbindings(FuncRequest const & func) const
289 {
290         std::ostringstream res;
291         Bindings bindings = findbindings(func);
292         for (Bindings::const_iterator cit = bindings.begin();
293              cit != bindings.end() ; ++cit)
294                 res << '[' << cit->print() << ']';
295         return res.str();
296 }
297
298
299 kb_keymap::Bindings
300 kb_keymap::findbindings(FuncRequest const & func) const
301 {
302         return findbindings(func, kb_sequence(0, 0));
303 }
304
305
306 kb_keymap::Bindings
307 kb_keymap::findbindings(FuncRequest const & func,
308                         kb_sequence const & prefix) const
309 {
310         Bindings res;
311         if (table.empty()) return res;
312
313         Table::const_iterator end = table.end();
314         for (Table::const_iterator cit = table.begin();
315             cit != end; ++cit) {
316                 if (cit->table.get()) {
317                         kb_sequence seq = prefix;
318                         seq.addkey(cit->code, cit->mod.first);
319                         Bindings res2 =
320                                 cit->table->findbindings(func, seq);
321                         res.insert(res.end(), res2.begin(), res2.end());
322                 } else if (cit->func == func) {
323                         kb_sequence seq = prefix;
324                         seq.addkey(cit->code, cit->mod.first);
325                         res.push_back(seq);
326                 }
327         }
328
329         return res;
330 }
331
332
333 std::pair<LyXKeySym const *, key_modifier::state>
334 kb_keymap::find1keybinding(FuncRequest const & func) const
335 {
336         Table::const_iterator end = table.end();
337         for (Table::const_iterator cit = table.begin();
338             cit != end; ++cit) {
339                 if (!cit->table.get() && cit->func == func)
340                         return std::make_pair(cit->code.get(), cit->mod.first);
341         }
342
343         return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);
344 }