summary refs log tree commit diff
path: root/Functions/Misc
diff options
context:
space:
mode:
Diffstat (limited to 'Functions/Misc')
-rw-r--r--Functions/Misc/.distfiles1
-rw-r--r--Functions/Misc/zcalc31
2 files changed, 24 insertions, 8 deletions
diff --git a/Functions/Misc/.distfiles b/Functions/Misc/.distfiles
index 9b078cb65..08cd89554 100644
--- a/Functions/Misc/.distfiles
+++ b/Functions/Misc/.distfiles
@@ -3,4 +3,5 @@ DISTFILES_SRC='
 allopt      getjobs       mere       relative   zcalc   zmv          zargs
 checkmail   harden        nslookup   run-help   zed     zrecompile
 colors      is-at-least   promptnl   tetris     zkbd    zstyle+
+zmathfuncdef
 '
diff --git a/Functions/Misc/zcalc b/Functions/Misc/zcalc
index b83a939c9..9ce02c02f 100644
--- a/Functions/Misc/zcalc
+++ b/Functions/Misc/zcalc
@@ -42,6 +42,13 @@
 # use the variables listed in the `local' and `integer' lines below
 # (translation: I can't be bothered to provide a sandbox).
 #
+# You can declare or delete math functions (implemented via zmathfuncdef):
+#   1> function cube $1 * $1 * $1
+# This has a single compulsory argument.  Note the function takes care of
+# the punctuation.  To delete the function, put nothing (at all) after
+# the function name:
+#   1> function cube
+#
 # Some constants are already available: (case sensitive as always):
 #   PI     pi, i.e. 3.1415926545897931
 #   E      e, i.e. 2.7182818284590455
@@ -86,6 +93,8 @@
 emulate -L zsh
 setopt extendedglob
 
+# 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-"
 integer num outdigits outform=1
@@ -96,6 +105,7 @@ history -ap "${ZDOTDIR:-$HOME}/.zcalc_history"
 forms=( '%2$g' '%.*g' '%.*f' '%.*E' )
 
 zmodload -i zsh/mathfunc 2>/dev/null
+autoload zmathfuncdef
 
 : ${ZCALCPROMPT="%1v> "}
 
@@ -167,34 +177,39 @@ while vared -cehp "${(%)ZCALCPROMPT}" line; do
   print -s -- $line
 
   case ${${line##[[:blank:]]#}%%[[:blank:]]#} in
-    q) # Exit if `q' on its own.
+    (q) # Exit if `q' on its own.
       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:]]##*|))
+    (local([[:blank:]]##*|))
       eval $line
       line=
       continue
     ;;
-    *)
+    (function[[:blank:]]##(#b)([^[:blank:]]##)(|[[:blank:]]##([^[:blank:]]*)))
+      zmathfuncdef $match[1] $match[3]
+      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 ))"