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;
40 char const TransState::TOKEN_SEP = 4;
44 TransInitState::TransInitState()
51 string const TransInitState::normalkey(char c, string const & t)
54 if (!t.empty()) res = t;
60 string const TransInitState::normalkey(char c)
69 string const TransInitState::deadkey(char c, KmodInfo d)
73 currentState = deadkey_state_;
79 TransDeadkeyState::TransDeadkeyState()
81 deadkey_state_ = this;
86 string const TransDeadkeyState::normalkey(char c, string const & trans)
90 // Check if it is an exception
91 KmodException l = deadkey_info_.exception_list;
100 // Not an exception. Check if it allowed
101 if (countChar(deadkey_info_.allowed, c) > 0) {
102 res = DoAccent(c, deadkey_info_.accent);
111 currentState = init_state_;
115 string const TransDeadkeyState::normalkey(char c)
120 // Check if it is an exception
121 KmodException l = deadkey_info_.exception_list;
130 res = DoAccent(c, deadkey_info_.accent);
132 currentState = init_state_;
137 KmodException::iterator it = deadkey_info_.exception_list.begin();
138 KmodException::iterator end = deadkey_info_.exception_list.end();
140 for (; it != end; ++it) {
147 res = DoAccent(c, deadkey_info_.accent);
149 currentState = init_state_;
156 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
160 // Check if the same deadkey was typed twice
164 deadkey_info_.accent = TEX_NOACCENT;
165 currentState = init_state_;
169 // Check if it is a combination or an exception
172 l = deadkey_info_.exception_list;
175 if (l->combined == true && l->accent == d.accent) {
179 currentState = combined_state_;
185 deadkey_info_.accent = TEX_NOACCENT;
186 currentState = init_state_;
192 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
193 KmodException::const_iterator end = deadkey_info_.exception_list.end();
194 for (; cit != end; ++cit) {
195 if (cit->combined == true && cit->accent == d.accent) {
199 currentState = combined_state_;
205 deadkey_info_.accent = TEX_NOACCENT;
206 currentState = init_state_;
211 // Not a combination or an exception.
212 // Output deadkey1 and keep deadkey2
218 currentState = deadkey_state_;
223 TransCombinedState::TransCombinedState()
225 combined_state_ = this;
230 string const TransCombinedState::normalkey(char c, string const & trans)
234 // Check if the key is allowed on the combination
235 if (countChar(comb_info_->data, c) > 0) {
236 string const temp = DoAccent(c, deadkey2_info_.accent);
237 res = DoAccent(temp, deadkey_info_.accent);
238 currentState = init_state_;
240 // Not allowed. Output deadkey1 and check deadkey2 + c
244 deadkey_ = deadkey2_;
245 deadkey_info_ = deadkey2_info_;
246 // Call deadkey state and leave it to setup the FSM
247 res += deadkey_state_->normalkey(c, trans);
252 string const TransCombinedState::normalkey(char c)
256 string const temp = DoAccent(c, deadkey2_info_.accent);
257 res = DoAccent(temp, deadkey_info_.accent);
258 currentState = init_state_;
264 string const TransCombinedState::deadkey(char c, KmodInfo d)
266 // Third key in a row. Output the first one and
267 // reenter with shifted deadkeys
272 deadkey_ = deadkey2_;
273 deadkey_info_ = deadkey2_info_;
274 res += deadkey_state_->deadkey(c, d);
280 TransFSM::TransFSM():
286 currentState = init_state_;
292 // Initialize static member.
293 Trans TransManager::default_;
296 TransManager::TransManager()
297 : active_(0), t1_(new Trans), t2_(new Trans)
301 TransManager::~TransManager()
308 int TransManager::SetPrimary(string const & language)
310 if (t1_->GetName() == language)
313 return t1_->Load(language);
317 int TransManager::SetSecondary(string const & language)
319 if (t2_->GetName() == language)
322 return t2_->Load(language);
326 bool TransManager::setCharset(string const & str)
328 return chset_.loadFile(str);
332 void TransManager::EnablePrimary()
334 if (t1_->IsDefined())
337 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
341 void TransManager::EnableSecondary()
343 if (t2_->IsDefined())
345 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
349 void TransManager::DisableKeymap()
352 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
356 void TransManager::TranslateAndInsert(char c, LyXText * text)
358 string res = active_->process(c, *this);
360 // Process with tokens
363 while(res.length() > 0) {
364 res = split(res, temp, TransState::TOKEN_SEP);
370 void TransManager::insertVerbatim(string const & str, LyXText * text)
372 string::size_type const l = str.length();
374 for (string::size_type i = 0; i < l; ++i) {
377 && text->getFont(current_view->buffer(),text->cursor.par(),
378 text->cursor.pos()).latex() == LyXFont::OFF
380 && text->getFont(current_view->buffer(),text->cursor.par(),
381 text->cursor.pos()).language()->lang() != "hebrew")
382 current_view->insertCorrectQuote();
384 text->insertChar(current_view, str[i]);
389 void TransManager::insert(string const & str, LyXText * text)
391 // Go through the character encoding only if the current
392 // encoding (chset_->name()) matches the current font_norm
395 // Is false to speak about "only if" the current encoding will
396 // almost always be equal to font_norm.
397 pair<bool, int> enc = chset_.encodeString(str);
398 if (chset_.getName() != lyxrc.font_norm ||
400 // Could not find an encoding
401 InsetLatexAccent ins(str);
402 if (ins.canDisplay()) {
403 text->insertInset(current_view,
404 new InsetLatexAccent(ins));
406 insertVerbatim(str, text);
411 tmp += static_cast<char>(enc.second);
412 insertVerbatim(tmp, text);
416 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
418 if (c == 0 && active_ != &default_) {
419 // A deadkey was pressed that cannot be printed
420 // or a accent command was typed in the minibuffer
422 if (active_->isAccentDefined(accent, i) == true) {
423 string const res = trans_fsm_
424 .currentState->deadkey(c, i);
430 if (active_ == &default_ || c == 0) {
434 i.allowed = lyx_accent_table[accent].native;
438 i.exception_list = 0;
440 string res = trans_fsm_.currentState->deadkey(c, i);
443 // Go through the translation
444 TranslateAndInsert(c, t);