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 "insets/insetlatexaccent.h"
27 #include "support/lstrings.h"
39 extern string const DoAccent(string const &, tex_accent);
40 extern string const DoAccent(char, tex_accent);
44 TransFSMData::TransFSMData()
46 deadkey_ = deadkey2_ = 0;
47 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
52 char const TransState::TOKEN_SEP = 4;
56 TransInitState::TransInitState()
62 string const TransInitState::normalkey(char c)
70 string const TransInitState::deadkey(char c, KmodInfo d)
74 currentState = deadkey_state_;
80 TransDeadkeyState::TransDeadkeyState()
82 deadkey_state_ = this;
86 string const TransDeadkeyState::normalkey(char c)
90 KmodException::iterator it = deadkey_info_.exception_list.begin();
91 KmodException::iterator end = deadkey_info_.exception_list.end();
93 for (; it != end; ++it) {
100 res = DoAccent(c, deadkey_info_.accent);
102 currentState = init_state_;
107 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
111 // Check if the same deadkey was typed twice
115 deadkey_info_.accent = TEX_NOACCENT;
116 currentState = init_state_;
120 // Check if it is a combination or an exception
121 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
122 KmodException::const_iterator end = deadkey_info_.exception_list.end();
123 for (; cit != end; ++cit) {
124 if (cit->combined == true && cit->accent == d.accent) {
128 currentState = combined_state_;
134 deadkey_info_.accent = TEX_NOACCENT;
135 currentState = init_state_;
140 // Not a combination or an exception.
141 // Output deadkey1 and keep deadkey2
147 currentState = deadkey_state_;
152 TransCombinedState::TransCombinedState()
154 combined_state_ = this;
158 string const TransCombinedState::normalkey(char c)
160 string const temp = DoAccent(c, deadkey2_info_.accent);
161 string const res = DoAccent(temp, deadkey_info_.accent);
162 currentState = init_state_;
167 string const TransCombinedState::deadkey(char c, KmodInfo d)
169 // Third key in a row. Output the first one and
170 // reenter with shifted deadkeys
175 deadkey_ = deadkey2_;
176 deadkey_info_ = deadkey2_info_;
177 res += deadkey_state_->deadkey(c, d);
183 TransFSM::TransFSM():
189 currentState = init_state_;
195 // Initialize static member.
196 Trans TransManager::default_;
199 TransManager::TransManager()
200 : active_(0), t1_(new Trans), t2_(new Trans)
204 // For the sake of boost::scoped_ptr.
205 TransManager::~TransManager()
209 int TransManager::setPrimary(string const & language)
211 if (t1_->getName() == language)
214 return t1_->load(language);
218 int TransManager::setSecondary(string const & language)
220 if (t2_->getName() == language)
223 return t2_->load(language);
227 bool TransManager::setCharset(string const & str)
229 return chset_.loadFile(str);
233 void TransManager::enablePrimary()
235 if (t1_->isDefined())
238 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
242 void TransManager::enableSecondary()
244 if (t2_->isDefined())
246 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
250 void TransManager::disableKeymap()
253 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
257 void TransManager::translateAndInsert(char c, LyXText * text, LCursor & cur)
259 string res = active_->process(c, *this);
261 // Process with tokens
264 while (res.length() > 0) {
265 res = split(res, temp, TransState::TOKEN_SEP);
266 insert(temp, text, cur);
271 void TransManager::insertVerbatim(string const & str, LyXText * text, LCursor & cur)
273 for (string::size_type i = 0, n = str.size(); i < n; ++i)
274 text->insertChar(cur, str[i]);
278 void TransManager::insert(string const & str, LyXText * text, LCursor & cur)
280 // Go through the character encoding only if the current
281 // encoding (chset_->name()) matches the current font_norm
282 // (lyrxc->font_norm)
284 // Is false to speak about "only if" the current encoding will
285 // almost always be equal to font_norm.
286 pair<bool, int> enc = chset_.encodeString(str);
287 if (chset_.getName() != lyxrc.font_norm ||
289 // Could not find an encoding
290 InsetLatexAccent ins(str);
291 if (ins.canDisplay()) {
292 cap::replaceSelection(cur);
293 cur.insert(new InsetLatexAccent(ins));
296 insertVerbatim(str, text, cur);
300 string const tmp(1, static_cast<char>(enc.second));
301 insertVerbatim(tmp, text, cur);
305 void TransManager::deadkey(char c, tex_accent accent, LyXText * t, LCursor & cur)
307 if (c == 0 && active_ != &default_) {
308 // A deadkey was pressed that cannot be printed
309 // or a accent command was typed in the minibuffer
311 if (active_->isAccentDefined(accent, i) == true) {
312 string const res = trans_fsm_
313 .currentState->deadkey(c, i);
319 if (active_ == &default_ || c == 0) {
323 string res = trans_fsm_.currentState->deadkey(c, i);
326 // Go through the translation
327 translateAndInsert(c, t, cur);