Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions lua/femaco/edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down