4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
22 extern string DoAccent(string const &, tex_accent);
23 extern string 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;
37 char const TransState::TOKEN_SEP = 4;
41 TransInitState::TransInitState()
47 string TransInitState::normalkey(char c, char * t)
57 string TransInitState::deadkey(char c, KmodInfo d)
61 currentState = deadkey_state_;
67 TransDeadkeyState::TransDeadkeyState()
69 deadkey_state_ = this;
73 string TransDeadkeyState::normalkey(char c, char * trans)
77 // Check if it is an exception
78 KmodException l = deadkey_info_.exception_list;
87 // Not an exception. Check if it allowed
88 if (current_view->buffer()->params.allowAccents == true ||
89 countChar(deadkey_info_.allowed, c) > 0) {
90 res = DoAccent(c, deadkey_info_.accent);
99 currentState = init_state_;
104 string TransDeadkeyState::deadkey(char c, KmodInfo d)
108 // Check if the same deadkey was typed twice
112 deadkey_info_.accent = TEX_NOACCENT;
113 currentState = init_state_;
117 // Check if it is a combination or an exception
119 l = deadkey_info_.exception_list;
122 if (l->combined == true && l->accent == d.accent) {
126 currentState = combined_state_;
132 deadkey_info_.accent = TEX_NOACCENT;
133 currentState = init_state_;
139 // Not a combination or an exception.
140 // Output deadkey1 and keep deadkey2
146 currentState = deadkey_state_;
151 TransCombinedState::TransCombinedState()
153 combined_state_ = this;
157 string TransCombinedState::normalkey(char c, char * trans)
161 // Check if the key is allowed on the combination
162 if (countChar(comb_info_->data, c) > 0) {
164 temp = DoAccent(c, deadkey2_info_.accent);
165 res = DoAccent(temp, deadkey_info_.accent);
166 currentState = init_state_;
168 // Not allowed. Output deadkey1 and check deadkey2 + c
172 deadkey_ = deadkey2_;
173 deadkey_info_ = deadkey2_info_;
174 // Call deadkey state and leave it to setup the FSM
175 res += deadkey_state_->normalkey(c, trans);
181 string TransCombinedState::deadkey(char c, KmodInfo d)
183 // Third key in a row. Output the first one and
184 // reenter with shifted deadkeys
189 deadkey_ = deadkey2_;
190 deadkey_info_ = deadkey2_info_;
191 res += deadkey_state_->deadkey(c, d);
197 TransFSM::TransFSM():
203 currentState = init_state_;
209 TransManager::TransManager()
210 : active_(0), t1_(new Trans), t2_(new Trans)
214 Trans * TransManager::default_ = new Trans;
217 TransManager::~TransManager()
224 int TransManager::SetPrimary(string const & language)
226 if (t1_->GetName() == language)
229 return t1_->Load(language);
233 int TransManager::SetSecondary(string const & language)
235 if (t2_->GetName() == language)
238 return t2_->Load(language);
242 bool TransManager::setCharset(char const * set)
244 return chset_.loadFile(set);
248 void TransManager::EnablePrimary()
250 if (t1_->IsDefined())
253 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
257 void TransManager::EnableSecondary()
259 if (t2_->IsDefined())
261 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
265 void TransManager::DisableKeymap()
268 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
272 void TransManager::TranslateAndInsert(char c, LyXText * text)
274 string res = active_->process(c, *this);
276 // Process with tokens
279 while(res.length() > 0) {
280 res = split(res, temp, TransState::TOKEN_SEP);
286 void TransManager::insertVerbatim(string const & str, LyXText * text)
288 int l = str.length();
290 for (int i = 0; i < l; ++i){
292 && text->GetFont(text->cursor.par,
293 text->cursor.pos).latex() == LyXFont::OFF
294 && text->GetFont(text->cursor.par,
295 text->cursor.pos).language()->lang != "hebrew")
296 current_view->insertCorrectQuote();
298 text->InsertChar(str[i]);
303 void TransManager::insert(string const & str, LyXText * text)
305 // Go through the character encoding only if the current
306 // encoding (chset_->name()) matches the current font_norm
309 // Is false to speak about "only if" the current encoding will
310 // almost always be equal to font_norm.
311 pair<bool, int> enc = chset_.encodeString(str);
312 if (chset_.getName() != lyxrc.font_norm ||
314 // Could not find an encoding
315 InsetLatexAccent ins(str);
316 if (ins.CanDisplay()) {
317 text->InsertInset(new InsetLatexAccent(ins));
319 insertVerbatim(str, text);
323 string tmp; tmp += static_cast<char>(enc.second);
324 insertVerbatim(tmp, text);
328 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
330 if (c == 0 && active_ != default_) {
331 // A deadkey was pressed that cannot be printed
332 // or a accent command was typed in the minibuffer
334 if (active_->isAccentDefined(accent, i) == true) {
335 string res = trans_fsm_.currentState->deadkey(c, i);
341 if (active_ == default_ || c == 0) {
344 i.allowed = lyx_accent_table[accent].native;
346 i.exception_list = 0;
348 string res = trans_fsm_.currentState->deadkey(c, i);
351 // Go through the translation
352 TranslateAndInsert(c, t);