4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
22 extern string const DoAccent(string const &, tex_accent);
23 extern string const DoAccent(char, tex_accent);
24 extern BufferView * current_view;
28 TransFSMData::TransFSMData()
30 deadkey_ = deadkey2_ = 0;
31 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
37 char const TransState::TOKEN_SEP = 4;
41 TransInitState::TransInitState()
47 string const TransInitState::normalkey(char c, string const & t)
50 if (!t.empty()) res = t;
57 string const TransInitState::deadkey(char c, KmodInfo d)
61 currentState = deadkey_state_;
67 TransDeadkeyState::TransDeadkeyState()
69 deadkey_state_ = this;
73 string const TransDeadkeyState::normalkey(char c, string const & trans)
77 // Check if it is an exception
78 KmodException l = deadkey_info_.exception_list;
87 // Not an exception. Check if it allowed
88 if (countChar(deadkey_info_.allowed, c) > 0) {
89 res = DoAccent(c, deadkey_info_.accent);
98 currentState = init_state_;
103 string const 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 const TransCombinedState::normalkey(char c, string const & 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 const 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(string const & str)
243 return chset_.loadFile(str);
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 string::size_type const l = str.length();
289 for (string::size_type i = 0; i < l; ++i) {
291 && text->GetFont(current_view->buffer(),text->cursor.par(),
292 text->cursor.pos()).latex() == LyXFont::OFF
293 && text->GetFont(current_view->buffer(),text->cursor.par(),
294 text->cursor.pos()).language()->lang() != "hebrew")
295 current_view->insertCorrectQuote();
297 text->InsertChar(current_view, str[i]);
302 void TransManager::insert(string const & str, LyXText * text)
304 // Go through the character encoding only if the current
305 // encoding (chset_->name()) matches the current font_norm
308 // Is false to speak about "only if" the current encoding will
309 // almost always be equal to font_norm.
310 pair<bool, int> enc = chset_.encodeString(str);
311 if (chset_.getName() != lyxrc.font_norm ||
313 // Could not find an encoding
314 InsetLatexAccent ins(str);
315 if (ins.CanDisplay()) {
316 text->InsertInset(current_view, new InsetLatexAccent(ins));
318 insertVerbatim(str, text);
322 string tmp; tmp += static_cast<char>(enc.second);
323 insertVerbatim(tmp, text);
327 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
329 if (c == 0 && active_ != default_) {
330 // A deadkey was pressed that cannot be printed
331 // or a accent command was typed in the minibuffer
333 if (active_->isAccentDefined(accent, i) == true) {
334 string res = trans_fsm_.currentState->deadkey(c, i);
340 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);