Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
64cf238c7f
docs: replace all setup() references with vim.g.preview 2026-03-04 19:33:36 -05:00
b95493ddee
feat: add detach provider field and vim.g.preview config support
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.
2026-03-04 19:22:32 -05:00
4 changed files with 55 additions and 30 deletions

View file

@ -21,8 +21,18 @@ Typst, Markdown, etc.)—diagnostics included.
## Installation
Install with your package manager of choice or via
[luarocks](https://luarocks.org/modules/barrettruth/preview.nvim):
With lazy.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
@ -39,7 +49,7 @@ luarocks install preview.nvim
**Q: How do I define a custom provider?**
```lua
require('preview').setup({
vim.g.preview = {
rst = {
cmd = { 'rst2html' },
args = function(ctx)
@ -49,15 +59,15 @@ require('preview').setup({
return ctx.file:gsub('%.rst$', '.html')
end,
},
})
}
```
**Q: How do I override a preset?**
```lua
require('preview').setup({
vim.g.preview = {
typst = { env = { TYPST_FONT_PATHS = '/usr/share/fonts' } },
})
}
```
**Q: How do I automatically open the output file?**
@ -67,7 +77,7 @@ open the output with `vim.ui.open()` after the first successful compilation in
toggle/watch mode. For a specific application, pass a command table:
```lua
require('preview').setup({
vim.g.preview = {
typst = { open = { 'sioyek', '--new-instance' } },
})
}
```

View file

@ -23,23 +23,20 @@ REQUIREMENTS *preview.nvim-requirements
==============================================================================
SETUP *preview.nvim-setup*
Load preview.nvim with your package manager. For example, with lazy.nvim: >lua
Set |vim.g.preview| before the plugin loads: >lua
{
'barrettruth/preview.nvim',
init = function()
vim.g.preview = { typst = true, latex = true }
end,
}
<
Call |preview.setup()| to configure providers before use.
==============================================================================
CONFIGURATION *preview.nvim-configuration*
Configure via `require('preview').setup()`.
*preview.setup()*
setup({opts?})
`opts` is a table where keys are preset names or filetypes. For each
key `k` with value `v` (excluding `debug`):
Configure by setting |vim.g.preview| to a table where keys are preset names
or filetypes. For each key `k` with value `v` (excluding `debug`):
- If `k` is a preset name and `v` is `true`, the preset is registered
as-is under its filetype.
@ -108,6 +105,12 @@ Provider fields:~
|preview.Context| and returns a
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*
Context fields:~
@ -120,30 +123,30 @@ Context fields:~
Example enabling presets:~
>lua
require('preview').setup({ typst = true, latex = true, github = true })
vim.g.preview = { typst = true, latex = true, github = true }
<
Example overriding a preset field:~
>lua
require('preview').setup({
vim.g.preview = {
typst = { open = { 'sioyek', '--new-instance' } },
})
}
<
Example overriding the output path (e.g. latexmk `$out_dir`):~
>lua
require('preview').setup({
vim.g.preview = {
latex = {
output = function(ctx)
return 'build/' .. vim.fn.fnamemodify(ctx.file, ':t:r') .. '.pdf'
end,
},
})
}
<
Example with a fully custom provider (key is not a preset name):~
>lua
require('preview').setup({
vim.g.preview = {
rst = {
cmd = { 'rst2html' },
args = function(ctx)
@ -153,7 +156,7 @@ Example with a fully custom provider (key is not a preset name):~
return ctx.file:gsub('%.rst$', '.html')
end,
},
})
}
<
==============================================================================
@ -173,14 +176,14 @@ Import them from `preview.presets`:
Enable presets with `preset_name = true`:
>lua
require('preview').setup({ typst = true, latex = true, github = true })
vim.g.preview = { typst = true, latex = true, github = true }
<
Override individual fields by passing a table instead of `true`:
>lua
require('preview').setup({
vim.g.preview = {
typst = { env = { TYPST_FONT_PATHS = '/usr/share/fonts' } },
})
}
<
==============================================================================

View file

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

View file

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