Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: more zcalc featurettes



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@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author