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

Re: Local history



Bart Schaefer wrote:
> As Sven mentioned, I've done this before.  The basic trick is that HISTSIZE
> truncates the history as soon as you assign to it, so the technique goes
> like this:
> 
> 	local temphist=${TMPPREFIX}hist SAVEHIST=$HISTSIZE
> 	HISTFILE=$temphist
> 	fc -W				# Save internal history
> 	local HISTSIZE=0		# Truncate internal history
> 	HISTSIZE=$SAVEHIST
> 	HISTFILE=~/.zcalc-history
> 	fc -R				# Read previous zcalc history
> 	# do zcalc stuff ...
> 	fc -W				# Save zcalc history
> 	HISTFILE=$temphist
> 	fc -R				# Reload old history

That should obviously restore the original HISTFILE at the second last
line.

I adapted this into the following, and it's mostly working fine.  However,
every time it saves back to ~/.zcalc_history, it adds two lines from the
old history at the start which I can't seem to get rid of.  Possibly
trimming SAVEHIST to the actual number of lines to save might work.

Index: Functions/Misc/zcalc
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v
retrieving revision 1.5
diff -u -r1.5 zcalc
--- Functions/Misc/zcalc	2001/11/15 18:42:33	1.5
+++ Functions/Misc/zcalc	2001/11/19 17:27:44
@@ -88,6 +88,25 @@
 emulate -L zsh
 setopt extendedglob
 
+# can't be local since required in EXIT trap
+zcalc_orighist=$HISTFILE 
+local temphist=${TMPPREFIX}hist SAVEHIST=$HISTSIZE
+HISTFILE=$temphist
+fc -W
+
+local HISTSIZE=0
+HISTSIZE=$SAVEHIST
+HISTFILE=~/.zcalc_history
+[[ -f $HISTFILE ]] && fc -R
+
+zcalc_restore() {
+    unfunction zcalc_restore
+    fc -W
+    HISTFILE=$zcalc_orighist
+    fc -R
+}
+trap zcalc_restore HUP INT QUIT EXIT
+
 local line latest base defbase match mbegin mend psvar optlist opt arg
 integer num
 
-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 392070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************



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