Update lyx examples to latest file format (for 1.5.0 release)
[lyx.git] / src / Trans.h
blob92cad9b0dcde38272effea467e4f1a80f4d4aafd
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 "lfuns.h"
18 #include "support/docstring.h"
20 #include <boost/scoped_ptr.hpp>
22 #include <list>
23 #include <map>
26 namespace lyx {
28 class Cursor;
29 class Text;
30 class Lexer;
31 class TransManager;
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
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;
86 ///
87 extern tex_accent_struct get_accent(kb_action action);
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;
103 typedef std::list<Keyexc> KmodException;
106 class KmodInfo {
107 public:
109 docstring data;
111 tex_accent accent;
113 KmodException exception_list;
117 /////////////////////////////////////////////////////////////////////
119 // Trans: holds a .kmap file
121 /////////////////////////////////////////////////////////////////////
123 class Trans {
124 public:
126 Trans() {}
128 ~Trans() { freeKeymap(); }
131 int load(std::string const & language);
133 bool isDefined() const;
135 std::string const & getName() const { return name_; }
137 docstring const process(char_type, TransManager &);
139 bool isAccentDefined(tex_accent, KmodInfo &) const;
141 private:
143 void addDeadkey(tex_accent, docstring const &);
145 void freeKeymap();
147 int load(Lexer &);
149 docstring const & match(char_type c);
151 void insertException(KmodException & exclist, char_type c,
152 docstring const & data, bool = false,
153 tex_accent = TEX_NOACCENT);
155 void freeException(KmodException & exclist);
158 std::string name_;
160 std::map<char_type, docstring> keymap_;
162 std::map<tex_accent, KmodInfo> kmod_list_;
167 inline
168 docstring const & Trans::match(char_type c)
170 std::map<char_type, docstring>::iterator it = keymap_.find(c);
171 if (it != keymap_.end()) {
172 return it->second;
174 static docstring dummy;
175 return dummy;
179 /////////////////////////////////////////////////////////////////////
181 // TransState
183 /////////////////////////////////////////////////////////////////////
185 /// Translation state
186 class TransState {
187 public:
189 virtual ~TransState() {}
191 virtual docstring const normalkey(char_type) = 0;
193 virtual bool backspace() = 0;
195 virtual docstring const deadkey(char_type, KmodInfo) = 0;
197 static char_type const TOKEN_SEP;
201 /// Translation FSM
202 class TransFSMData {
203 protected:
205 virtual ~TransFSMData() {}
207 char_type deadkey_;
209 KmodInfo deadkey_info_;
211 char_type deadkey2_;
213 KmodInfo deadkey2_info_;
215 Keyexc comb_info_;
217 TransState * init_state_;
219 TransState * deadkey_state_;
221 TransState * combined_state_;
223 public:
225 TransFSMData();
227 TransState * currentState;
231 /// Init State
232 class TransInitState : virtual public TransFSMData, public TransState {
233 public:
235 TransInitState();
237 virtual docstring const normalkey(char_type);
239 virtual bool backspace() { return true; }
241 virtual docstring const deadkey(char_type, KmodInfo);
245 /// Deadkey State
246 class TransDeadkeyState : virtual public TransFSMData, public TransState {
247 public:
249 TransDeadkeyState();
251 virtual docstring const normalkey(char_type);
253 virtual bool backspace() {
254 currentState = init_state_;
255 return false;
258 virtual docstring const deadkey(char_type, KmodInfo);
262 /// Combined State
263 class TransCombinedState : virtual public TransFSMData, public TransState {
264 public:
266 TransCombinedState();
268 virtual docstring const normalkey(char_type);
270 virtual bool backspace() {
271 // cancel the second deadkey
272 deadkey2_ = 0;
273 deadkey2_info_.accent = TEX_NOACCENT;
274 currentState = deadkey_state_;
276 return false;
279 virtual docstring const deadkey(char_type, KmodInfo);
284 class TransFSM : virtual public TransFSMData,
285 public TransInitState,
286 public TransDeadkeyState,
287 public TransCombinedState {
288 public:
290 TransFSM();
295 /////////////////////////////////////////////////////////////////////
297 // TransManager
299 /////////////////////////////////////////////////////////////////////
301 class TransManager {
302 private:
304 TransFSM trans_fsm_;
306 Trans * active_;
308 boost::scoped_ptr<Trans> t1_;
310 boost::scoped_ptr<Trans> t2_;
312 static Trans default_;
314 void insert(docstring const &, Text *, Cursor & cur);
315 public:
317 TransManager();
319 ~TransManager();
321 int setPrimary(std::string const &);
323 int setSecondary(std::string const &);
325 void enablePrimary();
327 void enableSecondary();
329 void disableKeymap();
331 bool backspace() { return trans_fsm_.currentState->backspace(); }
333 void translateAndInsert(char_type, Text *, Cursor &);
335 docstring const deadkey(char_type c, KmodInfo t)
336 { return trans_fsm_.currentState->deadkey(c, t); }
338 docstring const normalkey(char_type c)
339 { return trans_fsm_.currentState->normalkey(c); }
341 void deadkey(char_type, tex_accent, Text *, Cursor &);
344 } // namespace lyx
346 #endif // TRANS_H