]> git.lyx.org Git - lyx.git/blob - src/Trans.h
LaTeXFeatures.cpp: "wrapfig" has to be loaded after "float"
[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 "lfuns.h"
18 #include "support/docstring.h"
19
20 #include <list>
21 #include <map>
22
23
24 namespace lyx {
25
26 class Cursor;
27 class Text;
28 class Lexer;
29 class TransManager;
30
31 ///
32 enum tex_accent {
33         ///
34         TEX_NOACCENT = 0,
35         ///
36         TEX_ACUTE,
37         ///
38         TEX_GRAVE,
39         ///
40         TEX_MACRON,
41         ///
42         TEX_TILDE,
43         ///
44         TEX_UNDERBAR,
45         ///
46         TEX_CEDILLA,
47         ///
48         TEX_UNDERDOT,
49         ///
50         TEX_CIRCUMFLEX,
51         ///
52         TEX_CIRCLE,
53         ///
54         TEX_TIE,
55         ///
56         TEX_BREVE,
57         ///
58         TEX_CARON,
59 //  TEX_SPECIAL_CARON,
60         ///
61         TEX_HUNGUML,
62         ///
63         TEX_UMLAUT,
64         ///
65         TEX_DOT,
66         ///
67         TEX_OGONEK,
68         ///
69         TEX_MAX_ACCENT = TEX_OGONEK
70 };
71
72
73 struct tex_accent_struct {
74         ///
75         tex_accent accent;
76         /// UCS4 code point of this accent
77         char_type ucs4;
78         ///
79         char const * name;
80         ///
81         kb_action action;
82 };
83
84 ///
85 extern tex_accent_struct get_accent(kb_action action);
86
87
88 ///
89 struct Keyexc {
90         /// character to make exception
91         char_type c;
92         /// exception data
93         docstring data;
94         /// Combination with another deadkey
95         bool combined;
96         /// The accent comined with
97         tex_accent accent;
98 };
99
100 ///
101 typedef std::list<Keyexc> KmodException;
102
103 ///
104 class KmodInfo {
105 public:
106         ///
107         docstring data;
108         ///
109         tex_accent accent;
110         ///
111         KmodException exception_list;
112 };
113
114
115 /////////////////////////////////////////////////////////////////////
116 //
117 // Trans: holds a .kmap file
118 //
119 /////////////////////////////////////////////////////////////////////
120
121 class Trans {
122 public:
123         ///
124         Trans() {}
125         ///
126         ~Trans() { freeKeymap(); }
127
128         ///
129         int load(std::string const & language);
130         ///
131         bool isDefined() const;
132         ///
133         std::string const & getName() const { return name_; }
134         ///
135         docstring const process(char_type, TransManager &);
136         ///
137         bool isAccentDefined(tex_accent, KmodInfo &) const;
138
139 private:
140         ///
141         void addDeadkey(tex_accent, docstring const &);
142         ///
143         void freeKeymap();
144         ///
145         int load(Lexer &);
146         ///
147         docstring const & match(char_type c);
148         ///
149         void insertException(KmodException & exclist, char_type c,
150                              docstring const & data, bool = false,
151                              tex_accent = TEX_NOACCENT);
152         ///
153         void freeException(KmodException & exclist);
154
155         ///
156         std::string name_;
157         ///
158         std::map<char_type, docstring> keymap_;
159         ///
160         std::map<tex_accent, KmodInfo> kmod_list_;
161 };
162
163
164 ///
165 inline
166 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