]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.C
LyX Drinkers Union: patch 1
[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 const 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 // Initialize static member.
209 Trans TransManager::default_;
210
211
212 TransManager::TransManager()
213         : active_(0), t1_(new Trans), t2_(new Trans)
214 {}
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(string const & str)
243 {
244         return chset_.loadFile(str);
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         string::size_type const l = str.length();
289         
290         for (string::size_type 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 const res = trans_fsm_
336                                 .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 }