[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

autoloading es functions



hmm, i'm not sure if this was ever mentioned, other than as an aside.

> When you say, autoload directory, what do you mean?  Is this covered
> in the fine manual?

from my .esrc:

	autoload = ~/bin/es
	let (search = $fn-%pathsearch)
		fn %pathsearch prog {
			if {access -f -r $autoload/$prog} {
				. $autoload/$prog
				if {!~ $#(fn-$prog) 0} {
					return $(fn-$prog)
				}
			}
			$search $prog
		}

this function tries to load shell functions from appropriately named files in the  
directory $autoload before doing path searching.  the files are just run by the  
``.'' builtin, so they should include a definition of the function.  here's a few  
that i use.

(note, by the way, i am also using the path-caching, ala csh, described in the  
CHANGES file, mostly because i wanted to test out stacking of %pathsearch  
redefinitions.  i've found (surprising to me) that i like it.)

# to unbundle, es, rc, or sh this file
# bundled by haahr on utopia at Mon Apr  5 10:27:54 PDT 1993
# contents of bundle:
#	apply
#	cx
#	filter
#	in
#	ll
#	trace
#	watch
echo apply
sed 's/^-//' > apply <<'end of apply'
-fn apply function args {
-	if {~ $#function 0} {
-		throw error apply 'usage: apply function arg ...'
-	}
-	let (result =) {
-		for (i = $args) {
-			result = $result <>{$function $i}
-		}
-		result $result
-	}
-}
end of apply
echo cx
sed 's/^-//' > cx <<'end of cx'
-fn cx { chmod +x $* }
end of cx
echo filter
sed 's/^-//' > filter <<'end of filter'
-fn filter test args {
-	let (result = ) {
-		for (i = $args)
-			if {$test $i} {
-				result = $result $i
-			}
-		return $result
-	}
-}
end of filter
echo in
sed 's/^-//' > in <<'end of in'
-fn in {
-	fork if {cd $1} {
-		shift
-		$*
-	}
-}
end of in
echo ll
sed 's/^-//' > ll <<'end of ll'
-fn ll	{ ls -lgd $* }
end of ll
echo trace
sed 's/^-//' > trace <<'end of trace'
-fn trace functions {
-	for (func = $functions)
-		let (old = $(fn-$func)) {
-			if {~ $#old 0} {
-				throw error trace $func: undefined
-			}
-			fn $func args {  

-				echo >[1=2] $func $args
-				catch @ e {
-					echo >[1=2] $func raised exception: $e
-				} {
-					let (result = <>{$old $args}) {
-						echo >[1=2] $func '->' $result
-						result $result
-					}
-				}
-			}
-		}
-}
end of trace
echo watch
sed 's/^-//' > watch <<'end of watch'
-fn watch vars {
-	for (var = $vars) {
-		set-$var = @ {
-			echo >[1=2] old $var '=' $$var
-			echo >[1=2] new $var '=' $*
-			return $*
-		}
-	}
-}
end of watch