]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
More 'standard conformant blurb' nonsense.
[lyx.git] / src / kbmap.C
1 /**
2  * \file kbmap.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "kbmap.h"
13 #include "lfuns.h"
14 #include "kbsequence.h"
15 #include "LyXAction.h"
16 #include "support/filetools.h"
17 #include "lyxlex.h"
18 #include "debug.h"
19
20 using std::endl;
21 using lyx::support::i18nLibFileSearch;
22
23 string const kb_keymap::printKeysym(LyXKeySymPtr key,
24                                     key_modifier::state mod)
25 {
26         string buf;
27
28         string const s = key->getSymbolName();
29
30         if (mod & key_modifier::shift)
31                 buf += "S-";
32         if (mod & key_modifier::ctrl)
33                 buf += "C-";
34         if (mod & key_modifier::alt)
35                 buf += "M-";
36
37         buf += s;
38         return buf;
39 }
40
41
42 string const kb_keymap::printKey(kb_key const & key) const
43 {
44         return printKeysym(key.code, key.mod.first);
45 }
46
47
48 string::size_type kb_keymap::bind(string const & seq, int action)
49 {
50         if (lyxerr.debugging(Debug::KBMAP)) {
51                 lyxerr << "BIND: Sequence `"
52                        << seq << "' Action `"
53                        << action << '\'' << endl;
54         }
55
56         kb_sequence k(0, 0);
57
58         string::size_type const res = k.parse(seq);
59         if (res == string::npos) {
60                 defkey(&k, action);
61         } else {
62                 lyxerr[Debug::KBMAP] << "Parse error at position " << res
63                                      << " in key sequence '" << seq << "'."
64                                      << endl;
65         }
66
67         return res;
68 }
69
70
71 namespace {
72
73 enum BindTags {
74         BN_BIND,
75         BN_BINDFILE
76 };
77
78 keyword_item bindTags[] = {
79         { "\\bind", BN_BIND },
80         { "\\bind_file", BN_BINDFILE }
81 };
82
83 }
84
85
86 bool kb_keymap::read(string const & bind_file) 
87 {
88         const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
89
90         LyXLex lexrc(bindTags, bindCount);
91         if (lyxerr.debugging(Debug::PARSER))
92                 lexrc.printTable(lyxerr);
93
94         string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
95         lexrc.setFile(tmp);
96         if (!lexrc.isOK()) {
97                 lyxerr << "kb_keymap::read: cannot open bind file:" 
98                        << tmp << endl;
99                 return false;
100         }
101
102         lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
103
104         bool error = false;
105         while (!error && lexrc.isOK()) {
106                 switch (lexrc.lex()) {
107                 case LyXLex::LEX_UNDEF:
108                         lexrc.printError("Unknown tag `$$Token'");
109                         error = true;
110                         continue;
111                 case LyXLex::LEX_FEOF:
112                         continue;
113                 case BN_BIND:
114                 {
115                         string seq, cmd;
116                         
117                         if (lexrc.next()) {
118                                 seq = lexrc.getString();
119                         } else {
120                                 lexrc.printError("BN_BIND: Missing key sequence");
121                                 error = true;
122                                 break;
123                         }
124                         
125                         if (lexrc.next(true)) {
126                                 cmd = lexrc.getString();
127                         } else {
128                                 lexrc.printError("BN_BIND: missing command");
129                                 error = true;
130                                 break;
131                         }
132                         
133                         int action = lyxaction.LookupFunc(cmd);
134                         if (!action == LFUN_UNKNOWN_ACTION) {
135                                 lexrc.printError("BN_BIND: Unknown LyX"
136                                                  " function `$$Token'");
137                                 error = true;
138                                 break;
139                         }
140                         
141                         bind(seq, kb_action(action));
142                         break;
143                 }
144                 case BN_BINDFILE:
145                         if (lexrc.next()) {
146                                 string const tmp(lexrc.getString());
147                                 error = !read(tmp);
148                         } else {
149                                 lexrc.printError("BN_BINDFILE: Missing file name");
150                                 error = true;
151                                 break;
152
153                         }
154                         break;
155                 }
156         }
157
158         if (error)
159                 lyxerr << "kb_keymap::read: error while reading bind file:" 
160                        << tmp << endl;
161         return !error;
162 }
163
164
165 int kb_keymap::lookup(LyXKeySymPtr key,
166                       key_modifier::state mod, kb_sequence * seq) const
167 {
168         if (table.empty()) {
169                 seq->curmap = seq->stdmap;
170                 seq->mark_deleted();
171                 return LFUN_UNKNOWN_ACTION;
172         }
173
174         Table::const_iterator end = table.end();
175         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
176                 key_modifier::state mask(cit->mod.second);
177                 key_modifier::state check =
178                         static_cast<key_modifier::state>(mod & ~mask);
179
180                 if (*(cit->code) == *key && cit->mod.first == check) {
181                         // match found
182                         if (cit->table.get()) {
183                                 // this is a prefix key - set new map
184                                 seq->curmap = cit->table.get();
185                                 return LFUN_PREFIX;
186                         } else {
187                                 // final key - reset map
188                                 seq->curmap = seq->stdmap;
189                                 seq->mark_deleted();
190                                 return cit->action;
191                         }
192                 }
193         }
194
195         // error - key not found:
196         seq->curmap = seq->stdmap;
197         seq->mark_deleted();
198         return LFUN_UNKNOWN_ACTION;
199 }
200
201
202 string const kb_keymap::print() const
203 {
204         string buf;
205         Table::const_iterator end = table.end();
206         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
207                 buf += printKey((*cit));
208                 buf += ' ';
209         }
210         return buf;
211 }
212
213
214 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
215 {
216         LyXKeySymPtr code = seq->sequence[r];
217         if (!code->isOK())
218                 return;
219
220         key_modifier::state const mod1 = seq->modifiers[r].first;
221         key_modifier::state const mod2 = seq->modifiers[r].second;
222
223         // check if key is already there
224         Table::iterator end = table.end();
225         for (Table::iterator it = table.begin(); it != end; ++it) {
226                 if (*(code) == *(it->code)
227                     && mod1 == it->mod.first
228                     && mod2 == it->mod.second) {
229                         // overwrite binding
230                         if (r + 1 == seq->length()) {
231                                 lyxerr[Debug::KBMAP]
232                                         << "Warning: New binding for '"
233                                         << seq->print()
234                                         << "' is overriding old binding..."
235                                         << endl;
236                                 if (it->table.get()) {
237                                         it->table.reset();
238                                 }
239                                 it->action = action;
240                                 return;
241                         } else if (!it->table.get()) {
242                                 lyxerr << "Error: New binding for '" << seq->print()
243                                        << "' is overriding old binding..."
244                                                << endl;
245                                 return;
246                         } else {
247                                 it->table->defkey(seq, action, r + 1);
248                                 return;
249                         }
250                 }
251         }
252
253         Table::iterator newone = table.insert(table.end(), kb_key());
254         newone->code = code;
255         newone->mod = seq->modifiers[r];
256         if (r + 1 == seq->length()) {
257                 newone->action = action;
258                 newone->table.reset();
259                 return;
260         } else {
261                 newone->table.reset(new kb_keymap);
262                 newone->table->defkey(seq, action, r + 1);
263                 return;
264         }
265 }
266
267
268 string const kb_keymap::findbinding(int act, string const & prefix) const
269 {
270         string res;
271         if (table.empty()) return res;
272
273         Table::const_iterator end = table.end();
274         for (Table::const_iterator cit = table.begin();
275             cit != end; ++cit) {
276                 if (cit->table.get()) {
277                         res += cit->table->findbinding(act,
278                                                        prefix
279                                                        + printKey((*cit))
280                                                        + ' ');
281                 } else if (cit->action == act) {
282                         res += '[';
283                         res += prefix + printKey((*cit));
284                         res += "] ";
285                 }
286         }
287         return res;
288 }