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 void InsertCorrectQuote();
22 extern string DoAccent(char, tex_accent);
23 extern BufferView * current_view;
27 TransFSMData::TransFSMData()
29 deadkey_ = deadkey2_ = 0;
30 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
36 char const TransState::TOKEN_SEP = 4;
40 TransInitState::TransInitState()
46 string TransInitState::normalkey(char c, char * t)
58 string TransInitState::deadkey(char c, KmodInfo d)
62 currentState = deadkey_state_;
68 TransDeadkeyState::TransDeadkeyState()
70 deadkey_state_ = this;
74 string TransDeadkeyState::normalkey(char c, char * trans)
78 // Check if it is an exception
79 KmodException l = deadkey_info_.exception_list;
88 // Not an exception. Check if it allowed
89 if (current_view->buffer()->params.allowAccents == true ||
90 countChar(deadkey_info_.allowed, c) > 0) {
91 res = DoAccent(c, deadkey_info_.accent);
100 currentState = init_state_;
105 string TransDeadkeyState::deadkey(char c, KmodInfo d)
109 // Check if the same deadkey was typed twice
113 deadkey_info_.accent = TEX_NOACCENT;
114 currentState = init_state_;
118 // Check if it is a combination or an exception
120 l = deadkey_info_.exception_list;
123 if (l->combined == true && l->accent == d.accent) {
127 currentState = combined_state_;
133 deadkey_info_.accent = TEX_NOACCENT;
134 currentState = init_state_;
141 // Not a combination or an exception.
142 // Output deadkey1 and keep deadkey2
148 currentState = deadkey_state_;
153 TransCombinedState::TransCombinedState()
155 combined_state_ = this;
159 string TransCombinedState::normalkey(char c, char *trans)
163 // Check if the key is allowed on the combination
164 if (countChar(comb_info_->data, c) > 0) {
166 temp = DoAccent(c, deadkey2_info_.accent);
167 res = DoAccent(temp, deadkey_info_.accent);
168 currentState = init_state_;
170 // Not allowed. Output deadkey1 and check deadkey2 + c
174 deadkey_ = deadkey2_;
175 deadkey_info_ = deadkey2_info_;
176 // Call deadkey state and leave it to setup the FSM
177 res+= deadkey_state_->normalkey(c, trans);
183 string TransCombinedState::deadkey(char c, KmodInfo d)
185 // Third key in a row. Output the first one and
186 // reenter with shifted deadkeys
191 deadkey_ = deadkey2_;
192 deadkey_info_ = deadkey2_info_;
193 res += deadkey_state_->deadkey(c, d);
199 TransFSM::TransFSM():
205 currentState = init_state_;
211 TransManager::TransManager()
212 : active_(0), t1_(new Trans), t2_(new Trans)
216 Trans * TransManager::default_ = new Trans;
219 TransManager::~TransManager()
226 int TransManager::SetPrimary(string const & language)
228 if (t1_->GetName() == language)
231 return t1_->Load(language);
235 int TransManager::SetSecondary(string const & language)
237 if (t2_->GetName() == language)
240 return t2_->Load(language);
244 bool TransManager::setCharset(char const * set)
246 return chset_.loadFile(set);
250 void TransManager::EnablePrimary()
252 if (t1_->IsDefined())
255 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
259 void TransManager::EnableSecondary()
261 if (t2_->IsDefined( ))
263 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
267 void TransManager::DisableKeymap()
270 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
274 void TransManager::TranslateAndInsert(char c, LyXText * text)
276 string res = active_->process(c, *this);
278 // Process with tokens
281 while(res.length() > 0) {
282 res = split(res, temp, TransState::TOKEN_SEP);
288 void TransManager::insertVerbatim(string const & str, LyXText * text)
290 int l = str.length();
292 for (int i = 0; i < l; ++i){
294 && text->GetFont(text->cursor.par,
295 text->cursor.pos).latex() == LyXFont::OFF)
296 InsertCorrectQuote();
298 text->InsertChar(str[i]);
303 void TransManager::insert(string str, LyXText * text)
307 // Go through the character encoding only if the current
308 // encoding (chset_->name()) matches the current font_norm
311 if (chset_.getName() != lyxrc->font_norm ||
312 chset_.encodeString(str) == false) {
313 // Could not find an encoding
314 InsetLatexAccent ins(str);
315 if (ins.CanDisplay()) {
316 text->InsertInset(new InsetLatexAccent(ins));
321 insertVerbatim(str, text);
325 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 res = trans_fsm_.currentState->deadkey(c, i);
341 if (active_ == default_ || c == 0) {
343 i.allowed = lyx_accent_table[accent].native;
345 i.exception_list = 0;
347 string res = trans_fsm_.currentState->deadkey(c, i);
350 // Go through the translation
351 TranslateAndInsert(c, t);