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

PATCH: zcalc.



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