4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
20 extern string DoAccent(string const &, tex_accent);
21 extern string DoAccent(char, tex_accent);
22 extern BufferView * current_view;
26 TransFSMData::TransFSMData()
28 deadkey_ = deadkey2_ = 0;
29 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
35 char const TransState::TOKEN_SEP = 4;
39 TransInitState::TransInitState()
45 string TransInitState::normalkey(char c, char * t)
55 string TransInitState::deadkey(char c, KmodInfo d)
59 currentState = deadkey_state_;
65 TransDeadkeyState::TransDeadkeyState()
67 deadkey_state_ = this;
71 string TransDeadkeyState::normalkey(char c, char * trans)
75 // Check if it is an exception
76 KmodException l = deadkey_info_.exception_list;
85 // Not an exception. Check if it allowed
86 if (current_view->buffer()->params.allowAccents == true ||
87 countChar(deadkey_info_.allowed, c) > 0) {
88 res = DoAccent(c, deadkey_info_.accent);
97 currentState = init_state_;
102 string TransDeadkeyState::deadkey(char c, KmodInfo d)
106 // Check if the same deadkey was typed twice
110 deadkey_info_.accent = TEX_NOACCENT;
111 currentState = init_state_;
115 // Check if it is a combination or an exception
117 l = deadkey_info_.exception_list;
120 if (l->combined == true && l->accent == d.accent) {
124 currentState = combined_state_;
130 deadkey_info_.accent = TEX_NOACCENT;
131 currentState = init_state_;
137 // Not a combination or an exception.
138 // Output deadkey1 and keep deadkey2
144 currentState = deadkey_state_;
149 TransCombinedState::TransCombinedState()
151 combined_state_ = this;
155 string TransCombinedState::normalkey(char c, char * trans)
159 // Check if the key is allowed on the combination
160 if (countChar(comb_info_->data, c) > 0) {
162 temp = DoAccent(c, deadkey2_info_.accent);
163 res = DoAccent(temp, deadkey_info_.accent);
164 currentState = init_state_;
166 // Not allowed. Output deadkey1 and check deadkey2 + c
170 deadkey_ = deadkey2_;
171 deadkey_info_ = deadkey2_info_;
172 // Call deadkey state and leave it to setup the FSM
173 res += deadkey_state_->normalkey(c, trans);
179 string TransCombinedState::deadkey(char c, KmodInfo d)
181 // Third key in a row. Output the first one and
182 // reenter with shifted deadkeys
187 deadkey_ = deadkey2_;
188 deadkey_info_ = deadkey2_info_;
189 res += deadkey_state_->deadkey(c, d);
195 TransFSM::TransFSM():
201 currentState = init_state_;
207 TransManager::TransManager()
208 : active_(0), t1_(new Trans), t2_(new Trans)
212 Trans * TransManager::default_ = new Trans;
215 TransManager::~TransManager()
222 int TransManager::SetPrimary(string const & language)
224 if (t1_->GetName() == language)
227 return t1_->Load(language);
231 int TransManager::SetSecondary(string const & language)
233 if (t2_->GetName() == language)
236 return t2_->Load(language);
240 bool TransManager::setCharset(char const * set)
242 return chset_.loadFile(set);
246 void TransManager::EnablePrimary()
248 if (t1_->IsDefined())
251 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
255 void TransManager::EnableSecondary()
257 if (t2_->IsDefined())
259 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
263 void TransManager::DisableKeymap()
266 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
270 void TransManager::TranslateAndInsert(char c, LyXText * text)
272 string res = active_->process(c, *this);
274 // Process with tokens
277 while(res.length() > 0) {
278 res = split(res, temp, TransState::TOKEN_SEP);
284 void TransManager::insertVerbatim(string const & str, LyXText * text)
286 int l = str.length();
288 for (int i = 0; i < l; ++i){
290 && text->GetFont(text->cursor.par,
291 text->cursor.pos).latex() == LyXFont::OFF)
292 current_view->insertCorrectQuote();
294 text->InsertChar(str[i]);
299 void TransManager::insert(string str, LyXText * text)
301 // Go through the character encoding only if the current
302 // encoding (chset_->name()) matches the current font_norm
305 // Is false to speak about "only if" the current encoding will
306 // almost always be equal to font_norm.
307 pair<bool, int> enc = chset_.encodeString(str);
308 if (chset_.getName() != lyxrc->font_norm ||
310 // Could not find an encoding
311 InsetLatexAccent ins(str);
312 if (ins.CanDisplay()) {
313 text->InsertInset(new InsetLatexAccent(ins));
315 insertVerbatim(str, text);
319 string tmp; tmp += static_cast<char>(enc.second);
320 insertVerbatim(tmp, text);
324 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
326 if (c == 0 && active_ != default_) {
327 // A deadkey was pressed that cannot be printed
328 // or a accent command was typed in the minibuffer
330 if (active_->isAccentDefined(accent, i) == true) {
331 string res = trans_fsm_.currentState->deadkey(c, i);
337 if (active_ == default_ || c == 0) {
340 i.allowed = lyx_accent_table[accent].native;
342 i.exception_list = 0;
344 string res = trans_fsm_.currentState->deadkey(c, i);
347 // Go through the translation
348 TranslateAndInsert(c, t);