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