]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
last Friday's text*.C -> text_func shuffle
[lyx.git] / src / kbmap.C
1 /**
2  * \file kbmap.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "kbmap.h"
13 #include "lfuns.h"
14 #include "kbsequence.h"
15 #include "debug.h"
16
17 using std::endl;
18
19 string const kb_keymap::printKeysym(LyXKeySymPtr key,
20                                     key_modifier::state mod)
21 {
22         string buf;
23
24         string const s = key->getSymbolName();
25
26         if (mod & key_modifier::shift)
27                 buf += "S-";
28         if (mod & key_modifier::ctrl)
29                 buf += "C-";
30         if (mod & key_modifier::alt)
31                 buf += "M-";
32
33         buf += s;
34         return buf;
35 }
36
37
38 string const kb_keymap::printKey(kb_key const & key) const
39 {
40         return printKeysym(key.code, key.mod.first);
41 }
42
43
44 string::size_type kb_keymap::bind(string const & seq, int action)
45 {
46         if (lyxerr.debugging(Debug::KBMAP)) {
47                 lyxerr << "BIND: Sequence `"
48                        << seq << "' Action `"
49                        << action << '\'' << endl;
50         }
51
52         kb_sequence k(0, 0);
53
54         string::size_type const res = k.parse(seq);
55         if (res == string::npos) {
56                 defkey(&k, action);
57         } else {
58                 lyxerr[Debug::KBMAP] << "Parse error at position " << res
59                                      << " in key sequence '" << seq << "'."
60                                      << endl;
61         }
62
63         return res;
64 }
65
66
67 int kb_keymap::lookup(LyXKeySymPtr key,
68                       key_modifier::state mod, kb_sequence * seq) const
69 {
70         if (table.empty()) {
71                 seq->curmap = seq->stdmap;
72                 seq->mark_deleted();
73                 return LFUN_UNKNOWN_ACTION;
74         }
75
76         Table::const_iterator end = table.end();
77         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
78                 key_modifier::state mask(cit->mod.second);
79                 key_modifier::state check =
80                         static_cast<key_modifier::state>(mod & ~mask);
81
82                 if (*(cit->code) == *key && cit->mod.first == check) {
83                         // match found
84                         if (cit->table.get()) {
85                                 // this is a prefix key - set new map
86                                 seq->curmap = cit->table.get();
87                                 return LFUN_PREFIX;
88                         } else {
89                                 // final key - reset map
90                                 seq->curmap = seq->stdmap;
91                                 seq->mark_deleted();
92                                 return cit->action;
93                         }
94                 }
95         }
96
97         // error - key not found:
98         seq->curmap = seq->stdmap;
99         seq->mark_deleted();
100         return LFUN_UNKNOWN_ACTION;
101 }
102
103
104 string const kb_keymap::print() const
105 {
106         string buf;
107         Table::const_iterator end = table.end();
108         for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
109                 buf += printKey((*cit));
110                 buf += ' ';
111         }
112         return buf;
113 }
114
115
116 void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
117 {
118         LyXKeySymPtr code = seq->sequence[r];
119         if (!code->isOK())
120                 return;
121
122         key_modifier::state const mod1 = seq->modifiers[r].first;
123         key_modifier::state const mod2 = seq->modifiers[r].second;
124
125         // check if key is already there
126         Table::iterator end = table.end();
127         for (Table::iterator it = table.begin(); it != end; ++it) {
128                 if (*(code) == *(it->code)
129                     && mod1 == it->mod.first
130                     && mod2 == it->mod.second) {
131                         // overwrite binding
132                         if (r + 1 == seq->length()) {
133                                 lyxerr[Debug::KBMAP]
134                                         << "Warning: New binding for '"
135                                         << seq->print()
136                                         << "' is overriding old binding..."
137                                         << endl;
138                                 if (it->table.get()) {
139                                         it->table.reset();
140                                 }
141                                 it->action = action;
142                                 return;
143                         } else if (!it->table.get()) {
144                                 lyxerr << "Error: New binding for '" << seq->print()
145                                        << "' is overriding old binding..."
146                                                << endl;
147                                 return;
148                         } else {
149                                 it->table->defkey(seq, action, r + 1);
150                                 return;
151                         }
152                 }
153         }
154
155         Table::iterator newone = table.insert(table.end(), kb_key());
156         newone->code = code;
157         newone->mod = seq->modifiers[r];
158         if (r + 1 == seq->length()) {
159                 newone->action = action;
160                 newone->table.reset();
161                 return;
162         } else {
163                 newone->table.reset(new kb_keymap);
164                 newone->table->defkey(seq, action, r + 1);
165                 return;
166         }
167 }
168
169
170 string const kb_keymap::findbinding(int act, string const & prefix) const
171 {
172         string res;
173         if (table.empty()) return res;
174
175         Table::const_iterator end = table.end();
176         for (Table::const_iterator cit = table.begin();
177             cit != end; ++cit) {
178                 if (cit->table.get()) {
179                         res += cit->table->findbinding(act,
180                                                        prefix
181                                                        + printKey((*cit))
182                                                        + ' ');
183                 } else if (cit->action == act) {
184                         res += '[';
185                         res += prefix + printKey((*cit));
186                         res += "] ";
187                 }
188         }
189         return res;
190 }