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

JuliaLang / julia / #37618

12 Sep 2023 02:14AM UTC coverage: 85.083% (-0.03%) from 85.114%
#37618

push

local

web-flow
add tfuncs for `[and|or]_int` intrinsics (#51266)

So that they can be constant folded when either of argument is known to
be `Core([false|true])`. It may help inference accuracy.

16 of 16 new or added lines in 1 file covered. (100.0%)

71222 of 83709 relevant lines covered (85.08%)

13006801.97 hits per line

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

0.25
/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
using Base.Meta, Sockets
21
import InteractiveUtils
22

23
export
24
    AbstractREPL,
25
    BasicREPL,
26
    LineEditREPL,
27
    StreamREPL
28

29
import Base:
30
    AbstractDisplay,
31
    display,
32
    show,
33
    AnyDict,
34
    ==
35

36
_displaysize(io::IO) = displaysize(io)::Tuple{Int,Int}
×
37

38
include("Terminals.jl")
39
using .Terminals
40

41
abstract type AbstractREPL end
42

43
include("options.jl")
44

45
include("LineEdit.jl")
46
using .LineEdit
47
import ..LineEdit:
48
    CompletionProvider,
49
    HistoryProvider,
50
    add_history,
51
    complete_line,
52
    history_next,
53
    history_next_prefix,
54
    history_prev,
55
    history_prev_prefix,
56
    history_first,
57
    history_last,
58
    history_search,
59
    accept_result,
60
    setmodifiers!,
61
    terminal,
62
    MIState,
63
    PromptState,
64
    TextInterface,
65
    mode_idx
66

67
include("REPLCompletions.jl")
68
using .REPLCompletions
69

70
include("TerminalMenus/TerminalMenus.jl")
71
include("docview.jl")
72

73
@nospecialize # use only declared type signatures
74

75
answer_color(::AbstractREPL) = ""
×
76

77
const JULIA_PROMPT = "julia> "
78
const PKG_PROMPT = "pkg> "
79
const SHELL_PROMPT = "shell> "
80
const HELP_PROMPT = "help?> "
81

82
mutable struct REPLBackend
83
    "channel for AST"
84
    repl_channel::Channel{Any}
85
    "channel for results: (value, iserror)"
86
    response_channel::Channel{Any}
87
    "flag indicating the state of this backend"
88
    in_eval::Bool
89
    "transformation functions to apply before evaluating expressions"
90
    ast_transforms::Vector{Any}
91
    "current backend task"
92
    backend_task::Task
93

94
    REPLBackend(repl_channel, response_channel, in_eval, ast_transforms=copy(repl_ast_transforms)) =
×
95
        new(repl_channel, response_channel, in_eval, ast_transforms)
96
end
97
REPLBackend() = REPLBackend(Channel(1), Channel(1), false)
×
98

99
"""
100
    softscope(ex)
101

102
Return a modified version of the parsed expression `ex` that uses
103
the REPL's "soft" scoping rules for global syntax blocks.
104
"""
105
function softscope(@nospecialize ex)
×
106
    if ex isa Expr
×
107
        h = ex.head
×
108
        if h === :toplevel
×
109
            ex′ = Expr(h)
×
110
            map!(softscope, resize!(ex′.args, length(ex.args)), ex.args)
×
111
            return ex′
×
112
        elseif h in (:meta, :import, :using, :export, :module, :error, :incomplete, :thunk)
×
113
            return ex
×
114
        elseif h === :global && all(x->isa(x, Symbol), ex.args)
×
115
            return ex
×
116
        else
117
            return Expr(:block, Expr(:softscope, true), ex)
×
118
        end
119
    end
120
    return ex
×
121
end
122

123
# Temporary alias until Documenter updates
124
const softscope! = softscope
125

126
const repl_ast_transforms = Any[softscope] # defaults for new REPL backends
127

128
# Allows an external package to add hooks into the code loading.
129
# The hook should take a Vector{Symbol} of package names and
130
# return true if all packages could be installed, false if not
131
# to e.g. install packages on demand
132
const install_packages_hooks = Any[]
133

134
function eval_user_input(@nospecialize(ast), backend::REPLBackend, mod::Module)
×
135
    lasterr = nothing
×
136
    Base.sigatomic_begin()
×
137
    while true
×
138
        try
×
139
            Base.sigatomic_end()
×
140
            if lasterr !== nothing
×
141
                put!(backend.response_channel, Pair{Any, Bool}(lasterr, true))
×
142
            else
143
                backend.in_eval = true
×
144
                if !isempty(install_packages_hooks)
×
145
                    check_for_missing_packages_and_run_hooks(ast)
×
146
                end
147
                for xf in backend.ast_transforms
×
148
                    ast = Base.invokelatest(xf, ast)
×
149
                end
×
150
                value = Core.eval(mod, ast)
×
151
                backend.in_eval = false
×
152
                setglobal!(Base.MainInclude, :ans, value)
×
153
                put!(backend.response_channel, Pair{Any, Bool}(value, false))
×
154
            end
155
            break
×
156
        catch err
157
            if lasterr !== nothing
×
158
                println("SYSTEM ERROR: Failed to report error to REPL frontend")
×
159
                println(err)
×
160
            end
161
            lasterr = current_exceptions()
×
162
        end
163
    end
×
164
    Base.sigatomic_end()
×
165
    nothing
×
166
end
167

168
function check_for_missing_packages_and_run_hooks(ast)
×
169
    isa(ast, Expr) || return
×
170
    mods = modules_to_be_loaded(ast)
×
171
    filter!(mod -> isnothing(Base.identify_package(String(mod))), mods) # keep missing modules
×
172
    if !isempty(mods)
×
173
        for f in install_packages_hooks
×
174
            Base.invokelatest(f, mods) && return
×
175
        end
×
176
    end
177
end
178

179
function modules_to_be_loaded(ast::Expr, mods::Vector{Symbol} = Symbol[])
×
180
    ast.head === :quote && return mods # don't search if it's not going to be run during this eval
×
181
    if ast.head === :using || ast.head === :import
×
182
        for arg in ast.args
×
183
            arg = arg::Expr
×
184
            arg1 = first(arg.args)
×
185
            if arg1 isa Symbol # i.e. `Foo`
×
186
                if arg1 != :. # don't include local imports
×
187
                    push!(mods, arg1)
×
188
                end
189
            else # i.e. `Foo: bar`
190
                push!(mods, first((arg1::Expr).args))
×
191
            end
192
        end
×
193
    end
194
    for arg in ast.args
×
195
        if isexpr(arg, (:block, :if, :using, :import))
×
196
            modules_to_be_loaded(arg, mods)
×
197
        end
198
    end
×
199
    filter!(mod -> !in(String(mod), ["Base", "Main", "Core"]), mods) # Exclude special non-package modules
×
200
    return unique(mods)
×
201
end
202

203
"""
204
    start_repl_backend(repl_channel::Channel, response_channel::Channel)
205

206
    Starts loop for REPL backend
207
    Returns a REPLBackend with backend_task assigned
208

209
    Deprecated since sync / async behavior cannot be selected
210
"""
211
function start_repl_backend(repl_channel::Channel{Any}, response_channel::Channel{Any}
×
212
                            ; get_module::Function = ()->Main)
213
    # Maintain legacy behavior of asynchronous backend
214
    backend = REPLBackend(repl_channel, response_channel, false)
×
215
    # Assignment will be made twice, but will be immediately available
216
    backend.backend_task = @async start_repl_backend(backend; get_module)
×
217
    return backend
×
218
end
219

220
"""
221
    start_repl_backend(backend::REPLBackend)
222

223
    Call directly to run backend loop on current Task.
224
    Use @async for run backend on new Task.
225

226
    Does not return backend until loop is finished.
227
"""
228
function start_repl_backend(backend::REPLBackend,  @nospecialize(consumer = x -> nothing); get_module::Function = ()->Main)
×
229
    backend.backend_task = Base.current_task()
×
230
    consumer(backend)
×
231
    repl_backend_loop(backend, get_module)
×
232
    return backend
×
233
end
234

235
function repl_backend_loop(backend::REPLBackend, get_module::Function)
×
236
    # include looks at this to determine the relative include path
237
    # nothing means cwd
238
    while true
×
239
        tls = task_local_storage()
×
240
        tls[:SOURCE_PATH] = nothing
×
241
        ast, show_value = take!(backend.repl_channel)
×
242
        if show_value == -1
×
243
            # exit flag
244
            break
×
245
        end
246
        eval_user_input(ast, backend, get_module())
×
247
    end
×
248
    return nothing
×
249
end
250

251
struct REPLDisplay{Repl<:AbstractREPL} <: AbstractDisplay
252
    repl::Repl
253
end
254

255
function display(d::REPLDisplay, mime::MIME"text/plain", x)
×
256
    x = Ref{Any}(x)
×
257
    with_repl_linfo(d.repl) do io
×
258
        io = IOContext(io, :limit => true, :module => active_module(d)::Module)
259
        if d.repl isa LineEditREPL
260
            mistate = d.repl.mistate
261
            mode = LineEdit.mode(mistate)
262
            if mode isa LineEdit.Prompt
263
                LineEdit.write_output_prefix(io, mode, get(io, :color, false)::Bool)
264
            end
265
        end
266
        get(io, :color, false)::Bool && write(io, answer_color(d.repl))
267
        if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext)
