]> git.lyx.org Git - features.git/blob - src/trans.C
b738b63e9e39dad2809cc992fc3f618a5f76a2f3
[features.git] / src / trans.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation "trans.h"
5 #endif
6
7 #include "LyXView.h"
8 #include "trans.h"
9 #include "support/filetools.h"
10 #include "tex-strings.h"
11 #include "lyxlex.h"
12 #include "debug.h"
13 #include "trans_mgr.h"
14
15
16 // KmodInfo
17 KmodInfo::KmodInfo()
18 {
19         exception_list = 0;
20 }
21
22
23 // Default Trans
24 bool DefaultTrans::init_ = false;
25
26
27 DefaultTrans::DefaultTrans()
28 {
29         if (init_ == false) {
30                 // Do initialization
31                 init_ = true;
32         }
33 }
34
35
36 string DefaultTrans::process(char c, TransManager & k)
37 {
38         char dummy[2] = "?";
39         dummy[0] = c;
40     
41         return k.normalkey(c, dummy);
42 }
43
44
45 // Trans class
46
47 Trans::Trans()
48 {
49         int i;
50
51         for(i = 0; i < 256; ++i)
52                 keymap_[i] = 0;
53
54         for(i = 0; i < TEX_MAX_ACCENT + 1; ++i)
55                 kmod_list_[i] = 0;
56 }
57
58
59 Trans::~Trans()
60 {
61         FreeKeymap();
62 }
63
64
65 void Trans::InsertException(Trans::keyexc & exclist, char c,
66                             string const & data, bool flag, tex_accent accent)
67 {
68         keyexc p;
69
70         p = new Keyexc; 
71         p->next = exclist;
72         p->c = c;
73
74         p->data = data;
75         p->combined = flag;
76         p->accent = accent;
77
78         exclist = p;
79 }
80
81
82 void Trans::FreeException(Trans::keyexc & exclist)
83 {
84         Trans::keyexc p;
85
86         p = exclist;
87         while (p) {
88                 p = exclist->next;
89                 delete exclist;
90                 exclist = p;
91         }
92 }
93
94
95 void Trans::FreeKeymap()
96 {
97         int i;
98
99         for(i = 0; i < 256; ++i)
100                 if (keymap_[i]) {
101                         delete keymap_[i];
102                         keymap_[i] = 0;
103                 }
104         for(i = 0; i < TEX_MAX_ACCENT + 1; ++i)
105                 if (kmod_list_[i]) {
106                         FreeException(kmod_list_[i]->exception_list);
107                         delete kmod_list_[i];
108                         kmod_list_[i] = 0;
109                 }
110 }
111
112
113 bool Trans::IsDefined()
114 {
115         return !name_.empty();
116 }
117
118
119 string const & Trans::GetName()
120 {
121         return name_;
122 }
123
124
125 enum _kmaptags {
126         KCOMB = 1,
127         KMOD,
128         KMAP,
129         KXMOD,
130         K_LAST
131 };
132
133
134 struct keyword_item kmapTags[K_LAST-1] = {
135         {"\\kcomb", KCOMB },
136         { "\\kmap", KMAP },
137         { "\\kmod", KMOD },
138         { "\\kxmod", KXMOD }
139 };
140
141
142 tex_accent getkeymod(string const &);
143
144
145 void Trans::AddDeadkey(tex_accent accent, string const & keys,
146                        string const & allowed)
147 {
148         if (kmod_list_[accent]) {
149                 FreeException(kmod_list_[accent]->exception_list);
150                 
151                 delete kmod_list_[accent];
152         }
153         
154         kmod_list_[accent] = new kmod_list_decl;
155         kmod_list_[accent]->data = keys;
156         kmod_list_[accent]->accent = accent;
157         if (allowed == "all") { 
158                 kmod_list_[accent]->allowed= lyx_accent_table[accent].native;
159         } else { 
160                 kmod_list_[accent]->allowed = allowed;
161         }
162         
163         for(string::size_type i = 0; i < keys.length(); ++i) {
164                 char * temp =
165                         keymap_[static_cast<unsigned char>(keys[i])] =
166                         new char[2];
167                 temp[0] = 0; temp[1] = accent;
168         }
169         kmod_list_[accent]->exception_list = 0;
170 }
171
172
173 int Trans::Load(LyXLex & lex)
174 {
175         bool error = false;
176
177         while (lex.IsOK() && !error) {
178                 switch(lex.lex()) {
179                 case KMOD:
180                 {
181                         if (lyxerr.debugging(Debug::KBMAP))
182                                 lyxerr << "KMOD:\t" << lex.text() << endl;
183                         if (lex.next(true)) {
184                                 if (lyxerr.debugging(Debug::KBMAP))
185                                         lyxerr << "key\t`" << lex.text()
186                                                << "'" << endl;
187                         } else
188                                 return -1;
189                         
190                         string keys = lex.GetString();
191
192                         if (lex.next(true)) {
193                                 if ( lyxerr.debugging(Debug::KBMAP))
194                                         lyxerr << "accent\t`" << lex.text()
195                                                << "'" << endl;
196                         } else
197                                 return -1;
198
199                         tex_accent accent = getkeymod(lex.GetString());
200
201                         if (accent == TEX_NOACCENT)
202                                 return -1;
203
204                         if (lex.next(true)) {
205                                 if (lyxerr.debugging(Debug::KBMAP))
206                                         lyxerr << "allowed\t`" << lex.text()
207                                                << "'" << endl;
208                         } else
209                                 return -1;
210
211                         string allowed = lex.GetString();
212
213                         AddDeadkey(accent, keys, allowed);
214                         break;
215                 }       
216                 case KCOMB: {
217                         char const * str;
218
219                         lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
220                         if (lex.next(true)) {
221                                 str= lex.text();
222                                 lyxerr[Debug::KBMAP] << str << endl;
223                         } else
224                                 return -1;
225                         
226                         tex_accent accent_1 = getkeymod(str);
227                         if (accent_1 == TEX_NOACCENT) return -1;
228
229                         if (lex.next(true)) {
230                                 str = lex.text();
231                                 lyxerr[Debug::KBMAP] << str << endl;
232                         } else
233                                 return -1;
234
235                         tex_accent accent_2= getkeymod(str);
236                         if (accent_2 == TEX_NOACCENT) return -1;
237
238                         if (kmod_list_[accent_1] == 0 || kmod_list_[accent_2] == 0)
239                                 return -1;
240
241                         // Find what key accent_2 is on - should
242                         // check about accent_1 also
243                         int key = 0;
244                         for(; key < 256; ++key) {
245                                 if (keymap_[key] && keymap_[key][0] == 0
246                                     && keymap_[key][1] == accent_2)
247                                         break;
248                         }
249                         string allowed;
250
251                         if (lex.next()) {
252                                 allowed = lex.GetString();
253                                 lyxerr[Debug::KBMAP] << "allowed: "
254                                                      << allowed << endl;
255                         } else
256                                 return -1;
257
258                         InsertException(kmod_list_[accent_1]->exception_list,
259                                         static_cast<char>(key), allowed,
260                                         true, accent_2);
261                 }
262                 break;
263                 case KMAP: {
264                         unsigned char key_from;
265                         char * string_to;
266
267                         if (lyxerr.debugging(Debug::KBMAP))
268                                 lyxerr << "KMAP:\t" << lex.text() << endl;
269                         if (lex.next(true)) {
270                                 key_from= lex.text()[0];
271                                 if (lyxerr.debugging(Debug::KBMAP))
272                                         lyxerr << "\t`" << lex.text() << "'"
273                                                << endl;
274                         } else
275                                 return -1;
276
277                         if (lex.next(true)) {
278                                 char const * t = lex.text();
279                                 string_to = strcpy(new char[strlen(t)+1], t);
280                                 keymap_[key_from] = string_to;
281                                 if (lyxerr.debugging(Debug::KBMAP))
282                                         lyxerr << "\t`" << string_to << "'"
283                                                << endl;
284                         } else
285                                 return -1;
286
287                         break;
288                 }
289                 case KXMOD: {
290                         tex_accent accent;
291                         char key;
292                         char const * str;
293
294                         if (lyxerr.debugging(Debug::KBMAP))
295                                 lyxerr << "KXMOD:\t" << lex.text() << endl;
296                         if (lex.next(true)) {
297                                 if (lyxerr.debugging(Debug::KBMAP))
298                                         lyxerr << "\t`" << lex.text() << "'"
299                                                << endl;
300                                 accent = getkeymod(lex.GetString());
301                         } else
302                                 return -1;
303
304                         if (lex.next(true)) {
305                                 if (lyxerr.debugging(Debug::KBMAP))
306                                         lyxerr << "\t`" << lex.text() << "'"
307                                                << endl;
308                                 key = lex.text()[0];
309                         } else
310                                 return -1;
311
312                         if (lex.next(true)) {
313                                 if (lyxerr.debugging(Debug::KBMAP))
314                                         lyxerr << "\t`" << lex.text() << "'"
315                                                << endl;
316                                 str = lex.text();
317                         } else
318                                 return -1;
319
320                         InsertException(kmod_list_[accent]->exception_list, key, str);
321                         break;
322                 }
323                 case LyXLex::LEX_FEOF:
324                         lyxerr[Debug::PARSER] << "End of parsing" << endl;
325                         break;
326                 default:
327                         lex.printError("ParseKeymapFile: "
328                                        "Unknown tag: `$$Token'");
329                         return -1;
330                 }
331         }
332         return 0;
333 }
334
335
336 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i)
337 {
338         if (kmod_list_[accent]!= 0) {
339                 i = *kmod_list_[accent];
340                 return true;
341         }
342         return false;
343 }
344
345
346 string Trans::process(char c, TransManager & k)
347 {
348         char dummy[2] = "?";
349         char * dt = dummy;
350         char * t = Match(static_cast<unsigned char>(c));
351     
352         if ((t == 0 && (*dt = c)) || (t[0] != 0 && (dt = t)) ){
353                 return k.normalkey(c, dt);
354         } else {
355                 return k.deadkey(c, *kmod_list_[static_cast<tex_accent>(t[1])]);
356         }
357 }
358
359
360 int Trans::Load(string const & language)
361 {
362         string filename = LibFileSearch("kbd", language, "kmap");
363         if (filename.empty())
364                 return -1;
365
366         FreeKeymap();
367         LyXLex lex(kmapTags, K_LAST-1);
368         lex.setFile(filename);
369         
370         int res = Load(lex);
371
372         if (res == 0) {
373                 name_ = language;
374         } else
375                 name_.clear();
376
377         return res;
378 }
379
380
381 tex_accent getkeymod(string const & p)
382         /* return modifier - decoded from p and update p */
383 {
384         for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
385                 if (lyxerr.debugging(Debug::KBMAP))
386                         lyxerr << "p = " << p
387                                << ", lyx_accent_table[" << i
388                                << "].name = `" << lyx_accent_table[i].name
389                                << "'" << endl;
390                 
391                 if ( lyx_accent_table[i].name && contains(p, lyx_accent_table[i].name)) {
392                         lyxerr[Debug::KBMAP] << "Found it!" << endl;
393                         return static_cast<tex_accent>(i);
394                 }
395         }
396         return TEX_NOACCENT;
397 }