Skip to content

Commit 1767963

Browse files
authored
REPL: Optionize a bunch of global variables (#24940)
1 parent 037b818 commit 1767963

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

base/repl/LineEdit.jl

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ end
3737

3838
show(io::IO, x::Prompt) = show(io, string("Prompt(\"", prompt_string(x.prompt), "\",...)"))
3939

40-
"Maximum number of entries in the kill ring queue.
41-
Beyond this number, oldest entries are discarded first."
42-
const KILL_RING_MAX = Ref(100)
4340

4441
mutable struct MIState
4542
interface::ModalInterface
@@ -87,7 +84,7 @@ options(s::PromptState) =
8784
# in the REPL module
8885
s.p.repl.options
8986
else
90-
Base.REPL.Options(confirm_exit=false)
87+
Base.REPL.GlobalOptions
9188
end
9289

9390
function setmark(s::MIState, guess_region_active::Bool=true)
@@ -118,7 +115,6 @@ deactivate_region(s::ModeState) = activate_region(s, false)
118115
is_region_active(s::PromptState) = s.region_active
119116
is_region_active(s::ModeState) = false
120117

121-
const REGION_ANIMATION_DURATION = Ref(0.2)
122118

123119
input_string(s::PromptState) = String(take!(copy(s.input_buffer)))
124120

@@ -144,16 +140,11 @@ terminal(s::IO) = s
144140
terminal(s::PromptState) = s.terminal
145141

146142

147-
# these may be better stored in Prompt or LineEditREPL
148-
const BEEP_DURATION = Ref(0.2)
149-
const BEEP_BLINK = Ref(0.2)
150-
const BEEP_MAXDURATION = Ref(1.0)
151-
const BEEP_COLORS = ["\e[90m"] # gray (text_colors not yet available)
152-
const BEEP_USE_CURRENT = Ref(true)
153-
154-
function beep(s::PromptState, duration::Real=BEEP_DURATION[], blink::Real=BEEP_BLINK[],
155-
maxduration::Real=BEEP_MAXDURATION[];
156-
colors=BEEP_COLORS, use_current::Bool=BEEP_USE_CURRENT[])
143+
function beep(s::PromptState, duration::Real=options(s).beep_duration,
144+
blink::Real=options(s).beep_blink,
145+
maxduration::Real=options(s).beep_maxduration;
146+
colors=options(s).beep_colors,
147+
use_current::Bool=options(s).beep_use_current)
157148
isinteractive() || return # some tests fail on some platforms
158149
s.beeping = min(s.beeping + duration, maxduration)
159150
@async begin
@@ -855,7 +846,7 @@ function push_kill!(s::MIState, killed::String, concat = s.key_repeats > 0; rev=
855846
s.kill_ring[end] * killed
856847
else
857848
push!(s.kill_ring, killed)
858-
length(s.kill_ring) > KILL_RING_MAX[] && shift!(s.kill_ring)
849+
length(s.kill_ring) > options(s).kill_ring_max && shift!(s.kill_ring)
859850
end
860851
s.kill_idx = endof(s.kill_ring)
861852
true
@@ -880,9 +871,9 @@ function edit_copy_region(s::MIState)
880871
set_action!(s, :edit_copy_region)
881872
buf = buffer(s)
882873
push_kill!(s, content(buf, region(buf)), false) || return :ignore
883-
if REGION_ANIMATION_DURATION[] > 0.0
874+
if options(s).region_animation_duration > 0.0
884875
edit_exchange_point_and_mark(s)
885-
sleep(REGION_ANIMATION_DURATION[])
876+
sleep(options(s).region_animation_duration)
886877
edit_exchange_point_and_mark(s)
887878
end
888879
end
@@ -1822,16 +1813,7 @@ function commit_line(s)
18221813
state(s, mode(s)).ias = InputAreaState(0, 0)
18231814
end
18241815

1825-
"""
1826-
`Base.LineEdit.tabwidth` controls the presumed tab width of code pasted into the REPL.
1827-
1828-
You can modify it by doing `@eval Base.LineEdit tabwidth = 4`, for example.
1829-
1830-
Must satisfy `0 < tabwidth <= 16`.
1831-
"""
1832-
global tabwidth = 8
1833-
1834-
function bracketed_paste(s)
1816+
function bracketed_paste(s; tabwidth=options(s).tabwidth)
18351817
ps = state(s, mode(s))
18361818
str = readuntil(ps.terminal, "\e[201~")
18371819
input = str[1:prevind(str, end-5)]

base/repl/REPL.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ end
247247
mutable struct Options
248248
hascolor::Bool
249249
extra_keymap::Union{Dict,Vector{<:Dict}}
250+
# controls the presumed tab width of code pasted into the REPL.
251+
# Must satisfy `0 < tabwidth <= 16`.
252+
tabwidth::Int
253+
# Maximum number of entries in the kill ring queue.
254+
# Beyond this number, oldest entries are discarded first.
255+
kill_ring_max::Int
256+
region_animation_duration::Float64
257+
beep_duration::Float64
258+
beep_blink::Float64
259+
beep_maxduration::Float64
260+
beep_colors::Vector{String}
261+
beep_use_current::Bool
250262
backspace_align::Bool
251263
backspace_adjust::Bool
252264
confirm_exit::Bool # ^D must be repeated to confirm exit
@@ -255,9 +267,23 @@ end
255267
Options(;
256268
hascolor = true,
257269
extra_keymap = AnyDict[],
270+
tabwidth = 8,
271+
kill_ring_max = 100,
272+
region_animation_duration = 0.2,
273+
beep_duration = 0.2, beep_blink = 0.2, beep_maxduration = 1.0,
274+
beep_colors = ["\e[90m"], # gray (text_colors not yet available)
275+
beep_use_current = true,
258276
backspace_align = true, backspace_adjust = backspace_align,
259277
confirm_exit = false) =
260-
Options(hascolor, extra_keymap, backspace_align, backspace_adjust, confirm_exit)
278+
Options(hascolor, extra_keymap, tabwidth,
279+
kill_ring_max, region_animation_duration,
280+
beep_duration, beep_blink, beep_maxduration,
281+
beep_colors, beep_use_current,
282+
backspace_align, backspace_adjust, confirm_exit)
283+
284+
# for use by REPLs not having an options field
285+
const GlobalOptions = Options()
286+
261287

262288
## LineEditREPL ##
263289

test/lineedit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__),
77
using Main.TestHelpers
88

99
# no need to have animation in tests
10-
LineEdit.REGION_ANIMATION_DURATION[] = 0.001
10+
Base.REPL.GlobalOptions.region_animation_duration=0.001
1111

1212
## helper functions
1313

0 commit comments

Comments
 (0)