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;
39 TransState::~TransState() {}
43 TransInitState::TransInitState()
49 string TransInitState::normalkey(char c, char * t)
61 string TransInitState::deadkey(char c, KmodInfo d)
65 currentState = deadkey_state_;
71 TransDeadkeyState::TransDeadkeyState()
73 deadkey_state_ = this;
77 string TransDeadkeyState::normalkey(char c, char * trans)
81 // Check if it is an exception
82 KmodException l = deadkey_info_.exception_list;
91 // Not an exception. Check if it allowed
92 if (current_view->buffer()->params.allowAccents == true ||
93 countChar(deadkey_info_.allowed, c) > 0) {
94 res = DoAccent(c, deadkey_info_.accent);
103 currentState = init_state_;
108 string TransDeadkeyState::deadkey(char c, KmodInfo d)
112 // Check if the same deadkey was typed twice
116 deadkey_info_.accent = TEX_NOACCENT;
117 currentState = init_state_;
121 // Check if it is a combination or an exception
123 l = deadkey_info_.exception_list;
126 if (l->combined == true && l->accent == d.accent) {
130 currentState = combined_state_;
136 deadkey_info_.accent = TEX_NOACCENT;
137 currentState = init_state_;
144 // Not a combination or an exception.
145 // Output deadkey1 and keep deadkey2
151 currentState = deadkey_state_;
156 TransCombinedState::TransCombinedState()
158 combined_state_ = this;
162 string TransCombinedState::normalkey(char c, char *trans)
166 // Check if the key is allowed on the combination
167 if (countChar(comb_info_->data, c) > 0) {
169 temp = DoAccent(c, deadkey2_info_.accent);
170 res = DoAccent(temp, deadkey_info_.accent);
171 currentState = init_state_;
173 // Not allowed. Output deadkey1 and check deadkey2 + c
177 deadkey_ = deadkey2_;
178 deadkey_info_ = deadkey2_info_;
179 // Call deadkey state and leave it to setup the FSM
180 res+= deadkey_state_->normalkey(c, trans);
186 string TransCombinedState::deadkey(char c, KmodInfo d)
188 // Third key in a row. Output the first one and
189 // reenter with shifted deadkeys
194 deadkey_ = deadkey2_;
195 deadkey_info_ = deadkey2_info_;
196 res+= deadkey_state_->deadkey(c, d);
202 TransFSM::TransFSM():
208 currentState = init_state_;
214 TransManager::TransManager()
215 : active_(0), t1_(new Trans), t2_(new Trans)
219 Trans * TransManager::default_ = new Trans;
222 TransManager::~TransManager()
229 int TransManager::SetPrimary(string const & language)
231 if (t1_->GetName() == language)
234 return t1_->Load(language);
238 int TransManager::SetSecondary(string const & language)
240 if (t2_->GetName() == language)
243 return t2_->Load(language);
247 bool TransManager::setCharset(char const * set)
249 return chset_.loadFile(set);
253 void TransManager::EnablePrimary()
255 if (t1_->IsDefined())
258 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
262 void TransManager::EnableSecondary()
264 if (t2_->IsDefined( ))
266 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
270 void TransManager::DisableKeymap()
273 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
277 void TransManager::TranslateAndInsert(char c, LyXText * text)
279 string res = active_->process(c, *this);
281 // Process with tokens
284 while(res.length()>0) {
285 res = split(res, temp, TransState::TOKEN_SEP);
291 void TransManager::insertVerbatim(string const & str, LyXText * text)
293 int l = str.length();
295 for (int i = 0;i<l;i++){
297 && text->GetFont(text->cursor.par,
298 text->cursor.pos).latex() == LyXFont::OFF)
299 InsertCorrectQuote();
301 text->InsertChar(str[i]);
306 void TransManager::insert(string str, LyXText * text)
310 // Go through the character encoding only if the current
311 // encoding (chset_->name()) matches the current font_norm
314 if (chset_.getName()!= lyxrc->font_norm ||
315 chset_.encodeString(str) == false) {
316 // Could not find an encoding
317 InsetLatexAccent ins(str);
318 if (ins.CanDisplay()) {
319 text->InsertInset(new InsetLatexAccent(ins));
324 insertVerbatim(str, text);
328 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
333 if (c == 0 && active_!= default_) {
334 // A deadkey was pressed that cannot be printed
335 // or a accent command was typed in the minibuffer
337 if (active_->isAccentDefined(accent, i) == true) {
338 res = trans_fsm_.currentState->deadkey(c, i);
344 if (active_ == default_ || c == 0) {
346 i.allowed = lyx_accent_table[accent].native;
348 i.exception_list = 0;
350 string res = trans_fsm_.currentState->deadkey(c, i);
353 // Go through the translation
354 TranslateAndInsert(c, t);