268
            # this can override the :limit property set initially
269
            io = foldl(IOContext, d.repl.options.iocontext, init=io)
270
        end
271
        show(io, mime, x[])
272
        println(io)
273
    end
274
    return nothing
×
275
end
276
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)
×
277

278
function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool)
×
279
    repl.waserror = response[2]
×
280
    with_repl_linfo(repl) do io
×
281
        io = IOContext(io, :module => active_module(repl)::Module)
×
282
        print_response(io, response, show_value, have_color, specialdisplay(repl))
×
283
    end
284
    return nothing
×
285
end
286

287
function repl_display_error(errio::IO, @nospecialize errval)
×
288
    # this will be set to true if types in the stacktrace are truncated
289
    limitflag = Ref(false)
×
290
    errio = IOContext(errio, :stacktrace_types_limited => limitflag)
×
291
    Base.invokelatest(Base.display_error, errio, errval)
×
292
    if limitflag[]
×
293
        print(errio, "Some type information was truncated. Use `show(err)` to see complete types.")
×
294
        println(errio)
×
295
    end
296
    return nothing
×
297
end
298

299
function print_response(errio::IO, response, show_value::Bool, have_color::Bool, specialdisplay::Union{AbstractDisplay,Nothing}=nothing)
×
300
    Base.sigatomic_begin()
×
301
    val, iserr = response
×
302
    while true
×
303
        try
×
304
            Base.sigatomic_end()
×
305
            if iserr
×
306
                val = Base.scrub_repl_backtrace(val)
×
307
                Base.istrivialerror(val) || setglobal!(Base.MainInclude, :err, val)
×
308
                repl_display_error(errio, val)
×
309
            else
310
                if val !== nothing && show_value
×
311
                    try
×
312
                        if specialdisplay === nothing
×
313
                            Base.invokelatest(display, val)
×
314
                        else
315
                            Base.invokelatest(display, specialdisplay, val)
×
316
                        end
317
                    catch
318
                        println(errio, "Error showing value of type ", typeof(val), ":")
×
319
                        rethrow()
×
320
                    end
321
                end
322
            end
323
            break
×
324
        catch ex
325
            if iserr
×
326
                println(errio) # an error during printing is likely to leave us mid-line
×
327
                println(errio, "SYSTEM (REPL): showing an error caused an error")
×
328
                try
×
329
                    excs = Base.scrub_repl_backtrace(current_exceptions())
×
330
                    setglobal!(Base.MainInclude, :err, excs)
×
331
                    repl_display_error(errio, excs)
×
332
                catch e
333
                    # at this point, only print the name of the type as a Symbol to
334
                    # minimize the possibility of further errors.
335
                    println(errio)
×
336
                    println(errio, "SYSTEM (REPL): caught exception of type ", typeof(e).name.name,
×
337
                            " while trying to handle a nested exception; giving up")
338
                end
339
                break
×
340
            end
341
            val = current_exceptions()
×
342
            iserr = true
×
343
        end
344
    end
×
345
    Base.sigatomic_end()
×
346
    nothing
×
347
end
348

349
# A reference to a backend that is not mutable
350
struct REPLBackendRef
351
    repl_channel::Channel{Any}
352
    response_channel::Channel{Any}
353
end
354
REPLBackendRef(backend::REPLBackend) = REPLBackendRef(backend.repl_channel, backend.response_channel)
×
355

356
function destroy(ref::REPLBackendRef, state::Task)
×
357
    if istaskfailed(state)
×
358
        close(ref.repl_channel, TaskFailedException(state))
×
359
        close(ref.response_channel, TaskFailedException(state))
×
360
    end
361
    close(ref.repl_channel)
×
362
    close(ref.response_channel)
×
363
end
364

365
"""
366
    run_repl(repl::AbstractREPL)
367
    run_repl(repl, consumer = backend->nothing; backend_on_current_task = true)
368

369
    Main function to start the REPL
370

371
    consumer is an optional function that takes a REPLBackend as an argument
372
"""
373
function run_repl(repl::AbstractREPL, @nospecialize(consumer = x -> nothing); backend_on_current_task::Bool = true, backend = REPLBackend())
×
374
    backend_ref = REPLBackendRef(backend)
×
375
    cleanup = @task try
×
376
            destroy(backend_ref, t)
×
377
        catch e
378
            Core.print(Core.stderr, "\nINTERNAL ERROR: ")
×
379
            Core.println(Core.stderr, e)
×
380
            Core.println(Core.stderr, catch_backtrace())
×
381
        end
382
    get_module = () -> active_module(repl)
×
383
    if backend_on_current_task
×
384
        t = @async run_frontend(repl, backend_ref)
×
385
        errormonitor(t)
×
386
        Base._wait2(t, cleanup)
×
387
        start_repl_backend(backend, consumer; get_module)
×
388
    else
389
        t = @async start_repl_backend(backend, consumer; get_module)
×
390
        errormonitor(t)
×
391
        Base._wait2(t, cleanup)
×
392
        run_frontend(repl, backend_ref)
×
393
    end
394
    return backend
×
395
end
396

397
## BasicREPL ##
398

399
mutable struct BasicREPL <: AbstractREPL
400
    terminal::TextTerminal
401
    waserror::Bool
402
    frontend_task::Task
403
    BasicREPL(t) = new(t, false)
×
404
end
405

406
outstream(r::BasicREPL) = r.terminal
×
407
hascolor(r::BasicREPL) = hascolor(r.terminal)
×
408

409
function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
×
410
    repl.frontend_task = current_task()
×
411
    d = REPLDisplay(repl)
×
412
    dopushdisplay = !in(d,Base.Multimedia.displays)
×
413
    dopushdisplay && pushdisplay(d)
×
414
    hit_eof = false
×
415
    while true
×
416
        Base.reseteof(repl.terminal)
×
417
        write(repl.terminal, JULIA_PROMPT)
×
418
        line = ""
×
419
        ast = nothing
×
420
        interrupted = false
×
421
        while true
×
422
            try
×
423
                line *= readline(repl.terminal, keep=true)
×
424
            catch e
425
                if isa(e,InterruptException)
×
426
                    try # raise the debugger if present
×
427
                        ccall(:jl_raise_debugger, Int, ())
×
428
                    catch
×
429
                    end
430
                    line = ""
×
431
                    interrupted = true
×
432
                    break
×
433
                elseif isa(e,EOFError)
×
434
                    hit_eof = true
×
435
                    break
×
436
                else
437
                    rethrow()
×
438
                end
439
            end
440
            ast = Base.parse_input_line(line)
×
441
            (isa(ast,Expr) && ast.head === :incomplete) || break
×
442
        end
×
443
        if !isempty(line)
×
444
            response = eval_with_backend(ast, backend)
×
445
            print_response(repl, response, !ends_with_semicolon(line), false)
