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