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

Finding the file offsets for functions



So I've recently been poking around with the goal of finding the filename, and line-range where a function is actually declared.  The intent is to have the function equivalent of "vim =command", but to also include the relevant file numbers

The "whence" builtin, specifically "-v" and "-w" flags, are very useful for deciding if something is a command, function, autoloadable function, builtin, or alias.

In the "command" case, our work is done.

For functions, things get more complicated.

$ whence -v abcd
abcd is a shell function from /Users/zachriggle/.zprezto/modules/zach-riggle/alias.zsh

Autoloadable functions show up differently.

$ whence -v zztop
zztop is an autoload shell function

We can force-load these to determine their locations

$ whence -v zztop
zztop is an autoload shell function

$ zztop
Zztop module loaded

$ echo $functions_source[zztop]
/Users/zachriggle/.zprezto/modules/zach-riggle/functions/zztop

$ whence -v zztop
zztop is a shell function from /Users/zachriggle/.zprezto/modules/zach-riggle/functions/zztop

$ zztop
This is zztop

$ declare -f zztop
zztop () {
echo This is zztop
}

$ cat /Users/zachriggle/.zprezto/modules/zach-riggle/functions/zztop
#!/usr/bin/env zsh#
zztop() {
    echo This is zztop
}
echo Zztop module loaded

So now we can find the source file which contains a function ($functions or whence -v), and can work with autoloadable functions to get them loaded.

The state I'm at now is finding the first / last line for a given function, in the file it is declared in.  In the case of autoloadable functions, where no function need be declared (the whole body is the function) we have a convenient null solution.

I can also get the source code to the function, via e.g. "declare -f".  This is reformatted from its original form, which is generally fine.

I've run into issues where I'd like to create a path/to/autoloadable:$lineno such that I can easily pass it to vim or another editor.  This saves a lot of time on my end


Zach Riggle



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