×
446
        end
447
        write(repl.terminal, '\n')
×
448
        ((!interrupted && isempty(line)) || hit_eof) && break
×
449
    end
×
450
    # terminate backend
451
    put!(backend.repl_channel, (nothing, -1))
×
452
    dopushdisplay && popdisplay(d)
×
453
    nothing
×
454
end
455

456
## LineEditREPL ##
457

458
mutable struct LineEditREPL <: AbstractREPL
459
    t::TextTerminal
460
    hascolor::Bool
461
    prompt_color::String
462
    input_color::String
463
    answer_color::String
464
    shell_color::String
465
    help_color::String
466
    history_file::Bool
467
    in_shell::Bool
468
    in_help::Bool
469
    envcolors::Bool
470
    waserror::Bool
471
    specialdisplay::Union{Nothing,AbstractDisplay}
472
    options::Options
473
    mistate::Union{MIState,Nothing}
474
    last_shown_line_infos::Vector{Tuple{String,Int}}
475
    interface::ModalInterface
476
    backendref::REPLBackendRef
477
    frontend_task::Task
478
    function LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors)
×
479
        opts = Options()
×
480
        opts.hascolor = hascolor
×
481
        if !hascolor
×
482
            opts.beep_colors = [""]
×
483
        end
484
        new(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
×
485
            in_help,envcolors,false,nothing, opts, nothing, Tuple{String,Int}[])
486
    end
487
end
488
outstream(r::LineEditREPL) = (t = r.t; t isa TTYTerminal ? t.out_stream : t)
×
489
specialdisplay(r::LineEditREPL) = r.specialdisplay
×
490
specialdisplay(r::AbstractREPL) = nothing
×
491
terminal(r::LineEditREPL) = r.t
×
492
hascolor(r::LineEditREPL) = r.hascolor
×
493

494
LineEditREPL(t::TextTerminal, hascolor::Bool, envcolors::Bool=false) =
×
495
    LineEditREPL(t, hascolor,
496
        hascolor ? Base.text_colors[:green] : "",
497
        hascolor ? Base.input_color() : "",
498
        hascolor ? Base.answer_color() : "",
499
        hascolor ? Base.text_colors[:red] : "",
500
        hascolor ? Base.text_colors[:yellow] : "",
501
        false, false, false, envcolors
502
    )
503

504
mutable struct REPLCompletionProvider <: CompletionProvider
505
    modifiers::LineEdit.Modifiers
506
end
507
REPLCompletionProvider() = REPLCompletionProvider(LineEdit.Modifiers())
×
508

509
mutable struct ShellCompletionProvider <: CompletionProvider end
510
struct LatexCompletions <: CompletionProvider end
511

512
function active_module() # this method is also called from Base
62,481✔
513
    isdefined(Base, :active_repl) || return Main
124,962✔
514
    return active_module(Base.active_repl::AbstractREPL)
×
515
end
516
active_module((; mistate)::LineEditREPL) = mistate === nothing ? Main : mistate.active_module
×
517
active_module(::AbstractREPL) = Main
×
518
active_module(d::REPLDisplay) = active_module(d.repl)
×
519

520
setmodifiers!(c::CompletionProvider, m::LineEdit.Modifiers) = nothing
×
521

522
setmodifiers!(c::REPLCompletionProvider, m::LineEdit.Modifiers) = c.modifiers = m
×
523

524
"""
525
    activate(mod::Module=Main)
526

527
Set `mod` as the default contextual module in the REPL,
528
both for evaluating expressions and printing them.
529
"""
530
function activate(mod::Module=Main)
×
531
    mistate = (Base.active_repl::LineEditREPL).mistate
×
532
    mistate === nothing && return nothing
×
533
    mistate.active_module = mod
×
534
    Base.load_InteractiveUtils(mod)
×
535
    return nothing
×
536
end
537

538
beforecursor(buf::IOBuffer) = String(buf.data[1:buf.ptr-1])
×
539

540
function complete_line(c::REPLCompletionProvider, s::PromptState, mod::Module)
×
541
    partial = beforecursor(s.input_buffer)
×
542
    full = LineEdit.input_string(s)
×
543
    ret, range, should_complete = completions(full, lastindex(partial), mod, c.modifiers.shift)
×
544
    c.modifiers = LineEdit.Modifiers()
×
545
    return unique!(map(completion_text, ret)), partial[range], should_complete
×
546
end
547

548
function complete_line(c::ShellCompletionProvider, s::PromptState)
×
549
    # First parse everything up to the current position
550
    partial = beforecursor(s.input_buffer)
×
551
    full = LineEdit.input_string(s)
×
552
    ret, range, should_complete = shell_completions(full, lastindex(partial))
×
553
    return unique!(map(completion_text, ret)), partial[range], should_complete
×
554
end
555

556
function complete_line(c::LatexCompletions, s)
×
557
    partial = beforecursor(LineEdit.buffer(s))
×
558
    full = LineEdit.input_string(s)::String
×
559
    ret, range, should_complete = bslash_completions(full, lastindex(partial))[2]
×
560
    return unique!(map(completion_text, ret)), partial[range], should_complete
×
561
end
562

563
with_repl_linfo(f, repl) = f(outstream(repl))
×
564
function with_repl_linfo(f, repl::LineEditREPL)
×
565
    linfos = Tuple{String,Int}[]
×
566
    io = IOContext(outstream(repl), :last_shown_line_infos => linfos)
×
567
    f(io)
×
568
    if !isempty(linfos)
×
569
        repl.last_shown_line_infos = linfos
×
570
    end
571
    nothing
×
572
end
573

574
mutable struct REPLHistoryProvider <: HistoryProvider
575
    history::Vector{String}
576
    file_path::String
577
    history_file::Union{Nothing,IO}
578
    start_idx::Int
579
    cur_idx::Int
580
    last_idx::Int
581
    last_buffer::IOBuffer
582
    last_mode::Union{Nothing,Prompt}
583
    mode_mapping::Dict{Symbol,Prompt}
584
    modes::Vector{Symbol}
585
end
586
REPLHistoryProvider(mode_mapping::Dict{Symbol}) =
×
587
    REPLHistoryProvider(String[], "", nothing, 0, 0, -1, IOBuffer(),
588
                        nothing, mode_mapping, UInt8[])
589

590
invalid_history_message(path::String) = """
×
591
Invalid history file ($path) format:
592
If you have a history file left over from an older version of Julia,
593
try renaming or deleting it.
594
Invalid character: """
595

596
munged_history_message(path::String) = """
×
597
Invalid history file ($path) format:
598
An editor may have converted tabs to spaces at line """
599

600
function hist_open_file(hp::REPLHistoryProvider)
×
601
    f = open(hp.file_path, read=true, write=true, create=true)
×
602
    hp.history_file = f
×
603
    seekend(f)
×
604
end
605

606
function hist_from_file(hp::REPLHistoryProvider, path::String)
×
607
    getline(lines, i) = i > length(lines) ? "" : lines[i]
×
608
    file_lines = readlines(path)
×
609
    countlines = 0
×
610
    while true
×
611
        # First parse the metadata that starts with '#' in particular the REPL mode
612
        countlines += 1
×
613
        line = getline(file_lines, countlines)
×
614
        mode = :julia
×
615
        isempty(line) && break
×
616
        line[1] != '#' &&
×
617
            error(invalid_history_message(path), repr(line[1]), " at line ", countlines)
618
        while !isempty(line)
×
619
            startswith(line, '#') || break
×
620
            if startswith(line, "# mode: ")
×
621
                mode = Symbol(SubString(line, 9))
×
622
            end
623
            countlines += 1
×
624
            line = getline(file_lines, countlines)
×
625
        end
×
626
        isempty(line) && break
×
627

628
        # Now parse the code for the current REPL mode
629
        line[1] == ' '  &&
×
630
            error(munged_history_message(path), countlines)
631
        line[1] != '\t' &&
×
632
            error(invalid_history_message(path), repr(line[1]), " at line ", countlines)
633
        lines = String[]
×
634
        while !isempty(line)
×
635
            push!(lines, chomp(SubString(line, 2)))
×
636
            next_line = getline(file_lines, countlines+1)
×
637
            isempty(next_line) && break
×
638
            first(next_line) == ' '  && error(munged_history_message(path), countlines)
×
639
            # A line not starting with a tab means we are done with code for this entry
640
            first(next_line) != '\t' && break
×
641
            countlines += 1
×
642
            line = getline(file_lines, countlines)
×
643
        end
×
644
        push!(hp.modes, mode)
×
645
        push!(hp.history, join(lines, '\n'))
×
646
    end
×
647
    hp.start_idx = length(hp.history)
×
648
    return hp
×
649
end
650

651
function add_history(hist::REPLHistoryProvider, s::PromptState)
×
652
    str = rstrip(String(take!(copy(s.input_buffer))))
×
653
    isempty(strip(str)) && return
×
654
    mode = mode_idx(hist, LineEdit.mode(s))
×
655
    !isempty(hist.history) &&
×
656
        isequal(mode, hist.modes[end]) && str == hist.history[end] && return
657
    push!(hist.modes, mode)
×
658
    push!(hist.history, str)
×
659
    hist.history_file === nothing && return
×
660
    entry = """
×
661
    # time: $(Libc.strftime("%Y-%m-%d %H:%M:%S %Z", time()))
662
    # mode: $mode
663
    $(replace(str, r"^"ms => "\t"))
×
664
    """
665
    # TODO: write-lock history file
666
    try
×
667
        seekend(hist.history_file)
×
668
    catch err
669
        (err isa SystemError) || rethrow()
×
670
        # File handle might get stale after a while, especially under network file systems
671
        # If this doesn't fix it (e.g. when file is deleted), we'll end up rethrowing anyway
672
        hist_open_file(hist)
×
673
    end
674
    print(hist.history_file, entry)
×
675
    flush(hist.history_file)
×
676
    nothing
×
677
end
678

679
function history_move(s::Union{LineEdit.MIState,LineEdit.PrefixSearchState}, hist::REPLHistoryProvider, idx::Int, save_idx::Int = hist.cur_idx)
×
680
    max_idx = length(hist.history) + 1
×
681
    @assert 1 <= hist.cur_idx <= max_idx
×
682
    (1 <= idx <= max_idx) || return :none
×
683
    idx != hist.cur_idx || return :none
×
684

685
    # save the current line
686
    if save_idx == max_idx
×
687
        hist.last_mode = LineEdit.mode(s)
×
688
        hist.last_buffer = copy(LineEdit.buffer(s))
×
689
    else
690
        hist.history[save_idx] = LineEdit.input_string(s)
×
691
        hist.modes[save_idx] = mode_idx(hist, LineEdit.mode(s))
×
692
    end
693

694
    # load the saved line
695
    if idx == max_idx
×
696
        last_buffer = hist.last_buffer
×
697
        LineEdit.transition(s, hist.last_mode) do
×
698
            LineEdit.replace_line(s, last_buffer)
×
699
        end
700
        hist.last_mode = nothing
×
701
        hist.last_buffer = IOBuffer()
×
702
    else
703
        if haskey(hist.mode_mapping, hist.modes[idx])
×
704
            LineEdit.transition(s, hist.mode_mapping[hist.modes[idx]]) do
×
705
                LineEdit.replace_line(s, hist.history[idx])
×
706
            end
707
        else
708
            return :skip
×
709
        end
710
    end
711
    hist.cur_idx = idx
×
712

713
    return :ok
×
714
end
715

716
# REPL History can also transitions modes
717
function LineEdit.accept_result_newmode(hist::REPLHistoryProvider)
×
718
    if 1 <= hist.cur_idx <= length(hist.modes)
×
719
        return hist.mode_mapping[hist.modes[hist.cur_idx]]
×
720
    end
721
    return nothing
×
722
end
723

724
function history_prev(s::LineEdit.MIState, hist::REPLHistoryProvider,
×
725
                      num::Int=1, save_idx::Int = hist.cur_idx)
726
    num <= 0 && return history_next(s, hist, -num, save_idx)
×
727
    hist.last_idx = -1
×
728
    m = history_move(s, hist, hist.cur_idx-num, save_idx)
×
729
    if m === :ok
×
730
        LineEdit.move_input_start(s)
×
731
        LineEdit.reset_key_repeats(s) do
×
732
            LineEdit.move_line_end(s)
×
733
        end
734
        return LineEdit.refresh_line(s)
×
735
    elseif m === :skip
×
736
        return history_prev(s, hist, num+1, save_idx)
×
737
    else
738
        return Terminals.beep(s)
×
739
    end
740
end
741

742
function history_next(s::LineEdit.MIState, hist::REPLHistoryProvider,
×
743
                      num::Int=1, save_idx::Int = hist.cur_idx)
744
    if num == 0
×
745
        Terminals.beep(s)
×
746
        return
×
747
    end
748
    num < 0 && return history_prev(s, hist, -num, save_idx)
×
749
    cur_idx = hist.cur_idx
×
750
    max_idx = length(hist.history) + 1
×
751
    if cur_idx == max_idx && 0 < hist.last_idx
×
752
        # issue #6312
753
        cur_idx = hist.last_idx
×
754
        hist.last_idx = -1
×
755
    end
756
    m = history_move(s, hist, cur_idx+num, save_idx)
×
757
    if m === :ok
×
758
        LineEdit.move_input_end(s)
×
759
        return LineEdit.refresh_line(s)
×
760
    elseif m === :skip
×
761
        return history_next(s, hist, num+1, save_idx)
×
762
    else
763
        return Terminals.beep(s)
×
764
    end
765
end
766

767
history_first(s::LineEdit.MIState, hist::REPLHistoryProvider) =
×
768
    history_prev(s, hist, hist.cur_idx - 1 -
769
                 (hist.cur_idx > hist.start_idx+1 ? hist.start_idx : 0))
770

771
history_last(s::LineEdit.MIState, hist::REPLHistoryProvider) =
×
772
    history_next(s, hist, length(hist.history) - hist.cur_idx + 1)
773

774
function history_move_prefix(s::LineEdit.PrefixSearchState,
×
775
                             hist::REPLHistoryProvider,
776
                             prefix::AbstractString,
777
                             backwards::Bool,
778
                             cur_idx::Int = hist.cur_idx)
779
    cur_response = String(take!(copy(LineEdit.buffer(s))))
×
780
    # when searching forward, start at last_idx
781
    if !backwards && hist.last_idx > 0
×
782
        cur_idx = hist.last_idx
×
783
    end
784
    hist.last_idx = -1
×
785
    max_idx = length(hist.history)+1
×
786
    idxs = backwards ? ((cur_idx-1):-1:1) : ((cur_idx+1):1:max_idx)
×
787
    for idx in idxs
×
788
        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)))
×
789
            m = history_move(s, hist, idx)
×
790
            if m === :ok
×
791
                if idx == max_idx
×
792
                    # on resuming the in-progress edit, leave the cursor where the user last had it
793
                elseif isempty(prefix)
×
794
                    # on empty prefix search, move cursor to the end
795
                    LineEdit.move_input_end(s)
×
796
                else
797
                    # otherwise, keep cursor at the prefix position as a visual cue
798
                    seek(LineEdit.buffer(s), sizeof(prefix))
×
799
                end
800
                LineEdit.refresh_line(s)
×
801
                return :ok
×
802
            elseif m === :skip
×
803
                return history_move_prefix(s,hist,prefix,backwards,idx)
×
804
            end
805
        end
806
    end
×
807
    Terminals.beep(s)
×
808
    nothing
×
809
end
810
history_next_prefix(s::LineEdit.PrefixSearchState, hist::REPLHistoryProvider, prefix::AbstractString) =
×
811
    history_move_prefix(s, hist, prefix, false)
812
history_prev_prefix(s::LineEdit.PrefixSearchState, hist::REPLHistoryProvider, prefix::AbstractString) =
×
813
    history_move_prefix(s, hist, prefix, true)
814

