4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
23 extern string const DoAccent(string const &, tex_accent);
24 extern string const DoAccent(char, tex_accent);
25 extern BufferView * current_view;
29 TransFSMData::TransFSMData()
31 deadkey_ = deadkey2_ = 0;
32 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
38 char const TransState::TOKEN_SEP = 4;
42 TransInitState::TransInitState()
48 string const TransInitState::normalkey(char c, string const & t)
51 if (!t.empty()) res = t;
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, string const & trans)
78 // Check if it is an exception
79 KmodException l = deadkey_info_.exception_list;
89 // Not an exception. Check if it allowed
90 if (countChar(deadkey_info_.allowed, c) > 0) {
92 res = DoAccent(c, deadkey_info_.accent);
103 currentState = init_state_;
108 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
112 // Check if the same deadkey was typed twice
116 deadkey_info_.accent = TEX_NOACCENT;
117 currentState = init_state_;
121 // Check if it is a combination or an exception
123 l = deadkey_info_.exception_list;
126 if (l->combined == true && l->accent == d.accent) {
130 currentState = combined_state_;
136 deadkey_info_.accent = TEX_NOACCENT;
137 currentState = init_state_;
143 // Not a combination or an exception.
144 // Output deadkey1 and keep deadkey2
150 currentState = deadkey_state_;
155 TransCombinedState::TransCombinedState()
157 combined_state_ = this;
161 string const TransCombinedState::normalkey(char c, string const & trans)
166 // Check if the key is allowed on the combination
167 if (countChar(comb_info_->data, c) > 0) {
169 string const temp = DoAccent(c, deadkey2_info_.accent);
170 res = DoAccent(temp, deadkey_info_.accent);
171 currentState = init_state_;
174 // Not allowed. Output deadkey1 and check deadkey2 + c
178 deadkey_ = deadkey2_;
179 deadkey_info_ = deadkey2_info_;
180 // Call deadkey state and leave it to setup the FSM
181 res += deadkey_state_->normalkey(c, trans);
188 string const TransCombinedState::deadkey(char c, KmodInfo d)
190 // Third key in a row. Output the first one and
191 // reenter with shifted deadkeys
196 deadkey_ = deadkey2_;
197 deadkey_info_ = deadkey2_info_;
198 res += deadkey_state_->deadkey(c, d);
204 TransFSM::TransFSM():
210 currentState = init_state_;
216 // Initialize static member.
217 Trans TransManager::default_;
220 TransManager::TransManager()
221 : active_(0), t1_(new Trans), t2_(new Trans)
225 TransManager::~TransManager()
232 int TransManager::SetPrimary(string const & language)
234 if (t1_->GetName() == language)
237 return t1_->Load(language);
241 int TransManager::SetSecondary(string const & language)
243 if (t2_->GetName() == language)
246 return t2_->Load(language);
250 bool TransManager::setCharset(string const & str)
252 return chset_.loadFile(str);
256 void TransManager::EnablePrimary()
258 if (t1_->IsDefined())
261 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
265 void TransManager::EnableSecondary()
267 if (t2_->IsDefined())
269 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
273 void TransManager::DisableKeymap()
276 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
280 void TransManager::TranslateAndInsert(char c, LyXText * text)
282 string res = active_->process(c, *this);
284 // Process with tokens
287 while(res.length() > 0) {
288 res = split(res, temp, TransState::TOKEN_SEP);
294 void TransManager::insertVerbatim(string const & str, LyXText * text)
296 string::size_type const l = str.length();
298 for (string::size_type i = 0; i < l; ++i) {
301 && text->getFont(current_view->buffer(),text->cursor.par(),
302 text->cursor.pos()).latex() == LyXFont::OFF
304 && text->getFont(current_view->buffer(),text->cursor.par(),
305 text->cursor.pos()).language()->lang() != "hebrew")
306 current_view->insertCorrectQuote();
308 text->insertChar(current_view, str[i]);
313 void TransManager::insert(string const & str, LyXText * text)
315 // Go through the character encoding only if the current
316 // encoding (chset_->name()) matches the current font_norm
319 // Is false to speak about "only if" the current encoding will
320 // almost always be equal to font_norm.
321 pair<bool, int> enc = chset_.encodeString(str);
322 if (chset_.getName() != lyxrc.font_norm ||
324 // Could not find an encoding
325 InsetLatexAccent ins(str);
326 if (ins.canDisplay()) {
327 text->insertInset(current_view, new InsetLatexAccent(ins));
329 insertVerbatim(str, text);
333 string tmp; tmp += static_cast<char>(enc.second);
334 insertVerbatim(tmp, text);
338 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
340 if (c == 0 && active_ != &default_) {
341 // A deadkey was pressed that cannot be printed
342 // or a accent command was typed in the minibuffer
344 if (active_->isAccentDefined(accent, i) == true) {
345 string const res = trans_fsm_
346 .currentState->deadkey(c, i);
352 if (active_ == &default_ || c == 0) {
356 i.allowed = lyx_accent_table[accent].native;
359 i.exception_list = 0;
361 string res = trans_fsm_.currentState->deadkey(c, i);
364 // Go through the translation
365 TranslateAndInsert(c, t);