diff --git a/README.ja.md b/README.ja.md index 1ad475d..af95992 100644 --- a/README.ja.md +++ b/README.ja.md @@ -151,9 +151,9 @@ vim.keymap.set("n", "md", "(md-render-demo)", { desc = "Mar | キー | 動作 | |---|---| | `za` | カーソル行の折りたたみ / 展開ブロックを切り替える(該当しない行では何もしない) | -| `` | カーソル行の折りたたみ / 展開ブロックを切り替える。該当しなければウィンドウを閉じる(フローティング / タブモードのみ) | +| `` | カーソル行の折りたたみ / 展開ブロックを切り替える(該当しない行では何もしない) | | `` | クリックで折りたたみ・展開を切り替え、リンクを開く | -| `q` / `` | ウィンドウを閉じる(フローティング / タブモードのみ) | +| `q` / `` / `` | ウィンドウを閉じる(フローティング / タブモードのみ) | ## コマンド @@ -189,7 +189,7 @@ vim.keymap.set("n", "md", "(md-render-demo)", { desc = "Mar - 同じソースが複数のウィンドウで表示されている場合、切替はそのウィンドウだけに作用し、他のウィンドウの編集は次に当該ウィンドウをレンダーモードにした時点で反映されます。 - カーソル位置は source line マップを介してソース ↔ レンダー間を往復します。 - レンダーモードのウィンドウでは `number` / `relativenumber` / `list` が無効化されます。元の値はウィンドウに保存され、ソースに戻したときに復元されます。 -- レンダーモード中、`q` / `` / `` は閉じる動作に**割り当てられません**。ソースに戻すには再度 `:MdRender toggle` を呼びます。`` / `za` / `` は折りたたみ・展開を引き続き切り替えます(リンク open は ``)。 +- レンダーモード中、`q` / `` / `` は閉じる動作に**割り当てられません**。ソースに戻すには再度 `:MdRender toggle` を呼びます。`` / `za` / `` は折りたたみ・展開を引き続き切り替えます(リンク open は ``)。 ### Insert モード連動の自動トグル(実験的) diff --git a/README.md b/README.md index 342fe59..c671bd7 100644 --- a/README.md +++ b/README.md @@ -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) | -| `` | Toggle the fold / expandable region under the cursor; otherwise close the window (in floating / tab mode) | +| `` | Toggle the fold / expandable region under the cursor (no-op elsewhere) | | `` | Toggle folds, expand regions, and open links by clicking | -| `q` / `` | Close the window (floating / tab mode only) | +| `q` / `` / `` | Close the window (floating / tab mode only) | ## Commands @@ -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` / `` / `` are **not** bound to close — call `:MdRender toggle` again to return to source mode. ``, `za`, and `` still toggle folds and expand regions (and `` opens links). +- Inside render mode, `q` / `` / `` are **not** bound to close — call `:MdRender toggle` again to return to source mode. ``, `za`, and `` still toggle folds and expand regions (and `` opens links). ### Auto-toggle on Insert mode (experimental) diff --git a/doc/md-render.jax b/doc/md-render.jax index 9a3623c..aa6c0a1 100644 --- a/doc/md-render.jax +++ b/doc/md-render.jax @@ -138,11 +138,11 @@ lazy.nvim の場合: >lua プレビューウィンドウ内では以下のキーが使えます: - `q` / `` プレビューウィンドウを閉じる + `q` / `` / `` プレビューウィンドウを閉じる `za` カーソル行のコールアウトの折りたたみ / 省略領域の 展開を切り替える (該当しない行では何もしない) `` カーソル行のコールアウトの折りたたみ / 省略領域の - 展開を切り替える。該当しなければウィンドウを閉じる + 展開を切り替える (該当しない行では何もしない) `` リンクのクリック、折りたたみ、領域展開 `` マウスをリンクに乗せると、エディタ下部の小さな フロートウィンドウに完全な URL を表示。 @@ -150,7 +150,7 @@ lazy.nvim の場合: >lua ウィンドウが開かれている間、自動で有効化する)。 |:MdRender-toggle| で開かれたレンダーモードのバッファでは `q` / `` / -`` は閉じる動作に**割り当てられません**。ソースに戻すには再度 +`` は閉じる動作に**割り当てられません**。ソースに戻すには再度 |:MdRender-toggle| を呼びます。`` / `za` / `` は折りたたみ・ 展開を引き続き切り替え (リンク open は ``)、`` の URL 表示も有効なままです。 diff --git a/doc/md-render.txt b/doc/md-render.txt index c240c54..0d43bda 100644 --- a/doc/md-render.txt +++ b/doc/md-render.txt @@ -138,11 +138,11 @@ Map them yourself: >lua Inside the preview window, the following keys are available: - `q` / `` Close the preview window + `q` / `` / `` Close the preview window `za` Toggle the callout fold / expandable region under the cursor (no-op when the cursor is not on one) `` Toggle the callout fold / expandable region under the - cursor; otherwise close the preview window + cursor (no-op when the cursor is not on one) `` Click links, toggle folds, expand regions `` Hover a link to peek its full URL in a small floating window at the bottom of the editor. Requires @@ -150,7 +150,7 @@ Inside the preview window, the following keys are available: whenever a preview window is open). Inside a render-mode buffer opened with |:MdRender-toggle|, `q` / `` / -`` are NOT bound to close — call |:MdRender-toggle| again to return to +`` are NOT bound to close — call |:MdRender-toggle| again to return to source mode. ``, `za`, and `` still toggle folds and expand regions (and `` opens links); `` hover-peek also stays active. diff --git a/lua/md-render/display_utils.lua b/lua/md-render/display_utils.lua index 2711767..b20d5f7 100644 --- a/lua/md-render/display_utils.lua +++ b/lua/md-render/display_utils.lua @@ -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", "", "" } + local close_keys = opts.close_keys or { "q", "", "" } local cr_is_close = vim.tbl_contains(close_keys, "") for _, key in ipairs(close_keys) do -- is bound below so it can toggle a block under the cursor first and @@ -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 }) - -- `` 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. + -- `` 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 / + -- / ). It still falls back to closing when a caller opts into + -- close_keys explicitly (cr_is_close). vim.keymap.set("n", "", function() if toggle_at_cursor() then return end if cr_is_close then vim.cmd.close() end diff --git a/tests/key_toggle_test.lua b/tests/key_toggle_test.lua index d364419..1db1254 100644 --- a/tests/key_toggle_test.lua +++ b/tests/key_toggle_test.lua @@ -145,20 +145,43 @@ test("za on plain line is a no-op", function() end) -- ---------------------------------------------------------------------- --- Test 5: `` on a plain line (with as a close key) closes the window +-- Test 5: `` on a plain line does NOT close the window by default +-- (Enter is no longer a close key — closing on Enter is unintuitive). -- ---------------------------------------------------------------------- -test(" on plain line closes the window when is a close key", function() - local buf, win, calls = setup(make_content()) -- default close_keys include +test(" on plain line does not close the window by default", function() + local buf, win, calls = setup(make_content()) -- default close_keys: q / / vim.api.nvim_win_set_cursor(win, { 4, 0 }) feed "" - assert_false(vim.api.nvim_win_is_valid(win), " on plain line: window closed") + assert_true(vim.api.nvim_win_is_valid(win), " on plain line: window stays open") assert_eq(#calls.fold, 0, " on plain line: fold not fired") assert_eq(#calls.expand, 0, " on plain line: expand not fired") cleanup(win, buf) end) -- ---------------------------------------------------------------------- --- Test 6: in toggle mode (no close keys), on a plain line is a no-op +-- Test 6: `` on a plain line closes the window (default close key) +-- ---------------------------------------------------------------------- +test(" on plain line closes the window", function() + local buf, win, _ = setup(make_content()) -- default close_keys include + vim.api.nvim_win_set_cursor(win, { 4, 0 }) + feed "" + assert_false(vim.api.nvim_win_is_valid(win), " 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), on a plain line is a no-op -- ---------------------------------------------------------------------- test(" on plain line is a no-op in toggle mode", function() local buf, win, calls = setup(make_content(), {}) @@ -169,6 +192,18 @@ test(" on plain line is a no-op in toggle mode", function() cleanup(win, buf) end) +-- ---------------------------------------------------------------------- +-- Test 9: when a caller opts into close_keys, still closes +-- on a plain line (cr_is_close fallback path remains supported). +-- ---------------------------------------------------------------------- +test(" still closes when explicitly a close key", function() + local buf, win, _ = setup(make_content(), { "" }) + vim.api.nvim_win_set_cursor(win, { 4, 0 }) + feed "" + assert_false(vim.api.nvim_win_is_valid(win), " 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)