summaryrefslogtreecommitdiff
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
parent52b8303537015f603f35351abe2a01d8779868fe (diff)
downloadzsh-abd901094ea047da2b7b384f74af112e8453034c.tar.gz
zsh-abd901094ea047da2b7b384f74af112e8453034c.zip
16352: allow output formats to be specified in zcalc
-rw-r--r--ChangeLog5
-rw-r--r--Functions/Misc/zcalc73
2 files changed, 52 insertions, 26 deletions
diff --git a/ChangeLog b/ChangeLog
index 511c4904f..5dc8caac3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
2001-12-17 Oliver Kiddle <opk@zsh.org>
+ * 16352: Functions/Misc/zcalc: allow output formats to be
+ specified in zcalc
+
* 16353: Src/exec.c, Src/lex.c, Src/params.c, Src/parse.c,
Src/text.c, Src/zsh.h, Doc/Zsh/params.yo, Test/.distfiles,
Test/A06assign.ztst: add += parameter assignments
@@ -148,7 +151,7 @@
* 16254: Completion/Unix/Command/_yp: allow completion of map
names after ypmatch.
-2001-11-15 Chmouel Boudjnah <chmouel@mandrakesoft.com>
+2001-11-15 Chmouel Boudjnah <chmouel@mandrakesoft.com>
* 16253: Src/Zle/complete.mdd: Add Mandrake completion directory to
install.
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