* Finding the file offsets for functions
@ 2022-05-21 6:27 Zach Riggle
2022-05-21 20:17 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Zach Riggle @ 2022-05-21 6:27 UTC (permalink / raw)
To: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 2003 bytes --]
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*
[-- Attachment #2: Type: text/html, Size: 2918 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Finding the file offsets for functions
2022-05-21 6:27 Finding the file offsets for functions Zach Riggle
@ 2022-05-21 20:17 ` Bart Schaefer
2022-05-23 1:16 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2022-05-21 20:17 UTC (permalink / raw)
To: Zach Riggle; +Cc: Zsh Users
On Fri, May 20, 2022 at 11:27 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> Autoloadable functions show up differently.
>
> $ whence -v zztop
> zztop is an autoload shell function
In recent zsh you can use
autoload -r zztop
and then whence -v output and $functions_source will include the path
(but you also can't change that path later by changing $fpath).
> $ cat /Users/zachriggle/.zprezto/modules/zach-riggle/functions/zztop
> #!/usr/bin/env zsh#
> zztop() {
> echo This is zztop
> }
> echo Zztop module loaded
That pretty much obliterates the whole reason for "autoload".
If you don't like "autoload -r" at the time of declaring all the
autoloads, you can use "autoload +X" at the time you want the source
of a particular function.
The flag +X attempts to load each NAME as an autoloaded function,
but does _not_ execute it. ...
... This does _not_ replace any existing definition ...
> 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
Wouldn't
vim "+/function $name\\|$name *()" $functions_source[$name]
be sufficient in nearly all cases?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Finding the file offsets for functions
2022-05-21 20:17 ` Bart Schaefer
@ 2022-05-23 1:16 ` Bart Schaefer
0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2022-05-23 1:16 UTC (permalink / raw)
To: Zach Riggle; +Cc: Zsh Users
On Sat, May 21, 2022 at 1:17 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> Wouldn't
> vim "+/function $name\\|$name *()" $functions_source[$name]
> be sufficient in nearly all cases?
If you actually need the line number instead of +/, try:
lineno=${"$(grep -n "function $name\\|$name *()" $functions_source[$name])"%:*}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-05-23 1:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-21 6:27 Finding the file offsets for functions Zach Riggle
2022-05-21 20:17 ` Bart Schaefer
2022-05-23 1:16 ` Bart Schaefer
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).