]> git.lyx.org Git - lyx.git/blob - src/trans.C
Fix bug in input of characters >127 ; update fr.po ; portability fix to lyxstring.
[lyx.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                 temp = keymap_[static_cast<unsigned char>(keys[i])] = new char[2];
166                 temp[0] = 0; temp[1] = accent;
167         }
168         kmod_list_[accent]->exception_list = 0;
169 }
170
171
172 int Trans::Load(LyXLex & lex)
173 {
174         bool error = false;
175
176         while (lex.IsOK() && !error) {
177                 switch(lex.lex()) {
178                 case KMOD:
179                 {
180                         if (lyxerr.debugging(Debug::KBMAP))
181                                 lyxerr << "KMOD:\t" << lex.text() << endl;
182                         if (lex.next(true)) {
183                                 if (lyxerr.debugging(Debug::KBMAP))
184                                         lyxerr << "key\t`" << lex.text()
185                                                << "'" << endl;
186                         } else
187                                 return -1;
188                         
189                         string keys = lex.GetString();
190
191                         if (lex.next(true)) {
192                                 if ( lyxerr.debugging(Debug::KBMAP))
193                                         lyxerr << "accent\t`" << lex.text()
194                                                << "'" << endl;
195                         } else
196                                 return -1;
197
198                         tex_accent accent = getkeymod(lex.GetString());
199
200                         if (accent == TEX_NOACCENT)
201                                 return -1;
202
203                         if (lex.next(true)) {
204                                 if (lyxerr.debugging(Debug::KBMAP))
205                                         lyxerr << "allowed\t`" << lex.text()
206                                                << "'" << endl;
207                         } else
208                                 return -1;
209
210                         string allowed = lex.GetString();
211
212                         AddDeadkey(accent, keys, allowed);
213                         break;
214                 }       
215                 case KCOMB: {
216                         char const * str;
217
218                         lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
219                         if (lex.next(true)) {
220                                 str= lex.text();
221                                 lyxerr[Debug::KBMAP] << str << endl;
222                         } else
223                                 return -1;
224                         
225                         tex_accent accent_1= getkeymod(str);
226                         if (accent_1 == TEX_NOACCENT) return -1;
227
228                         if (lex.next(true)) {
229                                 str= lex.text();
230                                 lyxerr[Debug::KBMAP] << str << endl;
231                         } else
232                                 return -1;
233
234                         tex_accent accent_2= getkeymod(str);
235                         if (accent_2 == TEX_NOACCENT) return -1;
236
237                         if (kmod_list_[accent_1] == 0 || kmod_list_[accent_2] == 0)
238                                 return -1;
239
240                         // Find what key accent_2 is on - should check about accent_1 also
241                         int key;
242
243                         for(key = 0; key < 256; ++key) {
244                                 if (keymap_[key] && keymap_[key][0] == 0
245                                     && keymap_[key][1] == accent_2)
246                                         break;
247                         }
248                         string allowed;
249
250                         if (lex.next()) {
251                                 allowed = lex.GetString();
252                                 lyxerr[Debug::KBMAP] << "allowed: "
253                                                      << allowed << endl;
254                         } else
255                                 return -1;
256
257                         InsertException(kmod_list_[accent_1]->exception_list,(char)key, allowed, true, accent_2);
258                 }
259                 break;
260                 case KMAP: {
261                         unsigned char key_from;
262                         char * string_to;
263
264                         if (lyxerr.debugging(Debug::KBMAP))
265                                 lyxerr << "KMAP:\t" << lex.text() << endl;
266                         if (lex.next(true)) {
267                                 key_from= lex.text()[0];
268                                 if (lyxerr.debugging(Debug::KBMAP))
269                                         lyxerr << "\t`" << lex.text() << "'"
270                                                << endl;
271                         } else
272                                 return -1;
273
274                         if (lex.next(true)) {
275                                 char const * t = lex.text();
276                                 string_to = strcpy(new char[strlen(t)+1], t);
277                                 keymap_[key_from] = string_to;
278                                 if (lyxerr.debugging(Debug::KBMAP))
279                                         lyxerr << "\t`" << string_to << "'"
280                                                << endl;
281                         } else
282                                 return -1;
283
284                         break;
285                 }
286                 case KXMOD: {
287                         tex_accent accent;
288                         char key;
289                         char const * str;
290
291                         if (lyxerr.debugging(Debug::KBMAP))
292                                 lyxerr << "KXMOD:\t" << lex.text() << endl;
293                         if (lex.next(true)) {
294                                 if (lyxerr.debugging(Debug::KBMAP))
295                                         lyxerr << "\t`" << lex.text() << "'"
296                                                << endl;
297                                 accent = getkeymod(lex.GetString());
298                         } else
299                                 return -1;
300
301                         if (lex.next(true)) {
302                                 if (lyxerr.debugging(Debug::KBMAP))
303                                         lyxerr << "\t`" << lex.text() << "'"
304                                                << endl;
305                                 key = lex.text()[0];
306                         } else
307                                 return -1;
308
309                         if (lex.next(true)) {
310                                 if (lyxerr.debugging(Debug::KBMAP))
311                                         lyxerr << "\t`" << lex.text() << "'"
312                                                << endl;
313                                 str = lex.text();
314                         } else
315                                 return -1;
316
317                         InsertException(kmod_list_[accent]->exception_list, key, str);
318                         break;
319                 }
320                 case LyXLex::LEX_FEOF:
321                         lyxerr[Debug::PARSER] << "End of parsing" << endl;
322                         break;
323                 default:
324                         lex.printError("ParseKeymapFile: "
325                                        "Unknown tag: `$$Token'");
326                         return -1;
327                 }
328         }
329         return 0;
330 }
331
332
333 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i)
334 {
335         if (kmod_list_[accent]!= 0) {
336                 i = *kmod_list_[accent];
337                 return true;
338         }
339         return false;
340 }
341
342
343 string Trans::process(char c, TransManager & k)
344 {
345         char dummy[2] = "?";
346         char * dt = dummy;
347         char * t = Match(c);
348     
349         if ((t == 0 && (*dt = c)) || (t[0] != 0 && (dt = t)) ){
350                 return k.normalkey(c, dt);
351         } else {
352                 return k.deadkey(c, *kmod_list_[(tex_accent)t[1]]);
353         }
354 }
355
356
357 int Trans::Load(string const & language)
358 {
359         string filename = LibFileSearch("kbd", language, "kmap");
360         if (filename.empty())
361                 return -1;
362
363         FreeKeymap();
364         LyXLex lex(kmapTags, K_LAST-1);
365         lex.setFile(filename);
366         
367         int res = Load(lex);
368
369         if (res == 0) {
370                 name_ = language;
371         } else
372                 name_.clear();
373
374         return res;
375 }
376
377
378 tex_accent getkeymod(string const & p)
379         /* return modifier - decoded from p and update p */
380 {
381         for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
382                 if (lyxerr.debugging(Debug::KBMAP))
383                         lyxerr << "p = " << p
384                                << ", lyx_accent_table[" << i
385                                << "].name = `" << lyx_accent_table[i].name
386                                << "'" << endl;
387                 
388                 if ( lyx_accent_table[i].name && contains(p, lyx_accent_table[i].name)) {
389                         lyxerr[Debug::KBMAP] << "Found it!" << endl;
390                         return (tex_accent)i;
391                 }
392         }
393         return TEX_NOACCENT;
394 }