@@ -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 ()
@@ -38,30 +39,65 @@ M.setup = function(opts)
3839 cmd (
3940 " MarkmapWatch" ,
4041 function ()
42+ local watch_buffer = vim .api .nvim_get_current_buf ()
4143 config = vim .g .markmap_config
4244 arguments = utils .reset_arguments ()
4345 table.insert (arguments , " --watch" ) -- spetific to this command
4446 table.insert (arguments , vim .fn .expand (" %:p" )) -- current buffer path
45- if job ~= nil then jobstop (job ) end -- kill jobs
46- job = jobstart (config .markmap_cmd , arguments )
4747
48- -- Register buffer local autocmd to kill job when buffer closes
49- local kill_on_close = augroup (" markmap_kill_on_close" , { clear = true })
50- autocmd (" BufDelete" , {
51- buffer = 0 ,
52- desc = " Kill markmap when watched buffer is closed" ,
53- group = kill_on_close ,
54- callback = function ()
55- jobstop (job )
56- api .nvim_clear_autocmds ({ group = kill_on_close })
57- end ,
58- })
48+ if lookup_table [watch_buffer ] then
49+ vim .notify (" You're already watching this buffer." , vim .log .levels .WARN )
50+ else
51+ local kill_on_close =
52+ augroup (" markmap_kill_on_close" , { clear = false })
53+
54+ job = jobstart (config .markmap_cmd , arguments , {
55+ stderr_buffered = true , -- needed so on_stderr is only called once
56+ on_stderr = function (_ , data )
57+ local message = table.concat (data , " \n " )
58+ message = message :gsub (" \r " , " " )
59+ vim .notify (message , vim .log .levels .ERROR )
60+
61+ lookup_table [watch_buffer ] = nil
62+ api .nvim_clear_autocmds ({
63+ group = kill_on_close ,
64+ buffer = watch_buffer ,
65+ })
66+ end ,
67+ })
68+
69+ lookup_table [watch_buffer ] = job
70+
71+ -- Register buffer local autocmd to kill job when buffer closes
72+ autocmd (" BufDelete" , {
73+ desc = " Kill markmap when watched buffer is closed" ,
74+ buffer = watch_buffer ,
75+ group = kill_on_close ,
76+ callback = function ()
77+ jobstop (job )
78+ lookup_table [watch_buffer ] = nil
79+ api .nvim_clear_autocmds ({
80+ group = kill_on_close ,
81+ buffer = watch_buffer ,
82+ })
83+ end ,
84+ })
85+ end
5986 end ,
6087 { desc = " Show a mental map of the current file and watch for changes" }
6188 )
6289
6390 cmd (" MarkmapWatchStop" , function ()
64- if job ~= nil then jobstop (job ) end -- kill jobs
91+ local watch_buffer = vim .api .nvim_get_current_buf ()
92+ local job = lookup_table [watch_buffer ]
93+ if job then
94+ jobstop (job )
95+ lookup_table [watch_buffer ] = nil
96+ api .nvim_clear_autocmds ({
97+ group = kill_on_close ,
98+ buffer = watch_buffer ,
99+ })
100+ end
65101 end , { desc = " Manually stops markmap watch" })
66102end
67103
0 commit comments