2007-04-27 08:05:58 +00:00
|
|
|
// -*- C++ -*-
|
2002-02-26 10:50:48 +00:00
|
|
|
/**
|
2007-04-27 08:05:58 +00:00
|
|
|
* \file KeyMap.h
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-02-26 10:50:48 +00:00
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-02-26 10:50:48 +00:00
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-04-27 08:05:58 +00:00
|
|
|
#ifndef KEYMAP_H
|
|
|
|
#define KEYMAP_H
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "FuncRequest.h"
|
2007-11-02 20:26:28 +00:00
|
|
|
#include "KeySequence.h"
|
2003-09-06 20:08:10 +00:00
|
|
|
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/strfwd.h"
|
2006-12-13 14:13:01 +00:00
|
|
|
|
2016-06-02 17:13:55 +00:00
|
|
|
#include <memory>
|
2003-06-18 13:47:11 +00:00
|
|
|
#include <vector>
|
2002-05-22 01:16:37 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2009-07-16 23:12:45 +00:00
|
|
|
namespace support {
|
2017-07-03 17:45:58 +00:00
|
|
|
class FileName;
|
2009-07-16 23:12:45 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Defines key maps and actions for key sequences
|
2007-04-27 08:05:58 +00:00
|
|
|
class KeyMap {
|
1999-09-27 18:44:28 +00:00
|
|
|
public:
|
2008-09-23 13:53:38 +00:00
|
|
|
enum ItemType {
|
|
|
|
System, //< loaded from a bind file
|
|
|
|
UserBind, //< \bind loaded from user.bind
|
|
|
|
UserUnbind, //< \unbind loaded from user.bind, with corresponding
|
|
|
|
//< entry in system bind file
|
|
|
|
UserExtraUnbind //< \unbind loaded from user.bind, without
|
|
|
|
//< corresponding entry in system bind file.
|
|
|
|
};
|
2009-07-16 23:12:45 +00:00
|
|
|
enum BindReadType {
|
|
|
|
MissingOK, //< It's OK if this file is missing.
|
2017-07-03 17:45:58 +00:00
|
|
|
Fallback, //< If missing, fallback to default "cua". This should only
|
2009-07-16 23:12:45 +00:00
|
|
|
//< be used when attempting to read the user-secified bind file.
|
|
|
|
Default //< Report error and return.
|
|
|
|
};
|
2001-11-30 13:25:38 +00:00
|
|
|
/**
|
2007-10-26 20:43:22 +00:00
|
|
|
* Bind/Unbind a key sequence to an action.
|
2001-11-30 13:25:38 +00:00
|
|
|
* @return 0 on success, or position in string seq where error
|
|
|
|
* occurs.
|
2007-04-27 08:19:12 +00:00
|
|
|
* See KeySequence::parse for the syntax of the seq string
|
2001-11-30 13:25:38 +00:00
|
|
|
*/
|
2007-04-27 08:05:58 +00:00
|
|
|
size_t bind(std::string const & seq, FuncRequest const & func);
|
2007-10-20 20:35:33 +00:00
|
|
|
size_t unbind(std::string const & seq, FuncRequest const & func);
|
|
|
|
|
2007-10-26 20:43:22 +00:00
|
|
|
/**
|
|
|
|
* Define/Undefine an action for a key sequence.
|
|
|
|
* @param r internal recursion level
|
|
|
|
*/
|
|
|
|
void bind(KeySequence * seq, FuncRequest const & func,
|
|
|
|
unsigned int r = 0);
|
|
|
|
void unbind(KeySequence * seq, FuncRequest const & func,
|
|
|
|
unsigned int r = 0);
|
|
|
|
|
|
|
|
|
2017-07-03 17:45:58 +00:00
|
|
|
/// returns the function bound to this key sequence, or
|
2008-09-24 14:56:43 +00:00
|
|
|
/// FuncRequest::unknown if no binding exists for it.
|
|
|
|
/// @param r an internal recursion counter
|
|
|
|
// FIXME Surely there's a better way to do that?
|
|
|
|
FuncRequest getBinding(KeySequence const & seq, unsigned int r = 0);
|
2007-10-20 20:35:33 +00:00
|
|
|
|
2008-09-24 13:20:05 +00:00
|
|
|
/// clear all bindings
|
2007-10-20 20:35:33 +00:00
|
|
|
void clear();
|
|
|
|
|
2017-07-03 17:45:58 +00:00
|
|
|
/** Parse a bind file. If a valid unbind_map is given, put \unbind
|
2007-10-20 20:35:33 +00:00
|
|
|
* bindings to a separate KeyMap. This is used in the Shortcut preference
|
|
|
|
* dialog where main and user bind files are loaded separately so \unbind
|
|
|
|
* in user.bind can not nullify \bind in the master bind file.
|
|
|
|
*
|
|
|
|
* @param bind_file bind file
|
|
|
|
* @param unbind_map pointer to a KeyMap that holds \unbind bindings
|
2009-07-16 23:12:45 +00:00
|
|
|
* @param rt how to respond if the file can't be found
|
2022-11-25 10:30:20 +00:00
|
|
|
* @param i18n whether to search in localized folders
|
2007-10-20 20:35:33 +00:00
|
|
|
*/
|
2017-07-03 17:45:58 +00:00
|
|
|
bool read(std::string const & bind_file, KeyMap * unbind_map = 0,
|
2022-11-25 10:30:20 +00:00
|
|
|
BindReadType rt = Default, bool i18n = true);
|
2007-10-20 20:35:33 +00:00
|
|
|
|
|
|
|
/** write to a bind file.
|
|
|
|
* @param append append to the bind_file instead of overwrite it
|
|
|
|
* @param unbind use \unbind instead of \bind, indicating this KeyMap
|
|
|
|
* actually record unbind maps.
|
|
|
|
*/
|
2008-09-23 13:53:38 +00:00
|
|
|
void write(std::string const & bind_file, bool append, bool unbind = false) const;
|
2003-07-26 23:04:39 +00:00
|
|
|
|
2007-01-04 17:10:24 +00:00
|
|
|
/**
|
|
|
|
* print all available keysyms
|
2007-05-28 22:27:45 +00:00
|
|
|
* @param forgui true if the string should use translations and
|
|
|
|
* special characters.
|
2007-01-04 17:10:24 +00:00
|
|
|
*/
|
|
|
|
docstring const print(bool forgui) const;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
/**
|
|
|
|
* Look up a key press in the keymap.
|
|
|
|
* @param key the keysym
|
|
|
|
* @param mod the modifiers
|
|
|
|
* @param seq the current key sequence so far
|
2006-05-05 20:23:12 +00:00
|
|
|
* @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
|
2001-11-30 13:25:38 +00:00
|
|
|
*/
|
2003-09-21 23:00:47 +00:00
|
|
|
FuncRequest const &
|
2008-09-23 13:53:38 +00:00
|
|
|
lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2004-07-02 10:03:22 +00:00
|
|
|
///
|
2007-11-02 21:27:37 +00:00
|
|
|
typedef std::vector<KeySequence> Bindings;
|
2004-07-02 10:03:22 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Given an action, find all keybindings.
|
2007-11-02 21:27:37 +00:00
|
|
|
Bindings findBindings(FuncRequest const & func) const;
|
2004-07-02 10:03:22 +00:00
|
|
|
|
|
|
|
/// Given an action, print the keybindings.
|
2008-10-31 11:19:16 +00:00
|
|
|
docstring printBindings(FuncRequest const & func,
|
2019-03-09 11:00:40 +00:00
|
|
|
KeySequence::outputFormat format,
|
|
|
|
bool const untranslated = false) const;
|
2001-11-30 13:25:38 +00:00
|
|
|
|
2007-11-02 20:26:28 +00:00
|
|
|
struct Binding {
|
2008-09-23 13:53:38 +00:00
|
|
|
Binding(FuncRequest const & r, KeySequence const & s, ItemType t)
|
2007-11-02 20:26:28 +00:00
|
|
|
: request(r), sequence(s), tag(t) {}
|
|
|
|
FuncRequest request;
|
|
|
|
KeySequence sequence;
|
2008-09-23 13:53:38 +00:00
|
|
|
KeyMap::ItemType tag;
|
2017-07-03 17:45:58 +00:00
|
|
|
};
|
2007-10-18 15:48:51 +00:00
|
|
|
typedef std::vector<Binding> BindingList;
|
|
|
|
/**
|
|
|
|
* Return all lfun and their associated bindings.
|
|
|
|
* @param unbound list unbound (func without any keybinding) as well
|
2007-10-20 20:35:33 +00:00
|
|
|
* @param tag an optional tag to indicate the source of the bindinglist
|
2007-10-18 15:48:51 +00:00
|
|
|
*/
|
2008-09-23 13:53:38 +00:00
|
|
|
BindingList listBindings(bool unbound, ItemType tag = System) const;
|
2007-10-18 15:48:51 +00:00
|
|
|
|
2004-10-05 10:11:42 +00:00
|
|
|
/**
|
2004-07-07 09:32:19 +00:00
|
|
|
* Given an action, find the first 1-key binding (if it exists).
|
2007-04-27 08:43:38 +00:00
|
|
|
* The KeySymbol pointer is 0 is no key is found.
|
2004-07-07 09:32:19 +00:00
|
|
|
* [only used by the Qt/Mac frontend]
|
|
|
|
*/
|
2007-10-02 21:51:54 +00:00
|
|
|
std::pair<KeySymbol, KeyModifier>
|
2008-09-23 13:53:38 +00:00
|
|
|
find1keybinding(FuncRequest const & func) const;
|
2004-07-07 09:32:19 +00:00
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
/**
|
|
|
|
* Returns a string of the given keysym, with modifiers.
|
2002-06-18 15:44:30 +00:00
|
|
|
* @param key the key as a keysym
|
2001-11-30 13:25:38 +00:00
|
|
|
* @param mod the modifiers
|
|
|
|
*/
|
2007-04-27 08:43:38 +00:00
|
|
|
static std::string const printKeySym(KeySymbol const & key,
|
2007-10-02 21:51:54 +00:00
|
|
|
KeyModifier mod);
|
2001-11-30 13:25:38 +00:00
|
|
|
|
2008-03-21 22:31:57 +00:00
|
|
|
private:
|
|
|
|
///
|
2007-10-02 21:51:54 +00:00
|
|
|
typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
|
2002-06-10 07:57:39 +00:00
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
///
|
2007-04-27 08:05:58 +00:00
|
|
|
struct Key {
|
2000-02-04 09:38:32 +00:00
|
|
|
/// Keysym
|
2007-09-17 18:41:03 +00:00
|
|
|
KeySymbol code;
|
2000-02-04 09:38:32 +00:00
|
|
|
/// Modifier masks
|
2007-10-02 21:51:54 +00:00
|
|
|
ModifierPair mod;
|
2000-02-04 09:38:32 +00:00
|
|
|
/// Keymap for prefix keys
|
2016-06-02 17:13:55 +00:00
|
|
|
std::shared_ptr<KeyMap> prefixes;
|
2000-02-04 09:38:32 +00:00
|
|
|
/// Action for !prefix keys
|
2003-09-21 23:00:47 +00:00
|
|
|
FuncRequest func;
|
2000-02-04 09:38:32 +00:00
|
|
|
};
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2011-01-16 19:53:36 +00:00
|
|
|
enum ReturnValues {
|
|
|
|
ReadOK,
|
|
|
|
ReadError,
|
|
|
|
FileError,
|
|
|
|
FormatMismatch
|
|
|
|
};
|
2009-07-16 23:12:45 +00:00
|
|
|
///
|
|
|
|
bool read(support::FileName const & bind_file, KeyMap * unbind_map = 0);
|
2011-01-16 19:53:36 +00:00
|
|
|
///
|
|
|
|
ReturnValues readWithoutConv(support::FileName const & bind_file, KeyMap * unbind_map = 0);
|
2009-07-16 23:12:45 +00:00
|
|
|
|
2004-07-02 10:03:22 +00:00
|
|
|
/**
|
|
|
|
* Given an action, find all keybindings
|
|
|
|
* @param func the action
|
|
|
|
* @param prefix a sequence to prepend the results
|
|
|
|
*/
|
2007-11-02 21:27:37 +00:00
|
|
|
Bindings findBindings(FuncRequest const & func,
|
2008-09-23 13:53:38 +00:00
|
|
|
KeySequence const & prefix) const;
|
2017-07-03 17:45:58 +00:00
|
|
|
|
2007-10-20 20:35:33 +00:00
|
|
|
void listBindings(BindingList & list, KeySequence const & prefix,
|
2008-09-23 13:53:38 +00:00
|
|
|
ItemType tag) const;
|
2004-07-02 10:03:22 +00:00
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
/// is the table empty ?
|
2007-04-27 08:05:58 +00:00
|
|
|
bool empty() const { return table.empty(); }
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2007-10-18 15:48:51 +00:00
|
|
|
typedef std::vector<Key> Table;
|
|
|
|
///
|
2000-02-04 09:38:32 +00:00
|
|
|
Table table;
|
1999-09-27 18:44:28 +00:00
|
|
|
};
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
/// Implementation is in LyX.cpp
|
2007-04-27 08:05:58 +00:00
|
|
|
extern KeyMap & theTopLevelKeymap();
|
2006-10-19 07:20:32 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
} // namespace lyx
|
|
|
|
|
2007-04-27 08:05:58 +00:00
|
|
|
#endif // KEYMAP_H
|