SoftGlobalScope.jl
                                
                                 SoftGlobalScope.jl copied to clipboard
                                
                                    SoftGlobalScope.jl copied to clipboard
                            
                            
                            
                        add REPL hooks
As suggested by @dawbarton, in https://github.com/JuliaLang/julia/issues/28789#issuecomment-415063358, it is possible to hack the active REPL to use SoftGlobalScope.   Would be nice to add this feature here, so that using SoftGlobalScope patches the active REPL if there is one.
Similar to this code in OhMyREPL.jl, we would do this in __init__ only in interactive mode.
@KristofferC, your advice on the cleanest way to patch the REPL's eval loop would be appreciated.
Revise has a similar use case where it wants to patch the REPL to revise methods upon each evaluation in the REPL. The code on how it manages to do that (https://github.com/timholy/Revise.jl/blob/382e872375de76f279556d076730a0fe5de4520f/src/Revise.jl#L743-L842) is likely useful.
The question now is of course, what happens when there are two REPL thieves in play at the same time... Perhaps we need a REPLThieves.jl package ;). cc @timholy.
Yup, it looks like I did it exactly the same way as Revise does and doing it twice won't play nicely. It might well be possible to intercept the eval call in a slightly different way that does play nicely though. I can try to take a look at some point in the next few days...
FWIW a while ago I wrote a small package to easily work with hooks for this sort of thing:
https://github.com/ssfrr/Hooks.jl
I never ended up registering it but this is the sort of problem it's designed to solve so it might be worth revisiting. It's also possible we could add a set of Base hooks to the Hooks.jl package so that multiple packages can subscribe to them.
Basically we need someone to create a REPLHooks package that allows you to add hooks to the REPL, then refactor Revise to use it, at which point SoftGlobalScope can use it too.
Note that Julia 1.5 now does this for us, and adds a general mechanism for REPL hooks (https://github.com/JuliaLang/julia/pull/34626).