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"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
25 #include "support/lstrings.h"
37 extern string const DoAccent(string const &, tex_accent);
38 extern string const DoAccent(char, tex_accent);
42 TransFSMData::TransFSMData()
44 deadkey_ = deadkey2_ = 0;
45 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
50 char const TransState::TOKEN_SEP = 4;
54 TransInitState::TransInitState()
60 string const TransInitState::normalkey(char c)
68 string const TransInitState::deadkey(char c, KmodInfo d)
72 currentState = deadkey_state_;
78 TransDeadkeyState::TransDeadkeyState()
80 deadkey_state_ = this;
84 string const TransDeadkeyState::normalkey(char c)
88 KmodException::iterator it = deadkey_info_.exception_list.begin();
89 KmodException::iterator end = deadkey_info_.exception_list.end();
91 for (; it != end; ++it) {
98 res = DoAccent(c, deadkey_info_.accent);
100 currentState = init_state_;
105 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
109 // Check if the same deadkey was typed twice
113 deadkey_info_.accent = TEX_NOACCENT;
114 currentState = init_state_;
118 // Check if it is a combination or an exception
119 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
120 KmodException::const_iterator end = deadkey_info_.exception_list.end();
121 for (; cit != end; ++cit) {
122 if (cit->combined == true && cit->accent == d.accent) {
126 currentState = combined_state_;
132 deadkey_info_.accent = TEX_NOACCENT;
133 currentState = init_state_;
138 // Not a combination or an exception.
139 // Output deadkey1 and keep deadkey2
145 currentState = deadkey_state_;
150 TransCombinedState::TransCombinedState()
152 combined_state_ = this;
156 string const TransCombinedState::normalkey(char c)
158 string const temp = DoAccent(c, deadkey2_info_.accent);
159 string const res = DoAccent(temp, deadkey_info_.accent);
160 currentState = init_state_;
165 string const TransCombinedState::deadkey(char c, KmodInfo d)
167 // Third key in a row. Output the first one and
168 // reenter with shifted deadkeys
173 deadkey_ = deadkey2_;
174 deadkey_info_ = deadkey2_info_;
175 res += deadkey_state_->deadkey(c, d);
181 TransFSM::TransFSM():
187 currentState = init_state_;
193 // Initialize static member.
194 Trans TransManager::default_;
197 TransManager::TransManager()
198 : active_(0), t1_(new Trans), t2_(new Trans)
202 // For the sake of boost::scoped_ptr.
203 TransManager::~TransManager()
207 int TransManager::setPrimary(string const & language)
209 if (t1_->getName() == language)
212 return t1_->load(language);
216 int TransManager::setSecondary(string const & language)
218 if (t2_->getName() == language)
221 return t2_->load(language);
225 bool TransManager::setCharset(string const & str)
227 return chset_.loadFile(str);
231 void TransManager::enablePrimary()
233 if (t1_->isDefined())
236 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
240 void TransManager::enableSecondary()
242 if (t2_->isDefined())
244 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
248 void TransManager::disableKeymap()
251 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
255 void TransManager::translateAndInsert(char c, LyXText * text, LCursor & cur)
257 string res = active_->process(c, *this);
259 // Process with tokens
262 while (res.length() > 0) {
263 res = split(res, temp, TransState::TOKEN_SEP);
264 insert(temp, text, cur);
269 void TransManager::insertVerbatim(string const & str, LyXText * text, LCursor & cur)
271 for (string::size_type i = 0, n = str.size(); i < n; ++i)
272 text->insertChar(cur, str[i]);
276 void TransManager::insert(string const & str, LyXText * text, LCursor & cur)
278 // Go through the character encoding only if the current
279 // encoding (chset_->name()) matches the current font_norm
280 // (lyrxc->font_norm)
282 // Is false to speak about "only if" the current encoding will
283 // almost always be equal to font_norm.
284 pair<bool, int> enc = chset_.encodeString(str);
285 if (chset_.getName() != lyxrc.font_norm ||
287 // Could not find an encoding
288 insertVerbatim(str, text, cur);
291 string const tmp(1, static_cast<char>(enc.second));
292 insertVerbatim(tmp, text, cur);
296 void TransManager::deadkey(char c, tex_accent accent, LyXText * t, LCursor & cur)
298 if (c == 0 && active_ != &default_) {
299 // A deadkey was pressed that cannot be printed
300 // or a accent command was typed in the minibuffer
302 if (active_->isAccentDefined(accent, i) == true) {
303 string const res = trans_fsm_
304 .currentState->deadkey(c, i);
310 if (active_ == &default_ || c == 0) {
314 string res = trans_fsm_.currentState->deadkey(c, i);
317 // Go through the translation
318 translateAndInsert(c, t, cur);