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

Re: Source mangling in $functions_source and typeset -f

On Sat, Nov 27, 2021 at 12:02 AM Zach Riggle <zachriggle@xxxxxxxxx> wrote:
> Unfortunately, "$functions_source[foo]" and "typeset -f foo" both seem to remove all comments, and rewrite the source such that there's no empty newlines

With the exception of the contents of strings (including
here-documents), the original source of a function is not kept in
shell memory.  Instead a parse tree is stored and used to regenerate
the function definition by "typeset -f" et al.  Comments and
semantically-meaningless whitespace are discarded during parsing,
hence they're not available later.

> 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.

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