]> git.lyx.org Git - lyx.git/blob - src/Trans.h
error is always true at this point
[lyx.git] / src / Trans.h
1 // -*- C++ -*-
2 /**
3  * \file Trans.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Matthias Ettrich
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TRANS_H
15 #define TRANS_H
16
17 #include "FuncCode.h"
18
19 #include "support/docstring.h"
20
21 #include <list>
22 #include <map>
23
24
25 namespace lyx {
26
27 class Cursor;
28 class Text;
29 class Lexer;
30 class TransManager;
31
32 ///
33 enum tex_accent {
34         ///
35         TEX_NOACCENT = 0,
36         ///
37         TEX_ACUTE,
38         ///
39         TEX_GRAVE,
40         ///
41         TEX_MACRON,
42         ///
43         TEX_TILDE,
44         ///
45         TEX_PERISPOMENI,
46         ///
47         TEX_UNDERBAR,
48         ///
49         TEX_CEDILLA,
50         ///
51         TEX_UNDERDOT,
52         ///
53         TEX_CIRCUMFLEX,
54         ///
55         TEX_CIRCLE,
56         ///
57         TEX_TIE,
58         ///
59         TEX_BREVE,
60         ///
61         TEX_CARON,
62 //  TEX_SPECIAL_CARON,
63         ///
64         TEX_HUNGUML,
65         ///
66         TEX_UMLAUT,
67         ///
68         TEX_DOT,
69         ///
70         TEX_OGONEK,
71         ///
72         TEX_MAX_ACCENT = TEX_OGONEK
73 };
74
75
76 struct TeXAccent {
77         ///
78         tex_accent accent;
79         /// UCS4 code point of this accent
80         char_type ucs4;
81         ///
82         char const * name;
83         ///
84         FuncCode action;
85 };
86
87 ///
88 extern TeXAccent get_accent(FuncCode action);
89
90
91 ///
92 struct Keyexc {
93         /// character to make exception
94         char_type c;
95         /// exception data
96         docstring data;
97         /// Combination with another deadkey
98         bool combined;
99         /// The accent comined with
100         tex_accent accent;
101 };
102
103 ///
104 typedef std::list<Keyexc> KmodException;
105
106 ///
107 class KmodInfo {
108 public:
109         ///
110         docstring data;
111         ///
112         tex_accent accent;
113         ///
114         KmodException exception_list;
115 };
116
117
118 /////////////////////////////////////////////////////////////////////
119 //
120 // Trans: holds a .kmap file
121 //
122 /////////////////////////////////////////////////////////////////////
123
124 class Trans {
125 public:
126         ///
127         Trans() {}
128         ///
129         ~Trans() { freeKeymap(); }
130
131         ///
132         int load(std::string const & language);
133         ///
134         bool isDefined() const;
135         ///
136         std::string const & getName() const { return name_; }
137         ///
138         docstring const process(char_type, TransManager &);
139         ///
140         bool isAccentDefined(tex_accent, KmodInfo &) const;
141
142 private:
143         ///
144         void addDeadkey(tex_accent, docstring const &);
145         ///
146         void freeKeymap();
147         ///
148         int load(Lexer &);
149         ///
150         docstring const & match(char_type c);
151         ///
152         void insertException(KmodException & exclist, char_type c,
153                              docstring const & data, bool = false,
154                              tex_accent = TEX_NOACCENT);
155         ///
156         void freeException(KmodException & exclist);
157
158         ///
159         std::string name_;
160         ///
161         std::map<char_type, docstring> keymap_;
162         ///
163         std::map<tex_accent, KmodInfo> kmod_list_;
164 };
165
166
167 ///
168 inline docstring const & Trans::match(char_type c)
169 {
170         std::map<char_type, docstring>::iterator it = keymap_.find(c);
171         if (it != keymap_.end()) {
172                 return it->second;
173         }
174         static docstring dummy;
175         return dummy;
176 }
177
178
179 /////////////////////////////////////////////////////////////////////
180 //
181 // TransState
182 //
183 /////////////////////////////////////////////////////////////////////
184
185 /// Translation state
186 class TransState {
187 public:
188         ///
189         virtual ~TransState() {}
190         ///
191         virtual docstring const normalkey(char_type) = 0;
192         ///
193         virtual bool backspace() = 0;
194         ///
195         virtual docstring const deadkey(char_type, KmodInfo) = 0;
196         ///
197         static char_type const TOKEN_SEP;
198 };
199
200
201 /// Translation FSM
202 class TransFSMData {
203 protected:
204         ///
205         virtual ~TransFSMData() {}
206         ///
207         char_type deadkey_;
208         ///
209         KmodInfo deadkey_info_;
210         ///
211         char_type deadkey2_;
212         ///
213         KmodInfo deadkey2_info_;
214         ///
215         Keyexc comb_info_;
216         ///
217         TransState * init_state_;
218         ///
219         TransState * deadkey_state_;
220         ///
221         TransState * combined_state_;
222         ///
223 public:
224         ///
225         TransFSMData();
226         ///
227         TransState * currentState;
228 };
229
230
231 /// Init State
232 class TransInitState : virtual public TransFSMData, public TransState {
233 public:
234         ///
235         TransInitState();
236         ///
237         virtual docstring const normalkey(char_type);
238         ///
239         virtual bool backspace() { return true; }
240         ///
241         virtual docstring const deadkey(char_type, KmodInfo);
242 };
243
244
245 /// Deadkey State
246 class TransDeadkeyState : virtual public TransFSMData, public TransState {
247 public:
248         ///
249         TransDeadkeyState();
250         ///
251         virtual docstring const normalkey(char_type);
252         ///
253         virtual bool backspace() {
254                 currentState = init_state_;
255                 return false;
256         }
257         ///
258         virtual docstring const deadkey(char_type, KmodInfo);
259 };
260
261
262 /// Combined State
263 class TransCombinedState : virtual public TransFSMData, public TransState {
264 public:
265         ///
266         TransCombinedState();
267         ///
268         virtual docstring const normalkey(char_type);
269         ///
270         virtual bool backspace() {
271                 // cancel the second deadkey
272                 deadkey2_ = 0;
273                 deadkey2_info_.accent = TEX_NOACCENT;
274                 currentState = deadkey_state_;
275
276                 return false;
277         }
278         ///
279         virtual docstring const deadkey(char_type, KmodInfo);
280 };
281
282
283 ///
284 class TransFSM : virtual public TransFSMData,
285                  public TransInitState,
286                  public TransDeadkeyState,
287                  public TransCombinedState {
288 public:
289         ///
290         TransFSM();
291 };
292
293
294
295 /////////////////////////////////////////////////////////////////////
296 //
297 // TransManager
298 //
299 /////////////////////////////////////////////////////////////////////
300
301 class TransManager {
302 private:
303         ///
304         TransFSM trans_fsm_;
305         ///
306         Trans * active_;
307         ///
308         Trans t1_;
309         ///
310         Trans t2_;
311         ///
312         static Trans default_;
313         ///
314         void insert(docstring const &, Text *, Cursor & cur);
315 public:
316         ///
317         TransManager();
318         ///
319         int setPrimary(std::string const &);
320         ///
321         int setSecondary(std::string const &);
322         ///
323         void enablePrimary();
324         ///
325         void enableSecondary();
326         ///
327         void disableKeymap();
328         ///
329         bool backspace() { return trans_fsm_.currentState->backspace(); }
330         ///
331         void translateAndInsert(char_type, Text *, Cursor &);
332         ///
333         docstring const deadkey(char_type c, KmodInfo t)
334                 { return trans_fsm_.currentState->deadkey(c, t); }
335         ///
336         docstring const normalkey(char_type c)
337                 { return trans_fsm_.currentState->normalkey(c); }
338         ///
339         void deadkey(char_type, tex_accent, Text *, Cursor &);
340 };
341
342 } // namespace lyx
343
344 #endif // TRANS_H