Skip to content

Commit d1d6eff

Browse files
fix: Wipe unused temp buffer on remote edit
1 parent de26d82 commit d1d6eff

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

lua/orgmode/capture/init.lua

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,26 +346,17 @@ function Capture:_refile_to(opts)
346346
apply_properties(opts)
347347

348348
local is_same_file = opts.file == utils.current_file_path()
349-
local cur_win = vim.api.nvim_get_current_win()
350349

351350
local item = opts.item
352351
if is_same_file and item then
353352
vim.cmd(string.format('silent! %d,%d move %s', item.range.start_line, item.range.end_line, tostring(opts.file)))
354353
return true
355354
end
356355

356+
local edit_file = utils.edit_file(opts.file)
357+
357358
if not is_same_file then
358-
local bufnr = vim.fn.bufadd(opts.file)
359-
vim.api.nvim_open_win(bufnr, true, {
360-
relative = 'editor',
361-
width = 1,
362-
-- TODO: Revert to 1 once the https://github.com/neovim/neovim/issues/19464 is fixed
363-
height = 2,
364-
row = 99999,
365-
col = 99999,
366-
zindex = 1,
367-
style = 'minimal',
368-
})
359+
edit_file.open()
369360
end
370361

371362
remove_buffer_empty_lines(opts)
@@ -374,8 +365,7 @@ function Capture:_refile_to(opts)
374365
vim.api.nvim_buf_set_lines(0, range.start_line, range.end_line, false, opts.lines)
375366

376367
if not is_same_file then
377-
vim.cmd('silent! wq!')
378-
vim.api.nvim_set_current_win(cur_win)
368+
edit_file.close()
379369
end
380370

381371
if item and item.file == utils.current_file_path() then

lua/orgmode/parser/files.lua

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,11 @@ function Files.update_file(filename, action)
186186
end)
187187
end
188188

189-
local bufnr = vim.fn.bufadd(filename)
190-
vim.api.nvim_open_win(bufnr, true, {
191-
relative = 'editor',
192-
width = 1,
193-
-- TODO: Revert to 1 once the https://github.com/neovim/neovim/issues/19464 is fixed
194-
height = 2,
195-
row = 99999,
196-
col = 99999,
197-
zindex = 1,
198-
style = 'minimal',
199-
})
189+
local edit_file = utils.edit_file(filename)
190+
edit_file.open()
200191

201192
return Promise.resolve(action(file)):next(function(result)
202-
vim.cmd('silent! wq!')
203-
vim.api.nvim_set_current_win(cur_win)
193+
edit_file.close()
204194
return result
205195
end)
206196
end

lua/orgmode/utils/init.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,35 @@ function utils.pad_right(str, amount)
596596
return string.format('%s%s', str, string.rep(' ', spaces))
597597
end
598598

599+
---@param filename string
600+
function utils.edit_file(filename)
601+
local buf_not_already_loaded = vim.fn.bufexists(filename) ~= 1
602+
local cur_win = vim.api.nvim_get_current_win()
603+
604+
return {
605+
open = function()
606+
local bufnr = vim.fn.bufadd(filename)
607+
vim.api.nvim_open_win(bufnr, true, {
608+
relative = 'editor',
609+
width = 1,
610+
-- TODO: Revert to 1 once the https://github.com/neovim/neovim/issues/19464 is fixed
611+
height = 2,
612+
row = 99999,
613+
col = 99999,
614+
zindex = 1,
615+
style = 'minimal',
616+
})
617+
end,
618+
close = function()
619+
vim.cmd('silent! w')
620+
if buf_not_already_loaded then
621+
vim.cmd('silent! bw!')
622+
else
623+
vim.cmd('silent! q!')
624+
end
625+
vim.api.nvim_set_current_win(cur_win)
626+
end,
627+
}
628+
end
629+
599630
return utils

0 commit comments

Comments
 (0)