]> git.lyx.org Git - lyx.git/blob - src/kbsequence.C
unify toclevel for articles and reports/books; proper numbering of Paragraph/SubParag...
[lyx.git] / src / kbsequence.C
1 /**
2  * \file kbsequence.C
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 "kbsequence.h"
16
17 #include "gettext.h"
18 #include "kbmap.h"
19 #include "lfuns.h"
20
21 #include "frontends/LyXKeySym.h"
22 #include "frontends/LyXKeySymFactory.h"
23
24 using std::make_pair;
25 using std::string;
26
27
28 FuncRequest const &
29 kb_sequence::addkey(LyXKeySymPtr key,
30                     key_modifier::state mod, key_modifier::state nmod)
31 {
32         // adding a key to a deleted sequence
33         // starts a new sequence
34         if (deleted_) {
35                 deleted_ = false;
36                 sequence.clear();
37                 modifiers.clear();
38         }
39
40         modifiers.push_back(make_pair(mod, nmod));
41         sequence.push_back(key);
42
43         if (curmap) {
44                 return curmap->lookup(key, mod, this);
45         }
46
47         static FuncRequest unknown(LFUN_UNKNOWN_ACTION);
48         return unknown;
49 }
50
51
52 string::size_type kb_sequence::parse(string const & s)
53 {
54         if (s.empty()) return 1;
55
56         string::size_type i = 0;
57         key_modifier::state mod = key_modifier::none;
58         key_modifier::state nmod = key_modifier::none;
59
60         while (i < s.length()) {
61                 if (s[i] == ' ')
62                         ++i;
63                 if (i >= s.length())
64                         break;
65
66                 if (i + 1 < s.length() && s[i + 1] == '-') {
67                         switch (s[i]) {
68                         case 's': case 'S':
69                                 mod |= key_modifier::shift;
70                                 i += 2;
71                                 continue;
72                         case 'c': case 'C':
73                                 mod |= key_modifier::ctrl;
74                                 i += 2;
75                                 continue;
76                         case 'm': case 'M':
77                                 mod |= key_modifier::alt;
78                                 i += 2;
79                                 continue;
80                         default:
81                                 return i + 1;
82                         }
83                 } else if (i + 2 < s.length() && s[i] == '~'
84                            && s[i + 2] == '-') {
85                         switch (s[i + 1]) {
86                         case 's': case 'S':
87                                 nmod |= key_modifier::shift;
88                                 i += 3;
89                                 continue;
90                         case 'c': case 'C':
91                                 nmod |= key_modifier::ctrl;
92                                 i += 3;
93                                 continue;
94                         case 'm': case 'M':
95                                 nmod |= key_modifier::alt;
96                                 i += 3;
97                                 continue;
98                         default:
99                                 return i + 2;
100                         }
101                 } else {
102                         string tbuf;
103                         string::size_type j = i;
104                         for (; j < s.length() && s[j] != ' '; ++j)
105                                 tbuf += s[j];    // (!!!check bounds :-)
106
107                         LyXKeySymPtr key(LyXKeySymFactory::create());
108                         key->init(tbuf);
109
110                         if ( ! key->isOK() ) {
111                                 return j;
112                         }
113
114                         i = j;
115
116                         addkey(key, mod, nmod);
117                         mod = key_modifier::none;
118                 }
119         }
120
121         // empty sequence?
122         if (sequence.size() == 0)
123                 return 0;
124
125         // everything is fine
126         return string::npos;
127 }
128
129
130 string const kb_sequence::print() const
131 {
132         string buf;
133
134         //if (deleted_)
135         //      return buf;
136
137         KeySequence::size_type i, length = sequence.size();
138
139         for (i = 0; i < length; ++i) {
140                 buf += sequence[i]->print(modifiers[i].first);
141
142                 // append a blank
143                 if (i + 1 < length) {
144                         buf += ' ';
145                 }
146         }
147         return buf;
148 }
149
150
151 string const kb_sequence::printOptions() const
152 {
153         string buf;
154
155         buf += print();
156
157         if (!curmap)
158                 return buf;
159
160         buf += _("   options: ");
161         buf += curmap->print();
162         return buf;
163 }
164
165
166 void kb_sequence::mark_deleted()
167 {
168         deleted_ = true;
169 }
170
171
172 void kb_sequence::reset()
173 {
174         mark_deleted();
175         curmap = stdmap;
176 }
177
178 void kb_sequence::clear()
179 {
180         sequence.clear();
181         reset();
182 }