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"
17 #include "CutAndPaste.h"
24 #include "insets/insetlatexaccent.h"
26 #include "support/lstrings.h"
28 using lyx::support::split;
35 extern string const DoAccent(string const &, tex_accent);
36 extern string const DoAccent(char, tex_accent);
40 TransFSMData::TransFSMData()
42 deadkey_ = deadkey2_ = 0;
43 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
48 char const TransState::TOKEN_SEP = 4;
52 TransInitState::TransInitState()
58 string const TransInitState::normalkey(char c)
66 string const TransInitState::deadkey(char c, KmodInfo d)
70 currentState = deadkey_state_;
76 TransDeadkeyState::TransDeadkeyState()
78 deadkey_state_ = this;
82 string const TransDeadkeyState::normalkey(char c)
86 KmodException::iterator it = deadkey_info_.exception_list.begin();
87 KmodException::iterator end = deadkey_info_.exception_list.end();
89 for (; it != end; ++it) {
96 res = DoAccent(c, deadkey_info_.accent);
98 currentState = init_state_;
103 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
107 // Check if the same deadkey was typed twice
111 deadkey_info_.accent = TEX_NOACCENT;
112 currentState = init_state_;
116 // Check if it is a combination or an exception
117 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
118 KmodException::const_iterator end = deadkey_info_.exception_list.end();
119 for (; cit != end; ++cit) {
120 if (cit->combined == true && cit->accent == d.accent) {
124 currentState = combined_state_;
130 deadkey_info_.accent = TEX_NOACCENT;
131 currentState = init_state_;
136 // Not a combination or an exception.
137 // Output deadkey1 and keep deadkey2
143 currentState = deadkey_state_;
148 TransCombinedState::TransCombinedState()
150 combined_state_ = this;
154 string const TransCombinedState::normalkey(char c)
156 string const temp = DoAccent(c, deadkey2_info_.accent);
157 string const res = DoAccent(temp, deadkey_info_.accent);
158 currentState = init_state_;
163 string const TransCombinedState::deadkey(char c, KmodInfo d)
165 // Third key in a row. Output the first one and
166 // reenter with shifted deadkeys
171 deadkey_ = deadkey2_;
172 deadkey_info_ = deadkey2_info_;
173 res += deadkey_state_->deadkey(c, d);
179 TransFSM::TransFSM():
185 currentState = init_state_;
191 // Initialize static member.
192 Trans TransManager::default_;
195 TransManager::TransManager()
196 : active_(0), t1_(new Trans), t2_(new Trans)
200 // For the sake of boost::scoped_ptr.
201 TransManager::~TransManager()
205 int TransManager::SetPrimary(string const & language)
207 if (t1_->GetName() == language)
210 return t1_->Load(language);
214 int TransManager::SetSecondary(string const & language)
216 if (t2_->GetName() == language)
219 return t2_->Load(language);
223 bool TransManager::setCharset(string const & str)
225 return chset_.loadFile(str);
229 void TransManager::EnablePrimary()
231 if (t1_->IsDefined())
234 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
238 void TransManager::EnableSecondary()
240 if (t2_->IsDefined())
242 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
246 void TransManager::DisableKeymap()
249 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
253 void TransManager::TranslateAndInsert(char c, LyXText * text)
255 string res = active_->process(c, *this);
257 // Process with tokens
260 while (res.length() > 0) {
261 res = split(res, temp, TransState::TOKEN_SEP);
267 void TransManager::insertVerbatim(string const & str, LyXText * text)
269 for (string::size_type i = 0, n = str.size(); i < n; ++i)
270 text->insertChar(text->bv()->cursor(), str[i]);
274 void TransManager::insert(string const & str, LyXText * text)
276 // Go through the character encoding only if the current
277 // encoding (chset_->name()) matches the current font_norm
278 // (lyrxc->font_norm)
280 // Is false to speak about "only if" the current encoding will
281 // almost always be equal to font_norm.
282 pair<bool, int> enc = chset_.encodeString(str);
283 if (chset_.getName() != lyxrc.font_norm ||
285 // Could not find an encoding
286 InsetLatexAccent ins(str);
287 if (ins.canDisplay()) {
288 LCursor & cur = text->bv()->cursor();
289 lyx::cap::replaceSelection(cur);
290 cur.insert(new InsetLatexAccent(ins));
293 insertVerbatim(str, text);
297 string const tmp(1, static_cast<char>(enc.second));
298 insertVerbatim(tmp, text);
302 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
304 if (c == 0 && active_ != &default_) {
305 // A deadkey was pressed that cannot be printed
306 // or a accent command was typed in the minibuffer
308 if (active_->isAccentDefined(accent, i) == true) {
309 string const res = trans_fsm_
310 .currentState->deadkey(c, i);
316 if (active_ == &default_ || c == 0) {
320 string res = trans_fsm_.currentState->deadkey(c, i);
323 // Go through the translation
324 TranslateAndInsert(c, t);