From ddd9c7a022734e6a375926e0348ee0569d550727 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 24 Jun 2016 22:45:41 +0200 Subject: [PATCH] tweaks: try to distinguish between keystrokes and characters Keystrokes are single integers (aided by the flags meta_key and func_key) but in the input stream they can be encoded as escape sequences (a series of bytes). Characters are values in byte range, but in UTF-8 one character can consist of multiple bytes. Also rename two variables, because the secondary input buffer that they refer to contains only characters (mostly just one), never any shortcuts; and there are too many "kbinput" already. --- src/nano.c | 58 +++++++++++++++++++++++++---------------------------- src/winio.c | 26 +++++++++--------------- 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/nano.c b/src/nano.c index 9c2699d7..57517f56 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1562,24 +1562,23 @@ void unbound_key(int code) statusline(ALERT, _("Unbound key: %c"), code); } -/* Read in a character, interpret it as a shortcut or toggle if - * necessary, and return it. - * If allow_funcs is FALSE, don't actually run any functions associated - * with shortcut keys. */ +/* Read in a keystroke. Act on the keystroke if it is a shortcut or a toggle; + * otherwise, insert it into the edit buffer. If allow_funcs is FALSE, don't + * do anything with the keystroke -- just return it. */ int do_input(bool allow_funcs) { int input; - /* The character we read in. */ - static int *kbinput = NULL; - /* The input buffer. */ - static size_t kbinput_len = 0; + /* The keystroke we read in: a character or a shortcut. */ + static int *puddle = NULL; + /* The input buffer for actual characters. */ + static size_t depth = 0; /* The length of the input buffer. */ bool preserve = FALSE; - /* Preserve the contents of the cutbuffer? */ + /* Whether to preserve the contents of the cutbuffer. */ const sc *s; bool have_shortcut; - /* Read in a character. */ + /* Read in a keystroke. */ input = get_kbinput(edit); #ifndef NANO_TINY @@ -1619,26 +1618,23 @@ int do_input(bool allow_funcs) } if (allow_funcs) { - /* If we got a character, and it isn't a shortcut or toggle, - * it's a normal text character. Display the warning if we're - * in view mode, or add the character to the input buffer if - * we're not. */ + /* If the keystroke isn't a shortcut nor a toggle, it's a normal text + * character: add the character to the input buffer -- or display a + * warning when we're in view mode. */ if (input != ERR && !have_shortcut) { if (ISSET(VIEW_MODE)) print_view_warning(); else { - kbinput_len++; - kbinput = (int *)nrealloc(kbinput, kbinput_len * - sizeof(int)); - kbinput[kbinput_len - 1] = input; + depth++; + puddle = (int *)nrealloc(puddle, depth * sizeof(int)); + puddle[depth - 1] = input; } } /* If we got a shortcut or toggle, or if there aren't any other - * characters waiting after the one we read in, we need to - * output all the characters in the input buffer if it isn't - * empty. Note that it should be empty if we're in view - * mode. */ + * characters waiting after the one we read in, we need to output + * all available characters in the input puddle. Note that this + * puddle will be empty if we're in view mode. */ if (have_shortcut || get_key_buffer_len() == 0) { #ifndef DISABLE_WRAPPING /* If we got a shortcut or toggle, and it's not the shortcut @@ -1647,24 +1643,24 @@ int do_input(bool allow_funcs) wrap_reset(); #endif - if (kbinput != NULL) { + if (puddle != NULL) { /* Display all the characters in the input buffer at * once, filtering out control characters. */ - char *output = charalloc(kbinput_len + 1); + char *output = charalloc(depth + 1); size_t i; - for (i = 0; i < kbinput_len; i++) - output[i] = (char)kbinput[i]; + for (i = 0; i < depth; i++) + output[i] = (char)puddle[i]; output[i] = '\0'; - do_output(output, kbinput_len, FALSE); + do_output(output, depth, FALSE); free(output); /* Empty the input buffer. */ - kbinput_len = 0; - free(kbinput); - kbinput = NULL; + free(puddle); + puddle = NULL; + depth = 0; } } @@ -2653,7 +2649,7 @@ int main(int argc, char **argv) curs_set(1); wnoutrefresh(edit); - /* Read in and interpret characters. */ + /* Read in and interpret keystrokes. */ do_input(TRUE); } diff --git a/src/winio.c b/src/winio.c index 8c54f0b9..731e8b58 100644 --- a/src/winio.c +++ b/src/winio.c @@ -303,35 +303,29 @@ int *get_input(WINDOW *win, size_t input_len) return input; } -/* Read in a single character. If it's ignored, swallow it and go on. - * Otherwise, try to translate it from ASCII, meta key sequences, escape - * sequences, and/or extended keypad values. Supported extended keypad - * values consist of - * [arrow key], Ctrl-[arrow key], Shift-[arrow key], Enter, Backspace, - * the editing keypad (Insert, Delete, Home, End, PageUp, and PageDown), - * the function keypad (F1-F16), and the numeric keypad with NumLock - * off. */ +/* Read in a single keystroke, ignoring any that are invalid. */ int get_kbinput(WINDOW *win) { int kbinput; - /* Read in a character and interpret it. Continue doing this until - * we get a recognized value or sequence. */ + /* Extract one keystroke from the input stream. */ while ((kbinput = parse_kbinput(win)) == ERR) ; - /* If we read from the edit window, blank the statusbar if we need - * to. */ + /* If we read from the edit window, blank the statusbar if needed. */ if (win == edit) check_statusblank(); return kbinput; } -/* Translate ASCII characters, extended keypad values, and escape - * sequences into their corresponding key values. Set meta_key to TRUE - * when we get a meta key sequence, and set func_key to TRUE when we get - * a function key. */ +/* Extract a single keystroke from the input stream. Translate escape + * sequences and extended keypad codes into their corresponding values. + * Set meta_key to TRUE when we get a meta key sequence, and set func_key + * to TRUE when we get a function key. Supported extended keypad values + * are: [arrow key], Ctrl-[arrow key], Shift-[arrow key], Enter, Backspace, + * the editing keypad (Insert, Delete, Home, End, PageUp, and PageDown), + * the function keys (F1-F16), and the numeric keypad with NumLock off. */ int parse_kbinput(WINDOW *win) { static int escapes = 0, byte_digits = 0;