Skip to content

Commit

Permalink
make alt-m on an empty prompt go to previous module
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Jan 5, 2024
1 parent 38b8156 commit c8cab3f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ Standard library changes

* Tab complete hints now show in lighter text while typing in the repl. To disable
set `Base.active_repl.options.hint_tab_completes = false` ([#51229]).
* Meta-M with an empty prompt now returns the contextual module of the REPL to `Main`.
* Meta-M with an empty prompt now returns the contextual module of the REPL to the previous
contextual module if there was one so that switching back and forth is simple. ([#51616], [#52670])

#### SuiteSparse

Expand Down
10 changes: 8 additions & 2 deletions stdlib/REPL/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,10 @@ Main

It is possible to change this contextual module via the function
`REPL.activate(m)` where `m` is a `Module` or by typing the module in the REPL
and pressing the keybinding Alt-m (the cursor must be on the module name). The `Main` module can be "activated" with an empty prompt plus the keybinding. The
active module is shown in the prompt (unless it is `Main`):
and pressing the keybinding Alt-m with the cursor on the module name (Esc-m on MacOS).
Pressing the keybinding on an empty prompt returns to the previously active
module (i.e. `Main` after the first use). The active module is shown in the prompt
(unless it is `Main`):

```julia-repl
julia> using REPL
Expand All @@ -602,6 +604,10 @@ julia> Core<Alt-m> # using the keybinding to change module
(Core) julia> <Alt-m> # going back to Main via keybinding
julia>
julia> <Alt-m> # going back to previously-active Core via keybinding
(Core) julia>
```

Functions that take an optional module argument often defaults to the REPL
Expand Down
7 changes: 5 additions & 2 deletions stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1477,22 +1477,25 @@ end

current_word_with_dots(s::MIState) = current_word_with_dots(buffer(s))

previous_active_module = Main

function activate_module(s::MIState)
word = current_word_with_dots(s);
mod = if isempty(word)
edit_insert(s, ' ') # makes the `edit_clear` below actually update the prompt
Main
previous_active_module
else
try
Base.Core.eval(Base.active_module(), Base.Meta.parse(word))
catch
nothing
end
end
if !(mod isa Module)
if !(mod isa Module) || mod == Base.active_module()
beep(s)
return
end
global previous_active_module = Base.active_module()
REPL.activate(mod)
edit_clear(s)
end
Expand Down

0 comments on commit c8cab3f

Please sign in to comment.