fix: cancel visual/operator-pending mode instead of closing buffer

Problem: when close() is triggered from visual or operator-pending
mode (e.g. pressing q after d in an oil buffer), the buffer closes
instead of canceling the pending operation. This happens because
keymaps without an explicit mode default to "" which covers normal,
visual, select, and operator-pending modes.

Solution: check the current mode at the top of close() and send
<Esc> to cancel the mode instead of proceeding with the close. The
check covers all visual modes (v, V, CTRL-V), select modes (s, S,
CTRL-S), and operator-pending submodes (no, nov, noV, noCTRL-V).

Based on: stevearc/oil.nvim#495
This commit is contained in:
Barrett Ruth 2026-02-20 16:28:26 -05:00
parent 29239d56fb
commit 16f3d7bfa9
2 changed files with 54 additions and 0 deletions

View file

@ -410,6 +410,11 @@ end
---@param opts? oil.CloseOpts
M.close = function(opts)
opts = opts or {}
local mode = vim.api.nvim_get_mode().mode
if mode:match("^[vVsS\22\19]") or mode:match("^no") then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, true, true), "n", false)
return
end
-- If we're in a floating oil window, close it and try to restore focus to the original window
if vim.w.is_oil_win then
local original_winid = vim.w.oil_original_win