Skip to content

Commit

Permalink
src/frame.ts: replace nvim_call_atomic with nvim_exec_lua
Browse files Browse the repository at this point in the history
Fixes #1603, and much cleaner anyway.
  • Loading branch information
glacambre committed Apr 28, 2024
1 parent e99aa60 commit 570d6ec
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,40 @@ export const isReady = browser
window.addEventListener("focus", setCurrentChan);
window.addEventListener("click", setCurrentChan);

const augroupName = `FirenvimAugroupChan${chan}`;
// Cleanup means:
// - notify frontend that we're shutting down
// - delete file
// - remove own augroup
const cleanup = `call rpcnotify(${chan}, 'firenvim_vimleave') | `
+ `call delete('${filename}')`;
// Ask for notifications when user writes/leaves firenvim
nvim.call_atomic((`augroup ${augroupName}
au!
autocmd BufWrite ${filename} `
+ `call rpcnotify(${chan}, `
+ `'firenvim_bufwrite', `
+ `{`
+ `'text': nvim_buf_get_lines(0, 0, -1, 0),`
+ `'cursor': nvim_win_get_cursor(0),`
+ `})
au VimLeave * ${cleanup}
augroup END`).split("\n").map(command => ["nvim_command", [command]]));
nvim.exec_lua(`
local args = {...}
local augroupName = args[1]
local filename = args[2]
local channel = args[3]
local group = vim.api.nvim_create_augroup(augroupName, { clear = true })
vim.api.nvim_create_autocmd("BufWrite", {
group = group,
pattern = filename,
callback = function(ev)
vim.fn.rpcnotify(
channel,
"firenvim_bufwrite",
{
text = vim.api.nvim_buf_get_lines(0, 0, -1, 0),
cursor = vim.api.nvim_win_get_cursor(0)
}
)
end
})
vim.api.nvim_create_autocmd("VimLeave", {
group = group,
callback = function(ev)
-- Cleanup means:
-- - notify frontend that we're shutting down
-- - delete file
-- - remove own augroup
vim.fn.rpcnotify(channel, 'firenvim_vimleave')
vim.fn.delete(filename)
vim.api.nvim_del_augroup_by_id(augroup)
end
})
`, [`FirenvimAugroupChan${chan}`, filename, chan]);

let mouseEnabled = true;
rendererEvents.on("mouseOn", () => {
Expand Down

0 comments on commit 570d6ec

Please sign in to comment.