2016-05-01 12:31:25 +08:00
|
|
|
// Functions for reading a character of input from stdin, using the inputrc information for key
|
|
|
|
// bindings.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_INPUT_H
|
|
|
|
#define FISH_INPUT_H
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <stddef.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common.h"
|
2019-03-15 16:11:15 +08:00
|
|
|
#include "input_common.h"
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2014-01-19 17:27:39 +08:00
|
|
|
#define FISH_BIND_MODE_VAR L"fish_bind_mode"
|
2019-06-03 08:49:52 +08:00
|
|
|
#define DEFAULT_BIND_MODE L"default"
|
2013-12-31 08:52:41 +08:00
|
|
|
|
2021-04-11 04:13:31 +08:00
|
|
|
class event_queue_peeker_t;
|
2019-06-03 08:49:52 +08:00
|
|
|
class parser_t;
|
2018-09-14 15:36:26 +08:00
|
|
|
|
2014-02-13 04:49:32 +08:00
|
|
|
wcstring describe_char(wint_t c);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-02-16 12:09:26 +08:00
|
|
|
/// Set up arrays used by readch to detect escape sequences for special keys and perform related
|
|
|
|
/// initializations for our input subsystem.
|
|
|
|
void init_input();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-06-03 06:41:23 +08:00
|
|
|
struct input_mapping_t;
|
2021-04-11 10:49:56 +08:00
|
|
|
class inputter_t final : private input_event_queue_t {
|
2019-06-03 06:41:23 +08:00
|
|
|
public:
|
2020-11-15 11:41:24 +08:00
|
|
|
/// Construct from a parser, and the fd from which to read.
|
|
|
|
explicit inputter_t(parser_t &parser, int in = STDIN_FILENO);
|
2019-06-03 06:41:23 +08:00
|
|
|
|
2020-10-01 00:29:00 +08:00
|
|
|
/// Read a character from stdin. Try to convert some escape sequences into character constants,
|
2019-06-03 06:41:23 +08:00
|
|
|
/// but do not permanently block the escape character.
|
|
|
|
///
|
|
|
|
/// This is performed in the same way vim does it, i.e. if an escape character is read, wait for
|
|
|
|
/// more input for a short time (a few milliseconds). If more input is avaialable, it is assumed
|
|
|
|
/// to be an escape sequence for a special character (such as an arrow key), and readch attempts
|
|
|
|
/// to parse it. If no more input follows after the escape key, it is assumed to be an actual
|
|
|
|
/// escape key press, and is returned as such.
|
|
|
|
///
|
2020-11-16 04:26:06 +08:00
|
|
|
/// \p command_handler is used to run commands. If empty (in the std::function sense), when a
|
|
|
|
/// character is encountered that would invoke a fish command, it is unread and
|
|
|
|
/// char_event_type_t::check_exit is returned. Note the handler is not stored.
|
|
|
|
using command_handler_t = std::function<void(const wcstring_list_t &)>;
|
2021-04-11 10:49:56 +08:00
|
|
|
char_event_t read_char(const command_handler_t &command_handler = {});
|
2019-06-03 06:41:23 +08:00
|
|
|
|
|
|
|
/// Enqueue a char event to the queue of unread characters that input_readch will return before
|
|
|
|
/// actually reading from fd 0.
|
2021-04-11 10:49:56 +08:00
|
|
|
void queue_char(const char_event_t &ch);
|
2019-06-03 06:41:23 +08:00
|
|
|
|
|
|
|
/// Sets the return status of the most recently executed input function.
|
|
|
|
void function_set_status(bool status) { function_status_ = status; }
|
|
|
|
|
|
|
|
/// Pop an argument from the function argument stack.
|
|
|
|
wchar_t function_pop_arg();
|
2020-11-16 03:29:10 +08:00
|
|
|
|
|
|
|
private:
|
2021-04-11 11:41:33 +08:00
|
|
|
// Called right before potentially blocking in select().
|
|
|
|
void prepare_to_select() override;
|
|
|
|
|
2021-04-11 10:49:56 +08:00
|
|
|
// Called when select() is interrupted by a signal.
|
|
|
|
void select_interrupted() override;
|
|
|
|
|
2020-11-16 03:29:10 +08:00
|
|
|
void function_push_arg(wchar_t arg);
|
|
|
|
void function_push_args(readline_cmd_t code);
|
2020-11-16 04:26:06 +08:00
|
|
|
void mapping_execute(const input_mapping_t &m, const command_handler_t &command_handler);
|
|
|
|
void mapping_execute_matching_or_generic(const command_handler_t &command_handler);
|
2021-04-11 04:13:31 +08:00
|
|
|
maybe_t<input_mapping_t> find_mapping(event_queue_peeker_t *peeker);
|
2020-11-16 03:29:10 +08:00
|
|
|
char_event_t read_characters_no_readline();
|
2021-08-11 03:59:56 +08:00
|
|
|
|
|
|
|
// We need a parser to evaluate bindings.
|
|
|
|
const std::shared_ptr<parser_t> parser_;
|
|
|
|
|
|
|
|
std::vector<wchar_t> input_function_args_{};
|
|
|
|
bool function_status_{false};
|
|
|
|
|
|
|
|
// Transient storage to avoid repeated allocations.
|
|
|
|
std::vector<char_event_t> event_storage_{};
|
2019-06-03 06:41:23 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2014-09-23 12:04:06 +08:00
|
|
|
struct input_mapping_name_t {
|
|
|
|
wcstring seq;
|
|
|
|
wcstring mode;
|
|
|
|
};
|
|
|
|
|
2019-06-03 13:36:11 +08:00
|
|
|
/// The input mapping set is the set of mappings from character sequences to commands.
|
|
|
|
class input_mapping_set_t {
|
|
|
|
friend acquired_lock<input_mapping_set_t> input_mappings();
|
|
|
|
friend void init_input();
|
|
|
|
|
|
|
|
using mapping_list_t = std::vector<input_mapping_t>;
|
|
|
|
|
|
|
|
mapping_list_t mapping_list_;
|
|
|
|
mapping_list_t preset_mapping_list_;
|
|
|
|
std::shared_ptr<const mapping_list_t> all_mappings_cache_;
|
|
|
|
|
|
|
|
input_mapping_set_t();
|
2018-09-18 17:52:25 +08:00
|
|
|
|
2019-06-03 13:36:11 +08:00
|
|
|
public:
|
|
|
|
~input_mapping_set_t();
|
|
|
|
|
|
|
|
/// Erase all bindings.
|
2019-11-19 10:34:50 +08:00
|
|
|
void clear(const wchar_t *mode = nullptr, bool user = true);
|
2019-06-03 13:36:11 +08:00
|
|
|
|
|
|
|
/// Erase binding for specified key sequence.
|
|
|
|
bool erase(const wcstring &sequence, const wcstring &mode = DEFAULT_BIND_MODE,
|
|
|
|
bool user = true);
|
2007-09-26 00:14:47 +08:00
|
|
|
|
2019-06-03 13:36:11 +08:00
|
|
|
/// Gets the command bound to the specified key sequence in the specified mode. Returns true if
|
|
|
|
/// it exists, false if not.
|
|
|
|
bool get(const wcstring &sequence, const wcstring &mode, wcstring_list_t *out_cmds, bool user,
|
2020-03-14 04:59:10 +08:00
|
|
|
wcstring *out_sets_mode) const;
|
2007-09-26 00:14:47 +08:00
|
|
|
|
2019-06-03 13:36:11 +08:00
|
|
|
/// Returns all mapping names and modes.
|
|
|
|
std::vector<input_mapping_name_t> get_names(bool user = true) const;
|
|
|
|
|
|
|
|
/// Add a key mapping from the specified sequence to the specified command.
|
|
|
|
///
|
|
|
|
/// \param sequence the sequence to bind
|
|
|
|
/// \param command an input function that will be run whenever the key sequence occurs
|
2019-09-15 07:36:57 +08:00
|
|
|
void add(wcstring sequence, const wchar_t *command, const wchar_t *mode = DEFAULT_BIND_MODE,
|
2019-11-19 08:54:36 +08:00
|
|
|
const wchar_t *sets_mode = DEFAULT_BIND_MODE, bool user = true);
|
2019-06-03 13:36:11 +08:00
|
|
|
|
2019-09-15 07:36:57 +08:00
|
|
|
void add(wcstring sequence, const wchar_t *const *commands, size_t commands_len,
|
2019-11-19 08:54:36 +08:00
|
|
|
const wchar_t *mode = DEFAULT_BIND_MODE, const wchar_t *sets_mode = DEFAULT_BIND_MODE,
|
2019-06-03 13:36:11 +08:00
|
|
|
bool user = true);
|
|
|
|
|
|
|
|
/// \return a snapshot of the list of input mappings.
|
|
|
|
std::shared_ptr<const mapping_list_t> all_mappings();
|
|
|
|
};
|
2013-12-31 08:52:41 +08:00
|
|
|
|
2019-06-03 13:36:11 +08:00
|
|
|
/// Access the singleton input mapping set.
|
|
|
|
acquired_lock<input_mapping_set_t> input_mappings();
|
2014-01-23 17:23:04 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Return the sequence for the terminfo variable of the specified name.
|
|
|
|
///
|
|
|
|
/// If no terminfo variable of the specified name could be found, return false and set errno to
|
|
|
|
/// ENOENT. If the terminfo variable does not have a value, return false and set errno to EILSEQ.
|
2019-09-15 07:36:57 +08:00
|
|
|
bool input_terminfo_get_sequence(const wcstring &name, wcstring *out_seq);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Return the name of the terminfo variable with the specified sequence, in out_name. Returns true
|
|
|
|
/// if found, false if not found.
|
2014-07-08 01:45:26 +08:00
|
|
|
bool input_terminfo_get_name(const wcstring &seq, wcstring *out_name);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Return a list of all known terminfo names.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t input_terminfo_get_names(bool skip_null);
|
2007-09-26 00:14:47 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Returns the input function code for the given input function name.
|
2019-03-17 08:56:35 +08:00
|
|
|
maybe_t<readline_cmd_t> input_function_get_code(const wcstring &name);
|
2007-09-26 00:14:47 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Returns a list of all existing input function names.
|
2021-02-08 06:59:07 +08:00
|
|
|
const wcstring_list_t &input_function_get_names(void);
|
2007-09-26 00:14:47 +08:00
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|