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

Re: Calculator function

"Bart Schaefer" wrote:
> > +Each line typed is evaluated as an expression.  The prompt shows a number,
> > +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).
> Why not have the prompt look like `$4= ' instead of `4> '?

Probably not having thought of it.  But possibly because I wanted it to
look a prompt, since it prefixes the formula rather than the result.  It
can be made configurable with some psvar trickery.  See patch.

(This should really be expanded so that the same prompt is used to show
parameters passed on the command line, which I haven't done yet.)

> > +# Line editing and history are available. A blank line or `q' quits.
> Hrm, I'd rather that blank lines were just ignored ... I think ...

I didn't like the idea that `q' didn't just show the variable $q,
however (despite the double negative).  It's a good idea to make ^D end
of file --- the option -e to vared handles this properly, so I've added
that.  Maybe I can delete both the q and the empty line thing.
> > +# To do:
> > +# - separate zcalc history from shell history using arrays --- or allow
> > +#   zsh to switch internally to and from array-based history.
> I posted a function some while back that switches history contexts by
> dumping the history to a file, then zeroing and restoring HISTSIZE.  I
> can't seem to track it down just now ... there's something sort of like
> it in zsh-users/3506.
> What were you thinking of, here?

That function would probably be exactly what was needed.

The other proposal was that instead of using an invisible internal list
manipulated by the history code, the shell code would use an array
variable containing the lines and perhaps an index variable.  Zle would
manipulate the index to reflect the history setting, (or we could find
another way of doing this) but the function would be responsible for any
changes to the array and all the household management done in the usual
case by the history options

Index: Functions/Misc/zcalc
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v
retrieving revision 1.1
diff -u -r1.1 zcalc
--- Functions/Misc/zcalc	2001/07/27 11:34:46	1.1
+++ Functions/Misc/zcalc	2001/07/27 22:10:18
@@ -84,11 +84,13 @@
 emulate -L zsh
 setopt extendedglob
-local line latest base defbase match mbegin mend
+local line latest base defbase match mbegin mend psvar
 integer num
 zmodload -i zsh/mathfunc 2>/dev/null
+: ${ZCALCPROMPT="%1v> "}
 # Supply some constants.
 float PI E
 (( PI = 4 * atan(1), E = exp(1) ))
@@ -101,7 +103,8 @@
   print "$num> $argv[$num]"
-while vared -chp "$num> " line; do
+while vared -cehp "${(%)ZCALCPROMPT}" line; do
   [[ -z $line ]] && break
   # special cases
   # Set default base if `[#16]' or `[##16]' etc. on its own.
@@ -134,6 +137,7 @@
     # arrays always store scalars anyway.
     eval "latest=\$(( $base $line ))"
+    psvar[1]=$num
     print -- $latest
Index: Doc/Zsh/contrib.yo
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.13
diff -u -r1.13 contrib.yo
--- Doc/Zsh/contrib.yo	2001/07/27 11:34:46	1.13
+++ Doc/Zsh/contrib.yo	2001/07/27 22:10:23
@@ -820,6 +820,12 @@
 the given base.  Bases themselves are always specified in decimal.
 `tt([#])' restores the normal output format.
+The prompt is configurable via the parameter tt(ZCALCPROMPT), which
+undergoes standard prompt expansion.  The index of the current entry is
+stored locally in the first element of the array tt(psvar), which can be
+referred to in tt(ZCALCPROMPT) as `tt(%1v)'.  The default prompt is
+`tt(%1v> )'.
 See the comments in the function for a few extra tips.

Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxx>
Work: pws@xxxxxxx
Web: http://www.pwstephenson.fsnet.co.uk

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