Skip to content

Commit

Permalink
implement save_hist=always
Browse files Browse the repository at this point in the history
- create setting group for history
- add save_hist=always
- save history on quit only when save_hist=quit
- update history in pushHashHist when save_hist=always

We adjust the number of history based on the size of history saved in
the previous update. In this way we don't need to store history in local
memory, but the size of history may differ from the size limit.
  • Loading branch information
shinh committed Oct 31, 2009
1 parent 0e22b87 commit 53a5ada
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
45 changes: 45 additions & 0 deletions history.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,46 @@ loadHistory(Hist *hist)
fclose(f);
}

void
saveOneHistory(const char *ptr)
{
FILE *f;
Str line;
long ri, wi;
static int prev_num_hist = 0;
int num_skip = prev_num_hist - URLHistSize;

if ((f = fopen(rcFile(HISTORY_FILE), "r+t")) == NULL)
return;

ri = wi = 0;
prev_num_hist = 1;
flock(fileno(f), LOCK_EX);
while (!feof(f)) {
line = Strfgets(f);
Strchop(line);
Strremovefirstspaces(line);
Strremovetrailingspaces(line);
if (line->length == 0)
continue;
if (num_skip-- > 0)
continue;
if (!Strcmp_charp(line, ptr))
continue;
prev_num_hist++;

ri = ftell(f);
fseek(f, wi, SEEK_SET);
fprintf(f, "%s\n", (char *)line->ptr);
wi = ftell(f);
fseek(f, ri, SEEK_SET);
}
fseek(f, wi, SEEK_SET);
fprintf(f, "%s\n", ptr);
ftruncate(fileno(f), ftell(f));
fclose(f);
}

void
saveHistory(Hist *hist, size_t size)
{
Expand Down Expand Up @@ -150,6 +190,11 @@ pushHist(Hist *hist, char *ptr)
HistItem *
pushHashHist(Hist *hist, char *ptr)
{
#ifdef USE_HISTORY
if (UseHistory && SaveURLHist == WHEN_SAVE_HIST_ALWAYS)
saveOneHistory(ptr);
#endif /* USE_HISTORY */

HistItem *item;

if (hist == NULL || hist->list == NULL)
Expand Down
4 changes: 4 additions & 0 deletions history.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#define HIST_HASH_SIZE 127

#define WHEN_SAVE_HIST_NEVER 0
#define WHEN_SAVE_HIST_QUIT 1
#define WHEN_SAVE_HIST_ALWAYS 2

typedef ListItem HistItem;

typedef GeneralList HistList;
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2423,7 +2423,7 @@ _quitfm(int confirm)
save_cookies();
#endif /* USE_COOKIE */
#ifdef USE_HISTORY
if (UseHistory && SaveURLHist)
if (UseHistory && SaveURLHist == WHEN_SAVE_HIST_QUIT)
saveHistory(URLHist, URLHistSize);
#endif /* USE_HISTORY */
w3m_exit(0);
Expand Down
21 changes: 18 additions & 3 deletions rc.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ static struct sel_c auto_detect_str[] = {
};
#endif

#ifdef USE_HISTORY
static struct sel_c when_save_hist[] = {
{N_S(WHEN_SAVE_HIST_NEVER), N_("never")},
{N_S(WHEN_SAVE_HIST_QUIT), N_("quit")},
{N_S(WHEN_SAVE_HIST_ALWAYS), N_("always")},
{0, NULL, NULL}
};
#endif

struct param_ptr params1[] = {
{"tabstop", P_NZINT, PI_TEXT, (void *)&Tabstop, CMT_TABSTOP, NULL},
{"indent_incr", P_NZINT, PI_TEXT, (void *)&IndentIncr, CMT_INDENT_INCR,
Expand Down Expand Up @@ -431,13 +440,18 @@ struct param_ptr params2[] = {
#endif /* USE_COLOR */


struct param_ptr params3[] = {
{"pagerline", P_NZINT, PI_TEXT, (void *)&PagerMax, CMT_PAGERLINE, NULL},
#ifdef USE_HISTORY
struct param_ptr params11[] = {
{"use_history", P_INT, PI_ONOFF, (void *)&UseHistory, CMT_HISTORY, NULL},
{"history", P_INT, PI_TEXT, (void *)&URLHistSize, CMT_HISTSIZE, NULL},
{"save_hist", P_INT, PI_ONOFF, (void *)&SaveURLHist, CMT_SAVEHIST, NULL},
{"save_hist", P_INT, PI_SEL_C, (void *)&SaveURLHist, CMT_SAVEHIST,
(void *)when_save_hist},
};
#endif /* USE_HISTORY */


struct param_ptr params3[] = {
{"pagerline", P_NZINT, PI_TEXT, (void *)&PagerMax, CMT_PAGERLINE, NULL},
{"confirm_qq", P_INT, PI_ONOFF, (void *)&confirm_on_quit, CMT_CONFIRM_QQ,
NULL},
{"close_tab_back", P_INT, PI_ONOFF, (void *)&close_tab_back,
Expand Down Expand Up @@ -680,6 +694,7 @@ struct param_section sections[] = {
#ifdef USE_COLOR
{N_("Color Settings"), params2},
#endif /* USE_COLOR */
{N_("History Settings"), params11},
{N_("Miscellaneous Settings"), params3},
{N_("Directory Settings"), params5},
{N_("External Program Settings"), params6},
Expand Down

0 comments on commit 53a5ada

Please sign in to comment.