]> git.lyx.org Git - lyx.git/blob - src/Trans.h
Update of documentation in the source related to bug 4135 and the function callback...
[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 <boost/scoped_ptr.hpp>
21
22 #include <list>
23 #include <map>
24
25
26 namespace lyx {
27
28 class Cursor;
29 class Text;
30 class Lexer;
31 class TransManager;
32
33 ///
34 enum tex_accent {
35         ///
36         TEX_NOACCENT = 0,
37         ///
38         TEX_ACUTE,
39         ///
40         TEX_GRAVE,
41         ///
42         TEX_MACRON,
43         ///
44         TEX_TILDE,
45         ///
46         TEX_UNDERBAR,
47         ///
48         TEX_CEDILLA,
49         ///
50         TEX_UNDERDOT,
51         ///
52         TEX_CIRCUMFLEX,
53         ///
54         TEX_CIRCLE,
55         ///
56         TEX_TIE,
57         ///
58         TEX_BREVE,
59         ///
60         TEX_CARON,
61 //  TEX_SPECIAL_CARON,
62         ///
63         TEX_HUNGUML,
64         ///
65         TEX_UMLAUT,
66         ///
67         TEX_DOT,
68         ///
69         TEX_OGONEK,
70         ///
71         TEX_MAX_ACCENT = TEX_OGONEK
72 };
73
74
75 struct tex_accent_struct {
76         ///
77         tex_accent accent;
78         /// UCS4 code point of this accent
79         char_type ucs4;
80         ///
81         char const * name;
82         ///
83         kb_action action;
84 };
85
86 ///
87 extern tex_accent_struct get_accent(kb_action action);
88
89
90 ///
91 struct Keyexc {
92         /// character to make exception
93         char_type c;
94         /// exception data
95         docstring data;
96         /// Combination with another deadkey
97         bool combined;
98         /// The accent comined with
99         tex_accent accent;
100 };
101
102 ///
103 typedef std::list<Keyexc> KmodException;
104
105 ///
106 class KmodInfo {
107 public:
108         ///
109         docstring data;
110         ///
111         tex_accent accent;
112         ///
113         KmodException exception_list;
114 };
115
116
117 /////////////////////////////////////////////////////////////////////
118 //
119 // Trans: holds a .kmap file
120 //
121 /////////////////////////////////////////////////////////////////////
122
123 class Trans {
124 public:
125         ///
126         Trans() {}
127         ///
128         ~Trans() { freeKeymap(); }
129
130         ///
131         int load(std::string const & language);
132         ///
133         bool isDefined() const;
134         ///
135         std::string const & getName() const { return name_; }
136         ///
137         docstring const process(char_type, TransManager &);
138         ///
139         bool isAccentDefined(tex_accent, KmodInfo &) const;
140
141 private:
142         ///
143         void addDeadkey(tex_accent, docstring const &);
144         ///
145         void freeKeymap();
146         ///
147         int load(Lexer &);
148         ///
149         docstring const & match(char_type c);
150         ///
151         void insertException(KmodException & exclist, char_type c,
152                              docstring const & data, bool = false,
153                              tex_accent = TEX_NOACCENT);
154         ///
155         void freeException(KmodException & exclist);
156
157         ///
158         std::string name_;
159         ///
160         std::map<char_type, docstring> keymap_;
161         ///
162         std::map<tex_accent, KmodInfo> kmod_list_;
163 };
164
165
166 ///
167 inline
168 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         boost::scoped_ptr<Trans> t1_;
309         ///
310         boost::scoped_ptr<Trans> t2_;
311         ///
312         static Trans default_;
313         ///
314         void insert(docstring const &, Text *, Cursor & cur);
315 public:
316         ///
317         TransManager();
318         ///
319         ~TransManager();
320         ///
321         int setPrimary(std::string const &);
322         ///
323         int setSecondary(std::string const &);
324         ///
325         void enablePrimary();
326         ///
327         void enableSecondary();
328         ///
329         void disableKeymap();
330         ///
331         bool backspace() { return trans_fsm_.currentState->backspace(); }
332         ///
333         void translateAndInsert(char_type, Text *, Cursor &);
334         ///
335         docstring const deadkey(char_type c, KmodInfo t)
336                 { return trans_fsm_.currentState->deadkey(c, t); }
337         ///
338         docstring const normalkey(char_type c)
339                 { return trans_fsm_.currentState->normalkey(c); }
340         ///
341         void deadkey(char_type, tex_accent, Text *, Cursor &);
342 };
343
344 } // namespace lyx
345
346 #endif // TRANS_H