4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
22 extern string const DoAccent(string const &, tex_accent);
23 extern string const DoAccent(char, tex_accent);
24 extern BufferView * current_view;
28 TransFSMData::TransFSMData()
30 deadkey_ = deadkey2_ = 0;
31 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
39 char const TransState::TOKEN_SEP = 4;
43 TransInitState::TransInitState()
50 string const TransInitState::normalkey(char c, string const & t)
53 if (!t.empty()) res = t;
59 string const TransInitState::normalkey(char c)
68 string const TransInitState::deadkey(char c, KmodInfo d)
72 currentState = deadkey_state_;
78 TransDeadkeyState::TransDeadkeyState()
80 deadkey_state_ = this;
85 string const TransDeadkeyState::normalkey(char c, string const & trans)
89 // Check if it is an exception
90 KmodException l = deadkey_info_.exception_list;
99 // Not an exception. Check if it allowed
100 if (countChar(deadkey_info_.allowed, c) > 0) {
101 res = DoAccent(c, deadkey_info_.accent);
110 currentState = init_state_;
114 string const TransDeadkeyState::normalkey(char c)
119 // Check if it is an exception
120 KmodException l = deadkey_info_.exception_list;
129 res = DoAccent(c, deadkey_info_.accent);
131 currentState = init_state_;
136 KmodException::iterator it = deadkey_info_.exception_list.begin();
137 KmodException::iterator end = deadkey_info_.exception_list.end();
139 for (; it != end; ++it) {
146 res = DoAccent(c, deadkey_info_.accent);
148 currentState = init_state_;
155 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
159 // Check if the same deadkey was typed twice
163 deadkey_info_.accent = TEX_NOACCENT;
164 currentState = init_state_;
168 // Check if it is a combination or an exception
171 l = deadkey_info_.exception_list;
174 if (l->combined == true && l->accent == d.accent) {
178 currentState = combined_state_;
184 deadkey_info_.accent = TEX_NOACCENT;
185 currentState = init_state_;
191 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
192 KmodException::const_iterator end = deadkey_info_.exception_list.end();
193 for (; cit != end; ++cit) {
194 if (cit->combined == true && cit->accent == d.accent) {
198 currentState = combined_state_;
204 deadkey_info_.accent = TEX_NOACCENT;
205 currentState = init_state_;
210 // Not a combination or an exception.
211 // Output deadkey1 and keep deadkey2
217 currentState = deadkey_state_;
222 TransCombinedState::TransCombinedState()
224 combined_state_ = this;
229 string const TransCombinedState::normalkey(char c, string const & trans)
233 // Check if the key is allowed on the combination
234 if (countChar(comb_info_->data, c) > 0) {
235 string const temp = DoAccent(c, deadkey2_info_.accent);
236 res = DoAccent(temp, deadkey_info_.accent);
237 currentState = init_state_;
239 // Not allowed. Output deadkey1 and check deadkey2 + c
243 deadkey_ = deadkey2_;
244 deadkey_info_ = deadkey2_info_;
245 // Call deadkey state and leave it to setup the FSM
246 res += deadkey_state_->normalkey(c, trans);
251 string const TransCombinedState::normalkey(char c)
255 string const temp = DoAccent(c, deadkey2_info_.accent);
256 res = DoAccent(temp, deadkey_info_.accent);
257 currentState = init_state_;
263 string const TransCombinedState::deadkey(char c, KmodInfo d)
265 // Third key in a row. Output the first one and
266 // reenter with shifted deadkeys
271 deadkey_ = deadkey2_;
272 deadkey_info_ = deadkey2_info_;
273 res += deadkey_state_->deadkey(c, d);
279 TransFSM::TransFSM():
285 currentState = init_state_;
291 // Initialize static member.
292 Trans TransManager::default_;
295 TransManager::TransManager()
296 : active_(0), t1_(new Trans), t2_(new Trans)
300 TransManager::~TransManager()
307 int TransManager::SetPrimary(string const & language)
309 if (t1_->GetName() == language)
312 return t1_->Load(language);
316 int TransManager::SetSecondary(string const & language)
318 if (t2_->GetName() == language)
321 return t2_->Load(language);
325 bool TransManager::setCharset(string const & str)
327 return chset_.loadFile(str);
331 void TransManager::EnablePrimary()
333 if (t1_->IsDefined())
336 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
340 void TransManager::EnableSecondary()
342 if (t2_->IsDefined())
344 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
348 void TransManager::DisableKeymap()
351 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
355 void TransManager::TranslateAndInsert(char c, LyXText * text)
357 string res = active_->process(c, *this);
359 // Process with tokens
362 while(res.length() > 0) {
363 res = split(res, temp, TransState::TOKEN_SEP);
369 void TransManager::insertVerbatim(string const & str, LyXText * text)
371 string::size_type const l = str.length();
373 for (string::size_type i = 0; i < l; ++i) {
374 text->insertChar(current_view, str[i]);
379 void TransManager::insert(string const & str, LyXText * text)
381 // Go through the character encoding only if the current
382 // encoding (chset_->name()) matches the current font_norm
383 // (lyrxc->font_norm)
385 // Is false to speak about "only if" the current encoding will
386 // almost always be equal to font_norm.
387 pair<bool, int> enc = chset_.encodeString(str);
388 if (chset_.getName() != lyxrc.font_norm ||
390 // Could not find an encoding
391 InsetLatexAccent ins(str);
392 if (ins.canDisplay()) {
393 text->insertInset(current_view,
394 new InsetLatexAccent(ins));
396 insertVerbatim(str, text);
401 tmp += static_cast<char>(enc.second);
402 insertVerbatim(tmp, text);
406 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
408 if (c == 0 && active_ != &default_) {
409 // A deadkey was pressed that cannot be printed
410 // or a accent command was typed in the minibuffer
412 if (active_->isAccentDefined(accent, i) == true) {
413 string const res = trans_fsm_
414 .currentState->deadkey(c, i);
420 if (active_ == &default_ || c == 0) {
424 i.allowed = lyx_accent_table[accent].native;
428 i.exception_list = 0;
430 string res = trans_fsm_.currentState->deadkey(c, i);
433 // Go through the translation
434 TranslateAndInsert(c, t);