b2f2fc88bd
Config plumbing for the whole config batch (HtmlConfig + WikiConfig fields, RawWiki deserialization, From/defaults) plus these behaviors: - rss_name / rss_max_items: HtmlConfig fields (wired into write_rss in the RSS group). - toc_link_format: build_toc_text emits [[#anchor]] (no description) for 1, [[#anchor|title]] for 0. - generated_links_caption: build_links_text emits [[page|FirstHeading]] from a page->heading map (ops::page_captions) when enabled. - table_reduce_last_col: column_widths clamps the last column to width 1 when set; threaded through table_align_edit/table_move_column_edit + commands. - color_dic now ships a populated default palette (red/green/blue/...). - commentstring aligned to upstream's no-space `%%%s`. - auto_chdir (default off): :lcd into the owning wiki root on buffer enter; both clients (ftplugin.lua setup_auto_chdir + Vim NuwikiAutoChdir augroup), with config.wiki_root_for / nuwiki#commands#wiki_root_for resolvers. Also seeded HtmlConfig fields for later groups (valid_html_tags, list/text_ignore_newline, emoji_enable, user_htmls, color_tag_template) and WikiConfig (create_link, dir_link, bullet_types, cycle_bullets). Tests: toc_link_format, generated_links_caption, table_reduce_last_col, config defaults + JSON round-trip for every new key. Full rust suite + both keymap harnesses green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
141 lines
4.8 KiB
Lua
141 lines
4.8 KiB
Lua
-- lua/nuwiki/ftplugin.lua — per-buffer hookup invoked by
|
|
-- `ftplugin/nuwiki.vim`. Centralises the Lua side of the per-buffer
|
|
-- glue so the VimL ftplugin stays a one-liner regardless of which subgroups are
|
|
-- enabled.
|
|
|
|
local M = {}
|
|
|
|
local function setup_folding(bufnr, folding_mode)
|
|
if folding_mode == 'off' then
|
|
return
|
|
end
|
|
-- `foldmethod` / `foldexpr` / `foldtext` are window-local options.
|
|
-- The ftplugin runs during BufRead → FileType, by which time the
|
|
-- buffer is displayed in the current window. `vim.opt_local` writes
|
|
-- to the current window's view of the option.
|
|
-- The LSP foldexpr is wrapped via `nuwiki.folding.lsp_expr` so a
|
|
-- transient client failure (e.g. server still starting, buffer not
|
|
-- yet attached) degrades to the regex variant instead of spamming
|
|
-- the user with E5108 errors per line.
|
|
local apply = function()
|
|
if folding_mode == 'expr'
|
|
or not (vim.fn.has('nvim-0.11') == 1 and vim.lsp.foldexpr)
|
|
then
|
|
vim.opt_local.foldmethod = 'expr'
|
|
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").expr()'
|
|
vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
|
|
else
|
|
vim.opt_local.foldmethod = 'expr'
|
|
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").lsp_expr()'
|
|
vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
|
|
end
|
|
end
|
|
|
|
-- Set on the current window first (the one that triggered the
|
|
-- ftplugin). Also re-apply on any future split that views this
|
|
-- buffer, so `<C-w>v` / `:split` preserves the fold method.
|
|
apply()
|
|
-- Start with every heading-block fold open. `foldlevel` is window-local
|
|
-- but only sets the initial state; once the user closes folds with `zc`
|
|
-- those stay closed, so we don't re-apply this on BufWinEnter.
|
|
vim.opt_local.foldlevel = 99
|
|
vim.api.nvim_create_autocmd('BufWinEnter', {
|
|
buffer = bufnr,
|
|
group = vim.api.nvim_create_augroup('nuwiki_folding_' .. bufnr, { clear = true }),
|
|
callback = apply,
|
|
})
|
|
end
|
|
|
|
-- vimwiki `autowriteall`: while this wiki buffer is current, mirror the option
|
|
-- into Vim's global 'autowriteall' (auto-save on switch/make), restoring the
|
|
-- prior value on leave.
|
|
local function setup_autowriteall(bufnr, enabled)
|
|
if not enabled then
|
|
return
|
|
end
|
|
if bufnr == 0 then
|
|
bufnr = vim.api.nvim_get_current_buf()
|
|
end
|
|
local grp = vim.api.nvim_create_augroup('nuwiki_awa_' .. bufnr, { clear = true })
|
|
local function enter()
|
|
vim.b[bufnr].nuwiki_awa_saved = vim.o.autowriteall
|
|
vim.o.autowriteall = true
|
|
end
|
|
local function leave()
|
|
local saved = vim.b[bufnr].nuwiki_awa_saved
|
|
if saved ~= nil then
|
|
vim.o.autowriteall = saved
|
|
end
|
|
end
|
|
vim.api.nvim_create_autocmd('BufEnter', { buffer = bufnr, group = grp, callback = enter })
|
|
vim.api.nvim_create_autocmd('BufLeave', { buffer = bufnr, group = grp, callback = leave })
|
|
-- FileType fires after the initial BufEnter, so apply once now too.
|
|
enter()
|
|
end
|
|
|
|
-- vimwiki `table_auto_fmt`: re-align the table under the cursor on InsertLeave.
|
|
local function setup_table_auto_fmt(bufnr, enabled)
|
|
if not enabled then
|
|
return
|
|
end
|
|
if bufnr == 0 then
|
|
bufnr = vim.api.nvim_get_current_buf()
|
|
end
|
|
local grp = vim.api.nvim_create_augroup('nuwiki_taf_' .. bufnr, { clear = true })
|
|
vim.api.nvim_create_autocmd('InsertLeave', {
|
|
buffer = bufnr,
|
|
group = grp,
|
|
callback = function()
|
|
-- Cheap guard: only round-trip to the server on a table row.
|
|
if vim.api.nvim_get_current_line():find('|', 1, true) then
|
|
require('nuwiki.commands').table_align()
|
|
end
|
|
end,
|
|
})
|
|
end
|
|
|
|
-- vimwiki `auto_chdir`: `:lcd` into the owning wiki's root while this buffer
|
|
-- is current.
|
|
local function setup_auto_chdir(bufnr, enabled)
|
|
if not enabled then
|
|
return
|
|
end
|
|
if bufnr == 0 then
|
|
bufnr = vim.api.nvim_get_current_buf()
|
|
end
|
|
local function chdir()
|
|
local file = vim.api.nvim_buf_get_name(bufnr)
|
|
local root = require('nuwiki.config').wiki_root_for(file)
|
|
if root then
|
|
vim.cmd('lcd ' .. vim.fn.fnameescape(root))
|
|
end
|
|
end
|
|
local grp = vim.api.nvim_create_augroup('nuwiki_chdir_' .. bufnr, { clear = true })
|
|
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWinEnter' }, {
|
|
buffer = bufnr,
|
|
group = grp,
|
|
callback = chdir,
|
|
})
|
|
chdir()
|
|
end
|
|
|
|
function M.attach(bufnr)
|
|
bufnr = bufnr or 0
|
|
local config = require('nuwiki.config')
|
|
local opts = config.options or config.defaults
|
|
|
|
if opts.mappings and opts.mappings.enabled ~= false then
|
|
require('nuwiki.keymaps').attach(bufnr, opts.mappings)
|
|
if opts.mappings.text_objects ~= false then
|
|
require('nuwiki.textobjects').attach(bufnr)
|
|
end
|
|
end
|
|
|
|
setup_folding(bufnr, opts.folding or 'lsp')
|
|
setup_autowriteall(bufnr, opts.autowriteall ~= false)
|
|
setup_table_auto_fmt(bufnr, opts.table_auto_fmt ~= false)
|
|
setup_auto_chdir(bufnr, opts.auto_chdir == true)
|
|
end
|
|
|
|
return M
|