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