]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.C
Add a Buffer::fully_loaded member function, returning true only when
[lyx.git] / src / trans_mgr.C
1 /**
2  * \file trans_mgr.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Matthias Ettrich
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "trans_mgr.h"
15 #include "trans.h"
16 #include "lyxtext.h"
17 #include "debug.h"
18 #include "insets/insetlatexaccent.h"
19 #include "lyxrc.h"
20 #include "support/lstrings.h"
21
22 using lyx::support::split;
23
24 using std::endl;
25 using std::string;
26 using std::pair;
27
28
29 extern string const DoAccent(string const &, tex_accent);
30 extern string const DoAccent(char, tex_accent);
31
32
33 // TransFSMData
34 TransFSMData::TransFSMData()
35 {
36         deadkey_ = deadkey2_ = 0;
37         deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
38 }
39
40
41 // TransState
42 char const TransState::TOKEN_SEP = 4;
43
44
45 // TransInitState
46 TransInitState::TransInitState()
47 {
48         init_state_ = this;
49 }
50
51
52 string const TransInitState::normalkey(char c)
53 {
54         string res;
55         res = c;
56         return res;
57 }
58
59
60 string const TransInitState::deadkey(char c, KmodInfo d)
61 {
62         deadkey_ = c;
63         deadkey_info_ = d;
64         currentState = deadkey_state_;
65         return string();
66 }
67
68
69 // TransDeadkeyState
70 TransDeadkeyState::TransDeadkeyState()
71 {
72         deadkey_state_ = this;
73 }
74
75
76 string const TransDeadkeyState::normalkey(char c)
77 {
78         string res;
79
80         KmodException::iterator it = deadkey_info_.exception_list.begin();
81         KmodException::iterator end = deadkey_info_.exception_list.end();
82
83         for (; it != end; ++it) {
84                 if (it->c == c) {
85                         res = it->data;
86                         break;
87                 }
88         }
89         if (it == end) {
90                 res = DoAccent(c, deadkey_info_.accent);
91         }
92         currentState = init_state_;
93         return res;
94 }
95
96
97 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
98 {
99         string res;
100
101         // Check if the same deadkey was typed twice
102         if (deadkey_ == c) {
103                 res = deadkey_;
104                 deadkey_ = 0;
105                 deadkey_info_.accent = TEX_NOACCENT;
106                 currentState = init_state_;
107                 return res;
108         }
109
110         // Check if it is a combination or an exception
111         KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
112         KmodException::const_iterator end = deadkey_info_.exception_list.end();
113         for (; cit != end; ++cit) {
114                 if (cit->combined == true && cit->accent == d.accent) {
115                         deadkey2_ = c;
116                         deadkey2_info_ = d;
117                         comb_info_ = (*cit);
118                         currentState = combined_state_;
119                         return string();
120                 }
121                 if (cit->c == c) {
122                         res = cit->data;
123                         deadkey_ = 0;
124                         deadkey_info_.accent = TEX_NOACCENT;
125                         currentState = init_state_;
126                         return res;
127                 }
128         }
129
130         // Not a combination or an exception.
131         // Output deadkey1 and keep deadkey2
132
133         if (deadkey_!= 0)
134                 res = deadkey_;
135         deadkey_ = c;
136         deadkey_info_ = d;
137         currentState = deadkey_state_;
138         return res;
139 }
140
141
142 TransCombinedState::TransCombinedState()
143 {
144         combined_state_ = this;
145 }
146
147
148 string const TransCombinedState::normalkey(char c)
149 {
150         string const temp = DoAccent(c, deadkey2_info_.accent);
151         string const res = DoAccent(temp, deadkey_info_.accent);
152         currentState = init_state_;
153         return res;
154 }
155
156
157 string const TransCombinedState::deadkey(char c, KmodInfo d)
158 {
159         // Third key in a row. Output the first one and
160         // reenter with shifted deadkeys
161         string res;
162         if (deadkey_ != 0)
163                 res = deadkey_;
164         res += TOKEN_SEP;
165         deadkey_ = deadkey2_;
166         deadkey_info_ = deadkey2_info_;
167         res += deadkey_state_->deadkey(c, d);
168         return res;
169 }
170
171
172 // TransFSM
173 TransFSM::TransFSM():
174         TransFSMData(),
175         TransInitState(),
176         TransDeadkeyState(),
177         TransCombinedState()
178 {
179         currentState = init_state_;
180 }
181
182
183 // TransManager
184
185 // Initialize static member.
186 Trans TransManager::default_;
187
188
189 TransManager::TransManager()
190         : active_(0), t1_(new Trans), t2_(new Trans)
191 {}
192
193
194 TransManager::~TransManager()
195 {
196         delete t1_;
197         delete t2_;
198 }
199
200
201 int TransManager::SetPrimary(string const & language)
202 {
203         if (t1_->GetName() == language)
204                 return 0;
205
206         return t1_->Load(language);
207 }
208
209
210 int TransManager::SetSecondary(string const & language)
211 {
212         if (t2_->GetName() == language)
213                 return 0;
214
215         return t2_->Load(language);
216 }
217
218
219 bool TransManager::setCharset(string const & str)
220 {
221         return chset_.loadFile(str);
222 }
223
224
225 void TransManager::EnablePrimary()
226 {
227         if (t1_->IsDefined())
228                 active_ = t1_;
229
230         lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
231 }
232
233
234 void TransManager::EnableSecondary()
235 {
236         if (t2_->IsDefined())
237                 active_ = t2_;
238         lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
239 }
240
241
242 void TransManager::DisableKeymap()
243 {
244         active_ = &default_;
245         lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
246 }
247
248
249 void  TransManager::TranslateAndInsert(char c, LyXText * text)
250 {
251         string res = active_->process(c, *this);
252
253         // Process with tokens
254         string temp;
255
256         while (res.length() > 0) {
257                 res = split(res, temp, TransState::TOKEN_SEP);
258                 insert(temp, text);
259         }
260 }
261
262
263 void TransManager::insertVerbatim(string const & str, LyXText * text)
264 {
265         string::size_type const l = str.length();
266
267         for (string::size_type i = 0; i < l; ++i) {
268                 text->insertChar(str[i]);
269         }
270 }
271
272
273 void TransManager::insert(string const & str, LyXText * text)
274 {
275         // Go through the character encoding only if the current
276         // encoding (chset_->name()) matches the current font_norm
277         // (lyrxc->font_norm)
278
279         // Is false to speak about "only if" the current encoding will
280         // almost always be equal to font_norm.
281         pair<bool, int> enc = chset_.encodeString(str);
282         if (chset_.getName() != lyxrc.font_norm ||
283             !enc.first) {
284                 // Could not find an encoding
285                 InsetLatexAccent ins(str);
286                 if (ins.canDisplay()) {
287                         text->insertInset(
288                                           new InsetLatexAccent(ins));
289                 } else {
290                         insertVerbatim(str, text);
291                 }
292                 return;
293         }
294         string tmp;
295         tmp += static_cast<char>(enc.second);
296         insertVerbatim(tmp, text);
297 }
298
299
300 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
301 {
302         if (c == 0 && active_ != &default_) {
303                 // A deadkey was pressed that cannot be printed
304                 // or a accent command was typed in the minibuffer
305                 KmodInfo i;
306                 if (active_->isAccentDefined(accent, i) == true) {
307                         string const res = trans_fsm_
308                                 .currentState->deadkey(c, i);
309                         insert(res, t);
310                         return;
311                 }
312         }
313
314         if (active_ == &default_ || c == 0) {
315                 KmodInfo i;
316                 i.accent = accent;
317                 i.data.erase();
318                 string res = trans_fsm_.currentState->deadkey(c, i);
319                 insert(res, t);
320         } else {
321                 // Go through the translation
322                 TranslateAndInsert(c, t);
323         }
324 }