Is there a function that prints out the body of another function?

Say there is a function foo defined somewhere. Is there a function so that I can:


and it prints out the definition of foo?

I know about @less, but for whatever reason it doesn’t work in Pluto

I saw that Pluto is already supporting macros in the main branch, so @less will work on the release version soon (you can try it by adding the main branch).

But less is also a function, for example:

julia> @less 2+2

is equivalent to

julia> less(+,[Int64,Int64])

The Pluto macro PR is about reactivity, the issue here is that @less prints on the console and does not return a result.
You can use with_terminal() from PlutoUI.jl for displaying console output in Pluto.

@less just calls the less function under the hood, which opens the default terminal pager for displaying. Since this is running in the terminal, it’s no surprise that Pluto doesn’t display it - it’s a frontend running in the browser after all, not a command line application. The pager is a seperate process entirely.

The relevant code that actually opens the pager can be found in InteractiveUtils/editless.jl:

# terminal pager                                                          
if Sys.iswindows()                                                        
    function less(file::AbstractString, line::Integer)                    
        pager = shell_split(get(ENV, "PAGER", "more"))                    
        if pager[1] == "more"                                             
            g = ""                                                        
            line -= 1                                                     
            g = "g"                                                       
        run(Cmd(`$pager +$(line)$(g) \"$file\"`, windows_verbatim = true))
    function less(file::AbstractString, line::Integer)                    
        pager = shell_split(get(ENV, "PAGER", "less"))                    
        run(`$pager +$(line)g $file`)                                     
1 Like

Ah! Thanks for clarifying. I am not a Pluto user so I keep seeing posts about Pluto and macros and I thought there was something blocking their execution.

1 Like

You could use CodeTracking.jl for this. Copying the example from its README,

julia> using CodeTracking

julia> print(@code_string sum(1:5))
function sum(r::AbstractRange{<:Real})
    l = length(r)
    # note that a little care is required to avoid overflow in l*(l-1)/2
    return l * first(r) + (iseven(l) ? (step(r) * (l-1)) * (l>>1)
                                     : (step(r) * l) * ((l-1)>>1))

this prints to the console rather than showing in Pluto

Hi, can you tell me how I use with_terminal()? Do I put the less command inside it? That doesn’t seem to work…

this prints to the console too, instead of showing up in Pluto

1 Like

The general syntax is

with_terminal() do
    println("Hello World!")

But I am not sure if it also works for @less.

1 Like

thanks…with_terminal seems to be a PlutoUI function rather than in Pluto, let me see if I can figure that out

Yes, sorry I forgot to mention this.

You could also just omit the print part if you just want it as a string, or you could probably embed that string in markdown to get it to display nicer in Pluto.

We can also use Text for printing nicely in Pluto. The following works for me:

using CodeTracking
Text(@code_string sum(1:5))