From 0acbf9bf72cbbdd9c9b02854ccbcaf10c455c45d Mon Sep 17 00:00:00 2001 From: Christoffer Klang Date: Sun, 15 Oct 2023 00:08:53 +0200 Subject: [PATCH 1/3] Add post_close_float() And set the default implementation to delete the temp file. --- README.md | 4 ++++ lua/femaco/config.lua | 4 ++++ lua/femaco/edit.lua | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 564043f..509068e 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,10 @@ require('femaco').setup({ post_open_float = function(winnr) vim.wo.signcolumn = 'no' end + -- what to do after closing the float + post_close_float = function(tmp_filepath) + vim.loop.fs_unlink(tmp_filepath) + end, -- create the path to a temporary file create_tmp_filepath = function(filetype) return os.tmpname() diff --git a/lua/femaco/config.lua b/lua/femaco/config.lua index 9126a2a..27d3b8a 100644 --- a/lua/femaco/config.lua +++ b/lua/femaco/config.lua @@ -42,6 +42,10 @@ M.settings = { create_tmp_filepath = function(filetype) return os.tmpname() end, + -- what to do after closing the float + post_close_float = function(tmp_filepath) + vim.loop.fs_unlink(tmp_filepath) + end, -- if a newline should always be used, useful for multiline injections -- which separators needs to be on separate lines such as markdown, neorg etc -- @param base_filetype: The filetype which FeMaco is called from, not the diff --git a/lua/femaco/edit.lua b/lua/femaco/edit.lua index cc5fdfa..e9659dc 100644 --- a/lua/femaco/edit.lua +++ b/lua/femaco/edit.lua @@ -177,7 +177,8 @@ M.edit_code_block = function() })) local filetype = settings.ft_from_lang(match_data.lang) - vim.cmd('file ' .. settings.create_tmp_filepath(filetype)) + local tmp_filepath = settings.create_tmp_filepath(filetype) + vim.cmd('file ' .. tmp_filepath) vim.bo.filetype = filetype vim.api.nvim_buf_set_lines(vim.fn.bufnr(), 0, -1, true, match_lines) -- use nvim_exec to do this silently @@ -188,10 +189,15 @@ M.edit_code_block = function() local float_bufnr = vim.fn.bufnr() vim.api.nvim_create_autocmd({'BufWritePost', 'WinClosed'}, { buffer = 0, - callback = function() + callback = function(event) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, true) - if tbl_equal(match_lines, lines) then return end + if tbl_equal(match_lines, lines) then + if event.event == 'WinClosed' then + settings.post_close_float(tmp_filepath) + end + return + end if lines[#lines] ~= '' and settings.ensure_newline(base_filetype) then table.insert(lines, '') @@ -199,6 +205,9 @@ M.edit_code_block = function() local sr, sc, er, ec = unpack(range) vim.api.nvim_buf_set_text(bufnr, sr, sc, er, ec, lines) update_range(range, lines) + if event.event == 'WinClosed' then + settings.post_close_float(tmp_filepath) + end end, }) -- make sure the buffer is deleted when we close the window From 221950ad737fb8fe518c6d59cf6c457538601f4f Mon Sep 17 00:00:00 2001 From: Christoffer Klang Date: Sun, 5 Nov 2023 11:50:02 +0100 Subject: [PATCH 2/3] Unconditionally trigger post_close_float on WinClosed --- lua/femaco/edit.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lua/femaco/edit.lua b/lua/femaco/edit.lua index f5bab87..e8793b8 100644 --- a/lua/femaco/edit.lua +++ b/lua/femaco/edit.lua @@ -331,10 +331,11 @@ M.edit_code_block = function() callback = function(event) local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, true) + if event.event == 'WinClosed' then + settings.post_close_float(tmp_filepath) + end + if tbl_equal(lines_for_edit, lines) then - if event.event == 'WinClosed' then - settings.post_close_float(tmp_filepath) - end return end @@ -348,9 +349,6 @@ M.edit_code_block = function() end vim.api.nvim_buf_set_text(bufnr, sr, sc, er, ec, lines) update_range(range, lines) - if event.event == 'WinClosed' then - settings.post_close_float(tmp_filepath) - end end, }) -- make sure the buffer is deleted when we close the window From f0e5e2425bde522cbeb570f8af9f7cd459126b0d Mon Sep 17 00:00:00 2001 From: Christoffer Klang Date: Sun, 5 Nov 2023 11:51:19 +0100 Subject: [PATCH 3/3] Restore unintentional merge change --- lua/femaco/edit.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/femaco/edit.lua b/lua/femaco/edit.lua index e8793b8..f1041c4 100644 --- a/lua/femaco/edit.lua +++ b/lua/femaco/edit.lua @@ -336,7 +336,7 @@ M.edit_code_block = function() end if tbl_equal(lines_for_edit, lines) then - return + return -- unmodified end if lines[#lines] ~= "" and settings.ensure_newline(base_filetype) then