]> git.lyx.org Git - features.git/blob - src/KeyMap.cpp
simplify Lexer use a bit
[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 namespace {
121
122 enum BindTags {
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 }
135
136
137 bool KeyMap::read(string const & bind_file, KeyMap * unbind_map)
138 {
139         Lexer lexrc(bindTags);
140         if (lyxerr.debugging(Debug::PARSER))
141                 lexrc.printTable(lyxerr);
142
143         FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
144         lexrc.setFile(tmp);
145         if (!lexrc.isOK()) {
146                 lyxerr << "KeyMap::read: cannot open bind file:"
147                        << tmp << endl;
148                 return false;
149         }
150
151         LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
152
153         bool error = false;
154         while (lexrc.isOK()) {
155                 switch (lexrc.lex()) {
156                 case Lexer::LEX_UNDEF:
157                         lexrc.printError("Unknown tag `$$Token'");
158                         error = true;
159                         continue;
160                 case Lexer::LEX_FEOF:
161                         continue;
162                 case BN_BIND:
163                 {
164                         string seq, cmd;
165
166                         if (lexrc.next()) {
167                                 seq = lexrc.getString();
168                         } else {
169                                 lexrc.printError("BN_BIND: Missing key sequence");
170                                 error = true;
171                                 break;
172                         }
173
174                         if (lexrc.next(true)) {
175                                 cmd = lexrc.getString();
176                         } else {
177                                 lexrc.printError("BN_BIND: missing command");
178                                 error = true;
179                                 break;
180                         }
181
182                         FuncRequest func = lyxaction.lookupFunc(cmd);
183                         if (func. action == LFUN_UNKNOWN_ACTION) {
184                                 lexrc.printError("BN_BIND: Unknown LyX"
185                                                  " function `$$Token'");
186                                 error = true;
187                                 break;
188                         }
189
190                         bind(seq, func);
191                         break;
192                 }
193                 case BN_UNBIND:
194                 {
195                         string seq, cmd;
196
197                         if (lexrc.next()) {
198                                 seq = lexrc.getString();
199                         } else {
200                                 lexrc.printError("BN_UNBIND: Missing key sequence");
201                                 error = true;
202                                 break;
203                         }
204
205                         if (lexrc.next(true)) {
206                                 cmd = lexrc.getString();
207                         } else {
208                                 lexrc.printError("BN_UNBIND: missing command");
209                                 error = true;
210                                 break;
211                         }
212
213                         FuncRequest func = lyxaction.lookupFunc(cmd);
214                         if (func. action == LFUN_UNKNOWN_ACTION) {
215                                 lexrc.printError("BN_UNBIND: Unknown LyX"
216                                                  " function `$$Token'");
217                                 error = true;
218                                 break;
219                         }
220                         
221                         if (unbind_map)
222                                 unbind_map->bind(seq, func);
223                         else
224                                 unbind(seq, func);
225                         break;
226                 }
227                 case BN_BINDFILE:
228                         if (lexrc.next()) {
229                                 string const tmp(lexrc.getString());
230                                 error |= !read(tmp, unbind_map);
231                         } else {
232                                 lexrc.printError("BN_BINDFILE: Missing file name");
233                                 error = true;
234                                 break;
235
236                         }
237                         break;
238                 }
239         }
240
241         if (error)
242                 lyxerr << "KeyMap::read: error while reading bind file:"
243                        << tmp << endl;
244         return !error;
245 }
246
247
248 void KeyMap::write(string const & bind_file, bool append, bool unbind) const
249 {
250         ofstream os(bind_file.c_str(), 
251                 append ? (ios::app | ios::out) : ios::out);
252
253         if (!append)
254                 os << "## This file is automatically generated by lyx\n"
255                    << "## All modifications will be lost\n\n";
256         
257         string tag = unbind ? "\\unbind" : "\\bind";
258         BindingList const list = listBindings(false);
259         BindingList::const_iterator it = list.begin();
260         BindingList::const_iterator it_end = list.end();
261         for (; it != it_end; ++it) {
262                 FuncCode action = it->request.action;
263                 string arg = to_utf8(it->request.argument());
264
265                 os << tag << " \""
266                                 << to_utf8(it->sequence.print(KeySequence::BindFile))
267                                 << "\" \""
268                                 << lyxaction.getActionName(action)
269                                 << (arg.empty() ? "" : " ") << arg
270                                 << "\"\n";
271         }
272         os << "\n";
273         os.close();
274 }
275
276
277 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
278                   KeyModifier mod, KeySequence * seq) const
279 {
280         static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
281
282         if (table.empty()) {
283                 seq->curmap = seq->stdmap;
284                 seq->mark_deleted();
285                 return unknown;
286         }
287
288         Table::const_iterator end = table.end();
289         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
290                 KeyModifier mask = cit->mod.second;
291                 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
292
293                 if (cit->code == key && cit->mod.first == check) {
294                         // match found
295                         if (cit->table.get()) {
296                                 // this is a prefix key - set new map
297                                 seq->curmap = cit->table.get();
298                                 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
299                                 return prefix;
300                         } else {
301                                 // final key - reset map
302                                 seq->curmap = seq->stdmap;
303                                 seq->mark_deleted();
304                                 return cit->func;
305                         }
306                 }
307         }
308
309         // error - key not found:
310         seq->curmap = seq->stdmap;
311         seq->mark_deleted();
312
313         return unknown;
314 }
315
316
317 docstring const KeyMap::print(bool forgui) const
318 {
319         docstring buf;
320         Table::const_iterator end = table.end();
321         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
322                 buf += cit->code.print(cit->mod.first, forgui);
323                 buf += ' ';
324         }
325         return buf;
326 }
327
328
329 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
330 {
331         KeySymbol code = seq->sequence[r];
332         if (!code.isOK())
333                 return;
334
335         KeyModifier const mod1 = seq->modifiers[r].first;
336         KeyModifier const mod2 = seq->modifiers[r].second;
337
338         // check if key is already there
339         Table::iterator end = table.end();
340         for (Table::iterator it = table.begin(); it != end; ++it) {
341                 if (code == it->code
342                     && mod1 == it->mod.first
343                     && mod2 == it->mod.second) {
344                         // overwrite binding
345                         if (r + 1 == seq->length()) {
346                                 LYXERR(Debug::KBMAP, "Warning: New binding for '"
347                                         << to_utf8(seq->print(KeySequence::Portable))
348                                         << "' is overriding old binding...");
349                                 if (it->table.get()) {
350                                         it->table.reset();
351                                 }
352                                 it->func = func;
353                                 it->func.origin = FuncRequest::KEYBOARD;
354                                 return;
355                         } else if (!it->table.get()) {
356                                 lyxerr << "Error: New binding for '"
357                                        << to_utf8(seq->print(KeySequence::Portable))
358                                        << "' is overriding old binding..."
359                                                << endl;
360                                 return;
361                         } else {
362                                 it->table->bind(seq, func, r + 1);
363                                 return;
364                         }
365                 }
366         }
367
368         Table::iterator newone = table.insert(table.end(), Key());
369         newone->code = code;
370         newone->mod = seq->modifiers[r];
371         if (r + 1 == seq->length()) {
372                 newone->func = func;
373                 newone->func.origin = FuncRequest::KEYBOARD;
374                 newone->table.reset();
375         } else {
376                 newone->table.reset(new KeyMap);
377                 newone->table->bind(seq, func, r + 1);
378         }
379 }
380
381
382 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
383 {
384         KeySymbol code = seq->sequence[r];
385         if (!code.isOK())
386                 return;
387
388         KeyModifier const mod1 = seq->modifiers[r].first;
389         KeyModifier const mod2 = seq->modifiers[r].second;
390
391         // check if key is already there
392         Table::iterator end = table.end();
393         Table::iterator remove = end;
394         for (Table::iterator it = table.begin(); it != end; ++it) {
395                 if (code == it->code
396                     && mod1 == it->mod.first
397                     && mod2 == it->mod.second) {
398                         // remove
399                         if (r + 1 == seq->length()) {
400                                 if (it->func == func) {
401                                         remove = it;
402                                         if (it->table.get())
403                                                 it->table.reset();
404                                 }
405                         } else if (it->table.get()) {
406                                 it->table->unbind(seq, func, r + 1);
407                                 if (it->table->empty())
408                                         remove = it;
409                                 return;
410                         }
411                 }
412         }
413         if (remove != end) {
414                 table.erase(remove);
415                 return;
416         }
417 }
418
419
420 docstring KeyMap::printBindings(FuncRequest const & func) const
421 {
422         Bindings bindings = findBindings(func);
423         if (bindings.empty())
424                 return docstring();
425         
426         odocstringstream res;
427         Bindings::const_iterator cit = bindings.begin();
428         Bindings::const_iterator cit_end = bindings.end();
429         // prin the first item
430         res << cit->print(KeySequence::ForGui);
431         // more than one shortcuts?
432         for (++cit; cit != cit_end; ++cit)
433                 res << ", " << cit->print(KeySequence::ForGui);
434         return res.str();
435 }
436
437
438 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
439 {
440         return findBindings(func, KeySequence(0, 0));
441 }
442
443
444 KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
445                         KeySequence const & prefix) const
446 {
447         Bindings res;
448         if (table.empty())
449                 return res;
450
451         Table::const_iterator end = table.end();
452         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
453                 if (cit->table.get()) {
454                         KeySequence seq = prefix;
455                         seq.addkey(cit->code, cit->mod.first);
456                         Bindings res2 = cit->table->findBindings(func, seq);
457                         res.insert(res.end(), res2.begin(), res2.end());
458                 } else if (cit->func == func) {
459                         KeySequence seq = prefix;
460                         seq.addkey(cit->code, cit->mod.first);
461                         res.push_back(seq);
462                 }
463         }
464
465         return res;
466 }
467
468
469 KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
470 {
471         BindingList list;
472         listBindings(list, KeySequence(0, 0), tag);
473         if (unbound) {
474                 LyXAction::const_func_iterator fit = lyxaction.func_begin();
475                 LyXAction::const_func_iterator fit_end = lyxaction.func_end();
476                 for (; fit != fit_end; ++fit) {
477                         FuncCode action = fit->second;
478                         bool has_action = false;
479                         BindingList::const_iterator it = list.begin();
480                         BindingList::const_iterator it_end = list.end();
481                         for (; it != it_end; ++it)
482                                 if (it->request.action == action) {
483                                         has_action = true;
484                                         break;
485                                 }
486                         if (!has_action)
487                                 list.push_back(Binding(FuncRequest(action), KeySequence(0, 0), tag));
488                 }       
489         }
490         return list;
491 }
492
493
494 void KeyMap::listBindings(BindingList & list,
495         KeySequence const & prefix, int tag) const
496 {
497         Table::const_iterator it = table.begin();
498         Table::const_iterator it_end = table.end();
499         for (; it != it_end; ++it) {
500                 // a LFUN_COMMAND_PREFIX
501                 if (it->table.get()) {
502                         KeySequence seq = prefix;
503                         seq.addkey(it->code, it->mod.first);
504                         it->table->listBindings(list, seq, tag);
505                 } else {
506                         KeySequence seq = prefix;
507                         seq.addkey(it->code, it->mod.first);
508                         list.push_back(Binding(it->func, seq, tag));
509                 }
510         }
511 }
512
513
514 } // namespace lyx