feat: add detach provider field and vim.g.preview config support (#42)

Problem: viewer processes launched via a string[] `open` command were
always killed on buffer deletion with no way to opt out. Configuring
the plugin also required an explicit `setup()` call in a `config`
hook, preventing config from being declared before the plugin loads.

Solution: add a `detach` boolean to `ProviderConfig` that skips
SIGTERM on buffer unload. Auto-call `setup()` from `vim.g.preview`
at module load time, enabling config via lazy.nvim's `init` hook.
Update vimdoc and README accordingly.
This commit is contained in:
Barrett Ruth 2026-03-04 19:30:56 -05:00 committed by GitHub
parent bb9ca987e1
commit f1aed82f42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 40 additions and 7 deletions

View file

@ -21,8 +21,18 @@ Typst, Markdown, etc.)—diagnostics included.
## Installation ## Installation
Install with your package manager of choice or via With lazy.nvim:
[luarocks](https://luarocks.org/modules/barrettruth/preview.nvim):
```lua
{
'barrettruth/preview.nvim',
init = function()
vim.g.preview = { typst = true, latex = true }
end,
}
```
Or via [luarocks](https://luarocks.org/modules/barrettruth/preview.nvim):
``` ```
luarocks install preview.nvim luarocks install preview.nvim

View file

@ -23,12 +23,17 @@ REQUIREMENTS *preview.nvim-requirements
============================================================================== ==============================================================================
SETUP *preview.nvim-setup* SETUP *preview.nvim-setup*
Load preview.nvim with your package manager. For example, with lazy.nvim: >lua With lazy.nvim, set |vim.g.preview| in `init` so configuration is applied
before the plugin loads: >lua
{ {
'barrettruth/preview.nvim', 'barrettruth/preview.nvim',
init = function()
vim.g.preview = { typst = true, latex = true }
end,
} }
< <
Call |preview.setup()| to configure providers before use. Alternatively, call |preview.setup()| directly in a `config` function or
anywhere the plugin is already loaded.
============================================================================== ==============================================================================
CONFIGURATION *preview.nvim-configuration* CONFIGURATION *preview.nvim-configuration*
@ -108,6 +113,12 @@ Provider fields:~
|preview.Context| and returns a |preview.Context| and returns a
string[]. string[].
`detach` boolean When `true`, the viewer process opened
via a string[] `open` command is not
sent SIGTERM when the buffer is deleted.
Has no effect when `open` is `true`.
Default: `false`.
*preview.Context* *preview.Context*
Context fields:~ Context fields:~

View file

@ -291,7 +291,9 @@ function M.compile(bufnr, name, provider, ctx, opts)
callback = function() callback = function()
M.stop(bufnr) M.stop(bufnr)
stop_open_watcher(bufnr) stop_open_watcher(bufnr)
close_viewer(bufnr) if not provider.detach then
close_viewer(bufnr)
end
last_output[bufnr] = nil last_output[bufnr] = nil
end, end,
}) })
@ -404,7 +406,9 @@ function M.compile(bufnr, name, provider, ctx, opts)
once = true, once = true,
callback = function() callback = function()
M.stop(bufnr) M.stop(bufnr)
close_viewer(bufnr) if not provider.detach then
close_viewer(bufnr)
end
last_output[bufnr] = nil last_output[bufnr] = nil
end, end,
}) })
@ -507,7 +511,9 @@ function M.toggle(bufnr, name, provider, ctx_builder)
callback = function() callback = function()
M.unwatch(bufnr) M.unwatch(bufnr)
stop_open_watcher(bufnr) stop_open_watcher(bufnr)
close_viewer(bufnr) if not provider.detach then
close_viewer(bufnr)
end
opened[bufnr] = nil opened[bufnr] = nil
end, end,
}) })

View file

@ -10,6 +10,7 @@
---@field clean? string[]|fun(ctx: preview.Context): string[] ---@field clean? string[]|fun(ctx: preview.Context): string[]
---@field open? boolean|string[] ---@field open? boolean|string[]
---@field reload? boolean|string[]|fun(ctx: preview.Context): string[] ---@field reload? boolean|string[]|fun(ctx: preview.Context): string[]
---@field detach? boolean
---@class preview.Config ---@class preview.Config
---@field debug boolean|string ---@field debug boolean|string
@ -101,6 +102,7 @@ function M.setup(opts)
end, 'false, "diagnostic", or "quickfix"') end, 'false, "diagnostic", or "quickfix"')
vim.validate(prefix .. '.open', provider.open, { 'boolean', 'table' }, true) vim.validate(prefix .. '.open', provider.open, { 'boolean', 'table' }, true)
vim.validate(prefix .. '.reload', provider.reload, { 'boolean', 'table', 'function' }, true) vim.validate(prefix .. '.reload', provider.reload, { 'boolean', 'table', 'function' }, true)
vim.validate(prefix .. '.detach', provider.detach, 'boolean', true)
end end
config = vim.tbl_deep_extend('force', default_config, { config = vim.tbl_deep_extend('force', default_config, {
@ -246,4 +248,8 @@ M._test = {
end, end,
} }
if vim.g.preview then
M.setup(vim.g.preview)
end
return M return M