From 4b9fece9f48615f2e8068054c60763e844415a7c Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Thu, 14 Jan 2016 21:46:53 -0800 Subject: allow configuring the escape delay timeout Introduce a "fish_escape_delay_ms" variable to allow the user to configure the delay used when seeing a bare escape before assuming no other characters will be received that might match a bound character sequence. This is primarily useful for vi mode so that a bare escape character (i.e., keystroke) can switch to vi "insert" to "normal" mode in a timely fashion. --- src/input_common.cpp | 59 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'src/input_common.cpp') diff --git a/src/input_common.cpp b/src/input_common.cpp index cda4ca11..291a37a7 100644 --- a/src/input_common.cpp +++ b/src/input_common.cpp @@ -33,9 +33,11 @@ Implementation file for the low level input library reading after \\x1b is read before assuming that escape key was pressed, and not an escape sequence. - This is the value used by the readline library. + This is the value used by the readline library. It can be overridden by + setting the fish_escape_delay_ms variable. */ -#define WAIT_ON_ESCAPE 500 +#define WAIT_ON_ESCAPE_DEFAULT 500 +static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT; /** Characters that have been read and returned by the sequence matching code */ static std::deque lookahead_list; @@ -79,6 +81,7 @@ static int (*interrupt_handler)(); void input_common_init(int (*ih)()) { interrupt_handler = ih; + update_wait_on_escape_ms(); } void input_common_destroy() @@ -214,36 +217,48 @@ static wint_t readb() return arr[0]; } +// Update the wait_on_escape_ms value in response to the fish_escape_delay_ms +// user variable being set. +void update_wait_on_escape_ms() +{ + env_var_t escape_time_ms = env_get_string(L"fish_escape_delay_ms"); + if (escape_time_ms.missing_or_empty()) + { + wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT; + return; + } + + wchar_t *endptr; + long tmp = wcstol(escape_time_ms.c_str(), &endptr, 10); + + if (*endptr != '\0' || tmp < 10 || tmp >= 5000) + { + fwprintf(stderr, L"ignoring fish_escape_delay_ms: value '%ls' " + "is not an integer or is < 10 or >= 5000 ms\n", + escape_time_ms.c_str()); + } + else + { + wait_on_escape_ms = (int)tmp; + } +} + + wchar_t input_common_readch(int timed) { if (! has_lookahead()) { if (timed) { - int count; fd_set fds; - struct timeval tm= - { - 0, - 1000 * WAIT_ON_ESCAPE - } - ; - FD_ZERO(&fds); FD_SET(0, &fds); - count = select(1, &fds, 0, 0, &tm); - - switch (count) + struct timeval tm = {wait_on_escape_ms / 1000, + 1000 * (wait_on_escape_ms % 1000)}; + int count = select(1, &fds, 0, 0, &tm); + if (count <= 0) { - case 0: - return WEOF; - - case -1: - return WEOF; - break; - default: - break; - + return WEOF; } } -- cgit v1.2.3