Skip to content

Commit cd751c9

Browse files
authored
fix(auto): swap render → source on auto_off so toggle is symmetric (#18)
`:MdRender auto` is `auto_toggle` in disguise, so calling it twice should return to the pre-toggle state. Previously `auto_off` only tore down the autocmd / keymaps / b: marker but left the render buffer on screen, so two `:MdRender auto` invocations ended up with auto OFF plus render still displayed — surprising for a toggle. Make `auto_off` also call `MdPreview.toggle()` when the current window is showing this buffer's render view. `auto_toggle` inherits the fix without further change. Update :help and tests accordingly.
1 parent b8a4a36 commit cd751c9

3 files changed

Lines changed: 63 additions & 9 deletions

File tree

doc/md-render.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,12 @@ still registered as deprecated aliases — see |md-render-deprecated-commands|.
218218
immediately, and registers `InsertEnter` / `InsertLeave` autocmds
219219
that swap to source on entry and back to render on leave.
220220
- `:MdRender auto off` removes the autocmds and the buffer marker.
221-
The displayed mode is left unchanged.
221+
If the current window is showing the render view of this buffer,
222+
it also swaps back to source so the buffer ends up in the same
223+
state it had before `:MdRender auto on`.
222224
- `:MdRender auto toggle`, or `:MdRender auto` with no second arg,
223-
toggles between on and off.
225+
toggles between on and off. Calling it twice returns the buffer
226+
to its original (auto-off, source displayed) state.
224227

225228
The plugin does not register a |FileType| autocmd of its own. To
226229
enable auto mode for every Markdown buffer, add one yourself: >vim
@@ -531,8 +534,10 @@ MdPreview.auto_on({opts}) ~
531534

532535
*MdPreview.auto_off()*
533536
MdPreview.auto_off() ~
534-
[EXPERIMENTAL] Disable auto-toggle for the current buffer. The
535-
displayed mode is left as-is. See |:MdRender-auto|.
537+
[EXPERIMENTAL] Disable auto-toggle for the current buffer. If the
538+
current window is showing this buffer's render view, it also swaps
539+
back to source so the post-`auto_off` state matches what existed
540+
before |MdPreview.auto_on()|. See |:MdRender-auto|.
536541

537542
*MdPreview.auto_toggle()*
538543
MdPreview.auto_toggle({opts}) ~

lua/md-render/preview.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1954,7 +1954,9 @@ function MdPreview.auto_on(opts)
19541954
end
19551955
end
19561956

1957-
--- Disable auto-toggle for the current buffer; leave the displayed mode untouched.
1957+
--- Disable auto-toggle for the current buffer and, if the current window is
1958+
--- showing this buffer's render view, swap back to source so a single
1959+
--- `auto_off` (or a second `auto_toggle`) returns to the pre-`auto_on` state.
19581960
function MdPreview.auto_off()
19591961
local bufnr = get_auto_target_buf()
19601962
if not vim.b[bufnr].md_render_auto then return end
@@ -1970,6 +1972,12 @@ function MdPreview.auto_off()
19701972

19711973
local session = _toggle_sessions[bufnr]
19721974
if session then uninstall_auto_insert_keymaps(session.buf) end
1975+
1976+
local win = vim.api.nvim_get_current_win()
1977+
local win_state = get_win_state(win)
1978+
if win_state and win_state.mode == "render" and win_state.source_buf == bufnr then
1979+
MdPreview.toggle()
1980+
end
19731981
end
19741982

19751983
--- Flip auto-toggle state for the current buffer.

tests/toggle_test.lua

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,14 +607,14 @@ test("auto-transition to render swaps source → render after 50ms", function()
607607
end)
608608

609609
-- ----------------------------------------------------------------------
610-
-- Test 18: auto_off clears autocmds and leaves displayed mode untouched
610+
-- Test 18: auto_off clears autocmds and restores source display
611611
-- ----------------------------------------------------------------------
612-
test("auto_off clears autocmds and preserves displayed mode", function()
612+
test("auto_off clears autocmds and swaps render back to source", function()
613613
local source = setup_md_buffer({ "# Hello" })
614614
local win = vim.api.nvim_get_current_win()
615615

616616
preview.auto_on()
617-
local before_buf = vim.api.nvim_win_get_buf(win)
617+
assert_false(vim.api.nvim_win_get_buf(win) == source, "precondition: render after auto_on")
618618

619619
preview.auto_off()
620620

@@ -629,7 +629,48 @@ test("auto_off clears autocmds and preserves displayed mode", function()
629629
})
630630
assert_true(not ok or #autocmds == 0, "no auto autocmds should remain")
631631

632-
assert_eq(vim.api.nvim_win_get_buf(win), before_buf, "displayed mode should be unchanged")
632+
assert_eq(vim.api.nvim_win_get_buf(win), source, "should be back on source after auto_off")
633+
634+
cleanup_buffer(source)
635+
end)
636+
637+
-- ----------------------------------------------------------------------
638+
-- Test 18b: auto_toggle twice returns to the original (source, auto-off) state
639+
-- ----------------------------------------------------------------------
640+
test("auto_toggle twice returns to original source/auto-off state", function()
641+
local source = setup_md_buffer({ "# Hello" })
642+
local win = vim.api.nvim_get_current_win()
643+
644+
assert_eq(vim.api.nvim_win_get_buf(win), source, "precondition: source")
645+
assert_eq(vim.b[source].md_render_auto, nil, "precondition: auto off")
646+
647+
preview.auto_toggle()
648+
assert_eq(vim.b[source].md_render_auto, true, "after 1st toggle: auto on")
649+
assert_false(vim.api.nvim_win_get_buf(win) == source, "after 1st toggle: render displayed")
650+
651+
preview.auto_toggle()
652+
assert_eq(vim.b[source].md_render_auto, nil, "after 2nd toggle: auto off")
653+
assert_eq(vim.api.nvim_win_get_buf(win), source, "after 2nd toggle: source displayed")
654+
655+
cleanup_buffer(source)
656+
end)
657+
658+
-- ----------------------------------------------------------------------
659+
-- Test 18c: auto_off leaves displayed mode unchanged when not showing render
660+
-- ----------------------------------------------------------------------
661+
test("auto_off does not swap when current window is not showing render", function()
662+
local source = setup_md_buffer({ "# Hello" })
663+
local win = vim.api.nvim_get_current_win()
664+
665+
preview.auto_on()
666+
-- Manually swap back to source so auto_off has nothing to swap.
667+
preview.toggle()
668+
assert_eq(vim.api.nvim_win_get_buf(win), source, "precondition: source displayed")
669+
670+
preview.auto_off()
671+
672+
assert_eq(vim.b[source].md_render_auto, nil, "b:md_render_auto should be cleared")
673+
assert_eq(vim.api.nvim_win_get_buf(win), source, "should still be on source after auto_off")
633674

634675
cleanup_buffer(source)
635676
end)

0 commit comments

Comments
 (0)