3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * Full author contact details are available in file CREDITS.
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
24 #include "support/convert.h"
25 #include "support/debug.h"
26 #include "support/docstream.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
32 using namespace lyx::support;
36 /////////////////////////////////////////////////////////////////////
40 /////////////////////////////////////////////////////////////////////
42 /* the names used by TeX and XWindows for deadkeys/accents are not the same
43 so here follows a table to clearify the differences. Please correct this
46 |------------------|------------------|------------------|--------------|
47 | TeX | XWindows | \bind/LFUN | used by intl |
48 |------------------|------------------|------------------|--------------|
49 | grave | grave |LFUN_ACCENT_GRAVE | grave
50 | acute | acute |LFUN_ACCENT_ACUTE | acute
51 | circumflex | circumflex |LFUN_ACCENT_CIRCUMFLEX | circumflex
52 | umlaut/dieresis | diaeresis |LFUN_ACCENT_UMLAUT | umlaut
53 | tilde | tilde |LFUN_ACCENT_TILDE | tilde
54 | macron | maron |LFUN_ACCENT_MACRON | macron
55 | dot | abovedot |LFUN_ACCENT_DOT | dot
56 | cedilla | cedilla |LFUN_ACCENT_CEDILLA | cedilla
57 | underdot | |LFUN_ACCENT_UNDERDOT | underdot
58 | underbar | |LFUN_ACCENT_UNDERBAR | underbar
59 | hácek | caron |LFUN_ACCENT_CARON | caron
60 | breve | breve |LFUN_ACCENT_BREVE | breve
61 | tie | |LFUN_ACCENT_TIE | tie
62 | Hungarian umlaut | doubleacute |LFUN_ACCENT_HUNGARIAN_UMLAUT | hungarian umlaut
63 | circle | abovering |LFUN_ACCENT_CIRCLE | circle
67 | | semivoiced_sound | |
68 | | |LFUN_ACCENT_SPECIAL_CARON| special caron
70 static TeXAccent lyx_accent_table[] = {
71 {TEX_NOACCENT, 0, "", LFUN_NOACTION},
72 {TEX_ACUTE, 0x0301, "acute", LFUN_ACCENT_ACUTE},
73 {TEX_GRAVE, 0x0300, "grave", LFUN_ACCENT_GRAVE},
74 {TEX_MACRON, 0x0304, "macron", LFUN_ACCENT_MACRON},
75 {TEX_TILDE, 0x0303, "tilde", LFUN_ACCENT_TILDE},
76 {TEX_UNDERBAR, 0x0320, "underbar", LFUN_ACCENT_UNDERBAR},
77 {TEX_CEDILLA, 0x0327, "cedilla", LFUN_ACCENT_CEDILLA},
78 {TEX_UNDERDOT, 0x0323, "underdot", LFUN_ACCENT_UNDERDOT},
79 {TEX_CIRCUMFLEX, 0x0302, "circumflex", LFUN_ACCENT_CIRCUMFLEX},
80 {TEX_CIRCLE, 0x030a, "circle", LFUN_ACCENT_CIRCLE},
81 {TEX_TIE, 0x0361, "tie", LFUN_ACCENT_TIE},
82 {TEX_BREVE, 0x0306, "breve", LFUN_ACCENT_BREVE},
83 {TEX_CARON, 0x030c, "caron", LFUN_ACCENT_CARON},
84 // {TEX_SPECIAL_CARON, 0x030c, "ooo", LFUN_ACCENT_SPECIAL_CARON},
85 // Don't fix this typo for compatibility reasons!
86 {TEX_HUNGUML, 0x030b, "hugarian_umlaut", LFUN_ACCENT_HUNGARIAN_UMLAUT},
87 {TEX_UMLAUT, 0x0308, "umlaut", LFUN_ACCENT_UMLAUT},
88 {TEX_DOT, 0x0307, "dot", LFUN_ACCENT_DOT},
89 {TEX_OGONEK, 0x0328, "ogonek", LFUN_ACCENT_OGONEK}
93 TeXAccent get_accent(FuncCode action)
96 while (i <= TEX_MAX_ACCENT) {
97 if (lyx_accent_table[i].action == action)
98 return lyx_accent_table[i];
101 struct TeXAccent temp = { static_cast<tex_accent>(0), 0,
102 0, static_cast<FuncCode>(0)};
107 static docstring const doAccent(docstring const & s, tex_accent accent)
110 return docstring(1, lyx_accent_table[accent].ucs4);
114 os.put(lyx_accent_table[accent].ucs4);
115 if (s.length() > 1) {
116 if (accent != TEX_TIE || s.length() > 2)
117 lyxerr << "Warning: Too many characters given for accent "
118 << lyx_accent_table[accent].name << '.' << endl;
121 return normalize_c(os.str());
125 static docstring const doAccent(char_type c, tex_accent accent)
127 return doAccent(docstring(1, c), accent);
132 /////////////////////////////////////////////////////////////////////
136 /////////////////////////////////////////////////////////////////////
139 void Trans::insertException(KmodException & exclist, char_type c,
140 docstring const & data, bool flag, tex_accent accent)
147 exclist.insert(exclist.begin(), p);
149 // exclist.push_back(p);
153 void Trans::freeException(KmodException & exclist)
159 void Trans::freeKeymap()
166 bool Trans::isDefined() const
168 return !name_.empty();
180 tex_accent getkeymod(string const &);
183 void Trans::addDeadkey(tex_accent accent, docstring const & keys)
188 kmod_list_[accent] = tmp;
190 for (docstring::size_type i = 0; i < keys.length(); ++i) {
191 // FIXME This is a hack.
192 // tmp is no valid UCS4 string, but misused to store the
196 tmp += char_type(accent);
197 keymap_[keys[i]] = tmp;
202 int Trans::load(Lexer & lex)
206 while (lex.isOK() && !error) {
210 LYXERR(Debug::KBMAP, "KMOD:\t" << lex.getString());
214 LYXERR(Debug::KBMAP, "key\t`" << lex.getString() << '\'');
216 docstring const keys = lex.getDocString();
221 LYXERR(Debug::KBMAP, "accent\t`" << lex.getString() << '\'');
223 tex_accent accent = getkeymod(lex.getString());
225 if (accent == TEX_NOACCENT)
229 // FIXME: This code should be removed...
230 // But we need to fix up all the kmap files first
231 // so that this field is not present anymore.
235 LYXERR(Debug::KBMAP, "allowed\t`" << lex.getString() << '\'');
237 /* string const allowed = lex.getString(); */
238 addDeadkey(accent, keys /*, allowed*/);
240 addDeadkey(accent, keys);
247 LYXERR(Debug::KBMAP, "KCOMB:");
251 str = lex.getString();
252 LYXERR(Debug::KBMAP, str);
254 tex_accent accent_1 = getkeymod(str);
255 if (accent_1 == TEX_NOACCENT)
261 str = lex.getString();
262 LYXERR(Debug::KBMAP, str);
264 tex_accent accent_2 = getkeymod(str);
265 if (accent_2 == TEX_NOACCENT) return -1;
267 map<tex_accent, KmodInfo>::iterator it1 =
268 kmod_list_.find(accent_1);
269 map<tex_accent, KmodInfo>::iterator it2 =
270 kmod_list_.find(accent_2);
271 if (it1 == kmod_list_.end() || it2 == kmod_list_.end())
274 // Find what key accent_2 is on - should
275 // check about accent_1 also
276 map<char_type, docstring>::iterator it = keymap_.begin();
277 map<char_type, docstring>::iterator end = keymap_.end();
278 for (; it != end; ++it) {
279 if (!it->second.empty()
280 && it->second[0] == 0
281 && it->second[1] == accent_2)
288 allowed = lex.getDocString();
289 LYXERR(Debug::KBMAP, "allowed: " << to_utf8(allowed));
291 insertException(kmod_list_[accent_1].exception_list,
292 it->first, allowed, true, accent_2);
296 unsigned char key_from;
298 LYXERR(Debug::KBMAP, "KMAP:\t" << lex.getString());
303 key_from = lex.getString()[0];
304 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
309 docstring const string_to = lex.getDocString();
310 keymap_[key_from] = string_to;
311 LYXERR(Debug::KBMAP, "\t`" << to_utf8(string_to) << '\'');
319 LYXERR(Debug::KBMAP, "KXMOD:\t" << lex.getString());
324 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
325 accent = getkeymod(lex.getString());
330 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
331 key = lex.getDocString()[0];
336 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
337 str = lex.getDocString();
339 insertException(kmod_list_[accent].exception_list,
343 case Lexer::LEX_FEOF:
344 LYXERR(Debug::PARSER, "End of parsing");
347 lex.printError("ParseKeymapFile: Unknown tag: `$$Token'");
355 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
357 map<tex_accent, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
358 if (cit == kmod_list_.end())
365 docstring const Trans::process(char_type c, TransManager & k)
367 docstring const t = match(c);
369 if (t.empty() && c != 0)
370 return k.normalkey(c);
372 if (!t.empty() && t[0] != 0)
373 return t; //return k.normalkey(c);
375 return k.deadkey(c, kmod_list_[static_cast<tex_accent>(t[1])]);
379 int Trans::load(string const & language)
381 LexerKeyword kmapTags[] = {
388 FileName const filename = libFileSearch("kbd", language, "kmap");
389 if (filename.empty())
394 lex.setFile(filename);
396 int const res = load(lex);
407 tex_accent getkeymod(string const & p)
408 /* return modifier - decoded from p and update p */
410 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
411 LYXERR(Debug::KBMAP, "p = " << p
412 << ", lyx_accent_table[" << i
413 << "].name = `" << lyx_accent_table[i].name << '\'');
415 if (lyx_accent_table[i].name
416 && contains(p, lyx_accent_table[i].name)) {
417 LYXERR(Debug::KBMAP, "Found it!");
418 return static_cast<tex_accent>(i);
425 /////////////////////////////////////////////////////////////////////
429 /////////////////////////////////////////////////////////////////////
433 TransFSMData::TransFSMData()
435 deadkey_ = deadkey2_ = 0;
436 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
441 char_type const TransState::TOKEN_SEP = 4;
445 TransInitState::TransInitState()
451 docstring const TransInitState::normalkey(char_type c)
459 docstring const TransInitState::deadkey(char_type c, KmodInfo d)
463 currentState = deadkey_state_;
469 TransDeadkeyState::TransDeadkeyState()
471 deadkey_state_ = this;
475 docstring const TransDeadkeyState::normalkey(char_type c)
479 KmodException::iterator it = deadkey_info_.exception_list.begin();
480 KmodException::iterator end = deadkey_info_.exception_list.end();
482 for (; it != end; ++it) {
489 res = doAccent(c, deadkey_info_.accent);
491 currentState = init_state_;
496 docstring const TransDeadkeyState::deadkey(char_type c, KmodInfo d)
500 // Check if the same deadkey was typed twice
504 deadkey_info_.accent = TEX_NOACCENT;
505 currentState = init_state_;
509 // Check if it is a combination or an exception
510 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
511 KmodException::const_iterator end = deadkey_info_.exception_list.end();
512 for (; cit != end; ++cit) {
513 if (cit->combined == true && cit->accent == d.accent) {
517 currentState = combined_state_;
523 deadkey_info_.accent = TEX_NOACCENT;
524 currentState = init_state_;
529 // Not a combination or an exception.
530 // Output deadkey1 and keep deadkey2
536 currentState = deadkey_state_;
541 TransCombinedState::TransCombinedState()
543 combined_state_ = this;
547 docstring const TransCombinedState::normalkey(char_type c)
549 docstring const temp = doAccent(c, deadkey2_info_.accent);
550 docstring const res = doAccent(temp, deadkey_info_.accent);
551 currentState = init_state_;
556 docstring const TransCombinedState::deadkey(char_type c, KmodInfo d)
558 // Third key in a row. Output the first one and
559 // reenter with shifted deadkeys
564 deadkey_ = deadkey2_;
565 deadkey_info_ = deadkey2_info_;
566 res += deadkey_state_->deadkey(c, d);
573 : TransFSMData(), TransInitState(), TransDeadkeyState(), TransCombinedState()
575 currentState = init_state_;
581 // Initialize static member.
582 Trans TransManager::default_;
585 TransManager::TransManager()
590 int TransManager::setPrimary(string const & language)
592 if (t1_.getName() == language)
595 return t1_.load(language);
599 int TransManager::setSecondary(string const & language)
601 if (t2_.getName() == language)
604 return t2_.load(language);
608 void TransManager::enablePrimary()
613 LYXERR(Debug::KBMAP, "Enabling primary keymap");
617 void TransManager::enableSecondary()
621 LYXERR(Debug::KBMAP, "Enabling secondary keymap");
625 void TransManager::disableKeymap()
628 LYXERR(Debug::KBMAP, "Disabling keymap");
632 void TransManager::translateAndInsert(char_type c, Text * text, Cursor & cur)
634 docstring res = active_->process(c, *this);
636 // Process with tokens
639 while (res.length() > 0) {
640 res = split(res, temp, TransState::TOKEN_SEP);
641 insert(temp, text, cur);
646 void TransManager::insert(docstring const & str, Text * text, Cursor & cur)
648 for (size_t i = 0, n = str.size(); i != n; ++i)
649 text->insertChar(cur, str[i]);
653 void TransManager::deadkey(char_type c, tex_accent accent, Text * t, Cursor & cur)
655 if (c == 0 && active_ != &default_) {
656 // A deadkey was pressed that cannot be printed
657 // or a accent command was typed in the minibuffer
659 if (active_->isAccentDefined(accent, i) == true) {
660 docstring const res = trans_fsm_
661 .currentState->deadkey(c, i);
667 if (active_ == &default_ || c == 0) {
671 docstring res = trans_fsm_.currentState->deadkey(c, i);
674 // Go through the translation
675 translateAndInsert(c, t, cur);