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.
This commit is contained in:
Benno Schulenberg 2016-06-24 22:45:41 +02:00
parent 03c689c22c
commit ddd9c7a022
2 changed files with 37 additions and 47 deletions

View File

@ -1562,24 +1562,23 @@ void unbound_key(int code)
statusline(ALERT, _("Unbound key: %c"), code); statusline(ALERT, _("Unbound key: %c"), code);
} }
/* Read in a character, interpret it as a shortcut or toggle if /* Read in a keystroke. Act on the keystroke if it is a shortcut or a toggle;
* necessary, and return it. * otherwise, insert it into the edit buffer. If allow_funcs is FALSE, don't
* If allow_funcs is FALSE, don't actually run any functions associated * do anything with the keystroke -- just return it. */
* with shortcut keys. */
int do_input(bool allow_funcs) int do_input(bool allow_funcs)
{ {
int input; int input;
/* The character we read in. */ /* The keystroke we read in: a character or a shortcut. */
static int *kbinput = NULL; static int *puddle = NULL;
/* The input buffer. */ /* The input buffer for actual characters. */
static size_t kbinput_len = 0; static size_t depth = 0;
/* The length of the input buffer. */ /* The length of the input buffer. */
bool preserve = FALSE; bool preserve = FALSE;
/* Preserve the contents of the cutbuffer? */ /* Whether to preserve the contents of the cutbuffer. */
const sc *s; const sc *s;
bool have_shortcut; bool have_shortcut;
/* Read in a character. */ /* Read in a keystroke. */
input = get_kbinput(edit); input = get_kbinput(edit);
#ifndef NANO_TINY #ifndef NANO_TINY
@ -1619,26 +1618,23 @@ int do_input(bool allow_funcs)
} }
if (allow_funcs) { if (allow_funcs) {
/* If we got a character, and it isn't a shortcut or toggle, /* If the keystroke isn't a shortcut nor a toggle, it's a normal text
* it's a normal text character. Display the warning if we're * character: add the character to the input buffer -- or display a
* in view mode, or add the character to the input buffer if * warning when we're in view mode. */
* we're not. */
if (input != ERR && !have_shortcut) { if (input != ERR && !have_shortcut) {
if (ISSET(VIEW_MODE)) if (ISSET(VIEW_MODE))
print_view_warning(); print_view_warning();
else { else {
kbinput_len++; depth++;
kbinput = (int *)nrealloc(kbinput, kbinput_len * puddle = (int *)nrealloc(puddle, depth * sizeof(int));
sizeof(int)); puddle[depth - 1] = input;
kbinput[kbinput_len - 1] = input;
} }
} }
/* If we got a shortcut or toggle, or if there aren't any other /* 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 * characters waiting after the one we read in, we need to output
* output all the characters in the input buffer if it isn't * all available characters in the input puddle. Note that this
* empty. Note that it should be empty if we're in view * puddle will be empty if we're in view mode. */
* mode. */
if (have_shortcut || get_key_buffer_len() == 0) { if (have_shortcut || get_key_buffer_len() == 0) {
#ifndef DISABLE_WRAPPING #ifndef DISABLE_WRAPPING
/* If we got a shortcut or toggle, and it's not the shortcut /* 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(); wrap_reset();
#endif #endif
if (kbinput != NULL) { if (puddle != NULL) {
/* Display all the characters in the input buffer at /* Display all the characters in the input buffer at
* once, filtering out control characters. */ * once, filtering out control characters. */
char *output = charalloc(kbinput_len + 1); char *output = charalloc(depth + 1);
size_t i; size_t i;
for (i = 0; i < kbinput_len; i++) for (i = 0; i < depth; i++)
output[i] = (char)kbinput[i]; output[i] = (char)puddle[i];
output[i] = '\0'; output[i] = '\0';
do_output(output, kbinput_len, FALSE); do_output(output, depth, FALSE);
free(output); free(output);
/* Empty the input buffer. */ /* Empty the input buffer. */
kbinput_len = 0; free(puddle);
free(kbinput); puddle = NULL;
kbinput = NULL; depth = 0;
} }
} }
@ -2653,7 +2649,7 @@ int main(int argc, char **argv)
curs_set(1); curs_set(1);
wnoutrefresh(edit); wnoutrefresh(edit);
/* Read in and interpret characters. */ /* Read in and interpret keystrokes. */
do_input(TRUE); do_input(TRUE);
} }

View File

@ -303,35 +303,29 @@ int *get_input(WINDOW *win, size_t input_len)
return input; return input;
} }
/* Read in a single character. If it's ignored, swallow it and go on. /* Read in a single keystroke, ignoring any that are invalid. */
* 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. */
int get_kbinput(WINDOW *win) int get_kbinput(WINDOW *win)
{ {
int kbinput; int kbinput;
/* Read in a character and interpret it. Continue doing this until /* Extract one keystroke from the input stream. */
* we get a recognized value or sequence. */
while ((kbinput = parse_kbinput(win)) == ERR) while ((kbinput = parse_kbinput(win)) == ERR)
; ;
/* If we read from the edit window, blank the statusbar if we need /* If we read from the edit window, blank the statusbar if needed. */
* to. */
if (win == edit) if (win == edit)
check_statusblank(); check_statusblank();
return kbinput; return kbinput;
} }
/* Translate ASCII characters, extended keypad values, and escape /* Extract a single keystroke from the input stream. Translate escape
* sequences into their corresponding key values. Set meta_key to TRUE * sequences and extended keypad codes into their corresponding values.
* when we get a meta key sequence, and set func_key to TRUE when we get * Set meta_key to TRUE when we get a meta key sequence, and set func_key
* a function 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) int parse_kbinput(WINDOW *win)
{ {
static int escapes = 0, byte_digits = 0; static int escapes = 0, byte_digits = 0;