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 // For the sake of boost::scoped_ptr.
195 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);