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