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