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