diff --git a/README.md b/README.md index daaeb6f..280ca30 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 4a74d9c..802ec14 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 4ef1f06..f1041c4 100644 --- a/lua/femaco/edit.lua +++ b/lua/femaco/edit.lua @@ -296,7 +296,6 @@ M.edit_code_block = function() return end local match_lines = vim.split(get_match_text(match_data.content, 0), "\n") - local filetype = settings.ft_from_lang(match_data.lang) local indent = nil local lines_for_edit = match_lines @@ -315,7 +314,9 @@ M.edit_code_block = function() lang = match_data.lang, })) - vim.cmd("file " .. settings.create_tmp_filepath(filetype)) + local filetype = settings.ft_from_lang(match_data.lang) + 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, lines_for_edit) @@ -327,9 +328,13 @@ 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 event.event == 'WinClosed' then + settings.post_close_float(tmp_filepath) + end + if tbl_equal(lines_for_edit, lines) then return -- unmodified end