diff --git a/src/history.c b/src/history.c index aa4f60f59262f2a3a9bdee8ca4a438ab729ca940..0c21a84e77273b6a13e7eea2444b8657382f4a8e 100644 --- a/src/history.c +++ b/src/history.c @@ -40,6 +40,8 @@ static bool history_changed = FALSE; /* Whether any of the history lists has changed. */ +static struct stat stat_of_positions_file; + /* The last-obtained stat information of the positions file. */ /* Initialize the lists of historical search and replace strings * and the list of historical executed commands. */ @@ -386,6 +388,15 @@ void save_history(void) free(histname); } +/* Update the modification time of the position history file. */ +void update_posfile_timestamp(void) +{ + char *poshist = concatenate(statedir, POSITION_HISTORY); + + stat(poshist, &stat_of_positions_file); + free(poshist); +} + /* Load the recorded cursor positions for files that were edited. */ void load_poshistory(void) { @@ -448,6 +459,8 @@ void load_poshistory(void) } fclose(hisfile); free(line); + + update_posfile_timestamp(); } free(poshist); } @@ -487,10 +500,35 @@ void save_poshistory(void) free(path_and_place); } fclose(hisfile); + + update_posfile_timestamp(); } free(poshist); } +/* Reload the position history file if it has been modified since last load. */ +void reload_positions_if_needed(void) +{ + char *poshist = concatenate(statedir, POSITION_HISTORY); + struct stat newstat; + + stat(poshist, &newstat); + free(poshist); + + if (newstat.st_mtime != stat_of_positions_file.st_mtime) { + poshiststruct *ptr, *nextone; + + for (ptr = position_history; ptr != NULL; ptr = nextone) { + nextone = ptr->next; + free(ptr->filename); + free(ptr); + } + position_history = NULL; + + load_poshistory(); + } +} + /* Update the recorded last file positions, given a filename, a line * 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) @@ -503,6 +541,8 @@ void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos) return; } + reload_positions_if_needed(); + /* Look for a matching filename in the list. */ for (posptr = position_history; posptr != NULL; posptr = posptr->next) { if (!strcmp(posptr->filename, fullpath)) @@ -551,6 +591,8 @@ void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos) theone->next = NULL; free(fullpath); + + save_poshistory(); } /* Check whether the given file matches an existing entry in the recorded @@ -564,6 +606,8 @@ bool has_old_position(const char *file, ssize_t *line, ssize_t *column) if (fullpath == NULL) return FALSE; + reload_positions_if_needed(); + while (posptr != NULL && strcmp(posptr->filename, fullpath) != 0) posptr = posptr->next; diff --git a/src/nano.c b/src/nano.c index 018cd88d21b04d335ca961ea18d0346e38067d0f..e9f73e8fc7037005343bc3efe87f50cb08b11108 100644 --- a/src/nano.c +++ b/src/nano.c @@ -570,7 +570,6 @@ void finish(void) save_history(); if (ISSET(POS_HISTORY)) { update_poshistory(openfile->filename, openfile->current->lineno, xplustabs() + 1); - save_poshistory(); } #endif diff --git a/src/proto.h b/src/proto.h index 538b2b55953eb7df7397862975d432077d8db586..bad1de9f1168e2f210b9693ac486748a931ff09a 100644 --- a/src/proto.h +++ b/src/proto.h @@ -370,7 +370,6 @@ bool have_statedir(void); void load_history(void); void save_history(void); void load_poshistory(void); -void save_poshistory(void); void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos); bool has_old_position(const char *file, ssize_t *line, ssize_t *column); #endif