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"
38 TransFSMData::TransFSMData()
40 deadkey_ = deadkey2_ = 0;
41 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
46 char_type const TransState::TOKEN_SEP = 4;
50 TransInitState::TransInitState()
56 docstring const TransInitState::normalkey(char_type c)
64 docstring const TransInitState::deadkey(char_type c, KmodInfo d)
68 currentState = deadkey_state_;
74 TransDeadkeyState::TransDeadkeyState()
76 deadkey_state_ = this;
80 docstring const TransDeadkeyState::normalkey(char_type c)
84 KmodException::iterator it = deadkey_info_.exception_list.begin();
85 KmodException::iterator end = deadkey_info_.exception_list.end();
87 for (; it != end; ++it) {
94 res = DoAccent(c, deadkey_info_.accent);
96 currentState = init_state_;
101 docstring const TransDeadkeyState::deadkey(char_type c, KmodInfo d)
105 // Check if the same deadkey was typed twice
109 deadkey_info_.accent = TEX_NOACCENT;
110 currentState = init_state_;
114 // Check if it is a combination or an exception
115 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
116 KmodException::const_iterator end = deadkey_info_.exception_list.end();
117 for (; cit != end; ++cit) {
118 if (cit->combined == true && cit->accent == d.accent) {
122 currentState = combined_state_;
128 deadkey_info_.accent = TEX_NOACCENT;
129 currentState = init_state_;
134 // Not a combination or an exception.
135 // Output deadkey1 and keep deadkey2
141 currentState = deadkey_state_;
146 TransCombinedState::TransCombinedState()
148 combined_state_ = this;
152 docstring const TransCombinedState::normalkey(char_type c)
154 docstring const temp = DoAccent(c, deadkey2_info_.accent);
155 docstring const res = DoAccent(temp, deadkey_info_.accent);
156 currentState = init_state_;
161 docstring const TransCombinedState::deadkey(char_type c, KmodInfo d)
163 // Third key in a row. Output the first one and
164 // reenter with shifted deadkeys
169 deadkey_ = deadkey2_;
170 deadkey_info_ = deadkey2_info_;
171 res += deadkey_state_->deadkey(c, d);
177 TransFSM::TransFSM():
183 currentState = init_state_;
189 // Initialize static member.
190 Trans TransManager::default_;
193 TransManager::TransManager()
194 : active_(0), t1_(new Trans), t2_(new Trans)
198 // For the sake of boost::scoped_ptr.
199 TransManager::~TransManager()
203 int TransManager::setPrimary(string const & language)
205 if (t1_->getName() == language)
208 return t1_->load(language);
212 int TransManager::setSecondary(string const & language)
214 if (t2_->getName() == language)
217 return t2_->load(language);
221 void TransManager::enablePrimary()
223 if (t1_->isDefined())
226 LYXERR(Debug::KBMAP) << "Enabling primary keymap" << endl;
230 void TransManager::enableSecondary()
232 if (t2_->isDefined())
234 LYXERR(Debug::KBMAP) << "Enabling secondary keymap" << endl;
238 void TransManager::disableKeymap()
241 LYXERR(Debug::KBMAP) << "Disabling keymap" << endl;
245 void TransManager::translateAndInsert(char_type c, LyXText * text, LCursor & cur)
247 docstring res = active_->process(c, *this);
249 // Process with tokens
252 while (res.length() > 0) {
253 res = split(res, temp, TransState::TOKEN_SEP);
254 insert(temp, text, cur);
259 void TransManager::insert(docstring const & str, LyXText * text, LCursor & cur)
261 for (string::size_type i = 0, n = str.size(); i < n; ++i)
262 text->insertChar(cur, str[i]);
266 void TransManager::deadkey(char_type c, tex_accent accent, LyXText * t, LCursor & cur)
268 if (c == 0 && active_ != &default_) {
269 // A deadkey was pressed that cannot be printed
270 // or a accent command was typed in the minibuffer
272 if (active_->isAccentDefined(accent, i) == true) {
273 docstring const res = trans_fsm_
274 .currentState->deadkey(c, i);
280 if (active_ == &default_ || c == 0) {
284 docstring res = trans_fsm_.currentState->deadkey(c, i);
287 // Go through the translation
288 translateAndInsert(c, t, cur);