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) {
164 temp = DoAccent(c, deadkey2_info_.accent);
165 res = DoAccent(temp, deadkey_info_.accent);
166 currentState = init_state_;
168 // Not allowed. Output deadkey1 and check deadkey2 + c
172 deadkey_ = deadkey2_;
173 deadkey_info_ = deadkey2_info_;
174 // Call deadkey state and leave it to setup the FSM
175 res += deadkey_state_->normalkey(c, trans);
181 string const TransCombinedState::deadkey(char c, KmodInfo d)
183 // Third key in a row. Output the first one and
184 // reenter with shifted deadkeys
189 deadkey_ = deadkey2_;
190 deadkey_info_ = deadkey2_info_;
191 res += deadkey_state_->deadkey(c, d);
197 TransFSM::TransFSM():
203 currentState = init_state_;
209 // Initialize static member.
210 Trans TransManager::default_;
213 TransManager::TransManager()
214 : active_(0), t1_(new Trans), t2_(new Trans)
218 TransManager::~TransManager()
225 int TransManager::SetPrimary(string const & language)
227 if (t1_->GetName() == language)
230 return t1_->Load(language);
234 int TransManager::SetSecondary(string const & language)
236 if (t2_->GetName() == language)
239 return t2_->Load(language);
243 bool TransManager::setCharset(string const & str)
245 return chset_.loadFile(str);
249 void TransManager::EnablePrimary()
251 if (t1_->IsDefined())
254 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
258 void TransManager::EnableSecondary()
260 if (t2_->IsDefined())
262 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
266 void TransManager::DisableKeymap()
269 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
273 void TransManager::TranslateAndInsert(char c, LyXText * text)
275 string res = active_->process(c, *this);
277 // Process with tokens
280 while(res.length() > 0) {
281 res = split(res, temp, TransState::TOKEN_SEP);
287 void TransManager::insertVerbatim(string const & str, LyXText * text)
289 string::size_type const l = str.length();
291 for (string::size_type i = 0; i < l; ++i) {
293 && text->GetFont(current_view->buffer(),text->cursor.par(),
294 text->cursor.pos()).latex() == LyXFont::OFF
295 && text->GetFont(current_view->buffer(),text->cursor.par(),
296 text->cursor.pos()).language()->lang() != "hebrew")
297 current_view->insertCorrectQuote();
299 text->InsertChar(current_view, str[i]);
304 void TransManager::insert(string const & str, LyXText * text)
306 // Go through the character encoding only if the current
307 // encoding (chset_->name()) matches the current font_norm
310 // Is false to speak about "only if" the current encoding will
311 // almost always be equal to font_norm.
312 pair<bool, int> enc = chset_.encodeString(str);
313 if (chset_.getName() != lyxrc.font_norm ||
315 // Could not find an encoding
316 InsetLatexAccent ins(str);
317 if (ins.CanDisplay()) {
318 text->InsertInset(current_view, new InsetLatexAccent(ins));
320 insertVerbatim(str, text);
324 string tmp; tmp += static_cast<char>(enc.second);
325 insertVerbatim(tmp, text);
329 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
331 if (c == 0 && active_ != &default_) {
332 // A deadkey was pressed that cannot be printed
333 // or a accent command was typed in the minibuffer
335 if (active_->isAccentDefined(accent, i) == true) {
336 string res = trans_fsm_.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);