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

PATCH: zcalc update

Bits I've had lying around: implement the history as suggested by Bart,
fix a problem that an error in the evaluation would abort the function (and
possibly leave you with ~/.zcalc_history as the history file), and document
the -#base option.

Index: Doc/Zsh/contrib.yo
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.16
diff -u -r1.16 contrib.yo
--- Doc/Zsh/contrib.yo	2001/09/21 02:40:38	1.16
+++ Doc/Zsh/contrib.yo	2001/12/07 12:49:39
@@ -828,8 +828,9 @@
 which corresponds to a positional parameter where the result of that
 calculation is stored.  For example, the result of the calculation on the
 line preceeded by `tt(4> )' is available as tt($4).  Full command line
-editing, including the history of previous calculations, is available.
-To exit, enter a blank line or type `tt(q)' on its own.
+editing, including the history of previous calculations, is available; the
+history is saved in the file tt(~/.zcalc_history).  To exit, enter a blank
+line or type `tt(q)' on its own.
 If arguments are given to tt(zcalc) on start up, they are used to prime the
 first few positional parameters.  A visual indication of this is given when
@@ -844,6 +845,10 @@
 indication of the base, or `tt([##16])' just to display the raw number in
 the given base.  Bases themselves are always specified in decimal.
 `tt([#])' restores the normal output format.
+The output base can be initialised by passing the option `tt(-#)var(base)',
+for example `tt(zcalc -#16)' (the `tt(#)' may have to be quoted, depending
+on the globbing options set).
 The prompt is configurable via the parameter tt(ZCALCPROMPT), which
 undergoes standard prompt expansion.  The index of the current entry is
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/12/07 12:49:39
@@ -88,6 +88,25 @@
 emulate -L zsh
 setopt extendedglob
+# can't be local since required in EXIT trap
+local temphist=${TMPPREFIX}hist SAVEHIST=$HISTSIZE
+fc -W
+local HISTSIZE=0
+[[ -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
@@ -172,7 +191,10 @@
     # Since it's a string, we'd better make sure we know which
     # base it's in, so don't change that until we actually print it.
+    latest=
     eval "latest=\$(( $line ))"
+    # on error $latest is not set; let user re-edit line
+    [[ -n $latest ]] || continue
     if [[ -z $base ]]; then

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 
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