815
function history_search(hist::REPLHistoryProvider, query_buffer::IOBuffer, response_buffer::IOBuffer,
×
816
                        backwards::Bool=false, skip_current::Bool=false)
817

818
    qpos = position(query_buffer)
×
819
    qpos > 0 || return true
×
820
    searchdata = beforecursor(query_buffer)
×
821
    response_str = String(take!(copy(response_buffer)))
×
822

823
    # Alright, first try to see if the current match still works
824
    a = position(response_buffer) + 1 # position is zero-indexed
×
825
    # FIXME: I'm pretty sure this is broken since it uses an index
826
    # into the search data to index into the response string
827
    b = a + sizeof(searchdata)
×
828
    b = b ≤ ncodeunits(response_str) ? prevind(response_str, b) : b-1
×
829
    b = min(lastindex(response_str), b) # ensure that b is valid
×
830

831
    searchstart = backwards ? b : a
×
832
    if searchdata == response_str[a:b]
×
833
        if skip_current
×
834
            searchstart = backwards ? prevind(response_str, b) : nextind(response_str, a)
×
835
        else
836
            return true
×
837
        end
838
    end
839

840
    # Start searching
841
    # First the current response buffer
842
    if 1 <= searchstart <= lastindex(response_str)
×
843
        match = backwards ? findprev(searchdata, response_str, searchstart) :
×
844
                            findnext(searchdata, response_str, searchstart)
845
        if match !== nothing
×
846
            seek(response_buffer, first(match) - 1)
×
847
            return true
×
848
        end
849
    end
850

851
    # Now search all the other buffers
852
    idxs = backwards ? ((hist.cur_idx-1):-1:1) : ((hist.cur_idx+1):1:length(hist.history))
×
853
    for idx in idxs
×
854
        h = hist.history[idx]
×
855
        match = backwards ? findlast(searchdata, h) : findfirst(searchdata, h)
×
856
        if match !== nothing && h != response_str && haskey(hist.mode_mapping, hist.modes[idx])
×
857
            truncate(response_buffer, 0)
×
858
            write(response_buffer, h)
×
859
            seek(response_buffer, first(match) - 1)
×
860
            hist.cur_idx = idx
×
861
            return true
×
862
        end
863
    end
×
864

865
    return false
×
866
end
867

868
function history_reset_state(hist::REPLHistoryProvider)
×
869
    if hist.cur_idx != length(hist.history) + 1
×
870
        hist.last_idx = hist.cur_idx
×
871
        hist.cur_idx = length(hist.history) + 1
×
872
    end
873
    nothing
×
874
end
875
LineEdit.reset_state(hist::REPLHistoryProvider) = history_reset_state(hist)
×
876

877
function return_callback(s)
×
878
    ast = Base.parse_input_line(String(take!(copy(LineEdit.buffer(s)))), depwarn=false)
×
879
    return !(isa(ast, Expr) && ast.head === :incomplete)
×
880
end
881

882
find_hist_file() = get(ENV, "JULIA_HISTORY",
×
883
                       !isempty(DEPOT_PATH) ? joinpath(DEPOT_PATH[1], "logs", "repl_history.jl") :
884
                       error("DEPOT_PATH is empty and and ENV[\"JULIA_HISTORY\"] not set."))
885

886
backend(r::AbstractREPL) = r.backendref
×
887

888
function eval_with_backend(ast, backend::REPLBackendRef)
×
889
    put!(backend.repl_channel, (ast, 1))
×
890
    return take!(backend.response_channel) # (val, iserr)
×
891
end
892

893
function respond(f, repl, main; pass_empty::Bool = false, suppress_on_semicolon::Bool = true)
×
894
    return function do_respond(s::MIState, buf, ok::Bool)
×
895
        if !ok
×
896
            return transition(s, :abort)
×
897
        end
898
        line = String(take!(buf)::Vector{UInt8})
×
899
        if !isempty(line) || pass_empty
×
900
            reset(repl)
×
901
            local response
×
902
            try
×
903
                ast = Base.invokelatest(f, line)
×
904
                response = eval_with_backend(ast, backend(repl))
×
905
            catch
906
                response = Pair{Any, Bool}(current_exceptions(), true)
×
907
            end
908
            hide_output = suppress_on_semicolon && ends_with_semicolon(line)
×
909
            print_response(repl, response, !hide_output, hascolor(repl))
×
910
        end
911
        prepare_next(repl)
×
912
        reset_state(s)
×
913
        return s.current_mode.sticky ? true : transition(s, main)
×
914
    end
915
end
916

917
function reset(repl::LineEditREPL)
×
918
    raw!(repl.t, false)
×
919
    hascolor(repl) && print(repl.t, Base.text_colors[:normal])
×
920
    nothing
×
921
end
922

923
function prepare_next(repl::LineEditREPL)
×
924
    println(terminal(repl))
×
925
end
926

927
function mode_keymap(julia_prompt::Prompt)
×
928
    AnyDict(
×
929
    '\b' => function (s::MIState,o...)
×
930
        if isempty(s) || position(LineEdit.buffer(s)) == 0
×
931
            buf = copy(LineEdit.buffer(s))
×
932
            transition(s, julia_prompt) do
×
933
                LineEdit.state(s, julia_prompt).input_buffer = buf
×
934
            end
935
        else
936
            LineEdit.edit_backspace(s)
×
937
        end
938
    end,
939
    "^C" => function (s::MIState,o...)
×
940
        LineEdit.move_input_end(s)
×
941
        LineEdit.refresh_line(s)
×
942
        print(LineEdit.terminal(s), "^C\n\n")
×
943
        transition(s, julia_prompt)
×
944
        transition(s, :reset)
×
945
        LineEdit.refresh_line(s)
×
946
    end)
947
end
948

949
repl_filename(repl, hp::REPLHistoryProvider) = "REPL[$(max(length(hp.history)-hp.start_idx, 1))]"
×
950
repl_filename(repl, hp) = "REPL"
×
951

952
const JL_PROMPT_PASTE = Ref(true)
953
enable_promptpaste(v::Bool) = JL_PROMPT_PASTE[] = v
×
954

955
function contextual_prompt(repl::LineEditREPL, prompt::Union{String,Function})
×
956
    function ()
×
957
        mod = active_module(repl)
×
958
        prefix = mod == Main ? "" : string('(', mod, ") ")
×
959
        pr = prompt isa String ? prompt : prompt()
×
960
        prefix * pr
×
961
    end
962
end
963

964
setup_interface(
×
965
    repl::LineEditREPL;
966
    # those keyword arguments may be deprecated eventually in favor of the Options mechanism
967
    hascolor::Bool = repl.options.hascolor,
968
    extra_repl_keymap::Any = repl.options.extra_keymap
969
) = setup_interface(repl, hascolor, extra_repl_keymap)
970

971
# This non keyword method can be precompiled which is important
972
function setup_interface(
×
973
    repl::LineEditREPL,
974
    hascolor::Bool,
975
    extra_repl_keymap::Any, # Union{Dict,Vector{<:Dict}},
976
)
977
    # The precompile statement emitter has problem outputting valid syntax for the
978
    # type of `Union{Dict,Vector{<:Dict}}` (see #28808).
979
    # This function is however important to precompile for REPL startup time, therefore,
980
    # make the type Any and just assert that we have the correct type below.
981
    @assert extra_repl_keymap isa Union{Dict,Vector{<:Dict}}
×
982

983
    ###
984
    #
985
    # This function returns the main interface that describes the REPL
986
    # functionality, it is called internally by functions that setup a
987
    # Terminal-based REPL frontend.
988
    #
989
    # See run_frontend(repl::LineEditREPL, backend::REPLBackendRef)
990
    # for usage
991
    #
992
    ###
993

994
    ###
995
    # We setup the interface in two stages.
996
    # First, we set up all components (prompt,rsearch,shell,help)
997
    # Second, we create keymaps with appropriate transitions between them
998
    #   and assign them to the components
999
    #
1000
    ###
1001

1002
    ############################### Stage I ################################
1003

1004
    # This will provide completions for REPL and help mode
1005
    replc = REPLCompletionProvider()
×
1006

1007
    # Set up the main Julia prompt
