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