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