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