]> git.lyx.org Git - lyx.git/blob - src/trans.C
Remove unused font variable which caused a warning.
[lyx.git] / src / trans.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
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 using std::endl;
16
17
18 // KmodInfo
19 KmodInfo::KmodInfo()
20 {
21 #if 0
22         exception_list = 0;
23 #endif
24 }
25
26
27 #if 0
28 // Default Trans
29 bool DefaultTrans::init_ = false;
30
31
32 DefaultTrans::DefaultTrans()
33 {
34         if (init_ == false) {
35                 // Do initialization
36                 init_ = true;
37         }
38 }
39
40
41 #if 0
42 string const DefaultTrans::process(char c, TransManager & k)
43 {
44         char dummy[2] = "?";
45         dummy[0] = c;
46     
47         return k.normalkey(c, dummy);
48 }
49 #else
50 string const DefaultTrans::process(char c, TransManager & k)
51 {
52         return k.normalkey(c);
53 }
54 #endif
55 #endif
56
57 // Trans class
58
59 Trans::Trans()
60 {
61 #if 0
62         for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
63                 kmod_list_[i] = 0;
64 #endif
65 }
66
67
68 Trans::~Trans()
69 {
70         FreeKeymap();
71 }
72
73
74 void Trans::InsertException(KmodException & exclist, char c,
75                             string const & data, bool flag, tex_accent accent)
76 {
77 #if 0
78         keyexc p = new Keyexc; 
79         p->next = exclist;
80         p->c = c;
81
82         p->data = data;
83         p->combined = flag;
84         p->accent = accent;
85
86         exclist = p;
87 #else
88         Keyexc p;
89         p.c = c;
90         p.data = data;
91         p.combined = flag;
92         p.accent = accent;
93         exclist.insert(exclist.begin(), p);
94         // or just
95         // exclist.push_back(p);
96 #endif
97 }
98
99
100 void Trans::FreeException(KmodException & exclist)
101 {
102 #if 0
103         Trans::keyexc p = exclist;
104         while (p) {
105                 p = exclist->next;
106                 delete exclist;
107                 exclist = p;
108         }
109 #else
110         exclist.clear();
111 #endif
112 }
113
114
115 void Trans::FreeKeymap()
116 {
117 #if 0
118         for (int i = 0; i < 256; ++i)
119                 if (!keymap_[i].empty()) {
120                         keymap_[i].erase();
121                 }
122         for (int i = 0; i < TEX_MAX_ACCENT + 1; ++i)
123                 if (kmod_list_[i]) {
124                         FreeException(kmod_list_[i]->exception_list);
125                         delete kmod_list_[i];
126                         kmod_list_[i] = 0;
127                 }
128 #else
129         kmod_list_.clear();
130         keymap_.clear();
131 #endif
132 }
133
134
135 bool Trans::IsDefined() const
136 {
137         return !name_.empty();
138 }
139
140
141 string const & Trans::GetName() const
142 {
143         return name_;
144 }
145
146
147 enum kmaptags_ {
148         KCOMB = 1,
149         KMOD,
150         KMAP,
151         KXMOD,
152         K_LAST
153 };
154
155
156 struct keyword_item kmapTags[K_LAST - 1] = {
157         {"\\kcomb", KCOMB },
158         { "\\kmap", KMAP },
159         { "\\kmod", KMOD },
160         { "\\kxmod", KXMOD }
161 };
162
163
164 tex_accent getkeymod(string const &);
165
166
167 #if 0
168 void Trans::AddDeadkey(tex_accent accent, string const & keys,
169                        string const & allowed)
170 #else
171 void Trans::AddDeadkey(tex_accent accent, string const & keys)
172 #endif
173 {
174 #if 0
175         if (kmod_list_[accent]) {
176                 FreeException(kmod_list_[accent]->exception_list);
177                 
178                 delete kmod_list_[accent];
179         }
180         
181         kmod_list_[accent] = new KmodInfo;
182         kmod_list_[accent]->data = keys;
183         kmod_list_[accent]->accent = accent;
184 #else
185         KmodInfo tmp;
186         tmp.data = keys;
187         tmp.accent = accent;
188         kmod_list_[accent] = tmp;
189 #endif
190 #if 0
191         if (allowed == "native") { 
192                 kmod_list_[accent]->allowed= lyx_accent_table[accent].native;
193         } else {
194                 kmod_list_[accent]->allowed = allowed;
195         }
196
197         for (string::size_type i = 0; i < keys.length(); ++i) {
198                 string & temp =
199                         keymap_[static_cast<unsigned char>(keys[i])];
200                 if (!temp.empty())
201                         temp.erase();
202
203                 // But the question remains: "Should we be allowed
204                 // to change bindings, without unbinding first?"
205                 // Lgb
206                 temp += char(0);
207                 temp += char(accent);
208         }
209 #else
210         for (string::size_type i = 0; i < keys.length(); ++i) {
211                 string tmp;
212                 tmp += char(0);
213                 tmp += char(accent);
214                 keymap_[keys[i]] = tmp;
215         }
216 #endif
217 #if 0
218         kmod_list_[accent]->exception_list = 0;
219 #endif
220 }
221
222
223 int Trans::Load(LyXLex & lex)
224 {
225         bool error = false;
226
227         while (lex.IsOK() && !error) {
228                 switch (lex.lex()) {
229                 case KMOD:
230                 {
231                         if (lyxerr.debugging(Debug::KBMAP))
232                                 lyxerr << "KMOD:\t" << lex.text() << endl;
233                         if (lex.next(true)) {
234                                 if (lyxerr.debugging(Debug::KBMAP))
235                                         lyxerr << "key\t`" << lex.text()
236                                                << "'" << endl;
237                         } else
238                                 return -1;
239                         
240                         string const keys = lex.GetString();
241
242                         if (lex.next(true)) {
243                                 if (lyxerr.debugging(Debug::KBMAP))
244                                         lyxerr << "accent\t`" << lex.text()
245                                                << "'" << endl;
246                         } else
247                                 return -1;
248
249                         tex_accent accent = getkeymod(lex.GetString());
250
251                         if (accent == TEX_NOACCENT)
252                                 return -1;
253
254 #if 1
255 #warning This code should be removed...
256                         // But we need to fix up all the kmap files first
257                         // so that this field is not present anymore.
258                         if (lex.next(true)) {
259                                 if (lyxerr.debugging(Debug::KBMAP))
260                                         lyxerr << "allowed\t`" << lex.text()
261                                                << "'" << endl;
262                         } else
263                                 return -1;
264
265                         string const allowed = lex.GetString();
266                         AddDeadkey(accent, keys /*, allowed*/);
267 #else
268                         AddDeadkey(accent, keys);
269 #endif
270                         break;
271                 }       
272                 case KCOMB: {
273                         string str;
274
275                         lyxerr[Debug::KBMAP] << "KCOMB:" << endl;
276                         if (lex.next(true)) {
277                                 str= lex.text();
278                                 lyxerr[Debug::KBMAP] << str << endl;
279                         } else
280                                 return -1;
281                         
282                         tex_accent accent_1 = getkeymod(str);
283                         if (accent_1 == TEX_NOACCENT) return -1;
284
285                         if (lex.next(true)) {
286                                 str = lex.text();
287                                 lyxerr[Debug::KBMAP] << str << endl;
288                         } else
289                                 return -1;
290
291                         tex_accent accent_2= getkeymod(str);
292                         if (accent_2 == TEX_NOACCENT) return -1;
293
294 #if 0
295                         if (kmod_list_[accent_1] == 0
296                             || kmod_list_[accent_2] == 0)
297                                 return -1;
298 #else
299                         std::map<int, KmodInfo>::iterator it1 =
300                                 kmod_list_.find(accent_1);
301                         std::map<int, KmodInfo>::iterator it2 =
302                                 kmod_list_.find(accent_2);
303                         if (it1 == kmod_list_.end()
304                             || it2 == kmod_list_.end()) {
305                                 return -1;
306                         }
307 #endif
308
309                         // Find what key accent_2 is on - should
310                         // check about accent_1 also
311 #if 0
312                         int key = 0;
313                         for (; key < 256; ++key) {
314                                 if (!keymap_[key].empty()
315                                     && keymap_[key][0] == 0
316                                     && keymap_[key][1] == accent_2)
317                                         break;
318                         }
319                         string allowed;
320
321                         if (lex.next()) {
322                                 allowed = lex.GetString();
323                                 lyxerr[Debug::KBMAP] << "allowed: "
324                                                      << allowed << endl;
325                         } else
326                                 return -1;
327
328                         InsertException(kmod_list_[accent_1].exception_list,
329                                         static_cast<char>(key), allowed,
330                                         true, accent_2);
331 #else
332                         std::map<int, string>::iterator it = keymap_.begin();
333                         std::map<int, string>::iterator end = keymap_.end();
334                         for (; it != end; ++it) {
335                                 if (!it->second.empty()
336                                     && it->second[0] == 0
337                                     && it->second[1] == accent_2)
338                                         break;
339                         }
340                         string allowed;
341                         if (lex.next()) {
342                                 allowed = lex.GetString();
343                                 lyxerr[Debug::KBMAP] << "allowed: "
344                                                      << allowed << endl;
345                         } else {
346                                 return -1;
347                         }
348                         
349                         InsertException(kmod_list_[accent_1].exception_list,
350                                         static_cast<char>(it->first), allowed,
351                                         true, accent_2);
352 #endif
353                 }
354                 break;
355                 case KMAP: {
356                         unsigned char key_from;
357
358                         if (lyxerr.debugging(Debug::KBMAP))
359                                 lyxerr << "KMAP:\t" << lex.text() << endl;
360                         if (lex.next(true)) {
361                                 key_from = lex.text()[0];
362                                 if (lyxerr.debugging(Debug::KBMAP))
363                                         lyxerr << "\t`" << lex.text() << "'"
364                                                << endl;
365                         } else
366                                 return -1;
367
368                         if (lex.next(true)) {
369                                 string string_to = lex.text();
370 #if 0
371                                 keymap_[key_from] = string_to;
372 #else
373                                 keymap_[key_from] = string_to;
374 #endif
375                                 if (lyxerr.debugging(Debug::KBMAP))
376                                         lyxerr << "\t`" << string_to << "'"
377                                                << endl;
378                         } else
379                                 return -1;
380
381                         break;
382                 }
383                 case KXMOD: {
384                         tex_accent accent;
385                         char key;
386                         string str;
387
388                         if (lyxerr.debugging(Debug::KBMAP))
389                                 lyxerr << "KXMOD:\t" << lex.text() << endl;
390                         if (lex.next(true)) {
391                                 if (lyxerr.debugging(Debug::KBMAP))
392                                         lyxerr << "\t`" << lex.text() << "'"
393                                                << endl;
394                                 accent = getkeymod(lex.GetString());
395                         } else
396                                 return -1;
397
398                         if (lex.next(true)) {
399                                 if (lyxerr.debugging(Debug::KBMAP))
400                                         lyxerr << "\t`" << lex.text() << "'"
401                                                << endl;
402                                 key = lex.text()[0];
403                         } else
404                                 return -1;
405
406                         if (lex.next(true)) {
407                                 if (lyxerr.debugging(Debug::KBMAP))
408                                         lyxerr << "\t`" << lex.text() << "'"
409                                                << endl;
410                                 str = lex.text();
411                         } else
412                                 return -1;
413
414                         InsertException(kmod_list_[accent].exception_list,
415                                         key, str);
416                         break;
417                 }
418                 case LyXLex::LEX_FEOF:
419                         lyxerr[Debug::PARSER] << "End of parsing" << endl;
420                         break;
421                 default:
422                         lex.printError("ParseKeymapFile: "
423                                        "Unknown tag: `$$Token'");
424                         return -1;
425                 }
426         }
427         return 0;
428 }
429
430
431 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
432 {
433 #if 0
434         if (kmod_list_[accent] != 0) {
435                 i = *kmod_list_[accent];
436                 return true;
437         }
438         return false;
439 #else
440         std::map<int, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
441         if (cit != kmod_list_.end()) {
442                 i = cit->second;
443                 return true;
444         }
445         return false;
446 #endif
447 }
448
449
450 #if 0
451 string const Trans::process(char c, TransManager & k)
452 {
453         string dt("?");
454         string const t = Match(static_cast<unsigned char>(c));
455
456         if (t.empty() && c != 0) {
457                 dt[0] = c;
458                 return k.normalkey(c, dt);
459         } else if (!t.empty() && t[0] != char(0)) {
460                 dt = t;
461                 return k.normalkey(c, dt);
462         } else {
463                 return k.deadkey(c,
464                                  *kmod_list_[static_cast<tex_accent>(t[1])]);
465         }
466 }
467 #else
468 string const Trans::process(char c, TransManager & k)
469 {
470         string const t = Match(static_cast<unsigned char>(c));
471
472         if (t.empty() && c != 0) {
473                 return k.normalkey(c);
474         } else if (!t.empty() && t[0] != char(0)) {
475                 //return k.normalkey(c);
476                 return t;
477         } else {
478 #if 0
479                 return k.deadkey(c,
480                                  *kmod_list_[static_cast<tex_accent>(t[1])]);
481 #else
482                 return k.deadkey(c,
483                                  kmod_list_[static_cast<tex_accent>(t[1])]);
484 #endif
485         }
486 }
487 #endif
488
489
490 int Trans::Load(string const & language)
491 {
492         string const filename = LibFileSearch("kbd", language, "kmap");
493         if (filename.empty())
494                 return -1;
495
496         FreeKeymap();
497         LyXLex lex(kmapTags, K_LAST-1);
498         lex.setFile(filename);
499         
500         int const res = Load(lex);
501
502         if (res == 0) {
503                 name_ = language;
504         } else
505                 name_.erase();
506
507         return res;
508 }
509
510
511 tex_accent getkeymod(string const & p)
512         /* return modifier - decoded from p and update p */
513 {
514         for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
515                 if (lyxerr.debugging(Debug::KBMAP))
516                         lyxerr << "p = " << p
517                                << ", lyx_accent_table[" << i
518                                << "].name = `" << lyx_accent_table[i].name
519                                << "'" << endl;
520                 
521                 if (lyx_accent_table[i].name
522                      && contains(p, lyx_accent_table[i].name)) {
523                         lyxerr[Debug::KBMAP] << "Found it!" << endl;
524                         return static_cast<tex_accent>(i);
525                 }
526         }
527         return TEX_NOACCENT;
528 }