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

**URL:** <https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 27, 2021, 3:28pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380 "2021-07-27T15:28:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![sg-s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sg-s/32/2635_2.png) [@sg-s](https://discourse.julialang.org/u/sg-s)\
**Post date:** [July 27, 2021, 3:28pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/1 "2021-07-27T15:28:26Z")

</div>

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

```julia
disp(foo)

```

and it prints out the definition of foo?

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

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [July 27, 2021, 6:12pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/2 "2021-07-27T18:12:24Z")

</div>

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
julia> @less 2+2

```

is equivalent to

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

```

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 27, 2021, 6:25pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/3 "2021-07-27T18:25:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 27, 2021, 6:37pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/4 "2021-07-27T18:37:31Z")

</div>

`@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`:

```julia
# 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                                                     
        else                                                              
            g = "g"                                                       
        end                                                               
        run(Cmd(`$pager +$(line)$(g) \"$file\"`, windows_verbatim = true))
        nothing                                                           
    end                                                                   
else                                                                      
    function less(file::AbstractString, line::Integer)                    
        pager = shell_split(get(ENV, "PAGER", "less"))                    
        run(`$pager +$(line)g $file`)                                     
        nothing                                                           
    end                                                                   
end                                                                       

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [July 27, 2021, 6:46pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/5 "2021-07-27T18:46:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 27, 2021, 10:56pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/6 "2021-07-27T22:56:27Z")

</div>

You could use [CodeTracking.jl](https://github.com/timholy/CodeTracking.jl) for this. Copying the example from its README,

```julia
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))
end

```

---

<div class="post-metadata">

**Author:** ![sg-s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sg-s/32/2635_2.png) [@sg-s](https://discourse.julialang.org/u/sg-s)\
**Post date:** [July 28, 2021, 5:26pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/7 "2021-07-28T17:26:41Z")

</div>

this prints to the console rather than showing in Pluto

---

<div class="post-metadata">

**Author:** ![sg-s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sg-s/32/2635_2.png) [@sg-s](https://discourse.julialang.org/u/sg-s)\
**Post date:** [July 28, 2021, 5:28pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/8 "2021-07-28T17:28:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sg-s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sg-s/32/2635_2.png) [@sg-s](https://discourse.julialang.org/u/sg-s)\
**Post date:** [July 28, 2021, 5:30pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/9 "2021-07-28T17:30:32Z")

</div>

> [@ericphanson](#):
>
> `print(@code_string sum(1:5))`

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

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 28, 2021, 5:31pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/10 "2021-07-28T17:31:41Z")

</div>

The general syntax is

```julia
with_terminal() do
    println("Hello World!")
end

```

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

---

<div class="post-metadata">

**Author:** ![sg-s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sg-s/32/2635_2.png) [@sg-s](https://discourse.julialang.org/u/sg-s)\
**Post date:** [July 28, 2021, 5:44pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/11 "2021-07-28T17:44:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 28, 2021, 6:23pm UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/12 "2021-07-28T18:23:51Z")

</div>

Yes, sorry I forgot to mention this.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 29, 2021, 9:56am UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/13 "2021-07-29T09:56:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [July 31, 2021, 4:53am UTC](https://discourse.julialang.org/t/is-there-a-function-that-prints-out-the-body-of-another-function/65380/14 "2021-07-31T04:53:00Z")

</div>

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

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

```
