Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Finer control over what gets written to the history file



On Oct 16, 10:20am, Peter Stephenson wrote:
}
} Oh, wait, you're saying you didn't even realise at the time you wanted
} to omit the lines.  You want something you can activate at the point
} you're about to run fc -W or exit the shell.   So that would have to be
} a different hook, which isn't really appropriate for running on every
} line of a potentially huge file.  So probably your way is as good as it
} gets.

OK, here's a final patch including doc.  Nobody commented on the memory
management question and a push/pop heap is harmless if the heap is never
used, so ...

Hmm, this fails silently if the pattern can't be compiled.  I'll leave
any warnings to the next patch.


diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index 8c37c2d..97087a1 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -967,6 +967,16 @@ item(tt(HISTFILE))(
 The file to save the history in when an interactive shell exits.
 If unset, the history is not saved.
 )
+vindex(HISTORY_IGNORE)
+item(tt(HISTORY_IGNORE))(
+If set, is treated as a pattern at the time history files are written.
+Any potential history entry that matches the pattern is skipped.  For
+example, if the value is `tt(fc *)' then commands that invoke the
+interactive history editor are never written to the history file (compare
+the tt(HIST_NO_STORE) option or the tt(zshaddhistory) hook, either of
+which would prevent such commands from being added to the interactive
+history at all).
+)
 vindex(HISTSIZE)
 item(tt(HISTSIZE) <S>)(
 The maximum number of events stored in the internal history list.
diff --git a/Src/hist.c b/Src/hist.c
index fa5bdbb..1845bd8 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -2600,12 +2600,27 @@ savehistfile(char *fn, int err, int writeflags)
 	}
     }
     if (out) {
+	char *history_ignore;
+	Patprog histpat = NULL;
+
+	pushheap();
+
+	if ((history_ignore = getsparam("HISTORY_IGNORE")) != NULL) {
+	    tokenize(history_ignore = dupstring(history_ignore));
+	    remnulargs(history_ignore);
+	    histpat = patcompile(history_ignore, 0, NULL);
+	}
+
 	ret = 0;
 	for (; he && he->histnum <= xcurhist; he = down_histent(he)) {
 	    if ((writeflags & HFILE_SKIPDUPS && he->node.flags & HIST_DUP)
 	     || (writeflags & HFILE_SKIPFOREIGN && he->node.flags & HIST_FOREIGN)
 	     || he->node.flags & HIST_TMPSTORE)
 		continue;
+	    if (histpat &&
+		pattry(histpat, metafy(he->node.nam, -1, META_HEAPDUP))) {
+		continue;
+	    }
 	    if (writeflags & HFILE_SKIPOLD) {
 		if (he->node.flags & (HIST_OLD|HIST_NOWRITE))
 		    continue;
@@ -2685,6 +2700,8 @@ savehistfile(char *fn, int err, int writeflags)
 		histactive = remember_histactive;
 	    }
 	}
+
+	popheap();
     } else
 	ret = -1;
 



Messages sorted by: Reverse Date, Date, Thread, Author