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