9 #include "insets/insetlatexaccent.h"
10 #include "BufferView.h"
13 #include "support/lstrings.h"
18 extern string const DoAccent(string const &, tex_accent);
19 extern string const DoAccent(char, tex_accent);
20 extern BufferView * current_view;
24 TransFSMData::TransFSMData()
26 deadkey_ = deadkey2_ = 0;
27 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
32 char const TransState::TOKEN_SEP = 4;
36 TransInitState::TransInitState()
42 string const TransInitState::normalkey(char c)
50 string const TransInitState::deadkey(char c, KmodInfo d)
54 currentState = deadkey_state_;
60 TransDeadkeyState::TransDeadkeyState()
62 deadkey_state_ = this;
66 string const TransDeadkeyState::normalkey(char c)
70 KmodException::iterator it = deadkey_info_.exception_list.begin();
71 KmodException::iterator end = deadkey_info_.exception_list.end();
73 for (; it != end; ++it) {
80 res = DoAccent(c, deadkey_info_.accent);
82 currentState = init_state_;
87 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
91 // Check if the same deadkey was typed twice
95 deadkey_info_.accent = TEX_NOACCENT;
96 currentState = init_state_;
100 // Check if it is a combination or an exception
101 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
102 KmodException::const_iterator end = deadkey_info_.exception_list.end();
103 for (; cit != end; ++cit) {
104 if (cit->combined == true && cit->accent == d.accent) {
108 currentState = combined_state_;
114 deadkey_info_.accent = TEX_NOACCENT;
115 currentState = init_state_;
120 // Not a combination or an exception.
121 // Output deadkey1 and keep deadkey2
127 currentState = deadkey_state_;
132 TransCombinedState::TransCombinedState()
134 combined_state_ = this;
138 string const TransCombinedState::normalkey(char c)
140 string const temp = DoAccent(c, deadkey2_info_.accent);
141 string const res = DoAccent(temp, deadkey_info_.accent);
142 currentState = init_state_;
147 string const TransCombinedState::deadkey(char c, KmodInfo d)
149 // Third key in a row. Output the first one and
150 // reenter with shifted deadkeys
155 deadkey_ = deadkey2_;
156 deadkey_info_ = deadkey2_info_;
157 res += deadkey_state_->deadkey(c, d);
163 TransFSM::TransFSM():
169 currentState = init_state_;
175 // Initialize static member.
176 Trans TransManager::default_;
179 TransManager::TransManager()
180 : active_(0), t1_(new Trans), t2_(new Trans)
184 TransManager::~TransManager()
191 int TransManager::SetPrimary(string const & language)
193 if (t1_->GetName() == language)
196 return t1_->Load(language);
200 int TransManager::SetSecondary(string const & language)
202 if (t2_->GetName() == language)
205 return t2_->Load(language);
209 bool TransManager::setCharset(string const & str)
211 return chset_.loadFile(str);
215 void TransManager::EnablePrimary()
217 if (t1_->IsDefined())
220 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
224 void TransManager::EnableSecondary()
226 if (t2_->IsDefined())
228 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
232 void TransManager::DisableKeymap()
235 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
239 void TransManager::TranslateAndInsert(char c, LyXText * text)
241 string res = active_->process(c, *this);
243 // Process with tokens
246 while (res.length() > 0) {
247 res = split(res, temp, TransState::TOKEN_SEP);
253 void TransManager::insertVerbatim(string const & str, LyXText * text)
255 string::size_type const l = str.length();
257 for (string::size_type i = 0; i < l; ++i) {
258 text->insertChar(current_view, str[i]);
263 void TransManager::insert(string const & str, LyXText * text)
265 // Go through the character encoding only if the current
266 // encoding (chset_->name()) matches the current font_norm
267 // (lyrxc->font_norm)
269 // Is false to speak about "only if" the current encoding will
270 // almost always be equal to font_norm.
271 pair<bool, int> enc = chset_.encodeString(str);
272 if (chset_.getName() != lyxrc.font_norm ||
274 // Could not find an encoding
275 InsetLatexAccent ins(str);
276 if (ins.canDisplay()) {
277 text->insertInset(current_view,
278 new InsetLatexAccent(ins));
280 insertVerbatim(str, text);
285 tmp += static_cast<char>(enc.second);
286 insertVerbatim(tmp, text);
290 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
292 if (c == 0 && active_ != &default_) {
293 // A deadkey was pressed that cannot be printed
294 // or a accent command was typed in the minibuffer
296 if (active_->isAccentDefined(accent, i) == true) {
297 string const res = trans_fsm_
298 .currentState->deadkey(c, i);
304 if (active_ == &default_ || c == 0) {
308 string res = trans_fsm_.currentState->deadkey(c, i);
311 // Go through the translation
312 TranslateAndInsert(c, t);