]> git.lyx.org Git - lyx.git/blob - src/kbmap.C
redraw fix 1.
[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() ) return;
119
120         key_modifier::state const mod1 = seq->modifiers[r].first;
121         key_modifier::state const mod2 = seq->modifiers[r].second;
122
123         // check if key is already there
124         for (Table::iterator it = table.begin(); it != table.end(); ++it) {
125                 if (*(code) == *(it->code)
126                     && mod1 == it->mod.first
127                     && mod2 == it->mod.second) {
128                         // overwrite binding
129                         if (r + 1 == seq->length()) {
130                                 lyxerr[Debug::KBMAP]
131                                         << "Warning: New binding for '"
132                                         << seq->print()
133                                         << "' is overriding old binding..."
134                                         << endl;
135                                 if (it->table.get()) {
136                                         it->table.reset();
137                                 }
138                                 it->action = action;
139                                 return;
140                         } else if (!it->table.get()) {
141                                 lyxerr << "Error: New binding for '" << seq->print()
142                                        << "' is overriding old binding..."
143                                                << endl;
144                                 return;
145                         } else {
146                                 it->table->defkey(seq, action, r + 1);
147                                 return;
148                         }
149                 }
150         }
151
152         Table::iterator newone = table.insert(table.end(), kb_key());
153         newone->code = code;
154         newone->mod = seq->modifiers[r];
155         if (r + 1 == seq->length()) {
156                 newone->action = action;
157                 newone->table.reset();
158                 return;
159         } else {
160                 newone->table.reset(new kb_keymap);
161                 newone->table->defkey(seq, action, r + 1);
162                 return;
163         }
164 }
165
166
167 string const kb_keymap::findbinding(int act, string const & prefix) const
168 {
169         string res;
170         if (table.empty()) return res;
171
172         Table::const_iterator end = table.end();
173         for (Table::const_iterator cit = table.begin();
174             cit != end; ++cit) {
175                 if (cit->table.get()) {
176                         res += cit->table->findbinding(act,
177                                                        prefix
178                                                        + printKey((*cit))
179                                                        + " ");
180                 } else if (cit->action == act) {
181                         res += "[";
182                         res += prefix + printKey((*cit));
183                         res += "] ";
184                 }
185         }
186         return res;
187 }