From f13dd140ebefb360c18bc9e611fb1978db4048ba Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Wed, 27 Feb 2019 14:37:53 -0600 Subject: [PATCH] display: correctly trim an overshooting character from a prompt answer Tell display_string() explicitly when we're at a prompt, instead of letting it glean this from the current menu not being MMAIN, which fails in some circumstances. This improves the fix for https://savannah.gnu.org/bugs/?55620, fixes https://savannah.gnu.org/bugs/?55680 in a better way, and fixes https://savannah.gnu.org/bugs/?55773. --- src/browser.c | 4 ++-- src/files.c | 8 ++++---- src/prompt.c | 2 +- src/proto.h | 3 ++- src/search.c | 5 ++--- src/text.c | 1 - src/winio.c | 24 ++++++++++++++---------- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/browser.c b/src/browser.c index a192b56c..e66418ef 100644 --- a/src/browser.c +++ b/src/browser.c @@ -529,7 +529,7 @@ void browser_refresh(void) /* Whether to put an ellipsis before the filename? We don't * waste space on dots when there are fewer than 15 columns. */ char *disp = display_string(thename, dots ? - namelen + infomaxlen + 4 - longest : 0, longest, FALSE); + namelen + infomaxlen + 4 - longest : 0, longest, FALSE, FALSE); /* The filename (or a fragment of it) in displayable format. * When a fragment, account for dots plus one space padding. */ @@ -667,7 +667,7 @@ int filesearch_init(bool forwards) /* If something was searched for before, show it between square brackets. */ if (*last_search != '\0') { - char *disp = display_string(last_search, 0, COLS / 3, FALSE); + char *disp = display_string(last_search, 0, COLS / 3, FALSE, FALSE); thedefault = charalloc(strlen(disp) + 7); /* We use (COLS / 3) here because we need to see more on the line. */ diff --git a/src/files.c b/src/files.c index be2045f9..e065f508 100644 --- a/src/files.c +++ b/src/files.c @@ -359,13 +359,13 @@ int do_lockfile(const char *filename) postedname = mallocstrcpy(NULL, "_"); else if (room < strlenpt(filename)) { char *fragment = display_string(filename, - strlenpt(filename) - room + 3, room, FALSE); + strlenpt(filename) - room + 3, room, FALSE, FALSE); postedname = charalloc(strlen(fragment) + 4); strcpy(postedname, "..."); strcat(postedname, fragment); free(fragment); } else - postedname = display_string(filename, 0, room, FALSE); + postedname = display_string(filename, 0, room, FALSE, FALSE); /* Allow extra space for username (14), program name (8), PID (8), * and terminating \0 (1), minus the %s (2) for the file name. */ @@ -2256,7 +2256,7 @@ int do_writeout(bool exiting, bool withprompt) if (name_exists) { char *question = _("File \"%s\" exists; OVERWRITE? "); char *name = display_string(answer, 0, - COLS - strlenpt(question) + 1, FALSE); + COLS - strlenpt(question) + 1, FALSE, FALSE); char *message = charalloc(strlen(question) + strlen(name) + 1); @@ -2704,7 +2704,7 @@ char *input_tab(char *buf, bool allow_files, size_t *place, break; } - disp = display_string(matches[match], 0, longest_name, FALSE); + disp = display_string(matches[match], 0, longest_name, FALSE, FALSE); waddstr(edit, disp); free(disp); diff --git a/src/prompt.c b/src/prompt.c index 5a84813e..e4253363 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -400,7 +400,7 @@ void draw_the_promptbar(void) waddch(bottomwin, ':'); waddch(bottomwin, (the_page == 0) ? ' ' : '<'); - expanded = display_string(answer, the_page, COLS - base, FALSE); + expanded = display_string(answer, the_page, COLS - base, FALSE, TRUE); waddstr(bottomwin, expanded); free(expanded); diff --git a/src/proto.h b/src/proto.h index b3a79c5d..65675731 100644 --- a/src/proto.h +++ b/src/proto.h @@ -618,7 +618,8 @@ void blank_statusbar(void); void wipe_statusbar(void); void blank_bottombars(void); void check_statusblank(void); -char *display_string(const char *buf, size_t column, size_t span, bool isdata); +char *display_string(const char *buf, size_t column, size_t span, + bool isdata, bool isprompt); void titlebar(const char *path); void statusbar(const char *msg); void warn_and_shortly_pause(const char *msg); diff --git a/src/search.c b/src/search.c index a6e36912..5bdd1b02 100644 --- a/src/search.c +++ b/src/search.c @@ -83,7 +83,7 @@ void search_init(bool replacing, bool keep_the_answer) /* If something was searched for earlier, include it in the prompt. */ if (*last_search != '\0') { - char *disp = display_string(last_search, 0, COLS / 3, FALSE); + char *disp = display_string(last_search, 0, COLS / 3, FALSE, FALSE); thedefault = charalloc(strlen(disp) + 7); /* We use (COLS / 3) here because we need to see more on the line. */ @@ -388,7 +388,7 @@ void do_findnext(void) /* Report on the status bar that the given string was not found. */ void not_found_msg(const char *str) { - char *disp = display_string(str, 0, (COLS / 2) + 1, FALSE); + char *disp = display_string(str, 0, (COLS / 2) + 1, FALSE, FALSE); size_t numchars = actual_x(disp, strnlenpt(disp, COLS / 2)); statusline(HUSH, _("\"%.*s%s\" not found"), numchars, disp, @@ -579,7 +579,6 @@ ssize_t do_replace_loop(const char *needle, bool whole_word_only, light_from_col = xplustabs(); light_to_col = strnlenpt(openfile->current->data, openfile->current_x + match_len); - currmenu = MMAIN; /* Refresh the edit window, scrolling it if necessary. */ edit_refresh(); diff --git a/src/text.c b/src/text.c index 40ea24f7..f0be053e 100644 --- a/src/text.c +++ b/src/text.c @@ -2203,7 +2203,6 @@ bool fix_spello(const char *word) filestruct *saved_mark = openfile->mark; openfile->mark = NULL; #endif - currmenu = MMAIN; edit_refresh(); /* Let the user supply a correctly spelled alternative. */ diff --git a/src/winio.c b/src/winio.c index 53798e17..de94dbdf 100644 --- a/src/winio.c +++ b/src/winio.c @@ -1863,8 +1863,11 @@ void check_statusblank(void) * at most span columns. column is zero-based, and span is one-based, so * span == 0 means you get "" returned. The returned string is dynamically * allocated, and should be freed. If isdata is TRUE, the caller might put - * "<" at the beginning or ">" at the end of the line if it's too long. */ -char *display_string(const char *buf, size_t column, size_t span, bool isdata) + * "<" at the beginning or ">" at the end of the line if it's too long. If + * isprompt is TRUE, the caller might put ">" at the end of the line if it's + * too long. */ +char *display_string(const char *buf, size_t column, size_t span, + bool isdata, bool isprompt) { size_t start_index = actual_x(buf, column); /* The index of the first character that the caller wishes to show. */ @@ -1989,7 +1992,7 @@ char *display_string(const char *buf, size_t column, size_t span, bool isdata) /* If there is more text than can be shown, make room for the ">". */ if ((*buf != '\0' || column > beyond) && - (currmenu != MMAIN || (isdata && !ISSET(SOFTWRAP)))) { + (isprompt || (isdata && !ISSET(SOFTWRAP)))) { do { index = move_mbleft(converted, index); } while (mbwidth(converted + index) == 0); @@ -2143,13 +2146,13 @@ void titlebar(const char *path) /* Print the full path if there's room; otherwise, dottify it. */ if (pathlen + pluglen + statelen <= COLS) { - caption = display_string(path, 0, pathlen, FALSE); + caption = display_string(path, 0, pathlen, FALSE, FALSE); waddstr(topwin, caption); free(caption); } else if (5 + statelen <= COLS) { waddstr(topwin, "..."); caption = display_string(path, 3 + pathlen - COLS + statelen, - COLS - statelen, FALSE); + COLS - statelen, FALSE, FALSE); waddstr(topwin, caption); free(caption); } @@ -2240,7 +2243,7 @@ void statusline(message_type importance, const char *msg, ...) compound = charalloc(MAXCHARLEN * (COLS + 1)); vsnprintf(compound, MAXCHARLEN * (COLS + 1), msg, ap); va_end(ap); - message = display_string(compound, 0, COLS, FALSE); + message = display_string(compound, 0, COLS, FALSE, FALSE); free(compound); start_col = (COLS - strlenpt(message)) / 2; @@ -2772,7 +2775,7 @@ int update_line(filestruct *fileptr, size_t index) /* Expand the line, replacing tabs with spaces, and control * characters with their displayed forms. */ - converted = display_string(fileptr->data, from_col, editwincols, TRUE); + converted = display_string(fileptr->data, from_col, editwincols, TRUE, FALSE); /* Draw the line. */ edit_draw(fileptr, converted, row, from_col); @@ -2842,7 +2845,8 @@ int update_softwrapped_line(filestruct *fileptr) blank_row(edit, row, 0, COLS); /* Convert the chunk to its displayable form and draw it. */ - converted = display_string(fileptr->data, from_col, to_col - from_col, TRUE); + converted = display_string(fileptr->data, from_col, to_col - from_col, + TRUE, FALSE); edit_draw(fileptr, converted, row++, from_col); free(converted); @@ -3443,7 +3447,7 @@ void spotlight(size_t from_col, size_t to_col) to_col++; } else word = display_string(openfile->current->data, from_col, - to_col - from_col, FALSE); + to_col - from_col, FALSE, FALSE); word_span = strlenpt(word); @@ -3496,7 +3500,7 @@ void spotlight_softwrapped(size_t from_col, size_t to_col) break_col++; } else word = display_string(openfile->current->data, from_col, - break_col - from_col, FALSE); + break_col - from_col, FALSE, FALSE); wattron(edit, interface_color_pair[SELECTED_TEXT]);