]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
zlib stuff
[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()) return false;
97
98         lyxerr[Debug::KBMAP] << "Reading bindfile:" << tmp << endl;
99
100         bool error = false;
101         while (lexrc.isOK()) {
102                 switch (lexrc.lex()) {
103                 case LyXLex::LEX_UNDEF:
104                         lexrc.printError("Unknown tag `$$Token'");
105                         continue;
106                 case LyXLex::LEX_FEOF:
107                         continue;
108                 case BN_BIND:
109                 {
110                         string seq, cmd;
111                         
112                         if (lexrc.next()) {
113                                 seq = lexrc.getString();
114                         } else {
115                                 lexrc.printError("BN_BIND: Missing key sequence");
116                                 break;
117                         }
118                         
119                         if (lexrc.next(true)) {
120                                 cmd = lexrc.getString();
121                         } else {
122                                 lexrc.printError("BN_BIND: missing command");
123                                 break;
124                         }
125                         
126                         int action = lyxaction.LookupFunc(cmd);
127                         if (!action == LFUN_UNKNOWN_ACTION) {
128                                 lexrc.printError("BN_BIND: Unknown LyX"
129                                                  " function `$$Token'");
130                                 break;
131                         }
132                         
133                         error = (bind(seq, kb_action(action)) != string::npos);
134                         break;
135                 }
136                 case BN_BINDFILE:
137                         if (lexrc.next()) {
138                                 string const tmp(lexrc.getString());
139                                 error = read(tmp);
140                         } else {
141                                 lexrc.printError("BN_BINDFILE: Missing file name");
142                                 error = true;
143                                 break;
144
145                         }
146                         break;
147                 }
148         }
149
150         if (error) {
151                 lyxerr << "Error reading bind file: " << tmp << endl;
152         }
153
154         return error;
155 }
156
157
158 int kb_keymap::lookup(LyXKeySymPtr key,
159                       key_modifier::state mod, kb_sequence * seq) const
160 {
161         if (table.empty()) {
162                 seq->curmap = seq->stdmap;
163                 seq->mark_deleted();
164                 return LFUN_UNKNOWN_ACTION;
165         }
166
167         Table::const_iterator end = table.end();
168         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
169                 key_modifier::state mask(cit->mod.second);
170                 key_modifier::state check =
171                         static_cast<key_modifier::state>(mod & ~mask);
172
173                 if (*(cit->code) == *key && cit->mod.first == check) {
174                         // match found
175                         if (cit->table.get()) {
176                                 // this is a prefix key - set new map
177                                 seq->curmap = cit->table.get();
178                                 return LFUN_PREFIX;
179                         } else {
180                                 // final key - reset map
181                                 seq->curmap = seq->stdmap;
182                                 seq->mark_deleted();
183                                 return cit->action;
184                         }
185                 }
186         }
187
188         // error - key not found:
189         seq->curmap = seq->stdmap;
190         seq->mark_deleted();
191         return LFUN_UNKNOWN_ACTION;
192 }
193
194
195 string const kb_keymap::print() const
196 {
197         string buf;
198         Table::const_iterator end = table.end();
199         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
200                 buf += printKey((*cit));
201                 buf += ' ';
202         }
203         return buf;
204 }
205
206
207 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
208 {
209         LyXKeySymPtr code = seq->sequence[r];
210         if (!code->isOK())
211                 return;
212
213         key_modifier::state const mod1 = seq->modifiers[r].first;
214         key_modifier::state const mod2 = seq->modifiers[r].second;
215
216         // check if key is already there
217         Table::iterator end = table.end();
218         for (Table::iterator it = table.begin(); it != end; ++it) {
219                 if (*(code) == *(it->code)
220                     && mod1 == it->mod.first
221                     && mod2 == it->mod.second) {
222                         // overwrite binding
223                         if (r + 1 == seq->length()) {
224                                 lyxerr[Debug::KBMAP]
225                                         << "Warning: New binding for '"
226                                         << seq->print()
227                                         << "' is overriding old binding..."
228                                         << endl;
229                                 if (it->table.get()) {
230                                         it->table.reset();
231                                 }
232                                 it->action = action;
233                                 return;
234                         } else if (!it->table.get()) {
235                                 lyxerr << "Error: New binding for '" << seq->print()
236                                        << "' is overriding old binding..."
237                                                << endl;
238                                 return;
239                         } else {
240                                 it->table->defkey(seq, action, r + 1);
241                                 return;
242                         }
243                 }
244         }
245
246         Table::iterator newone = table.insert(table.end(), kb_key());
247         newone->code = code;
248         newone->mod = seq->modifiers[r];
249         if (r + 1 == seq->length()) {
250                 newone->action = action;
251                 newone->table.reset();
252                 return;
253         } else {
254                 newone->table.reset(new kb_keymap);
255                 newone->table->defkey(seq, action, r + 1);
256                 return;
257         }
258 }
259
260
261 string const kb_keymap::findbinding(int act, string const & prefix) const
262 {
263         string res;
264         if (table.empty()) return res;
265
266         Table::const_iterator end = table.end();
267         for (Table::const_iterator cit = table.begin();
268             cit != end; ++cit) {
269                 if (cit->table.get()) {
270                         res += cit->table->findbinding(act,
271                                                        prefix
272                                                        + printKey((*cit))
273                                                        + ' ');
274                 } else if (cit->action == act) {
275                         res += '[';
276                         res += prefix + printKey((*cit));
277                         res += "] ";
278                 }
279         }
280         return res;
281 }