zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: Autoload vs regular function
Date: Fri, 18 Sep 2015 14:53:00 -0700	[thread overview]
Message-ID: <150918145300.ZM27125@torch.brasslantern.com> (raw)
In-Reply-To: <55FC5BC8.4040107@eastlink.ca>

On Sep 18, 11:45am, Ray Andrews wrote:
}
} Can you rough in an example of that for us Bart?  Sounds like an elegant 
} idea.

Functions/Misc/tetris in the zsh distribution is a perfect example of
this already.  It defines a whole bunch of helper functions (most of
which end up becoming zle widgets) and a single entry point function
"tetris" which kicks the whole thing off.

Note at the very end of Functions/Misc/tetris, the function that it
defined is explicitly called.  This is so tetris can be autoloaded in
the default zsh style.  If you do "autoload -k tetris" the command is
run twice.  (Many autoloadable functions in the zsh distribution test
for KSH_AUTOLOAD before calling themselves this way; tetris probably
ought to as well, but currently doesn't.)

} I wonder if it is possible that when one types a command that is not
} found, it could trigger a sort of search in some directory for a
} script that would itself source the very command not found and then
} run it? Sorta a reinvention of autoload but maybe more transparent.

Recent zsh support a command_not_found_handler function which is run
if a the searches of $fpath and $path do not find anything.  So you
can do this by building a custom search in command_not_found_handler.
I don't know that this any more "transparent" than otherwise.  Also
the command_not_found handler runs after the parent shell has forked,
so anything it loads does not appear in the parent.

E.g. to always attempt to load functions from a .zwc file in the
current working directory, you could:

    command_not_found_handler () {
        FPATH=$PWD autoload +X $1
        "$@"
    }

Similarly if you don't like zsh's way of handling autoloads, you can
do you own like this:

    auto_load_me () {
	FPATH=/my/custom/fpath autoload -X
    }

This is [almost!] the same as writing

    autoload auto_load_me

except that the custom fpath is asserted.  I say "almost" because things
like sticky emulation get handled a little differently when using the
"autoload -X" command, but that is pretty esoteric.  Of course you can
throw in other things besides just fpath changes, you have a whole
function body to play with, but at some point you're just ruining the
whole purpose of autoload and might as well define the real function.

-- 
Barton E. Schaefer


  reply	other threads:[~2015-09-18 21:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-18  6:34 Sebastian Gniazdowski
2015-09-18 17:15 ` Bart Schaefer
2015-09-18 18:45   ` Ray Andrews
2015-09-18 21:53     ` Bart Schaefer [this message]
2015-09-19 16:23       ` simple question regarding local variables Ray Andrews
2015-09-19 16:32         ` Bart Schaefer
2015-09-19 18:21           ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=150918145300.ZM27125@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).