]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.C
6a9abf437d61483719ee44e122dd95c6593a87e7
[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 namespace lyx::support;
23
24 using std::endl;
25 using std::pair;
26
27 extern string const DoAccent(string const &, tex_accent);
28 extern string const DoAccent(char, tex_accent);
29
30
31 // TransFSMData
32 TransFSMData::TransFSMData()
33 {
34         deadkey_ = deadkey2_ = 0;
35         deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
36 }
37
38
39 // TransState
40 char const TransState::TOKEN_SEP = 4;
41
42
43 // TransInitState
44 TransInitState::TransInitState()
45 {
46         init_state_ = this;
47 }
48
49
50 string const TransInitState::normalkey(char c)
51 {
52         string res;
53         res = c;
54         return res;
55 }
56
57
58 string const TransInitState::deadkey(char c, KmodInfo d)
59 {
60         deadkey_ = c;
61         deadkey_info_ = d;
62         currentState = deadkey_state_;
63         return string();
64 }
65
66
67 // TransDeadkeyState
68 TransDeadkeyState::TransDeadkeyState()
69 {
70         deadkey_state_ = this;
71 }
72
73
74 string const TransDeadkeyState::normalkey(char c)
75 {
76         string res;
77
78         KmodException::iterator it = deadkey_info_.exception_list.begin();
79         KmodException::iterator end = deadkey_info_.exception_list.end();
80
81         for (; it != end; ++it) {
82                 if (it->c == c) {
83                         res = it->data;
84                         break;
85                 }
86         }
87         if (it == end) {
88                 res = DoAccent(c, deadkey_info_.accent);
89         }
90         currentState = init_state_;
91         return res;
92 }
93
94
95 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
96 {
97         string res;
98
99         // Check if the same deadkey was typed twice
100         if (deadkey_ == c) {
101                 res = deadkey_;
102                 deadkey_ = 0;
103                 deadkey_info_.accent = TEX_NOACCENT;
104                 currentState = init_state_;
105                 return res;
106         }
107
108         // Check if it is a combination or an exception
109         KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
110         KmodException::const_iterator end = deadkey_info_.exception_list.end();
111         for (; cit != end; ++cit) {
112                 if (cit->combined == true && cit->accent == d.accent) {
113                         deadkey2_ = c;
114                         deadkey2_info_ = d;
115                         comb_info_ = (*cit);
116                         currentState = combined_state_;
117                         return string();
118                 }
119                 if (cit->c == c) {
120                         res = cit->data;
121                         deadkey_ = 0;
122                         deadkey_info_.accent = TEX_NOACCENT;
123                         currentState = init_state_;
124                         return res;
125                 }
126         }
127
128         // Not a combination or an exception.
129         // Output deadkey1 and keep deadkey2
130
131         if (deadkey_!= 0)
132                 res = deadkey_;
133         deadkey_ = c;
134         deadkey_info_ = d;
135         currentState = deadkey_state_;
136         return res;
137 }
138
139
140 TransCombinedState::TransCombinedState()
141 {
142         combined_state_ = this;
143 }
144
145
146 string const TransCombinedState::normalkey(char c)
147 {
148         string const temp = DoAccent(c, deadkey2_info_.accent);
149         string const res = DoAccent(temp, deadkey_info_.accent);
150         currentState = init_state_;
151         return res;
152 }
153
154
155 string const TransCombinedState::deadkey(char c, KmodInfo d)
156 {
157         // Third key in a row. Output the first one and
158         // reenter with shifted deadkeys
159         string res;
160         if (deadkey_ != 0)
161                 res = deadkey_;
162         res += TOKEN_SEP;
163         deadkey_ = deadkey2_;
164         deadkey_info_ = deadkey2_info_;
165         res += deadkey_state_->deadkey(c, d);
166         return res;
167 }
168
169
170 // TransFSM
171 TransFSM::TransFSM():
172         TransFSMData(),
173         TransInitState(),
174         TransDeadkeyState(),
175         TransCombinedState()
176 {
177         currentState = init_state_;
178 }
179
180
181 // TransManager
182
183 // Initialize static member.
184 Trans TransManager::default_;
185
186
187 TransManager::TransManager()
188         : active_(0), t1_(new Trans), t2_(new Trans)
189 {}
190
191
192 TransManager::~TransManager()
193 {
194         delete t1_;
195         delete t2_;
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_;
227
228         lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
229 }
230
231
232 void TransManager::EnableSecondary()
233 {
234         if (t2_->IsDefined())
235                 active_ = t2_;
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 }