]> git.lyx.org Git - features.git/blob - src/KeyMap.cpp
revert last patch. there's something wrong, possibly unrelated to this
[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 keyword_item 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         const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
140
141         Lexer lexrc(bindTags, bindCount);
142         if (lyxerr.debugging(Debug::PARSER))
143                 lexrc.printTable(lyxerr);
144
145         FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
146         lexrc.setFile(tmp);
147         if (!lexrc.isOK()) {
148                 lyxerr << "KeyMap::read: cannot open bind file:"
149                        << tmp << endl;
150                 return false;
151         }
152
153         LYXERR(Debug::KBMAP, "Reading bind file:" << tmp);
154
155         bool error = false;
156         while (lexrc.isOK()) {
157                 switch (lexrc.lex()) {
158                 case Lexer::LEX_UNDEF:
159                         lexrc.printError("Unknown tag `$$Token'");
160                         error = true;
161                         continue;
162                 case Lexer::LEX_FEOF:
163                         continue;
164                 case BN_BIND:
165                 {
166                         string seq, cmd;
167
168                         if (lexrc.next()) {
169                                 seq = lexrc.getString();
170                         } else {
171                                 lexrc.printError("BN_BIND: Missing key sequence");
172                                 error = true;
173                                 break;
174                         }
175
176                         if (lexrc.next(true)) {
177                                 cmd = lexrc.getString();
178                         } else {
179                                 lexrc.printError("BN_BIND: missing command");
180                                 error = true;
181                                 break;
182                         }
183
184                         FuncRequest func = lyxaction.lookupFunc(cmd);
185                         if (func. action == LFUN_UNKNOWN_ACTION) {
186                                 lexrc.printError("BN_BIND: Unknown LyX 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                 FuncCode 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                         FuncCode 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