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