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"
38 extern string const DoAccent(string const &, tex_accent);
39 extern string const DoAccent(char, tex_accent);
43 TransFSMData::TransFSMData()
45 deadkey_ = deadkey2_ = 0;
46 deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
51 char const TransState::TOKEN_SEP = 4;
55 TransInitState::TransInitState()
61 string const TransInitState::normalkey(char c)
69 string const TransInitState::deadkey(char c, KmodInfo d)
73 currentState = deadkey_state_;
79 TransDeadkeyState::TransDeadkeyState()
81 deadkey_state_ = this;
85 string const TransDeadkeyState::normalkey(char c)
89 KmodException::iterator it = deadkey_info_.exception_list.begin();
90 KmodException::iterator end = deadkey_info_.exception_list.end();
92 for (; it != end; ++it) {
99 res = DoAccent(c, deadkey_info_.accent);
101 currentState = init_state_;
106 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
110 // Check if the same deadkey was typed twice
114 deadkey_info_.accent = TEX_NOACCENT;
115 currentState = init_state_;
119 // Check if it is a combination or an exception
120 KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
121 KmodException::const_iterator end = deadkey_info_.exception_list.end();
122 for (; cit != end; ++cit) {
123 if (cit->combined == true && cit->accent == d.accent) {
127 currentState = combined_state_;
133 deadkey_info_.accent = TEX_NOACCENT;
134 currentState = init_state_;
139 // Not a combination or an exception.
140 // Output deadkey1 and keep deadkey2
146 currentState = deadkey_state_;
151 TransCombinedState::TransCombinedState()
153 combined_state_ = this;
157 string const TransCombinedState::normalkey(char c)
159 string const temp = DoAccent(c, deadkey2_info_.accent);
160 string const res = DoAccent(temp, deadkey_info_.accent);
161 currentState = init_state_;
166 string const TransCombinedState::deadkey(char c, KmodInfo d)
168 // Third key in a row. Output the first one and
169 // reenter with shifted deadkeys
174 deadkey_ = deadkey2_;
175 deadkey_info_ = deadkey2_info_;
176 res += deadkey_state_->deadkey(c, d);
182 TransFSM::TransFSM():
188 currentState = init_state_;
194 // Initialize static member.
195 Trans TransManager::default_;
198 TransManager::TransManager()
199 : active_(0), t1_(new Trans), t2_(new Trans)
203 // For the sake of boost::scoped_ptr.
204 TransManager::~TransManager()
208 int TransManager::setPrimary(string const & language)
210 if (t1_->getName() == language)
213 return t1_->load(language);
217 int TransManager::setSecondary(string const & language)
219 if (t2_->getName() == language)
222 return t2_->load(language);
226 bool TransManager::setCharset(string const & str)
228 return chset_.loadFile(str);
232 void TransManager::enablePrimary()
234 if (t1_->isDefined())
237 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
241 void TransManager::enableSecondary()
243 if (t2_->isDefined())
245 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
249 void TransManager::disableKeymap()
252 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
256 void TransManager::translateAndInsert(char c, LyXText * text)
258 string res = active_->process(c, *this);
260 // Process with tokens
263 while (res.length() > 0) {
264 res = split(res, temp, TransState::TOKEN_SEP);
270 void TransManager::insertVerbatim(string const & str, LyXText * text)
272 for (string::size_type i = 0, n = str.size(); i < n; ++i)
273 text->insertChar(text->bv()->cursor(), str[i]);
277 void TransManager::insert(string const & str, LyXText * text)
279 // Go through the character encoding only if the current
280 // encoding (chset_->name()) matches the current font_norm
281 // (lyrxc->font_norm)
283 // Is false to speak about "only if" the current encoding will
284 // almost always be equal to font_norm.
285 pair<bool, int> enc = chset_.encodeString(str);
286 if (chset_.getName() != lyxrc.font_norm ||
288 // Could not find an encoding
289 InsetLatexAccent ins(str);
290 if (ins.canDisplay()) {
291 LCursor & cur = text->bv()->cursor();
292 cap::replaceSelection(cur);
293 cur.insert(new InsetLatexAccent(ins));
296 insertVerbatim(str, text);
300 string const tmp(1, static_cast<char>(enc.second));
301 insertVerbatim(tmp, text);
305 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
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);