From b05d0a34cffd8b9490609e5ec739e5ca05392c14 Mon Sep 17 00:00:00 2001 From: Maltimore Date: Mon, 16 Jan 2023 21:32:40 +0100 Subject: [PATCH] open tmp window: handle case where tmp window is prev window --- lua/orgmode/utils/init.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lua/orgmode/utils/init.lua b/lua/orgmode/utils/init.lua index 22656c594..f3d132075 100644 --- a/lua/orgmode/utils/init.lua +++ b/lua/orgmode/utils/init.lua @@ -536,10 +536,12 @@ end function utils.open_tmp_org_window(height, split_mode, on_close) local winnr = vim.api.nvim_get_current_win() + local bufnr = vim.api.nvim_get_current_buf() utils.open_window(vim.fn.tempname(), height or 16, split_mode) vim.cmd([[setf org]]) vim.cmd([[setlocal bufhidden=wipe nobuflisted nolist noswapfile nofoldenable]]) vim.api.nvim_buf_set_var(0, 'org_prev_window', winnr) + vim.api.nvim_buf_set_var(0, 'org_prev_buffer', bufnr) if on_close then vim.api.nvim_create_autocmd('BufWipeout', { @@ -559,9 +561,14 @@ function utils.open_tmp_org_window(height, split_mode, on_close) return function() vim.api.nvim_create_augroup('OrgTmpWindow', { clear = true }) local prev_winnr = vim.api.nvim_buf_get_var(0, 'org_prev_window') - vim.api.nvim_win_close(0, true) - if prev_winnr and vim.api.nvim_win_is_valid(prev_winnr) then - vim.api.nvim_set_current_win(prev_winnr) + if prev_winnr ~= vim.api.nvim_get_current_win() then + vim.api.nvim_win_close(0, true) + if prev_winnr and vim.api.nvim_win_is_valid(prev_winnr) then + vim.api.nvim_set_current_win(prev_winnr) + end + else + local prev_bufnr = vim.api.nvim_buf_get_var(0, 'org_prev_buffer') + vim.api.nvim_set_current_buf(prev_bufnr) end end end