]> git.lyx.org Git - lyx.git/blob - src/Trans.h
c200bb9544b4c7714eaf7a04f3569ddbdbe75095
[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         ///
94         Keyexc() : c('\0'), combined(false), accent(TEX_NOACCENT) {}
95         /// character to make exception
96         char_type c;
97         /// exception data
98         docstring data;
99         /// Combination with another deadkey
100         bool combined;
101         /// The accent comined with
102         tex_accent accent;
103 };
104
105 ///
106 typedef std::list<Keyexc> KmodException;
107
108 ///
109 class KmodInfo {
110 public:
111         ///
112         KmodInfo() : accent(TEX_NOACCENT) {}
113         ///
114         docstring data;
115         ///
116         tex_accent accent;
117         ///
118         KmodException exception_list;
119 };
120
121
122 /////////////////////////////////////////////////////////////////////
123 //
124 // Trans: holds a .kmap file
125 //
126 /////////////////////////////////////////////////////////////////////
127
128 class Trans {
129 public:
130         ///
131         Trans() {}
132         ///
133         ~Trans() { freeKeymap(); }
134
135         ///
136         int load(std::string const & language);
137         ///
138         bool isDefined() const;
139         ///
140         std::string const & getName() const { return name_; }
141         ///
142         docstring const process(char_type, TransManager &);
143         ///
144         bool isAccentDefined(tex_accent, KmodInfo &) const;
145
146 private:
147         ///
148         void addDeadkey(tex_accent, docstring const &);
149         ///
150         void freeKeymap();
151         ///
152         int load(Lexer &);
153         ///
154         docstring const & match(char_type c);
155         ///
156         void insertException(KmodException & exclist, char_type c,
157                              docstring const & data, bool = false,
158                              tex_accent = TEX_NOACCENT);
159         ///
160         void freeException(KmodException & exclist);
161
162         ///
163         std::string name_;
164         ///
165         std::map<char_type, docstring> keymap_;
166         ///
167         std::map<tex_accent, KmodInfo> kmod_list_;
168 };
169
170
171 ///
172 inline docstring const & Trans::match(char_type c)
173 {
174         std::map<char_type, docstring>::iterator it = keymap_.find(c);
175         if (it != keymap_.end()) {
176                 return it->second;
177         }
178         static docstring dummy;
179         return dummy;
180 }
181
182
183 /////////////////////////////////////////////////////////////////////
184 //
185 // TransState
186 //
187 /////////////////////////////////////////////////////////////////////
188
189 /// Translation state
190 class TransState {
191 public:
192         ///
193         virtual ~TransState() {}
194         ///
195         virtual docstring const normalkey(char_type) = 0;
196         ///
197         virtual bool backspace() = 0;
198         ///
199         virtual docstring const deadkey(char_type, KmodInfo) = 0;
200         ///
201         static char_type const TOKEN_SEP;
202 };
203
204
205 /// Translation FSM
206 class TransFSMData {
207 protected:
208         ///
209         virtual ~TransFSMData() {}
210         ///
211         char_type deadkey_;
212         ///
213         KmodInfo deadkey_info_;
214         ///
215         char_type deadkey2_;
216         ///
217         KmodInfo deadkey2_info_;
218         ///
219         Keyexc comb_info_;
220         ///
221         TransState * init_state_;
222         ///
223         TransState * deadkey_state_;
224         ///
225         TransState * combined_state_;
226         ///
227 public:
228         ///
229         TransFSMData();
230         ///
231         TransState * currentState;
232 };
233
234
235 /// Init State
236 class TransInitState : virtual public TransFSMData, public TransState {
237 public:
238         ///
239         TransInitState();
240         ///
241         docstring const normalkey(char_type) override;
242         ///
243         bool backspace() override { return true; }
244         ///
245         docstring const deadkey(char_type, KmodInfo) override;
246 };
247
248
249 /// Deadkey State
250 class TransDeadkeyState : virtual public TransFSMData, public TransState {
251 public:
252         ///
253         TransDeadkeyState();
254         ///
255         docstring const normalkey(char_type) override;
256         ///
257         bool backspace() override {
258                 currentState = init_state_;
259                 return false;
260         }
261         ///
262         docstring const deadkey(char_type, KmodInfo) override;
263 };
264
265
266 /// Combined State
267 class TransCombinedState : virtual public TransFSMData, public TransState {
268 public:
269         ///
270         TransCombinedState();
271         ///
272         docstring const normalkey(char_type) override;
273         ///
274         bool backspace() override {
275                 // cancel the second deadkey
276                 deadkey2_ = 0;
277                 deadkey2_info_.accent = TEX_NOACCENT;
278                 currentState = deadkey_state_;
279
280                 return false;
281         }
282         ///
283         docstring const deadkey(char_type, KmodInfo) override;
284 };
285
286
287 ///
288 class TransFSM : virtual public TransFSMData,
289                  public TransInitState,
290                  public TransDeadkeyState,
291                  public TransCombinedState {
292 public:
293         ///
294         TransFSM();
295 };
296
297
298
299 /////////////////////////////////////////////////////////////////////
300 //
301 // TransManager
302 //
303 /////////////////////////////////////////////////////////////////////
304
305 class TransManager {
306 private:
307         ///
308         TransFSM trans_fsm_;
309         ///
310         Trans * active_;
311         ///
312         Trans t1_;
313         ///
314         Trans t2_;
315         ///
316         static Trans default_;
317         ///
318         void insert(docstring const &, Text *, Cursor & cur);
319 public:
320         ///
321         TransManager();
322         ///
323         int setPrimary(std::string const &);
324         ///
325         int setSecondary(std::string const &);
326         ///
327         void enablePrimary();
328         ///
329         void enableSecondary();
330         ///
331         void disableKeymap();
332         ///
333         bool backspace() { return trans_fsm_.currentState->backspace(); }
334         ///
335         void translateAndInsert(char_type, Text *, Cursor &);
336         ///
337         docstring const deadkey(char_type c, KmodInfo t)
338                 { return trans_fsm_.currentState->deadkey(c, t); }
339         ///
340         docstring const normalkey(char_type c)
341                 { return trans_fsm_.currentState->normalkey(c); }
342         ///
343         void deadkey(char_type, tex_accent, Text *, Cursor &);
344 };
345
346 } // namespace lyx
347
348 #endif // TRANS_H