• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37802

09 Jun 2024 07:01AM UTC coverage: 87.542% (+0.04%) from 87.504%
#37802

push

local

web-flow
Reimplement dummy pkg prompt as standard prompt (#54674)

13 of 20 new or added lines in 2 files covered. (65.0%)

197 existing lines in 11 files now uncovered.

77076 of 88045 relevant lines covered (87.54%)

15007024.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.64
/stdlib/REPL/src/REPL.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
Run Evaluate Print Loop (REPL)
5

6
Example minimal code
7

8
```julia
9
import REPL
10
term = REPL.Terminals.TTYTerminal("dumb", stdin, stdout, stderr)
11
repl = REPL.LineEditREPL(term, true)
12
REPL.run_repl(repl)
13
```
14
"""
15
module REPL
16

17
Base.Experimental.@optlevel 1
18
Base.Experimental.@max_methods 1
19

20
function UndefVarError_hint(io::IO, ex::UndefVarError)
2✔
21
    var = ex.var
2✔
22
    if var === :or
2✔
23
        print(io, "\nSuggestion: Use `||` for short-circuiting boolean OR.")
×
24
    elseif var === :and
2✔
25
        print(io, "\nSuggestion: Use `&&` for short-circuiting boolean AND.")
×
26
    elseif var === :help
2✔
27
        println(io)
×
28
        # Show friendly help message when user types help or help() and help is undefined
29
        show(io, MIME("text/plain"), Base.Docs.parsedoc(Base.Docs.keywords[:help]))
×
30
    elseif var === :quit
2✔
31
        print(io, "\nSuggestion: To exit Julia, use Ctrl-D, or type exit() and press enter.")
×
32
    end
33
    if isdefined(ex, :scope)
2✔
34
        scope = ex.scope
2✔
35
        if scope isa Module
2✔
36
            bnd = ccall(:jl_get_module_binding, Any, (Any, Any, Cint), scope, var, true)::Core.Binding
2✔
37
            if isdefined(bnd, :owner)
2✔
38
                owner = bnd.owner
×
39
                if owner === bnd
×
40
                    print(io, "\nSuggestion: add an appropriate import or assignment. This global was declared but not assigned.")
×
41
                end
42
            else
43
                owner = ccall(:jl_binding_owner, Ptr{Cvoid}, (Any, Any), scope, var)
2✔
44
                if C_NULL == owner
2✔
45
                    # No global of this name exists in this module.
46
                    # This is the common case, so do not print that information.
47
                    # It could be the binding was exported by two modules, which we can detect
48
                    # by the `usingfailed` flag in the binding:
49
                    if isdefined(bnd, :flags) && Bool(bnd.flags >> 4 & 1) # magic location of the `usingfailed` flag
3✔
50
                        print(io, "\nHint: It looks like two or more modules export different ",
1✔
51
                              "bindings with this name, resulting in ambiguity. Try explicitly ",
52
                              "importing it from a particular module, or qualifying the name ",
53
                              "with the module it should come from.")
54
                    else
55
                        print(io, "\nSuggestion: check for spelling errors or missing imports.")
1✔
56
                    end
57
                    owner = bnd
2✔
58
                else
59
                    owner = unsafe_pointer_to_objref(owner)::Core.Binding
×
60
                end
61
            end
62
            if owner !== bnd
2✔
63
                # this could use jl_binding_dbgmodule for the exported location in the message too
64
                print(io, "\nSuggestion: this global was defined as `$(owner.globalref)` but not assigned a value.")
×
65
            end
66
        elseif scope === :static_parameter
×
67
            print(io, "\nSuggestion: run Test.detect_unbound_args to detect method arguments that do not fully constrain a type parameter.")
×
68
        elseif scope === :local
×
69
            print(io, "\nSuggestion: check for an assignment to a local variable that shadows a global of the same name.")
×
70
        end
71
    else
72
        scope = undef
×
73
    end
74
    if scope !== Base && !_UndefVarError_warnfor(io, Base, var)
2✔
75
        warned = false
1✔
76
        for m in Base.loaded_modules_order
1✔
77
            m === Core && continue
30✔
78
            m === Base && continue
29✔
79
            m === Main && continue
28✔
80
            m === scope && continue
27✔
81
            warned |= _UndefVarError_warnfor(io, m, var)
27✔
82
        end
30✔
83
        warned ||
2✔
84
            _UndefVarError_warnfor(io, Core, var) ||
85
            _UndefVarError_warnfor(io, Main, var)
86
    end
87
    return nothing
2✔
88
end
89

90
function _UndefVarError_warnfor(io::IO, m::Module, var::Symbol)
31✔
91
    Base.isbindingresolved(m, var) || return false
60✔
92
    (Base.isexported(m, var) || Base.ispublic(m, var)) || return false
4✔
93
    print(io, "\nHint: a global variable of this name also exists in $m.")
1✔
94
    return true
1✔
95
end
96

97
function __init__()
4✔
98
    Base.REPL_MODULE_REF[] = REPL
4✔
99
    Base.Experimental.register_error_hint(UndefVarError_hint, UndefVarError)
4✔
100
    return nothing
4✔
101
end
102

103
using Base.Meta, Sockets, StyledStrings
104
import InteractiveUtils
105

106
export
107
    AbstractREPL,
108
    BasicREPL,
109
    LineEditREPL,
110
    StreamREPL
111

112
import Base:
113
    AbstractDisplay,
114
    display,
115
    show,
116
    AnyDict,
117
    ==
118

119
_displaysize(io::IO) = displaysize(io)::Tuple{Int,Int}
32✔
120

121
include("Terminals.jl")
122
using .Terminals
123

124
abstract type AbstractREPL end
125

126
include("options.jl")
127

128
include("LineEdit.jl")
129
using .LineEdit
130
import ..LineEdit:
131
    CompletionProvider,
132
    HistoryProvider,
133
    add_history,
134
    complete_line,
135
    history_next,
136
    history_next_prefix,
137
    history_prev,
138
    history_prev_prefix,
139
    history_first,
140
    history_last,
141
    history_search,
142
    setmodifiers!,
143
    terminal,
144
    MIState,
145
    PromptState,
146
    mode_idx
147

148
include("REPLCompletions.jl")
149
using .REPLCompletions
150

151
include("TerminalMenus/TerminalMenus.jl")
152
include("docview.jl")
153

154
@nospecialize # use only declared type signatures
155

156
answer_color(::AbstractREPL) = ""
×
157

158
const JULIA_PROMPT = "julia> "
159
const PKG_PROMPT = "pkg> "
160
const SHELL_PROMPT = "shell> "
161
const HELP_PROMPT = "help?> "
162

163
mutable struct REPLBackend
164
    "channel for AST"
165
    repl_channel::Channel{Any}
166
    "channel for results: (value, iserror)"
167
    response_channel::Channel{Any}
168
    "flag indicating the state of this backend"
169
    in_eval::Bool
170
    "transformation functions to apply before evaluating expressions"
171
    ast_transforms::Vector{Any}
172
    "current backend task"
173
    backend_task::Task
174

175
    REPLBackend(repl_channel, response_channel, in_eval, ast_transforms=copy(repl_ast_transforms)) =
52✔
176
        new(repl_channel, response_channel, in_eval, ast_transforms)
177
end
178
REPLBackend() = REPLBackend(Channel(1), Channel(1), false)
26✔
179

180
"""
181
    softscope(ex)
182

183
Return a modified version of the parsed expression `ex` that uses
184
the REPL's "soft" scoping rules for global syntax blocks.
185
"""
186
function softscope(@nospecialize ex)
376✔
187
    if ex isa Expr
376✔
188
        h = ex.head
241✔
189
        if h === :toplevel
241✔
190
            ex′ = Expr(h)
137✔
191
            map!(softscope, resize!(ex′.args, length(ex.args)), ex.args)
137✔
192
            return ex′
137✔
193
        elseif h in (:meta, :import, :using, :export, :module, :error, :incomplete, :thunk)
104✔
194
            return ex
2✔
195
        elseif h === :global && all(x->isa(x, Symbol), ex.args)
103✔
196
            return ex
1✔
197
        else
198
            return Expr(:block, Expr(:softscope, true), ex)
101✔
199
        end
200
    end
201
    return ex
135✔
202
end
203

204
# Temporary alias until Documenter updates
205
const softscope! = softscope
206

207
const repl_ast_transforms = Any[softscope] # defaults for new REPL backends
208

209
# Allows an external package to add hooks into the code loading.
210
# The hook should take a Vector{Symbol} of package names and
211
# return true if all packages could be installed, false if not
212
# to e.g. install packages on demand
213
const install_packages_hooks = Any[]
214

215
# N.B.: Any functions starting with __repl_entry cut off backtraces when printing in the REPL.
216
# We need to do this for both the actual eval and macroexpand, since the latter can cause custom macro
217
# code to run (and error).
218
__repl_entry_lower_with_loc(mod::Module, @nospecialize(ast), toplevel_file::Ref{Ptr{UInt8}}, toplevel_line::Ref{Cint}) =
217✔
219
    ccall(:jl_expand_with_loc, Any, (Any, Any, Ptr{UInt8}, Cint), ast, mod, toplevel_file[], toplevel_line[])
220
__repl_entry_eval_expanded_with_loc(mod::Module, @nospecialize(ast), toplevel_file::Ref{Ptr{UInt8}}, toplevel_line::Ref{Cint}) =
216✔
221
    ccall(:jl_toplevel_eval_flex, Any, (Any, Any, Cint, Cint, Ptr{Ptr{UInt8}}, Ptr{Cint}), mod, ast, 1, 1, toplevel_file, toplevel_line)
222

223
function toplevel_eval_with_hooks(mod::Module, @nospecialize(ast), toplevel_file=Ref{Ptr{UInt8}}(Base.unsafe_convert(Ptr{UInt8}, :REPL)), toplevel_line=Ref{Cint}(1))
345✔
224
    if !isexpr(ast, :toplevel)
453✔
225
        ast = __repl_entry_lower_with_loc(mod, ast, toplevel_file, toplevel_line)
217✔
226
        check_for_missing_packages_and_run_hooks(ast)
217✔
227
        return __repl_entry_eval_expanded_with_loc(mod, ast, toplevel_file, toplevel_line)
216✔
228
    end
229
    local value=nothing
128✔
230
    for i = 1:length(ast.args)
128✔
231
        value = toplevel_eval_with_hooks(mod, ast.args[i], toplevel_file, toplevel_line)
237✔
232
    end
339✔
233
    return value
121✔
234
end
235

236
function eval_user_input(@nospecialize(ast), backend::REPLBackend, mod::Module)
108✔
237
    lasterr = nothing
×
238
    Base.sigatomic_begin()
108✔
239
    while true
113✔
240
        try
113✔
241
            Base.sigatomic_end()
113✔
242
            if lasterr !== nothing
113✔
243
                put!(backend.response_channel, Pair{Any, Bool}(lasterr, true))
5✔
244
            else
245
                backend.in_eval = true
108✔
246
                for xf in backend.ast_transforms
108✔
247
                    ast = Base.invokelatest(xf, ast)
126✔
248
                end
126✔
249
                value = toplevel_eval_with_hooks(mod, ast)
108✔
250
                backend.in_eval = false
102✔
251
                setglobal!(Base.MainInclude, :ans, value)
102✔
252
                put!(backend.response_channel, Pair{Any, Bool}(value, false))
102✔
253
            end
254
            break
112✔
255
        catch err
256
            if lasterr !== nothing
5✔
257
                println("SYSTEM ERROR: Failed to report error to REPL frontend")
×
258
                println(err)
×
259
            end
260
            lasterr = current_exceptions()
5✔
261
        end
262
    end
5✔
263
    Base.sigatomic_end()
107✔
264
    nothing
265
end
266

267
function check_for_missing_packages_and_run_hooks(ast)
217✔
268
    isa(ast, Expr) || return
327✔
269
    mods = modules_to_be_loaded(ast)
107✔
270
    filter!(mod -> isnothing(Base.identify_package(String(mod))), mods) # keep missing modules
112✔
271
    if !isempty(mods)
107✔
272
        isempty(install_packages_hooks) && load_pkg()
1✔
273
        for f in install_packages_hooks
1✔
274
            Base.invokelatest(f, mods) && return
1✔
275
        end
×
276
    end
277
end
278

279
function _modules_to_be_loaded!(ast::Expr, mods::Vector{Symbol})
664✔
280
    ast.head === :quote && return mods # don't search if it's not going to be run during this eval
664✔
281
    if ast.head === :using || ast.head === :import
1,312✔
282
        for arg in ast.args
20✔
283
            arg = arg::Expr
24✔
284
            arg1 = first(arg.args)
24✔
285
            if arg1 isa Symbol # i.e. `Foo`
24✔
286
                if arg1 != :. # don't include local imports
21✔
287
                    push!(mods, arg1)
20✔
288
                end
289
            else # i.e. `Foo: bar`
290
                push!(mods, first((arg1::Expr).args))
3✔
291
            end
292
        end
24✔
293
    end
294
    if ast.head !== :thunk
664✔
295
        for arg in ast.args
554✔
296
            if isexpr(arg, (:block, :if, :using, :import))
1,338✔
297
                _modules_to_be_loaded!(arg, mods)
33✔
298
            end
299
        end
1,338✔
300
    else
301
        code = ast.args[1]
110✔
302
        for arg in code.code
110✔
303
            isa(arg, Expr) || continue
882✔
304
            _modules_to_be_loaded!(arg, mods)
502✔
305
        end
882✔
306
    end
307
end
308

309
function modules_to_be_loaded(ast::Expr, mods::Vector{Symbol} = Symbol[])
22✔
310
    _modules_to_be_loaded!(ast, mods)
258✔
311
    filter!(mod::Symbol -> !in(mod, (:Base, :Main, :Core)), mods) # Exclude special non-package modules
152✔
312
    return unique(mods)
129✔
313
end
314

315
"""
316
    start_repl_backend(repl_channel::Channel, response_channel::Channel)
317

318
    Starts loop for REPL backend
319
    Returns a REPLBackend with backend_task assigned
320

321
    Deprecated since sync / async behavior cannot be selected
322
"""
323
function start_repl_backend(repl_channel::Channel{Any}, response_channel::Channel{Any}
×
324
                            ; get_module::Function = ()->Main)
325
    # Maintain legacy behavior of asynchronous backend
326
    backend = REPLBackend(repl_channel, response_channel, false)
×
327
    # Assignment will be made twice, but will be immediately available
328
    backend.backend_task = @async start_repl_backend(backend; get_module)
×
329
    return backend
×
330
end
331

332
"""
333
    start_repl_backend(backend::REPLBackend)
334

335
    Call directly to run backend loop on current Task.
336
    Use @async for run backend on new Task.
337

338
    Does not return backend until loop is finished.
339
"""
340
function start_repl_backend(backend::REPLBackend,  @nospecialize(consumer = x -> nothing); get_module::Function = ()->Main)
56✔
341
    backend.backend_task = Base.current_task()
26✔
342
    consumer(backend)
26✔
343
    repl_backend_loop(backend, get_module)
26✔
344
    return backend
25✔
345
end
346

347
function repl_backend_loop(backend::REPLBackend, get_module::Function)
26✔
348
    # include looks at this to determine the relative include path
349
    # nothing means cwd
350
    while true
×
351
        tls = task_local_storage()
133✔
352
        tls[:SOURCE_PATH] = nothing
133✔
353
        ast, show_value = take!(backend.repl_channel)
133✔
354
        if show_value == -1
133✔
355
            # exit flag
356
            break
25✔
357
        end
358
        eval_user_input(ast, backend, get_module())
108✔
359
    end
107✔
360
    return nothing
25✔
361
end
362

363
struct REPLDisplay{Repl<:AbstractREPL} <: AbstractDisplay
364
    repl::Repl
35✔
365
end
366

367
function display(d::REPLDisplay, mime::MIME"text/plain", x)
63✔
368
    x = Ref{Any}(x)
63✔
369
    with_repl_linfo(d.repl) do io
63✔
370
        io = IOContext(io, :limit => true, :module => active_module(d)::Module)
123✔
371
        if d.repl isa LineEditREPL
3✔
372
            mistate = d.repl.mistate
60✔
373
            mode = LineEdit.mode(mistate)
60✔
374
            if mode isa LineEdit.Prompt
60✔
375
                LineEdit.write_output_prefix(io, mode, get(io, :color, false)::Bool)
240✔
376
            end
377
        end
378
        get(io, :color, false)::Bool && write(io, answer_color(d.repl))
69✔
379
        if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext)
3✔
380
            # this can override the :limit property set initially
381
            io = foldl(IOContext, d.repl.options.iocontext, init=io)
60✔
382
        end
383
        show(io, mime, x[])
63✔
384
        println(io)
62✔
385
    end
386
    return nothing
62✔
387
end
388
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)
63✔
389

390
function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool)
104✔
391
    repl.waserror = response[2]
104✔
392
    with_repl_linfo(repl) do io
104✔
393
        io = IOContext(io, :module => active_module(repl)::Module)
205✔
394
        print_response(io, response, show_value, have_color, specialdisplay(repl))
104✔
395
    end
396
    return nothing
104✔
397
end
398

399
function repl_display_error(errio::IO, @nospecialize errval)
9✔
400
    # this will be set to true if types in the stacktrace are truncated
401
    limitflag = Ref(false)
9✔
402
    errio = IOContext(errio, :stacktrace_types_limited => limitflag)
9✔
403
    Base.invokelatest(Base.display_error, errio, errval)
9✔
404
    if limitflag[]
7✔
405
        print(errio, "Some type information was truncated. Use `show(err)` to see complete types.")
2✔
406
        println(errio)
2✔
407
    end
408
    return nothing
7✔
409
end
410

411
function print_response(errio::IO, response, show_value::Bool, have_color::Bool, specialdisplay::Union{AbstractDisplay,Nothing}=nothing)
105✔
412
    Base.sigatomic_begin()
105✔
413
    val, iserr = response
105✔
414
    while true
106✔
415
        try
106✔
416
            Base.sigatomic_end()
106✔
417
            if iserr
106✔
418
                val = Base.scrub_repl_backtrace(val)
8✔
419
                Base.istrivialerror(val) || setglobal!(Base.MainInclude, :err, val)
13✔
420
                repl_display_error(errio, val)
8✔
421
            else
422
                if val !== nothing && show_value
98✔
423
                    try
63✔
424
                        if specialdisplay === nothing
63✔
425
                            Base.invokelatest(display, val)
49✔
426
                        else
427
                            Base.invokelatest(display, specialdisplay, val)
62✔
428
                        end
429
                    catch
430
                        println(errio, "Error showing value of type ", typeof(val), ":")
1✔
431
                        rethrow()
1✔
432
                    end
433
                end
434
            end
435
            break
106✔
436
        catch ex
437
            if iserr
2✔
438
                println(errio) # an error during printing is likely to leave us mid-line
1✔
439
                println(errio, "SYSTEM (REPL): showing an error caused an error")
1✔
440
                try
1✔
441
                    excs = Base.scrub_repl_backtrace(current_exceptions())
1✔
442
                    setglobal!(Base.MainInclude, :err, excs)
1✔
443
                    repl_display_error(errio, excs)
2✔
444
                catch e
445
                    # at this point, only print the name of the type as a Symbol to
446
                    # minimize the possibility of further errors.
447
                    println(errio)
1✔
448
                    println(errio, "SYSTEM (REPL): caught exception of type ", typeof(e).name.name,
1✔
449
                            " while trying to handle a nested exception; giving up")
450
                end
451
                break
1✔
452
            end
453
            val = current_exceptions()
1✔
454
            iserr = true
1✔
455
        end
456
    end
1✔
457
    Base.sigatomic_end()
105✔
458
    nothing
459
end
460

461
# A reference to a backend that is not mutable
462
struct REPLBackendRef
463
    repl_channel::Channel{Any}
24✔
464
    response_channel::Channel{Any}
465
end
466
REPLBackendRef(backend::REPLBackend) = REPLBackendRef(backend.repl_channel, backend.response_channel)
24✔
467

468
function destroy(ref::REPLBackendRef, state::Task)
23✔
469
    if istaskfailed(state)
23✔
470
        close(ref.repl_channel, TaskFailedException(state))
×
471
        close(ref.response_channel, TaskFailedException(state))
×
472
    end
473
    close(ref.repl_channel)
23✔
474
    close(ref.response_channel)
23✔
475
end
476

477
"""
478
    run_repl(repl::AbstractREPL)
479
    run_repl(repl, consumer = backend->nothing; backend_on_current_task = true)
480

481
    Main function to start the REPL
482

483
    consumer is an optional function that takes a REPLBackend as an argument
484
"""
485
function run_repl(repl::AbstractREPL, @nospecialize(consumer = x -> nothing); backend_on_current_task::Bool = true, backend = REPLBackend())
71✔
486
    backend_ref = REPLBackendRef(backend)
24✔
487
    cleanup = @task try
47✔
488
            destroy(backend_ref, t)
23✔
489
        catch e
490
            Core.print(Core.stderr, "\nINTERNAL ERROR: ")
×
491
            Core.println(Core.stderr, e)
×
492
            Core.println(Core.stderr, catch_backtrace())
×
493
        end
494
    get_module = () -> active_module(repl)
128✔
495
    if backend_on_current_task
24✔
496
        t = @async run_frontend(repl, backend_ref)
48✔
497
        errormonitor(t)
24✔
498
        Base._wait2(t, cleanup)
24✔
499
        start_repl_backend(backend, consumer; get_module)
24✔
500
    else
501
        t = @async start_repl_backend(backend, consumer; get_module)
×
502
        errormonitor(t)
×
503
        Base._wait2(t, cleanup)
×
504
        run_frontend(repl, backend_ref)
×
505
    end
506
    return backend
23✔
507
end
508

509
## BasicREPL ##
510

511
mutable struct BasicREPL <: AbstractREPL
512
    terminal::TextTerminal
513
    waserror::Bool
514
    frontend_task::Task
515
    BasicREPL(t) = new(t, false)
3✔
516
end
517

518
outstream(r::BasicREPL) = r.terminal
6✔
519
hascolor(r::BasicREPL) = hascolor(r.terminal)
×
520

521
function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
3✔
522
    repl.frontend_task = current_task()
3✔
523
    d = REPLDisplay(repl)
3✔
524
    dopushdisplay = !in(d,Base.Multimedia.displays)
6✔
525
    dopushdisplay && pushdisplay(d)
3✔
526
    hit_eof = false
3✔
527
    while true
6✔
528
        Base.reseteof(repl.terminal)
6✔
529
        write(repl.terminal, JULIA_PROMPT)
6✔
530
        line = ""
6✔
531
        ast = nothing
6✔
532
        interrupted = false
6✔
533
        while true
6✔
534
            try
6✔
535
                line *= readline(repl.terminal, keep=true)
6✔
536
            catch e
537
                if isa(e,InterruptException)
×
538
                    try # raise the debugger if present
×
539
                        ccall(:jl_raise_debugger, Int, ())
×
540
                    catch
×
541
                    end
542
                    line = ""
×
543
                    interrupted = true
×
544
                    break
×
545
                elseif isa(e,EOFError)
×
546
                    hit_eof = true
×
547
                    break
×
548
                else
549
                    rethrow()
×
550
                end
551
            end
552
            ast = Base.parse_input_line(line)
12✔
553
            (isa(ast,Expr) && ast.head === :incomplete) || break
12✔
554
        end
×
555
        if !isempty(line)
6✔
556
            response = eval_with_backend(ast, backend)
4✔
557
            print_response(repl, response, !ends_with_semicolon(line), false)
3✔
558
        end
559
        write(repl.terminal, '\n')
5✔
560
        ((!interrupted && isempty(line)) || hit_eof) && break
8✔
561
    end
3✔
562
    # terminate backend
563
    put!(backend.repl_channel, (nothing, -1))
2✔
564
    dopushdisplay && popdisplay(d)
2✔
565
    nothing
566
end
567

568
## LineEditREPL ##
569

570
mutable struct LineEditREPL <: AbstractREPL
571
    t::TextTerminal
572
    hascolor::Bool
573
    prompt_color::String
574
    input_color::String
575
    answer_color::String
576
    shell_color::String
577
    help_color::String
578
    pkg_color::String
579
    history_file::Bool
580
    in_shell::Bool
581
    in_help::Bool
582
    envcolors::Bool
583
    waserror::Bool
584
    specialdisplay::Union{Nothing,AbstractDisplay}
585
    options::Options
586
    mistate::Union{MIState,Nothing}
587
    last_shown_line_infos::Vector{Tuple{String,Int}}
588
    interface::ModalInterface
589
    backendref::REPLBackendRef
590
    frontend_task::Task
591
    function LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,pkg_color,history_file,in_shell,in_help,envcolors)
28✔
592
        opts = Options()
28✔
593
        opts.hascolor = hascolor
28✔
594
        if !hascolor
28✔
595
            opts.beep_colors = [""]
×
596
        end
597
        new(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,pkg_color,history_file,in_shell,
28✔
598
            in_help,envcolors,false,nothing, opts, nothing, Tuple{String,Int}[])
599
    end
600
end
601
outstream(r::LineEditREPL) = (t = r.t; t isa TTYTerminal ? t.out_stream : t)
171✔
602
specialdisplay(r::LineEditREPL) = r.specialdisplay
101✔
603
specialdisplay(r::AbstractREPL) = nothing
×
604
terminal(r::LineEditREPL) = r.t
158✔
605
hascolor(r::LineEditREPL) = r.hascolor
203✔
606

607
LineEditREPL(t::TextTerminal, hascolor::Bool, envcolors::Bool=false) =
56✔
608
    LineEditREPL(t, hascolor,
609
        hascolor ? Base.text_colors[:green] : "",
610
        hascolor ? Base.input_color() : "",
611
        hascolor ? Base.answer_color() : "",
612
        hascolor ? Base.text_colors[:red] : "",
613
        hascolor ? Base.text_colors[:yellow] : "",
614
        hascolor ? Base.text_colors[:blue] : "",
615
        false, false, false, envcolors
616
    )
617

618
mutable struct REPLCompletionProvider <: CompletionProvider
619
    modifiers::LineEdit.Modifiers
25✔
620
end
621
REPLCompletionProvider() = REPLCompletionProvider(LineEdit.Modifiers())
25✔
622

623
mutable struct ShellCompletionProvider <: CompletionProvider end
25✔
624
struct LatexCompletions <: CompletionProvider end
625

626
function active_module() # this method is also called from Base
24,813✔
627
    isdefined(Base, :active_repl) || return Main
49,626✔
628
    return active_module(Base.active_repl::AbstractREPL)
×
629
end
630
active_module((; mistate)::LineEditREPL) = mistate === nothing ? Main : mistate.active_module
4,787✔
631
active_module(::AbstractREPL) = Main
10✔
632
active_module(d::REPLDisplay) = active_module(d.repl)
123✔
633

634
setmodifiers!(c::CompletionProvider, m::LineEdit.Modifiers) = nothing
×
635

636
setmodifiers!(c::REPLCompletionProvider, m::LineEdit.Modifiers) = c.modifiers = m
×
637

638
"""
639
    activate(mod::Module=Main)
640

641
Set `mod` as the default contextual module in the REPL,
642
both for evaluating expressions and printing them.
643
"""
644
function activate(mod::Module=Main)
×
645
    mistate = (Base.active_repl::LineEditREPL).mistate
×
646
    mistate === nothing && return nothing
×
647
    mistate.active_module = mod
×
648
    Base.load_InteractiveUtils(mod)
×
649
    return nothing
×
650
end
651

652
beforecursor(buf::IOBuffer) = String(buf.data[1:buf.ptr-1])
4,394✔
653

654
function complete_line(c::REPLCompletionProvider, s::PromptState, mod::Module; hint::Bool=false)
3,486✔
655
    partial = beforecursor(s.input_buffer)
3,486✔
656
    full = LineEdit.input_string(s)
1,743✔
657
    ret, range, should_complete = completions(full, lastindex(partial), mod, c.modifiers.shift, hint)
3,486✔
658
    c.modifiers = LineEdit.Modifiers()
1,743✔
659
    return unique!(map(completion_text, ret)), partial[range], should_complete
1,743✔
660
end
661

662
function complete_line(c::ShellCompletionProvider, s::PromptState; hint::Bool=false)
844✔
663
    # First parse everything up to the current position
664
    partial = beforecursor(s.input_buffer)
844✔
665
    full = LineEdit.input_string(s)
422✔
666
    ret, range, should_complete = shell_completions(full, lastindex(partial), hint)
844✔
667
    return unique!(map(completion_text, ret)), partial[range], should_complete
422✔
668
end
669

670
function complete_line(c::LatexCompletions, s; hint::Bool=false)
×
671
    partial = beforecursor(LineEdit.buffer(s))
×
672
    full = LineEdit.input_string(s)::String
×
673
    ret, range, should_complete = bslash_completions(full, lastindex(partial), hint)[2]
×
674
    return unique!(map(completion_text, ret)), partial[range], should_complete
×
675
end
676

677
with_repl_linfo(f, repl) = f(outstream(repl))
6✔
678
function with_repl_linfo(f, repl::LineEditREPL)
161✔
679
    linfos = Tuple{String,Int}[]
161✔
680
    io = IOContext(outstream(repl), :last_shown_line_infos => linfos)
161✔
681
    f(io)
161✔
682
    if !isempty(linfos)
160✔
683
        repl.last_shown_line_infos = linfos
6✔
684
    end
685
    nothing
686
end
687

688
mutable struct REPLHistoryProvider <: HistoryProvider
689
    history::Vector{String}
30✔
690
    file_path::String
691
    history_file::Union{Nothing,IO}
692
    start_idx::Int
693
    cur_idx::Int
694
    last_idx::Int
695
    last_buffer::IOBuffer
696
    last_mode::Union{Nothing,Prompt}
697
    mode_mapping::Dict{Symbol,Prompt}
698
    modes::Vector{Symbol}
699
end
700
REPLHistoryProvider(mode_mapping::Dict{Symbol}) =
30✔
701
    REPLHistoryProvider(String[], "", nothing, 0, 0, -1, IOBuffer(),
702
                        nothing, mode_mapping, UInt8[])
703

704
invalid_history_message(path::String) = """
×
705
Invalid history file ($path) format:
706
If you have a history file left over from an older version of Julia,
707
try renaming or deleting it.
708
Invalid character: """
709

710
munged_history_message(path::String) = """
×
711
Invalid history file ($path) format:
712
An editor may have converted tabs to spaces at line """
713

714
function hist_open_file(hp::REPLHistoryProvider)
715
    f = open(hp.file_path, read=true, write=true, create=true)
4✔
716
    hp.history_file = f
4✔
717
    seekend(f)
4✔
718
end
719

720
function hist_from_file(hp::REPLHistoryProvider, path::String)
8✔
721
    getline(lines, i) = i > length(lines) ? "" : lines[i]
276✔
722
    file_lines = readlines(path)
8✔
723
    countlines = 0
×
724
    while true
42✔
725
        # First parse the metadata that starts with '#' in particular the REPL mode
726
        countlines += 1
42✔
727
        line = getline(file_lines, countlines)
76✔
728
        mode = :julia
×
729
        isempty(line) && break
42✔
730
        line[1] != '#' &&
68✔
731
            error(invalid_history_message(path), repr(line[1]), " at line ", countlines)
732
        while !isempty(line)
102✔
733
            startswith(line, '#') || break
204✔
734
            if startswith(line, "# mode: ")
68✔
735
                mode = Symbol(SubString(line, 9))
68✔
736
            end
737
            countlines += 1
68✔
738
            line = getline(file_lines, countlines)
136✔
739
        end
68✔
740
        isempty(line) && break
34✔
741

742
        # Now parse the code for the current REPL mode
743
        line[1] == ' '  &&
68✔
744
            error(munged_history_message(path), countlines)
745
        line[1] != '\t' &&
68✔
746
            error(invalid_history_message(path), repr(line[1]), " at line ", countlines)
747
        lines = String[]
34✔
748
        while !isempty(line)
34✔
749
            push!(lines, chomp(SubString(line, 2)))
68✔
750
            next_line = getline(file_lines, countlines+1)
64✔
751
            isempty(next_line) && break
34✔
752
            first(next_line) == ' '  && error(munged_history_message(path), countlines)
60✔
753
            # A line not starting with a tab means we are done with code for this entry
754
            first(next_line) != '\t' && break
60✔
755
            countlines += 1
×
756
            line = getline(file_lines, countlines)
×
757
        end
×
758
        push!(hp.modes, mode)
34✔
759
        push!(hp.history, join(lines, '\n'))
34✔
760
    end
34✔
761
    hp.start_idx = length(hp.history)
8✔
762
    return hp
8✔
763
end
764

765
function add_history(hist::REPLHistoryProvider, s::PromptState)
119✔
766
    str = rstrip(String(take!(copy(s.input_buffer))))
223✔
767
    isempty(strip(str)) && return
119✔
768
    mode = mode_idx(hist, LineEdit.mode(s))
101✔
769
    !isempty(hist.history) &&
101✔
770
        isequal(mode, hist.modes[end]) && str == hist.history[end] && return
771
    push!(hist.modes, mode)
95✔
772
    push!(hist.history, str)
95✔
773
    hist.history_file === nothing && return
95✔
774
    entry = """
16✔
775
    # time: $(Libc.strftime("%Y-%m-%d %H:%M:%S %Z", time()))
776
    # mode: $mode
777
    $(replace(str, r"^"ms => "\t"))
778
    """
779
    # TODO: write-lock history file
780
    try
16✔
781
        seekend(hist.history_file)
16✔
782
    catch err
783
        (err isa SystemError) || rethrow()
×
784
        # File handle might get stale after a while, especially under network file systems
785
        # If this doesn't fix it (e.g. when file is deleted), we'll end up rethrowing anyway
786
        hist_open_file(hist)
×
787
    end
788
    print(hist.history_file, entry)
32✔
789
    flush(hist.history_file)
16✔
790
    nothing
791
end
792

793
function history_move(s::Union{LineEdit.MIState,LineEdit.PrefixSearchState}, hist::REPLHistoryProvider, idx::Int, save_idx::Int = hist.cur_idx)
96✔
794
    max_idx = length(hist.history) + 1
136✔
795
    @assert 1 <= hist.cur_idx <= max_idx
96✔
796
    (1 <= idx <= max_idx) || return :none
98✔
797
    idx != hist.cur_idx || return :none
94✔
798

799
    # save the current line
800
    if save_idx == max_idx
94✔
801
        hist.last_mode = LineEdit.mode(s)
46✔
802
        hist.last_buffer = copy(LineEdit.buffer(s))
46✔
803
    else
804
        hist.history[save_idx] = LineEdit.input_string(s)
84✔
805
        hist.modes[save_idx] = mode_idx(hist, LineEdit.mode(s))
63✔
806
    end
807

808
    # load the saved line
809
    if idx == max_idx
94✔
810
        last_buffer = hist.last_buffer
10✔
811
        LineEdit.transition(s, hist.last_mode) do
10✔
812
            LineEdit.replace_line(s, last_buffer)
10✔
813
        end
814
        hist.last_mode = nothing
10✔
815
        hist.last_buffer = IOBuffer()
10✔
816
    else
817
        if haskey(hist.mode_mapping, hist.modes[idx])
168✔
818
            LineEdit.transition(s, hist.mode_mapping[hist.modes[idx]]) do
68✔
819
                LineEdit.replace_line(s, hist.history[idx])
68✔
820
            end
821
        else
822
            return :skip
16✔
823
        end
824
    end
825
    hist.cur_idx = idx
78✔
826

827
    return :ok
78✔
828
end
829

830
# REPL History can also transitions modes
831
function LineEdit.accept_result_newmode(hist::REPLHistoryProvider)
27✔
832
    if 1 <= hist.cur_idx <= length(hist.modes)
27✔
833
        return hist.mode_mapping[hist.modes[hist.cur_idx]]
23✔
834
    end
835
    return nothing
4✔
836
end
837

838
function history_prev(s::LineEdit.MIState, hist::REPLHistoryProvider,
34✔
839
                      num::Int=1, save_idx::Int = hist.cur_idx)
840
    num <= 0 && return history_next(s, hist, -num, save_idx)
58✔
841
    hist.last_idx = -1
32✔
842
    m = history_move(s, hist, hist.cur_idx-num, save_idx)
32✔
843
    if m === :ok
32✔
844
        LineEdit.move_input_start(s)
48✔
845
        LineEdit.reset_key_repeats(s) do
24✔
846
            LineEdit.move_line_end(s)
24✔
847
        end
848
        return LineEdit.refresh_line(s)
24✔
849
    elseif m === :skip
8✔
850
        return history_prev(s, hist, num+1, save_idx)
8✔
851
    else
852
        return Terminals.beep(s)
×
853
    end
854
end
855

856
function history_next(s::LineEdit.MIState, hist::REPLHistoryProvider,
26✔
857
                      num::Int=1, save_idx::Int = hist.cur_idx)
858
    if num == 0
44✔
859
        Terminals.beep(s)
×
860
        return
×
861
    end
862
    num < 0 && return history_prev(s, hist, -num, save_idx)
26✔
863
    cur_idx = hist.cur_idx
24✔
864
    max_idx = length(hist.history) + 1
24✔
865
    if cur_idx == max_idx && 0 < hist.last_idx
24✔
866
        # issue #6312
867
        cur_idx = hist.last_idx
×
868
        hist.last_idx = -1
×
869
    end
870
    m = history_move(s, hist, cur_idx+num, save_idx)
24✔
871
    if m === :ok
24✔
872
        LineEdit.move_input_end(s)
16✔
873
        return LineEdit.refresh_line(s)
16✔
874
    elseif m === :skip
8✔
875
        return history_next(s, hist, num+1, save_idx)
6✔
876
    else
877
        return Terminals.beep(s)
2✔
878
    end
879
end
880

881
history_first(s::LineEdit.MIState, hist::REPLHistoryProvider) =
6✔
882
    history_prev(s, hist, hist.cur_idx - 1 -
883
                 (hist.cur_idx > hist.start_idx+1 ? hist.start_idx : 0))
884

885
history_last(s::LineEdit.MIState, hist::REPLHistoryProvider) =
4✔
886
    history_next(s, hist, length(hist.history) - hist.cur_idx + 1)
887

888
function history_move_prefix(s::LineEdit.PrefixSearchState,
38✔
889
                             hist::REPLHistoryProvider,
890
                             prefix::AbstractString,
891
                             backwards::Bool,
892
                             cur_idx::Int = hist.cur_idx)
893
    cur_response = String(take!(copy(LineEdit.buffer(s))))
101✔
894
    # when searching forward, start at last_idx
895
    if !backwards && hist.last_idx > 0
38✔
896
        cur_idx = hist.last_idx
1✔
897
    end
898
    hist.last_idx = -1
38✔
899
    max_idx = length(hist.history)+1
38✔
900
    idxs = backwards ? ((cur_idx-1):-1:1) : ((cur_idx+1):1:max_idx)
65✔
901
    for idx in idxs
38✔
902
        if (idx == max_idx) || (startswith(hist.history[idx], prefix) && (hist.history[idx] != cur_response || get(hist.mode_mapping, hist.modes[idx], nothing) !== LineEdit.mode(s)))
192✔
903
            m = history_move(s, hist, idx)
36✔
904
            if m === :ok
36✔
905
                if idx == max_idx
34✔
906
                    # on resuming the in-progress edit, leave the cursor where the user last had it
907
                elseif isempty(prefix)
30✔
908
                    # on empty prefix search, move cursor to the end
909
                    LineEdit.move_input_end(s)
14✔
910
                else
911
                    # otherwise, keep cursor at the prefix position as a visual cue
912
                    seek(LineEdit.buffer(s), sizeof(prefix))
16✔
913
                end
914
                LineEdit.refresh_line(s)
34✔
915
                return :ok
34✔
916
            elseif m === :skip
2✔
917
                return history_move_prefix(s,hist,prefix,backwards,idx)
2✔
918
            end
919
        end
920
    end
62✔
921
    Terminals.beep(s)
×
922
    nothing
923
end
924
history_next_prefix(s::LineEdit.PrefixSearchState, hist::REPLHistoryProvider, prefix::AbstractString) =
5✔
925
    history_move_prefix(s, hist, prefix, false)
926
history_prev_prefix(s::LineEdit.PrefixSearchState, hist::REPLHistoryProvider, prefix::AbstractString) =
31✔
927
    history_move_prefix(s, hist, prefix, true)
928

929
function history_search(hist::REPLHistoryProvider, query_buffer::IOBuffer, response_buffer::IOBuffer,
28✔
930
                        backwards::Bool=false, skip_current::Bool=false)
931

932
    qpos = position(query_buffer)
28✔
933
    qpos > 0 || return true
28✔
934
    searchdata = beforecursor(query_buffer)
56✔
935
    response_str = String(take!(copy(response_buffer)))
50✔
936

937
    # Alright, first try to see if the current match still works
938
    a = position(response_buffer) + 1 # position is zero-indexed
28✔
939
    # FIXME: I'm pretty sure this is broken since it uses an index
940
    # into the search data to index into the response string
941
    b = a + sizeof(searchdata)
28✔
942
    b = b ≤ ncodeunits(response_str) ? prevind(response_str, b) : b-1
48✔
943
    b = min(lastindex(response_str), b) # ensure that b is valid
50✔
944

945
    searchstart = backwards ? b : a
28✔
946
    if searchdata == response_str[a:b]
44✔
947
        if skip_current
10✔
948
            searchstart = backwards ? prevind(response_str, b) : nextind(response_str, a)
4✔
949
        else
950
            return true
6✔
951
        end
952
    end
953

954
    # Start searching
955
    # First the current response buffer
956
    if 1 <= searchstart <= lastindex(response_str)
36✔
957
        match = backwards ? findprev(searchdata, response_str, searchstart) :
14✔
958
                            findnext(searchdata, response_str, searchstart)
959
        if match !== nothing
14✔
960
            seek(response_buffer, first(match) - 1)
12✔
961
            return true
6✔
962
        end
963
    end
964

965
    # Now search all the other buffers
966
    idxs = backwards ? ((hist.cur_idx-1):-1:1) : ((hist.cur_idx+1):1:length(hist.history))
32✔
967
    for idx in idxs
16✔
968
        h = hist.history[idx]
40✔
969
        match = backwards ? findlast(searchdata, h) : findfirst(searchdata, h)
80✔
970
        if match !== nothing && h != response_str && haskey(hist.mode_mapping, hist.modes[idx])
54✔
971
            truncate(response_buffer, 0)
12✔
972
            write(response_buffer, h)
12✔
973
            seek(response_buffer, first(match) - 1)
24✔
974
            hist.cur_idx = idx
12✔
975
            return true
12✔
976
        end
977
    end
52✔
978

979
    return false
4✔
980
end
981

982
function history_reset_state(hist::REPLHistoryProvider)
983
    if hist.cur_idx != length(hist.history) + 1
276✔
984
        hist.last_idx = hist.cur_idx
133✔
985
        hist.cur_idx = length(hist.history) + 1
133✔
986
    end
987
    nothing
988
end
989
LineEdit.reset_state(hist::REPLHistoryProvider) = history_reset_state(hist)
244✔
990

991
function return_callback(s)
98✔
992
    ast = Base.parse_input_line(String(take!(copy(LineEdit.buffer(s)))), depwarn=false)
181✔
993
    return !(isa(ast, Expr) && ast.head === :incomplete)
98✔
994
end
995

996
find_hist_file() = get(ENV, "JULIA_HISTORY",
8✔
997
                       !isempty(DEPOT_PATH) ? joinpath(DEPOT_PATH[1], "logs", "repl_history.jl") :
998
                       error("DEPOT_PATH is empty and ENV[\"JULIA_HISTORY\"] not set."))
999

1000
backend(r::AbstractREPL) = r.backendref
100✔
1001

1002
function eval_with_backend(ast, backend::REPLBackendRef)
104✔
1003
    put!(backend.repl_channel, (ast, 1))
104✔
1004
    return take!(backend.response_channel) # (val, iserr)
104✔
1005
end
1006

1007
function respond(f, repl, main; pass_empty::Bool = false, suppress_on_semicolon::Bool = true)
100✔
1008
    return function do_respond(s::MIState, buf, ok::Bool)
237✔
1009
        if !ok
137✔
1010
            return transition(s, :abort)
21✔
1011
        end
1012
        line = String(take!(buf)::Vector{UInt8})
217✔
1013
        if !isempty(line) || pass_empty
131✔
1014
            reset(repl)
101✔
1015
            local response
1016
            try
101✔
1017
                ast = Base.invokelatest(f, line)
101✔
1018
                response = eval_with_backend(ast, backend(repl))
100✔
1019
            catch
1020
                response = Pair{Any, Bool}(current_exceptions(), true)
1✔
1021
            end
1022
            hide_output = suppress_on_semicolon && ends_with_semicolon(line)
101✔
1023
            print_response(repl, response, !hide_output, hascolor(repl))
101✔
1024
        end
1025
        prepare_next(repl)
116✔
1026
        reset_state(s)
116✔
1027
        return s.current_mode.sticky ? true : transition(s, main)
116✔
1028
    end
1029
end
1030

1031
function reset(repl::LineEditREPL)
101✔
1032
    raw!(repl.t, false)
101✔
1033
    hascolor(repl) && print(repl.t, Base.text_colors[:normal])
101✔
1034
    nothing
1035
end
1036

1037
function prepare_next(repl::LineEditREPL)
116✔
1038
    println(terminal(repl))
116✔
1039
end
1040

1041
function mode_keymap(julia_prompt::Prompt)
1042
    AnyDict(
27✔
1043
    '\b' => function (s::MIState,o...)
7✔
1044
        if isempty(s) || position(LineEdit.buffer(s)) == 0
7✔
1045
            buf = copy(LineEdit.buffer(s))
7✔
1046
            transition(s, julia_prompt) do
7✔
1047
                LineEdit.state(s, julia_prompt).input_buffer = buf
7✔
1048
            end
1049
        else
1050
            LineEdit.edit_backspace(s)
×
1051
        end
1052
    end,
1053
    "^C" => function (s::MIState,o...)
1054
        LineEdit.move_input_end(s)
1055
        LineEdit.refresh_line(s)
1056
        print(LineEdit.terminal(s), "^C\n\n")
1057
        transition(s, julia_prompt)
1058
        transition(s, :reset)
1059
        LineEdit.refresh_line(s)
1060
    end)
1061
end
1062

1063
repl_filename(repl, hp::REPLHistoryProvider) = "REPL[$(max(length(hp.history)-hp.start_idx, 1))]"
90✔
1064
repl_filename(repl, hp) = "REPL"
×
1065

1066
const JL_PROMPT_PASTE = Ref(true)
1067
enable_promptpaste(v::Bool) = JL_PROMPT_PASTE[] = v
×
1068

1069
function contextual_prompt(repl::LineEditREPL, prompt::Union{String,Function})
1070
    function ()
2,186✔
1071
        mod = active_module(repl)
4,265✔
1072
        prefix = mod == Main ? "" : string('(', mod, ") ")
2,168✔
1073
        pr = prompt isa String ? prompt : prompt()
2,136✔
1074
        prefix * pr
2,136✔
1075
    end
1076
end
1077

1078
setup_interface(
71✔
1079
    repl::LineEditREPL;
1080
    # those keyword arguments may be deprecated eventually in favor of the Options mechanism
1081
    hascolor::Bool = repl.options.hascolor,
1082
    extra_repl_keymap::Any = repl.options.extra_keymap
1083
) = setup_interface(repl, hascolor, extra_repl_keymap)
1084

1085
const Pkg_pkgid = Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg")
1086
const Pkg_REPLExt_pkgid = Base.PkgId(Base.UUID("ceef7b17-42e7-5b1c-81d4-4cc4a2494ccf"), "REPLExt")
1087

NEW
1088
function load_pkg()
×
NEW
1089
    @lock Base.require_lock begin
×
NEW
1090
        REPLExt = Base.require_stdlib(Pkg_pkgid, "REPLExt")
×
1091
        # require_stdlib does not guarantee that the `__init__` of the package is done when loading is done async
1092
        # but we need to wait for the repl mode to be set up
NEW
1093
        lock = get(Base.package_locks, Pkg_REPLExt_pkgid.uuid, nothing)
×
1094
        lock !== nothing && wait(lock[2])
NEW
1095
        return REPLExt
×
1096
    end
1097
end
1098

1099
# This non keyword method can be precompiled which is important
1100
function setup_interface(
25✔
1101
    repl::LineEditREPL,
1102
    hascolor::Bool,
1103
    extra_repl_keymap::Any, # Union{Dict,Vector{<:Dict}},
1104
)
1105
    # The precompile statement emitter has problem outputting valid syntax for the
1106
    # type of `Union{Dict,Vector{<:Dict}}` (see #28808).
1107
    # This function is however important to precompile for REPL startup time, therefore,
1108
    # make the type Any and just assert that we have the correct type below.
1109
    @assert extra_repl_keymap isa Union{Dict,Vector{<:Dict}}
25✔
1110

1111
    ###
1112
    #
1113
    # This function returns the main interface that describes the REPL
1114
    # functionality, it is called internally by functions that setup a
1115
    # Terminal-based REPL frontend.
1116
    #
1117
    # See run_frontend(repl::LineEditREPL, backend::REPLBackendRef)
1118
    # for usage
1119
    #
1120
    ###
1121

1122
    ###
1123
    # We setup the interface in two stages.
1124
    # First, we set up all components (prompt,rsearch,shell,help)
1125
    # Second, we create keymaps with appropriate transitions between them
1126
    #   and assign them to the components
1127
    #
1128
    ###
1129

1130
    ############################### Stage I ################################
1131

1132
    # This will provide completions for REPL and help mode
1133
    replc = REPLCompletionProvider()
25✔
1134

1135
    # Set up the main Julia prompt
1136
    julia_prompt = Prompt(contextual_prompt(repl, JULIA_PROMPT);
50✔
1137
        # Copy colors from the prompt object
1138
        prompt_prefix = hascolor ? repl.prompt_color : "",
1139
        prompt_suffix = hascolor ?
1140
            (repl.envcolors ? Base.input_color : repl.input_color) : "",
1141
        repl = repl,
1142
        complete = replc,
1143
        on_enter = return_callback)
1144

1145
    # Setup help mode
1146
    help_mode = Prompt(contextual_prompt(repl, "help?> "),
50✔
1147
        prompt_prefix = hascolor ? repl.help_color : "",
1148
        prompt_suffix = hascolor ?
1149
            (repl.envcolors ? Base.input_color : repl.input_color) : "",
1150
        repl = repl,
1151
        complete = replc,
1152
        # When we're done transform the entered line into a call to helpmode function
1153
        on_done = respond(line::String->helpmode(outstream(repl), line, repl.mistate.active_module),
2✔
1154
                          repl, julia_prompt, pass_empty=true, suppress_on_semicolon=false))
1155

1156

1157
    # Set up shell mode
1158
    shell_mode = Prompt(SHELL_PROMPT;
50✔
1159
        prompt_prefix = hascolor ? repl.shell_color : "",
1160
        prompt_suffix = hascolor ?
1161
            (repl.envcolors ? Base.input_color : repl.input_color) : "",
1162
        repl = repl,
1163
        complete = ShellCompletionProvider(),
1164
        # Transform "foo bar baz" into `foo bar baz` (shell quoting)
1165
        # and pass into Base.repl_cmd for processing (handles `ls` and `cd`
1166
        # special)
1167
        on_done = respond(repl, julia_prompt) do line
1168
            Expr(:call, :(Base.repl_cmd),
9✔
1169
                :(Base.cmd_gen($(Base.shell_parse(line::String)[1]))),
1170
                outstream(repl))
1171
        end,
1172
        sticky = true)
1173

1174
    # Set up dummy Pkg mode that will be replaced once Pkg is loaded
1175
    # use 6 dots to occupy the same space as the most likely "@v1.xx" env name
1176
    dummy_pkg_mode = Prompt("(......) $PKG_PROMPT",
50✔
1177
        prompt_prefix = hascolor ? repl.pkg_color : "",
1178
        prompt_suffix = hascolor ?
1179
        (repl.envcolors ? Base.input_color : repl.input_color) : "",
1180
        repl = repl,
1181
        complete = LineEdit.EmptyCompletionProvider(),
1182
        on_done = respond(line->nothing, repl, julia_prompt),
1183
        on_enter = function (s::MIState)
1184
                # This is hit when the user tries to execute a command before the real Pkg mode has been
1185
                # switched to. Ok to do this even if Pkg is loading on the other task because of the loading lock.
1186
                REPLExt = load_pkg()
1187
                if REPLExt isa Module && isdefined(REPLExt, :PkgCompletionProvider)
1188
                    for mode in repl.interface.modes
1189
                        if mode isa LineEdit.Prompt && mode.complete isa REPLExt.PkgCompletionProvider
1190
                            # pkg mode
1191
                            buf = copy(LineEdit.buffer(s))
1192
                            transition(s, mode) do
1193
                                LineEdit.state(s, mode).input_buffer = buf
1194
                            end
1195
                        end
1196
                    end
1197
                end
1198
                return true
1199
            end,
1200
        sticky = true)
1201

1202

1203
    ################################# Stage II #############################
1204

1205
    # Setup history
1206
    # We will have a unified history for all REPL modes
1207
    hp = REPLHistoryProvider(Dict{Symbol,Prompt}(:julia => julia_prompt,
25✔
1208
                                                 :shell => shell_mode,
1209
                                                 :help  => help_mode,
1210
                                                 :pkg  => dummy_pkg_mode))
1211
    if repl.history_file
25✔
1212
        try
4✔
1213
            hist_path = find_hist_file()
8✔
1214
            mkpath(dirname(hist_path))
4✔
1215
            hp.file_path = hist_path
4✔
1216
            hist_open_file(hp)
4✔
1217
            finalizer(replc) do replc
4✔
1218
                close(hp.history_file)
4✔
1219
            end
1220
            hist_from_file(hp, hist_path)
4✔
1221
        catch
1222
            # use REPL.hascolor to avoid using the local variable with the same name
1223
            print_response(repl, Pair{Any, Bool}(current_exceptions(), true), true, REPL.hascolor(repl))
×
1224
            println(outstream(repl))
×
1225
            @info "Disabling history file for this session"
×
1226
            repl.history_file = false
×
1227
        end
1228
    end
1229
    history_reset_state(hp)
25✔
1230
    julia_prompt.hist = hp
25✔
1231
    shell_mode.hist = hp
25✔
1232
    help_mode.hist = hp
25✔
1233
    dummy_pkg_mode.hist = hp
25✔
1234

1235
    julia_prompt.on_done = respond(x->Base.parse_input_line(x,filename=repl_filename(repl,hp)), repl, julia_prompt)
115✔
1236

1237

1238
    search_prompt, skeymap = LineEdit.setup_search_keymap(hp)
25✔
1239
    search_prompt.complete = LatexCompletions()
25✔
1240

1241
    shell_prompt_len = length(SHELL_PROMPT)
×
1242
    help_prompt_len = length(HELP_PROMPT)
×
1243
    jl_prompt_regex = r"^In \[[0-9]+\]: |^(?:\(.+\) )?julia> "
×
1244
    pkg_prompt_regex = r"^(?:\(.+\) )?pkg> "
×
1245

1246
    # Canonicalize user keymap input
1247
    if isa(extra_repl_keymap, Dict)
25✔
1248
        extra_repl_keymap = AnyDict[extra_repl_keymap]
×
1249
    end
1250

1251
    repl_keymap = AnyDict(
25✔
1252
        ';' => function (s::MIState,o...)
55✔
1253
            if isempty(s) || position(LineEdit.buffer(s)) == 0
103✔
1254
                buf = copy(LineEdit.buffer(s))
7✔
1255
                transition(s, shell_mode) do
7✔
1256
                    LineEdit.state(s, shell_mode).input_buffer = buf
7✔
1257
                end
1258
            else
1259
                edit_insert(s, ';')
48✔
1260
            end
1261
        end,
1262
        '?' => function (s::MIState,o...)
1✔
1263
            if isempty(s) || position(LineEdit.buffer(s)) == 0
1✔
1264
                buf = copy(LineEdit.buffer(s))
1✔
1265
                transition(s, help_mode) do
1✔
1266
                    LineEdit.state(s, help_mode).input_buffer = buf
1✔
1267
                end
1268
            else
1269
                edit_insert(s, '?')
×
1270
            end
1271
        end,
1272
        ']' => function (s::MIState,o...)
3✔
1273
            if isempty(s) || position(LineEdit.buffer(s)) == 0
6✔
NEW
1274
                buf = copy(LineEdit.buffer(s))
×
NEW
1275
                transition(s, dummy_pkg_mode) do
×
1276
                    LineEdit.state(s, dummy_pkg_mode).input_buffer = buf
1277
                end
1278
                # load Pkg on another thread if available so that typing in the dummy Pkg prompt
1279
                # isn't blocked, but instruct the main REPL task to do the transition via s.async_channel
UNCOV
1280
                t_replswitch = Threads.@spawn begin
×
1281
                    REPLExt = load_pkg()
1282
                    if REPLExt isa Module && isdefined(REPLExt, :PkgCompletionProvider)
1283
                        put!(s.async_channel,
1284
                            function (s::MIState, _)
1285
                                LineEdit.mode(s) === dummy_pkg_mode || return :ok
1286
                                for mode in repl.interface.modes
1287
                                    if mode isa LineEdit.Prompt && mode.complete isa REPLExt.PkgCompletionProvider
1288
                                        buf = copy(LineEdit.buffer(s))
1289
                                        transition(s, mode) do
1290
                                            LineEdit.state(s, mode).input_buffer = buf
1291
                                        end
1292
                                        if !isempty(s) && @invokelatest(LineEdit.check_for_hint(s))
1293
                                            @invokelatest(LineEdit.refresh_line(s))
1294
                                        end
1295
                                        break
1296
                                    end
1297
                                end
1298
                                return :ok
1299
                            end
1300
                        )
1301
                    end
1302
                end
1303
                Base.errormonitor(t_replswitch)
×
1304
            else
1305
                edit_insert(s, ']')
3✔
1306
            end
1307
        end,
1308

1309
        # Bracketed Paste Mode
1310
        "\e[200~" => (s::MIState,o...)->begin
8✔
1311
            input = LineEdit.bracketed_paste(s) # read directly from s until reaching the end-bracketed-paste marker
8✔
1312
            sbuffer = LineEdit.buffer(s)
8✔
1313
            curspos = position(sbuffer)
8✔
1314
            seek(sbuffer, 0)
16✔
1315
            shouldeval = (bytesavailable(sbuffer) == curspos && !occursin(UInt8('\n'), sbuffer))
8✔
1316
            seek(sbuffer, curspos)
16✔
1317
            if curspos == 0
8✔
1318
                # if pasting at the beginning, strip leading whitespace
1319
                input = lstrip(input)
7✔
1320
            end
1321
            if !shouldeval
8✔
1322
                # when pasting in the middle of input, just paste in place
1323
                # don't try to execute all the WIP, since that's rather confusing
1324
                # and is often ill-defined how it should behave
1325
                edit_insert(s, input)
×
1326
                return
×
1327
            end
1328
            LineEdit.push_undo(s)
8✔
1329
            edit_insert(sbuffer, input)
8✔
1330
            input = String(take!(sbuffer))
16✔
1331
            oldpos = firstindex(input)
×
1332
            firstline = true
×
1333
            isprompt_paste = false
×
1334
            curr_prompt_len = 0
×
1335
            pasting_help = false
×
1336

1337
            while oldpos <= lastindex(input) # loop until all lines have been executed
50✔
1338
                if JL_PROMPT_PASTE[]
24✔
1339
                    # Check if the next statement starts with a prompt i.e. "julia> ", in that case
1340
                    # skip it. But first skip whitespace unless pasting in a docstring which may have
1341
                    # indented prompt examples that we don't want to execute
1342
                    while input[oldpos] in (pasting_help ? ('\n') : ('\n', ' ', '\t'))
66✔
1343
                        oldpos = nextind(input, oldpos)
28✔
1344
                        oldpos >= sizeof(input) && return
14✔
1345
                    end
14✔
1346
                    substr = SubString(input, oldpos)
48✔
1347
                    # Check if input line starts with "julia> ", remove it if we are in prompt paste mode
1348
                    if (firstline || isprompt_paste) && startswith(substr, jl_prompt_regex)
24✔
1349
                        detected_jl_prompt = match(jl_prompt_regex, substr).match
11✔
1350
                        isprompt_paste = true
×
1351
                        curr_prompt_len = sizeof(detected_jl_prompt)
11✔
1352
                        oldpos += curr_prompt_len
11✔
1353
                        transition(s, julia_prompt)
11✔
1354
                        pasting_help = false
×
1355
                    # Check if input line starts with "pkg> " or "(...) pkg> ", remove it if we are in prompt paste mode and switch mode
1356
                    elseif (firstline || isprompt_paste) && startswith(substr, pkg_prompt_regex)
13✔
1357
                        detected_pkg_prompt = match(pkg_prompt_regex, substr).match
×
1358
                        isprompt_paste = true
×
1359
                        curr_prompt_len = sizeof(detected_pkg_prompt)
×
1360
                        oldpos += curr_prompt_len
×
1361
                        Base.active_repl.interface.modes[1].keymap_dict[']'](s, o...)
×
1362
                        pasting_help = false
×
1363
                    # Check if input line starts with "shell> ", remove it if we are in prompt paste mode and switch mode
1364
                    elseif (firstline || isprompt_paste) && startswith(substr, SHELL_PROMPT)
13✔
1365
                        isprompt_paste = true
×
1366
                        oldpos += shell_prompt_len
2✔
1367
                        curr_prompt_len = shell_prompt_len
2✔
1368
                        transition(s, shell_mode)
2✔
1369
                        pasting_help = false
×
1370
                    # Check if input line starts with "help?> ", remove it if we are in prompt paste mode and switch mode
1371
                    elseif (firstline || isprompt_paste) && startswith(substr, HELP_PROMPT)
11✔
1372
                        isprompt_paste = true
×
1373
                        oldpos += help_prompt_len
1✔
1374
                        curr_prompt_len = help_prompt_len
1✔
1375
                        transition(s, help_mode)
1✔
1376
                        pasting_help = true
×
1377
                    # If we are prompt pasting and current statement does not begin with a mode prefix, skip to next line
1378
                    elseif isprompt_paste
10✔
1379
                        while input[oldpos] != '\n'
316✔
1380
                            oldpos = nextind(input, oldpos)
302✔
1381
                            oldpos >= sizeof(input) && return
151✔
1382
                        end
149✔
1383
                        continue
7✔
1384
                    end
1385
                end
1386
                dump_tail = false
15✔
1387
                nl_pos = findfirst('\n', input[oldpos:end])
30✔
1388
                if s.current_mode == julia_prompt
15✔
1389
                    ast, pos = Meta.parse(input, oldpos, raise=false, depwarn=false)
12✔
1390
                    if (isa(ast, Expr) && (ast.head === :error || ast.head === :incomplete)) ||
22✔
1391
                            (pos > ncodeunits(input) && !endswith(input, '\n'))
1392
                        # remaining text is incomplete (an error, or parser ran to the end but didn't stop with a newline):
1393
                        # Insert all the remaining text as one line (might be empty)
1394
                        dump_tail = true
×
1395
                    end
1396
                elseif isnothing(nl_pos) # no newline at end, so just dump the tail into the prompt and don't execute
6✔
1397
                    dump_tail = true
×
1398
                elseif s.current_mode == shell_mode # handle multiline shell commands
3✔
1399
                    lines = split(input[oldpos:end], '\n')
4✔
1400
                    pos = oldpos + sizeof(lines[1]) + 1
2✔
1401
                    if length(lines) > 1
2✔
1402
                        for line in lines[2:end]
2✔
1403
                            # to be recognized as a multiline shell command, the lines must be indented to the
1404
                            # same prompt position
1405
                            if !startswith(line, ' '^curr_prompt_len)
3✔
1406
                                break
2✔
1407
                            end
1408
                            pos += sizeof(line) + 1
1✔
1409
                        end
1✔
1410
                    end
1411
                else
1412
                    pos = oldpos + nl_pos
1✔
1413
                end
1414
                if dump_tail
15✔
1415
                    tail = input[oldpos:end]
10✔
1416
                    if !firstline
5✔
1417
                        # strip leading whitespace, but only if it was the result of executing something
1418
                        # (avoids modifying the user's current leading wip line)
1419
                        tail = lstrip(tail)
1✔
1420
                    end
1421
                    if isprompt_paste # remove indentation spaces corresponding to the prompt
5✔
1422
                        tail = replace(tail, r"^"m * ' '^curr_prompt_len => "")
7✔
1423
                    end
1424
                    LineEdit.replace_line(s, tail, true)
10✔
1425
                    LineEdit.refresh_line(s)
5✔
1426
                    break
5✔
1427
                end
1428
                # get the line and strip leading and trailing whitespace
1429
                line = strip(input[oldpos:prevind(input, pos)])
20✔
1430
                if !isempty(line)
10✔
1431
                    if isprompt_paste # remove indentation spaces corresponding to the prompt
10✔
1432
                        line = replace(line, r"^"m * ' '^curr_prompt_len => "")
10✔
1433
                    end
1434
                    # put the line on the screen and history
1435
                    LineEdit.replace_line(s, line)
20✔
1436
                    LineEdit.commit_line(s)
10✔
1437
                    # execute the statement
1438
                    terminal = LineEdit.terminal(s) # This is slightly ugly but ok for now
10✔
1439
                    raw!(terminal, false) && disable_bracketed_paste(terminal)
10✔
1440
                    @invokelatest LineEdit.mode(s).on_done(s, LineEdit.buffer(s), true)
10✔
1441
                    raw!(terminal, true) && enable_bracketed_paste(terminal)
10✔
1442
                    LineEdit.push_undo(s) # when the last line is incomplete
10✔
1443
                end
1444
                oldpos = pos
10✔
1445
                firstline = false
×
1446
            end
17✔
1447
        end,
1448

1449
        # Open the editor at the location of a stackframe or method
1450
        # This is accessing a contextual variable that gets set in
1451
        # the show_backtrace and show_method_table functions.
1452
        "^Q" => (s::MIState, o...) -> begin
1453
            linfos = repl.last_shown_line_infos
1454
            str = String(take!(LineEdit.buffer(s)))
1455
            n = tryparse(Int, str)
1456
            n === nothing && @goto writeback
1457
            if n <= 0 || n > length(linfos) || startswith(linfos[n][1], "REPL[")
1458
                @goto writeback
1459
            end
1460
            try
1461
                InteractiveUtils.edit(Base.fixup_stdlib_path(linfos[n][1]), linfos[n][2])
1462
            catch ex
1463
                ex isa ProcessFailedException || ex isa Base.IOError || ex isa SystemError || rethrow()
1464
                @info "edit failed" _exception=ex
1465
            end
1466
            LineEdit.refresh_line(s)
1467
            return
1468
            @label writeback
1469
            write(LineEdit.buffer(s), str)
1470
            return
1471
        end,
1472
    )
1473

1474
    prefix_prompt, prefix_keymap = LineEdit.setup_prefix_keymap(hp, julia_prompt)
25✔
1475

1476
    a = Dict{Any,Any}[skeymap, repl_keymap, prefix_keymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
150✔
1477
    prepend!(a, extra_repl_keymap)
25✔
1478

1479
    julia_prompt.keymap_dict = LineEdit.keymap(a)
25✔
1480

1481
    mk = mode_keymap(julia_prompt)
25✔
1482

1483
    b = Dict{Any,Any}[skeymap, mk, prefix_keymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
150✔
1484
    prepend!(b, extra_repl_keymap)
25✔
1485

1486
    shell_mode.keymap_dict = help_mode.keymap_dict = dummy_pkg_mode.keymap_dict = LineEdit.keymap(b)
25✔
1487

1488
    allprompts = LineEdit.TextInterface[julia_prompt, shell_mode, help_mode, dummy_pkg_mode, search_prompt, prefix_prompt]
25✔
1489
    return ModalInterface(allprompts)
25✔
1490
end
1491

1492
function run_frontend(repl::LineEditREPL, backend::REPLBackendRef)
21✔
1493
    repl.frontend_task = current_task()
21✔
1494
    d = REPLDisplay(repl)
21✔
1495
    dopushdisplay = repl.specialdisplay === nothing && !in(d,Base.Multimedia.displays)
36✔
1496
    dopushdisplay && pushdisplay(d)
21✔
1497
    if !isdefined(repl,:interface)
21✔
1498
        interface = repl.interface = setup_interface(repl)
26✔
1499
    else
1500
        interface = repl.interface
8✔
1501
    end
1502
    repl.backendref = backend
21✔
1503
    repl.mistate = LineEdit.init_state(terminal(repl), interface)
21✔
1504
    run_interface(terminal(repl), interface, repl.mistate)
21✔
1505
    # Terminate Backend
1506
    put!(backend.repl_channel, (nothing, -1))
21✔
1507
    dopushdisplay && popdisplay(d)
21✔
1508
    nothing
1509
end
1510

1511
## StreamREPL ##
1512

1513
mutable struct StreamREPL <: AbstractREPL
1514
    stream::IO
1515
    prompt_color::String
1516
    input_color::String
1517
    answer_color::String
1518
    waserror::Bool
1519
    frontend_task::Task
1520
    StreamREPL(stream,pc,ic,ac) = new(stream,pc,ic,ac,false)
×
1521
end
1522
StreamREPL(stream::IO) = StreamREPL(stream, Base.text_colors[:green], Base.input_color(), Base.answer_color())
×
1523
run_repl(stream::IO) = run_repl(StreamREPL(stream))
×
1524

1525
outstream(s::StreamREPL) = s.stream
×
1526
hascolor(s::StreamREPL) = get(s.stream, :color, false)::Bool
×
1527

1528
answer_color(r::LineEditREPL) = r.envcolors ? Base.answer_color() : r.answer_color
×
1529
answer_color(r::StreamREPL) = r.answer_color
×
1530
input_color(r::LineEditREPL) = r.envcolors ? Base.input_color() : r.input_color
×
1531
input_color(r::StreamREPL) = r.input_color
×
1532

1533
let matchend = Dict("\"" => r"\"", "\"\"\"" => r"\"\"\"", "'" => r"'",
1534
    "`" => r"`", "```" => r"```", "#" => r"$"m, "#=" => r"=#|#=")
1535
    global _rm_strings_and_comments
1536
    function _rm_strings_and_comments(code::Union{String,SubString{String}})
126✔
1537
        buf = IOBuffer(sizehint = sizeof(code))
252✔
1538
        pos = 1
×
1539
        while true
166✔
1540
            i = findnext(r"\"(?!\"\")|\"\"\"|'|`(?!``)|```|#(?!=)|#=", code, pos)
332✔
1541
            isnothing(i) && break
212✔
1542
            match = SubString(code, i)
46✔
1543
            j = findnext(matchend[match]::Regex, code, nextind(code, last(i)))
92✔
1544
            if match == "#=" # possibly nested
46✔
1545
                nested = 1
×
1546
                while j !== nothing
11✔
1547
                    nested += SubString(code, j) == "#=" ? +1 : -1
10✔
1548
                    iszero(nested) && break
10✔
1549
                    j = findnext(r"=#|#=", code, nextind(code, last(j)))
12✔
1550
                end
6✔
1551
            elseif match[1] != '#' # quote match: check non-escaped
82✔
1552
                while j !== nothing
38✔
1553
                    notbackslash = findprev(!=('\\'), code, prevind(code, first(j)))::Int
66✔
1554
                    isodd(first(j) - notbackslash) && break # not escaped
33✔
1555
                    j = findnext(matchend[match]::Regex, code, nextind(code, first(j)))
14✔
1556
                end
7✔
1557
            end
1558
            isnothing(j) && break
86✔
1559
            if match[1] == '#'
80✔
1560
                print(buf, SubString(code, pos, prevind(code, first(i))))
14✔
1561
            else
1562
                print(buf, SubString(code, pos, last(i)), ' ', SubString(code, j))
26✔
1563
            end
1564
            pos = nextind(code, last(j))
40✔
1565
        end
40✔
1566
        print(buf, SubString(code, pos, lastindex(code)))
126✔
1567
        return String(take!(buf))
126✔
1568
    end
1569
end
1570

1571
# heuristic function to decide if the presence of a semicolon
1572
# at the end of the expression was intended for suppressing output
1573
ends_with_semicolon(code::AbstractString) = ends_with_semicolon(String(code))
×
1574
ends_with_semicolon(code::Union{String,SubString{String}}) =
126✔
1575
    contains(_rm_strings_and_comments(code), r";\s*$")
1576

1577
function banner(io::IO = stdout; short = false)
4✔
1578
    if Base.GIT_VERSION_INFO.tagged_commit
×
1579
        commit_string = Base.TAGGED_RELEASE_BANNER
×
1580
    elseif isempty(Base.GIT_VERSION_INFO.commit)
×
1581
        commit_string = ""
×
1582
    else
1583
        days = Int(floor((ccall(:jl_clock_now, Float64, ()) - Base.GIT_VERSION_INFO.fork_master_timestamp) / (60 * 60 * 24)))
2✔
1584
        days = max(0, days)
2✔
1585
        unit = days == 1 ? "day" : "days"
2✔
1586
        distance = Base.GIT_VERSION_INFO.fork_master_distance
×
1587
        commit = Base.GIT_VERSION_INFO.commit_short
2✔
1588

1589
        if distance == 0
×
1590
            commit_string = "Commit $(commit) ($(days) $(unit) old master)"
2✔
1591
        else
1592
            branch = Base.GIT_VERSION_INFO.branch
×
1593
            commit_string = "$(branch)/$(commit) (fork: $(distance) commits, $(days) $(unit))"
×
1594
        end
1595
    end
1596

1597
    commit_date = isempty(Base.GIT_VERSION_INFO.date_string) ? "" : " ($(split(Base.GIT_VERSION_INFO.date_string)[1]))"
×
1598

1599
    if get(io, :color, false)::Bool
2✔
1600
        c = Base.text_colors
×
1601
        tx = c[:normal] # text
×
1602
        jl = c[:normal] # julia
×
1603
        d1 = c[:bold] * c[:blue]    # first dot
×
1604
        d2 = c[:bold] * c[:red]     # second dot
×
1605
        d3 = c[:bold] * c[:green]   # third dot
×
1606
        d4 = c[:bold] * c[:magenta] # fourth dot
×
1607

1608
        if short
×
1609
            print(io,"""
×
1610
              $(d3)o$(tx)  | Version $(VERSION)$(commit_date)
1611
             $(d2)o$(tx) $(d4)o$(tx) | $(commit_string)
1612
            """)
1613
        else
1614
            print(io,"""               $(d3)_$(tx)
×
1615
               $(d1)_$(tx)       $(jl)_$(tx) $(d2)_$(d3)(_)$(d4)_$(tx)     |  Documentation: https://docs.julialang.org
1616
              $(d1)(_)$(jl)     | $(d2)(_)$(tx) $(d4)(_)$(tx)    |
1617
               $(jl)_ _   _| |_  __ _$(tx)   |  Type \"?\" for help, \"]?\" for Pkg help.
1618
              $(jl)| | | | | | |/ _` |$(tx)  |
1619
              $(jl)| | |_| | | | (_| |$(tx)  |  Version $(VERSION)$(commit_date)
1620
             $(jl)_/ |\\__'_|_|_|\\__'_|$(tx)  |  $(commit_string)
1621
            $(jl)|__/$(tx)                   |
1622

1623
            """)
1624
        end
1625
    else
1626
        if short
2✔
1627
            print(io,"""
1✔
1628
              o  |  Version $(VERSION)$(commit_date)
1629
             o o |  $(commit_string)
1630
            """)
1631
        else
1632
            print(io,"""
1✔
1633
                           _
1634
               _       _ _(_)_     |  Documentation: https://docs.julialang.org
1635
              (_)     | (_) (_)    |
1636
               _ _   _| |_  __ _   |  Type \"?\" for help, \"]?\" for Pkg help.
1637
              | | | | | | |/ _` |  |
1638
              | | |_| | | | (_| |  |  Version $(VERSION)$(commit_date)
1639
             _/ |\\__'_|_|_|\\__'_|  |  $(commit_string)
1640
            |__/                   |
1641

1642
            """)
1643
        end
1644
    end
1645
end
1646

1647
function run_frontend(repl::StreamREPL, backend::REPLBackendRef)
×
1648
    repl.frontend_task = current_task()
×
1649
    have_color = hascolor(repl)
×
1650
    banner(repl.stream)
×
1651
    d = REPLDisplay(repl)
×
1652
    dopushdisplay = !in(d,Base.Multimedia.displays)
×
1653
    dopushdisplay && pushdisplay(d)
×
1654
    while !eof(repl.stream)::Bool
×
1655
        if have_color
×
1656
            print(repl.stream,repl.prompt_color)
×
1657
        end
1658
        print(repl.stream, "julia> ")
×
1659
        if have_color
×
1660
            print(repl.stream, input_color(repl))
×
1661
        end
1662
        line = readline(repl.stream, keep=true)
×
1663
        if !isempty(line)
×
1664
            ast = Base.parse_input_line(line)
×
1665
            if have_color
×
1666
                print(repl.stream, Base.color_normal)
×
1667
            end
1668
            response = eval_with_backend(ast, backend)
×
1669
            print_response(repl, response, !ends_with_semicolon(line), have_color)
×
1670
        end
1671
    end
×
1672
    # Terminate Backend
1673
    put!(backend.repl_channel, (nothing, -1))
×
1674
    dopushdisplay && popdisplay(d)
×
1675
    nothing
×
1676
end
1677

1678
module Numbered
1679

1680
using ..REPL
1681

1682
__current_ast_transforms() = isdefined(Base, :active_repl_backend) ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
×
1683

1684
function repl_eval_counter(hp)
773✔
1685
    return length(hp.history) - hp.start_idx
773✔
1686
end
1687

1688
function out_transform(@nospecialize(x), n::Ref{Int})
16✔
1689
    return Expr(:toplevel, get_usings!([], x)..., quote
16✔
1690
        let __temp_val_a72df459 = $x
1691
            $capture_result($n, __temp_val_a72df459)
1692
            __temp_val_a72df459
1693
        end
1694
    end)
1695
end
1696

1697
function get_usings!(usings, ex)
25✔
1698
    ex isa Expr || return usings
25✔
1699
    # get all `using` and `import` statements which are at the top level
1700
    for (i, arg) in enumerate(ex.args)
25✔
1701
        if Base.isexpr(arg, :toplevel)
48✔
1702
            get_usings!(usings, arg)
9✔
1703
        elseif Base.isexpr(arg, [:using, :import])
64✔
1704
            push!(usings, popat!(ex.args, i))
2✔
1705
        end
1706
    end
71✔
1707
    return usings
25✔
1708
end
1709

1710
function capture_result(n::Ref{Int}, @nospecialize(x))
16✔
1711
    n = n[]
16✔
1712
    mod = Base.MainInclude
16✔
1713
    if !isdefined(mod, :Out)
16✔
1714
        @eval mod global Out
1✔
1715
        @eval mod export Out
1✔
1716
        setglobal!(mod, :Out, Dict{Int, Any}())
1✔
1717
    end
1718
    if x !== getglobal(mod, :Out) && x !== nothing # remove this?
16✔
1719
        getglobal(mod, :Out)[n] = x
14✔
1720
    end
1721
    nothing
16✔
1722
end
1723

1724
function set_prompt(repl::LineEditREPL, n::Ref{Int})
1✔
1725
    julia_prompt = repl.interface.modes[1]
1✔
1726
    julia_prompt.prompt = function()
774✔
1727
        n[] = repl_eval_counter(julia_prompt.hist)+1
773✔
1728
        string("In [", n[], "]: ")
773✔
1729
    end
1730
    nothing
1✔
1731
end
1732

1733
function set_output_prefix(repl::LineEditREPL, n::Ref{Int})
1✔
1734
    julia_prompt = repl.interface.modes[1]
1✔
1735
    if REPL.hascolor(repl)
1✔
1736
        julia_prompt.output_prefix_prefix = Base.text_colors[:red]
1✔
1737
    end
1738
    julia_prompt.output_prefix = () -> string("Out[", n[], "]: ")
15✔
1739
    nothing
1✔
1740
end
1741

1742
function __current_ast_transforms(backend)
1743
    if backend === nothing
1✔
1744
        isdefined(Base, :active_repl_backend) ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
×
1745
    else
1746
        backend.ast_transforms
1✔
1747
    end
1748
end
1749

1750
function numbered_prompt!(repl::LineEditREPL=Base.active_repl, backend=nothing)
1751
    n = Ref{Int}(0)
1✔
1752
    set_prompt(repl, n)
1✔
1753
    set_output_prefix(repl, n)
1✔
1754
    push!(__current_ast_transforms(backend), @nospecialize(ast) -> out_transform(ast, n))
17✔
1755
    return
1✔
1756
end
1757

1758
"""
1759
    Out[n]
1760

1761
A variable referring to all previously computed values, automatically imported to the interactive prompt.
1762
Only defined and exists while using [Numbered prompt](@ref Numbered-prompt).
1763

1764
See also [`ans`](@ref).
1765
"""
1766
Base.MainInclude.Out
1767

1768
end
1769

1770
import .Numbered.numbered_prompt!
1771

1772
# this assignment won't survive precompilation,
1773
# but will stick if REPL is baked into a sysimg.
1774
# Needs to occur after this module is finished.
1775
Base.REPL_MODULE_REF[] = REPL
1776

1777
if Base.generating_output()
1778
    include("precompile.jl")
1779
end
1780

1781
end # module
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc