1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-07 09:21:18 +02:00

31940: zcalc -e uses arguments as input

This commit is contained in:
Peter Stephenson 2013-11-08 11:34:37 +00:00
parent f77a7a5b18
commit 12251d65ff
3 changed files with 37 additions and 10 deletions

View file

@ -1,3 +1,8 @@
2013-11-08 Peter Stephenson <p.stephenson@samsung.com>
* 31940: Doc/Zsh/contrib.yo, Functions/Misc/zcalc: add -e option
to zcalc to use input from arguments to command.
2013-11-07 Peter Stephenson <p.stephenson@samsung.com> 2013-11-07 Peter Stephenson <p.stephenson@samsung.com>
* 31937: Doc/Zsh/zle.yo, Src/Zle/zle_main.c, * 31937: Doc/Zsh/zle.yo, Src/Zle/zle_main.c,

View file

@ -3155,7 +3155,7 @@ sect(Mathematical Functions)
startitem() startitem()
findex(zcalc) findex(zcalc)
item(tt(zcalc) [ var(expression) ... ])( item(tt(zcalc) [ tt(-ef) ] [ var(expression) ... ])(
A reasonably powerful calculator based on zsh's arithmetic evaluation A reasonably powerful calculator based on zsh's arithmetic evaluation
facility. The syntax is similar to that of formulae in most programming facility. The syntax is similar to that of formulae in most programming
languages; see languages; see
@ -3195,6 +3195,10 @@ The output base can be initialised by passing the option `tt(-#)var(base)',
for example `tt(zcalc -#16)' (the `tt(#)' may have to be quoted, depending for example `tt(zcalc -#16)' (the `tt(#)' may have to be quoted, depending
on the globbing options set). on the globbing options set).
If the option `tt(-e)' is set, the function runs non-interactively:
the arguments are treated as expressions to be evaluated as if entered
interactively line by line.
If the option `tt(-f)' is set, all numbers are treated as floating If the option `tt(-f)' is set, all numbers are treated as floating
point, hence for example the expression `tt(3/4)' evaluates to 0.75 point, hence for example the expression `tt(3/4)' evaluates to 0.75
rather than 0. Options must appear in separate words. rather than 0. Options must appear in separate words.

View file

@ -85,10 +85,13 @@
# similarly -##<base>; they set the default output base, with and without # similarly -##<base>; they set the default output base, with and without
# a base discriminator in front, respectively. # a base discriminator in front, respectively.
# #
# # With the option -e, the arguments are evaluated as if entered
# To do: # interactively. So, for example:
# - separate zcalc history from shell history using arrays --- or allow # zcalc -e -\#16 -e 1055
# zsh to switch internally to and from array-based history. # prints
# 0x41f
# Any number of expressions may be given and they are evaluated
# sequentially just as if read automatically.
emulate -L zsh emulate -L zsh
setopt extendedglob setopt extendedglob
@ -97,7 +100,8 @@ setopt extendedglob
# begin with _. # begin with _.
local line ans base defbase forms match mbegin mend psvar optlist opt arg local line ans base defbase forms match mbegin mend psvar optlist opt arg
local compcontext="-zcalc-line-" local compcontext="-zcalc-line-"
integer num outdigits outform=1 integer num outdigits outform=1 expression_mode
local -a expressions
# We use our own history file with an automatic pop on exit. # We use our own history file with an automatic pop on exit.
history -ap "${ZDOTDIR:-$HOME}/.zcalc_history" history -ap "${ZDOTDIR:-$HOME}/.zcalc_history"
@ -114,7 +118,7 @@ float PI E
(( PI = 4 * atan(1), E = exp(1) )) (( PI = 4 * atan(1), E = exp(1) ))
# Process command line # Process command line
while [[ -n $1 && $1 = -(|[#-]*|f) ]]; do while [[ -n $1 && $1 = -(|[#-]*|f|e) ]]; do
optlist=${1[2,-1]} optlist=${1[2,-1]}
shift shift
[[ $optlist = (|-) ]] && break [[ $optlist = (|-) ]] && break
@ -130,11 +134,11 @@ while [[ -n $1 && $1 = -(|[#-]*|f) ]]; do
arg=$1 arg=$1
shift shift
else else
print "-# requires an argument" >&2 print -- "-# requires an argument" >&2
return 1 return 1
fi fi
if [[ $arg != (|\#)[[:digit:]]## ]]; then if [[ $arg != (|\#)[[:digit:]]## ]]; then
print - "-# requires a decimal number as an argument" >&2 print -- "-# requires a decimal number as an argument" >&2
return 1 return 1
fi fi
defbase="[#${arg}]" defbase="[#${arg}]"
@ -142,10 +146,18 @@ while [[ -n $1 && $1 = -(|[#-]*|f) ]]; do
(f) # Force floating point operation (f) # Force floating point operation
setopt forcefloat setopt forcefloat
;; ;;
(e) # Arguments are expressions
(( expression_mode = 1 ));
;;
esac esac
done done
done done
if (( expression_mode )); then
expressions=("$@")
argv=()
fi
for (( num = 1; num <= $#; num++ )); do for (( num = 1; num <= $#; num++ )); do
# Make sure all arguments have been evaluated. # Make sure all arguments have been evaluated.
# The `$' before the second argv forces string rather than numeric # The `$' before the second argv forces string rather than numeric
@ -156,7 +168,13 @@ done
psvar[1]=$num psvar[1]=$num
local prev_line cont_prompt local prev_line cont_prompt
while vared -cehp "${cont_prompt}${ZCALCPROMPT}" line; do while (( expression_mode )) ||
vared -cehp "${cont_prompt}${ZCALCPROMPT}" line; do
if (( expression_mode )); then
(( ${#expressions} )) || break
line=$expressions[1]
shift expressions
fi
if [[ $line = (|*[^\\])('\\')#'\' ]]; then if [[ $line = (|*[^\\])('\\')#'\' ]]; then
prev_line+=$line[1,-2] prev_line+=$line[1,-2]
cont_prompt="..." cont_prompt="..."