1008
    julia_prompt = Prompt(contextual_prompt(repl, JULIA_PROMPT);
×
1009
        # Copy colors from the prompt object
1010
        prompt_prefix = hascolor ? repl.prompt_color : "",
1011
        prompt_suffix = hascolor ?
1012
            (repl.envcolors ? Base.input_color : repl.input_color) : "",
1013
        repl = repl,
1014
        complete = replc,
1015
        on_enter = return_callback)
1016

1017
    # Setup help mode
1018
    help_mode = Prompt(contextual_prompt(repl, "help?> "),
×
1019
        prompt_prefix = hascolor ? repl.help_color : "",
1020
        prompt_suffix = hascolor ?
1021
            (repl.envcolors ? Base.input_color : repl.input_color) : "",
1022
        repl = repl,
1023
        complete = replc,
1024
        # When we're done transform the entered line into a call to helpmode function
1025
        on_done = respond(line::String->helpmode(outstream(repl), line, repl.mistate.active_module),
×
1026
                          repl, julia_prompt, pass_empty=true, suppress_on_semicolon=false))
1027

1028

1029
    # Set up shell mode
1030
    shell_mode = Prompt(SHELL_PROMPT;
×
1031
        prompt_prefix = hascolor ? repl.shell_color : "",
1032
        prompt_suffix = hascolor ?
1033
            (repl.envcolors ? Base.input_color : repl.input_color) : "",
1034
        repl = repl,
1035
        complete = ShellCompletionProvider(),
1036
        # Transform "foo bar baz" into `foo bar baz` (shell quoting)
1037
        # and pass into Base.repl_cmd for processing (handles `ls` and `cd`
1038
        # special)
1039
        on_done = respond(repl, julia_prompt) do line
1040
            Expr(:call, :(Base.repl_cmd),
×
1041
                :(Base.cmd_gen($(Base.shell_parse(line::String)[1]))),
1042
                outstream(repl))
1043
        end,
1044
        sticky = true)
1045

1046

1047
    ################################# Stage II #############################
1048

1049
    # Setup history
1050
    # We will have a unified history for all REPL modes
1051
    hp = REPLHistoryProvider(Dict{Symbol,Prompt}(:julia => julia_prompt,
×
1052
                                                 :shell => shell_mode,
1053
                                                 :help  => help_mode))
1054
    if repl.history_file
×
1055
        try
×
1056
            hist_path = find_hist_file()
×
1057
            mkpath(dirname(hist_path))
×
1058
            hp.file_path = hist_path
×
1059
            hist_open_file(hp)
×
1060
            finalizer(replc) do replc
×
1061
                close(hp.history_file)
×
1062
            end
1063
            hist_from_file(hp, hist_path)
×
1064
        catch
1065
            # use REPL.hascolor to avoid using the local variable with the same name
1066
            print_response(repl, Pair{Any, Bool}(current_exceptions(), true), true, REPL.hascolor(repl))
×
1067
            println(outstream(repl))
×
1068
            @info "Disabling history file for this session"
×
1069
            repl.history_file = false
×
1070
        end
1071
    end
1072
    history_reset_state(hp)
×
1073
    julia_prompt.hist = hp
×
1074
    shell_mode.hist = hp
×
1075
    help_mode.hist = hp
×
1076

1077
    julia_prompt.on_done = respond(x->Base.parse_input_line(x,filename=repl_filename(repl,hp)), repl, julia_prompt)
×
1078

1079

1080
    search_prompt, skeymap = LineEdit.setup_search_keymap(hp)
×
1081
    search_prompt.complete = LatexCompletions()
×
1082

1083
    shell_prompt_len = length(SHELL_PROMPT)
×
1084
    help_prompt_len = length(HELP_PROMPT)
×
1085
    jl_prompt_regex = r"^In \[[0-9]+\]: |^(?:\(.+\) )?julia> "
×
1086
    pkg_prompt_regex = r"^(?:\(.+\) )?pkg> "
×
1087

1088
    # Canonicalize user keymap input
1089
    if isa(extra_repl_keymap, Dict)
×
1090
        extra_repl_keymap = AnyDict[extra_repl_keymap]
×
1091
    end
1092

