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 0, 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 (docstring::size_type i = 0; i < keys.length(); ++i) {
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);
196 keymap_[keys[i]] = tmpd;
201 int Trans::load(Lexer & lex)
205 while (lex.isOK() && !error) {
209 LYXERR(Debug::KBMAP, "KMOD:\t" << lex.getString());
213 LYXERR(Debug::KBMAP, "key\t`" << lex.getString() << '\'');
215 docstring const keys = lex.getDocString();
220 LYXERR(Debug::KBMAP, "accent\t`" << lex.getString() << '\'');
222 tex_accent accent = getkeymod(lex.getString());
224 if (accent == TEX_NOACCENT)
228 // FIXME: This code should be removed...
229 // But we need to fix up all the kmap files first
230 // so that this field is not present anymore.
234 LYXERR(Debug::KBMAP, "allowed\t`" << lex.getString() << '\'');
236 /* string const allowed = lex.getString(); */
237 addDeadkey(accent, keys /*, allowed*/);
239 addDeadkey(accent, keys);
246 LYXERR(Debug::KBMAP, "KCOMB:");
250 str = lex.getString();
251 LYXERR(Debug::KBMAP, str);
253 tex_accent accent_1 = getkeymod(str);
254 if (accent_1 == TEX_NOACCENT)
260 str = lex.getString();
261 LYXERR(Debug::KBMAP, str);
263 tex_accent accent_2 = getkeymod(str);
264 if (accent_2 == TEX_NOACCENT) return -1;
266 map<tex_accent, KmodInfo>::iterator it1 =
267 kmod_list_.find(accent_1);
268 map<tex_accent, KmodInfo>::iterator it2 =
269 kmod_list_.find(accent_2);
270 if (it1 == kmod_list_.end() || it2 == kmod_list_.end())
273 // Find what key accent_2 is on - should
274 // check about accent_1 also
275 map<char_type, docstring>::iterator it = keymap_.begin();
276 map<char_type, docstring>::iterator end = keymap_.end();
277 for (; it != end; ++it) {
278 if (!it->second.empty()
279 && it->second[0] == 0
280 && it->second[1] == accent_2)
284 // could not find accent2 on a key -- this should not happen.
291 allowed = lex.getDocString();
292 LYXERR(Debug::KBMAP, "allowed: " << to_utf8(allowed));
294 insertException(kmod_list_[accent_1].exception_list,
295 it->first, allowed, true, accent_2);
299 unsigned char key_from;
301 LYXERR(Debug::KBMAP, "KMAP:\t" << lex.getString());
306 key_from = lex.getString()[0];
307 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
312 docstring const string_to = lex.getDocString();
313 keymap_[key_from] = string_to;
314 LYXERR(Debug::KBMAP, "\t`" << to_utf8(string_to) << '\'');
322 LYXERR(Debug::KBMAP, "KXMOD:\t" << lex.getString());
327 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
328 accent = getkeymod(lex.getString());
333 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
334 key = lex.getDocString()[0];
339 LYXERR(Debug::KBMAP, "\t`" << lex.getString() << '\'');
340 str = lex.getDocString();
342 insertException(kmod_list_[accent].exception_list,
346 case Lexer::LEX_FEOF:
347 LYXERR(Debug::PARSER, "End of parsing");
350 lex.printError("ParseKeymapFile: Unknown tag: `$$Token'");
358 bool Trans::isAccentDefined(tex_accent accent, KmodInfo & i) const
360 map<tex_accent, KmodInfo>::const_iterator cit = kmod_list_.find(accent);
361 if (cit == kmod_list_.end())
368 docstring const Trans::process(char_type c, TransManager & k)
370 docstring const t = match(c);
372 if (t.empty() && c != 0)
373 return k.normalkey(c);
375 if (!t.empty() && t[0] != 0)
376 return t; //return k.normalkey(c);
378 return k.deadkey(c, kmod_list_[static_cast<tex_accent>(t[1])]);
382 int Trans::load(string const & language)
384 LexerKeyword kmapTags[] = {
391 FileName const filename = libFileSearch("kbd", language, "kmap");
392 if (filename.empty())
397 lex.setFile(filename);
399 int const res = load(lex);
410 tex_accent getkeymod(string const & p)
411 /* return modifier - decoded from p and update p */
413 for (int i = 1; i <= TEX_MAX_ACCENT; ++i) {
414 LYXERR(Debug::KBMAP, "p = " << p
415 << ", lyx_accent_table[" << i
416 << "].name = `" << lyx_accent_table[i].name << '\'');
418 if (lyx_accent_table[i].name
419 && contains(p, lyx_accent_table[i].name)) {
420 LYXERR(Debug::KBMAP, "Found it!");
421 return static_cast<tex_accent>(i);
428 /////////////////////////////////////////////////////////////////////
432 /////////////////////////////////////////////////////////////////////
436 TransFSMData::TransFSMData() : deadkey_(0), deadkey2_(0), init_state_(0),
437 deadkey_state_(0), combined_state_(0), currentState(0)
443 char_type const TransState::TOKEN_SEP = 4;
447 TransInitState::TransInitState()
453 docstring const TransInitState::normalkey(char_type c)
461 docstring const TransInitState::deadkey(char_type c, KmodInfo d)
465 currentState = deadkey_state_;
471 TransDeadkeyState::TransDeadkeyState()
473 deadkey_state_ = this;
477 docstring const TransDeadkeyState::normalkey(char_type c)
481 KmodException::iterator it = deadkey_info_.exception_list.begin();
482 KmodException::iterator end = deadkey_info_.exception_list.end();
484 for (; it != end; ++it) {
491 res = doAccent(c, deadkey_info_.accent);
493 currentState = init_state_;
498 docstring const TransDeadkeyState::deadkey(char_type c, KmodInfo d)
502 // Check if the same deadkey was typed twice
506 deadkey_info_.accent = TEX_NOACCENT;
507 currentState = init_state_;
511 // Check if it is a combination or an exception
512 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
513 KmodException::const_iterator end = deadkey_info_.exception_list.end();
514 for (; cit != end; ++cit) {
515 if (cit->combined == true && cit->accent == d.accent) {
519 currentState = combined_state_;
525 deadkey_info_.accent = TEX_NOACCENT;
526 currentState = init_state_;
531 // Not a combination or an exception.
532 // Output deadkey1 and keep deadkey2
538 currentState = deadkey_state_;
543 TransCombinedState::TransCombinedState()
545 combined_state_ = this;
549 docstring const TransCombinedState::normalkey(char_type c)
551 docstring const temp = doAccent(c, deadkey2_info_.accent);
552 docstring const res = doAccent(temp, deadkey_info_.accent);
553 currentState = init_state_;
558 docstring const TransCombinedState::deadkey(char_type c, KmodInfo d)
560 // Third key in a row. Output the first one and
561 // reenter with shifted deadkeys
566 deadkey_ = deadkey2_;
567 deadkey_info_ = deadkey2_info_;
568 res += deadkey_state_->deadkey(c, d);
575 : TransFSMData(), TransInitState(), TransDeadkeyState(), TransCombinedState()
577 currentState = init_state_;
583 // Initialize static member.
584 Trans TransManager::default_;
587 TransManager::TransManager()
592 int TransManager::setPrimary(string const & language)
594 if (t1_.getName() == language)
597 return t1_.load(language);
601 int TransManager::setSecondary(string const & language)
603 if (t2_.getName() == language)
606 return t2_.load(language);
610 void TransManager::enablePrimary()
615 LYXERR(Debug::KBMAP, "Enabling primary keymap");
619 void TransManager::enableSecondary()
623 LYXERR(Debug::KBMAP, "Enabling secondary keymap");
627 void TransManager::disableKeymap()
630 LYXERR(Debug::KBMAP, "Disabling keymap");
634 void TransManager::translateAndInsert(char_type c, Text * text, Cursor & cur)
636 docstring res = active_->process(c, *this);
638 // Process with tokens
641 while (res.length() > 0) {
642 res = split(res, temp, TransState::TOKEN_SEP);
643 insert(temp, text, cur);
648 void TransManager::insert(docstring const & str, Text * text, Cursor & cur)
650 for (size_t i = 0, n = str.size(); i != n; ++i)
651 text->insertChar(cur, str[i]);
655 void TransManager::deadkey(char_type c, tex_accent accent, Text * t, Cursor & cur)
657 if (c == 0 && active_ != &default_) {
658 // A deadkey was pressed that cannot be printed
659 // or a accent command was typed in the minibuffer
661 if (active_->isAccentDefined(accent, i) == true) {
662 docstring const res = trans_fsm_
663 .currentState->deadkey(c, i);
669 if (active_ == &default_ || c == 0) {
673 docstring res = trans_fsm_.currentState->deadkey(c, i);
676 // Go through the translation
677 translateAndInsert(c, t, cur);