summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2009-05-19 15:33:24 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2009-05-19 15:33:24 +0000
commitad3d514ac79b573ce43fa7089175de519f39f21d (patch)
treec5dc3b1284102a6e51c01d210bae81d8bb475f85
parentcac27544ff9d42069acd89a7587a2f9b581a9a2b (diff)
downloadzsh-ad3d514ac79b573ce43fa7089175de519f39f21d.tar.gz
zsh-ad3d514ac79b573ce43fa7089175de519f39f21d.tar.xz
zsh-ad3d514ac79b573ce43fa7089175de519f39f21d.zip
26973: zmathfuncdef enhancements
-rw-r--r--Doc/Zsh/contrib.yo9
-rw-r--r--Functions/Misc/zmathfuncdef20
2 files changed, 25 insertions, 4 deletions
diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 7412f810d..620aaea3d 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -2371,7 +2371,7 @@ enditem()
 See the comments in the function for a few extra tips.
 )
 findex(zmathfuncdef)
-item(tt(zmathfuncdef) var(mathfunc) [ var(body) ])(
+item(tt(zmathfuncdef) [ var(mathfunc) [ var(body) ] ])(
 A convenient front end to tt(functions -M).
 
 With two arguments, define a mathematical function named var(mathfunc)
@@ -2383,10 +2383,15 @@ to refer to optional parameters.  Note that the forms must be
 strictly adhered to for the function to calculate the correct number
 of arguments.  The implementation is held in a shell function named
 tt(zsh_math_func_)var(mathfunc); usually the user will not need
-to refer to the shell function directly.
+to refer to the shell function directly.  Any existing function
+of the same name is silently replaced.
 
 With one argument, remove the mathematical function var(mathfunc)
 as well as the shell function implementation.
+
+With no arguments, list all var(mathfunc) functions in a form
+suitable for restoring the definition.
+The functions have not necessarily been defined by tt(zmathfuncdef).
 )
 enditem()
 
diff --git a/Functions/Misc/zmathfuncdef b/Functions/Misc/zmathfuncdef
index 21d3c249e..e5692e769 100644
--- a/Functions/Misc/zmathfuncdef
+++ b/Functions/Misc/zmathfuncdef
@@ -5,18 +5,34 @@
 
 emulate -L zsh
 setopt extendedglob
+local -a match mbegin mend line
+local func
 
-if (( $# < 1 || $# > 2 )); then
-  print "Usage: $0 name [body]" >&2
+if (( $# > 2 )); then
+  print "Usage: $0 [name [body]]" >&2
   return 1
 fi
 
+zmodload -i zsh/parameter || return 1
+
+if (( $# == 0 )); then
+  functions -M | while read -A line; do
+    func=${functions[$line[6]]}
+    if [[ $func = (#b)[[:space:]]#\(\([[:space:]]#(*[^[:space:]])[[:space:]]#\)\) ]]; then
+      print "zmathfuncdef $line[3] ${(qq)match[1]}"
+    fi
+  done
+  return 0
+fi
+
 local mname=$1
 local fname="zsh_math_func_$1"
 
 if (( $# == 1 )); then
   functions +M $mname && unfunction $fname
   return 0
+elif [[ -n $functions[$fname] ]]; then
+  functions +M $mname
 fi
 
 integer iarg=0 ioptarg