1093
    repl_keymap = AnyDict(
×
1094
        ';' => function (s::MIState,o...)
×
1095
            if isempty(s) || position(LineEdit.buffer(s)) == 0
×
1096
                buf = copy(LineEdit.buffer(s))
×
1097
                transition(s, shell_mode) do
×
1098
                    LineEdit.state(s, shell_mode).input_buffer = buf
×
1099
                end
1100
            else
1101
                edit_insert(s, ';')
×
1102
            end
1103
        end,
1104
        '?' => function (s::MIState,o...)
×
1105
            if isempty(s) || position(LineEdit.buffer(s)) == 0
×
1106
                buf = copy(LineEdit.buffer(s))
×
1107
                transition(s, help_mode) do
×
1108
                    LineEdit.state(s, help_mode).input_buffer = buf
×
1109
                end
1110
            else
1111
                edit_insert(s, '?')
×
1112
            end
1113
        end,
1114

1115
        # Bracketed Paste Mode
1116
        "\e[200~" => (s::MIState,o...)->begin
×
1117
            input = LineEdit.bracketed_paste(s) # read directly from s until reaching the end-bracketed-paste marker
×
1118
            sbuffer = LineEdit.buffer(s)
×
1119
            curspos = position(sbuffer)
×
1120
            seek(sbuffer, 0)
×
1121
            shouldeval = (bytesavailable(sbuffer) == curspos && !occursin(UInt8('\n'), sbuffer))
×
1122
            seek(sbuffer, curspos)
×
1123
            if curspos == 0
×
1124
                # if pasting at the beginning, strip leading whitespace
1125
                input = lstrip(input)
×
1126
            end
1127
            if !shouldeval
×
1128
                # when pasting in the middle of input, just paste in place
1129
                # don't try to execute all the WIP, since that's rather confusing
1130
                # and is often ill-defined how it should behave
1131
                edit_insert(s, input)
×
1132
                return
×
1133
            end
1134
            LineEdit.push_undo(s)
×
1135
            edit_insert(sbuffer, input)
×
1136
            input = String(take!(sbuffer))
×
1137
            oldpos = firstindex(input)
×
1138
            firstline = true
×
1139
            isprompt_paste = false
×
1140
            curr_prompt_len = 0
×
1141
            pasting_help = false
×
1142

1143
            while oldpos <= lastindex(input) # loop until all lines have been executed
×
1144
                if JL_PROMPT_PASTE[]
×
1145
                    # Check if the next statement starts with a prompt i.e. "julia> ", in that case
1146
                    # skip it. But first skip whitespace unless pasting in a docstring which may have
1147
                    # indented prompt examples that we don't want to execute
1148
                    while input[oldpos] in (pasting_help ? ('\n') : ('\n', ' ', '\t'))
×
1149
                        oldpos = nextind(input, oldpos)
×
1150
                        oldpos >= sizeof(input) && return
×
1151
                    end
×
1152
                    substr = SubString(input, oldpos)
×
1153
                    # Check if input line starts with "julia> ", remove it if we are in prompt paste mode
1154
                    if (firstline || isprompt_paste) && startswith(substr, jl_prompt_regex)
×
1155
                        detected_jl_prompt = match(jl_prompt_regex, substr).match
×
1156
                        isprompt_paste = true
×
1157
                        curr_prompt_len = sizeof(detected_jl_prompt)
×
1158
                        oldpos += curr_prompt_len
×
1159
                        transition(s, julia_prompt)
×
1160
                        pasting_help = false
×
1161
                    # Check if input line starts with "pkg> " or "(...) pkg> ", remove it if we are in prompt paste mode and switch mode
1162
                    elseif (firstline || isprompt_paste) && startswith(substr, pkg_prompt_regex)
×
1163
                        detected_pkg_prompt = match(pkg_prompt_regex, substr).match
×
1164
                        isprompt_paste = true
×
1165
                        curr_prompt_len = sizeof(detected_pkg_prompt)
×
1166
                        oldpos += curr_prompt_len
×
1167
                        Base.active_repl.interface.modes[1].keymap_dict[']'](s, o...)
×
1168
                        pasting_help = false
×
1169
                    # Check if input line starts with "shell> ", remove it if we are in prompt paste mode and switch mode
1170
                    elseif (firstline || isprompt_paste) && startswith(substr, SHELL_PROMPT)
×
1171
                        isprompt_paste = true
×
1172
                        oldpos += shell_prompt_len
×
1173
                        curr_prompt_len = shell_prompt_len
×
1174
                        transition(s, shell_mode)
×
1175
                        pasting_help = false
×
1176
                    # Check if input line starts with "help?> ", remove it if we are in prompt paste mode and switch mode
1177
                    elseif (firstline || isprompt_paste) && startswith(substr, HELP_PROMPT)
×
1178
                        isprompt_paste = true
×
1179
                        oldpos += help_prompt_len
×
1180
                        curr_prompt_len = help_prompt_len
×
1181
                        transition(s, help_mode)
×
1182
                        pasting_help = true
×
1183
                    # If we are prompt pasting and current statement does not begin with a mode prefix, skip to next line
1184
                    elseif isprompt_paste
×
1185
                        while input[oldpos] != '\n'
×
1186
                            oldpos = nextind(input, oldpos)
×
1187
                            oldpos >= sizeof(input) && return
×
1188
                        end
×
1189
                        continue
×
1190
                    end
1191
                end
1192
                dump_tail = false
×
1193
                nl_pos = findfirst('\n', input[oldpos:end])
×
1194
                if s.current_mode == julia_prompt
×
1195
                    ast, pos = Meta.parse(input, oldpos, raise=false, depwarn=false)
×
1196
                    if (isa(ast, Expr) && (ast.head === :error || ast.head === :incomplete)) ||
×
1197
                            (pos > ncodeunits(input) && !endswith(input, '\n'))
1198
                        # remaining text is incomplete (an error, or parser ran to the end but didn't stop with a newline):
1199
                        # Insert all the remaining text as one line (might be empty)
1200
                        dump_tail = true
×
1201
                    end
1202
                elseif isnothing(nl_pos) # no newline at end, so just dump the tail into the prompt and don't execute
×
1203
                    dump_tail = true
×
1204
                elseif s.current_mode == shell_mode # handle multiline shell commands
×
1205
                    lines = split(input[oldpos:end], '\n')
×
1206
                    pos = oldpos + sizeof(lines[1]) + 1
×
1207
                    if length(lines) > 1
×
1208
                        for line in lines[2:end]
×
1209
                            # to be recognized as a multiline shell command, the lines must be indented to the
1210
                            # same prompt position
1211
                            if !startswith(line, ' '^curr_prompt_len)
×
1212
                                break
×
1213
                            end
1214
                            pos += sizeof(line) + 1
×
1215
                        end
×
1216
                    end
1217
                else
1218
                    pos = oldpos + nl_pos
×
1219
                end
1220
                if dump_tail
×
1221
                    tail = input[oldpos:end]
×
1222
                    if !firstline
×
1223
                        # strip leading whitespace, but only if it was the result of executing something
1224
                        # (avoids modifying the user's current leading wip line)
1225
                        tail = lstrip(tail)
×
1226
                    end
1227
                    if isprompt_paste # remove indentation spaces corresponding to the prompt
×
1228
                        tail = replace(tail, r"^"m * ' '^curr_prompt_len => "")
×
1229
                    end
1230
                    LineEdit.replace_line(s, tail, true)
×
1231
                    LineEdit.refresh_line(s)
×
1232
                    break
×
1233
                end
1234
                # get the line and strip leading and trailing whitespace
1235
                line = strip(input[oldpos:prevind(input, pos)])
×
1236
                if !isempty(line)
×
1237
                    if isprompt_paste # remove indentation spaces corresponding to the prompt
×
1238
                        line = replace(line, r"^"m * ' '^curr_prompt_len => "")
×
1239
                    end
1240
                    # put the line on the screen and history
1241
                    LineEdit.replace_line(s, line)
×
1242
                    LineEdit.commit_line(s)
×
1243
                    # execute the statement
1244
                    terminal = LineEdit.terminal(s) # This is slightly ugly but ok for now
×
1245
                    raw!(terminal, false) && disable_bracketed_paste(terminal)
×
1246
                    LineEdit.mode(s).on_done(s, LineEdit.buffer(s), true)
×
1247
                    raw!(terminal, true) && enable_bracketed_paste(terminal)
×
1248
                    LineEdit.push_undo(s) # when the last line is incomplete
×
1249
                end
1250
                oldpos = pos
×
1251
                firstline = false
×
1252
            end
×
1253
        end,
1254

1255
        # Open the editor at the location of a stackframe or method
1256
        # This is accessing a contextual variable that gets set in
1257
        # the show_backtrace and show_method_table functions.
1258
        "^Q" => (s::MIState, o...) -> begin
×
1259
            linfos = repl.last_shown_line_infos
×
1260
            str = String(take!(LineEdit.buffer(s)))
×
1261
            n = tryparse(Int, str)
×
1262
            n === nothing && @goto writeback
×
1263
            if n <= 0 || n > length(linfos) || startswith(linfos[n][1], "REPL[")
×
1264
                @goto writeback
×
1265
            end
1266
            try
×
1267
                InteractiveUtils.edit(Base.fixup_stdlib_path(linfos[n][1]), linfos[n][2])
×
1268
            catch ex
1269
                ex isa ProcessFailedException || ex isa Base.IOError || ex isa SystemError || rethrow()
×
1270
                @info "edit failed" _exception=ex
×
1271
            end
1272
            LineEdit.refresh_line(s)
×
1273
            return
×
1274
            @label writeback
×
1275
            write(LineEdit.buffer(s), str)
×
1276
            return
×
1277
        end,
1278
    )
1279

1280
    prefix_prompt, prefix_keymap = LineEdit.setup_prefix_keymap(hp, julia_prompt)
×
1281

