zsh-users
 help / color / mirror / code / Atom feed
* Source mangling in $functions_source and typeset -f
@ 2021-11-27  8:01 Zach Riggle
  2021-11-27 17:47 ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Zach Riggle @ 2021-11-27  8:01 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1928 bytes --]

Hello again!

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.

https://i.imgur.com/oPSiPWB.png


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.

https://i.imgur.com/nvzuFEm.png


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.

*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 2691 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-12-01  6:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-27  8:01 Source mangling in $functions_source and typeset -f Zach Riggle
2021-11-27 17:47 ` Bart Schaefer
2021-11-27 19:25   ` Ray Andrews
2021-11-27 19:40     ` Lawrence Velázquez
2021-11-27 20:09       ` Ray Andrews
2021-11-27 20:14     ` Bart Schaefer
2021-11-27 21:56       ` Ray Andrews
2021-11-29 16:00   ` Peter Stephenson
2021-11-29 17:55     ` Bart Schaefer
2021-12-01  6:53       ` Daniel Shahaf

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