From c77029570dd49133f36e4e0ea5efbb64365fb6f5 Mon Sep 17 00:00:00 2001 From: Rene Kita Date: Sun, 25 Dec 2022 15:33:33 +0100 Subject: [PATCH] Merge history file if it was modified after start w3m reads the history file on startup and writes it on exit. That means if you open multiple instances of w3m, the history file will contain the history of the last instance closed. All other history changes are lost. Check if the modification time of the history file has changed before writing. If it has changed read the history file from the disk into a new history. Push the entries that are in the current history but not in the history file into the new history and write the new history to disk. --- history.c | 35 +++++++++++++++++++++++++++++++++++ history.h | 1 + 2 files changed, 36 insertions(+) diff --git a/history.c b/history.c index 69eb3d8..b5d0ac4 100644 --- a/history.c +++ b/history.c @@ -2,6 +2,19 @@ #include "fm.h" #ifdef USE_HISTORY +/* Merge entries from their history into ours */ +static int +mergeHistory(Hist *ours, Hist *theirs) +{ + HistItem *item; + + for (item = theirs->list->first; item; item = item->next) + if (!getHashHist(ours, item->ptr)) + pushHist(ours, (char *)item->ptr); + + return 0; +} + Buffer * historyBuffer(Hist *hist) { @@ -36,12 +49,19 @@ loadHistory(Hist *hist) { FILE *f; Str line; + struct stat st; if (hist == NULL) return 1; if ((f = fopen(rcFile(HISTORY_FILE), "rt")) == NULL) return 1; + if (fstat(fileno(f), &st) == -1) { + fclose(f); + return 1; + } + hist->mtime = (long long)st.st_mtim.tv_sec; + while (!feof(f)) { line = Strfgets(f); Strchop(line); @@ -59,12 +79,27 @@ void saveHistory(Hist *hist, size_t size) { FILE *f; + Hist *fhist; HistItem *item; + char *histf; char *tmpf; int rename_ret; + struct stat st; if (hist == NULL || hist->list == NULL) return; + + histf = rcFile(HISTORY_FILE); + if (stat(histf, &st) == -1) + goto fail; + if (hist->mtime != (long long)st.st_mtim.tv_sec) { + fhist = newHist(); + if (loadHistory(fhist) || mergeHistory(fhist, hist)) + disp_err_message("Can't merge history", FALSE); + else + hist = fhist; + } + tmpf = tmpfname(TMPF_HIST, NULL)->ptr; if ((f = fopen(tmpf, "w")) == NULL) goto fail; diff --git a/history.h b/history.h index d6791df..3f229f5 100644 --- a/history.h +++ b/history.h @@ -16,6 +16,7 @@ typedef struct { HistList *list; HistItem *current; Hash_sv *hash; + long long mtime; } Hist; extern Hist *newHist(void);