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