4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
21 extern string DoAccent(string const &, tex_accent);
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)
56 string TransInitState::deadkey(char c, KmodInfo d)
60 currentState = deadkey_state_;
66 TransDeadkeyState::TransDeadkeyState()
68 deadkey_state_ = this;
72 string TransDeadkeyState::normalkey(char c, char * trans)
76 // Check if it is an exception
77 KmodException l = deadkey_info_.exception_list;
86 // Not an exception. Check if it allowed
87 if (current_view->buffer()->params.allowAccents == true ||
88 countChar(deadkey_info_.allowed, c) > 0) {
89 res = DoAccent(c, deadkey_info_.accent);
98 currentState = init_state_;
103 string TransDeadkeyState::deadkey(char c, KmodInfo d)
107 // Check if the same deadkey was typed twice
111 deadkey_info_.accent = TEX_NOACCENT;
112 currentState = init_state_;
116 // Check if it is a combination or an exception
118 l = deadkey_info_.exception_list;
121 if (l->combined == true && l->accent == d.accent) {
125 currentState = combined_state_;
131 deadkey_info_.accent = TEX_NOACCENT;
132 currentState = init_state_;
138 // Not a combination or an exception.
139 // Output deadkey1 and keep deadkey2
145 currentState = deadkey_state_;
150 TransCombinedState::TransCombinedState()
152 combined_state_ = this;
156 string TransCombinedState::normalkey(char c, char * trans)
160 // Check if the key is allowed on the combination
161 if (countChar(comb_info_->data, c) > 0) {
163 temp = DoAccent(c, deadkey2_info_.accent);
164 res = DoAccent(temp, deadkey_info_.accent);
165 currentState = init_state_;
167 // Not allowed. Output deadkey1 and check deadkey2 + c
171 deadkey_ = deadkey2_;
172 deadkey_info_ = deadkey2_info_;
173 // Call deadkey state and leave it to setup the FSM
174 res += deadkey_state_->normalkey(c, trans);
180 string TransCombinedState::deadkey(char c, KmodInfo d)
182 // Third key in a row. Output the first one and
183 // reenter with shifted deadkeys
188 deadkey_ = deadkey2_;
189 deadkey_info_ = deadkey2_info_;
190 res += deadkey_state_->deadkey(c, d);
196 TransFSM::TransFSM():
202 currentState = init_state_;
208 TransManager::TransManager()
209 : active_(0), t1_(new Trans), t2_(new Trans)
213 Trans * TransManager::default_ = new Trans;
216 TransManager::~TransManager()
223 int TransManager::SetPrimary(string const & language)
225 if (t1_->GetName() == language)
228 return t1_->Load(language);
232 int TransManager::SetSecondary(string const & language)
234 if (t2_->GetName() == language)
237 return t2_->Load(language);
241 bool TransManager::setCharset(char const * set)
243 return chset_.loadFile(set);
247 void TransManager::EnablePrimary()
249 if (t1_->IsDefined())
252 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
256 void TransManager::EnableSecondary()
258 if (t2_->IsDefined())
260 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
264 void TransManager::DisableKeymap()
267 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
271 void TransManager::TranslateAndInsert(char c, LyXText * text)
273 string res = active_->process(c, *this);
275 // Process with tokens
278 while(res.length() > 0) {
279 res = split(res, temp, TransState::TOKEN_SEP);
285 void TransManager::insertVerbatim(string const & str, LyXText * text)
287 int l = str.length();
289 for (int i = 0; i < l; ++i){
291 && text->GetFont(text->cursor.par,
292 text->cursor.pos).latex() == LyXFont::OFF)
293 current_view->insertCorrectQuote();
295 text->InsertChar(str[i]);
300 void TransManager::insert(string str, LyXText * text)
302 // Go through the character encoding only if the current
303 // encoding (chset_->name()) matches the current font_norm
306 // Is false to speak about "only if" the current encoding will
307 // almost always be equal to font_norm.
308 pair<bool, int> enc = chset_.encodeString(str);
309 if (chset_.getName() != lyxrc.font_norm ||
311 // Could not find an encoding
312 InsetLatexAccent ins(str);
313 if (ins.CanDisplay()) {
314 text->InsertInset(new InsetLatexAccent(ins));
316 insertVerbatim(str, text);
320 string tmp; tmp += static_cast<char>(enc.second);
321 insertVerbatim(tmp, text);
325 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
327 if (c == 0 && active_ != default_) {
328 // A deadkey was pressed that cannot be printed
329 // or a accent command was typed in the minibuffer
331 if (active_->isAccentDefined(accent, i) == true) {
332 string res = trans_fsm_.currentState->deadkey(c, i);
338 if (active_ == default_ || c == 0) {
341 i.allowed = lyx_accent_table[accent].native;
343 i.exception_list = 0;
345 string res = trans_fsm_.currentState->deadkey(c, i);
348 // Go through the translation
349 TranslateAndInsert(c, t);