]> git.lyx.org Git - lyx.git/blob - src/KeySequence.cpp
Allow boldface and font size changing commands for unicode symbols in math.
[lyx.git] / src / KeySequence.cpp
1 /**
2  * \file KeySequence.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "KeySequence.h"
16 #include "KeyMap.h"
17
18 #include "support/gettext.h"
19
20 #include "frontends/KeySymbol.h"
21
22 using namespace std;
23
24 namespace lyx {
25
26 FuncRequest const & KeySequence::addkey(KeySymbol const & key,
27         KeyModifier mod, KeyModifier nmod)
28 {
29         // adding a key to a deleted sequence
30         // starts a new sequence
31         if (deleted_) {
32                 deleted_ = false;
33                 sequence.clear();
34                 modifiers.clear();
35         }
36
37         modifiers.push_back(make_pair(mod, nmod));
38         sequence.push_back(key);
39
40         if (curmap)
41                 return curmap->lookup(key, mod, this);
42
43         static FuncRequest unknown(LFUN_UNKNOWN_ACTION);
44         return unknown;
45 }
46
47
48 size_t KeySequence::parse(string const & s)
49 {
50         if (s.empty())
51                 return 1;
52
53         size_t i = 0;
54         KeyModifier mod = NoModifier;
55         KeyModifier nmod = NoModifier;
56
57         while (i < s.length()) {
58                 if (s[i] == ' ')
59                         ++i;
60                 if (i >= s.length())
61                         break;
62
63                 if (i + 1 < s.length() && s[i + 1] == '-') {
64                         switch (s[i]) {
65                         case 's': case 'S':
66                                 mod |= ShiftModifier;
67                                 i += 2;
68                                 continue;
69                         case 'c': case 'C':
70                                 mod |= ControlModifier;
71                                 i += 2;
72                                 continue;
73                         case 'm': case 'M':
74                                 mod |= AltModifier;
75                                 i += 2;
76                                 continue;
77                         default:
78                                 return i + 1;
79                         }
80                 } else if (i + 2 < s.length() && s[i] == '~'
81                            && s[i + 2] == '-') {
82                         switch (s[i + 1]) {
83                         case 's': case 'S':
84                                 nmod |= ShiftModifier;
85                                 i += 3;
86                                 continue;
87                         case 'c': case 'C':
88                                 nmod |= ControlModifier;
89                                 i += 3;
90                                 continue;
91                         case 'm': case 'M':
92                                 nmod |= AltModifier;
93                                 i += 3;
94                                 continue;
95                         default:
96                                 return i + 2;
97                         }
98                 } else {
99                         string tbuf;
100                         size_t j = i;
101                         for (; j < s.length() && s[j] != ' '; ++j)
102                                 tbuf += s[j];    // (!!!check bounds :-)
103
104                         KeySymbol key;
105                         key.init(tbuf);
106
107                         if (!key.isOK())
108                                 return j;
109
110                         i = j;
111
112                         addkey(key, mod, nmod);
113                         mod = NoModifier;
114                 }
115         }
116
117         // empty sequence?
118         if (sequence.size() == 0)
119                 return 0;
120
121         // everything is fine
122         return string::npos;
123 }
124
125
126 docstring const KeySequence::print(outputFormat format) const
127 {
128         docstring buf;
129
130         size_t const length = sequence.size();
131
132         for (size_t i = 0; i != length; ++i) {
133                 switch (format) {
134                 case Portable:
135                         buf += sequence[i].print(modifiers[i].first, false);
136                         break;
137                 case ForGui:
138                         buf += sequence[i].print(modifiers[i].first, true);
139                         break;
140                 case BindFile:
141                         KeyModifier mod = modifiers[i].first;
142                         if (mod & ControlModifier)
143                                 buf += "C-";
144                         if (mod & AltModifier)
145                                 buf += "M-";
146                         if (mod & ShiftModifier)
147                                 buf += "S-";
148                 
149                         buf += from_utf8(sequence[i].getSymbolName());
150                         break;
151                 }
152                 // append a blank
153                 if (i + 1 != length)
154                         buf += ' ';
155         }
156         return buf;
157 }
158
159
160 docstring const KeySequence::printOptions(bool forgui) const
161 {
162         docstring buf = print(forgui ? ForGui : Portable);
163
164         if (!curmap)
165                 return buf;
166
167         buf += _("   options: ");
168         buf += curmap->print(forgui ? ForGui : Portable);
169         return buf;
170 }
171
172
173 void KeySequence::mark_deleted()
174 {
175         deleted_ = true;
176 }
177
178
179 void KeySequence::reset()
180 {
181         mark_deleted();
182         curmap = stdmap;
183 }
184
185
186 void KeySequence::clear()
187 {
188         sequence.clear();
189         reset();
190 }
191
192
193 } // namespace lyx