]> git.lyx.org Git - lyx.git/blob - src/KeyMap.cpp
69aa1ec60588ac58bab3eb76dccd3fc599e2ad09
[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 "debug.h"
19 #include "KeySequence.h"
20 #include "LyXAction.h"
21 #include "Lexer.h"
22
23 #include "frontends/KeySymbol.h"
24
25 #include "support/filetools.h"
26
27 #include <sstream>
28
29 using std::endl;
30 using std::string;
31
32
33 namespace lyx {
34
35 using support::FileName;
36 using support::i18nLibFileSearch;
37
38
39 string const KeyMap::printKeySym(KeySymbol const & key,
40                                     key_modifier::state mod)
41 {
42         string buf;
43
44         string const s = key.getSymbolName();
45
46         if (mod & key_modifier::shift)
47                 buf += "S-";
48         if (mod & key_modifier::ctrl)
49                 buf += "C-";
50         if (mod & key_modifier::alt)
51                 buf += "M-";
52
53         buf += s;
54         return buf;
55 }
56
57
58 size_t KeyMap::bind(string const & seq, FuncRequest const & func)
59 {
60         LYXERR(Debug::KBMAP) << "BIND: Sequence `"
61                << seq << "' Action `"
62                << func.action << '\'' << endl;
63
64         KeySequence k(0, 0);
65
66         string::size_type const res = k.parse(seq);
67         if (res == string::npos) {
68                 defkey(&k, func);
69         } else {
70                 LYXERR(Debug::KBMAP) << "Parse error at position " << res
71                                      << " in key sequence '" << seq << "'."
72                                      << endl;
73         }
74
75         return res;
76 }
77
78
79 namespace {
80
81 enum BindTags {
82         BN_BIND,
83         BN_BINDFILE
84 };
85
86 keyword_item bindTags[] = {
87         { "\\bind", BN_BIND },
88         { "\\bind_file", BN_BINDFILE }
89 };
90
91 }
92
93
94 bool KeyMap::read(string const & bind_file)
95 {
96         const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
97
98         Lexer lexrc(bindTags, bindCount);
99         if (lyxerr.debugging(Debug::PARSER))
100                 lexrc.printTable(lyxerr);
101
102         FileName const tmp(i18nLibFileSearch("bind", bind_file, "bind"));
103         lexrc.setFile(tmp);
104         if (!lexrc.isOK()) {
105                 lyxerr << "KeyMap::read: cannot open bind file:"
106                        << tmp << endl;
107                 return false;
108         }
109
110         LYXERR(Debug::KBMAP) << "Reading bind file:" << tmp << endl;
111
112         bool error = false;
113         while (lexrc.isOK()) {
114                 switch (lexrc.lex()) {
115                 case Lexer::LEX_UNDEF:
116                         lexrc.printError("Unknown tag `$$Token'");
117                         error = true;
118                         continue;
119                 case Lexer::LEX_FEOF:
120                         continue;
121                 case BN_BIND:
122                 {
123                         string seq, cmd;
124
125                         if (lexrc.next()) {
126                                 seq = lexrc.getString();
127                         } else {
128                                 lexrc.printError("BN_BIND: Missing key sequence");
129                                 error = true;
130                                 break;
131                         }
132
133                         if (lexrc.next(true)) {
134                                 cmd = lexrc.getString();
135                         } else {
136                                 lexrc.printError("BN_BIND: missing command");
137                                 error = true;
138                                 break;
139                         }
140
141                         FuncRequest func = lyxaction.lookupFunc(cmd);
142                         if (func. action == LFUN_UNKNOWN_ACTION) {
143                                 lexrc.printError("BN_BIND: Unknown LyX"
144                                                  " function `$$Token'");
145                                 error = true;
146                                 break;
147                         }
148
149                         bind(seq, func);
150                         break;
151                 }
152                 case BN_BINDFILE:
153                         if (lexrc.next()) {
154                                 string const tmp(lexrc.getString());
155                                 error |= !read(tmp);
156                         } else {
157                                 lexrc.printError("BN_BINDFILE: Missing file name");
158                                 error = true;
159                                 break;
160
161                         }
162                         break;
163                 }
164         }
165
166         if (error)
167                 lyxerr << "KeyMap::read: error while reading bind file:"
168                        << tmp << endl;
169         return !error;
170 }
171
172
173 FuncRequest const & KeyMap::lookup(KeySymbol const &key,
174                   key_modifier::state mod, KeySequence * seq) const
175 {
176         static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
177
178         if (table.empty()) {
179                 seq->curmap = seq->stdmap;
180                 seq->mark_deleted();
181                 return unknown;
182         }
183
184         Table::const_iterator end = table.end();
185         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
186                 key_modifier::state mask(cit->mod.second);
187                 key_modifier::state check =
188                         static_cast<key_modifier::state>(mod & ~mask);
189
190                 if (cit->code == key && cit->mod.first == check) {
191                         // match found
192                         if (cit->table.get()) {
193                                 // this is a prefix key - set new map
194                                 seq->curmap = cit->table.get();
195                                 static FuncRequest prefix(LFUN_COMMAND_PREFIX);
196                                 return prefix;
197                         } else {
198                                 // final key - reset map
199                                 seq->curmap = seq->stdmap;
200                                 seq->mark_deleted();
201                                 return cit->func;
202                         }
203                 }
204         }
205
206         // error - key not found:
207         seq->curmap = seq->stdmap;
208         seq->mark_deleted();
209
210         return unknown;
211 }
212
213
214 docstring const KeyMap::print(bool forgui) const
215 {
216         docstring buf;
217         Table::const_iterator end = table.end();
218         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
219                 buf += cit->code.print(cit->mod.first, forgui);
220                 buf += ' ';
221         }
222         return buf;
223 }
224
225
226 void KeyMap::defkey(KeySequence * seq, FuncRequest const & func, unsigned int r)
227 {
228         KeySymbol code = seq->sequence[r];
229         if (!code.isOK())
230                 return;
231
232         key_modifier::state const mod1 = seq->modifiers[r].first;
233         key_modifier::state const mod2 = seq->modifiers[r].second;
234
235         // check if key is already there
236         Table::iterator end = table.end();
237         for (Table::iterator it = table.begin(); it != end; ++it) {
238                 if (code == it->code
239                     && mod1 == it->mod.first
240                     && mod2 == it->mod.second) {
241                         // overwrite binding
242                         if (r + 1 == seq->length()) {
243                                 LYXERR(Debug::KBMAP)
244                                         << "Warning: New binding for '"
245                                         << to_utf8(seq->print(false))
246                                         << "' is overriding old binding..."
247                                         << endl;
248                                 if (it->table.get()) {
249                                         it->table.reset();
250                                 }
251                                 it->func = func;
252                                 it->func.origin = FuncRequest::KEYBOARD;
253                                 return;
254                         } else if (!it->table.get()) {
255                                 lyxerr << "Error: New binding for '"
256                                        << to_utf8(seq->print(false))
257                                        << "' is overriding old binding..."
258                                                << endl;
259                                 return;
260                         } else {
261                                 it->table->defkey(seq, func, r + 1);
262                                 return;
263                         }
264                 }
265         }
266
267         Table::iterator newone = table.insert(table.end(), Key());
268         newone->code = code;
269         newone->mod = seq->modifiers[r];
270         if (r + 1 == seq->length()) {
271                 newone->func = func;
272                 newone->func.origin = FuncRequest::KEYBOARD;
273                 newone->table.reset();
274         } else {
275                 newone->table.reset(new KeyMap);
276                 newone->table->defkey(seq, func, r + 1);
277         }
278 }
279
280
281 docstring const KeyMap::printbindings(FuncRequest const & func) const
282 {
283         odocstringstream res;
284         Bindings bindings = findbindings(func);
285         for (Bindings::const_iterator cit = bindings.begin();
286              cit != bindings.end() ; ++cit)
287                 res << '[' << cit->print(true) << ']';
288         return res.str();
289 }
290
291
292 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func) const
293 {
294         return findbindings(func, KeySequence(0, 0));
295 }
296
297
298 KeyMap::Bindings KeyMap::findbindings(FuncRequest const & func,
299                         KeySequence const & prefix) const
300 {
301         Bindings res;
302         if (table.empty()) return res;
303
304         Table::const_iterator end = table.end();
305         for (Table::const_iterator cit = table.begin();
306             cit != end; ++cit) {
307                 if (cit->table.get()) {
308                         KeySequence seq = prefix;
309                         seq.addkey(cit->code, cit->mod.first);
310                         Bindings res2 =
311                                 cit->table->findbindings(func, seq);
312                         res.insert(res.end(), res2.begin(), res2.end());
313                 } else if (cit->func == func) {
314                         KeySequence seq = prefix;
315                         seq.addkey(cit->code, cit->mod.first);
316                         res.push_back(seq);
317                 }
318         }
319
320         return res;
321 }
322
323
324 } // namespace lyx