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
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
enum {
|
2016-01-22 11:56:39 +08:00
|
|
|
R_MIN = INPUT_COMMON_BASE,
|
2016-05-02 10:54:25 +08:00
|
|
|
// R_NULL is sometimes returned by the input when a character was requested but none could be
|
|
|
|
// delivered, or when an exception happened.
|
2016-01-22 11:56:39 +08:00
|
|
|
R_NULL = R_MIN,
|
2018-01-30 03:52:55 +08:00
|
|
|
|
2016-01-22 11:56:39 +08:00
|
|
|
R_BEGINNING_OF_LINE,
|
|
|
|
R_END_OF_LINE,
|
|
|
|
R_FORWARD_CHAR,
|
|
|
|
R_BACKWARD_CHAR,
|
|
|
|
R_FORWARD_WORD,
|
|
|
|
R_BACKWARD_WORD,
|
|
|
|
R_FORWARD_BIGWORD,
|
|
|
|
R_BACKWARD_BIGWORD,
|
|
|
|
R_HISTORY_SEARCH_BACKWARD,
|
|
|
|
R_HISTORY_SEARCH_FORWARD,
|
|
|
|
R_DELETE_CHAR,
|
|
|
|
R_BACKWARD_DELETE_CHAR,
|
|
|
|
R_KILL_LINE,
|
|
|
|
R_YANK,
|
|
|
|
R_YANK_POP,
|
|
|
|
R_COMPLETE,
|
|
|
|
R_COMPLETE_AND_SEARCH,
|
2018-01-31 01:39:04 +08:00
|
|
|
R_PAGER_TOGGLE_SEARCH,
|
2016-01-22 11:56:39 +08:00
|
|
|
R_BEGINNING_OF_HISTORY,
|
|
|
|
R_END_OF_HISTORY,
|
|
|
|
R_BACKWARD_KILL_LINE,
|
|
|
|
R_KILL_WHOLE_LINE,
|
|
|
|
R_KILL_WORD,
|
|
|
|
R_KILL_BIGWORD,
|
|
|
|
R_BACKWARD_KILL_WORD,
|
|
|
|
R_BACKWARD_KILL_PATH_COMPONENT,
|
|
|
|
R_BACKWARD_KILL_BIGWORD,
|
|
|
|
R_HISTORY_TOKEN_SEARCH_BACKWARD,
|
|
|
|
R_HISTORY_TOKEN_SEARCH_FORWARD,
|
|
|
|
R_SELF_INSERT,
|
|
|
|
R_TRANSPOSE_CHARS,
|
|
|
|
R_TRANSPOSE_WORDS,
|
|
|
|
R_UPCASE_WORD,
|
|
|
|
R_DOWNCASE_WORD,
|
|
|
|
R_CAPITALIZE_WORD,
|
|
|
|
R_VI_ARG_DIGIT,
|
|
|
|
R_VI_DELETE_TO,
|
|
|
|
R_EXECUTE,
|
|
|
|
R_BEGINNING_OF_BUFFER,
|
|
|
|
R_END_OF_BUFFER,
|
|
|
|
R_REPAINT,
|
|
|
|
R_FORCE_REPAINT,
|
|
|
|
R_UP_LINE,
|
|
|
|
R_DOWN_LINE,
|
|
|
|
R_SUPPRESS_AUTOSUGGESTION,
|
|
|
|
R_ACCEPT_AUTOSUGGESTION,
|
|
|
|
R_BEGIN_SELECTION,
|
2016-03-20 09:08:23 +08:00
|
|
|
R_SWAP_SELECTION_START_STOP,
|
2016-01-22 11:56:39 +08:00
|
|
|
R_END_SELECTION,
|
|
|
|
R_KILL_SELECTION,
|
|
|
|
R_FORWARD_JUMP,
|
|
|
|
R_BACKWARD_JUMP,
|
2018-08-11 15:05:49 +08:00
|
|
|
R_FORWARD_JUMP_TILL,
|
|
|
|
R_BACKWARD_JUMP_TILL,
|
2016-01-22 11:56:39 +08:00
|
|
|
R_AND,
|
|
|
|
R_CANCEL,
|
2018-08-11 15:05:49 +08:00
|
|
|
R_REPEAT_JUMP,
|
|
|
|
R_REVERSE_REPEAT_JUMP,
|
2018-01-30 03:52:55 +08:00
|
|
|
|
|
|
|
// The range of key codes for inputrc-style keyboard functions that are passed on to the caller
|
|
|
|
// of input_read().
|
|
|
|
R_BEGIN_INPUT_FUNCTIONS = R_BEGINNING_OF_LINE,
|
2018-08-11 15:05:49 +08:00
|
|
|
R_END_INPUT_FUNCTIONS = R_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.
|
|
|
|
enum class char_event_type_t {
|
|
|
|
/// A character was entered.
|
|
|
|
charc,
|
|
|
|
|
|
|
|
/// A timeout was hit.
|
|
|
|
timeout,
|
2019-03-17 03:35:49 +08:00
|
|
|
|
|
|
|
/// end-of-file was reached.
|
|
|
|
eof
|
2019-03-15 16:11:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class char_event_t {
|
|
|
|
/// Set if the type is charc.
|
|
|
|
wchar_t c_;
|
|
|
|
|
|
|
|
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-15 16:11:15 +08:00
|
|
|
bool is_readline() const {
|
|
|
|
return is_char() && c_ >= R_BEGIN_INPUT_FUNCTIONS && c_ < R_END_INPUT_FUNCTIONS;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t get_char() const {
|
|
|
|
assert(type == char_event_type_t::charc && "Not a char type");
|
|
|
|
return c_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* implicit */ char_event_t(wchar_t c) : c_(c), type(char_event_type_t::charc) {}
|
|
|
|
|
|
|
|
/* implicit */ char_event_t(char_event_type_t type) : c_(0), type(type) {
|
|
|
|
assert(type != char_event_type_t::charc &&
|
|
|
|
"Cannot create a char event with this constructor");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-02 10:54:25 +08:00
|
|
|
/// Init the library.
|
2012-11-19 08:30:30 +08:00
|
|
|
void input_common_init(int (*ih)());
|
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
|