4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
11 * Full author contact details are available in file CREDITS.
17 #include "FuncRequest.h"
18 #include "KeySequence.h"
20 #include "support/strfwd.h"
32 /// Defines key maps and actions for key sequences
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.
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.
50 * Bind/Unbind a key sequence to an action.
51 * @return 0 on success, or position in string seq where error
53 * See KeySequence::parse for the syntax of the seq string
55 size_t bind(std::string const & seq, FuncRequest const & func);
56 size_t unbind(std::string const & seq, FuncRequest const & func);
59 * Define/Undefine an action for a key sequence.
60 * @param r internal recursion level
62 void bind(KeySequence * seq, FuncRequest const & func,
64 void unbind(KeySequence * seq, FuncRequest const & func,
68 /// returns the function bound to this key sequence, or:
69 /// * FuncRequest::unknown if no binding exists for it;
70 /// * FuncRequest::prefix if this is the start of longer keysequences
71 /// @param r an internal recursion counter
72 // FIXME Surely there's a better way to do that?
73 FuncRequest getBinding(KeySequence const & seq, unsigned int r = 0);
75 /// clear all bindings
78 /** Parse a bind file. If a valid unbind_map is given, put \unbind
79 * bindings to a separate KeyMap. This is used in the Shortcut preference
80 * dialog where main and user bind files are loaded separately so \unbind
81 * in user.bind can not nullify \bind in the master bind file.
83 * @param bind_file bind file
84 * @param unbind_map pointer to a KeyMap that holds \unbind bindings
85 * @param rt how to respond if the file can't be found
86 * @param i18n whether to search in localized folders
88 bool read(std::string const & bind_file, KeyMap * unbind_map = 0,
89 BindReadType rt = Default, bool i18n = true);
91 /** write to a bind file.
92 * @param append append to the bind_file instead of overwrite it
93 * @param unbind use \unbind instead of \bind, indicating this KeyMap
94 * actually record unbind maps.
96 void write(std::string const & bind_file, bool append, bool unbind = false) const;
99 * print all available keysyms
100 * @param forgui true if the string should use translations and
101 * special characters.
103 docstring const print(bool forgui) const;
106 * Look up a key press in the keymap.
107 * @param key the keysym
108 * @param mod the modifiers
109 * @param seq the current key sequence so far
110 * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
113 lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
116 typedef std::vector<KeySequence> Bindings;
118 /// Given an action, find all keybindings.
119 Bindings findBindings(FuncRequest const & func) const;
121 /// Given an action, print the keybindings.
122 docstring printBindings(FuncRequest const & func,
123 KeySequence::outputFormat format,
124 bool const untranslated = false) const;
127 Binding(FuncRequest const & r, KeySequence const & s, ItemType t)
128 : request(r), sequence(s), tag(t) {}
130 KeySequence sequence;
131 KeyMap::ItemType tag;
133 typedef std::vector<Binding> BindingList;
135 * Return all lfun and their associated bindings.
136 * @param unbound list unbound (func without any keybinding) as well
137 * @param tag an optional tag to indicate the source of the bindinglist
139 BindingList listBindings(bool unbound, ItemType tag = System) const;
142 * Given an action, find the first 1-key binding (if it exists).
143 * The KeySymbol pointer is 0 is no key is found.
144 * [only used by the Qt/Mac frontend]
146 std::pair<KeySymbol, KeyModifier>
147 find1keybinding(FuncRequest const & func) const;
150 * Returns a string of the given keysym, with modifiers.
151 * @param key the key as a keysym
152 * @param mod the modifiers
154 static std::string const printKeySym(KeySymbol const & key,
159 typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
167 /// Keymap for prefix keys
168 std::shared_ptr<KeyMap> prefixes;
169 /// Action for !prefix keys
180 bool read(support::FileName const & bind_file, KeyMap * unbind_map = 0);
182 ReturnValues readWithoutConv(support::FileName const & bind_file, KeyMap * unbind_map = 0);
185 * Given an action, find all keybindings
186 * @param func the action
187 * @param prefix a sequence to prepend the results
189 Bindings findBindings(FuncRequest const & func,
190 KeySequence const & prefix) const;
192 void listBindings(BindingList & list, KeySequence const & prefix,
195 /// is the table empty ?
196 bool empty() const { return table.empty(); }
198 typedef std::vector<Key> Table;
203 /// Implementation is in LyX.cpp
204 extern KeyMap & theTopLevelKeymap();