2016-05-01 19:54:25 -07:00
|
|
|
// Header file for the low level input library.
|
2005-10-05 01:11:39 +10:00
|
|
|
#ifndef INPUT_COMMON_H
|
|
|
|
#define INPUT_COMMON_H
|
|
|
|
|
2022-08-20 23:14:48 -07:00
|
|
|
#include <unistd.h>
|
2019-10-13 15:50:48 -07:00
|
|
|
|
2022-08-20 23:14:48 -07:00
|
|
|
#include <cstdint>
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2019-04-28 22:05:03 -07:00
|
|
|
|
2019-10-13 15:50:48 -07:00
|
|
|
#include "common.h"
|
|
|
|
#include "maybe.h"
|
|
|
|
|
2019-03-16 17:56:35 -07:00
|
|
|
enum class readline_cmd_t {
|
2019-03-23 18:07:32 -07:00
|
|
|
beginning_of_line,
|
2019-03-23 17:32:39 -07:00
|
|
|
end_of_line,
|
|
|
|
forward_char,
|
|
|
|
backward_char,
|
2020-07-21 15:08:38 +02:00
|
|
|
forward_single_char,
|
2019-03-23 17:32:39 -07:00
|
|
|
forward_word,
|
|
|
|
backward_word,
|
|
|
|
forward_bigword,
|
|
|
|
backward_bigword,
|
2021-12-08 11:16:26 -05:00
|
|
|
nextd_or_forward_word,
|
|
|
|
prevd_or_backward_word,
|
2019-03-23 17:32:39 -07:00
|
|
|
history_search_backward,
|
|
|
|
history_search_forward,
|
2019-09-28 01:56:47 +10:00
|
|
|
history_prefix_search_backward,
|
|
|
|
history_prefix_search_forward,
|
2022-07-17 20:23:54 +02:00
|
|
|
history_pager,
|
2023-01-10 01:25:06 +01:00
|
|
|
history_pager_delete,
|
2019-03-23 17:32:39 -07:00
|
|
|
delete_char,
|
|
|
|
backward_delete_char,
|
|
|
|
kill_line,
|
|
|
|
yank,
|
2019-09-21 14:31:13 -07:00
|
|
|
yank_pop,
|
2019-03-23 17:32:39 -07:00
|
|
|
complete,
|
2019-06-16 14:38:27 -07:00
|
|
|
complete_and_search,
|
2019-03-23 17:32:39 -07:00
|
|
|
pager_toggle_search,
|
|
|
|
beginning_of_history,
|
|
|
|
end_of_history,
|
|
|
|
backward_kill_line,
|
|
|
|
kill_whole_line,
|
2022-05-30 22:12:24 -05:00
|
|
|
kill_inner_line,
|
2019-03-23 17:32:39 -07:00
|
|
|
kill_word,
|
|
|
|
kill_bigword,
|
|
|
|
backward_kill_word,
|
|
|
|
backward_kill_path_component,
|
|
|
|
backward_kill_bigword,
|
|
|
|
history_token_search_backward,
|
|
|
|
history_token_search_forward,
|
|
|
|
self_insert,
|
2020-03-04 14:10:24 -08:00
|
|
|
self_insert_notfirst,
|
2019-03-23 17:32:39 -07:00
|
|
|
transpose_chars,
|
|
|
|
transpose_words,
|
|
|
|
upcase_word,
|
|
|
|
downcase_word,
|
|
|
|
capitalize_word,
|
2020-04-16 23:24:25 -04:00
|
|
|
togglecase_char,
|
|
|
|
togglecase_selection,
|
2019-03-23 17:32:39 -07:00
|
|
|
execute,
|
|
|
|
beginning_of_buffer,
|
|
|
|
end_of_buffer,
|
2019-04-01 15:52:21 +02:00
|
|
|
repaint_mode,
|
2019-03-23 17:32:39 -07:00
|
|
|
repaint,
|
|
|
|
force_repaint,
|
|
|
|
up_line,
|
|
|
|
down_line,
|
|
|
|
suppress_autosuggestion,
|
|
|
|
accept_autosuggestion,
|
|
|
|
begin_selection,
|
|
|
|
swap_selection_start_stop,
|
|
|
|
end_selection,
|
|
|
|
kill_selection,
|
2020-12-19 14:26:13 -08:00
|
|
|
insert_line_under,
|
2020-12-19 14:31:33 -08:00
|
|
|
insert_line_over,
|
2019-03-23 17:32:39 -07:00
|
|
|
forward_jump,
|
|
|
|
backward_jump,
|
|
|
|
forward_jump_till,
|
|
|
|
backward_jump_till,
|
|
|
|
func_and,
|
2020-07-22 19:18:24 +02:00
|
|
|
func_or,
|
2019-04-01 15:59:15 +02:00
|
|
|
expand_abbr,
|
2019-06-05 19:02:32 +02:00
|
|
|
delete_or_exit,
|
2021-01-04 09:45:34 +01:00
|
|
|
exit,
|
2020-07-01 20:54:13 +02:00
|
|
|
cancel_commandline,
|
2019-03-23 17:32:39 -07:00
|
|
|
cancel,
|
2020-02-04 12:47:44 +01:00
|
|
|
undo,
|
|
|
|
redo,
|
2021-01-05 15:40:09 -06:00
|
|
|
begin_undo_group,
|
|
|
|
end_undo_group,
|
2019-03-23 17:32:39 -07:00
|
|
|
repeat_jump,
|
2021-02-06 17:13:27 -06:00
|
|
|
disable_mouse_tracking,
|
2019-05-29 20:36:01 +02:00
|
|
|
// NOTE: This one has to be last.
|
|
|
|
reverse_repeat_jump
|
2019-03-16 17:56:35 -07:00
|
|
|
};
|
2018-01-29 11:52:55 -08:00
|
|
|
|
2019-03-16 17:56:35 -07:00
|
|
|
// The range of key codes for inputrc-style keyboard functions.
|
2019-06-03 20:30:48 -07:00
|
|
|
enum { R_END_INPUT_FUNCTIONS = static_cast<int>(readline_cmd_t::reverse_repeat_jump) + 1 };
|
2005-09-20 23:26:39 +10:00
|
|
|
|
2019-03-15 01:11:15 -07:00
|
|
|
/// Represents an event on the character input stream.
|
2019-03-16 17:56:35 -07:00
|
|
|
enum class char_event_type_t : uint8_t {
|
2019-03-15 01:11:15 -07:00
|
|
|
/// A character was entered.
|
|
|
|
charc,
|
|
|
|
|
2019-03-16 16:48:23 -07:00
|
|
|
/// A readline event.
|
|
|
|
readline,
|
|
|
|
|
2019-03-16 12:35:49 -07:00
|
|
|
/// end-of-file was reached.
|
2019-03-16 15:49:35 -07:00
|
|
|
eof,
|
|
|
|
|
|
|
|
/// An event was handled internally, or an interrupt was received. Check to see if the reader
|
|
|
|
/// loop should exit.
|
|
|
|
check_exit,
|
2019-03-15 01:11:15 -07:00
|
|
|
};
|
|
|
|
|
2020-03-07 13:55:19 -08:00
|
|
|
/// Hackish: the input style, which describes how char events (only) are applied to the command
|
|
|
|
/// line. Note this is set only after applying bindings; it is not set from readb().
|
|
|
|
enum class char_input_style_t : uint8_t {
|
|
|
|
// Insert characters normally.
|
|
|
|
normal,
|
|
|
|
|
|
|
|
// Insert characters only if the cursor is not at the beginning. Otherwise, discard them.
|
|
|
|
notfirst,
|
|
|
|
};
|
|
|
|
|
2019-03-15 01:11:15 -07:00
|
|
|
class char_event_t {
|
2019-03-16 17:56:35 -07:00
|
|
|
union {
|
|
|
|
/// Set if the type is charc.
|
|
|
|
wchar_t c;
|
|
|
|
|
|
|
|
/// Set if the type is readline.
|
|
|
|
readline_cmd_t rl;
|
|
|
|
} v_{};
|
2019-03-15 01:11:15 -07:00
|
|
|
|
|
|
|
public:
|
2019-09-21 15:57:21 -07:00
|
|
|
/// The type of event.
|
2019-03-15 01:11:15 -07:00
|
|
|
char_event_type_t type;
|
|
|
|
|
2020-03-07 13:55:19 -08:00
|
|
|
/// The style to use when inserting characters into the command line.
|
|
|
|
char_input_style_t input_style{char_input_style_t::normal};
|
2020-03-04 14:10:24 -08:00
|
|
|
|
2019-09-21 15:57:21 -07:00
|
|
|
/// The sequence of characters in the input mapping which generated this event.
|
|
|
|
/// Note that the generic self-insert case does not have any characters, so this would be empty.
|
|
|
|
wcstring seq{};
|
|
|
|
|
2019-03-15 01:11:15 -07:00
|
|
|
bool is_char() const { return type == char_event_type_t::charc; }
|
|
|
|
|
2019-03-16 12:35:49 -07:00
|
|
|
bool is_eof() const { return type == char_event_type_t::eof; }
|
|
|
|
|
2019-03-16 15:49:35 -07:00
|
|
|
bool is_check_exit() const { return type == char_event_type_t::check_exit; }
|
|
|
|
|
2019-03-16 16:48:23 -07:00
|
|
|
bool is_readline() const { return type == char_event_type_t::readline; }
|
2019-03-15 01:11:15 -07:00
|
|
|
|
|
|
|
wchar_t get_char() const {
|
|
|
|
assert(type == char_event_type_t::charc && "Not a char type");
|
2019-03-16 17:56:35 -07:00
|
|
|
return v_.c;
|
2019-03-15 01:11:15 -07:00
|
|
|
}
|
|
|
|
|
2021-02-06 17:11:05 -06:00
|
|
|
maybe_t<wchar_t> maybe_char() const {
|
|
|
|
if (type == char_event_type_t::charc) {
|
|
|
|
return v_.c;
|
|
|
|
} else {
|
|
|
|
return none();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 17:56:35 -07:00
|
|
|
readline_cmd_t get_readline() const {
|
2019-03-16 16:48:23 -07:00
|
|
|
assert(type == char_event_type_t::readline && "Not a readline type");
|
2019-03-16 17:56:35 -07:00
|
|
|
return v_.rl;
|
2019-03-16 16:48:23 -07:00
|
|
|
}
|
|
|
|
|
2019-03-16 17:56:35 -07:00
|
|
|
/* implicit */ char_event_t(wchar_t c) : type(char_event_type_t::charc) { v_.c = c; }
|
|
|
|
|
2019-09-21 15:57:21 -07:00
|
|
|
/* implicit */ char_event_t(readline_cmd_t rl, wcstring seq = {})
|
|
|
|
: type(char_event_type_t::readline), seq(std::move(seq)) {
|
2019-03-16 17:56:35 -07:00
|
|
|
v_.rl = rl;
|
|
|
|
}
|
2019-03-15 01:11:15 -07:00
|
|
|
|
2019-03-16 17:56:35 -07:00
|
|
|
/* implicit */ char_event_t(char_event_type_t type) : type(type) {
|
2019-03-16 16:48:23 -07:00
|
|
|
assert(type != char_event_type_t::charc && type != char_event_type_t::readline &&
|
2019-03-15 01:11:15 -07:00
|
|
|
"Cannot create a char event with this constructor");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-01 19:54:25 -07:00
|
|
|
/// Adjust the escape timeout.
|
2019-03-15 01:11:15 -07:00
|
|
|
class environment_t;
|
2021-04-10 17:34:28 -07:00
|
|
|
void update_wait_on_escape_ms(const environment_t &vars);
|
2023-05-16 20:51:34 -05:00
|
|
|
void update_wait_on_escape_ms_ffi(std::unique_ptr<env_var_t> fish_escape_delay_ms);
|
2016-01-14 21:46:53 -08:00
|
|
|
|
2023-08-31 00:12:22 +03:00
|
|
|
void update_wait_on_sequence_key_ms(const environment_t &vars);
|
|
|
|
void update_wait_on_sequence_key_ms_ffi(std::unique_ptr<env_var_t> fish_sequence_key_delay_ms);
|
|
|
|
|
2019-06-02 15:41:23 -07:00
|
|
|
/// A class which knows how to produce a stream of input events.
|
2021-04-10 19:49:56 -07:00
|
|
|
/// This is a base class; you may subclass it for its override points.
|
2019-06-02 15:41:23 -07:00
|
|
|
class input_event_queue_t {
|
|
|
|
public:
|
2021-04-06 20:01:32 -07:00
|
|
|
/// Construct from a file descriptor \p in, and an interrupt handler \p handler.
|
2021-04-10 19:49:56 -07:00
|
|
|
explicit input_event_queue_t(int in = STDIN_FILENO);
|
2020-09-30 18:29:00 +02:00
|
|
|
|
2019-06-02 15:41:23 -07:00
|
|
|
/// Function used by input_readch to read bytes from stdin until enough bytes have been read to
|
|
|
|
/// convert them to a wchar_t. Conversion is done using mbrtowc. If a character has previously
|
2021-04-10 13:40:53 -07:00
|
|
|
/// been read and then 'unread' using \c input_common_unreadch, that character is returned.
|
2019-06-02 15:41:23 -07:00
|
|
|
char_event_t readch();
|
|
|
|
|
|
|
|
/// Like readch(), except it will wait at most WAIT_ON_ESCAPE milliseconds for a
|
|
|
|
/// character to be available for reading.
|
2021-04-10 13:40:53 -07:00
|
|
|
/// \return none on timeout, the event on success.
|
2023-08-31 00:12:22 +03:00
|
|
|
maybe_t<char_event_t> readch_timed(const int wait_time_ms);
|
|
|
|
|
|
|
|
maybe_t<char_event_t> readch_timed_esc();
|
|
|
|
maybe_t<char_event_t> readch_timed_sequence_key();
|
2019-06-02 15:41:23 -07:00
|
|
|
|
|
|
|
/// Enqueue a character or a readline function to the queue of unread characters that
|
|
|
|
/// readch will return before actually reading from fd 0.
|
2021-04-05 22:36:55 -07:00
|
|
|
void push_back(const char_event_t &ch);
|
2019-06-02 15:41:23 -07:00
|
|
|
|
|
|
|
/// Add a character or a readline function to the front of the queue of unread characters. This
|
|
|
|
/// will be the next character returned by readch.
|
2021-04-05 22:36:55 -07:00
|
|
|
void push_front(const char_event_t &ch);
|
2021-02-06 17:05:46 -06:00
|
|
|
|
2022-01-30 13:15:50 -08:00
|
|
|
/// Find the first sequence of non-char events, and promote them to the front.
|
|
|
|
void promote_interruptions_to_front();
|
|
|
|
|
2021-02-06 17:05:46 -06:00
|
|
|
/// Add multiple characters or readline events to the front of the queue of unread characters.
|
|
|
|
/// The order of the provided events is not changed, i.e. they are not inserted in reverse
|
|
|
|
/// order.
|
2021-04-10 17:34:28 -07:00
|
|
|
template <typename Iterator>
|
2021-02-06 17:05:46 -06:00
|
|
|
void insert_front(const Iterator begin, const Iterator end) {
|
|
|
|
queue_.insert(queue_.begin(), begin, end);
|
|
|
|
}
|
2021-04-05 22:36:55 -07:00
|
|
|
|
2023-01-29 11:02:26 +01:00
|
|
|
/// Forget all enqueued readline events in the front of the queue.
|
|
|
|
void drop_leading_readline_events();
|
|
|
|
|
2021-04-10 20:41:33 -07:00
|
|
|
/// Override point for when we are about to (potentially) block in select(). The default does
|
|
|
|
/// nothing.
|
|
|
|
virtual void prepare_to_select();
|
|
|
|
|
2021-04-10 19:49:56 -07:00
|
|
|
/// Override point for when when select() is interrupted by a signal. The default does nothing.
|
|
|
|
virtual void select_interrupted();
|
|
|
|
|
2022-03-27 18:59:34 -07:00
|
|
|
/// Override point for when when select() is interrupted by the universal variable notifier.
|
|
|
|
/// The default does nothing.
|
|
|
|
virtual void uvar_change_notified();
|
|
|
|
|
2021-04-10 19:49:56 -07:00
|
|
|
virtual ~input_event_queue_t();
|
|
|
|
|
2021-04-05 22:36:55 -07:00
|
|
|
private:
|
|
|
|
/// \return if we have any lookahead.
|
|
|
|
bool has_lookahead() const { return !queue_.empty(); }
|
|
|
|
|
2021-04-10 13:40:53 -07:00
|
|
|
/// \return the next event in the queue, or none if the queue is empty.
|
|
|
|
maybe_t<char_event_t> try_pop();
|
2021-04-05 22:36:55 -07:00
|
|
|
|
|
|
|
int in_{0};
|
|
|
|
std::deque<char_event_t> queue_;
|
2019-06-02 15:41:23 -07:00
|
|
|
};
|
2005-09-20 23:26:39 +10:00
|
|
|
|
|
|
|
#endif
|