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 lyx::support::split;
29 extern string const DoAccent(string const &, tex_accent);
30 extern string const DoAccent(char, tex_accent);
34 TransFSMData::TransFSMData()
36 deadkey_ = deadkey2_ = 0;
37 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
42 char const TransState::TOKEN_SEP = 4;
46 TransInitState::TransInitState()
52 string const TransInitState::normalkey(char c)
60 string const TransInitState::deadkey(char c, KmodInfo d)
64 currentState = deadkey_state_;
70 TransDeadkeyState::TransDeadkeyState()
72 deadkey_state_ = this;
76 string const TransDeadkeyState::normalkey(char c)
80 KmodException::iterator it = deadkey_info_.exception_list.begin();
81 KmodException::iterator end = deadkey_info_.exception_list.end();
83 for (; it != end; ++it) {
90 res = DoAccent(c, deadkey_info_.accent);
92 currentState = init_state_;
97 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
101 // Check if the same deadkey was typed twice
105 deadkey_info_.accent = TEX_NOACCENT;
106 currentState = init_state_;
110 // Check if it is a combination or an exception
111 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
112 KmodException::const_iterator end = deadkey_info_.exception_list.end();
113 for (; cit != end; ++cit) {
114 if (cit->combined == true && cit->accent == d.accent) {
118 currentState = combined_state_;
124 deadkey_info_.accent = TEX_NOACCENT;
125 currentState = init_state_;
130 // Not a combination or an exception.
131 // Output deadkey1 and keep deadkey2
137 currentState = deadkey_state_;
142 TransCombinedState::TransCombinedState()
144 combined_state_ = this;
148 string const TransCombinedState::normalkey(char c)
150 string const temp = DoAccent(c, deadkey2_info_.accent);
151 string const res = DoAccent(temp, deadkey_info_.accent);
152 currentState = init_state_;
157 string const TransCombinedState::deadkey(char c, KmodInfo d)
159 // Third key in a row. Output the first one and
160 // reenter with shifted deadkeys
165 deadkey_ = deadkey2_;
166 deadkey_info_ = deadkey2_info_;
167 res += deadkey_state_->deadkey(c, d);
173 TransFSM::TransFSM():
179 currentState = init_state_;
185 // Initialize static member.
186 Trans TransManager::default_;
189 TransManager::TransManager()
190 : active_(0), t1_(new Trans), t2_(new Trans)
194 TransManager::~TransManager()
201 int TransManager::SetPrimary(string const & language)
203 if (t1_->GetName() == language)
206 return t1_->Load(language);
210 int TransManager::SetSecondary(string const & language)
212 if (t2_->GetName() == language)
215 return t2_->Load(language);
219 bool TransManager::setCharset(string const & str)
221 return chset_.loadFile(str);
225 void TransManager::EnablePrimary()
227 if (t1_->IsDefined())
230 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
234 void TransManager::EnableSecondary()
236 if (t2_->IsDefined())
238 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
242 void TransManager::DisableKeymap()
245 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
249 void TransManager::TranslateAndInsert(char c, LyXText * text)
251 string res = active_->process(c, *this);
253 // Process with tokens
256 while (res.length() > 0) {
257 res = split(res, temp, TransState::TOKEN_SEP);
263 void TransManager::insertVerbatim(string const & str, LyXText * text)
265 string::size_type const l = str.length();
267 for (string::size_type i = 0; i < l; ++i) {
268 text->insertChar(str[i]);
273 void TransManager::insert(string const & str, LyXText * text)
275 // Go through the character encoding only if the current
276 // encoding (chset_->name()) matches the current font_norm
277 // (lyrxc->font_norm)
279 // Is false to speak about "only if" the current encoding will
280 // almost always be equal to font_norm.
281 pair<bool, int> enc = chset_.encodeString(str);
282 if (chset_.getName() != lyxrc.font_norm ||
284 // Could not find an encoding
285 InsetLatexAccent ins(str);
286 if (ins.canDisplay()) {
288 new InsetLatexAccent(ins));
290 insertVerbatim(str, text);
295 tmp += static_cast<char>(enc.second);
296 insertVerbatim(tmp, text);
300 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
302 if (c == 0 && active_ != &default_) {
303 // A deadkey was pressed that cannot be printed
304 // or a accent command was typed in the minibuffer
306 if (active_->isAccentDefined(accent, i) == true) {
307 string const res = trans_fsm_
308 .currentState->deadkey(c, i);
314 if (active_ == &default_ || c == 0) {
318 string res = trans_fsm_.currentState->deadkey(c, i);
321 // Go through the translation
322 TranslateAndInsert(c, t);