]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
Enable the file dialog to open files with non-latin filenames.
[lyx.git] / src / kbmap.C
1 /**
2  * \file kbmap.C
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 "kbmap.h"
17
18 #include "debug.h"
19 #include "kbsequence.h"
20 #include "LyXAction.h"
21 #include "lyxlex.h"
22
23 #include "frontends/LyXKeySym.h"
24
25 #include "support/filetools.h"
26
27 using std::endl;
28 using lyx::support::i18nLibFileSearch;
29
30
31 string const kb_keymap::printKeysym(LyXKeySymPtr key,
32                                     key_modifier::state mod)
33 {
34         string buf;
35
36         string const s = key->getSymbolName();
37
38         if (mod & key_modifier::shift)
39                 buf += "S-";
40         if (mod & key_modifier::ctrl)
41                 buf += "C-";
42         if (mod & key_modifier::alt)
43                 buf += "M-";
44
45         buf += s;
46         return buf;
47 }
48
49
50 string const kb_keymap::printKey(kb_key const & key) const
51 {
52         return printKeysym(key.code, key.mod.first);
53 }
54
55
56 string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
57 {
58         if (lyxerr.debugging(Debug::KBMAP)) {
59                 lyxerr << "BIND: Sequence `"
60                        << seq << "' Action `"
61                        << func.action << '\'' << endl;
62         }
63
64         kb_sequence 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 kb_keymap::read(string const & bind_file)
95 {
96         const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
97
98         LyXLex lexrc(bindTags, bindCount);
99         if (lyxerr.debugging(Debug::PARSER))
100                 lexrc.printTable(lyxerr);
101
102         string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
103         lexrc.setFile(tmp);
104         if (!lexrc.isOK()) {
105                 lyxerr << "kb_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 (!error && lexrc.isOK()) {
114                 switch (lexrc.lex()) {
115                 case LyXLex::LEX_UNDEF:
116                         lexrc.printError("Unknown tag `$$Token'");
117                         error = true;
118                         continue;
119                 case LyXLex::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 << "kb_keymap::read: error while reading bind file:"
168                        << tmp << endl;
169         return !error;
170 }
171
172
173 FuncRequest const &
174 kb_keymap::lookup(LyXKeySymPtr key,
175                   key_modifier::state mod, kb_sequence * seq) const
176 {
177         static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
178
179         if (table.empty()) {
180                 seq->curmap = seq->stdmap;
181                 seq->mark_deleted();
182                 return unknown;
183         }
184
185         Table::const_iterator end = table.end();
186         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
187                 key_modifier::state mask(cit->mod.second);
188                 key_modifier::state check =
189                         static_cast<key_modifier::state>(mod & ~mask);
190
191                 if (*(cit->code) == *key && cit->mod.first == check) {
192                         // match found
193                         if (cit->table.get()) {
194                                 // this is a prefix key - set new map
195                                 seq->curmap = cit->table.get();
196                                 static FuncRequest prefix(LFUN_PREFIX);
197                                 return prefix;
198                         } else {
199                                 // final key - reset map
200                                 seq->curmap = seq->stdmap;
201                                 seq->mark_deleted();
202                                 return cit->func;
203                         }
204                 }
205         }
206
207         // error - key not found:
208         seq->curmap = seq->stdmap;
209         seq->mark_deleted();
210
211         return unknown;
212 }
213
214
215 string const kb_keymap::print() const
216 {
217         string buf;
218         Table::const_iterator end = table.end();
219         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
220                 buf += printKey((*cit));
221                 buf += ' ';
222         }
223         return buf;
224 }
225
226
227 void kb_keymap::defkey(kb_sequence * seq,
228                        FuncRequest const & func, unsigned int r)
229 {
230         LyXKeySymPtr code = seq->sequence[r];
231         if (!code->isOK())
232                 return;
233
234         key_modifier::state const mod1 = seq->modifiers[r].first;
235         key_modifier::state const mod2 = seq->modifiers[r].second;
236
237         // check if key is already there
238         Table::iterator end = table.end();
239         for (Table::iterator it = table.begin(); it != end; ++it) {
240                 if (*(code) == *(it->code)
241                     && mod1 == it->mod.first
242                     && mod2 == it->mod.second) {
243                         // overwrite binding
244                         if (r + 1 == seq->length()) {
245                                 lyxerr[Debug::KBMAP]
246                                         << "Warning: New binding for '"
247                                         << seq->print()
248                                         << "' is overriding old binding..."
249                                         << endl;
250                                 if (it->table.get()) {
251                                         it->table.reset();
252                                 }
253                                 it->func = func;
254                                 return;
255                         } else if (!it->table.get()) {
256                                 lyxerr << "Error: New binding for '" << seq->print()
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(), kb_key());
268         newone->code = code;
269         newone->mod = seq->modifiers[r];
270         if (r + 1 == seq->length()) {
271                 newone->func = func;
272                 newone->table.reset();
273                 return;
274         } else {
275                 newone->table.reset(new kb_keymap);
276                 newone->table->defkey(seq, func, r + 1);
277                 return;
278         }
279 }
280
281
282 string const kb_keymap::findbinding(FuncRequest const & func,
283                                     string const & prefix) const
284 {
285         string res;
286         if (table.empty()) return res;
287
288         Table::const_iterator end = table.end();
289         for (Table::const_iterator cit = table.begin();
290             cit != end; ++cit) {
291                 if (cit->table.get()) {
292                         res += cit->table->findbinding(func,
293                                                        prefix
294                                                        + printKey((*cit))
295                                                        + ' ');
296                 } else if (cit->func == func) {
297                         res += '[';
298                         res += prefix + printKey((*cit));
299                         res += "] ";
300                 }
301         }
302
303         return res;
304 }