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