feat(keymaps): configurable map_prefix for the wiki command family (P2)
CI / cargo fmt --check (push) Failing after 33s
CI / cargo clippy (push) Successful in 31s
CI / cargo test (push) Successful in 41s
CI / editor keymaps (push) Successful in 1m49s

Mirror vimwiki's g:vimwiki_map_prefix. The whole <Leader>w* family was
hardcoded across the map layer; it is now built from a configurable prefix:

- Neovim: `map_prefix` setup() option (default '<Leader>w'), read by
  lua/nuwiki/keymaps.lua (buffer-local) and lua/nuwiki/init.lua (global
  entry points).
- Vim: g:nuwiki_map_prefix, applied via :exe in plugin/nuwiki.vim (global)
  and ftplugin/vimwiki.vim (buffer-local).

Setting a custom prefix relocates <prefix>{w,t,s,i,n,d,r,c,h,hh,ha} and
<prefix><Leader>{w,y,t,m,i}, leaving nothing under the old <Leader>w*.
Non-prefix maps (<CR>, gl*, headers, …) are untouched.

Tests: new test-keymaps-vim-prefix.vim harness (21 cases, wired into
test-keymaps-vim.sh) + map_prefix.relocates_wiki_family in test-keymaps.lua.
Docs: README, doc/nuwiki.txt, lua config comment, vimwiki-gap.md ticked.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 19:18:06 +00:00
parent 23aec3e6c7
commit feedd30c4d
11 changed files with 234 additions and 55 deletions
+30
View File
@@ -820,6 +820,36 @@ vim.defer_fn(function()
end
end)
-- ===== Configurable map_prefix (vimwiki g:vimwiki_map_prefix) =====
-- Overriding map_prefix must relocate the whole <prefix>* family. We attach
-- to a throwaway scratch buffer with a custom prefix and assert the new
-- bindings exist there while the default <Leader>w* ones do not.
tobj_case('map_prefix.relocates_wiki_family', function()
local config = require('nuwiki.config')
local keymaps = require('nuwiki.keymaps')
local saved = config.options.map_prefix
config.options.map_prefix = '<Leader>n'
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_current_buf(buf)
local ok, err = pcall(function()
keymaps.attach(buf, { enabled = true, wiki_prefix = true, links = true, html_export = true })
local function bmap(lhs)
local d = vim.fn.maparg(lhs, 'n', false, true)
return not vim.tbl_isempty(d) and d.buffer == 1
end
for _, lhs in ipairs({
'<Leader>nw', '<Leader>nt', '<Leader>n<Leader>w', '<Leader>nr', '<Leader>nh', '<Leader>nha',
}) do
if not bmap(lhs) then error('relocated map missing: ' .. lhs) end
end
if bmap('<Leader>ww') then error('<Leader>ww still bound under custom prefix') end
end)
-- Always restore global state so later cases see the default prefix.
config.options.map_prefix = saved
vim.api.nvim_buf_delete(buf, { force = true })
if not ok then error(err) end
end)
-- ===== Wiki picker (config-driven, no LSP) =====
-- The picker must build its list from config so it works from any buffer
-- before the server attaches.