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

Re: Source mangling in $functions_source and typeset -f



> On 27 November 2021 at 17:47 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> > Is there a convenient way, from within zsh, to get either:
> >
> > The original, unmodified source of a function (autoloaded or otherwise)
> > The line offsets in the file where the function is defined (if any)?
> 
> The parse tree only tracks the line numbers of executable code, so as
> to be able to update the LINENO variable and print line numbers in
> debug traces and prompts.  The line number of the closing brace isn't
> recorded (in fact it's possible to define a function without any open
> or close brace if the body is a single expression).
> 
> So, strictly speaking, no, neither of those.

Actually, the original line number is remembered, and can be output
using a prompt --- typically PS4.  See the zshmisc manual and
the description of prompt escapes.

%i     The  line  number  currently being executed in the script, sourced file, or
       shell function given by %N.  This is most useful for debugging as  part  of
       $PS4.

%I     The  line  number currently being executed in the file %x.  This is similar
       to %i, but the line number is always a line number in the  file  where  the
       code was defined, even if the code is a shell function.

So, for example, my precmd is defined in .zshrc from line 297 and if I edit PS4 to

%e:%N:%i(%I)> 

then I get output lines with "set -x" like

+1:precmd:2(299)> integer new_status=0

I don't think there's any way of getting this information without actually executing
the code, though.

pws




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