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"
20 #include "support/debug.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27 #include "support/convert.h"
28 #include "support/docstream.h"
33 using support::contains;
34 using support::libFileSearch;
42 /////////////////////////////////////////////////////////////////////
46 /////////////////////////////////////////////////////////////////////
48 /* the names used by TeX and XWindows for deadkeys/accents are not the same
49 so here follows a table to clearify the differences. Please correct this
52 |------------------|------------------|------------------|--------------|
53 | TeX | XWindows | \bind/LFUN | used by intl |
54 |------------------|------------------|------------------|--------------|
55 | grave | grave |LFUN_ACCENT_GRAVE | grave
56 | acute | acute |LFUN_ACCENT_ACUTE | acute
57 | circumflex | circumflex |LFUN_ACCENT_CIRCUMFLEX | circumflex
58 | umlaut/dieresis | diaeresis |LFUN_ACCENT_UMLAUT | umlaut
59 | tilde | tilde |LFUN_ACCENT_TILDE | tilde
60 | macron | maron |LFUN_ACCENT_MACRON | macron
61 | dot | abovedot |LFUN_ACCENT_DOT | dot
62 | cedilla | cedilla |LFUN_ACCENT_CEDILLA | cedilla
63 | underdot | |LFUN_ACCENT_UNDERDOT | underdot
64 | underbar | |LFUN_ACCENT_UNDERBAR | underbar
65 | hácek | caron |LFUN_ACCENT_CARON | caron
66 | breve | breve |LFUN_ACCENT_BREVE | breve
67 | tie | |LFUN_ACCENT_TIE | tie
68 | Hungarian umlaut | doubleacute |LFUN_ACCENT_HUNGARIAN_UMLAUT | hungarian umlaut
69 | circle | abovering |LFUN_ACCENT_CIRCLE | circle
73 | | semivoiced_sound | |
74 | | |LFUN_ACCENT_SPECIAL_CARON| special caron
76 static tex_accent_struct lyx_accent_table[] = {
77 {TEX_NOACCENT, 0, "", LFUN_NOACTION},
78 {TEX_ACUTE, 0x0301, "acute", LFUN_ACCENT_ACUTE},
79 {TEX_GRAVE, 0x0300, "grave", LFUN_ACCENT_GRAVE},
80 {TEX_MACRON, 0x0304, "macron", LFUN_ACCENT_MACRON},
81 {TEX_TILDE, 0x0303, "tilde", LFUN_ACCENT_TILDE},
82 {TEX_UNDERBAR, 0x0320, "underbar", LFUN_ACCENT_UNDERBAR},
83 {TEX_CEDILLA, 0x0327, "cedilla", LFUN_ACCENT_CEDILLA},
84 {TEX_UNDERDOT, 0x0323, "underdot", LFUN_ACCENT_UNDERDOT},
85 {TEX_CIRCUMFLEX, 0x0302, "circumflex", LFUN_ACCENT_CIRCUMFLEX},
86 {TEX_CIRCLE, 0x030a, "circle", LFUN_ACCENT_CIRCLE},
87 {TEX_TIE, 0x0361, "tie", LFUN_ACCENT_TIE},
88 {TEX_BREVE, 0x0306, "breve", LFUN_ACCENT_BREVE},
89 {TEX_CARON, 0x030c, "caron", LFUN_ACCENT_CARON},
90 // {TEX_SPECIAL_CARON, 0x030c, "ooo", LFUN_ACCENT_SPECIAL_CARON},
91 // Don't fix this typo for compatibility reasons!
92 {TEX_HUNGUML, 0x030b, "hugarian_umlaut", LFUN_ACCENT_HUNGARIAN_UMLAUT},
93 {TEX_UMLAUT, 0x0308, "umlaut", LFUN_ACCENT_UMLAUT},
94 {TEX_DOT, 0x0307, "dot", LFUN_ACCENT_DOT},
95 {TEX_OGONEK, 0x0328, "ogonek", LFUN_ACCENT_OGONEK}
99 tex_accent_struct get_accent(kb_action action)
102 while (i <= TEX_MAX_ACCENT) {
103 if (lyx_accent_table[i].action == action)
104 return lyx_accent_table[i];
107 struct tex_accent_struct temp = { static_cast<tex_accent>(0), 0,
108 0, static_cast<kb_action>(0)};
113 static docstring const doAccent(docstring const & s, tex_accent accent)
116 return docstring(1, lyx_accent_table[accent].ucs4);
120 os.put(lyx_accent_table[accent].ucs4);
121 if (s.length() > 1) {
122 if (accent != TEX_TIE || s.length() > 2)
123 lyxerr << "Warning: Too many characters given for accent "
124 << lyx_accent_table[accent].name << '.' << std::endl;
127 return normalize_c(os.str());
131 static docstring const doAccent(char_type c, tex_accent accent)
133 return doAccent(docstring(1, c), accent);
138 /////////////////////////////////////////////////////////////////////
142 /////////////////////////////////////////////////////////////////////
145 void Trans::insertException(KmodException & exclist, char_type c,
146 docstring const & data, bool flag, tex_accent accent)
153 exclist.insert(exclist.begin(), p);
155 // exclist.push_back(p);
159 void Trans::freeException(KmodException & exclist)
165 void Trans::freeKeymap()
172 bool Trans::isDefined() const
174 return !name_.empty();
187 struct keyword_item kmapTags[K_LAST - 1] = {
195 tex_accent getkeymod(string const &);
198 void Trans::addDeadkey(tex_accent accent, docstring const & keys)
203 kmod_list_[accent] = tmp;
205 for (docstring::size_type i = 0; i < keys.length(); ++i) {
206 // FIXME This is a hack.
207 // tmp is no valid UCS4 string, but misused to store the
211 tmp += char_type(accent);
212 keymap_[keys[i]] = tmp;
217 int Trans::load(Lexer & lex)
221 while (lex.isOK() && !error) {
225 LYXERR(Debug::KBMAP, "KMOD:\t" << lex.getString());
229 LYXERR(Debug::KBMAP, "key\t`" << lex.getString() << '\'');
231 docstring const keys = lex.getDocString();
236 LYXERR(Debug::KBMAP, "accent\t`" << lex.getString() << '\'');
238 tex_accent accent = getkeymod(lex.getString());
240 if (accent == TEX_NOACCENT)
244 // FIXME: This code should be removed...
245 // But we need to fix up all the kmap files first
246 // so that this field is not present anymore.
250 LYXERR(Debug::KBMAP, "allowed\t`" << lex.getString() << '\'');
252 /* string const allowed = lex.getString(); */
253 addDeadkey(accent, keys /*, allowed*/);
255 addDeadkey(accent, keys);
262 LYXERR(Debug::KBMAP, "KCOMB:");
266 str = lex.getString();
267 LYXERR(Debug::KBMAP, str);
269 tex_accent accent_1 = getkeymod(str);
270 if (accent_1 == TEX_NOACCENT)
276 str = lex.getString();
277 LYXERR(Debug::KBMAP, str);
279 tex_accent accent_2 = getkeymod(str);
280 if (accent_2 == TEX_NOACCENT) return -1;
282 map<tex_accent, KmodInfo>::iterator it1 =
283 kmod_list_.find(accent_1);
284 map<tex_accent, KmodInfo>::iterator it2 =
285 kmod_list_.find(accent_2);
286 if (it1 == kmod_list_.end() || it2 == kmod_list_.end())
289 // Find what key accent_2 is on - should
290 // check about accent_1 also
291 map<char_type, docstring>::iterator it = keymap_.begin();
292 map<char_type, docstring>::iterator end = keymap_.end();
293 for (; it != end; ++it) {
294 if (!it->second.empty()
295 && it->second[0] == 0
296 && it->second[1] == accent_2)
303 allowed = lex.getDocString();
304 LYXERR(Debug::KBMAP, "allowed: " << to_utf8(allowed));
306 insertException(kmod_list_[accent_1].exception_list,
307 it->first, allowed, true, accent_2);
311 unsigned char key_from;
313 LYXERR(Debug::KBMAP, "KMAP:\t" << lex.getString());
318 key_from = lex.getString()[0];
319 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
324 docstring const string_to = lex.getDocString();
325 keymap_[key_from] = string_to;
326 LYXERR(Debug::KBMAP, "\t`" << to_utf8(string_to) << '\'');
334 LYXERR(Debug::KBMAP, "KXMOD:\t" << lex.getString());
339 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
340 accent = getkeymod(lex.getString());
345 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
346 key = lex.getDocString()[0];
351 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
352 str = lex.getDocString();
354 insertException(kmod_list_[accent].exception_list,
358 case Lexer::LEX_FEOF:
359 LYXERR(Debug::PARSER, "End of parsing");
362 lex.printError("ParseKeymapFile: Unknown tag: `$$Token'");
370 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
372 map<tex_accent, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
373 if (cit == kmod_list_.end())
380 docstring const Trans::process(char_type c, TransManager & k)
382 docstring const t = match(c);
384 if (t.empty() && c != 0)
385 return k.normalkey(c);
387 if (!t.empty() && t[0] != 0)
388 return t; //return k.normalkey(c);
390 return k.deadkey(c, kmod_list_[static_cast<tex_accent>(t[1])]);
394 int Trans::load(string const & language)
396 support::FileName const filename = libFileSearch("kbd", language, "kmap");
397 if (filename.empty())
401 Lexer lex(kmapTags, K_LAST - 1);
402 lex.setFile(filename);
404 int const res = load(lex);
415 tex_accent getkeymod(string const & p)
416 /* return modifier - decoded from p and update p */
418 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
419 LYXERR(Debug::KBMAP, "p = " << p
420 << ", lyx_accent_table[" << i
421 << "].name = `" << lyx_accent_table[i].name << '\'');
423 if (lyx_accent_table[i].name
424 && contains(p, lyx_accent_table[i].name)) {
425 LYXERR(Debug::KBMAP, "Found it!");
426 return static_cast<tex_accent>(i);
433 /////////////////////////////////////////////////////////////////////
437 /////////////////////////////////////////////////////////////////////
441 TransFSMData::TransFSMData()
443 deadkey_ = deadkey2_ = 0;
444 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
449 char_type const TransState::TOKEN_SEP = 4;
453 TransInitState::TransInitState()
459 docstring const TransInitState::normalkey(char_type c)
467 docstring const TransInitState::deadkey(char_type c, KmodInfo d)
471 currentState = deadkey_state_;
477 TransDeadkeyState::TransDeadkeyState()
479 deadkey_state_ = this;
483 docstring const TransDeadkeyState::normalkey(char_type c)
487 KmodException::iterator it = deadkey_info_.exception_list.begin();
488 KmodException::iterator end = deadkey_info_.exception_list.end();
490 for (; it != end; ++it) {
497 res = doAccent(c, deadkey_info_.accent);
499 currentState = init_state_;
504 docstring const TransDeadkeyState::deadkey(char_type c, KmodInfo d)
508 // Check if the same deadkey was typed twice
512 deadkey_info_.accent = TEX_NOACCENT;
513 currentState = init_state_;
517 // Check if it is a combination or an exception
518 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
519 KmodException::const_iterator end = deadkey_info_.exception_list.end();
520 for (; cit != end; ++cit) {
521 if (cit->combined == true && cit->accent == d.accent) {
525 currentState = combined_state_;
531 deadkey_info_.accent = TEX_NOACCENT;
532 currentState = init_state_;
537 // Not a combination or an exception.
538 // Output deadkey1 and keep deadkey2
544 currentState = deadkey_state_;
549 TransCombinedState::TransCombinedState()
551 combined_state_ = this;
555 docstring const TransCombinedState::normalkey(char_type c)
557 docstring const temp = doAccent(c, deadkey2_info_.accent);
558 docstring const res = doAccent(temp, deadkey_info_.accent);
559 currentState = init_state_;
564 docstring const TransCombinedState::deadkey(char_type c, KmodInfo d)
566 // Third key in a row. Output the first one and
567 // reenter with shifted deadkeys
572 deadkey_ = deadkey2_;
573 deadkey_info_ = deadkey2_info_;
574 res += deadkey_state_->deadkey(c, d);
581 : TransFSMData(), TransInitState(), TransDeadkeyState(), TransCombinedState()
583 currentState = init_state_;
589 // Initialize static member.
590 Trans TransManager::default_;
593 TransManager::TransManager()
598 int TransManager::setPrimary(string const & language)
600 if (t1_.getName() == language)
603 return t1_.load(language);
607 int TransManager::setSecondary(string const & language)
609 if (t2_.getName() == language)
612 return t2_.load(language);
616 void TransManager::enablePrimary()
621 LYXERR(Debug::KBMAP, "Enabling primary keymap");
625 void TransManager::enableSecondary()
629 LYXERR(Debug::KBMAP, "Enabling secondary keymap");
633 void TransManager::disableKeymap()
636 LYXERR(Debug::KBMAP, "Disabling keymap");
640 void TransManager::translateAndInsert(char_type c, Text * text, Cursor & cur)
642 docstring res = active_->process(c, *this);
644 // Process with tokens
647 while (res.length() > 0) {
648 res = split(res, temp, TransState::TOKEN_SEP);
649 insert(temp, text, cur);
654 void TransManager::insert(docstring const & str, Text * text, Cursor & cur)
656 for (size_t i = 0, n = str.size(); i != n; ++i)
657 text->insertChar(cur, str[i]);
661 void TransManager::deadkey(char_type c, tex_accent accent, Text * t, Cursor & cur)
663 if (c == 0 && active_ != &default_) {
664 // A deadkey was pressed that cannot be printed
665 // or a accent command was typed in the minibuffer
667 if (active_->isAccentDefined(accent, i) == true) {
668 docstring const res = trans_fsm_
669 .currentState->deadkey(c, i);
675 if (active_ == &default_ || c == 0) {
679 docstring res = trans_fsm_.currentState->deadkey(c, i);
682 // Go through the translation
683 translateAndInsert(c, t, cur);