Skip to content

Commit 5d80445

Browse files
committed
feat(init): added tracking of watched buffers
1 parent b31e7e5 commit 5d80445

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

lua/markmap/init.lua

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ M.setup = function(opts)
1515
local config = vim.g.markmap_config
1616
local job = nil
1717
local arguments = {}
18+
local lookup_table = {}
1819

1920
-- Setup commands -----------------------------------------------------------
2021
cmd("MarkmapOpen", function()
@@ -37,30 +38,65 @@ M.setup = function(opts)
3738
cmd(
3839
"MarkmapWatch",
3940
function()
41+
local watch_buffer = vim.api.nvim_get_current_buf()
4042
config = vim.g.markmap_config
4143
arguments = utils.reset_arguments()
4244
table.insert(arguments, "--watch") -- spetific to this command
4345
table.insert(arguments, vim.fn.expand("%:p")) -- current buffer path
44-
if job ~= nil then jobstop(job) end -- kill jobs
45-
job = jobstart(config.markmap_cmd, arguments)
4646

47-
-- Register buffer local autocmd to kill job when buffer closes
48-
local kill_on_close = augroup("markmap_kill_on_close", { clear = true })
49-
autocmd("BufDelete", {
50-
buffer = 0,
51-
desc = "Kill markmap when watched buffer is closed",
52-
group = kill_on_close,
53-
callback = function()
54-
jobstop(job)
55-
api.nvim_clear_autocmds({ group = kill_on_close })
56-
end,
57-
})
47+
if lookup_table[watch_buffer] then
48+
vim.notify("You're already watching this buffer.", vim.log.levels.WARN)
49+
else
50+
local kill_on_close =
51+
augroup("markmap_kill_on_close", { clear = false })
52+
53+
job = jobstart(config.markmap_cmd, arguments, {
54+
stderr_buffered = true, -- needed so on_stderr is only called once
55+
on_stderr = function(_, data)
56+
local message = table.concat(data, "\n")
57+
message = message:gsub("\r", "")
58+
vim.notify(message, vim.log.levels.ERROR)
59+
60+
lookup_table[watch_buffer] = nil
61+
api.nvim_clear_autocmds({
62+
group = kill_on_close,
63+
buffer = watch_buffer,
64+
})
65+
end,
66+
})
67+
68+
lookup_table[watch_buffer] = job
69+
70+
-- Register buffer local autocmd to kill job when buffer closes
71+
autocmd("BufDelete", {
72+
desc = "Kill markmap when watched buffer is closed",
73+
buffer = watch_buffer,
74+
group = kill_on_close,
75+
callback = function()
76+
jobstop(job)
77+
lookup_table[watch_buffer] = nil
78+
api.nvim_clear_autocmds({
79+
group = kill_on_close,
80+
buffer = watch_buffer,
81+
})
82+
end,
83+
})
84+
end
5885
end,
5986
{ desc = "Show a mental map of the current file and watch for changes" }
6087
)
6188

6289
cmd("MarkmapWatchStop", function()
63-
if job ~= nil then jobstop(job) end -- kill jobs
90+
local watch_buffer = vim.api.nvim_get_current_buf()
91+
local job = lookup_table[watch_buffer]
92+
if job then
93+
jobstop(job)
94+
lookup_table[watch_buffer] = nil
95+
api.nvim_clear_autocmds({
96+
group = kill_on_close,
97+
buffer = watch_buffer,
98+
})
99+
end
64100
end, { desc = "Manually stops markmap watch" })
65101
end
66102

0 commit comments

Comments
 (0)