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