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