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"
20 #include "insets/insetlatexaccent.h"
21 #include "BufferView.h"
24 #include "support/lstrings.h"
26 using namespace lyx::support;
31 extern string const DoAccent(string const &, tex_accent);
32 extern string const DoAccent(char, tex_accent);
36 TransFSMData::TransFSMData()
38 deadkey_ = deadkey2_ = 0;
39 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
44 char const TransState::TOKEN_SEP = 4;
48 TransInitState::TransInitState()
54 string const TransInitState::normalkey(char c)
62 string const TransInitState::deadkey(char c, KmodInfo d)
66 currentState = deadkey_state_;
72 TransDeadkeyState::TransDeadkeyState()
74 deadkey_state_ = this;
78 string const TransDeadkeyState::normalkey(char c)
82 KmodException::iterator it = deadkey_info_.exception_list.begin();
83 KmodException::iterator end = deadkey_info_.exception_list.end();
85 for (; it != end; ++it) {
92 res = DoAccent(c, deadkey_info_.accent);
94 currentState = init_state_;
99 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
103 // Check if the same deadkey was typed twice
107 deadkey_info_.accent = TEX_NOACCENT;
108 currentState = init_state_;
112 // Check if it is a combination or an exception
113 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
114 KmodException::const_iterator end = deadkey_info_.exception_list.end();
115 for (; cit != end; ++cit) {
116 if (cit->combined == true && cit->accent == d.accent) {
120 currentState = combined_state_;
126 deadkey_info_.accent = TEX_NOACCENT;
127 currentState = init_state_;
132 // Not a combination or an exception.
133 // Output deadkey1 and keep deadkey2
139 currentState = deadkey_state_;
144 TransCombinedState::TransCombinedState()
146 combined_state_ = this;
150 string const TransCombinedState::normalkey(char c)
152 string const temp = DoAccent(c, deadkey2_info_.accent);
153 string const res = DoAccent(temp, deadkey_info_.accent);
154 currentState = init_state_;
159 string const TransCombinedState::deadkey(char c, KmodInfo d)
161 // Third key in a row. Output the first one and
162 // reenter with shifted deadkeys
167 deadkey_ = deadkey2_;
168 deadkey_info_ = deadkey2_info_;
169 res += deadkey_state_->deadkey(c, d);
175 TransFSM::TransFSM():
181 currentState = init_state_;
187 // Initialize static member.
188 Trans TransManager::default_;
191 TransManager::TransManager()
192 : active_(0), t1_(new Trans), t2_(new Trans)
196 TransManager::~TransManager()
203 int TransManager::SetPrimary(string const & language)
205 if (t1_->GetName() == language)
208 return t1_->Load(language);
212 int TransManager::SetSecondary(string const & language)
214 if (t2_->GetName() == language)
217 return t2_->Load(language);
221 bool TransManager::setCharset(string const & str)
223 return chset_.loadFile(str);
227 void TransManager::EnablePrimary()
229 if (t1_->IsDefined())
232 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
236 void TransManager::EnableSecondary()
238 if (t2_->IsDefined())
240 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
244 void TransManager::DisableKeymap()
247 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
251 void TransManager::TranslateAndInsert(char c, LyXText * text)
253 string res = active_->process(c, *this);
255 // Process with tokens
258 while (res.length() > 0) {
259 res = split(res, temp, TransState::TOKEN_SEP);
265 void TransManager::insertVerbatim(string const & str, LyXText * text)
267 string::size_type const l = str.length();
269 for (string::size_type i = 0; i < l; ++i) {
270 text->insertChar(str[i]);
275 void TransManager::insert(string const & str, LyXText * text)
277 // Go through the character encoding only if the current
278 // encoding (chset_->name()) matches the current font_norm
279 // (lyrxc->font_norm)
281 // Is false to speak about "only if" the current encoding will
282 // almost always be equal to font_norm.
283 pair<bool, int> enc = chset_.encodeString(str);
284 if (chset_.getName() != lyxrc.font_norm ||
286 // Could not find an encoding
287 InsetLatexAccent ins(str);
288 if (ins.canDisplay()) {
290 new InsetLatexAccent(ins));
292 insertVerbatim(str, text);
297 tmp += static_cast<char>(enc.second);
298 insertVerbatim(tmp, text);
302 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
304 if (c == 0 && active_ != &default_) {
305 // A deadkey was pressed that cannot be printed
306 // or a accent command was typed in the minibuffer
308 if (active_->isAccentDefined(accent, i) == true) {
309 string const res = trans_fsm_
310 .currentState->deadkey(c, i);
316 if (active_ == &default_ || c == 0) {
320 string res = trans_fsm_.currentState->deadkey(c, i);
323 // Go through the translation
324 TranslateAndInsert(c, t);