]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.C
656e57c6dfdc0485fb2555565edfe8d2afaad9d7
[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 #include "language.h"
19
20 using std::endl;
21 using std::pair;
22
23 extern string const DoAccent(string const &, tex_accent);
24 extern string const DoAccent(char, tex_accent);
25 extern BufferView * current_view;
26
27
28 // TransFSMData
29 TransFSMData::TransFSMData()
30 {
31         deadkey_ = deadkey2_ = 0;
32         deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
33         comb_info_ = 0;
34 }
35
36
37 // TransState
38 char const TransState::TOKEN_SEP = 4;
39
40
41 // TransInitState
42 TransInitState::TransInitState()
43 {
44         init_state_ = this;
45 }
46
47
48 string const TransInitState::normalkey(char c, string const & t)
49 {
50         string res;
51         if (!t.empty()) res = t;
52         else res = c;
53         
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, string const & trans)
75 {
76         string res;
77         
78         // Check if it is an exception
79         KmodException l = deadkey_info_.exception_list;
80         while(l != 0) {
81                 if (l->c == c) {
82                         res = l->data;
83                         break;
84                 }
85                 l = l->next;
86         }
87         if (l == 0) {
88                 // Not an exception. Check if it allowed
89                 if (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 const 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 const TransCombinedState::normalkey(char c, string const & 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 const 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 // Initialize static member.
210 Trans TransManager::default_;
211
212
213 TransManager::TransManager()
214         : active_(0), t1_(new Trans), t2_(new Trans)
215 {}
216
217
218 TransManager::~TransManager() 
219
220         delete t1_;
221         delete t2_;
222 }
223
224
225 int TransManager::SetPrimary(string const & language)
226 {
227         if (t1_->GetName() == language) 
228                 return 0;
229         
230         return t1_->Load(language);
231 }
232
233
234 int TransManager::SetSecondary(string const & language)
235 {
236         if (t2_->GetName() == language)
237                 return 0;
238         
239         return t2_->Load(language);
240 }
241
242
243 bool TransManager::setCharset(string const & str)
244 {
245         return chset_.loadFile(str);
246 }
247
248
249 void TransManager::EnablePrimary()
250 {
251         if (t1_->IsDefined())
252                 active_ = t1_;
253         
254         lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
255 }
256
257
258 void TransManager::EnableSecondary()
259 {
260         if (t2_->IsDefined())
261                 active_ = t2_;
262         lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
263 }
264
265
266 void TransManager::DisableKeymap()
267 {
268         active_ = &default_;
269         lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
270 }
271
272
273 void  TransManager::TranslateAndInsert(char c, LyXText * text)
274 {
275         string res = active_->process(c, *this);
276         
277         // Process with tokens
278         string temp;
279         
280         while(res.length() > 0) {
281                 res = split(res, temp, TransState::TOKEN_SEP);
282                 insert(temp, text);
283         }
284 }
285
286
287 void TransManager::insertVerbatim(string const & str, LyXText * text)
288 {       
289         string::size_type const l = str.length();
290         
291         for (string::size_type i = 0; i < l; ++i) {
292                 if (str[i] == '\"' 
293                     && text->GetFont(current_view->buffer(),text->cursor.par(),
294                                      text->cursor.pos()).latex() == LyXFont::OFF
295                     && text->GetFont(current_view->buffer(),text->cursor.par(),
296                                      text->cursor.pos()).language()->lang() != "hebrew")
297                         current_view->insertCorrectQuote();
298                 else
299                         text->InsertChar(current_view, str[i]);
300         }
301 }
302
303
304 void TransManager::insert(string const & str, LyXText * text)
305 {
306         // Go through the character encoding only if the current 
307         // encoding (chset_->name()) matches the current font_norm
308         // (lyrxc->font_norm
309         
310         // Is false to speak about "only if" the current encoding will
311         // almost always be equal to font_norm.
312         pair<bool, int> enc = chset_.encodeString(str);
313         if (chset_.getName() != lyxrc.font_norm || 
314             !enc.first) {
315                 // Could not find an encoding
316                 InsetLatexAccent ins(str);
317                 if (ins.CanDisplay()) {
318                         text->InsertInset(current_view, new InsetLatexAccent(ins));
319                 } else {
320                         insertVerbatim(str, text);
321                 }
322                 return;
323         }
324         string tmp; tmp += static_cast<char>(enc.second);
325         insertVerbatim(tmp, text);
326 }
327
328
329 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
330 {
331         if (c == 0 && active_ != &default_) {
332                 // A deadkey was pressed that cannot be printed
333                 // or a accent command was typed in the minibuffer
334                 KmodInfo i;
335                 if (active_->isAccentDefined(accent, i) == true) {
336                         string res = trans_fsm_.currentState->deadkey(c, i);
337                         insert(res, t);
338                         return;
339                 }
340         }
341         
342         if (active_ == &default_ || c == 0) {
343                 KmodInfo i;
344                 i.accent = accent;
345                 i.allowed = lyx_accent_table[accent].native;
346                 i.data.erase();
347                 i.exception_list = 0;
348                 
349                 string res = trans_fsm_.currentState->deadkey(c, i);
350                 insert(res, t);
351         } else {
352                 // Go through the translation
353                 TranslateAndInsert(c, t);
354         }
355 }