tpapp / julia-repl

Run an inferior Julia REPL in a terminal inside Emacs
Other
169 stars 35 forks source link

feature wish/question: is it possible to display plots in an Emacs buffer? #126

Closed rdiaz02 closed 1 year ago

rdiaz02 commented 1 year ago

Is it possible to have plots display in an Emacs buffer when using julia-repl?

Why? I've realized I really like this after displaying plots from R in Emacs buffers with the httpg package (https://github.com/nx10/httpgd) using with xwidgets.

With Julia, this is available with julia-snail (https://github.com/gcv/julia-snail#multimedia-and-plotting ; see discussion here: https://github.com/gcv/julia-snail/issues/15 and https://github.com/gcv/julia-snail/pull/21). This seems to have been available with julia-repl in the past with julia-img-view (https://github.com/dennisog/julia-img-view) but it hasn't been updated for more than three years, and no longer works (see also https://github.com/dennisog/julia-img-view/issues/2; I just tried it and, for example, the code tries to call no longer existing functions such as julia-repl--live-buffer).

wentasah commented 1 year ago

I've just added a possibility to display images in an Emacs buffer to EmacsVterm.jl. For this to work, you would need to apply the changes in PR #112. If you want to test it, I'd be happy for some feedback.

rdiaz02 commented 1 year ago

Thanks! I just tried it, but I must be doing something wrong. I installed EmacsVterm from Julia, modified my startup.jl that now is

using OhMyREPL
using Revise

atreplinit() do repl
    @eval using EmacsVterm
    EmacsVterm.options.image = true
    # Optionally set EmacsVterm.options as you like (see below)
    ## EmacsVterm.options.markdown = false
end

and cloned your julia-repl repo and checked-out branch emacsvterm.jl-integration.

[ Info: Emacs vterm detected
UndefVarError: options not defined

As expected, plots are not shown in an Emacs buffer, even if I have set EmacsVterm.options.image = true.

julia> EmacsVterm.display_on()
ERROR: UndefVarError: display_on not defined
Stacktrace:
 [1] getproperty(x::Module, f::Symbol)
   @ Base ./Base.jl:31
 [2] top-level scope
   @ REPL[2]:1

But other features do work:

wentasah commented 1 year ago

I forgot to mention that you should use development version of EmacsVterm. I've not yet made a new release with this functionality. Install it with:

]develop EmacsVterm
tpapp commented 1 year ago

I just merged #112. This works fine for me, I plan to close when a release is tagged with EmacsVterm's current features.

rdiaz02 commented 1 year ago

Thanks! I checked the figure display in buffer and it works perfectly, except for Gadfly, where the plot is not shown (anywhere, as far as I can tell) but it is shown in a buffer by julia-snail. Example code:

## In buffer, by both julia-repl and julia-snail
using Plots
Plots.plot(rand(2, 3)) 

## Makie. In buffer by both julia-repl and snail
using CairoMakie
Makie.heatmap(rand(4, 4))

## Does not work with julia-repl: not shown.
## It is shown by snail
import Gadfly
Gadfly.plot(sin, 0, 2π)

P.S. In case someone stumbles upon this: figures were being shown as tiny squares. The culprit was this setting in my custom-set-variables: '(current-language-environment "ASCII") (I think that is a relic from the past). I don't know why that setting affects the display of figures in buffers, but it affected both julia-repl and julia-snail.

rdiaz02 commented 1 year ago

I just noticed that if display of images in Emacs buffers is used (EmacsVterm.options.markdown = false in startup.jl or EmacsVterm.display_on() in the REPL), then no output is shown in the REPL itself.

For example

EmacsVterm.display_on()
x = 5
x
## Nothing is shown
## Must do, for example
print(x)

I think I am missing something here. I would have expected, as per the README's "Images can be shown in an Emacs buffer. " that this setting would not have affected the display of results in the REPL. And I think these two are distinct ideas/desires:

Moreover, the behavior seems inconsistent (if I am understanding correctly the meaning of the options):

(I am leaving this here, as continuation of the former comments, but maybe I should move it to the EmacsVterm repo's issues?)

wentasah commented 1 year ago

I just noticed that if display of images in Emacs buffers is used (EmacsVterm.options.markdown = false in startup.jl or EmacsVterm.display_on() in the REPL), then no output is shown in the REPL itself.

Yes. This was caused by EmacsVterm.display() not throwing exception for non-image data. Fixed with this commit.

I'll look at the other issues later or tomorrow. It's probably just a matter of supporting the right MIME types.

wentasah commented 1 year ago

SVG support (needed for Gadfly) was added to EmacsVterm. @rdiaz02 does it work for you? Anyway, I'll make a release tomorrow.

rdiaz02 commented 1 year ago

Yes! Thanks, both things work: Gadfly and Emacs not eating anymore the non-display data.

wentasah commented 1 year ago

Thanks to all for testing, new release is out.

tpapp commented 1 year ago

I think this was fixed by #112.