about summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
authorOliver Kiddle <opk@users.sourceforge.net>2001-12-17 17:24:09 +0000
committerOliver Kiddle <opk@users.sourceforge.net>2001-12-17 17:24:09 +0000
commitabd901094ea047da2b7b384f74af112e8453034c (patch)
treecb52717ba40df18791f4d2d67c14f7ec3a7ef110 /Functions
parent52b8303537015f603f35351abe2a01d8779868fe (diff)
downloadzsh-abd901094ea047da2b7b384f74af112e8453034c.tar.gz
zsh-abd901094ea047da2b7b384f74af112e8453034c.tar.xz
zsh-abd901094ea047da2b7b384f74af112e8453034c.zip
16352: allow output formats to be specified in zcalc
Diffstat (limited to 'Functions')
-rw-r--r--Functions/Misc/zcalc73
1 files changed, 48 insertions, 25 deletions
diff --git a/Functions/Misc/zcalc b/Functions/Misc/zcalc
index 5e01e73d0..633a38e92 100644
--- a/Functions/Misc/zcalc
+++ b/Functions/Misc/zcalc
@@ -82,8 +82,6 @@
 # To do:
 # - separate zcalc history from shell history using arrays --- or allow
 #   zsh to switch internally to and from array-based history.
-# - allow setting number of decimal places for display, scientific notation, 
-#   etc.
 
 emulate -L zsh
 setopt extendedglob
@@ -107,8 +105,10 @@ zcalc_restore() {
 }
 trap zcalc_restore HUP INT QUIT EXIT
 
-local line latest base defbase match mbegin mend psvar optlist opt arg
-integer num
+local line ans base defbase forms match mbegin mend psvar optlist opt arg
+integer num outdigits outform=1
+
+forms=( '%2$g' '%.*g' '%.*f' '%.*E' )
 
 zmodload -i zsh/mathfunc 2>/dev/null
 
@@ -178,30 +178,53 @@ while vared -cehp "${(%)ZCALCPROMPT}" line; do
   else
     base=$defbase
   fi
-  # Exit if `q' on its own.
-  [[ $line = [[:blank:]]#q[[:blank:]]# ]] && return 0
 
   print -s -- $line
-  if [[ $line = [[:blank:]]#local([[:blank:]]##*|) ]]; then
-    eval $line
+
+  case ${${line##[[:blank:]]#}%%[[:blank:]]#} in
+    q) # Exit if `q' on its own.
+      return 0
+    ;;
+    norm) # restore output format to default
+      outform=1
+    ;;
+    sci[[:blank:]]#(#b)(<->)(#B))
+      outdigits=$match[1]
+      outform=2
+    ;;
+    fix[[:blank:]]#(#b)(<->)(#B))
+      outdigits=$match[1]
+      outform=3
+    ;;
+    eng[[:blank:]]#(#b)(<->)(#B))
+      outdigits=$match[1]
+      outform=4
+    ;;
+    local([[:blank:]]##*|))
+      eval $line
+      line=
+      continue
+    ;;
+    *)
+      # 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.
+      # 
+      # 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 "ans=\$(( $line ))"
+      # on error $ans is not set; let user re-edit line
+      [[ -n $ans ]] || continue
+      argv[num++]=$ans
+      psvar[1]=$num
+    ;;
+  esac
+  if [[ -n $base ]]; then
+    print -- $(( $base $ans ))
+  elif [[ $ans = *.* ]] || (( outdigits )); then
+    printf "$forms[outform]\n" $outdigits $ans
   else
-    # 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.
-    # 
-    # 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
-    argv[num++]=$latest
-    psvar[1]=$num
-    if [[ -z $base ]]; then
-      print -- $latest
-    else
-      print -- $(( $base $latest ))
-    fi
+    printf "%d\n" $ans
   fi
   line=
 done