fix(mappings): add global entry-point keymaps and correct <Leader>ww/<Leader>wt targets
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:
@@ -13,6 +13,58 @@ 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
|
||||
|
||||
-- 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)
|
||||
config.apply(opts or {})
|
||||
|
||||
@@ -30,6 +82,7 @@ function M.setup(opts)
|
||||
end
|
||||
|
||||
lsp.register()
|
||||
_setup_global_mappings()
|
||||
end
|
||||
|
||||
function M.install()
|
||||
|
||||
@@ -160,8 +160,8 @@ function M.attach(bufnr, mappings)
|
||||
|
||||
-- ===== Wiki prefix (`<Leader>w*`) =====
|
||||
if on('wiki_prefix') then
|
||||
map('n', '<Leader>ww', cmd.diary_today, { desc = 'nuwiki: open default wiki / today' }, bufnr)
|
||||
map('n', '<Leader>wt', cmd.diary_today, { desc = 'nuwiki: open today\'s diary' }, bufnr)
|
||||
map('n', '<Leader>ww', function() cmd.wiki_index(0) end, { desc = 'nuwiki: open wiki index' }, 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>wi', cmd.diary_index, { desc = 'nuwiki: open diary index' }, bufnr)
|
||||
map('n', '<Leader>w<Leader>w', cmd.diary_today, { desc = 'nuwiki: today' }, bufnr)
|
||||
|
||||
Reference in New Issue
Block a user