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