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.
14 #include "trans_mgr.h"
18 #include "insets/insetlatexaccent.h"
20 #include "support/lstrings.h"
22 using namespace lyx::support;
27 extern string const DoAccent(string const &, tex_accent);
28 extern string const DoAccent(char, tex_accent);
32 TransFSMData::TransFSMData()
34 deadkey_ = deadkey2_ = 0;
35 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
40 char const TransState::TOKEN_SEP = 4;
44 TransInitState::TransInitState()
50 string const TransInitState::normalkey(char c)
58 string const TransInitState::deadkey(char c, KmodInfo d)
62 currentState = deadkey_state_;
68 TransDeadkeyState::TransDeadkeyState()
70 deadkey_state_ = this;
74 string const TransDeadkeyState::normalkey(char c)
78 KmodException::iterator it = deadkey_info_.exception_list.begin();
79 KmodException::iterator end = deadkey_info_.exception_list.end();
81 for (; it != end; ++it) {
88 res = DoAccent(c, deadkey_info_.accent);
90 currentState = init_state_;
95 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
99 // Check if the same deadkey was typed twice
103 deadkey_info_.accent = TEX_NOACCENT;
104 currentState = init_state_;
108 // Check if it is a combination or an exception
109 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
110 KmodException::const_iterator end = deadkey_info_.exception_list.end();
111 for (; cit != end; ++cit) {
112 if (cit->combined == true && cit->accent == d.accent) {
116 currentState = combined_state_;
122 deadkey_info_.accent = TEX_NOACCENT;
123 currentState = init_state_;
128 // Not a combination or an exception.
129 // Output deadkey1 and keep deadkey2
135 currentState = deadkey_state_;
140 TransCombinedState::TransCombinedState()
142 combined_state_ = this;
146 string const TransCombinedState::normalkey(char c)
148 string const temp = DoAccent(c, deadkey2_info_.accent);
149 string const res = DoAccent(temp, deadkey_info_.accent);
150 currentState = init_state_;
155 string const TransCombinedState::deadkey(char c, KmodInfo d)
157 // Third key in a row. Output the first one and
158 // reenter with shifted deadkeys
163 deadkey_ = deadkey2_;
164 deadkey_info_ = deadkey2_info_;
165 res += deadkey_state_->deadkey(c, d);
171 TransFSM::TransFSM():
177 currentState = init_state_;
183 // Initialize static member.
184 Trans TransManager::default_;
187 TransManager::TransManager()
188 : active_(0), t1_(new Trans), t2_(new Trans)
192 TransManager::~TransManager()
199 int TransManager::SetPrimary(string const & language)
201 if (t1_->GetName() == language)
204 return t1_->Load(language);
208 int TransManager::SetSecondary(string const & language)
210 if (t2_->GetName() == language)
213 return t2_->Load(language);
217 bool TransManager::setCharset(string const & str)
219 return chset_.loadFile(str);
223 void TransManager::EnablePrimary()
225 if (t1_->IsDefined())
228 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
232 void TransManager::EnableSecondary()
234 if (t2_->IsDefined())
236 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
240 void TransManager::DisableKeymap()
243 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
247 void TransManager::TranslateAndInsert(char c, LyXText * text)
249 string res = active_->process(c, *this);
251 // Process with tokens
254 while (res.length() > 0) {
255 res = split(res, temp, TransState::TOKEN_SEP);
261 void TransManager::insertVerbatim(string const & str, LyXText * text)
263 string::size_type const l = str.length();
265 for (string::size_type i = 0; i < l; ++i) {
266 text->insertChar(str[i]);
271 void TransManager::insert(string const & str, LyXText * text)
273 // Go through the character encoding only if the current
274 // encoding (chset_->name()) matches the current font_norm
275 // (lyrxc->font_norm)
277 // Is false to speak about "only if" the current encoding will
278 // almost always be equal to font_norm.
279 pair<bool, int> enc = chset_.encodeString(str);
280 if (chset_.getName() != lyxrc.font_norm ||
282 // Could not find an encoding
283 InsetLatexAccent ins(str);
284 if (ins.canDisplay()) {
286 new InsetLatexAccent(ins));
288 insertVerbatim(str, text);
293 tmp += static_cast<char>(enc.second);
294 insertVerbatim(tmp, text);
298 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
300 if (c == 0 && active_ != &default_) {
301 // A deadkey was pressed that cannot be printed
302 // or a accent command was typed in the minibuffer
304 if (active_->isAccentDefined(accent, i) == true) {
305 string const res = trans_fsm_
306 .currentState->deadkey(c, i);
312 if (active_ == &default_ || c == 0) {
316 string res = trans_fsm_.currentState->deadkey(c, i);
319 // Go through the translation
320 TranslateAndInsert(c, t);