4 #pragma implementation "trans_mgr.h"
13 #include "insets/insetlatexaccent.h"
14 #include "BufferView.h"
17 #include "support/lstrings.h"
23 extern string const DoAccent(string const &, tex_accent);
24 extern string const DoAccent(char, tex_accent);
25 extern BufferView * current_view;
29 TransFSMData::TransFSMData()
31 deadkey_ = deadkey2_ = 0;
32 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
38 char const TransState::TOKEN_SEP = 4;
42 TransInitState::TransInitState()
48 string const TransInitState::normalkey(char c, string const & t)
51 if (!t.empty()) res = t;
58 string const TransInitState::deadkey(char c, KmodInfo d)
62 currentState = deadkey_state_;
68 TransDeadkeyState::TransDeadkeyState()
70 deadkey_state_ = this;
74 string const TransDeadkeyState::normalkey(char c, string const & 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 (countChar(deadkey_info_.allowed, c) > 0) {
90 res = DoAccent(c, deadkey_info_.accent);
99 currentState = init_state_;
104 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
108 // Check if the same deadkey was typed twice
112 deadkey_info_.accent = TEX_NOACCENT;
113 currentState = init_state_;
117 // Check if it is a combination or an exception
119 l = deadkey_info_.exception_list;
122 if (l->combined == true && l->accent == d.accent) {
126 currentState = combined_state_;
132 deadkey_info_.accent = TEX_NOACCENT;
133 currentState = init_state_;
139 // Not a combination or an exception.
140 // Output deadkey1 and keep deadkey2
146 currentState = deadkey_state_;
151 TransCombinedState::TransCombinedState()
153 combined_state_ = this;
157 string const TransCombinedState::normalkey(char c, string const & trans)
161 // Check if the key is allowed on the combination
162 if (countChar(comb_info_->data, c) > 0) {
163 string const 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 // Initialize static member.
209 Trans TransManager::default_;
212 TransManager::TransManager()
213 : active_(0), t1_(new Trans), t2_(new Trans)
217 TransManager::~TransManager()
224 int TransManager::SetPrimary(string const & language)
226 if (t1_->GetName() == language)
229 return t1_->Load(language);
233 int TransManager::SetSecondary(string const & language)
235 if (t2_->GetName() == language)
238 return t2_->Load(language);
242 bool TransManager::setCharset(string const & str)
244 return chset_.loadFile(str);
248 void TransManager::EnablePrimary()
250 if (t1_->IsDefined())
253 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
257 void TransManager::EnableSecondary()
259 if (t2_->IsDefined())
261 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
265 void TransManager::DisableKeymap()
268 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
272 void TransManager::TranslateAndInsert(char c, LyXText * text)
274 string res = active_->process(c, *this);
276 // Process with tokens
279 while(res.length() > 0) {
280 res = split(res, temp, TransState::TOKEN_SEP);
286 void TransManager::insertVerbatim(string const & str, LyXText * text)
288 string::size_type const l = str.length();
290 for (string::size_type i = 0; i < l; ++i) {
292 && text->GetFont(current_view->buffer(),text->cursor.par(),
293 text->cursor.pos()).latex() == LyXFont::OFF
294 && text->GetFont(current_view->buffer(),text->cursor.par(),
295 text->cursor.pos()).language()->lang() != "hebrew")
296 current_view->insertCorrectQuote();
298 text->InsertChar(current_view, str[i]);
303 void TransManager::insert(string const & str, LyXText * text)
305 // Go through the character encoding only if the current
306 // encoding (chset_->name()) matches the current font_norm
309 // Is false to speak about "only if" the current encoding will
310 // almost always be equal to font_norm.
311 pair<bool, int> enc = chset_.encodeString(str);
312 if (chset_.getName() != lyxrc.font_norm ||
314 // Could not find an encoding
315 InsetLatexAccent ins(str);
316 if (ins.CanDisplay()) {
317 text->InsertInset(current_view, new InsetLatexAccent(ins));
319 insertVerbatim(str, text);
323 string tmp; tmp += static_cast<char>(enc.second);
324 insertVerbatim(tmp, text);
328 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 string const res = trans_fsm_
336 .currentState->deadkey(c, i);
342 if (active_ == &default_ || c == 0) {
345 i.allowed = lyx_accent_table[accent].native;
347 i.exception_list = 0;
349 string res = trans_fsm_.currentState->deadkey(c, i);
352 // Go through the translation
353 TranslateAndInsert(c, t);