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()
49 string const TransInitState::normalkey(char c, string const & t)
52 if (!t.empty()) res = t;
58 string const TransInitState::normalkey(char c)
67 string const TransInitState::deadkey(char c, KmodInfo d)
71 currentState = deadkey_state_;
77 TransDeadkeyState::TransDeadkeyState()
79 deadkey_state_ = this;
84 string const TransDeadkeyState::normalkey(char c, string const & trans)
88 // Check if it is an exception
89 KmodException l = deadkey_info_.exception_list;
98 // Not an exception. Check if it allowed
99 if (countChar(deadkey_info_.allowed, c) > 0) {
100 res = DoAccent(c, deadkey_info_.accent);
109 currentState = init_state_;
113 string const TransDeadkeyState::normalkey(char c)
117 // Check if it is an exception
118 KmodException l = deadkey_info_.exception_list;
127 res = DoAccent(c, deadkey_info_.accent);
129 currentState = init_state_;
135 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
139 // Check if the same deadkey was typed twice
143 deadkey_info_.accent = TEX_NOACCENT;
144 currentState = init_state_;
148 // Check if it is a combination or an exception
150 l = deadkey_info_.exception_list;
153 if (l->combined == true && l->accent == d.accent) {
157 currentState = combined_state_;
163 deadkey_info_.accent = TEX_NOACCENT;
164 currentState = init_state_;
170 // Not a combination or an exception.
171 // Output deadkey1 and keep deadkey2
177 currentState = deadkey_state_;
182 TransCombinedState::TransCombinedState()
184 combined_state_ = this;
189 string const TransCombinedState::normalkey(char c, string const & trans)
193 // Check if the key is allowed on the combination
194 if (countChar(comb_info_->data, c) > 0) {
195 string const temp = DoAccent(c, deadkey2_info_.accent);
196 res = DoAccent(temp, deadkey_info_.accent);
197 currentState = init_state_;
199 // Not allowed. Output deadkey1 and check deadkey2 + c
203 deadkey_ = deadkey2_;
204 deadkey_info_ = deadkey2_info_;
205 // Call deadkey state and leave it to setup the FSM
206 res += deadkey_state_->normalkey(c, trans);
211 string const TransCombinedState::normalkey(char c)
215 string const temp = DoAccent(c, deadkey2_info_.accent);
216 res = DoAccent(temp, deadkey_info_.accent);
217 currentState = init_state_;
223 string const TransCombinedState::deadkey(char c, KmodInfo d)
225 // Third key in a row. Output the first one and
226 // reenter with shifted deadkeys
231 deadkey_ = deadkey2_;
232 deadkey_info_ = deadkey2_info_;
233 res += deadkey_state_->deadkey(c, d);
239 TransFSM::TransFSM():
245 currentState = init_state_;
251 // Initialize static member.
252 Trans TransManager::default_;
255 TransManager::TransManager()
256 : active_(0), t1_(new Trans), t2_(new Trans)
260 TransManager::~TransManager()
267 int TransManager::SetPrimary(string const & language)
269 if (t1_->GetName() == language)
272 return t1_->Load(language);
276 int TransManager::SetSecondary(string const & language)
278 if (t2_->GetName() == language)
281 return t2_->Load(language);
285 bool TransManager::setCharset(string const & str)
287 return chset_.loadFile(str);
291 void TransManager::EnablePrimary()
293 if (t1_->IsDefined())
296 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
300 void TransManager::EnableSecondary()
302 if (t2_->IsDefined())
304 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
308 void TransManager::DisableKeymap()
311 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
315 void TransManager::TranslateAndInsert(char c, LyXText * text)
317 string res = active_->process(c, *this);
319 // Process with tokens
322 while(res.length() > 0) {
323 res = split(res, temp, TransState::TOKEN_SEP);
329 void TransManager::insertVerbatim(string const & str, LyXText * text)
331 string::size_type const l = str.length();
333 for (string::size_type i = 0; i < l; ++i) {
336 && text->getFont(current_view->buffer(),text->cursor.par(),
337 text->cursor.pos()).latex() == LyXFont::OFF
339 && text->getFont(current_view->buffer(),text->cursor.par(),
340 text->cursor.pos()).language()->lang() != "hebrew")
341 current_view->insertCorrectQuote();
343 text->insertChar(current_view, str[i]);
348 void TransManager::insert(string const & str, LyXText * text)
350 // Go through the character encoding only if the current
351 // encoding (chset_->name()) matches the current font_norm
354 // Is false to speak about "only if" the current encoding will
355 // almost always be equal to font_norm.
356 pair<bool, int> enc = chset_.encodeString(str);
357 if (chset_.getName() != lyxrc.font_norm ||
359 // Could not find an encoding
360 InsetLatexAccent ins(str);
361 if (ins.canDisplay()) {
362 text->insertInset(current_view, new InsetLatexAccent(ins));
364 insertVerbatim(str, text);
368 string tmp; tmp += static_cast<char>(enc.second);
369 insertVerbatim(tmp, text);
373 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
375 if (c == 0 && active_ != &default_) {
376 // A deadkey was pressed that cannot be printed
377 // or a accent command was typed in the minibuffer
379 if (active_->isAccentDefined(accent, i) == true) {
380 string const res = trans_fsm_
381 .currentState->deadkey(c, i);
387 if (active_ == &default_ || c == 0) {
391 i.allowed = lyx_accent_table[accent].native;
394 i.exception_list = 0;
396 string res = trans_fsm_.currentState->deadkey(c, i);
399 // Go through the translation
400 TranslateAndInsert(c, t);