]> git.lyx.org Git - lyx.git/blob - src/KeyMap.h
Debug output for paragraph params.
[lyx.git] / src / KeyMap.h
1 // -*- C++ -*-
2 /**
3  * \file KeyMap.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef KEYMAP_H
15 #define KEYMAP_H
16
17 #include "FuncRequest.h"
18 #include "KeySequence.h"
19
20 #include "support/strfwd.h"
21
22 #include <memory>
23 #include <vector>
24
25
26 namespace lyx {
27
28 namespace support {
29         class FileName;
30 }
31
32 /// Defines key maps and actions for key sequences
33 class KeyMap {
34 public:
35         enum ItemType {
36                 System,         //< loaded from a bind file
37                 UserBind,       //< \bind loaded from user.bind
38                 UserUnbind,     //< \unbind loaded from user.bind, with corresponding
39                                 //<    entry in system bind file
40                 UserExtraUnbind //< \unbind loaded from user.bind, without
41                                 //<    corresponding entry in system bind file.
42         };
43         enum BindReadType {
44                 MissingOK,      //< It's OK if this file is missing.
45                 Fallback,       //< If missing, fallback to default "cua". This should only
46                                 //< be used when attempting to read the user-secified bind file.
47                 Default         //< Report error and return.
48         };
49         /**
50          * Bind/Unbind a key sequence to an action.
51          * @return 0 on success, or position in string seq where error
52          * occurs.
53          * See KeySequence::parse for the syntax of the seq string
54          */
55         size_t bind(std::string const & seq, FuncRequest const & func);
56         size_t unbind(std::string const & seq, FuncRequest const & func);
57
58         /**
59          * Define/Undefine an action for a key sequence.
60          * @param r internal recursion level
61          */
62         void bind(KeySequence * seq, FuncRequest const & func,
63                     unsigned int r = 0);
64         void unbind(KeySequence * seq, FuncRequest const & func,
65                     unsigned int r = 0);
66
67
68         /// returns the function bound to this key sequence, or
69         /// FuncRequest::unknown if no binding exists for it.
70         /// @param r an internal recursion counter
71         // FIXME Surely there's a better way to do that?
72         FuncRequest getBinding(KeySequence const & seq, unsigned int r = 0);
73
74         /// clear all bindings
75         void clear();
76
77         /** Parse a bind file. If a valid unbind_map is given, put \unbind
78          * bindings to a separate KeyMap. This is used in the Shortcut preference
79          * dialog where main and user bind files are loaded separately so \unbind
80          * in user.bind can not nullify \bind in the master bind file.
81          *
82          * @param bind_file bind file
83          * @param unbind_map pointer to a KeyMap that holds \unbind bindings
84          * @param rt how to respond if the file can't be found
85          */
86         bool read(std::string const & bind_file, KeyMap * unbind_map = 0,
87                         BindReadType rt = Default);
88
89         /** write to a bind file.
90          * @param append append to the bind_file instead of overwrite it
91          * @param unbind use \unbind instead of \bind, indicating this KeyMap
92          *        actually record unbind maps.
93          */
94         void write(std::string const & bind_file, bool append, bool unbind = false) const;
95
96         /**
97          * print all available keysyms
98          * @param forgui true if the string should use translations and
99          *   special characters.
100          */
101         docstring const print(bool forgui) const;
102
103         /**
104          * Look up a key press in the keymap.
105          * @param key the keysym
106          * @param mod the modifiers
107          * @param seq the current key sequence so far
108          * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
109          */
110         FuncRequest const &
111                 lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
112
113         ///
114         typedef std::vector<KeySequence> Bindings;
115
116         /// Given an action, find all keybindings.
117         Bindings findBindings(FuncRequest const & func) const;
118
119         /// Given an action, print the keybindings.
120         docstring printBindings(FuncRequest const & func,
121                                 KeySequence::outputFormat format,
122                                 bool const untranslated = false) const;
123
124         struct Binding {
125                 Binding(FuncRequest const & r, KeySequence const & s, ItemType t)
126                         : request(r), sequence(s), tag(t) {}
127                 FuncRequest request;
128                 KeySequence sequence;
129                 KeyMap::ItemType tag;
130         };
131         typedef std::vector<Binding> BindingList;
132         /**
133          * Return all lfun and their associated bindings.
134          * @param unbound list unbound (func without any keybinding) as well
135          * @param tag an optional tag to indicate the source of the bindinglist
136          */
137         BindingList listBindings(bool unbound, ItemType tag = System) const;
138
139         /**
140          *  Given an action, find the first 1-key binding (if it exists).
141          *  The KeySymbol pointer is 0 is no key is found.
142          *  [only used by the Qt/Mac frontend]
143          */
144         std::pair<KeySymbol, KeyModifier>
145                 find1keybinding(FuncRequest const & func) const;
146
147         /**
148          * Returns a string of the given keysym, with modifiers.
149          * @param key the key as a keysym
150          * @param mod the modifiers
151          */
152         static std::string const printKeySym(KeySymbol const & key,
153                                              KeyModifier mod);
154
155 private:
156         ///
157         typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
158
159         ///
160         struct Key {
161                 /// Keysym
162                 KeySymbol code;
163                 /// Modifier masks
164                 ModifierPair mod;
165                 /// Keymap for prefix keys
166                 std::shared_ptr<KeyMap> prefixes;
167                 /// Action for !prefix keys
168                 FuncRequest func;
169         };
170
171         enum ReturnValues {
172                 ReadOK,
173                 ReadError,
174                 FileError,
175                 FormatMismatch
176         };
177         ///
178         bool read(support::FileName const & bind_file, KeyMap * unbind_map = 0);
179         ///
180         ReturnValues readWithoutConv(support::FileName const & bind_file, KeyMap * unbind_map = 0);
181
182         /**
183          * Given an action, find all keybindings
184          * @param func the action
185          * @param prefix a sequence to prepend the results
186          */
187         Bindings findBindings(FuncRequest const & func,
188                               KeySequence const & prefix) const;
189
190         void listBindings(BindingList & list, KeySequence const & prefix,
191                           ItemType tag) const;
192
193         /// is the table empty ?
194         bool empty() const { return table.empty(); }
195         ///
196         typedef std::vector<Key> Table;
197         ///
198         Table table;
199 };
200
201 /// Implementation is in LyX.cpp
202 extern KeyMap & theTopLevelKeymap();
203
204
205 } // namespace lyx
206
207 #endif // KEYMAP_H