Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions lua/femaco/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions lua/femaco/edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -188,17 +189,25 @@ 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be moved before checking if nothing changed to avoid having this clause both here and below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess nothing relies on the file content in this callback, so that makes sense. Good call!

settings.post_close_float(tmp_filepath)
end
return
end

if lines[#lines] ~= '' and settings.ensure_newline(base_filetype) then
table.insert(lines, '')
end
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
Expand Down