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