]> git.lyx.org Git - features.git/blob - src/KeyMap.cpp
simplify Lexer handling a bit more
[features.git] / src / KeyMap.cpp
1 /**
2  * \file KeyMap.cpp
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 "KeyMap.h"
17
18 #include "KeySequence.h"
19 #include "LyXAction.h"
20 #include "Lexer.h"
21
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26
27 #include <fstream>
28 #include <sstream>
29 #include <utility>
30
31 using namespace std;
32 using namespace lyx::support;
33
34 namespace lyx {
35
36
37 string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
38 {
39         string buf;
40
41         string const s = key.getSymbolName();
42
43         if (mod & ControlModifier)
44                 buf += "C-";
45         if (mod & AltModifier)
46                 buf += "M-";
47         if (mod & ShiftModifier)
48                 buf += "S-";
49
50         buf += s;
51         return buf;
52 }
53
54
55 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
56 {
57         LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
58                << func.action << '\'');
59
60         KeySequence k(0, 0);
61
62         string::size_type const res = k.parse(seq);
63         if (res == string::npos) {
64                 bind(&k, func);
65         } else {
66                 LYXERR(Debug::KBMAP, "Parse error at position " << res
67                                      << " in key sequence '" << seq << "'.");
68         }
69
70         return res == string::npos ? 0 : res;
71 }
72
73
74 size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
75 {
76         KeySequence k(0, 0);
77
78         string::size_type const res = k.parse(seq);
79         if (res == string::npos)
80                 unbind(&k, func);
81         else
82                 LYXERR(Debug::KBMAP, "Parse error at position " << res
83                                      << " in key sequence '" << seq << "'.");
84         return res == string::npos ? 0 : res;
85 }
86
87
88 bool KeyMap::hasBinding(KeySequence const & seq, FuncRequest const & func,
89                 unsigned int r)
90 {
91         KeySymbol code = seq.sequence[r];
92         if (!code.isOK())
93                 return false;
94
95         KeyModifier const mod1 = seq.modifiers[r].first;
96         KeyModifier const mod2 = seq.modifiers[r].second;
97
98         // check if key is already there
99         Table::iterator end = table.end();
100         for (Table::iterator it = table.begin(); it != end; ++it) {
101                 if (code == it->code
102                     && mod1 == it->mod.first
103                     && mod2 == it->mod.second) {
104                         if (r + 1 == seq.length())
105                                 return it->func == func;
106                         else if (it->table.get())
107                                 return it->table->hasBinding(seq, func, r + 1);
108                 }
109         }
110         return false;
111 }
112
113
114 void KeyMap::clear()
115 {
116         table.clear();
117 }
118
119
120 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
121 {
122         enum {
123                 BN_BIND,
124                 BN_BINDFILE,
125                 BN_UNBIND,
126         };
127
128         LexerKeyword bindTags[] = {
129                 { "\\bind", BN_BIND },
130                 { "\\bind_file", BN_BINDFILE },
131                 { "\\unbind", BN_UNBIND },
132         };
133
134         Lexer lexrc(bindTags);
135         if (lyxerr.debugging(Debug::PARSER))
136                 lexrc.printTable(lyxerr);
137
138         FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
139         lexrc.setFile(tmp);
140         if (!lexrc.isOK()) {
141                 lyxerr << "KeyMap::read: cannot open bind file:"
142                        << tmp << endl;
143                 return false;
144         }
145
146         LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
147
148         bool error = false;
149         while (lexrc.isOK()) {
150                 switch (lexrc.lex()) {
151                 case Lexer::LEX_UNDEF:
152                         lexrc.printError("Unknown tag `$$Token'");
153                         error = true;
154                         continue;
155                 case Lexer::LEX_FEOF:
156                         continue;
157                 case BN_BIND:
158                 {
159                         string seq, cmd;
160
161                         if (lexrc.next()) {
162                                 seq = lexrc.getString();
163                         } else {
164                                 lexrc.printError("BN_BIND: Missing key sequence");
165                                 error = true;
166                                 break;
167                         }
168
169                         if (lexrc.next(true)) {
170                                 cmd = lexrc.getString();
171                         } else {
172                                 lexrc.printError("BN_BIND: missing command");
173                                 error = true;
174                                 break;
175                         }
176
177                         FuncRequest func = lyxaction.lookupFunc(cmd);
178                         if (func. action == LFUN_UNKNOWN_ACTION) {
179                                 lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
180                                 error = true;
181                                 break;
182                         }
183
184                         bind(seq, func);
185                         break;
186                 }
187                 case BN_UNBIND:
188                 {
189                         string seq, cmd;
190
191                         if (lexrc.next()) {
192                                 seq = lexrc.getString();
193                         } else {
194                                 lexrc.printError("BN_UNBIND: Missing key sequence");
195                                 error = true;
196                                 break;
197                         }
198
199                         if (lexrc.next(true)) {
200                                 cmd = lexrc.getString();
201                         } else {
202                                 lexrc.printError("BN_UNBIND: missing command");
203                                 error = true;
204                                 break;
205                         }
206
207                         FuncRequest func = lyxaction.lookupFunc(cmd);
208                         if (func. action == LFUN_UNKNOWN_ACTION) {
209                                 lexrc.printError("BN_UNBIND: Unknown LyX"
210                                                  " function `$$Token'");
211                                 error = true;
212                                 break;
213                         }
214                         
215                         if (unbind_map)
216                                 unbind_map->bind(seq, func);
217                         else
218                                 unbind(seq, func);
219                         break;
220                 }
221                 case BN_BINDFILE:
222                         if (lexrc.next()) {
223                                 string const tmp(lexrc.getString());
224                                 error |= !read(tmp, unbind_map);
225                         } else {
226                                 lexrc.printError("BN_BINDFILE: Missing file name");
227                                 error = true;
228                                 break;
229
230                         }
231                         break;
232                 }
233         }
234
235         if (error)
236                 lyxerr << "KeyMap::read: error while reading bind file:"
237                        << tmp << endl;
238         return !error;
239 }
240
241
242 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
243 {
244         ofstream os(bind_file.c_str(), 
245                 append ? (ios::app | ios::out) : ios::out);
246
247         if (!append)
248                 os << "## This file is automatically generated by lyx\n"
249                    << "## All modifications will be lost\n\n";
250         
251         string tag = unbind ? "\\unbind" : "\\bind";
252         BindingList const list = listBindings(false);
253         BindingList::const_iterator it = list.begin();
254         BindingList::const_iterator it_end = list.end();
255         for (; it != it_end; ++it) {
256                 FuncCode action = it->request.action;
257                 string arg = to_utf8(it->request.argument());
258
259                 os << tag << " \""
260                                 << to_utf8(it->sequence.print(KeySequence::BindFile))
261                                 << "\" \""
262                                 << lyxaction.getActionName(action)
263                                 << (arg.empty() ? "" : " ") << arg
264                                 << "\"\n";
265         }
266         os << "\n";
267         os.close();
268 }
269
270
271 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
272                   KeyModifier mod, KeySequence * seq) const
273 {
274         static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
275
276         if (table.empty()) {
277                 seq->curmap = seq->stdmap;
278                 seq->mark_deleted();
279                 return unknown;
280         }
281
282         Table::const_iterator end = table.end();
283         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
284                 KeyModifier mask = cit->mod.second;
285                 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
286
287                 if (cit->code == key && cit->mod.first == check) {
288                         // match found
289                         if (cit->table.get()) {
290                                 // this is a prefix key - set new map
291                                 seq->curmap = cit->table.get();
292                                 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
293                                 return prefix;
294                         } else {
295                                 // final key - reset map
296                                 seq->curmap = seq->stdmap;
297                                 seq->mark_deleted();
298                                 return cit->func;
299                         }
300                 }
301         }
302
303         // error - key not found:
304         seq->curmap = seq->stdmap;
305         seq->mark_deleted();
306
307         return unknown;
308 }
309
310
311 docstring const KeyMap::print(bool forgui) const
312 {
313         docstring buf;
314         Table::const_iterator end = table.end();
315         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
316                 buf += cit->code.print(cit->mod.first, forgui);
317                 buf += ' ';
318         }
319         return buf;
320 }
321
322
323 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
324 {
325         KeySymbol code = seq->sequence[r];
326         if (!code.isOK())
327                 return;
328
329         KeyModifier const mod1 = seq->modifiers[r].first;
330         KeyModifier const mod2 = seq->modifiers[r].second;
331
332         // check if key is already there
333         Table::iterator end = table.end();
334         for (Table::iterator it = table.begin(); it != end; ++it) {
335                 if (code == it->code
336                     && mod1 == it->mod.first
337                     && mod2 == it->mod.second) {
338                         // overwrite binding
339                         if (r + 1 == seq->length()) {
340                                 LYXERR(Debug::KBMAP, "Warning: New binding for '"
341                                         << to_utf8(seq->print(KeySequence::Portable))
342                                         << "' is overriding old binding...");
343                                 if (it->table.get()) {
344                                         it->table.reset();
345                                 }
346                                 it->func = func;
347                                 it->func.origin = FuncRequest::KEYBOARD;
348                                 return;
349                         } else if (!it->table.get()) {
350                                 lyxerr << "Error: New binding for '"
351                                        << to_utf8(seq->print(KeySequence::Portable))
352                                        << "' is overriding old binding..."
353                                                << endl;
354                                 return;
355                         } else {
356                                 it->table->bind(seq, func, r + 1);
357                                 return;
358                         }
359                 }
360         }
361
362         Table::iterator newone = table.insert(table.end(), Key());
363         newone->code = code;
364         newone->mod = seq->modifiers[r];
365         if (r + 1 == seq->length()) {
366                 newone->func = func;
367                 newone->func.origin = FuncRequest::KEYBOARD;
368                 newone->table.reset();
369         } else {
370                 newone->table.reset(new KeyMap);
371                 newone->table->bind(seq, func, r + 1);
372         }
373 }
374
375
376 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
377 {
378         KeySymbol code = seq->sequence[r];
379         if (!code.isOK())
380                 return;
381
382         KeyModifier const mod1 = seq->modifiers[r].first;
383         KeyModifier const mod2 = seq->modifiers[r].second;
384
385         // check if key is already there
386         Table::iterator end = table.end();
387         Table::iterator remove = end;
388         for (Table::iterator it = table.begin(); it != end; ++it) {
389                 if (code == it->code
390                     && mod1 == it->mod.first
391                     && mod2 == it->mod.second) {
392                         // remove
393                         if (r + 1 == seq->length()) {
394                                 if (it->func == func) {
395                                         remove = it;
396                                         if (it->table.get())
397                                                 it->table.reset();
398                                 }
399                         } else if (it->table.get()) {
400                                 it->table->unbind(seq, func, r + 1);
401                                 if (it->table->empty())
402                                         remove = it;
403                                 return;
404                         }
405                 }
406         }
407         if (remove != end) {
408                 table.erase(remove);
409                 return;
410         }
411 }
412
413
414 docstring KeyMap::printBindings(FuncRequest const & func) const
415 {
416         Bindings bindings = findBindings(func);
417         if (bindings.empty())
418                 return docstring();
419         
420         odocstringstream res;
421         Bindings::const_iterator cit = bindings.begin();
422         Bindings::const_iterator cit_end = bindings.end();
423         // prin the first item
424         res << cit->print(KeySequence::ForGui);
425         // more than one shortcuts?
426         for (++cit; cit != cit_end; ++cit)
427                 res << ", " << cit->print(KeySequence::ForGui);
428         return res.str();
429 }
430
431
432 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
433 {
434         return findBindings(func, KeySequence(0, 0));
435 }
436
437
438 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
439                         KeySequence const & prefix) const
440 {
441         Bindings res;
442         if (table.empty())
443                 return res;
444
445         Table::const_iterator end = table.end();
446         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
447                 if (cit->table.get()) {
448                         KeySequence seq = prefix;
449                         seq.addkey(cit->code, cit->mod.first);
450                         Bindings res2 = cit->table->findBindings(func, seq);
451                         res.insert(res.end(), res2.begin(), res2.end());
452                 } else if (cit->func == func) {
453                         KeySequence seq = prefix;
454                         seq.addkey(cit->code, cit->mod.first);
455                         res.push_back(seq);
456                 }
457         }
458
459         return res;
460 }
461
462
463 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
464 {
465         BindingList list;
466         listBindings(list, KeySequence(0, 0), tag);
467         if (unbound) {
468                 LyXAction::const_func_iterator fit = lyxaction.func_begin();
469                 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
470                 for (; fit != fit_end; ++fit) {
471                         FuncCode action = fit->second;
472                         bool has_action = false;
473                         BindingList::const_iterator it = list.begin();
474                         BindingList::const_iterator it_end = list.end();
475                         for (; it != it_end; ++it)
476                                 if (it->request.action == action) {
477                                         has_action = true;
478                                         break;
479                                 }
480                         if (!has_action)
481                                 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
482                 }       
483         }
484         return list;
485 }
486
487
488 void KeyMap::listBindings(BindingList & list,
489         KeySequence const & prefix, int tag) const
490 {
491         Table::const_iterator it = table.begin();
492         Table::const_iterator it_end = table.end();
493         for (; it != it_end; ++it) {
494                 // a LFUN_COMMAND_PREFIX
495                 if (it->table.get()) {
496                         KeySequence seq = prefix;
497                         seq.addkey(it->code, it->mod.first);
498                         it->table->listBindings(list, seq, tag);
499                 } else {
500                         KeySequence seq = prefix;
501                         seq.addkey(it->code, it->mod.first);
502                         list.push_back(Binding(it->func, seq, tag));
503                 }
504         }
505 }
506
507
508 } // namespace lyx