2 # An almost ksh-compatible `autoload'. A function declared as `autoload' will
3 # be read in from a file the same name as the function found by searching the
4 # $FPATH (which works the same as $PATH), then that definition will be run.
6 # To do this without source support, we define a dummy function that, when
7 # executed, will load the file (thereby re-defining the function), then
8 # execute that newly-redefined function with the original arguments.
10 # It's not identical to ksh because ksh apparently does lazy evaluation
11 # and looks for the file to load from only when the function is referenced.
12 # This one requires that the file exist when the function is declared as
15 # usage: autoload [-pu] [func ...]
18 # -p print in a format that can be reused as input
19 # -u unset each function and remove it from the autoload list
21 # The first cut of this was by Bill Trost, trost@reed.edu
30 # Declare a function ($1) to be autoloaded from a file ($2) when it is first
31 # called. This defines a `temporary' function that will `.' the file
32 # containg the real function definition, then execute that new definition with
33 # the arguments given to this `fake' function. The autoload function defined
34 # by the file and the file itself *must* be named identically.
39 eval $1 '() { . '$2' ; '$1' "$@" ; return $? ; }'
40 _autoload_addlist "$1"
47 while (( i < $_aindex )); do
48 case "${_AUTOLOADS[i]}" in
53 _AUTOLOADS[_aindex]="$1"
62 for func in ${_AUTOLOADS[@]}; do
63 [ -n "$1" ] && echo -n "autoload "
68 # Remove $1 from the list of autoloaded functions
69 _autoload_remove_one()
74 while (( i < _aindex )); do
75 case "${_AUTOLOADS[i]}" in
77 *) nlist[nnl]="${_AUTOLOADS[i]}" ; (( nnl += 1 ));;
81 unset _AUTOLOADS _aindex
82 eval _AUTOLOADS=( ${nlist[@]} )
86 # Remove all function arguments from the list of autoloaded functions
91 # first unset the autoloaded functions
94 while (( i < _aindex )); do
95 case "${_AUTOLOADS[i]}" in
96 "$func") unset -f $func ; break ;;
100 if (( i == _aindex )); then
101 echo "autoload: $func: not an autoloaded function" >&2
106 # then rebuild the list of autoloaded functions
108 _autoload_remove_one "$func"
115 # Search $FPATH for a file the same name as the function given as $1, and
116 # autoload the function from that file. There is no default $FPATH.
122 local _autoload_unset nfp i
124 if (( $# == 0 )) ; then
133 p) _autoload_dump printable; return 0;;
134 u) _autoload_unset=y ;;
135 *) echo "autoload: usage: autoload [-pu] [function ...]" >&2
140 shift $(( $OPTIND - 1 ))
142 if [ -n "$_autoload_unset" ]; then
143 _autoload_remove "$@"
148 # If there is no $FPATH, there is no work to be done
151 if [ -z "$FPATH" ] ; then
152 echo "autoload: FPATH not set or null" >&2
157 # This treats FPATH exactly like PATH: a null field anywhere in the
158 # FPATH is treated the same as the current directory.
160 # This turns $FPATH into an array, substituting `.' for `'
165 for p in "$@" ; do echo -n "${p:-.} "; done
173 while (( i < nfp )) ; do
174 if [ -f ${fp[i]}/$FUNC ] ; then
180 if (( i == nfp )) ; then
181 echo "autoload: $FUNC: autoload function not found" >&2
186 # echo auto-loading $FUNC from ${fp[i]}/$FUNC
187 _aload $FUNC ${fp[i]}/$FUNC