]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.C
more cursor dispatch
[lyx.git] / src / trans_mgr.C
1 /**
2  * \file trans_mgr.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Matthias Ettrich
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "trans_mgr.h"
15 #include "trans.h"
16 #include "lyxtext.h"
17 #include "debug.h"
18 #include "insets/insetlatexaccent.h"
19 #include "lyxrc.h"
20 #include "support/lstrings.h"
21
22 using lyx::support::split;
23
24 using std::endl;
25 using std::string;
26 using std::pair;
27
28
29 extern string const DoAccent(string const &, tex_accent);
30 extern string const DoAccent(char, tex_accent);
31
32
33 // TransFSMData
34 TransFSMData::TransFSMData()
35 {
36         deadkey_ = deadkey2_ = 0;
37         deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
38 }
39
40
41 // TransState
42 char const TransState::TOKEN_SEP = 4;
43
44
45 // TransInitState
46 TransInitState::TransInitState()
47 {
48         init_state_ = this;
49 }
50
51
52 string const TransInitState::normalkey(char c)
53 {
54         string res;
55         res = c;
56         return res;
57 }
58
59
60 string const TransInitState::deadkey(char c, KmodInfo d)
61 {
62         deadkey_ = c;
63         deadkey_info_ = d;
64         currentState = deadkey_state_;
65         return string();
66 }
67
68
69 // TransDeadkeyState
70 TransDeadkeyState::TransDeadkeyState()
71 {
72         deadkey_state_ = this;
73 }
74
75
76 string const TransDeadkeyState::normalkey(char c)
77 {
78         string res;
79
80         KmodException::iterator it = deadkey_info_.exception_list.begin();
81         KmodException::iterator end = deadkey_info_.exception_list.end();
82
83         for (; it != end; ++it) {
84                 if (it->c == c) {
85                         res = it->data;
86                         break;
87                 }
88         }
89         if (it == end) {
90                 res = DoAccent(c, deadkey_info_.accent);
91         }
92         currentState = init_state_;
93         return res;
94 }
95
96
97 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
98 {
99         string res;
100
101         // Check if the same deadkey was typed twice
102         if (deadkey_ == c) {
103                 res = deadkey_;
104                 deadkey_ = 0;
105                 deadkey_info_.accent = TEX_NOACCENT;
106                 currentState = init_state_;
107                 return res;
108         }
109
110         // Check if it is a combination or an exception
111         KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
112         KmodException::const_iterator end = deadkey_info_.exception_list.end();
113         for (; cit != end; ++cit) {
114                 if (cit->combined == true && cit->accent == d.accent) {
115                         deadkey2_ = c;
116                         deadkey2_info_ = d;
117                         comb_info_ = (*cit);
118                         currentState = combined_state_;
119                         return string();
120                 }
121                 if (cit->c == c) {
122                         res = cit->data;
123                         deadkey_ = 0;
124                         deadkey_info_.accent = TEX_NOACCENT;
125                         currentState = init_state_;
126                         return res;
127                 }
128         }
129
130         // Not a combination or an exception.
131         // Output deadkey1 and keep deadkey2
132
133         if (deadkey_!= 0)
134                 res = deadkey_;
135         deadkey_ = c;
136         deadkey_info_ = d;
137         currentState = deadkey_state_;
138         return res;
139 }
140
141
142 TransCombinedState::TransCombinedState()
143 {
144         combined_state_ = this;
145 }
146
147
148 string const TransCombinedState::normalkey(char c)
149 {
150         string const temp = DoAccent(c, deadkey2_info_.accent);
151         string const res = DoAccent(temp, deadkey_info_.accent);
152         currentState = init_state_;
153         return res;
154 }
155
156
157 string const TransCombinedState::deadkey(char c, KmodInfo d)
158 {
159         // Third key in a row. Output the first one and
160         // reenter with shifted deadkeys
161         string res;
162         if (deadkey_ != 0)
163                 res = deadkey_;
164         res += TOKEN_SEP;
165         deadkey_ = deadkey2_;
166         deadkey_info_ = deadkey2_info_;
167         res += deadkey_state_->deadkey(c, d);
168         return res;
169 }
170
171
172 // TransFSM
173 TransFSM::TransFSM():
174         TransFSMData(),
175         TransInitState(),
176         TransDeadkeyState(),
177         TransCombinedState()
178 {
179         currentState = init_state_;
180 }
181
182
183 // TransManager
184
185 // Initialize static member.
186 Trans TransManager::default_;
187
188
189 TransManager::TransManager()
190         : active_(0), t1_(new Trans), t2_(new Trans)
191 {}
192
193
194 // For the sake of boost::scoped_ptr.
195 TransManager::~TransManager()
196 {}
197
198
199 int TransManager::SetPrimary(string const & language)
200 {
201         if (t1_->GetName() == language)
202                 return 0;
203
204         return t1_->Load(language);
205 }
206
207
208 int TransManager::SetSecondary(string const & language)
209 {
210         if (t2_->GetName() == language)
211                 return 0;
212
213         return t2_->Load(language);
214 }
215
216
217 bool TransManager::setCharset(string const & str)
218 {
219         return chset_.loadFile(str);
220 }
221
222
223 void TransManager::EnablePrimary()
224 {
225         if (t1_->IsDefined())
226                 active_ = t1_.get();
227
228         lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
229 }
230
231
232 void TransManager::EnableSecondary()
233 {
234         if (t2_->IsDefined())
235                 active_ = t2_.get();
236         lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
237 }
238
239
240 void TransManager::DisableKeymap()
241 {
242         active_ = &default_;
243         lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
244 }
245
246
247 void  TransManager::TranslateAndInsert(char c, LyXText * text)
248 {
249         string res = active_->process(c, *this);
250
251         // Process with tokens
252         string temp;
253
254         while (res.length() > 0) {
255                 res = split(res, temp, TransState::TOKEN_SEP);
256                 insert(temp, text);
257         }
258 }
259
260
261 void TransManager::insertVerbatim(string const & str, LyXText * text)
262 {
263         string::size_type const l = str.length();
264
265         for (string::size_type i = 0; i < l; ++i) {
266                 text->insertChar(str[i]);
267         }
268 }
269
270
271 void TransManager::insert(string const & str, LyXText * text)
272 {
273         // Go through the character encoding only if the current
274         // encoding (chset_->name()) matches the current font_norm
275         // (lyrxc->font_norm)
276
277         // Is false to speak about "only if" the current encoding will
278         // almost always be equal to font_norm.
279         pair<bool, int> enc = chset_.encodeString(str);
280         if (chset_.getName() != lyxrc.font_norm ||
281             !enc.first) {
282                 // Could not find an encoding
283                 InsetLatexAccent ins(str);
284                 if (ins.canDisplay()) {
285                         text->insertInset(
286                                           new InsetLatexAccent(ins));
287                 } else {
288                         insertVerbatim(str, text);
289                 }
290                 return;
291         }
292         string tmp;
293         tmp += static_cast<char>(enc.second);
294         insertVerbatim(tmp, text);
295 }
296
297
298 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
299 {
300         if (c == 0 && active_ != &default_) {
301                 // A deadkey was pressed that cannot be printed
302                 // or a accent command was typed in the minibuffer
303                 KmodInfo i;
304                 if (active_->isAccentDefined(accent, i) == true) {
305                         string const res = trans_fsm_
306                                 .currentState->deadkey(c, i);
307                         insert(res, t);
308                         return;
309                 }
310         }
311
312         if (active_ == &default_ || c == 0) {
313                 KmodInfo i;
314                 i.accent = accent;
315                 i.data.erase();
316                 string res = trans_fsm_.currentState->deadkey(c, i);
317                 insert(res, t);
318         } else {
319                 // Go through the translation
320                 TranslateAndInsert(c, t);
321         }
322 }