zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: zcalc.
@ 2001-11-15 18:39 Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2001-11-15 18:39 UTC (permalink / raw)
  To: Zsh hackers list

Secondly, allow a default output base to be specified as an option to
zcalc.

Index: Functions/Misc/zcalc
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v
retrieving revision 1.4
diff -u -r1.4 zcalc
--- Functions/Misc/zcalc	2001/10/08 15:18:26	1.4
+++ Functions/Misc/zcalc	2001/11/15 18:38:26
@@ -74,7 +74,11 @@
 # also understood.  However, leading 0 for octal is not understood --- it's
 # too confusing in a calculator.  Use 8#777 etc.
 #
+# Options: -#<base> is the same as a line containing just `[#<base>],
+# similarly -##<base>; they set the default output base, with and without
+# a base discriminator in front, respectively.
 #
+#
 # To do:
 # - separate zcalc history from shell history using arrays --- or allow
 #   zsh to switch internally to and from array-based history.
@@ -84,7 +88,7 @@
 emulate -L zsh
 setopt extendedglob
 
-local line latest base defbase match mbegin mend psvar
+local line latest base defbase match mbegin mend psvar optlist opt arg
 integer num
 
 zmodload -i zsh/mathfunc 2>/dev/null
@@ -94,6 +98,36 @@
 # Supply some constants.
 float PI E
 (( PI = 4 * atan(1), E = exp(1) ))
+
+# Process command line
+while [[ -n $1 && $1 = -(|[#-]*) ]]; do
+  optlist=${1[2,-1]}
+  shift
+  [[ $optlist = (|-) ]] && break
+  while [[ -n $optlist ]]; do
+    opt=${optlist[1]}
+    optlist=${optlist[2,-1]}
+    case $opt in
+      ('#') # Default base
+            if [[ -n $optlist ]]; then
+	       arg=$optlist
+	       optlist=
+	    elif [[ -n $1 ]]; then
+	       arg=$1
+	       shift
+	    else
+	       print "-# requires an argument" >&2
+	       return 1
+	    fi
+	    if [[ $arg != (|\#)[[:digit:]]## ]]; then
+	      print - "-# requires a decimal number as an argument" >&2
+	      return 1
+	    fi
+            defbase="[#${arg}]"
+	    ;;
+    esac
+  done
+done
 
 for (( num = 1; num <= $#; num++ )); do
   # Make sure all arguments have been evaluated.

-- 
Peter Stephenson <pws@csr.com>                  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.
**********************************************************************


^ permalink raw reply	[flat|nested] 2+ messages in thread

* PATCH: zcalc
@ 2001-08-16 16:32 Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2001-08-16 16:32 UTC (permalink / raw)
  To: Zsh hackers list

If you were using a default base without showing the leading N#
(e.g. [##16]) using a previous value didn't get the base right.

Index: Functions/Misc/zcalc
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v
retrieving revision 1.2
diff -u -r1.2 zcalc
--- Functions/Misc/zcalc	2001/07/27 22:23:16	1.2
+++ Functions/Misc/zcalc	2001/08/16 16:31:14
@@ -135,10 +135,13 @@
     # Latest value is stored as a string, because it might be floating
     # point or integer --- we don't know till after the evaluation, and
     # arrays always store scalars anyway.
-    eval "latest=\$(( $base $line ))"
+    # 
+    # 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.
+    eval "latest=\$(( $line ))"
     argv[num++]=$latest
     psvar[1]=$num
-    print -- $latest
+    print -- $(( $base $latest ))
   fi
   line=
 done

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, 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.
**********************************************************************


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-11-15 18:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-15 18:39 PATCH: zcalc Peter Stephenson
  -- strict thread matches above, loose matches on Subject: below --
2001-08-16 16:32 Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).