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