commit b8c684193e9a958ff28c74e8cc30bbed6ed85fdf
parent 84b1fdf6c6da941ab342e54faa0470e3b21d8d2d
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Fri, 9 May 2025 12:02:22 +0200
tweaks: elide the unneeded passing around of three parameters
The function has_old_position() was used just twice, and both calls
were followed by basically the same call of goto_line_and_column().
So... move the latter call into the first function, to avoid the
unneeded passing back and forth of the line and column numbers.
Diffstat:
4 files changed, 11 insertions(+), 22 deletions(-)
diff --git a/src/files.c b/src/files.c
@@ -1342,12 +1342,10 @@ void insert_a_file_or(bool execute)
if (ISSET(MULTIBUFFER)) {
#ifdef ENABLE_HISTORIES
if (ISSET(POSITIONLOG)) {
- ssize_t priorline, priorcol;
#ifndef NANO_TINY
if (!execute)
#endif
- if (has_old_position(answer, &priorline, &priorcol))
- goto_line_and_column(priorline, priorcol, FALSE, FALSE);
+ restore_cursor_position_if_any();
}
#endif
/* Update title bar and color info for this new buffer. */
diff --git a/src/history.c b/src/history.c
@@ -556,16 +556,15 @@ void update_poshistory(void)
save_poshistory();
}
-/* Check whether the given file matches an existing entry in the recorded
- * last file positions. If not, return FALSE. If yes, return TRUE and
- * set line and column to the retrieved values. */
-bool has_old_position(const char *file, ssize_t *line, ssize_t *column)
+/* Check whether the current filename matches an entry in the list of
+ * recorded positions. If yes, restore the relevant cursor position. */
+void restore_cursor_position_if_any(void)
{
- char *fullpath = get_full_path(file);
+ char *fullpath = get_full_path(openfile->filename);
poshiststruct *item;
if (fullpath == NULL)
- return FALSE;
+ return;
reload_positions_if_needed();
@@ -575,11 +574,7 @@ bool has_old_position(const char *file, ssize_t *line, ssize_t *column)
free(fullpath);
- if (item == NULL)
- return FALSE;
-
- *line = item->linenumber;
- *column = item->columnnumber;
- return TRUE;
+ if (item)
+ goto_line_and_column(item->linenumber, item->columnnumber, FALSE, FALSE);
}
#endif /* ENABLE_HISTORIES */
diff --git a/src/nano.c b/src/nano.c
@@ -2602,12 +2602,8 @@ int main(int argc, char **argv)
}
#endif
#ifdef ENABLE_HISTORIES
- else if (ISSET(POSITIONLOG) && openfile->filename[0] != '\0') {
- ssize_t savedline, savedcol;
- /* If edited before, restore the last cursor position. */
- if (has_old_position(argv[optind - 1], &savedline, &savedcol))
- goto_line_and_column(savedline, savedcol, FALSE, FALSE);
- }
+ else if (ISSET(POSITIONLOG) && openfile->filename[0] != '\0')
+ restore_cursor_position_if_any();
#endif
}
diff --git a/src/prototypes.h b/src/prototypes.h
@@ -358,7 +358,7 @@ void load_history(void);
void save_history(void);
void load_poshistory(void);
void update_poshistory(void);
-bool has_old_position(const char *file, ssize_t *line, ssize_t *column);
+void restore_cursor_position_if_any(void);
#endif
/* Most functions in move.c. */