I've been playing around with some things regarding $fpath and autoloadable functions.
Ultimately, I've got a nice wrapper which will print out the source of a function (and autoload it if necessary) and then pass it to `bat` for syntax highlighting.
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 -- even if done explicitly with line-continuation slashes, or if spaced out manually and meticulously in an array.
With a little bit of grep-foo, it's possible to use "$functions[foo]" and search for the file for the function declaration. With this little trick, it makes it easy to open the editor to the correct line in the file where the function is declared.
This makes it easy to display the path/to/file:linenum on which a given function is declared, and open it easily in the editor of your choice with a ⌘-Click or ⌃-Click depending on your chosen editor.
However, there's no easy way to determine the LAST line in the original file which corresponds to the function -- due to aforementioned newline- and comment-stripping.
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)?
Getting the starting offset is easy-ish (thanks grep!) but finding the function end is less easy. I expect there are Zsh internals that could track this if desired, but it simply isn't tracked.
Are there any easy fixes to this? My best path forward for detecting the [start, end] of a function, with its original comments, will rely on finding a closing '}' with the same indentation as the 'function foo()' definition.