From d2e261d87382ec6c506fe206f2df850d30f22235 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 3 Jul 2025 10:55:31 +0200 Subject: [PATCH] history: do not forget anchors when line number is given on command line When a line number is given on the command line, the position-history file needs to be read first, because it might contain saved anchors. (Before placing the cursor on the given line, or searching for a given string, the current position now needs to be set back to top-of-file, as otherwise the saved column position is likely to be inherited, or the search will start from the inherited position.) This fixes https://savannah.gnu.org/bugs/?67272. Bug existed since version 8.5, commit 60e2f2ba, since the saving of anchors was introduced. --- src/nano.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/nano.c b/src/nano.c index b6146b72..cf10273a 100644 --- a/src/nano.c +++ b/src/nano.c @@ -2580,11 +2580,21 @@ int main(int argc, char **argv) continue; } +#ifdef ENABLE_HISTORIES + if (ISSET(POSITIONLOG) && openfile->filename[0] != '\0') + restore_cursor_position_if_any(); +#endif + /* If a position was given on the command line, go there. */ - if (givenline != 0 || givencol != 0) + if (givenline != 0 || givencol != 0) { + openfile->current = openfile->filetop; + openfile->placewewant = 0; goto_line_and_column(givenline, givencol, FALSE, FALSE); + } #ifndef NANO_TINY else if (searchstring != NULL) { + openfile->current = openfile->filetop; + openfile->current_x = 0; if (ISSET(USE_REGEXP)) regexp_init(searchstring); if (!findnextstr(searchstring, FALSE, JUSTFIND, NULL, @@ -2600,10 +2610,6 @@ int main(int argc, char **argv) last_search = searchstring; searchstring = NULL; } -#endif -#ifdef ENABLE_HISTORIES - else if (ISSET(POSITIONLOG) && openfile->filename[0] != '\0') - restore_cursor_position_if_any(); #endif }