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