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

Re: autoloading questions



On Oct 14,  4:54pm, Adam Spiers wrote:
} Subject: autoloading questions
}
} 1. What's the best way of checking whether an autoload of a function
}    will succeed?  (I mean the actual loading, rather than the
}    invocation of the `autoload' built-in.)

I can't think of anything except

    function testauto {
	emulate -L zsh
	local f
	f=( $^fpath/$1(N) )
	[[ -n $f[1] ]] && $ZSH_NAME -ns < $f[1]
    }

That requires forking an extra shell, though, and it's still nothing
more than a syntax check.

} 2. How can I manually cause an autoloaded function to be loaded
}    without actually running it

Try this:

    function loadauto {
	emulate -L zsh
	local f
	f=( $^fpath/$1(N) )
	eval "function $1 {
	    $(< $f[1])
	}"
    }

(You can replace $(< $f[1]) with $mapfile[$f[1]] if you use that module,
but $(<...) is already a non-forking special-case.)

Actually, I guess calling loadauto would tell you whether the autoload
succeeds, so it sort of answers both questions ....


BTW, this question made me notice that it's not a good idea to do

    autoload fun
    zmodload parameter
    noglob vared functions[fun]

unless you're very careful about whether you press enter.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



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