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

PATCH: zmathfuncdef



This makes zmathfuncdef (i) silently replace existing functions, which
is much more standard and useful than requiring you to delete them first
(ii) with no arguments, output the current definitions in a form
suitable for inputting.

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.99
diff -u -r1.99 contrib.yo
--- Doc/Zsh/contrib.yo	6 Apr 2009 19:19:10 -0000	1.99
+++ Doc/Zsh/contrib.yo	19 May 2009 15:24:52 -0000
@@ -2371,7 +2371,7 @@
 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 @@
 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()
 
Index: Functions/Misc/zmathfuncdef
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zmathfuncdef,v
retrieving revision 1.3
diff -u -r1.3 zmathfuncdef
--- Functions/Misc/zmathfuncdef	2 Jun 2006 13:34:59 -0000	1.3
+++ Functions/Misc/zmathfuncdef	19 May 2009 15:24:52 -0000
@@ -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


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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