Current REPL number

Does Julia provide an API for extracting the current REPL number?

By “current REPL number”, I mean the x in REPL[x] in error messages like the following:

julia> error()
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:42
 [2] top-level scope
   @ REPL[1]:1

@__FILE__ in the REPL.

Ok, but I’m looking for something which works also in library code.

I use this to set my repl prompt in startup.jl:

function repl_line() 
    hist = Base.active_repl.interface.modes[1].hist
    hist.cur_idx - hist.start_idx
end

(or actually, repl.interface.modes[1].hist if i get a handle on repl from atreplinit)

This is close to what I want, but it seems to have some quirks:

   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' |  |
  | | |_| | | | (_| |  |  Version 1.7.2 (2022-02-06)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> function repl_line() 
           hist = Base.active_repl.interface.modes[1].hist
           hist.cur_idx - hist.start_idx
       end;

julia> repl_line()
0  # Would have expected this to be 2, but fine

julia> repl_line()
2  # Why does it jump by 2 now?

julia> repl_line()
2  # No increment, presumably because same input

julia> exp(1);  # Dummy input 

julia> repl_line()
2  # Still same?!

julia> repl_line()
4  # Jumping by 2 again

Yes this is tricky, this is because you used something like uparrow or Ctrl-p to go back to previous history entries, this is how the REPL works. For example, the first repl_line() call shows 0: this is presumably because you used uparrow instead of actually typing repl_line(), and then repl_line() was the last thing typed in the previous julia session.

The alternative is to use: length(hist.history) + 1 - hist.start_idx (which i actually also show in my REPL prompt when it differs from the repl_line() above, so e.g. my prompt shows 0/2> for your first repl_line() example if i pressed uparrow).

Better, but still not perfect :cry:

   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' |  |
  | | |_| | | | (_| |  |  Version 1.7.2 (2022-02-06)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> function repl_line() 
           hist = Base.active_repl.interface.modes[1].hist
           length(hist.history) + 1 - hist.start_idx
       end;

julia> repl_line()
2

julia> repl_line()
2  # Still no update for ctrl+up inputs

julia> exp(1);  # Dummy input

julia> repl_line()
4  # But at least immediate updates

Thanks for your help anyway, much appreciated! :heart: