fix(mappings): add global entry-point keymaps and correct <Leader>ww/<Leader>wt targets
CI / cargo fmt --check (push) Successful in 17s
CI / cargo clippy (push) Successful in 31s
CI / cargo test (push) Successful in 53s
CI / editor keymaps (push) Successful in 1m30s

Two bugs prevented keymaps from working after a Dein install:

1. <Leader>ww and <Leader>wt both called diary_today() instead of
   wiki_index() / wiki_tab_index(). Fixed in keymaps.lua (Neovim)
   and ftplugin/vimwiki.vim (Vim).

2. All keymaps were buffer-local (<buffer> / buffer=bufnr), so they
   only activated inside an already-open .wiki file. Users had no way
   to reach the wiki from any other buffer, making the plugin appear
   broken on a fresh Vim start.

   Fix: register the eight <Leader>w* entry-point mappings globally —
   in setup() for Neovim (lua/nuwiki/init.lua) and in plugin/nuwiki.vim
   for plain Vim. To avoid a chicken-and-egg LSP dependency, the global
   mappings open files directly from config (wiki_root, diary_rel_path,
   file_extension); the LSP auto-starts via the FileType autocmd once
   the vimwiki buffer loads. Three new autoload helpers
   (open_wiki_path, open_diary_path, open_diary_index_path) provide the
   LSP-free path for the Vim side.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-21 21:34:41 -03:00
parent a8b89a5303
commit 9354e1c176
5 changed files with 101 additions and 4 deletions
+30
View File
@@ -342,6 +342,36 @@ function! nuwiki#commands#diary_generate_index() abort
call s:exec('nuwiki.diary.generateIndex', [{ 'uri': s:buf_uri() }]) call s:exec('nuwiki.diary.generateIndex', [{ 'uri': s:buf_uri() }])
endfunction endfunction
" ===== Global entry-point helpers (no LSP required) =====
"
" These are used by the global mappings in plugin/nuwiki.vim so users can
" open their wiki from any buffer. Files are opened directly from config;
" the LSP auto-starts via the FileType autocmd once the buffer loads.
function! nuwiki#commands#open_wiki_path(tab) abort
let l:root = expand(get(g:, 'nuwiki_wiki_root', '~/vimwiki'))
let l:ext = get(g:, 'nuwiki_file_extension', '.wiki')
if l:ext[0] !=# '.' | let l:ext = '.' . l:ext | endif
execute (a:tab ? 'tabedit' : 'edit') . ' ' . fnameescape(l:root . '/index' . l:ext)
endfunction
function! nuwiki#commands#open_diary_path(day_offset) abort
let l:root = expand(get(g:, 'nuwiki_wiki_root', '~/vimwiki'))
let l:ext = get(g:, 'nuwiki_file_extension', '.wiki')
if l:ext[0] !=# '.' | let l:ext = '.' . l:ext | endif
let l:diary = get(g:, 'nuwiki_diary_rel_path', 'diary')
let l:date = strftime('%Y-%m-%d', localtime() + a:day_offset * 86400)
execute 'edit ' . fnameescape(l:root . '/' . l:diary . '/' . l:date . l:ext)
endfunction
function! nuwiki#commands#open_diary_index_path() abort
let l:root = expand(get(g:, 'nuwiki_wiki_root', '~/vimwiki'))
let l:ext = get(g:, 'nuwiki_file_extension', '.wiki')
if l:ext[0] !=# '.' | let l:ext = '.' . l:ext | endif
let l:diary = get(g:, 'nuwiki_diary_rel_path', 'diary')
execute 'edit ' . fnameescape(l:root . '/' . l:diary . '/diary' . l:ext)
endfunction
" ===== List + heading editing ===== " ===== List + heading editing =====
function! s:exec_pos(cmd) abort function! s:exec_pos(cmd) abort
+2 -2
View File
@@ -115,8 +115,8 @@ if !has('nvim')
if !get(g:, 'nuwiki_no_default_mappings', 0) if !get(g:, 'nuwiki_no_default_mappings', 0)
" Wiki prefix " Wiki prefix
nnoremap <silent><buffer> <Leader>ww :call nuwiki#commands#diary_today()<CR> nnoremap <silent><buffer> <Leader>ww :call nuwiki#commands#wiki_index(0)<CR>
nnoremap <silent><buffer> <Leader>wt :call nuwiki#commands#diary_today()<CR> nnoremap <silent><buffer> <Leader>wt :call nuwiki#commands#wiki_tab_index(0)<CR>
nnoremap <silent><buffer> <Leader>ws :call nuwiki#commands#wiki_ui_select()<CR> nnoremap <silent><buffer> <Leader>ws :call nuwiki#commands#wiki_ui_select()<CR>
nnoremap <silent><buffer> <Leader>wi :call nuwiki#commands#diary_index()<CR> nnoremap <silent><buffer> <Leader>wi :call nuwiki#commands#diary_index()<CR>
nnoremap <silent><buffer> <Leader>w<Leader>w :call nuwiki#commands#diary_today()<CR> nnoremap <silent><buffer> <Leader>w<Leader>w :call nuwiki#commands#diary_today()<CR>
+53
View File
@@ -13,6 +13,58 @@ local M = {}
local config = require('nuwiki.config') local config = require('nuwiki.config')
local lsp = require('nuwiki.lsp') 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
-- Compute the wiki index path for wiki #n (0-based) from config.
local function _wiki_index_path(n)
local opts = config.options
local w = opts.wikis and opts.wikis[(n or 0) + 1]
local root = (w and w.root) or opts.wiki_root or '~/vimwiki'
local idx = (w and w.index) or 'index'
local ext = (w and w.file_extension) or opts.file_extension or '.wiki'
if not ext:match('^%.') then ext = '.' .. ext end
return root .. '/' .. idx .. ext
end
-- Compute a diary file path from config. day_offset=0 → today.
-- Pass nil for the diary index file itself.
local function _diary_path(day_offset)
local opts = config.options
local w = opts.wikis and opts.wikis[1]
local root = (w and w.root) or opts.wiki_root or '~/vimwiki'
local rel = (w and w.diary_rel_path) or 'diary'
local ext = (w and w.file_extension) or opts.file_extension or '.wiki'
if not ext:match('^%.') then ext = '.' .. ext end
if day_offset == nil then
local didx = (w and w.diary_index) or 'diary'
return root .. '/' .. rel .. '/' .. didx .. ext
end
local t = os.time() + day_offset * 86400
return root .. '/' .. rel .. '/' .. os.date('%Y-%m-%d', t) .. 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
kmap('n', '<Leader>ww', function() _open(_wiki_index_path(0)) end, o('wiki index'))
kmap('n', '<Leader>wt', function() _open(_wiki_index_path(0), 'tabedit') end, o('wiki index (tab)'))
kmap('n', '<Leader>ws', function() require('nuwiki.commands').wiki_ui_select() end, o('pick wiki'))
kmap('n', '<Leader>wi', function() _open(_diary_path(nil)) end, o('diary index'))
kmap('n', '<Leader>w<Leader>w', function() _open(_diary_path(0)) end, o('today diary'))
kmap('n', '<Leader>w<Leader>y', function() _open(_diary_path(-1)) end, o('yesterday diary'))
kmap('n', '<Leader>w<Leader>t', function() _open(_diary_path(1)) end, o('tomorrow diary'))
kmap('n', '<Leader>w<Leader>i', function() require('nuwiki.commands').diary_generate_index() end, o('rebuild diary index'))
end
function M.setup(opts) function M.setup(opts)
config.apply(opts or {}) config.apply(opts or {})
@@ -30,6 +82,7 @@ function M.setup(opts)
end end
lsp.register() lsp.register()
_setup_global_mappings()
end end
function M.install() function M.install()
+2 -2
View File
@@ -160,8 +160,8 @@ function M.attach(bufnr, mappings)
-- ===== Wiki prefix (`<Leader>w*`) ===== -- ===== Wiki prefix (`<Leader>w*`) =====
if on('wiki_prefix') then if on('wiki_prefix') then
map('n', '<Leader>ww', cmd.diary_today, { desc = 'nuwiki: open default wiki / today' }, bufnr) map('n', '<Leader>ww', function() cmd.wiki_index(0) end, { desc = 'nuwiki: open wiki index' }, bufnr)
map('n', '<Leader>wt', cmd.diary_today, { desc = 'nuwiki: open today\'s diary' }, bufnr) map('n', '<Leader>wt', function() cmd.wiki_tab_index(0) end, { desc = 'nuwiki: open wiki index (tab)' }, bufnr)
map('n', '<Leader>ws', cmd.wiki_ui_select, { desc = 'nuwiki: pick wiki' }, bufnr) map('n', '<Leader>ws', cmd.wiki_ui_select, { desc = 'nuwiki: pick wiki' }, bufnr)
map('n', '<Leader>wi', cmd.diary_index, { desc = 'nuwiki: open diary index' }, bufnr) map('n', '<Leader>wi', cmd.diary_index, { desc = 'nuwiki: open diary index' }, bufnr)
map('n', '<Leader>w<Leader>w', cmd.diary_today, { desc = 'nuwiki: today' }, bufnr) map('n', '<Leader>w<Leader>w', cmd.diary_today, { desc = 'nuwiki: today' }, bufnr)
+14
View File
@@ -27,3 +27,17 @@ augroup END
" :NuwikiInstall — convenience wrapper around scripts/download_bin.vim, so " :NuwikiInstall — convenience wrapper around scripts/download_bin.vim, so
" users don't have to invoke a plugin-manager build hook by hand. " users don't have to invoke a plugin-manager build hook by hand.
command! -nargs=0 NuwikiInstall execute 'source' fnamemodify(resolve(expand('<sfile>:p')), ':h:h') . '/scripts/download_bin.vim' command! -nargs=0 NuwikiInstall execute 'source' fnamemodify(resolve(expand('<sfile>:p')), ':h:h') . '/scripts/download_bin.vim'
" Global entry-point mappings — work from any buffer, not just .wiki files.
" Files are opened directly from config (no LSP required); the server
" auto-starts via the FileType autocmd once the vimwiki buffer loads.
if !get(g:, 'nuwiki_no_default_mappings', 0)
nnoremap <silent> <Leader>ww :call nuwiki#commands#open_wiki_path(0)<CR>
nnoremap <silent> <Leader>wt :call nuwiki#commands#open_wiki_path(1)<CR>
nnoremap <silent> <Leader>ws :call nuwiki#commands#wiki_ui_select()<CR>
nnoremap <silent> <Leader>wi :call nuwiki#commands#open_diary_index_path()<CR>
nnoremap <silent> <Leader>w<Leader>w :call nuwiki#commands#open_diary_path(0)<CR>
nnoremap <silent> <Leader>w<Leader>y :call nuwiki#commands#open_diary_path(-1)<CR>
nnoremap <silent> <Leader>w<Leader>t :call nuwiki#commands#open_diary_path(1)<CR>
nnoremap <silent> <Leader>w<Leader>i :call nuwiki#commands#diary_generate_index()<CR>
endif