From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2099 invoked from network); 13 Jun 2008 13:51:11 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.4 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 13 Jun 2008 13:51:11 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 22860 invoked from network); 13 Jun 2008 13:51:07 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 13 Jun 2008 13:51:07 -0000 Received: (qmail 28136 invoked by alias); 13 Jun 2008 13:51:04 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 25152 Received: (qmail 28119 invoked from network); 13 Jun 2008 13:51:03 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 13 Jun 2008 13:51:03 -0000 Received: from cluster-g.mailcontrol.com (cluster-g.mailcontrol.com [208.87.233.190]) by bifrost.dotsrc.org (Postfix) with ESMTPS id 07AA280525B2 for ; Fri, 13 Jun 2008 15:50:59 +0200 (CEST) Received: from cameurexb01.EUROPE.ROOT.PRI ([193.128.72.68]) by rly02g.srv.mailcontrol.com (MailControl) with ESMTP id m5DDoOhD020777 for ; Fri, 13 Jun 2008 14:50:51 +0100 Received: from news01.csr.com ([10.103.143.38]) by cameurexb01.EUROPE.ROOT.PRI with Microsoft SMTPSVC(6.0.3790.3959); Fri, 13 Jun 2008 14:50:49 +0100 Received: from news01.csr.com (localhost.localdomain [127.0.0.1]) by news01.csr.com (8.14.2/8.13.4) with ESMTP id m5DDoogP019905 for ; Fri, 13 Jun 2008 14:50:50 +0100 Received: from csr.com (pws@localhost) by news01.csr.com (8.14.2/8.14.2/Submit) with ESMTP id m5DDojmb019901 for ; Fri, 13 Jun 2008 14:50:50 +0100 X-Authentication-Warning: news01.csr.com: pws owned process doing -bs To: zsh-workers@sunsite.dk (Zsh hackers list) Subject: PATCH: zcalc escapes and completion X-Mailer: MH-E 8.0.3; nmh 1.3-RC1; GNU Emacs 22.1.1 Date: Fri, 13 Jun 2008 14:50:45 +0100 Message-ID: <19900.1213365045@csr.com> From: Peter Stephenson X-OriginalArrivalTime: 13 Jun 2008 13:50:49.0911 (UTC) FILETIME=[7D128470:01C8CD5C] X-Scanned-By: MailControl A-08-50-03 (www.mailcontrol.com) on 10.71.0.112 X-Virus-Scanned: ClamAV 0.92.1/7465/Fri Jun 13 15:16:01 2008 on bifrost X-Virus-Status: Clean Various zcalc enhancements: - escapes (i.e. commands other than expressions to evaluate) are now standardised as beginning with ":", which can't introduce a formula. However, I've left all the current commands for compatibility. - :! does a shell escape - :raw uses raw math expression output with no formatting - add a completion function to complete special commands as well as math. ":!" works properly, so you don't have to leave zcalc to type commands. Index: Completion/Zsh/Type/.distfiles =================================================================== RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Type/.distfiles,v retrieving revision 1.3 diff -u -r1.3 .distfiles --- Completion/Zsh/Type/.distfiles 15 Mar 2008 23:21:02 -0000 1.3 +++ Completion/Zsh/Type/.distfiles 13 Jun 2008 13:48:49 -0000 @@ -20,4 +20,5 @@ _parameters _suffix_alias_files _vars +_zcalc_line ' Index: Completion/Zsh/Type/_zcalc_line =================================================================== RCS file: Completion/Zsh/Type/_zcalc_line diff -N Completion/Zsh/Type/_zcalc_line --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Completion/Zsh/Type/_zcalc_line 13 Jun 2008 13:48:49 -0000 @@ -0,0 +1,83 @@ +#compdef -zcalc-line- + +# This handles completion of a zcalc command line read via vared. + +_zcalc_line_escapes() { + local -a cmds + cmds=( + "!:shell escape" + "q:quit" + "norm:normal output format" + "sci:scientific output format" + "fix:fixed point output format" + "eng:engineering (power of 1000) output format" + "raw:raw output format" + "local:make variables local" + "function:define math function" + ) + cmds=("\:"${^cmds}) + _describe -t command-escapes "command escapes" cmds -Q +} + +_zcalc_line() { + local expl + + if [[ CURRENT -eq 1 && $words[1] != ":!"* ]]; then + local -a alts + if [[ $words[1] = (|:*) ]]; then + alts=("command-escapes:command escape:_zcalc_line_escapes") + fi + if [[ $words[1] = (|[^:]*) ]]; then + alts+=("math:math formula:_math") + fi + _alternative $alts + return + fi + + case $words[1] in + (":!"*) + if [[ $words[1] = ":!" ]]; then + shift words + (( CURRENT >1 && CURRENT-- )) + else + words[1]=${words[1]##:\!} + if (( CURRENT == 1 )); then + compset -P ":\!" + fi + fi + _normal + ;; + + (:function) + # completing already defined user math functions is in fact exactly + # the wrong thing to do since currently zmathfuncdef won't overwrite, + # but it may jog the user's memory... + if (( CURRENT == 2 )); then + _wanted math-functions expl 'math function' \ + compadd -- ${${(k)functions:#^zsh_math_func_*}##zsh_math_func_} + else + _math + fi + ;; + + (:local) + _parameter + ;; + + (:(fix|sci|eng)) + if (( CURRENT == 2 )); then + _message "precision" + fi + ;& + + (:*) + _message "no more arguments" + ;; + + ([^:]*) + _math + ;; + esac +} + +_zcalc_line "$@" Index: Doc/Zsh/contrib.yo =================================================================== RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v retrieving revision 1.79 diff -u -r1.79 contrib.yo --- Doc/Zsh/contrib.yo 17 May 2008 22:42:16 -0000 1.79 +++ Doc/Zsh/contrib.yo 13 Jun 2008 13:48:50 -0000 @@ -1825,8 +1825,8 @@ line preceded by `tt(4> )' is available as tt($4). The last value calculated is available as tt(ans). Full command line editing, including the history of previous calculations, is available; the history is saved in -the file tt(~/.zcalc_history). To exit, enter a blank line or type `tt(q)' -on its own. +the file tt(~/.zcalc_history). To exit, enter a blank line or type `tt(:q)' +on its own (`tt(q)' is allowed for historical compatibility). If arguments are given to tt(zcalc) on start up, they are used to prime the first few positional parameters. A visual indication of this is given when @@ -1846,37 +1846,51 @@ referred to in tt(ZCALCPROMPT) as `tt(%1v)'. The default prompt is `tt(%1v> )'. +A few special commands are available; these are introduced by a colon. +For backward compatibility, the colon may be omitted for certain +commands. Completion is available if tt(compinit) has been run. + The output precision may be specified within zcalc by special commands -familiar from many calculators: +familiar from many calculators. startitem() -item(tt(norm))( +item(tt(:norm))( The default output format. It corresponds to the printf tt(%g) specification. Typically this shows six decimal digits. ) -item(tt(sci) var(digits))( +item(tt(:sci) var(digits))( Scientific notation, corresponding to the printf tt(%g) output format with the precision given by var(digits). This produces either fixed point or exponential notation depending on the value output. ) -item(tt(fix) var(digits))( +item(tt(:fix) var(digits))( Fixed point notation, corresponding to the printf tt(%f) output format with the precision given by var(digits). ) -item(tt(eng) var(digits))( +item(tt(:eng) var(digits))( Exponential notation, corresponding to the printf tt(%E) output format with the precision given by var(digits). ) +item(tt(:raw))( +Raw output: this is the default form of the output from a math +evaluation. This may show more precision than the number actually +possesses. +) enditem() Other special commands: startitem() -item(tt(local) var(arg) ...)( +item(tt(:!)var(line...))( +Execute var(line...) as a normal shell command line. Note that it +is executed in the context of the function, i.e. with local variables. +Space is optional after tt(:!). +) +item(tt(:local) var(arg) ...)( Declare variables local to the function. Note that certain variables are used by the function for its own purposes. Other variables may be used, too, but they will be taken from or put into the global scope. ) -item(tt(function) var(name) [ var(body) ])( +item(tt(:function) var(name) [ var(body) ])( Define a mathematical function or (with no var(body)) delete it. The function is defined using tt(zmathfuncdef), see below. @@ -1887,14 +1901,14 @@ defines a function to cube the sole argument. ) item(tt([#)var(base)tt(]))( -When this syntax appears on a line by itself, the default output radix -is set to var(base). Use, for example, `tt([#16])' to display hexadecimal -output preceded by an indication of the base, or `tt([##16])' just to -display the raw number in the given base. Bases themselves are always -specified in decimal. `tt([#])' restores the normal output format. Note -that setting an output base suppresses floating point output; use `tt([#])' -to return to normal operation. - +This is not a special command, rather part of normal arithmetic +syntax; however, when this form appears on a line by itself the default +output radix is set to var(base). Use, for example, `tt([#16])' to display +hexadecimal output preceded by an indication of the base, or `tt([##16])' +just to display the raw number in the given base. Bases themselves are +always specified in decimal. `tt([#])' restores the normal output format. +Note that setting an output base suppresses floating point output; use +`tt([#])' to return to normal operation. ) enditem() Index: Functions/Misc/zcalc =================================================================== RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v retrieving revision 1.15 diff -u -r1.15 zcalc --- Functions/Misc/zcalc 13 Dec 2007 22:20:59 -0000 1.15 +++ Functions/Misc/zcalc 13 Jun 2008 13:48:50 -0000 @@ -96,16 +96,16 @@ # TODO: make local variables that shouldn't be visible in expressions # begin with _. local line ans base defbase forms match mbegin mend psvar optlist opt arg -local compcontext="-math-" +local compcontext="-zcalc-line-" integer num outdigits outform=1 # We use our own history file with an automatic pop on exit. history -ap "${ZDOTDIR:-$HOME}/.zcalc_history" -forms=( '%2$g' '%.*g' '%.*f' '%.*E' ) +forms=( '%2$g' '%.*g' '%.*f' '%.*E' '') zmodload -i zsh/mathfunc 2>/dev/null -autoload zmathfuncdef +autoload -U zmathfuncdef : ${ZCALCPROMPT="%1v> "} @@ -176,35 +176,62 @@ print -s -- $line - case ${${line##[[:blank:]]#}%%[[:blank:]]#} in - (q) # Exit if `q' on its own. - return 0 + line="${${line##[[:blank:]]#}%%[[:blank:]]#}" + case "$line" in + # Escapes begin with a colon + (:!*) + # shell escape + eval ${line##:\![[:blank:]]#} + line= + continue + ;; + + ((:|)q) + # Exit + return 0 ;; - (norm) # restore output format to default + + ((:|)norm) # restore output format to default outform=1 ;; - (sci[[:blank:]]#(#b)(<->)(#B)) + + ((:|)sci[[:blank:]]#(#b)(<->)(#B)) outdigits=$match[1] outform=2 ;; - (fix[[:blank:]]#(#b)(<->)(#B)) + + ((:|)fix[[:blank:]]#(#b)(<->)(#B)) outdigits=$match[1] outform=3 ;; - (eng[[:blank:]]#(#b)(<->)(#B)) + + ((:|)eng[[:blank:]]#(#b)(<->)(#B)) outdigits=$match[1] outform=4 ;; - (local([[:blank:]]##*|)) + + (:raw) + outform=5 + ;; + + ((:|)local([[:blank:]]##*|)) eval $line line= continue ;; - (function[[:blank:]]##(#b)([^[:blank:]]##)(|[[:blank:]]##([^[:blank:]]*))) + + ((:|)function[[:blank:]]##(#b)([^[:blank:]]##)(|[[:blank:]]##([^[:blank:]]*))) zmathfuncdef $match[1] $match[3] line= continue ;; + + (:*) + print "Unrecognised escape" + 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 @@ -222,7 +249,11 @@ if [[ -n $base ]]; then print -- $(( $base $ans )) elif [[ $ans = *.* ]] || (( outdigits )); then - printf "$forms[outform]\n" $outdigits $ans + if [[ -z $forms[outform] ]]; then + print -- $(( $ans )) + else + printf "$forms[outform]\n" $outdigits $ans + fi else printf "%d\n" $ans fi