From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh Hackers' List <zsh-workers@zsh.org>
Subject: PATCH: more zcalc featurettes
Date: Fri, 15 Mar 2013 20:41:15 +0000 [thread overview]
Message-ID: <20130315204115.0c123676@pws-pc.ntlworld.com> (raw)
Hardly worth posting unless anyone can think of why it's a bad idea to
assume an input line consisting of an arithmetic formula should contain
at least as many close parentheses as open parentheses.
- Display the current answer in the new base when changing the default
output base.
- Prompt for more input if uncompleted parenthesised expressions.
- Allow :function to be abbreviated as :func or :f.
Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.150
diff -p -u -r1.150 contrib.yo
--- Doc/Zsh/contrib.yo 15 Mar 2013 20:15:10 -0000 1.150
+++ Doc/Zsh/contrib.yo 15 Mar 2013 20:39:31 -0000
@@ -3144,12 +3144,14 @@ line preceded by `tt(4> )' is available
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 (`tt(q)' is allowed for historical compatibility). A
-line ending with a single backslash is treated in the same fashion
+on its own (`tt(q)' is allowed for historical compatibility).
+
+A line ending with a single backslash is treated in the same fashion
as it is in command line editing: the backslash is removed, the
function prompts for more input (the prompt is preceded by `tt(...)'
to indicate this), and the lines are combined into one to get the final
-result.
+result. In addition, if the input so far contains more open than
+close parentheses tt(zcalc) will prompt for more input.
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
@@ -3219,11 +3221,13 @@ scope.
)
item(tt(:function) var(name) [ var(body) ])(
Define a mathematical function or (with no var(body)) delete it.
+tt(:function) may be abbreviated to tt(:func) or simply tt(:f).
+The var(name) may contain the same characters as a shell function name.
The function is defined using tt(zmathfuncdef), see below.
Note that tt(zcalc) takes care of all quoting. Hence for example:
-example(function cube $1 * $1 * $1)
+example(:f cube $1 * $1 * $1)
defines a function to cube the sole argument.
)
Index: Functions/Misc/zcalc
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v
retrieving revision 1.22
diff -p -u -r1.22 zcalc
--- Functions/Misc/zcalc 15 Mar 2013 20:15:10 -0000 1.22
+++ Functions/Misc/zcalc 15 Mar 2013 20:39:31 -0000
@@ -166,6 +166,14 @@ while vared -cehp "${cont_prompt}${ZCALC
line="$prev_line$line"
prev_line=
cont_prompt=
+ # Test whether there are as many open as close
+ # parentheses in the line so far.
+ if [[ ${#line//[^\(]} -gt ${#line//[^\)]} ]]; then
+ prev_line+=$line
+ cont_prompt="..."
+ line=
+ continue
+ fi
[[ -z $line ]] && break
# special cases
# Set default base if `[#16]' or `[##16]' etc. on its own.
@@ -178,6 +186,7 @@ while vared -cehp "${cont_prompt}${ZCALC
defbase=$match[1]
fi
print -s -- $line
+ print -- $(( ${defbase} ans ))
line=
continue
else
@@ -233,7 +242,7 @@ while vared -cehp "${cont_prompt}${ZCALC
continue
;;
- ((:|)function[[:blank:]]##(#b)([^[:blank:]]##)(|[[:blank:]]##([^[:blank:]]*)))
+ ((function|:f(unc(tion|)|))[[:blank:]]##(#b)([^[:blank:]]##)(|[[:blank:]]##([^[:blank:]]*)))
zmathfuncdef $match[1] $match[3]
line=
continue
--
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
reply other threads:[~2013-03-15 21:12 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130315204115.0c123676@pws-pc.ntlworld.com \
--to=p.w.stephenson@ntlworld.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).