Skip to content

Commit f6314c6

Browse files
committed
fix: set alternate when using floating windows (#285)
1 parent 3c2de37 commit f6314c6

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lua/oil/init.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,25 @@ M.select = function(opts, callback)
617617
if callback then
618618
callback(err)
619619
end
620+
621+
-- Set alternate for buffer
622+
local has_orig, orig_buffer = pcall(vim.api.nvim_win_get_var, 0, "oil_original_buffer")
623+
624+
if has_orig and vim.api.nvim_buf_is_valid(orig_buffer) then
625+
if vim.api.nvim_get_current_buf() ~= orig_buffer then
626+
-- If we are editing a new file after navigating around oil, set the alternate buffer
627+
-- to be the last buffer we were in before opening oil
628+
vim.fn.setreg("#", orig_buffer)
629+
else
630+
-- If we are editing the same buffer that we started oil from, set the alternate to be
631+
-- what it was before we opened oil
632+
local has_orig_alt, alt_buffer =
633+
pcall(vim.api.nvim_win_get_var, 0, "oil_original_alternate")
634+
if has_orig_alt and vim.api.nvim_buf_is_valid(alt_buffer) then
635+
vim.fn.setreg("#", alt_buffer)
636+
end
637+
end
638+
end
620639
end
621640
if not opts.split and (opts.horizontal or opts.vertical) then
622641
if opts.horizontal then

tests/altbuf_spec.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,17 @@ a.describe("Alternate buffer", function()
141141
oil.close()
142142
assert.equals("foo", vim.fn.expand("#"))
143143
end)
144+
145+
a.it("preserves alternate when traversing to a new file", function()
146+
vim.cmd.edit({ args = { "foo" } })
147+
oil.open_float()
148+
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
149+
assert.equals("foo", vim.fn.expand("#"))
150+
local last_item = vim.fn.line("$")
151+
vim.api.nvim_win_set_cursor(0, { last_item, 5 })
152+
oil.select()
153+
test_util.wait_for_autocmd("BufEnter")
154+
assert.equals("foo", vim.fn.expand("#"))
155+
end)
144156
end)
145157
end)

0 commit comments

Comments
 (0)