9 #include "insets/insetlatexaccent.h"
10 #include "BufferView.h"
13 #include "support/lstrings.h"
15 using namespace lyx::support;
20 extern string const DoAccent(string const &, tex_accent);
21 extern string const DoAccent(char, tex_accent);
25 TransFSMData::TransFSMData()
27 deadkey_ = deadkey2_ = 0;
28 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
33 char const TransState::TOKEN_SEP = 4;
37 TransInitState::TransInitState()
43 string const TransInitState::normalkey(char c)
51 string const TransInitState::deadkey(char c, KmodInfo d)
55 currentState = deadkey_state_;
61 TransDeadkeyState::TransDeadkeyState()
63 deadkey_state_ = this;
67 string const TransDeadkeyState::normalkey(char c)
71 KmodException::iterator it = deadkey_info_.exception_list.begin();
72 KmodException::iterator end = deadkey_info_.exception_list.end();
74 for (; it != end; ++it) {
81 res = DoAccent(c, deadkey_info_.accent);
83 currentState = init_state_;
88 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
92 // Check if the same deadkey was typed twice
96 deadkey_info_.accent = TEX_NOACCENT;
97 currentState = init_state_;
101 // Check if it is a combination or an exception
102 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
103 KmodException::const_iterator end = deadkey_info_.exception_list.end();
104 for (; cit != end; ++cit) {
105 if (cit->combined == true && cit->accent == d.accent) {
109 currentState = combined_state_;
115 deadkey_info_.accent = TEX_NOACCENT;
116 currentState = init_state_;
121 // Not a combination or an exception.
122 // Output deadkey1 and keep deadkey2
128 currentState = deadkey_state_;
133 TransCombinedState::TransCombinedState()
135 combined_state_ = this;
139 string const TransCombinedState::normalkey(char c)
141 string const temp = DoAccent(c, deadkey2_info_.accent);
142 string const res = DoAccent(temp, deadkey_info_.accent);
143 currentState = init_state_;
148 string const TransCombinedState::deadkey(char c, KmodInfo d)
150 // Third key in a row. Output the first one and
151 // reenter with shifted deadkeys
156 deadkey_ = deadkey2_;
157 deadkey_info_ = deadkey2_info_;
158 res += deadkey_state_->deadkey(c, d);
164 TransFSM::TransFSM():
170 currentState = init_state_;
176 // Initialize static member.
177 Trans TransManager::default_;
180 TransManager::TransManager()
181 : active_(0), t1_(new Trans), t2_(new Trans)
185 TransManager::~TransManager()
192 int TransManager::SetPrimary(string const & language)
194 if (t1_->GetName() == language)
197 return t1_->Load(language);
201 int TransManager::SetSecondary(string const & language)
203 if (t2_->GetName() == language)
206 return t2_->Load(language);
210 bool TransManager::setCharset(string const & str)
212 return chset_.loadFile(str);
216 void TransManager::EnablePrimary()
218 if (t1_->IsDefined())
221 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
225 void TransManager::EnableSecondary()
227 if (t2_->IsDefined())
229 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
233 void TransManager::DisableKeymap()
236 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
240 void TransManager::TranslateAndInsert(char c, LyXText * text)
242 string res = active_->process(c, *this);
244 // Process with tokens
247 while (res.length() > 0) {
248 res = split(res, temp, TransState::TOKEN_SEP);
254 void TransManager::insertVerbatim(string const & str, LyXText * text)
256 string::size_type const l = str.length();
258 for (string::size_type i = 0; i < l; ++i) {
259 text->insertChar(str[i]);
264 void TransManager::insert(string const & str, LyXText * text)
266 // Go through the character encoding only if the current
267 // encoding (chset_->name()) matches the current font_norm
268 // (lyrxc->font_norm)
270 // Is false to speak about "only if" the current encoding will
271 // almost always be equal to font_norm.
272 pair<bool, int> enc = chset_.encodeString(str);
273 if (chset_.getName() != lyxrc.font_norm ||
275 // Could not find an encoding
276 InsetLatexAccent ins(str);
277 if (ins.canDisplay()) {
279 new InsetLatexAccent(ins));
281 insertVerbatim(str, text);
286 tmp += static_cast<char>(enc.second);
287 insertVerbatim(tmp, text);
291 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
293 if (c == 0 && active_ != &default_) {
294 // A deadkey was pressed that cannot be printed
295 // or a accent command was typed in the minibuffer
297 if (active_->isAccentDefined(accent, i) == true) {
298 string const res = trans_fsm_
299 .currentState->deadkey(c, i);
305 if (active_ == &default_ || c == 0) {
309 string res = trans_fsm_.currentState->deadkey(c, i);
312 // Go through the translation
313 TranslateAndInsert(c, t);