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