Add :Watch command
This commit is contained in:
@@ -249,3 +249,29 @@ vim.api.nvim_create_user_command('ResizeFixed', function(opts)
|
||||
vim.wo.winfixheight = true
|
||||
end
|
||||
end, { nargs = '?' })
|
||||
|
||||
-- :Watch a file for changes and immediately load them.
|
||||
vim.api.nvim_create_user_command("Watch", function()
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local path = vim.api.nvim_buf_get_name(bufnr)
|
||||
|
||||
local w, err = vim.uv.new_fs_event()
|
||||
if not w then
|
||||
return vim.notify("Watch: " .. err, vim.log.levels.ERROR)
|
||||
end
|
||||
|
||||
local ok, start_err = w:start(path, {}, vim.schedule_wrap(function(cb_err)
|
||||
if cb_err or not vim.api.nvim_buf_is_loaded(bufnr) then
|
||||
w:close()
|
||||
return
|
||||
end
|
||||
vim.cmd("silent! checktime " .. bufnr)
|
||||
end))
|
||||
if not ok then
|
||||
w:close()
|
||||
return vim.notify("Watch: " .. start_err, vim.log.levels.ERROR)
|
||||
end
|
||||
|
||||
vim.bo[bufnr].autoread = true -- buffer-local
|
||||
vim.notify("Watching " .. vim.fn.fnamemodify(path, ":t"))
|
||||
end, {})
|
||||
|
||||
Reference in New Issue
Block a user