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;
36 char const TransState::TOKEN_SEP = 4;
40 TransInitState::TransInitState()
46 string const TransInitState::normalkey(char c)
54 string const TransInitState::deadkey(char c, KmodInfo d)
58 currentState = deadkey_state_;
64 TransDeadkeyState::TransDeadkeyState()
66 deadkey_state_ = this;
70 string const TransDeadkeyState::normalkey(char c)
74 KmodException::iterator it = deadkey_info_.exception_list.begin();
75 KmodException::iterator end = deadkey_info_.exception_list.end();
77 for (; it != end; ++it) {
84 res = DoAccent(c, deadkey_info_.accent);
86 currentState = init_state_;
91 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
95 // Check if the same deadkey was typed twice
99 deadkey_info_.accent = TEX_NOACCENT;
100 currentState = init_state_;
104 // Check if it is a combination or an exception
105 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
106 KmodException::const_iterator end = deadkey_info_.exception_list.end();
107 for (; cit != end; ++cit) {
108 if (cit->combined == true && cit->accent == d.accent) {
112 currentState = combined_state_;
118 deadkey_info_.accent = TEX_NOACCENT;
119 currentState = init_state_;
124 // Not a combination or an exception.
125 // Output deadkey1 and keep deadkey2
131 currentState = deadkey_state_;
136 TransCombinedState::TransCombinedState()
138 combined_state_ = this;
142 string const TransCombinedState::normalkey(char c)
144 string const temp = DoAccent(c, deadkey2_info_.accent);
145 string const res = DoAccent(temp, deadkey_info_.accent);
146 currentState = init_state_;
151 string const TransCombinedState::deadkey(char c, KmodInfo d)
153 // Third key in a row. Output the first one and
154 // reenter with shifted deadkeys
159 deadkey_ = deadkey2_;
160 deadkey_info_ = deadkey2_info_;
161 res += deadkey_state_->deadkey(c, d);
167 TransFSM::TransFSM():
173 currentState = init_state_;
179 // Initialize static member.
180 Trans TransManager::default_;
183 TransManager::TransManager()
184 : active_(0), t1_(new Trans), t2_(new Trans)
188 TransManager::~TransManager()
195 int TransManager::SetPrimary(string const & language)
197 if (t1_->GetName() == language)
200 return t1_->Load(language);
204 int TransManager::SetSecondary(string const & language)
206 if (t2_->GetName() == language)
209 return t2_->Load(language);
213 bool TransManager::setCharset(string const & str)
215 return chset_.loadFile(str);
219 void TransManager::EnablePrimary()
221 if (t1_->IsDefined())
224 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
228 void TransManager::EnableSecondary()
230 if (t2_->IsDefined())
232 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
236 void TransManager::DisableKeymap()
239 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
243 void TransManager::TranslateAndInsert(char c, LyXText * text)
245 string res = active_->process(c, *this);
247 // Process with tokens
250 while (res.length() > 0) {
251 res = split(res, temp, TransState::TOKEN_SEP);
257 void TransManager::insertVerbatim(string const & str, LyXText * text)
259 string::size_type const l = str.length();
261 for (string::size_type i = 0; i < l; ++i) {
262 text->insertChar(current_view, str[i]);
267 void TransManager::insert(string const & str, LyXText * text)
269 // Go through the character encoding only if the current
270 // encoding (chset_->name()) matches the current font_norm
271 // (lyrxc->font_norm)
273 // Is false to speak about "only if" the current encoding will
274 // almost always be equal to font_norm.
275 pair<bool, int> enc = chset_.encodeString(str);
276 if (chset_.getName() != lyxrc.font_norm ||
278 // Could not find an encoding
279 InsetLatexAccent ins(str);
280 if (ins.canDisplay()) {
281 text->insertInset(current_view,
282 new InsetLatexAccent(ins));
284 insertVerbatim(str, text);
289 tmp += static_cast<char>(enc.second);
290 insertVerbatim(tmp, text);
294 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
296 if (c == 0 && active_ != &default_) {
297 // A deadkey was pressed that cannot be printed
298 // or a accent command was typed in the minibuffer
300 if (active_->isAccentDefined(accent, i) == true) {
301 string const res = trans_fsm_
302 .currentState->deadkey(c, i);
308 if (active_ == &default_ || c == 0) {
312 string res = trans_fsm_.currentState->deadkey(c, i);
315 // Go through the translation
316 TranslateAndInsert(c, t);