3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * Full author contact details are available in file CREDITS.
14 #include "trans_mgr.h"
16 #include "BufferView.h"
23 #include "insets/insetlatexaccent.h"
25 #include "support/lstrings.h"
27 using lyx::support::split;
34 extern string const DoAccent(string const &, tex_accent);
35 extern string const DoAccent(char, tex_accent);
39 TransFSMData::TransFSMData()
41 deadkey_ = deadkey2_ = 0;
42 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
47 char const TransState::TOKEN_SEP = 4;
51 TransInitState::TransInitState()
57 string const TransInitState::normalkey(char c)
65 string const TransInitState::deadkey(char c, KmodInfo d)
69 currentState = deadkey_state_;
75 TransDeadkeyState::TransDeadkeyState()
77 deadkey_state_ = this;
81 string const TransDeadkeyState::normalkey(char c)
85 KmodException::iterator it = deadkey_info_.exception_list.begin();
86 KmodException::iterator end = deadkey_info_.exception_list.end();
88 for (; it != end; ++it) {
95 res = DoAccent(c, deadkey_info_.accent);
97 currentState = init_state_;
102 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
106 // Check if the same deadkey was typed twice
110 deadkey_info_.accent = TEX_NOACCENT;
111 currentState = init_state_;
115 // Check if it is a combination or an exception
116 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
117 KmodException::const_iterator end = deadkey_info_.exception_list.end();
118 for (; cit != end; ++cit) {
119 if (cit->combined == true && cit->accent == d.accent) {
123 currentState = combined_state_;
129 deadkey_info_.accent = TEX_NOACCENT;
130 currentState = init_state_;
135 // Not a combination or an exception.
136 // Output deadkey1 and keep deadkey2
142 currentState = deadkey_state_;
147 TransCombinedState::TransCombinedState()
149 combined_state_ = this;
153 string const TransCombinedState::normalkey(char c)
155 string const temp = DoAccent(c, deadkey2_info_.accent);
156 string const res = DoAccent(temp, deadkey_info_.accent);
157 currentState = init_state_;
162 string const TransCombinedState::deadkey(char c, KmodInfo d)
164 // Third key in a row. Output the first one and
165 // reenter with shifted deadkeys
170 deadkey_ = deadkey2_;
171 deadkey_info_ = deadkey2_info_;
172 res += deadkey_state_->deadkey(c, d);
178 TransFSM::TransFSM():
184 currentState = init_state_;
190 // Initialize static member.
191 Trans TransManager::default_;
194 TransManager::TransManager()
195 : active_(0), t1_(new Trans), t2_(new Trans)
199 // For the sake of boost::scoped_ptr.
200 TransManager::~TransManager()
204 int TransManager::SetPrimary(string const & language)
206 if (t1_->GetName() == language)
209 return t1_->Load(language);
213 int TransManager::SetSecondary(string const & language)
215 if (t2_->GetName() == language)
218 return t2_->Load(language);
222 bool TransManager::setCharset(string const & str)
224 return chset_.loadFile(str);
228 void TransManager::EnablePrimary()
230 if (t1_->IsDefined())
233 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
237 void TransManager::EnableSecondary()
239 if (t2_->IsDefined())
241 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
245 void TransManager::DisableKeymap()
248 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
252 void TransManager::TranslateAndInsert(char c, LyXText * text)
254 string res = active_->process(c, *this);
256 // Process with tokens
259 while (res.length() > 0) {
260 res = split(res, temp, TransState::TOKEN_SEP);
266 void TransManager::insertVerbatim(string const & str, LyXText * text)
268 for (string::size_type i = 0, n = str.size(); i < n; ++i)
269 text->insertChar(text->bv()->cursor(), str[i]);
273 void TransManager::insert(string const & str, LyXText * text)
275 // Go through the character encoding only if the current
276 // encoding (chset_->name()) matches the current font_norm
277 // (lyrxc->font_norm)
279 // Is false to speak about "only if" the current encoding will
280 // almost always be equal to font_norm.
281 pair<bool, int> enc = chset_.encodeString(str);
282 if (chset_.getName() != lyxrc.font_norm ||
284 // Could not find an encoding
285 InsetLatexAccent ins(str);
286 if (ins.canDisplay()) {
287 text->bv()->cursor().insert(new InsetLatexAccent(ins));
289 insertVerbatim(str, text);
293 string const tmp(1, static_cast<char>(enc.second));
294 insertVerbatim(tmp, text);
298 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
300 if (c == 0 && active_ != &default_) {
301 // A deadkey was pressed that cannot be printed
302 // or a accent command was typed in the minibuffer
304 if (active_->isAccentDefined(accent, i) == true) {
305 string const res = trans_fsm_
306 .currentState->deadkey(c, i);
312 if (active_ == &default_ || c == 0) {
316 string res = trans_fsm_.currentState->deadkey(c, i);
319 // Go through the translation
320 TranslateAndInsert(c, t);