Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ vim.keymap.set("n", "<leader>md", "<Plug>(md-render-demo)", { desc = "Mar
| キー | 動作 |
|---|---|
| `za` | カーソル行の折りたたみ / 展開ブロックを切り替える(該当しない行では何もしない) |
| `<CR>` | カーソル行の折りたたみ / 展開ブロックを切り替える。該当しなければウィンドウを閉じる(フローティング / タブモードのみ) |
| `<CR>` | カーソル行の折りたたみ / 展開ブロックを切り替える(該当しない行では何もしない) |
| `<LeftMouse>` | クリックで折りたたみ・展開を切り替え、リンクを開く |
| `q` / `<Esc>` | ウィンドウを閉じる(フローティング / タブモードのみ) |
| `q` / `<Esc>` / `<C-c>` | ウィンドウを閉じる(フローティング / タブモードのみ) |

## コマンド

Expand Down Expand Up @@ -189,7 +189,7 @@ vim.keymap.set("n", "<leader>md", "<Plug>(md-render-demo)", { desc = "Mar
- 同じソースが複数のウィンドウで表示されている場合、切替はそのウィンドウだけに作用し、他のウィンドウの編集は次に当該ウィンドウをレンダーモードにした時点で反映されます。
- カーソル位置は source line マップを介してソース ↔ レンダー間を往復します。
- レンダーモードのウィンドウでは `number` / `relativenumber` / `list` が無効化されます。元の値はウィンドウに保存され、ソースに戻したときに復元されます。
- レンダーモード中、`q` / `<Esc>` / `<CR>` は閉じる動作に**割り当てられません**。ソースに戻すには再度 `:MdRender toggle` を呼びます。`<LeftMouse>` / `za` / `<CR>` は折りたたみ・展開を引き続き切り替えます(リンク open は `<LeftMouse>`)。
- レンダーモード中、`q` / `<Esc>` / `<C-c>` は閉じる動作に**割り当てられません**。ソースに戻すには再度 `:MdRender toggle` を呼びます。`<LeftMouse>` / `za` / `<CR>` は折りたたみ・展開を引き続き切り替えます(リンク open は `<LeftMouse>`)。

### Insert モード連動の自動トグル(実験的)

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ Inside a rendered preview (floating, tab, or in-place toggle), these buffer-loca
| Key | Action |
|---|---|
| `za` | Toggle the fold / expandable region under the cursor (no-op elsewhere) |
| `<CR>` | Toggle the fold / expandable region under the cursor; otherwise close the window (in floating / tab mode) |
| `<CR>` | Toggle the fold / expandable region under the cursor (no-op elsewhere) |
| `<LeftMouse>` | Toggle folds, expand regions, and open links by clicking |
| `q` / `<Esc>` | Close the window (floating / tab mode only) |
| `q` / `<Esc>` / `<C-c>` | Close the window (floating / tab mode only) |

## Commands

Expand Down Expand Up @@ -189,7 +189,7 @@ Behavior:
- When the same source is shown in multiple windows, only the invoking window swaps; edits from other windows are reflected on the next toggle into render mode.
- Cursor position round-trips between source and render via the source-line mapping.
- `number`, `relativenumber`, and `list` are turned off on render-mode windows. The originals are stashed on the window and restored when toggling back to source.
- Inside render mode, `q` / `<Esc>` / `<CR>` are **not** bound to close — call `:MdRender toggle` again to return to source mode. `<LeftMouse>`, `za`, and `<CR>` still toggle folds and expand regions (and `<LeftMouse>` opens links).
- Inside render mode, `q` / `<Esc>` / `<C-c>` are **not** bound to close — call `:MdRender toggle` again to return to source mode. `<LeftMouse>`, `za`, and `<CR>` still toggle folds and expand regions (and `<LeftMouse>` opens links).

### Auto-toggle on Insert mode (experimental)

Expand Down
6 changes: 3 additions & 3 deletions doc/md-render.jax
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@ lazy.nvim の場合: >lua

プレビューウィンドウ内では以下のキーが使えます:

`q` / `<Esc>` プレビューウィンドウを閉じる
`q` / `<Esc>` / `<C-c>` プレビューウィンドウを閉じる
`za` カーソル行のコールアウトの折りたたみ / 省略領域の
展開を切り替える (該当しない行では何もしない)
`<CR>` カーソル行のコールアウトの折りたたみ / 省略領域の
展開を切り替える。該当しなければウィンドウを閉じる
展開を切り替える (該当しない行では何もしない)
`<LeftMouse>` リンクのクリック、折りたたみ、領域展開
`<MouseMove>` マウスをリンクに乗せると、エディタ下部の小さな
フロートウィンドウに完全な URL を表示。
'mousemoveevent' が必要 (md-render はプレビュー
ウィンドウが開かれている間、自動で有効化する)。

|:MdRender-toggle| で開かれたレンダーモードのバッファでは `q` / `<Esc>` /
`<CR>` は閉じる動作に**割り当てられません**。ソースに戻すには再度
`<C-c>` は閉じる動作に**割り当てられません**。ソースに戻すには再度
|:MdRender-toggle| を呼びます。`<LeftMouse>` / `za` / `<CR>` は折りたたみ・
展開を引き続き切り替え (リンク open は `<LeftMouse>`)、`<MouseMove>` の URL
表示も有効なままです。
Expand Down
6 changes: 3 additions & 3 deletions doc/md-render.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@ Map them yourself: >lua

Inside the preview window, the following keys are available:

`q` / `<Esc>` Close the preview window
`q` / `<Esc>` / `<C-c>` Close the preview window
`za` Toggle the callout fold / expandable region under the
cursor (no-op when the cursor is not on one)
`<CR>` Toggle the callout fold / expandable region under the
cursor; otherwise close the preview window
cursor (no-op when the cursor is not on one)
`<LeftMouse>` Click links, toggle folds, expand regions
`<MouseMove>` Hover a link to peek its full URL in a small floating
window at the bottom of the editor. Requires
'mousemoveevent' (md-render enables it automatically
whenever a preview window is open).

Inside a render-mode buffer opened with |:MdRender-toggle|, `q` / `<Esc>` /
`<CR>` are NOT bound to close — call |:MdRender-toggle| again to return to
`<C-c>` are NOT bound to close — call |:MdRender-toggle| again to return to
source mode. `<LeftMouse>`, `za`, and `<CR>` still toggle folds and expand
regions (and `<LeftMouse>` opens links); `<MouseMove>` hover-peek also stays
active.
Expand Down
8 changes: 5 additions & 3 deletions lua/md-render/display_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function M.setup_float_keymaps(buf, ns, win, content, close_handle, opts)
return content
end

local close_keys = opts.close_keys or { "q", "<Esc>", "<CR>" }
local close_keys = opts.close_keys or { "q", "<Esc>", "<C-c>" }
local cr_is_close = vim.tbl_contains(close_keys, "<CR>")
for _, key in ipairs(close_keys) do
-- <CR> is bound below so it can toggle a block under the cursor first and
Expand Down Expand Up @@ -374,8 +374,10 @@ function M.setup_float_keymaps(buf, ns, win, content, close_handle, opts)
-- it buffer-locally also suppresses Vim's default "E490: No fold found".
vim.keymap.set("n", "za", toggle_at_cursor, { buffer = buf, noremap = true, silent = true })

-- `<CR>` toggles the block under the cursor; otherwise it closes the window
-- when it is a close key (float/tab mode), and is a no-op in toggle mode.
-- `<CR>` toggles the block under the cursor and is otherwise a no-op: it is
-- not a close key by default (closing on Enter is unintuitive — use q / <Esc>
-- / <C-c>). It still falls back to closing when a caller opts <CR> into
-- close_keys explicitly (cr_is_close).
vim.keymap.set("n", "<CR>", function()
if toggle_at_cursor() then return end
if cr_is_close then vim.cmd.close() end
Expand Down
45 changes: 40 additions & 5 deletions tests/key_toggle_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,43 @@ test("za on plain line is a no-op", function()
end)

-- ----------------------------------------------------------------------
-- Test 5: `<CR>` on a plain line (with <CR> as a close key) closes the window
-- Test 5: `<CR>` on a plain line does NOT close the window by default
-- (Enter is no longer a close key — closing on Enter is unintuitive).
-- ----------------------------------------------------------------------
test("<CR> on plain line closes the window when <CR> is a close key", function()
local buf, win, calls = setup(make_content()) -- default close_keys include <CR>
test("<CR> on plain line does not close the window by default", function()
local buf, win, calls = setup(make_content()) -- default close_keys: q / <Esc> / <C-c>
vim.api.nvim_win_set_cursor(win, { 4, 0 })
feed "<CR>"
assert_false(vim.api.nvim_win_is_valid(win), "<CR> on plain line: window closed")
assert_true(vim.api.nvim_win_is_valid(win), "<CR> on plain line: window stays open")
assert_eq(#calls.fold, 0, "<CR> on plain line: fold not fired")
assert_eq(#calls.expand, 0, "<CR> on plain line: expand not fired")
cleanup(win, buf)
end)

-- ----------------------------------------------------------------------
-- Test 6: in toggle mode (no close keys), <CR> on a plain line is a no-op
-- Test 6: `<C-c>` on a plain line closes the window (default close key)
-- ----------------------------------------------------------------------
test("<C-c> on plain line closes the window", function()
local buf, win, _ = setup(make_content()) -- default close_keys include <C-c>
vim.api.nvim_win_set_cursor(win, { 4, 0 })
feed "<C-c>"
assert_false(vim.api.nvim_win_is_valid(win), "<C-c> on plain line: window closed")
cleanup(win, buf)
end)

-- ----------------------------------------------------------------------
-- Test 7: `q` on a plain line closes the window (default close key)
-- ----------------------------------------------------------------------
test("q on plain line closes the window", function()
local buf, win, _ = setup(make_content())
vim.api.nvim_win_set_cursor(win, { 4, 0 })
feed "q"
assert_false(vim.api.nvim_win_is_valid(win), "q on plain line: window closed")
cleanup(win, buf)
end)

-- ----------------------------------------------------------------------
-- Test 8: in toggle mode (no close keys), <CR> on a plain line is a no-op
-- ----------------------------------------------------------------------
test("<CR> on plain line is a no-op in toggle mode", function()
local buf, win, calls = setup(make_content(), {})
Expand All @@ -169,6 +192,18 @@ test("<CR> on plain line is a no-op in toggle mode", function()
cleanup(win, buf)
end)

-- ----------------------------------------------------------------------
-- Test 9: when a caller opts <CR> into close_keys, <CR> still closes
-- on a plain line (cr_is_close fallback path remains supported).
-- ----------------------------------------------------------------------
test("<CR> still closes when explicitly a close key", function()
local buf, win, _ = setup(make_content(), { "<CR>" })
vim.api.nvim_win_set_cursor(win, { 4, 0 })
feed "<CR>"
assert_false(vim.api.nvim_win_is_valid(win), "<CR> as close key: window closed")
cleanup(win, buf)
end)

print(string.format("key_toggle_test: %d passed, %d failed", pass_count, fail_count))
if fail_count > 0 then
os.exit(1)
Expand Down
Loading