diff --git a/README.md b/README.md index 3ffbc38..4160267 100644 --- a/README.md +++ b/README.md @@ -21,18 +21,8 @@ Typst, Markdown, etc.)—diagnostics included. ## Installation -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): +Install with your package manager of choice or via +[luarocks](https://luarocks.org/modules/barrettruth/preview.nvim): ``` luarocks install preview.nvim @@ -49,7 +39,7 @@ luarocks install preview.nvim **Q: How do I define a custom provider?** ```lua -vim.g.preview = { +require('preview').setup({ rst = { cmd = { 'rst2html' }, args = function(ctx) @@ -59,15 +49,15 @@ vim.g.preview = { return ctx.file:gsub('%.rst$', '.html') end, }, -} +}) ``` **Q: How do I override a preset?** ```lua -vim.g.preview = { +require('preview').setup({ typst = { env = { TYPST_FONT_PATHS = '/usr/share/fonts' } }, -} +}) ``` **Q: How do I automatically open the output file?** @@ -77,7 +67,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 -vim.g.preview = { +require('preview').setup({ typst = { open = { 'sioyek', '--new-instance' } }, -} +}) ``` diff --git a/doc/preview.nvim.txt b/doc/preview.nvim.txt index 2566301..e2747d6 100644 --- a/doc/preview.nvim.txt +++ b/doc/preview.nvim.txt @@ -23,20 +23,23 @@ REQUIREMENTS *preview.nvim-requirements ============================================================================== SETUP *preview.nvim-setup* -Set |vim.g.preview| before the plugin loads: >lua +Load preview.nvim with your package manager. For example, with lazy.nvim: >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 by setting |vim.g.preview| to a table where keys are preset names -or filetypes. For each key `k` with value `v` (excluding `debug`): +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`): - If `k` is a preset name and `v` is `true`, the preset is registered as-is under its filetype. @@ -105,12 +108,6 @@ 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:~ @@ -123,30 +120,30 @@ Context fields:~ Example enabling presets:~ >lua - vim.g.preview = { typst = true, latex = true, github = true } + require('preview').setup({ typst = true, latex = true, github = true }) < Example overriding a preset field:~ >lua - vim.g.preview = { + require('preview').setup({ typst = { open = { 'sioyek', '--new-instance' } }, - } + }) < Example overriding the output path (e.g. latexmk `$out_dir`):~ >lua - vim.g.preview = { + require('preview').setup({ 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 - vim.g.preview = { + require('preview').setup({ rst = { cmd = { 'rst2html' }, args = function(ctx) @@ -156,7 +153,7 @@ Example with a fully custom provider (key is not a preset name):~ return ctx.file:gsub('%.rst$', '.html') end, }, - } + }) < ============================================================================== @@ -176,14 +173,14 @@ Import them from `preview.presets`: Enable presets with `preset_name = true`: >lua - vim.g.preview = { typst = true, latex = true, github = true } + require('preview').setup({ typst = true, latex = true, github = true }) < Override individual fields by passing a table instead of `true`: >lua - vim.g.preview = { + require('preview').setup({ typst = { env = { TYPST_FONT_PATHS = '/usr/share/fonts' } }, - } + }) < ============================================================================== diff --git a/lua/preview/compiler.lua b/lua/preview/compiler.lua index a193ad2..a3b9c47 100644 --- a/lua/preview/compiler.lua +++ b/lua/preview/compiler.lua @@ -291,9 +291,7 @@ function M.compile(bufnr, name, provider, ctx, opts) callback = function() M.stop(bufnr) stop_open_watcher(bufnr) - if not provider.detach then - close_viewer(bufnr) - end + close_viewer(bufnr) last_output[bufnr] = nil end, }) @@ -406,9 +404,7 @@ function M.compile(bufnr, name, provider, ctx, opts) once = true, callback = function() M.stop(bufnr) - if not provider.detach then - close_viewer(bufnr) - end + close_viewer(bufnr) last_output[bufnr] = nil end, }) @@ -511,9 +507,7 @@ function M.toggle(bufnr, name, provider, ctx_builder) callback = function() M.unwatch(bufnr) stop_open_watcher(bufnr) - if not provider.detach then - close_viewer(bufnr) - end + close_viewer(bufnr) opened[bufnr] = nil end, }) diff --git a/lua/preview/init.lua b/lua/preview/init.lua index 322b893..fd54d71 100644 --- a/lua/preview/init.lua +++ b/lua/preview/init.lua @@ -10,7 +10,6 @@ ---@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 @@ -102,7 +101,6 @@ 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, { @@ -248,8 +246,4 @@ M._test = { end, } -if vim.g.preview then - M.setup(vim.g.preview) -end - return M