]> git.lyx.org Git - lyx.git/blob - src/Trans.cpp
merge LyXTabular.{cpp,h} and insets/InsetTabular.{cpp,h}. More reorganization necessa...
[lyx.git] / src / Trans.cpp
1 /**
2  * \file Trans.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 Matthias Ettrich
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Trans.h"
15 #include "support/filetools.h"
16 #include "support/lstrings.h"
17 #include "Lexer.h"
18 #include "debug.h"
19 #include "TransState.h"
20
21
22 namespace lyx {
23
24 using support::contains;
25 using support::libFileSearch;
26
27 using std::endl;
28 using std::string;
29 using std::map;
30
31
32 // KmodInfo
33 KmodInfo::KmodInfo()
34 {
35 }
36
37
38 // Trans class
39
40 Trans::Trans()
41 {
42 }
43
44
45 Trans::~Trans()
46 {
47         freeKeymap();
48 }
49
50
51 void Trans::insertException(KmodException & exclist, char_type c,
52                             docstring const & data, bool flag, tex_accent accent)
53 {
54         Keyexc p;
55         p.c = c;
56         p.data = data;
57         p.combined = flag;
58         p.accent = accent;
59         exclist.insert(exclist.begin(), p);
60         // or just
61         // exclist.push_back(p);
62 }
63
64
65 void Trans::freeException(KmodException & exclist)
66 {
67         exclist.clear();
68 }
69
70
71 void Trans::freeKeymap()
72 {
73         kmod_list_.clear();
74         keymap_.clear();
75 }
76
77
78 bool Trans::isDefined() const
79 {
80         return !name_.empty();
81 }
82
83
84 string const & Trans::getName() const
85 {
86         return name_;
87 }
88
89
90 enum kmaptags_ {
91         KCOMB = 1,
92         KMOD,
93         KMAP,
94         KXMOD,
95         K_LAST
96 };
97
98
99 struct keyword_item kmapTags[K_LAST - 1] = {
100         {"\\kcomb", KCOMB },
101         { "\\kmap", KMAP },
102         { "\\kmod", KMOD },
103         { "\\kxmod", KXMOD }
104 };
105
106
107 tex_accent getkeymod(string const &);
108
109
110 void Trans::addDeadkey(tex_accent accent, docstring const & keys)
111 {
112         KmodInfo tmp;
113         tmp.data = keys;
114         tmp.accent = accent;
115         kmod_list_[accent] = tmp;
116
117         for (docstring::size_type i = 0; i < keys.length(); ++i) {
118                 // FIXME This is a hack.
119                 // tmp is no valid UCS4 string, but misused to store the
120                 // accent.
121                 docstring tmp;
122                 tmp += char_type(0);
123                 tmp += char_type(accent);
124                 keymap_[keys[i]] = tmp;
125         }
126 }
127
128
129 int Trans::load(Lexer & lex)
130 {
131         bool error = false;
132
133         while (lex.isOK() && !error) {
134                 switch (lex.lex()) {
135                 case KMOD:
136                 {
137                         LYXERR(Debug::KBMAP) << "KMOD:\t" << lex.getString() << endl;
138
139                         if (lex.next(true)) {
140                                 LYXERR(Debug::KBMAP) << "key\t`" << lex.getString()
141                                        << '\'' << endl;
142                         } else
143                                 return -1;
144
145                         docstring const keys = lex.getDocString();
146
147                         if (lex.next(true)) {
148                                 LYXERR(Debug::KBMAP) << "accent\t`" << lex.getString()
149                                                << '\'' << endl;
150                         } else
151                                 return -1;
152
153                         tex_accent accent = getkeymod(lex.getString());
154
155                         if (accent == TEX_NOACCENT)
156                                 return -1;
157
158 #if 1
159 //#warning This code should be removed...
160                         // But we need to fix up all the kmap files first
161                         // so that this field is not present anymore.
162                         if (lex.next(true)) {
163                                 LYXERR(Debug::KBMAP) << "allowed\t`" << lex.getString()
164                                                << '\'' << endl;
165                         } else
166                                 return -1;
167
168                         /* string const allowed = lex.getString(); */
169                         addDeadkey(accent, keys /*, allowed*/);
170 #else
171                         addDeadkey(accent, keys);
172 #endif
173                         break;
174                 }
175                 case KCOMB: {
176                         string str;
177
178                         LYXERR(Debug::KBMAP) << "KCOMB:" << endl;
179                         if (lex.next(true)) {
180                                 str = lex.getString();
181                                 LYXERR(Debug::KBMAP) << str << endl;
182                         } else
183                                 return -1;
184
185                         tex_accent accent_1 = getkeymod(str);
186                         if (accent_1 == TEX_NOACCENT) return -1;
187
188                         if (lex.next(true)) {
189                                 str = lex.getString();
190                                 LYXERR(Debug::KBMAP) << str << endl;
191                         } else
192                                 return -1;
193
194                         tex_accent accent_2= getkeymod(str);
195                         if (accent_2 == TEX_NOACCENT) return -1;
196
197                         map<tex_accent, KmodInfo>::iterator it1 =
198                                 kmod_list_.find(accent_1);
199                         map<tex_accent, KmodInfo>::iterator it2 =
200                                 kmod_list_.find(accent_2);
201                         if (it1 == kmod_list_.end()
202                             || it2 == kmod_list_.end()) {
203                                 return -1;
204                         }
205
206                         // Find what key accent_2 is on - should
207                         // check about accent_1 also
208                         map<char_type, docstring>::iterator it = keymap_.begin();
209                         map<char_type, docstring>::iterator end = keymap_.end();
210                         for (; it != end; ++it) {
211                                 if (!it->second.empty()
212                                     && it->second[0] == 0
213                                     && it->second[1] == accent_2)
214                                         break;
215                         }
216                         docstring allowed;
217                         if (lex.next()) {
218                                 allowed = lex.getDocString();
219                                 LYXERR(Debug::KBMAP) << "allowed: "
220                                                      << to_utf8(allowed) << endl;
221                         } else {
222                                 return -1;
223                         }
224
225                         insertException(kmod_list_[accent_1].exception_list,
226                                         it->first, allowed,
227                                         true, accent_2);
228                 }
229                 break;
230                 case KMAP: {
231                         unsigned char key_from;
232
233                         LYXERR(Debug::KBMAP) << "KMAP:\t" << lex.getString() << endl;
234
235                         if (lex.next(true)) {
236                                 key_from = lex.getString()[0];
237                                 LYXERR(Debug::KBMAP) << "\t`" << lex.getString() << '\''
238                                         << endl;
239                         } else
240                                 return -1;
241
242                         if (lex.next(true)) {
243                                 docstring const string_to = lex.getDocString();
244                                 keymap_[key_from] = string_to;
245                                 LYXERR(Debug::KBMAP) << "\t`" << to_utf8(string_to) << '\''
246                                         << endl;
247                         } else
248                                 return -1;
249
250                         break;
251                 }
252                 case KXMOD: {
253                         tex_accent accent;
254                         char_type key;
255                         docstring str;
256
257                         LYXERR(Debug::KBMAP) << "KXMOD:\t" << lex.getString() << endl;
258
259                         if (lex.next(true)) {
260                                 LYXERR(Debug::KBMAP) << "\t`" << lex.getString() << '\''
261                                         << endl;
262                                 accent = getkeymod(lex.getString());
263                         } else
264                                 return -1;
265
266                         if (lex.next(true)) {
267                                 LYXERR(Debug::KBMAP) << "\t`" << lex.getString() << '\''
268                                         << endl;
269                                 key = lex.getDocString()[0];
270                         } else
271                                 return -1;
272
273                         if (lex.next(true)) {
274                                 LYXERR(Debug::KBMAP) << "\t`" << lex.getString() << '\''
275                                         << endl;
276                                 str = lex.getDocString();
277                         } else
278                                 return -1;
279
280                         insertException(kmod_list_[accent].exception_list,
281                                         key, str);
282                         break;
283                 }
284                 case Lexer::LEX_FEOF:
285                         LYXERR(Debug::PARSER) << "End of parsing" << endl;
286                         break;
287                 default:
288                         lex.printError("ParseKeymapFile: "
289                                        "Unknown tag: `$$Token'");
290                         return -1;
291                 }
292         }
293         return 0;
294 }
295
296
297 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
298 {
299         map<tex_accent, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
300         if (cit != kmod_list_.end()) {
301                 i = cit->second;
302                 return true;
303         }
304         return false;
305 }
306
307
308 docstring const Trans::process(char_type c, TransManager & k)
309 {
310         docstring const t = match(c);
311
312         if (t.empty() && c != 0) {
313                 return k.normalkey(c);
314         } else if (!t.empty() && t[0] != 0) {
315                 //return k.normalkey(c);
316                 return t;
317         } else {
318                 return k.deadkey(c,
319                                  kmod_list_[static_cast<tex_accent>(t[1])]);
320         }
321 }
322
323
324 int Trans::load(string const & language)
325 {
326         support::FileName const filename = libFileSearch("kbd", language, "kmap");
327         if (filename.empty())
328                 return -1;
329
330         freeKeymap();
331         Lexer lex(kmapTags, K_LAST - 1);
332         lex.setFile(filename);
333
334         int const res = load(lex);
335
336         if (res == 0) {
337                 name_ = language;
338         } else
339                 name_.erase();
340
341         return res;
342 }
343
344
345 tex_accent getkeymod(string const & p)
346         /* return modifier - decoded from p and update p */
347 {
348         for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
349                 LYXERR(Debug::KBMAP) << "p = " << p
350                        << ", lyx_accent_table[" << i
351                        << "].name = `" << lyx_accent_table[i].name
352                        << '\'' << endl;
353
354                 if (lyx_accent_table[i].name
355                      && contains(p, lyx_accent_table[i].name)) {
356                         LYXERR(Debug::KBMAP) << "Found it!" << endl;
357                         return static_cast<tex_accent>(i);
358                 }
359         }
360         return TEX_NOACCENT;
361 }
362
363
364 } // namespace lyx