1282
    a = Dict{Any,Any}[skeymap, repl_keymap, prefix_keymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
×
1283
    prepend!(a, extra_repl_keymap)
×
1284

1285
    julia_prompt.keymap_dict = LineEdit.keymap(a)
×
1286

1287
    mk = mode_keymap(julia_prompt)
×
1288

1289
    b = Dict{Any,Any}[skeymap, mk, prefix_keymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
×
1290
    prepend!(b, extra_repl_keymap)
×
1291

1292
    shell_mode.keymap_dict = help_mode.keymap_dict = LineEdit.keymap(b)
×
1293

1294
    allprompts = LineEdit.TextInterface[julia_prompt, shell_mode, help_mode, search_prompt, prefix_prompt]
×
1295
    return ModalInterface(allprompts)
×
1296
end
1297

1298
function run_frontend(repl::LineEditREPL, backend::REPLBackendRef)
×
1299
    repl.frontend_task = current_task()
×
1300
    d = REPLDisplay(repl)
×
1301
    dopushdisplay = repl.specialdisplay === nothing && !in(d,Base.Multimedia.displays)
×
1302
    dopushdisplay && pushdisplay(d)
×
1303
    if !isdefined(repl,:interface)
×
1304
        interface = repl.interface = setup_interface(repl)
×
1305
    else
1306
        interface = repl.interface
×
1307
    end
1308
    repl.backendref = backend
×
1309
    repl.mistate = LineEdit.init_state(terminal(repl), interface)
×
1310
    run_interface(terminal(repl), interface, repl.mistate)
×
1311
    # Terminate Backend
1312
    put!(backend.repl_channel, (nothing, -1))
×
1313
    dopushdisplay && popdisplay(d)
×
1314
    nothing
×
1315
end
1316

1317
## StreamREPL ##
1318

1319
mutable struct StreamREPL <: AbstractREPL
1320
    stream::IO
1321
    prompt_color::String
1322
    input_color::String
1323
    answer_color::String
1324
    waserror::Bool
1325
    frontend_task::Task
1326
    StreamREPL(stream,pc,ic,ac) = new(stream,pc,ic,ac,false)
×
1327
end
1328
StreamREPL(stream::IO) = StreamREPL(stream, Base.text_colors[:green], Base.input_color(), Base.answer_color())
×
1329
run_repl(stream::IO) = run_repl(StreamREPL(stream))
×
1330

1331
outstream(s::StreamREPL) = s.stream
×
1332
hascolor(s::StreamREPL) = get(s.stream, :color, false)::Bool
×
1333

1334
answer_color(r::LineEditREPL) = r.envcolors ? Base.answer_color() : r.answer_color
×
1335
answer_color(r::StreamREPL) = r.answer_color
×
1336
input_color(r::LineEditREPL) = r.envcolors ? Base.input_color() : r.input_color
×
1337
input_color(r::StreamREPL) = r.input_color
×
1338

1339
let matchend = Dict("\"" => r"\"", "\"\"\"" => r"\"\"\"", "'" => r"'",
1340
    "`" => r"`", "```" => r"```", "#" => r"$"m, "#=" => r"=#|#=")
1341
    global _rm_strings_and_comments
1342
    function _rm_strings_and_comments(code::Union{String,SubString{String}})
×
1343
        buf = IOBuffer(sizehint = sizeof(code))
×
1344
        pos = 1
×
1345
        while true
×
1346
            i = findnext(r"\"(?!\"\")|\"\"\"|'|`(?!``)|```|#(?!=)|#=", code, pos)
×
1347
            isnothing(i) && break
×
1348
            match = SubString(code, i)
×
1349
            j = findnext(matchend[match]::Regex, code, nextind(code, last(i)))
×
1350
            if match == "#=" # possibly nested
×
1351
                nested = 1
×
1352
                while j !== nothing
×
1353
                    nested += SubString(code, j) == "#=" ? +1 : -1
×
1354
                    iszero(nested) && break
×
1355
                    j = findnext(r"=#|#=", code, nextind(code, last(j)))
×
1356
                end
×
1357
            elseif match[1] != '#' # quote match: check non-escaped
×
1358
                while j !== nothing
×
1359
                    notbackslash = findprev(!=('\\'), code, prevind(code, first(j)))::Int
×
1360
                    isodd(first(j) - notbackslash) && break # not escaped
×
1361
                    j = findnext(matchend[match]::Regex, code, nextind(code, first(j)))
×
1362
                end
×
1363
            end
1364
            isnothing(j) && break
×
1365
            if match[1] == '#'
×
1366
                print(buf, SubString(code, pos, prevind(code, first(i))))
×
1367
            else
1368
                print(buf, SubString(code, pos, last(i)), ' ', SubString(code, j))
×
1369
            end
1370
            pos = nextind(code, last(j))
×
1371
        end
×
1372
        print(buf, SubString(code, pos, lastindex(code)))
×
1373
        return String(take!(buf))
×
1374
    end
1375
end
1376

1377
# heuristic function to decide if the presence of a semicolon
1378
# at the end of the expression was intended for suppressing output
1379
ends_with_semicolon(code::AbstractString) = ends_with_semicolon(String(code))
×
1380
ends_with_semicolon(code::Union{String,SubString{String}}) =
×
1381
    contains(_rm_strings_and_comments(code), r";\s*$")
×
1382

1383
function run_frontend(repl::StreamREPL, backend::REPLBackendRef)
×
1384
    repl.frontend_task = current_task()
×
1385
    have_color = hascolor(repl)
×
1386
    Base.banner(repl.stream)
×
1387
    d = REPLDisplay(repl)
×
1388
    dopushdisplay = !in(d,Base.Multimedia.displays)
×
1389
    dopushdisplay && pushdisplay(d)
×
1390
    while !eof(repl.stream)::Bool
×
1391
        if have_color
×
1392
            print(repl.stream,repl.prompt_color)
×
1393
        end
1394
        print(repl.stream, "julia> ")
×
1395
        if have_color
×
1396
            print(repl.stream, input_color(repl))
×
1397
        end
1398
        line = readline(repl.stream, keep=true)
×
1399
        if !isempty(line)
×
1400
            ast = Base.parse_input_line(line)
×
1401
            if have_color
×
1402
                print(repl.stream, Base.color_normal)
×
1403
            end
1404
            response = eval_with_backend(ast, backend)
×
1405
            print_response(repl, response, !ends_with_semicolon(line), have_color)
×
1406
        end
1407
    end
×
1408
    # Terminate Backend
1409
    put!(backend.repl_channel, (nothing, -1))
×
1410
    dopushdisplay && popdisplay(d)
×
1411
    nothing
×
1412
end
1413

1414
module Numbered
1415

1416
using ..REPL
1417

1418
__current_ast_transforms() = isdefined(Base, :active_repl_backend) ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
×
1419

1420
function repl_eval_counter(hp)
×
1421
    return length(hp.history) - hp.start_idx
×
1422
end
1423

1424
function out_transform(@nospecialize(x), n::Ref{Int})
×
1425
    return Expr(:toplevel, get_usings!([], x)..., quote
×
1426
        let __temp_val_a72df459 = $x
×
1427
            $capture_result($n, __temp_val_a72df459)
×
1428
            __temp_val_a72df459
×
1429
        end
1430
    end)
1431
end
1432

1433
function get_usings!(usings, ex)
×
1434
    ex isa Expr || return usings
×
1435
    # get all `using` and `import` statements which are at the top level
1436
    for (i, arg) in enumerate(ex.args)
×
1437
        if Base.isexpr(arg, :toplevel)
×
1438
            get_usings!(usings, arg)
×
1439
        elseif Base.isexpr(arg, [:using, :import])
×
1440
            push!(usings, popat!(ex.args, i))
×
1441
        end
1442
    end
×
1443
    return usings
×
1444
end
1445

1446
function capture_result(n::Ref{Int}, @nospecialize(x))
×
1447
    n = n[]
×
1448
    mod = Base.MainInclude
×
1449
    if !isdefined(mod, :Out)
×
1450
        @eval mod global Out
×
1451
        @eval mod export Out
×
1452
        setglobal!(mod, :Out, Dict{Int, Any}())
×
1453
    end
1454
    if x !== getglobal(mod, :Out) && x !== nothing # remove this?
×
1455
        getglobal(mod, :Out)[n] = x
×
1456
    end
1457
    nothing
×
1458
end
1459

1460
function set_prompt(repl::LineEditREPL, n::Ref{Int})
×
1461
    julia_prompt = repl.interface.modes[1]
×
1462
    julia_prompt.prompt = function()
×
1463
        n[] = repl_eval_counter(julia_prompt.hist)+1
×
1464
        string("In [", n[], "]: ")
×
1465
    end
1466
    nothing
×
1467
end
1468

1469
function set_output_prefix(repl::LineEditREPL, n::Ref{Int})
×
1470
    julia_prompt = repl.interface.modes[1]
×
1471
    if REPL.hascolor(repl)
×
1472
        julia_prompt.output_prefix_prefix = Base.text_colors[:red]
×
1473
    end
1474
    julia_prompt.output_prefix = () -> string("Out[", n[], "]: ")
×
1475
    nothing
×
1476
end
1477

1478
function __current_ast_transforms(backend)
×
1479
    if backend === nothing
×
1480
        isdefined(Base, :active_repl_backend) ? Base.active_repl_backend.ast_transforms : REPL.repl_ast_transforms
×
1481
    else
1482
        backend.ast_transforms
×
1483
    end
1484
end
1485

1486

1487
function numbered_prompt!(repl::LineEditREPL=Base.active_repl, backend=nothing)
×
1488
    n = Ref{Int}(0)
×
1489
    set_prompt(repl, n)
×
1490
    set_output_prefix(repl, n)
×
1491
    push!(__current_ast_transforms(backend), @nospecialize(ast) -> out_transform(ast, n))
×
1492
    return
×
1493
end
1494

1495
"""
1496
    Out[n]
1497

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

1501
See also [`ans`](@ref).
1502
"""
1503
Base.MainInclude.Out
1504

1505
end
1506

1507
import .Numbered.numbered_prompt!
1508

1509
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

© 2025 Coveralls, Inc