zsh-users
 help / color / mirror / code / Atom feed
From: Zach Riggle <zachriggle@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Finding the file offsets for functions
Date: Sat, 21 May 2022 01:27:22 -0500	[thread overview]
Message-ID: <CAMP9c5n4Tho2-ddGNKbf1mFa7ZdtdHRSMN-dv3NcsudAGNKXQQ@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2022-05-21  6:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-21  6:27 Zach Riggle [this message]
2022-05-21 20:17 ` Bart Schaefer
2022-05-23  1:16   ` Bart Schaefer

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=CAMP9c5n4Tho2-ddGNKbf1mFa7ZdtdHRSMN-dv3NcsudAGNKXQQ@mail.gmail.com \
    --to=zachriggle@gmail.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).