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