4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
19 extern string DoAccent(string const &, tex_accent);
20 extern string DoAccent(char, tex_accent);
21 extern BufferView * current_view;
25 TransFSMData::TransFSMData()
27 deadkey_ = deadkey2_ = 0;
28 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
34 char const TransState::TOKEN_SEP = 4;
38 TransInitState::TransInitState()
44 string TransInitState::normalkey(char c, char * t)
54 string TransInitState::deadkey(char c, KmodInfo d)
58 currentState = deadkey_state_;
64 TransDeadkeyState::TransDeadkeyState()
66 deadkey_state_ = this;
70 string TransDeadkeyState::normalkey(char c, char * trans)
74 // Check if it is an exception
75 KmodException l = deadkey_info_.exception_list;
84 // Not an exception. Check if it allowed
85 if (current_view->buffer()->params.allowAccents == true ||
86 countChar(deadkey_info_.allowed, c) > 0) {
87 res = DoAccent(c, deadkey_info_.accent);
96 currentState = init_state_;
101 string TransDeadkeyState::deadkey(char c, KmodInfo d)
105 // Check if the same deadkey was typed twice
109 deadkey_info_.accent = TEX_NOACCENT;
110 currentState = init_state_;
114 // Check if it is a combination or an exception
116 l = deadkey_info_.exception_list;
119 if (l->combined == true && l->accent == d.accent) {
123 currentState = combined_state_;
129 deadkey_info_.accent = TEX_NOACCENT;
130 currentState = init_state_;
136 // Not a combination or an exception.
137 // Output deadkey1 and keep deadkey2
143 currentState = deadkey_state_;
148 TransCombinedState::TransCombinedState()
150 combined_state_ = this;
154 string TransCombinedState::normalkey(char c, char * trans)
158 // Check if the key is allowed on the combination
159 if (countChar(comb_info_->data, c) > 0) {
161 temp = DoAccent(c, deadkey2_info_.accent);
162 res = DoAccent(temp, deadkey_info_.accent);
163 currentState = init_state_;
165 // Not allowed. Output deadkey1 and check deadkey2 + c
169 deadkey_ = deadkey2_;
170 deadkey_info_ = deadkey2_info_;
171 // Call deadkey state and leave it to setup the FSM
172 res += deadkey_state_->normalkey(c, trans);
178 string TransCombinedState::deadkey(char c, KmodInfo d)
180 // Third key in a row. Output the first one and
181 // reenter with shifted deadkeys
186 deadkey_ = deadkey2_;
187 deadkey_info_ = deadkey2_info_;
188 res += deadkey_state_->deadkey(c, d);
194 TransFSM::TransFSM():
200 currentState = init_state_;
206 TransManager::TransManager()
207 : active_(0), t1_(new Trans), t2_(new Trans)
211 Trans * TransManager::default_ = new Trans;
214 TransManager::~TransManager()
221 int TransManager::SetPrimary(string const & language)
223 if (t1_->GetName() == language)
226 return t1_->Load(language);
230 int TransManager::SetSecondary(string const & language)
232 if (t2_->GetName() == language)
235 return t2_->Load(language);
239 bool TransManager::setCharset(char const * set)
241 return chset_.loadFile(set);
245 void TransManager::EnablePrimary()
247 if (t1_->IsDefined())
250 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
254 void TransManager::EnableSecondary()
256 if (t2_->IsDefined())
258 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
262 void TransManager::DisableKeymap()
265 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
269 void TransManager::TranslateAndInsert(char c, LyXText * text)
271 string res = active_->process(c, *this);
273 // Process with tokens
276 while(res.length() > 0) {
277 res = split(res, temp, TransState::TOKEN_SEP);
283 void TransManager::insertVerbatim(string const & str, LyXText * text)
285 int l = str.length();
287 for (int i = 0; i < l; ++i){
289 && text->GetFont(text->cursor.par,
290 text->cursor.pos).latex() == LyXFont::OFF)
291 current_view->insertCorrectQuote();
293 text->InsertChar(str[i]);
298 void TransManager::insert(string str, LyXText * text)
300 // Go through the character encoding only if the current
301 // encoding (chset_->name()) matches the current font_norm
304 // Is false to speak about "only if" the current encoding will
305 // almost always be equal to font_norm.
306 pair<bool, int> enc = chset_.encodeString(str);
307 if (chset_.getName() != lyxrc.font_norm ||
309 // Could not find an encoding
310 InsetLatexAccent ins(str);
311 if (ins.CanDisplay()) {
312 text->InsertInset(new InsetLatexAccent(ins));
314 insertVerbatim(str, text);
318 string tmp; tmp += static_cast<char>(enc.second);
319 insertVerbatim(tmp, text);
323 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
325 if (c == 0 && active_ != default_) {
326 // A deadkey was pressed that cannot be printed
327 // or a accent command was typed in the minibuffer
329 if (active_->isAccentDefined(accent, i) == true) {
330 string res = trans_fsm_.currentState->deadkey(c, i);
336 if (active_ == default_ || c == 0) {
339 i.allowed = lyx_accent_table[accent].native;
341 i.exception_list = 0;
343 string res = trans_fsm_.currentState->deadkey(c, i);
346 // Go through the translation
347 TranslateAndInsert(c, t);