Skip to content

Commit 79ca923

Browse files
committed
src/frame.ts: replace nvim_call_atomic with nvim_exec_lua
Fixes #1603, and much cleaner anyway.
1 parent e99aa60 commit 79ca923

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

src/frame.ts

+33-18
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,40 @@ export const isReady = browser
107107
window.addEventListener("focus", setCurrentChan);
108108
window.addEventListener("click", setCurrentChan);
109109

110-
const augroupName = `FirenvimAugroupChan${chan}`;
111-
// Cleanup means:
112-
// - notify frontend that we're shutting down
113-
// - delete file
114-
// - remove own augroup
115-
const cleanup = `call rpcnotify(${chan}, 'firenvim_vimleave') | `
116-
+ `call delete('${filename}')`;
117110
// Ask for notifications when user writes/leaves firenvim
118-
nvim.call_atomic((`augroup ${augroupName}
119-
au!
120-
autocmd BufWrite ${filename} `
121-
+ `call rpcnotify(${chan}, `
122-
+ `'firenvim_bufwrite', `
123-
+ `{`
124-
+ `'text': nvim_buf_get_lines(0, 0, -1, 0),`
125-
+ `'cursor': nvim_win_get_cursor(0),`
126-
+ `})
127-
au VimLeave * ${cleanup}
128-
augroup END`).split("\n").map(command => ["nvim_command", [command]]));
111+
nvim.exec_lua(`
112+
local args = {...}
113+
local augroupName = args[1]
114+
local filename = args[2]
115+
local channel = args[3]
116+
local group = vim.api.nvim_create_augroup(augroupName, { clear = true })
117+
vim.api.nvim_create_autocmd("BufWrite", {
118+
group = group,
119+
pattern = filename,
120+
callback = function(ev)
121+
vim.fn.rpcnotify(
122+
channel,
123+
"firenvim_bufwrite",
124+
{
125+
text = vim.api.nvim_buf_get_lines(0, 0, -1, 0),
126+
cursor = vim.api.nvim_win_get_cursor(0)
127+
}
128+
)
129+
end
130+
})
131+
vim.api.nvim_create_autocmd("VimLeave", {
132+
group = group,
133+
callback = function(ev)
134+
-- Cleanup means:
135+
-- - notify frontend that we're shutting down
136+
-- - delete file
137+
-- - remove own augroup
138+
vim.fn.rpcnotify(channel, 'firenvim_vimleave')
139+
vim.fn.delete(filename)
140+
vim.api.nvim_del_augroup_by_id(group)
141+
end
142+
})
143+
`, [`FirenvimAugroupChan${chan}`, filename, chan]);
129144

130145
let mouseEnabled = true;
131146
rendererEvents.on("mouseOn", () => {

0 commit comments

Comments
 (0)