]> git.lyx.org Git - lyx.git/blob - src/Trans.cpp
LYX_CXX_GLOBAL_CSTD is not really useful anymore
[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
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "CutAndPaste.h"
20 #include "support/debug.h"
21 #include "Lexer.h"
22 #include "LyXRC.h"
23 #include "Text.h"
24
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27 #include "support/convert.h"
28 #include "support/docstream.h"
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 /////////////////////////////////////////////////////////////////////
36 //
37 // TeXAccents
38 //
39 /////////////////////////////////////////////////////////////////////
40
41 /* the names used by TeX and XWindows for deadkeys/accents are not the same
42    so here follows a table to clearify the differences. Please correct this
43    if I got it wrong
44
45    |------------------|------------------|------------------|--------------|
46    |      TeX         |     XWindows     |   \bind/LFUN     | used by intl |
47    |------------------|------------------|------------------|--------------|
48    |    grave         |    grave         |LFUN_ACCENT_GRAVE        | grave
49    |    acute         |    acute         |LFUN_ACCENT_ACUTE        | acute
50    |    circumflex    |    circumflex    |LFUN_ACCENT_CIRCUMFLEX   | circumflex
51    | umlaut/dieresis  |    diaeresis     |LFUN_ACCENT_UMLAUT       | umlaut
52    |    tilde         |    tilde         |LFUN_ACCENT_TILDE        | tilde
53    |    macron        |    maron         |LFUN_ACCENT_MACRON       | macron
54    |    dot           |    abovedot      |LFUN_ACCENT_DOT          | dot
55    |    cedilla       |    cedilla       |LFUN_ACCENT_CEDILLA      | cedilla
56    |    underdot      |                  |LFUN_ACCENT_UNDERDOT     | underdot
57    |    underbar      |                  |LFUN_ACCENT_UNDERBAR     | underbar
58    |    hácek         |    caron         |LFUN_ACCENT_CARON        | caron
59    |    breve         |    breve         |LFUN_ACCENT_BREVE        | breve
60    |    tie           |                  |LFUN_ACCENT_TIE          | tie
61    | Hungarian umlaut |    doubleacute   |LFUN_ACCENT_HUNGARIAN_UMLAUT  | hungarian umlaut
62    |    circle        |    abovering     |LFUN_ACCENT_CIRCLE       | circle
63    |                  |    ogonek        |                  |
64    |                  |    iota          |                  |
65    |                  |    voiced_sound  |                  |
66    |                  | semivoiced_sound |                  |
67    |                  |                  |LFUN_ACCENT_SPECIAL_CARON| special caron
68    */
69 static tex_accent_struct lyx_accent_table[] = {
70         {TEX_NOACCENT,   0,      "",                LFUN_NOACTION},
71         {TEX_ACUTE,      0x0301, "acute",           LFUN_ACCENT_ACUTE},
72         {TEX_GRAVE,      0x0300, "grave",           LFUN_ACCENT_GRAVE},
73         {TEX_MACRON,     0x0304, "macron",          LFUN_ACCENT_MACRON},
74         {TEX_TILDE,      0x0303, "tilde",           LFUN_ACCENT_TILDE},
75         {TEX_UNDERBAR,   0x0320, "underbar",        LFUN_ACCENT_UNDERBAR},
76         {TEX_CEDILLA,    0x0327, "cedilla",         LFUN_ACCENT_CEDILLA},
77         {TEX_UNDERDOT,   0x0323, "underdot",        LFUN_ACCENT_UNDERDOT},
78         {TEX_CIRCUMFLEX, 0x0302, "circumflex",      LFUN_ACCENT_CIRCUMFLEX},
79         {TEX_CIRCLE,     0x030a, "circle",          LFUN_ACCENT_CIRCLE},
80         {TEX_TIE,        0x0361, "tie",             LFUN_ACCENT_TIE},
81         {TEX_BREVE,      0x0306, "breve",           LFUN_ACCENT_BREVE},
82         {TEX_CARON,      0x030c, "caron",           LFUN_ACCENT_CARON},
83 //      {TEX_SPECIAL_CARON, 0x030c, "ooo",          LFUN_ACCENT_SPECIAL_CARON},
84         // Don't fix this typo for compatibility reasons!
85         {TEX_HUNGUML,    0x030b, "hugarian_umlaut", LFUN_ACCENT_HUNGARIAN_UMLAUT},
86         {TEX_UMLAUT,     0x0308, "umlaut",          LFUN_ACCENT_UMLAUT},
87         {TEX_DOT,        0x0307, "dot",             LFUN_ACCENT_DOT},
88         {TEX_OGONEK,     0x0328, "ogonek",          LFUN_ACCENT_OGONEK}
89 };
90
91
92 tex_accent_struct get_accent(kb_action action)
93 {
94         int i = 0;
95         while (i <= TEX_MAX_ACCENT) {
96                 if (lyx_accent_table[i].action == action)
97                         return lyx_accent_table[i];
98                 ++i;
99         }
100         struct tex_accent_struct temp = { static_cast<tex_accent>(0), 0,
101                                           0, static_cast<kb_action>(0)};
102         return temp;
103 }
104
105
106 static docstring const doAccent(docstring const & s, tex_accent accent)
107 {
108         if (s.empty())
109                 return docstring(1, lyx_accent_table[accent].ucs4);
110
111         odocstringstream os;
112         os.put(s[0]);
113         os.put(lyx_accent_table[accent].ucs4);
114         if (s.length() > 1) {
115                 if (accent != TEX_TIE || s.length() > 2)
116                         lyxerr << "Warning: Too many characters given for accent "
117                                << lyx_accent_table[accent].name << '.' << endl;
118                 os << s.substr(1);
119         }
120         return normalize_c(os.str());
121 }
122
123
124 static docstring const doAccent(char_type c, tex_accent accent)
125 {
126         return doAccent(docstring(1, c), accent);
127 }
128
129
130
131 /////////////////////////////////////////////////////////////////////
132 //
133 // Trans
134 //
135 /////////////////////////////////////////////////////////////////////
136
137
138 void Trans::insertException(KmodException & exclist, char_type c,
139         docstring const & data, bool flag, tex_accent accent)
140 {
141         Keyexc p;
142         p.c = c;
143         p.data = data;
144         p.combined = flag;
145         p.accent = accent;
146         exclist.insert(exclist.begin(), p);
147         // or just
148         // exclist.push_back(p);
149 }
150
151
152 void Trans::freeException(KmodException & exclist)
153 {
154         exclist.clear();
155 }
156
157
158 void Trans::freeKeymap()
159 {
160         kmod_list_.clear();
161         keymap_.clear();
162 }
163
164
165 bool Trans::isDefined() const
166 {
167         return !name_.empty();
168 }
169
170
171 enum kmaptags_ {
172         KCOMB = 1,
173         KMOD,
174         KMAP,
175         KXMOD,
176         K_LAST
177 };
178
179
180 struct keyword_item kmapTags[K_LAST - 1] = {
181         {"\\kcomb", KCOMB },
182         { "\\kmap", KMAP },
183         { "\\kmod", KMOD },
184         { "\\kxmod", KXMOD }
185 };
186
187
188 tex_accent getkeymod(string const &);
189
190
191 void Trans::addDeadkey(tex_accent accent, docstring const & keys)
192 {
193         KmodInfo tmp;
194         tmp.data = keys;
195         tmp.accent = accent;
196         kmod_list_[accent] = tmp;
197
198         for (docstring::size_type i = 0; i < keys.length(); ++i) {
199                 // FIXME This is a hack.
200                 // tmp is no valid UCS4 string, but misused to store the
201                 // accent.
202                 docstring tmp;
203                 tmp += char_type(0);
204                 tmp += char_type(accent);
205                 keymap_[keys[i]] = tmp;
206         }
207 }
208
209
210 int Trans::load(Lexer & lex)
211 {
212         bool error = false;
213
214         while (lex.isOK() && !error) {
215                 switch (lex.lex()) {
216                 case KMOD:
217                 {
218                         LYXERR(Debug::KBMAP, "KMOD:\t" << lex.getString());
219                         if (!lex.next(true))
220                                 return -1;
221
222                         LYXERR(Debug::KBMAP, "key\t`" << lex.getString() << '\'');
223
224                         docstring const keys = lex.getDocString();
225
226                         if (!lex.next(true))
227                                 return -1;
228
229                         LYXERR(Debug::KBMAP, "accent\t`" << lex.getString() << '\'');
230
231                         tex_accent accent = getkeymod(lex.getString());
232
233                         if (accent == TEX_NOACCENT)
234                                 return -1;
235
236 #if 1
237                         // FIXME: This code should be removed...
238                         // But we need to fix up all the kmap files first
239                         // so that this field is not present anymore.
240                         if (!lex.next(true))
241                                 return -1;
242
243                         LYXERR(Debug::KBMAP, "allowed\t`" << lex.getString() << '\'');
244
245                         /* string const allowed = lex.getString(); */
246                         addDeadkey(accent, keys /*, allowed*/);
247 #else
248                         addDeadkey(accent, keys);
249 #endif
250                         break;
251                 }
252                 case KCOMB: {
253                         string str;
254
255                         LYXERR(Debug::KBMAP, "KCOMB:");
256                         if (!lex.next(true))
257                                 return -1;
258
259                         str = lex.getString();
260                         LYXERR(Debug::KBMAP, str);
261
262                         tex_accent accent_1 = getkeymod(str);
263                         if (accent_1 == TEX_NOACCENT)
264                                 return -1;
265
266                         if (!lex.next(true))
267                                 return -1;
268
269                         str = lex.getString();
270                         LYXERR(Debug::KBMAP, str);
271
272                         tex_accent accent_2 = getkeymod(str);
273                         if (accent_2 == TEX_NOACCENT) return -1;
274
275                         map<tex_accent, KmodInfo>::iterator it1 =
276                                 kmod_list_.find(accent_1);
277                         map<tex_accent, KmodInfo>::iterator it2 =
278                                 kmod_list_.find(accent_2);
279                         if (it1 == kmod_list_.end() || it2 == kmod_list_.end())
280                                 return -1;
281
282                         // Find what key accent_2 is on - should
283                         // check about accent_1 also
284                         map<char_type, docstring>::iterator it = keymap_.begin();
285                         map<char_type, docstring>::iterator end = keymap_.end();
286                         for (; it != end; ++it) {
287                                 if (!it->second.empty()
288                                     && it->second[0] == 0
289                                     && it->second[1] == accent_2)
290                                         break;
291                         }
292                         docstring allowed;
293                         if (!lex.next())
294                                 return -1;
295
296                         allowed = lex.getDocString();
297                         LYXERR(Debug::KBMAP, "allowed: " << to_utf8(allowed));
298
299                         insertException(kmod_list_[accent_1].exception_list,
300                                         it->first, allowed, true, accent_2);
301                 }
302                 break;
303                 case KMAP: {
304                         unsigned char key_from;
305
306                         LYXERR(Debug::KBMAP, "KMAP:\t" << lex.getString());
307
308                         if (!lex.next(true))
309                                 return -1;
310
311                         key_from = lex.getString()[0];
312                         LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
313
314                         if (!lex.next(true))
315                                 return -1;
316
317                         docstring const string_to = lex.getDocString();
318                         keymap_[key_from] = string_to;
319                         LYXERR(Debug::KBMAP, "\t`" << to_utf8(string_to) << '\'');
320                         break;
321                 }
322                 case KXMOD: {
323                         tex_accent accent;
324                         char_type key;
325                         docstring str;
326
327                         LYXERR(Debug::KBMAP, "KXMOD:\t" << lex.getString());
328
329                         if (!lex.next(true))
330                                 return -1;
331
332                         LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
333                         accent = getkeymod(lex.getString());
334
335                         if (!lex.next(true))
336                                 return -1;
337
338                         LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
339                         key = lex.getDocString()[0];
340
341                         if (!lex.next(true))
342                                 return -1;
343
344                         LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
345                         str = lex.getDocString();
346
347                         insertException(kmod_list_[accent].exception_list,
348                                         key, str);
349                         break;
350                 }
351                 case Lexer::LEX_FEOF:
352                         LYXERR(Debug::PARSER, "End of parsing");
353                         break;
354                 default:
355                         lex.printError("ParseKeymapFile: Unknown tag: `$$Token'");
356                         return -1;
357                 }
358         }
359         return 0;
360 }
361
362
363 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
364 {
365         map<tex_accent, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
366         if (cit == kmod_list_.end())
367                 return false;
368         i = cit->second;
369         return true;
370 }
371
372
373 docstring const Trans::process(char_type c, TransManager & k)
374 {
375         docstring const t = match(c);
376
377         if (t.empty() && c != 0)
378                 return k.normalkey(c);
379
380         if (!t.empty() && t[0] != 0)
381                 return t; //return k.normalkey(c);
382
383         return k.deadkey(c, kmod_list_[static_cast<tex_accent>(t[1])]);
384 }
385
386
387 int Trans::load(string const & language)
388 {
389         FileName const filename = libFileSearch("kbd", language, "kmap");
390         if (filename.empty())
391                 return -1;
392
393         freeKeymap();
394         Lexer lex(kmapTags, K_LAST - 1);
395         lex.setFile(filename);
396
397         int const res = load(lex);
398
399         if (res == 0)
400                 name_ = language;
401         else
402                 name_.erase();
403
404         return res;
405 }
406
407
408 tex_accent getkeymod(string const & p)
409         /* return modifier - decoded from p and update p */
410 {
411         for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
412                 LYXERR(Debug::KBMAP, "p = " << p
413                        << ", lyx_accent_table[" << i
414                        << "].name = `" << lyx_accent_table[i].name << '\'');
415
416                 if (lyx_accent_table[i].name
417                      && contains(p, lyx_accent_table[i].name)) {
418                         LYXERR(Debug::KBMAP, "Found it!");
419                         return static_cast<tex_accent>(i);
420                 }
421         }
422         return TEX_NOACCENT;
423 }
424
425
426 /////////////////////////////////////////////////////////////////////
427 //
428 // TransState
429 //
430 /////////////////////////////////////////////////////////////////////
431
432
433 // TransFSMData
434 TransFSMData::TransFSMData()
435 {
436         deadkey_ = deadkey2_ = 0;
437         deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
438 }
439
440
441 // TransState
442 char_type const TransState::TOKEN_SEP = 4;
443
444
445 // TransInitState
446 TransInitState::TransInitState()
447 {
448         init_state_ = this;
449 }
450
451
452 docstring const TransInitState::normalkey(char_type c)
453 {
454         docstring res;
455         res = c;
456         return res;
457 }
458
459
460 docstring const TransInitState::deadkey(char_type c, KmodInfo d)
461 {
462         deadkey_ = c;
463         deadkey_info_ = d;
464         currentState = deadkey_state_;
465         return docstring();
466 }
467
468
469 // TransDeadkeyState
470 TransDeadkeyState::TransDeadkeyState()
471 {
472         deadkey_state_ = this;
473 }
474
475
476 docstring const TransDeadkeyState::normalkey(char_type c)
477 {
478         docstring res;
479
480         KmodException::iterator it = deadkey_info_.exception_list.begin();
481         KmodException::iterator end = deadkey_info_.exception_list.end();
482
483         for (; it != end; ++it) {
484                 if (it->c == c) {
485                         res = it->data;
486                         break;
487                 }
488         }
489         if (it == end) {
490                 res = doAccent(c, deadkey_info_.accent);
491         }
492         currentState = init_state_;
493         return res;
494 }
495
496
497 docstring const TransDeadkeyState::deadkey(char_type c, KmodInfo d)
498 {
499         docstring res;
500
501         // Check if the same deadkey was typed twice
502         if (deadkey_ == c) {
503                 res = deadkey_;
504                 deadkey_ = 0;
505                 deadkey_info_.accent = TEX_NOACCENT;
506                 currentState = init_state_;
507                 return res;
508         }
509
510         // Check if it is a combination or an exception
511         KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
512         KmodException::const_iterator end = deadkey_info_.exception_list.end();
513         for (; cit != end; ++cit) {
514                 if (cit->combined == true && cit->accent == d.accent) {
515                         deadkey2_ = c;
516                         deadkey2_info_ = d;
517                         comb_info_ = (*cit);
518                         currentState = combined_state_;
519                         return docstring();
520                 }
521                 if (cit->c == c) {
522                         res = cit->data;
523                         deadkey_ = 0;
524                         deadkey_info_.accent = TEX_NOACCENT;
525                         currentState = init_state_;
526                         return res;
527                 }
528         }
529
530         // Not a combination or an exception.
531         // Output deadkey1 and keep deadkey2
532
533         if (deadkey_!= 0)
534                 res = deadkey_;
535         deadkey_ = c;
536         deadkey_info_ = d;
537         currentState = deadkey_state_;
538         return res;
539 }
540
541
542 TransCombinedState::TransCombinedState()
543 {
544         combined_state_ = this;
545 }
546
547
548 docstring const TransCombinedState::normalkey(char_type c)
549 {
550         docstring const temp = doAccent(c, deadkey2_info_.accent);
551         docstring const res = doAccent(temp, deadkey_info_.accent);
552         currentState = init_state_;
553         return res;
554 }
555
556
557 docstring const TransCombinedState::deadkey(char_type c, KmodInfo d)
558 {
559         // Third key in a row. Output the first one and
560         // reenter with shifted deadkeys
561         docstring res;
562         if (deadkey_ != 0)
563                 res = deadkey_;
564         res += TOKEN_SEP;
565         deadkey_ = deadkey2_;
566         deadkey_info_ = deadkey2_info_;
567         res += deadkey_state_->deadkey(c, d);
568         return res;
569 }
570
571
572 // TransFSM
573 TransFSM::TransFSM()
574         : TransFSMData(), TransInitState(), TransDeadkeyState(), TransCombinedState()
575 {
576         currentState = init_state_;
577 }
578
579
580 // TransManager
581
582 // Initialize static member.
583 Trans TransManager::default_;
584
585
586 TransManager::TransManager()
587         : active_(0)
588 {}
589
590
591 int TransManager::setPrimary(string const & language)
592 {
593         if (t1_.getName() == language)
594                 return 0;
595
596         return t1_.load(language);
597 }
598
599
600 int TransManager::setSecondary(string const & language)
601 {
602         if (t2_.getName() == language)
603                 return 0;
604
605         return t2_.load(language);
606 }
607
608
609 void TransManager::enablePrimary()
610 {
611         if (t1_.isDefined())
612                 active_ = &t1_;
613
614         LYXERR(Debug::KBMAP, "Enabling primary keymap");
615 }
616
617
618 void TransManager::enableSecondary()
619 {
620         if (t2_.isDefined())
621                 active_ = &t2_;
622         LYXERR(Debug::KBMAP, "Enabling secondary keymap");
623 }
624
625
626 void TransManager::disableKeymap()
627 {
628         active_ = &default_;
629         LYXERR(Debug::KBMAP, "Disabling keymap");
630 }
631
632
633 void  TransManager::translateAndInsert(char_type c, Text * text, Cursor & cur)
634 {
635         docstring res = active_->process(c, *this);
636
637         // Process with tokens
638         docstring temp;
639
640         while (res.length() > 0) {
641                 res = split(res, temp, TransState::TOKEN_SEP);
642                 insert(temp, text, cur);
643         }
644 }
645
646
647 void TransManager::insert(docstring const & str, Text * text, Cursor & cur)
648 {
649         for (size_t i = 0, n = str.size(); i != n; ++i)
650                 text->insertChar(cur, str[i]);
651 }
652
653
654 void TransManager::deadkey(char_type c, tex_accent accent, Text * t, Cursor & cur)
655 {
656         if (c == 0 && active_ != &default_) {
657                 // A deadkey was pressed that cannot be printed
658                 // or a accent command was typed in the minibuffer
659                 KmodInfo i;
660                 if (active_->isAccentDefined(accent, i) == true) {
661                         docstring const res = trans_fsm_
662                                 .currentState->deadkey(c, i);
663                         insert(res, t, cur);
664                         return;
665                 }
666         }
667
668         if (active_ == &default_ || c == 0) {
669                 KmodInfo i;
670                 i.accent = accent;
671                 i.data.erase();
672                 docstring res = trans_fsm_.currentState->deadkey(c, i);
673                 insert(res, t, cur);
674         } else {
675                 // Go through the translation
676                 translateAndInsert(c, t, cur);
677         }
678 }
679
680
681 } // namespace lyx