2016-05-02 10:54:25 +08:00
|
|
|
// Header file for the low level input library.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef INPUT_COMMON_H
|
|
|
|
#define INPUT_COMMON_H
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <stddef.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2017-01-27 12:00:43 +08:00
|
|
|
#include <functional>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-01-22 11:56:39 +08:00
|
|
|
#include "common.h"
|
2019-03-15 16:11:15 +08:00
|
|
|
#include "maybe.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-03-17 08:56:35 +08:00
|
|
|
enum class readline_cmd_t {
|
2019-03-24 09:07:32 +08:00
|
|
|
beginning_of_line,
|
2019-03-24 08:32:39 +08:00
|
|
|
end_of_line,
|
|
|
|
forward_char,
|
|
|
|
backward_char,
|
|
|
|
forward_word,
|
|
|
|
backward_word,
|
|
|
|
forward_bigword,
|
|
|
|
backward_bigword,
|
|
|
|
history_search_backward,
|
|
|
|
history_search_forward,
|
|
|
|
delete_char,
|
|
|
|
backward_delete_char,
|
|
|
|
kill_line,
|
|
|
|
yank,
|
|
|
|
yank_POP,
|
|
|
|
complete,
|
|
|
|
complete_AND_SEARCH,
|
|
|
|
pager_toggle_search,
|
|
|
|
beginning_of_history,
|
|
|
|
end_of_history,
|
|
|
|
backward_kill_line,
|
|
|
|
kill_whole_line,
|
|
|
|
kill_word,
|
|
|
|
kill_bigword,
|
|
|
|
backward_kill_word,
|
|
|
|
backward_kill_path_component,
|
|
|
|
backward_kill_bigword,
|
|
|
|
history_token_search_backward,
|
|
|
|
history_token_search_forward,
|
|
|
|
self_insert,
|
|
|
|
transpose_chars,
|
|
|
|
transpose_words,
|
|
|
|
upcase_word,
|
|
|
|
downcase_word,
|
|
|
|
capitalize_word,
|
|
|
|
vi_arg_digit,
|
|
|
|
vi_delete_to,
|
|
|
|
execute,
|
|
|
|
beginning_of_buffer,
|
|
|
|
end_of_buffer,
|
|
|
|
repaint,
|
|
|
|
force_repaint,
|
|
|
|
up_line,
|
|
|
|
down_line,
|
|
|
|
suppress_autosuggestion,
|
|
|
|
accept_autosuggestion,
|
|
|
|
begin_selection,
|
|
|
|
swap_selection_start_stop,
|
|
|
|
end_selection,
|
|
|
|
kill_selection,
|
|
|
|
forward_jump,
|
|
|
|
backward_jump,
|
|
|
|
forward_jump_till,
|
|
|
|
backward_jump_till,
|
|
|
|
func_and,
|
2019-04-01 21:59:15 +08:00
|
|
|
expand_abbr,
|
2019-03-24 08:32:39 +08:00
|
|
|
cancel,
|
|
|
|
repeat_jump,
|
|
|
|
reverse_repeat_jump,
|
2019-03-17 08:56:35 +08:00
|
|
|
};
|
2018-01-30 03:52:55 +08:00
|
|
|
|
2019-03-17 08:56:35 +08:00
|
|
|
// The range of key codes for inputrc-style keyboard functions.
|
|
|
|
enum {
|
2019-03-24 08:32:39 +08:00
|
|
|
R_BEGIN_INPUT_FUNCTIONS = static_cast<int>(readline_cmd_t::beginning_of_line),
|
|
|
|
R_END_INPUT_FUNCTIONS = static_cast<int>(readline_cmd_t::reverse_repeat_jump) + 1
|
2016-01-22 11:56:39 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
/// Represents an event on the character input stream.
|
2019-03-17 08:56:35 +08:00
|
|
|
enum class char_event_type_t : uint8_t {
|
2019-03-15 16:11:15 +08:00
|
|
|
/// A character was entered.
|
|
|
|
charc,
|
|
|
|
|
2019-03-17 07:48:23 +08:00
|
|
|
/// A readline event.
|
|
|
|
readline,
|
|
|
|
|
2019-03-15 16:11:15 +08:00
|
|
|
/// A timeout was hit.
|
|
|
|
timeout,
|
2019-03-17 03:35:49 +08:00
|
|
|
|
|
|
|
/// end-of-file was reached.
|
2019-03-17 06:49:35 +08: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 16:11:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class char_event_t {
|
2019-03-17 08:56:35 +08: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 16:11:15 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
char_event_type_t type;
|
|
|
|
|
|
|
|
bool is_timeout() const { return type == char_event_type_t::timeout; }
|
|
|
|
|
|
|
|
bool is_char() const { return type == char_event_type_t::charc; }
|
|
|
|
|
2019-03-17 03:35:49 +08:00
|
|
|
bool is_eof() const { return type == char_event_type_t::eof; }
|
|
|
|
|
2019-03-17 06:49:35 +08:00
|
|
|
bool is_check_exit() const { return type == char_event_type_t::check_exit; }
|
|
|
|
|
2019-03-17 07:48:23 +08:00
|
|
|
bool is_readline() const { return type == char_event_type_t::readline; }
|
2019-03-15 16:11:15 +08:00
|
|
|
|
|
|
|
wchar_t get_char() const {
|
|
|
|
assert(type == char_event_type_t::charc && "Not a char type");
|
2019-03-17 08:56:35 +08:00
|
|
|
return v_.c;
|
2019-03-15 16:11:15 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 08:56:35 +08:00
|
|
|
readline_cmd_t get_readline() const {
|
2019-03-17 07:48:23 +08:00
|
|
|
assert(type == char_event_type_t::readline && "Not a readline type");
|
2019-03-17 08:56:35 +08:00
|
|
|
return v_.rl;
|
2019-03-17 07:48:23 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 08:56:35 +08:00
|
|
|
/* implicit */ char_event_t(wchar_t c) : type(char_event_type_t::charc) { v_.c = c; }
|
|
|
|
|
|
|
|
/* implicit */ char_event_t(readline_cmd_t rl) : type(char_event_type_t::readline) {
|
|
|
|
v_.rl = rl;
|
|
|
|
}
|
2019-03-15 16:11:15 +08:00
|
|
|
|
2019-03-17 08:56:35 +08:00
|
|
|
/* implicit */ char_event_t(char_event_type_t type) : type(type) {
|
2019-03-17 07:48:23 +08:00
|
|
|
assert(type != char_event_type_t::charc && type != char_event_type_t::readline &&
|
2019-03-15 16:11:15 +08:00
|
|
|
"Cannot create a char event with this constructor");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-17 04:52:07 +08:00
|
|
|
/// A type of function invoked on interrupt.
|
|
|
|
/// \return the event which is to be returned to the reader loop, or none if VINTR is 0.
|
2019-03-17 05:45:36 +08:00
|
|
|
using interrupt_func_t = maybe_t<char_event_t> (*)();
|
2019-03-17 04:52:07 +08:00
|
|
|
|
|
|
|
/// Init the library with an interrupt function.
|
|
|
|
void input_common_init(interrupt_func_t func);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
/// Adjust the escape timeout.
|
2019-03-15 16:11:15 +08:00
|
|
|
class environment_t;
|
2018-09-18 12:26:21 +08:00
|
|
|
void update_wait_on_escape_ms(const environment_t &vars);
|
2016-01-15 13:46:53 +08:00
|
|
|
|
2016-05-02 10:54:25 +08: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 been
|
2019-03-15 16:11:15 +08:00
|
|
|
/// read and then 'unread' using \c input_common_unreadch, that character is returned.
|
|
|
|
/// This function never returns a timeout.
|
|
|
|
char_event_t input_common_readch();
|
|
|
|
|
|
|
|
/// Like input_common_readch(), except it will wait at most WAIT_ON_ESCAPE milliseconds for a
|
|
|
|
/// character to be available for reading.
|
|
|
|
/// If \p dequeue_timeouts is set, remove any timeout from the queue; otherwise retain them.
|
|
|
|
char_event_t input_common_readch_timed(bool dequeue_timeouts = false);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
/// Enqueue a character or a readline function to the queue of unread characters that input_readch
|
|
|
|
/// will return before actually reading from fd 0.
|
2019-03-15 16:11:15 +08:00
|
|
|
void input_common_queue_ch(char_event_t ch);
|
2015-04-06 02:07:17 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
/// Add a character or a readline function to the front of the queue of unread characters. This
|
|
|
|
/// will be the first character returned by input_readch (unless this function is called more than
|
|
|
|
/// once).
|
2019-03-15 16:11:15 +08:00
|
|
|
void input_common_next_ch(char_event_t ch);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
/// Adds a callback to be invoked at the next turn of the "event loop." The callback function will
|
|
|
|
/// be invoked and passed arg.
|
2017-01-22 09:15:45 +08:00
|
|
|
void input_common_add_callback(std::function<void(void)>);
|
2013-04-04 04:49:58 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#endif
|