Files
nuwiki/lua/nuwiki/init.lua
T
gffranco 2a8e2b9ea9
CI / cargo fmt --check (push) Failing after 27s
CI / cargo clippy (push) Successful in 33s
CI / cargo test (push) Successful in 37s
CI / editor keymaps (push) Successful in 1m33s
fix(keymaps): correct Neovim global diary tab/tomorrow maps (map_prefix audit)
A map_prefix parity audit against upstream vimwiki found the Neovim global
entry-point block in lua/nuwiki/init.lua carried the same collision the Vim
global + buffer-local maps were already fixed for: <prefix><Leader>t was
bound to tomorrow and <prefix><Leader>m (tomorrow) was missing entirely.

Now <prefix><Leader>t opens today in a new tab and <prefix><Leader>m is
tomorrow — consistent with plugin/nuwiki.vim, the buffer-local maps, and
upstream (VimwikiTabMakeDiaryNote / VimwikiMakeTomorrowDiaryNote).

Adds global_maps.diary_tab_and_tomorrow_targets to test-keymaps.lua (queries
in a scratch buffer so buffer-local maps don't shadow the globals). Logs the
fix plus a new "RSS feed structure fidelity" gap (the audit confirmed the
custom_wiki2html arg contract and base_url scope are faithful; only the RSS
document shape diverges) in development/vimwiki-gap.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 19:22:52 +00:00

105 lines
4.2 KiB
Lua

-- lua/nuwiki/init.lua — Neovim entry point.
--
-- Public surface:
-- require('nuwiki').setup(opts) — apply user config + register LSP
-- require('nuwiki').install() — install / build the language server
-- require('nuwiki').health() — :checkhealth nuwiki target
--
-- This layer is wiring only. Everything substantive
-- happens in the Rust language server.
local M = {}
local config = require('nuwiki.config')
local lsp = require('nuwiki.lsp')
-- Open a file directly, expanding ~ / env vars in the path.
local function _open(path, open_cmd)
vim.cmd((open_cmd or 'edit') .. ' ' .. vim.fn.fnameescape(vim.fn.expand(path)))
end
-- Return the config dict for wiki #n (0-based). See config.wiki_cfg.
local function _wiki_cfg(n)
return config.wiki_cfg(n)
end
-- Compute the wiki index path for wiki #n (0-based) from config.
local function _wiki_index_path(n)
local c = _wiki_cfg(n)
return c.root .. '/' .. c.idx .. c.ext
end
-- Compute a diary file path from config. day_offset=0 → today.
-- Pass nil to get the diary index file instead.
local function _diary_path(day_offset)
local c = _wiki_cfg(0)
if day_offset == nil then
return c.root .. '/' .. c.diary_rel .. '/' .. c.diary_idx .. c.ext
end
local t = os.time() + day_offset * 86400
return c.root .. '/' .. c.diary_rel .. '/' .. os.date('%Y-%m-%d', t) .. c.ext
end
-- Register global (non-buffer-local) entry-point keymaps so users can open
-- their wiki from any buffer, not just from inside a .wiki file. Files are
-- opened directly from config — no LSP client required; the server auto-starts
-- once the vimwiki FileType fires on the newly opened buffer.
local function _setup_global_mappings()
if vim.g.nuwiki_no_default_mappings then return end
local kmap = vim.keymap.set
local function o(d) return { silent = true, desc = 'nuwiki: ' .. d } end
-- Same configurable prefix as the buffer-local maps (vimwiki map_prefix).
local p = config.options.map_prefix or '<Leader>w'
kmap('n', p .. 'w', function() _open(_wiki_index_path(0)) end, o('wiki index'))
kmap('n', p .. 't', function() _open(_wiki_index_path(0), 'tabedit') end, o('wiki index (tab)'))
kmap('n', p .. 's', function() require('nuwiki.commands').wiki_ui_select() end, o('pick wiki'))
kmap('n', p .. 'i', function() _open(_diary_path(nil)) end, o('diary index'))
kmap('n', p .. '<Leader>w', function() _open(_diary_path(0)) end, o('today diary'))
kmap('n', p .. '<Leader>y', function() _open(_diary_path(-1)) end, o('yesterday diary'))
kmap('n', p .. '<Leader>t', function() _open(_diary_path(0), 'tabedit') end, o('today diary (tab)'))
kmap('n', p .. '<Leader>m', function() _open(_diary_path(1)) end, o('tomorrow diary'))
kmap('n', p .. '<Leader>i', function() require('nuwiki.commands').diary_generate_index() end, o('rebuild diary index'))
end
-- Global wiki-picker commands so a wiki can be chosen from any buffer before
-- a .wiki file exists. The picker reads its list from config (no LSP); the
-- buffer-local versions in ftplugin/vimwiki.vim shadow these inside wiki
-- buffers but dispatch to the same function.
local function _setup_global_commands()
local function ui_select() require('nuwiki.commands').wiki_ui_select() end
vim.api.nvim_create_user_command('VimwikiUISelect', ui_select, {})
vim.api.nvim_create_user_command('NuwikiUISelect', ui_select, {})
end
function M.setup(opts)
config.apply(opts or {})
-- Neovim 0.7+ resolves filetypes via `vim.filetype.match` before any
-- `ftdetect/*.vim` autocmd fires, and its bundled rule for `*.wiki`
-- returns `mediawiki`. Register an authoritative rule here so this
-- plugin's filetype (vimwiki) wins.
if vim.filetype and vim.filetype.add then
local extensions = { wiki = 'vimwiki' }
local ext = (config.options.file_extension or '.wiki'):gsub('^%.', '')
if ext ~= 'wiki' and ext ~= '' then
extensions[ext] = 'vimwiki'
end
vim.filetype.add({ extension = extensions })
end
lsp.register()
_setup_global_mappings()
_setup_global_commands()
end
function M.install()
return require('nuwiki.install').install()
end
function M.health()
return require('nuwiki.health').check()
end
return M