]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.C
get rid of dead code, some new functions constify variables.
[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         comb_info_ = 0;
33 }
34
35
36 // TransState
37 char const TransState::TOKEN_SEP = 4;
38
39
40 // TransInitState
41 TransInitState::TransInitState()
42 {
43         init_state_ = this;
44 }
45
46
47 string const TransInitState::normalkey(char c, string const & t)
48 {
49         string res;
50         if (!t.empty()) res = t;
51         else res = c;
52         
53         return res;
54 }
55
56
57 string const TransInitState::deadkey(char c, KmodInfo d)
58 {
59         deadkey_ = c;
60         deadkey_info_ = d;
61         currentState = deadkey_state_;
62         return string();
63 }
64
65
66 // TransDeadkeyState
67 TransDeadkeyState::TransDeadkeyState()
68 {
69         deadkey_state_ = this;
70 }
71
72
73 string const TransDeadkeyState::normalkey(char c, string const & trans)
74 {
75         string res;
76         
77         // Check if it is an exception
78         KmodException l = deadkey_info_.exception_list;
79         while(l != 0) {
80                 if (l->c == c) {
81                         res = l->data;
82                         break;
83                 }
84                 l = l->next;
85         }
86         if (l == 0) {
87                 // Not an exception. Check if it allowed
88                 if (countChar(deadkey_info_.allowed, c) > 0) {
89                         res = DoAccent(c, deadkey_info_.accent);
90                 } else {
91                         // Not allowed
92                         if (deadkey_!= 0)
93                                 res = deadkey_;
94                         res+= TOKEN_SEP;
95                         res+= trans;
96                 }
97         }
98         currentState = init_state_;
99         return res;
100 }
101
102
103 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
104 {
105         string res;
106         
107         // Check if the same deadkey was typed twice
108         if (deadkey_ == c) {
109                 res = deadkey_;
110                 deadkey_ = 0;
111                 deadkey_info_.accent = TEX_NOACCENT;
112                 currentState = init_state_;
113                 return res;
114         }
115         
116         // Check if it is a combination or an exception
117         KmodException l;
118         l = deadkey_info_.exception_list;
119         
120         while(l) {
121                 if (l->combined == true && l->accent == d.accent) {
122                         deadkey2_ = c;
123                         deadkey2_info_ = d;
124                         comb_info_ = l;
125                         currentState = combined_state_;
126                         return string();
127                 }
128                 if (l->c == c) {
129                         res = l->data;
130                         deadkey_ = 0;
131                         deadkey_info_.accent = TEX_NOACCENT;
132                         currentState = init_state_;
133                         return res;
134                 }
135                 l = l->next;
136         }
137         
138         // Not a combination or an exception. 
139         // Output deadkey1 and keep deadkey2
140         
141         if (deadkey_!= 0)
142                 res = deadkey_;
143         deadkey_ = c;
144         deadkey_info_ = d;
145         currentState = deadkey_state_;
146         return res;
147 }
148
149
150 TransCombinedState::TransCombinedState()
151 {
152         combined_state_ = this;
153 }
154
155
156 string const TransCombinedState::normalkey(char c, string const & trans)
157 {
158         string res;
159         
160         // Check if the key is allowed on the combination
161         if (countChar(comb_info_->data, c) > 0) {
162                 string temp;
163                 temp = DoAccent(c, deadkey2_info_.accent);
164                 res = DoAccent(temp, deadkey_info_.accent);
165                 currentState = init_state_;
166         } else {
167                 // Not allowed. Output deadkey1 and check deadkey2 + c
168                 if (deadkey_ != 0)
169                         res += deadkey_;
170                 res += TOKEN_SEP;
171                 deadkey_ = deadkey2_;
172                 deadkey_info_ = deadkey2_info_;
173                 // Call deadkey state and leave it to setup the FSM
174                 res += deadkey_state_->normalkey(c, trans);
175         }
176         return res;
177 }
178
179
180 string const TransCombinedState::deadkey(char c, KmodInfo d)
181 {
182         // Third key in a row. Output the first one and
183         // reenter with shifted deadkeys
184         string res;
185         if (deadkey_ != 0)
186                 res = deadkey_;
187         res += TOKEN_SEP;
188         deadkey_ = deadkey2_;
189         deadkey_info_ = deadkey2_info_;
190         res += deadkey_state_->deadkey(c, d);
191         return res;
192 }
193
194
195 // TransFSM
196 TransFSM::TransFSM():
197         TransFSMData(),
198         TransInitState(),
199         TransDeadkeyState(),
200         TransCombinedState()
201 {
202         currentState = init_state_;
203 }
204
205
206 // TransManager
207
208 TransManager::TransManager()
209         : active_(0), t1_(new Trans), t2_(new Trans)
210 {}
211
212
213 Trans * TransManager::default_ = new Trans;
214
215
216 TransManager::~TransManager() 
217
218         delete t1_;
219         delete t2_;
220 }
221
222
223 int TransManager::SetPrimary(string const & language)
224 {
225         if (t1_->GetName() == language) 
226                 return 0;
227         
228         return t1_->Load(language);
229 }
230
231
232 int TransManager::SetSecondary(string const & language)
233 {
234         if (t2_->GetName() == language)
235                 return 0;
236         
237         return t2_->Load(language);
238 }
239
240
241 bool TransManager::setCharset(string const & str)
242 {
243         return chset_.loadFile(str);
244 }
245
246
247 void TransManager::EnablePrimary()
248 {
249         if (t1_->IsDefined())
250                 active_ = t1_;
251         
252         lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
253 }
254
255
256 void TransManager::EnableSecondary()
257 {
258         if (t2_->IsDefined())
259                 active_ = t2_;
260         lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
261 }
262
263
264 void TransManager::DisableKeymap()
265 {
266         active_ = default_;
267         lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
268 }
269
270
271 void  TransManager::TranslateAndInsert(char c, LyXText * text)
272 {
273         string res = active_->process(c, *this);
274         
275         // Process with tokens
276         string temp;
277         
278         while(res.length() > 0) {
279                 res = split(res, temp, TransState::TOKEN_SEP);
280                 insert(temp, text);
281         }
282 }
283
284
285 void TransManager::insertVerbatim(string const & str, LyXText * text)
286 {       
287         int const l = str.length();
288         
289         for (int i = 0; i < l; ++i){
290                 if (str[i] == '\"' 
291                     && text->GetFont(current_view->buffer(),text->cursor.par(),
292                                      text->cursor.pos()).latex() == LyXFont::OFF
293                     && text->GetFont(current_view->buffer(),text->cursor.par(),
294                                      text->cursor.pos()).language()->lang() != "hebrew")
295                         current_view->insertCorrectQuote();
296                 else
297                         text->InsertChar(current_view, str[i]);
298         }
299 }
300
301
302 void TransManager::insert(string const & str, LyXText * text)
303 {
304         // Go through the character encoding only if the current 
305         // encoding (chset_->name()) matches the current font_norm
306         // (lyrxc->font_norm
307         
308         // Is false to speak about "only if" the current encoding will
309         // almost always be equal to font_norm.
310         pair<bool, int> enc = chset_.encodeString(str);
311         if (chset_.getName() != lyxrc.font_norm || 
312             !enc.first) {
313                 // Could not find an encoding
314                 InsetLatexAccent ins(str);
315                 if (ins.CanDisplay()) {
316                         text->InsertInset(current_view, new InsetLatexAccent(ins));
317                 } else {
318                         insertVerbatim(str, text);
319                 }
320                 return;
321         }
322         string tmp; tmp += static_cast<char>(enc.second);
323         insertVerbatim(tmp, text);
324 }
325
326
327 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
328 {
329         if (c == 0 && active_ != default_) {
330                 // A deadkey was pressed that cannot be printed
331                 // or a accent command was typed in the minibuffer
332                 KmodInfo i;
333                 if (active_->isAccentDefined(accent, i) == true) {
334                         string res = trans_fsm_.currentState->deadkey(c, i);
335                         insert(res, t);
336                         return;
337                 }
338         }
339         
340         if (active_ == default_ || c == 0) {
341                 KmodInfo i;
342                 i.accent = accent;
343                 i.allowed = lyx_accent_table[accent].native;
344                 i.data.erase();
345                 i.exception_list = 0;
346                 
347                 string res = trans_fsm_.currentState->deadkey(c, i);
348                 insert(res, t);
349         } else {
350                 // Go through the translation
351                 TranslateAndInsert(c, t);
352         }
353 }