]> git.lyx.org Git - features.git/blob - src/KeyMap.cpp
Save a few bytes.
[features.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         if (table.empty()) {
266                 seq->curmap = seq->stdmap;
267                 seq->mark_deleted();
268                 return FuncRequest::unknown;
269         }
270
271         Table::const_iterator end = table.end();
272         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
273                 KeyModifier mask = cit->mod.second;
274                 KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
275
276                 if (cit->code == key && cit->mod.first == check) {
277                         // match found
278                         if (cit->table.get()) {
279                                 // this is a prefix key - set new map
280                                 seq->curmap = cit->table.get();
281                                 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
282                                 return prefix;
283                         } else {
284                                 // final key - reset map
285                                 seq->curmap = seq->stdmap;
286                                 seq->mark_deleted();
287                                 return cit->func;
288                         }
289                 }
290         }
291
292         // error - key not found:
293         seq->curmap = seq->stdmap;
294         seq->mark_deleted();
295
296         return FuncRequest::unknown;
297 }
298
299
300 docstring const KeyMap::print(bool forgui) const
301 {
302         docstring buf;
303         Table::const_iterator end = table.end();
304         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
305                 buf += cit->code.print(cit->mod.first, forgui);
306                 buf += ' ';
307         }
308         return buf;
309 }
310
311
312 void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
313 {
314         KeySymbol code = seq->sequence[r];
315         if (!code.isOK())
316                 return;
317
318         KeyModifier const mod1 = seq->modifiers[r].first;
319         KeyModifier const mod2 = seq->modifiers[r].second;
320
321         // check if key is already there
322         Table::iterator end = table.end();
323         for (Table::iterator it = table.begin(); it != end; ++it) {
324                 if (code == it->code
325                     && mod1 == it->mod.first
326                     && mod2 == it->mod.second) {
327                         // overwrite binding
328                         if (r + 1 == seq->length()) {
329                                 LYXERR(Debug::KBMAP, "Warning: New binding for '"
330                                         << to_utf8(seq->print(KeySequence::Portable))
331                                         << "' is overriding old binding...");
332                                 if (it->table.get()) {
333                                         it->table.reset();
334                                 }
335                                 it->func = func;
336                                 it->func.origin = FuncRequest::KEYBOARD;
337                                 return;
338                         } else if (!it->table.get()) {
339                                 lyxerr << "Error: New binding for '"
340                                        << to_utf8(seq->print(KeySequence::Portable))
341                                        << "' is overriding old binding..."
342                                                << endl;
343                                 return;
344                         } else {
345                                 it->table->bind(seq, func, r + 1);
346                                 return;
347                         }
348                 }
349         }
350
351         Table::iterator newone = table.insert(table.end(), Key());
352         newone->code = code;
353         newone->mod = seq->modifiers[r];
354         if (r + 1 == seq->length()) {
355                 newone->func = func;
356                 newone->func.origin = FuncRequest::KEYBOARD;
357                 newone->table.reset();
358         } else {
359                 newone->table.reset(new KeyMap);
360                 newone->table->bind(seq, func, r + 1);
361         }
362 }
363
364
365 void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
366 {
367         KeySymbol code = seq->sequence[r];
368         if (!code.isOK())
369                 return;
370
371         KeyModifier const mod1 = seq->modifiers[r].first;
372         KeyModifier const mod2 = seq->modifiers[r].second;
373
374         // check if key is already there
375         Table::iterator end = table.end();
376         Table::iterator remove = end;
377         for (Table::iterator it = table.begin(); it != end; ++it) {
378                 if (code == it->code
379                     && mod1 == it->mod.first
380                     && mod2 == it->mod.second) {
381                         // remove
382                         if (r + 1 == seq->length()) {
383                                 if (it->func == func) {
384                                         remove = it;
385                                         if (it->table.get())
386                                                 it->table.reset();
387                                 }
388                         } else if (it->table.get()) {
389                                 it->table->unbind(seq, func, r + 1);
390                                 if (it->table->empty())
391                                         remove = it;
392                                 return;
393                         }
394                 }
395         }
396         if (remove != end)
397                 table.erase(remove);
398 }
399
400
401 docstring KeyMap::printBindings(FuncRequest const & func) 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         // prin the first item
411         res << cit->print(KeySequence::ForGui);
412         // more than one shortcuts?
413         for (++cit; cit != cit_end; ++cit)
414                 res << ", " << cit->print(KeySequence::ForGui);
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