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/debug.h"
25 #include "support/docstream.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
31 using namespace lyx::support;
35 /////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////
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
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
66 | | semivoiced_sound | |
68 static TeXAccent lyx_accent_table[] = {
69 {TEX_NOACCENT, 0, "", LFUN_NOACTION},
70 {TEX_ACUTE, 0x0301, "acute", LFUN_ACCENT_ACUTE},
71 {TEX_GRAVE, 0x0300, "grave", LFUN_ACCENT_GRAVE},
72 {TEX_MACRON, 0x0304, "macron", LFUN_ACCENT_MACRON},
73 {TEX_TILDE, 0x0303, "tilde", LFUN_ACCENT_TILDE},
74 {TEX_PERISPOMENI, 0x0342, "perispomeni", LFUN_ACCENT_PERISPOMENI},
75 {TEX_UNDERBAR, 0x0320, "underbar", LFUN_ACCENT_UNDERBAR}, // COMBINING MINUS SIGN BELOW or 0x0331 COMBINING MACRON BELOW ?
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 // 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}
92 TeXAccent get_accent(FuncCode action)
95 while (i <= TEX_MAX_ACCENT) {
96 if (lyx_accent_table[i].action == action)
97 return lyx_accent_table[i];
100 struct TeXAccent temp = { static_cast<tex_accent>(0), 0,
101 nullptr, static_cast<FuncCode>(0)};
106 static docstring const doAccent(docstring const & s, tex_accent accent)
109 return docstring(1, lyx_accent_table[accent].ucs4);
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;
120 return normalize_c(os.str());
124 static docstring const doAccent(char_type c, tex_accent accent)
126 return doAccent(docstring(1, c), accent);
131 /////////////////////////////////////////////////////////////////////
135 /////////////////////////////////////////////////////////////////////
138 void Trans::insertException(KmodException & exclist, char_type c,
139 docstring const & data, bool flag, tex_accent accent)
146 exclist.insert(exclist.begin(), p);
148 // exclist.push_back(p);
152 void Trans::freeException(KmodException & exclist)
158 void Trans::freeKeymap()
165 bool Trans::isDefined() const
167 return !name_.empty();
179 tex_accent getkeymod(string const &);
182 void Trans::addDeadkey(tex_accent accent, docstring const & keys)
187 kmod_list_[accent] = tmp;
189 for (char_type key : keys) {
190 // FIXME This is a hack.
191 // tmp is no valid UCS4 string, but misused to store the
194 tmpd += char_type(0);
195 tmpd += char_type(accent);
201 int Trans::load(Lexer & lex)
207 LYXERR(Debug::KBMAP, "KMOD:\t" << lex.getString());
211 LYXERR(Debug::KBMAP, "key\t`" << lex.getString() << '\'');
213 docstring const keys = lex.getDocString();
218 LYXERR(Debug::KBMAP, "accent\t`" << lex.getString() << '\'');
220 tex_accent accent = getkeymod(lex.getString());
222 if (accent == TEX_NOACCENT)
226 // FIXME: This code should be removed...
227 // But we need to fix up all the kmap files first
228 // so that this field is not present anymore.
232 LYXERR(Debug::KBMAP, "allowed\t`" << lex.getString() << '\'');
234 /* string const allowed = lex.getString(); */
235 addDeadkey(accent, keys /*, allowed*/);
237 addDeadkey(accent, keys);
244 LYXERR(Debug::KBMAP, "KCOMB:");
248 str = lex.getString();
249 LYXERR(Debug::KBMAP, str);
251 tex_accent accent_1 = getkeymod(str);
252 if (accent_1 == TEX_NOACCENT)
258 str = lex.getString();
259 LYXERR(Debug::KBMAP, str);
261 tex_accent accent_2 = getkeymod(str);
262 if (accent_2 == TEX_NOACCENT) return -1;
264 map<tex_accent, KmodInfo>::iterator it1 =
265 kmod_list_.find(accent_1);
266 map<tex_accent, KmodInfo>::iterator it2 =
267 kmod_list_.find(accent_2);
268 if (it1 == kmod_list_.end() || it2 == kmod_list_.end())
271 // Find what key accent_2 is on - should
272 // check about accent_1 also
273 map<char_type, docstring>::iterator it = keymap_.begin();
274 map<char_type, docstring>::iterator end = keymap_.end();
275 for (; it != end; ++it) {
276 if (!it->second.empty()
277 && it->second[0] == 0
278 && it->second[1] == accent_2)
282 // could not find accent2 on a key -- this should not happen.
289 allowed = lex.getDocString();
290 LYXERR(Debug::KBMAP, "allowed: " << to_utf8(allowed));
292 insertException(kmod_list_[accent_1].exception_list,
293 it->first, allowed, true, accent_2);
297 unsigned char key_from;
299 LYXERR(Debug::KBMAP, "KMAP:\t" << lex.getString());
304 key_from = static_cast<unsigned char>(lex.getString()[0]);
305 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
310 docstring const string_to = lex.getDocString();
311 keymap_[key_from] = string_to;
312 LYXERR(Debug::KBMAP, "\t`" << to_utf8(string_to) << '\'');
320 LYXERR(Debug::KBMAP, "KXMOD:\t" << lex.getString());
325 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
326 accent = getkeymod(lex.getString());
331 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
332 key = lex.getDocString()[0];
337 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
338 str = lex.getDocString();
340 insertException(kmod_list_[accent].exception_list,
344 case Lexer::LEX_FEOF:
345 LYXERR(Debug::PARSER, "End of parsing");
348 lex.printError("ParseKeymapFile: Unknown tag: `$$Token'");
356 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
358 map<tex_accent, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
359 if (cit == kmod_list_.end())
366 docstring const Trans::process(char_type c, TransManager & k)
368 docstring const t = match(c);
370 if (t.empty() && c != 0)
371 return k.normalkey(c);
373 if (!t.empty() && t[0] != 0)
374 return t; //return k.normalkey(c);
376 return k.deadkey(c, kmod_list_[static_cast<tex_accent>(t[1])]);
380 int Trans::load(string const & language)
382 LexerKeyword kmapTags[] = {
389 FileName const filename = libFileSearch("kbd", language, "kmap");
390 if (filename.empty())
395 lex.setFile(filename);
397 int const res = load(lex);
408 tex_accent getkeymod(string const & p)
409 /* return modifier - decoded from p and update p */
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 << '\'');
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);
426 /////////////////////////////////////////////////////////////////////
430 /////////////////////////////////////////////////////////////////////
434 TransFSMData::TransFSMData() : deadkey_(0), deadkey2_(0), init_state_(nullptr),
435 deadkey_state_(nullptr), combined_state_(nullptr), currentState(nullptr)
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 && 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)) {
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);