LyX 1.6.0 release candidate 4 (rc4)
[lyx.git] / src / Trans.h
blobe2b196705dd638149dc81000c74725f8860acddb
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.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
9 * \author John Levon
11 * Full author contact details are available in file CREDITS.
14 #ifndef TRANS_H
15 #define TRANS_H
17 #include "FuncCode.h"
19 #include "support/docstring.h"
21 #include <list>
22 #include <map>
25 namespace lyx {
27 class Cursor;
28 class Text;
29 class Lexer;
30 class TransManager;
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
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;
85 ///
86 extern TeXAccent get_accent(FuncCode action);
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;
102 typedef std::list<Keyexc> KmodException;
105 class KmodInfo {
106 public:
108 docstring data;
110 tex_accent accent;
112 KmodException exception_list;
116 /////////////////////////////////////////////////////////////////////
118 // Trans: holds a .kmap file
120 /////////////////////////////////////////////////////////////////////
122 class Trans {
123 public:
125 Trans() {}
127 ~Trans() { freeKeymap(); }
130 int load(std::string const & language);
132 bool isDefined() const;
134 std::string const & getName() const { return name_; }
136 docstring const process(char_type, TransManager &);
138 bool isAccentDefined(tex_accent, KmodInfo &) const;
140 private:
142 void addDeadkey(tex_accent, docstring const &);
144 void freeKeymap();
146 int load(Lexer &);
148 docstring const & match(char_type c);
150 void insertException(KmodException & exclist, char_type c,
151 docstring const & data, bool = false,
152 tex_accent = TEX_NOACCENT);
154 void freeException(KmodException & exclist);
157 std::string name_;
159 std::map<char_type, docstring> keymap_;
161 std::map<tex_accent, KmodInfo> kmod_list_;
166 inline docstring const & Trans::match(char_type c)
168 std::map<char_type, docstring>::iterator it = keymap_.find(c);
169 if (it != keymap_.end()) {
170 return it->second;
172 static docstring dummy;
173 return dummy;
177 /////////////////////////////////////////////////////////////////////
179 // TransState
181 /////////////////////////////////////////////////////////////////////
183 /// Translation state
184 class TransState {
185 public:
187 virtual ~TransState() {}
189 virtual docstring const normalkey(char_type) = 0;
191 virtual bool backspace() = 0;
193 virtual docstring const deadkey(char_type, KmodInfo) = 0;
195 static char_type const TOKEN_SEP;
199 /// Translation FSM
200 class TransFSMData {
201 protected:
203 virtual ~TransFSMData() {}
205 char_type deadkey_;
207 KmodInfo deadkey_info_;
209 char_type deadkey2_;
211 KmodInfo deadkey2_info_;
213 Keyexc comb_info_;
215 TransState * init_state_;
217 TransState * deadkey_state_;
219 TransState * combined_state_;
221 public:
223 TransFSMData();
225 TransState * currentState;
229 /// Init State
230 class TransInitState : virtual public TransFSMData, public TransState {
231 public:
233 TransInitState();
235 virtual docstring const normalkey(char_type);
237 virtual bool backspace() { return true; }
239 virtual docstring const deadkey(char_type, KmodInfo);
243 /// Deadkey State
244 class TransDeadkeyState : virtual public TransFSMData, public TransState {
245 public:
247 TransDeadkeyState();
249 virtual docstring const normalkey(char_type);
251 virtual bool backspace() {
252 currentState = init_state_;
253 return false;
256 virtual docstring const deadkey(char_type, KmodInfo);
260 /// Combined State
261 class TransCombinedState : virtual public TransFSMData, public TransState {
262 public:
264 TransCombinedState();
266 virtual docstring const normalkey(char_type);
268 virtual bool backspace() {
269 // cancel the second deadkey
270 deadkey2_ = 0;
271 deadkey2_info_.accent = TEX_NOACCENT;
272 currentState = deadkey_state_;
274 return false;
277 virtual docstring const deadkey(char_type, KmodInfo);
282 class TransFSM : virtual public TransFSMData,
283 public TransInitState,
284 public TransDeadkeyState,
285 public TransCombinedState {
286 public:
288 TransFSM();
293 /////////////////////////////////////////////////////////////////////
295 // TransManager
297 /////////////////////////////////////////////////////////////////////
299 class TransManager {
300 private:
302 TransFSM trans_fsm_;
304 Trans * active_;
306 Trans t1_;
308 Trans t2_;
310 static Trans default_;
312 void insert(docstring const &, Text *, Cursor & cur);
313 public:
315 TransManager();
317 int setPrimary(std::string const &);
319 int setSecondary(std::string const &);
321 void enablePrimary();
323 void enableSecondary();
325 void disableKeymap();
327 bool backspace() { return trans_fsm_.currentState->backspace(); }
329 void translateAndInsert(char_type, Text *, Cursor &);
331 docstring const deadkey(char_type c, KmodInfo t)
332 { return trans_fsm_.currentState->deadkey(c, t); }
334 docstring const normalkey(char_type c)
335 { return trans_fsm_.currentState->normalkey(c); }
337 void deadkey(char_type, tex_accent, Text *, Cursor &);
340 } // namespace lyx
342 #endif // TRANS_H