From 8c705b5dc224513b4e7d27e5facef4e893aaf6f5 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sun, 24 Jan 2016 14:49:42 +0000 Subject: [PATCH] Moving an updated position-history item to the end of the list, so that it won't be dropped any time soon. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5582 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 5 +++++ src/files.c | 42 +++++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4a16352b..997096db 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2016-01-24 Benno Schulenberg + * src/files.c (update_poshistory): Move an updated item to the end + of the list, so that it won't be dropped any time soon. The problem + was pointed out by David Niklas. + 2016-01-22 Benno Schulenberg * src/utils.c (get_homedir): Don't use $HOME when we're root, because some sudos don't filter it out of the environment (which can lead to diff --git a/src/files.c b/src/files.c index 906970fa..51e9c9cf 100644 --- a/src/files.c +++ b/src/files.c @@ -3183,33 +3183,41 @@ void save_poshistory(void) * and a column. If no entry is found, add a new one at the end. */ void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos) { - poshiststruct *posptr, *posprev = NULL; + poshiststruct *posptr, *theone, *posprev = NULL; char *fullpath = get_full_path(filename); if (fullpath == NULL) return; + /* Look for a matching filename in the list. */ for (posptr = position_history; posptr != NULL; posptr = posptr->next) { - if (!strcmp(posptr->filename, fullpath)) { - posptr->lineno = lineno; - posptr->xno = xpos; - free(fullpath); - return; - } + if (!strcmp(posptr->filename, fullpath)) + break; posprev = posptr; } - /* Didn't find it, make a new node yo! */ - posptr = (poshiststruct *)nmalloc(sizeof(poshiststruct)); - posptr->filename = mallocstrcpy(NULL, fullpath); - posptr->lineno = lineno; - posptr->xno = xpos; - posptr->next = NULL; + theone = posptr; - if (position_history == NULL) - position_history = posptr; - else - posprev->next = posptr; + /* If we didn't find it, make a new node; otherwise, if we're + * not at the end, move the matching one to the end. */ + if (theone == NULL) { + theone = (poshiststruct *)nmalloc(sizeof(poshiststruct)); + theone->filename = mallocstrcpy(NULL, fullpath); + if (position_history == NULL) + position_history = theone; + else + posprev->next = theone; + } else if (posptr->next != NULL) { + posprev->next = posptr->next; + while (posptr->next != NULL) + posptr = posptr->next; + posptr->next = theone; + } + + /* Store the last cursor position. */ + theone->lineno = lineno; + theone->xno = xpos; + theone->next = NULL; free(fullpath); } -- 2.39.5