From b61d97eb9a167ba05f0d5b98461e741f1e779ff2 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 18 Jun 2020 11:54:40 +0200 Subject: [PATCH] tweaks: elide a variable, and shortcircuit a return Instead of first trying to match things, and then discarding these matches when the cursor is not at the end of the offered fragment ('buf'), simply don't bother to do any matching in that case. --- src/files.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/files.c b/src/files.c index 21872297..8076f913 100644 --- a/src/files.c +++ b/src/files.c @@ -2500,11 +2500,17 @@ char **cwd_tab_completion(const char *buf, bool allow_files, char *input_tab(char *buf, bool allow_files, size_t *place, bool *lastwastab, void (*refresh_func)(void), bool *listed) { - size_t num_matches = 0, buf_len; + size_t num_matches = 0; char **matches = NULL; *listed = FALSE; + /* If the cursor is not at the end of the fragment, do nothing. */ + if (buf[*place] != '\0') { + beep(); + return buf; + } + /* If the word starts with `~' and there is no slash in the word, * then try completing this word as a username. */ if (*place > 0 && *buf == '~') { @@ -2518,9 +2524,7 @@ char *input_tab(char *buf, bool allow_files, size_t *place, if (matches == NULL) matches = cwd_tab_completion(buf, allow_files, &num_matches, *place); - buf_len = strlen(buf); - - if (num_matches == 0 || *place != buf_len) + if (num_matches == 0) beep(); else { size_t match, common_len = 0; @@ -2567,8 +2571,8 @@ char *input_tab(char *buf, bool allow_files, size_t *place, /* If the matches have something in common, show that part. */ if (common_len != *place) { - buf = charealloc(buf, common_len + buf_len - *place + 1); - memmove(buf + common_len, buf + *place, buf_len - *place + 1); + buf = charealloc(buf, common_len + 1); + memmove(buf + common_len, buf + *place, 1); strncpy(buf, mzero, common_len); *place = common_len; }