From 9354e1c17644f33008fb245310789278a26f3692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Fr=C3=B3es=20Franco?= Date: Thu, 21 May 2026 21:34:41 -0300 Subject: [PATCH] fix(mappings): add global entry-point keymaps and correct ww/wt targets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two bugs prevented keymaps from working after a Dein install: 1. ww and 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=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 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 --- autoload/nuwiki/commands.vim | 30 ++++++++++++++++++++ ftplugin/vimwiki.vim | 4 +-- lua/nuwiki/init.lua | 53 ++++++++++++++++++++++++++++++++++++ lua/nuwiki/keymaps.lua | 4 +-- plugin/nuwiki.vim | 14 ++++++++++ 5 files changed, 101 insertions(+), 4 deletions(-) diff --git a/autoload/nuwiki/commands.vim b/autoload/nuwiki/commands.vim index ab627fe..6e9d3d6 100644 --- a/autoload/nuwiki/commands.vim +++ b/autoload/nuwiki/commands.vim @@ -342,6 +342,36 @@ function! nuwiki#commands#diary_generate_index() abort call s:exec('nuwiki.diary.generateIndex', [{ 'uri': s:buf_uri() }]) 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 ===== function! s:exec_pos(cmd) abort diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim index c3f6886..1d6cf6b 100644 --- a/ftplugin/vimwiki.vim +++ b/ftplugin/vimwiki.vim @@ -115,8 +115,8 @@ if !has('nvim') if !get(g:, 'nuwiki_no_default_mappings', 0) " Wiki prefix - nnoremap ww :call nuwiki#commands#diary_today() - nnoremap wt :call nuwiki#commands#diary_today() + nnoremap ww :call nuwiki#commands#wiki_index(0) + nnoremap wt :call nuwiki#commands#wiki_tab_index(0) nnoremap ws :call nuwiki#commands#wiki_ui_select() nnoremap wi :call nuwiki#commands#diary_index() nnoremap ww :call nuwiki#commands#diary_today() diff --git a/lua/nuwiki/init.lua b/lua/nuwiki/init.lua index d11cc32..9ac94f5 100644 --- a/lua/nuwiki/init.lua +++ b/lua/nuwiki/init.lua @@ -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', 'ww', function() _open(_wiki_index_path(0)) end, o('wiki index')) + kmap('n', 'wt', function() _open(_wiki_index_path(0), 'tabedit') end, o('wiki index (tab)')) + kmap('n', 'ws', function() require('nuwiki.commands').wiki_ui_select() end, o('pick wiki')) + kmap('n', 'wi', function() _open(_diary_path(nil)) end, o('diary index')) + kmap('n', 'ww', function() _open(_diary_path(0)) end, o('today diary')) + kmap('n', 'wy', function() _open(_diary_path(-1)) end, o('yesterday diary')) + kmap('n', 'wt', function() _open(_diary_path(1)) end, o('tomorrow diary')) + kmap('n', 'wi', 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() diff --git a/lua/nuwiki/keymaps.lua b/lua/nuwiki/keymaps.lua index e6127fc..d8d0c2c 100644 --- a/lua/nuwiki/keymaps.lua +++ b/lua/nuwiki/keymaps.lua @@ -160,8 +160,8 @@ function M.attach(bufnr, mappings) -- ===== Wiki prefix (`w*`) ===== if on('wiki_prefix') then - map('n', 'ww', cmd.diary_today, { desc = 'nuwiki: open default wiki / today' }, bufnr) - map('n', 'wt', cmd.diary_today, { desc = 'nuwiki: open today\'s diary' }, bufnr) + map('n', 'ww', function() cmd.wiki_index(0) end, { desc = 'nuwiki: open wiki index' }, bufnr) + map('n', 'wt', function() cmd.wiki_tab_index(0) end, { desc = 'nuwiki: open wiki index (tab)' }, bufnr) map('n', 'ws', cmd.wiki_ui_select, { desc = 'nuwiki: pick wiki' }, bufnr) map('n', 'wi', cmd.diary_index, { desc = 'nuwiki: open diary index' }, bufnr) map('n', 'ww', cmd.diary_today, { desc = 'nuwiki: today' }, bufnr) diff --git a/plugin/nuwiki.vim b/plugin/nuwiki.vim index 02298db..78f6aff 100644 --- a/plugin/nuwiki.vim +++ b/plugin/nuwiki.vim @@ -27,3 +27,17 @@ augroup END " :NuwikiInstall — convenience wrapper around scripts/download_bin.vim, so " users don't have to invoke a plugin-manager build hook by hand. command! -nargs=0 NuwikiInstall execute 'source' fnamemodify(resolve(expand(':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 ww :call nuwiki#commands#open_wiki_path(0) + nnoremap wt :call nuwiki#commands#open_wiki_path(1) + nnoremap ws :call nuwiki#commands#wiki_ui_select() + nnoremap wi :call nuwiki#commands#open_diary_index_path() + nnoremap ww :call nuwiki#commands#open_diary_path(0) + nnoremap wy :call nuwiki#commands#open_diary_path(-1) + nnoremap wt :call nuwiki#commands#open_diary_path(1) + nnoremap wi :call nuwiki#commands#diary_generate_index() +endif