From f1131b5e90ce5cdbbd122e298d62726dfa4b808a Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 24 Feb 2023 06:20:56 -0800 Subject: [PATCH] fix: set alternate buffer when inside oil (#60) --- lua/oil/init.lua | 6 ++++++ tests/altbuf_spec.lua | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 28968693..f52f45d4 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -709,6 +709,12 @@ M.setup = function(opts) local scheme = util.parse_url(bufname) if scheme and config.adapters[scheme] then require("oil.view").maybe_set_cursor() + -- While we are in an oil buffer, set the alternate file to the buffer we were in prior to + -- opening oil + local has_orig, orig_buffer = pcall(vim.api.nvim_win_get_var, 0, "oil_original_buffer") + if has_orig and vim.api.nvim_buf_is_valid(orig_buffer) then + vim.fn.setreg("#", orig_buffer) + end elseif vim.fn.isdirectory(bufname) == 0 then -- Only run this logic if we are *not* in an oil buffer (and it's not a directory, which -- will be replaced by an oil:// url) diff --git a/tests/altbuf_spec.lua b/tests/altbuf_spec.lua index 0c319af5..3c0113ff 100644 --- a/tests/altbuf_spec.lua +++ b/tests/altbuf_spec.lua @@ -70,6 +70,17 @@ a.describe("Alternate buffer", function() assert.equals("foo", vim.fn.expand("#")) end) + a.it("sets previous buffer as alternate when inside oil buffer", function() + vim.cmd.edit({ args = { "foo" } }) + oil.open() + test_util.wait_for_autocmd("BufReadPost") + assert.equals("foo", vim.fn.expand("#")) + vim.cmd.edit({ args = { "bar" } }) + assert.equals("foo", vim.fn.expand("#")) + oil.open() + assert.equals("bar", vim.fn.expand("#")) + end) + a.describe("floating window", function() a.it("sets previous buffer as alternate", function() vim.cmd.edit({ args = { "foo" } })