fix(mappings): respect g:nuwiki_wikis in global entry-point helpers
The global <Leader>ww / diary helpers read g:nuwiki_wiki_root directly, so they always opened the root-level index.wiki even when the user had configured per-wiki roots via g:nuwiki_wikis (e.g. personal_wiki.root = '~/.vimwiki/personal_wiki'). Vim path (autoload): replace the three open_*_path functions with a shared s:wiki_cfg(n) helper that checks g:nuwiki_wikis[n] first, then falls back to the scalar g:nuwiki_* vars and built-in defaults. Lua path (init.lua): replace the separate _wiki_index_path /_diary_path locals with a unified _wiki_cfg() that checks setup() opts.wikis, then vim.g.nuwiki_wikis (for users who configure via VimL rather than setup()), then scalar opts / g: vars. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -348,28 +348,44 @@ endfunction
|
|||||||
" open their wiki from any buffer. Files are opened directly from config;
|
" open their wiki from any buffer. Files are opened directly from config;
|
||||||
" the LSP auto-starts via the FileType autocmd once the buffer loads.
|
" the LSP auto-starts via the FileType autocmd once the buffer loads.
|
||||||
|
|
||||||
function! nuwiki#commands#open_wiki_path(tab) abort
|
" Return a dict with {root, ext, idx, diary_rel, diary_idx} for wiki #n
|
||||||
let l:root = expand(get(g:, 'nuwiki_wiki_root', '~/vimwiki'))
|
" (0-based). Prefers g:nuwiki_wikis[n]; falls back to scalar g:nuwiki_*
|
||||||
let l:ext = get(g:, 'nuwiki_file_extension', '.wiki')
|
" vars and finally to built-in defaults.
|
||||||
|
function! s:wiki_cfg(n) abort
|
||||||
|
let l:w = {}
|
||||||
|
if exists('g:nuwiki_wikis') && len(g:nuwiki_wikis) > a:n
|
||||||
|
let l:w = g:nuwiki_wikis[a:n]
|
||||||
|
endif
|
||||||
|
let l:root = expand(get(l:w, 'root',
|
||||||
|
\ get(g:, 'nuwiki_wiki_root', '~/vimwiki')))
|
||||||
|
let l:ext = get(l:w, 'file_extension',
|
||||||
|
\ get(g:, 'nuwiki_file_extension', '.wiki'))
|
||||||
if l:ext[0] !=# '.' | let l:ext = '.' . l:ext | endif
|
if l:ext[0] !=# '.' | let l:ext = '.' . l:ext | endif
|
||||||
execute (a:tab ? 'tabedit' : 'edit') . ' ' . fnameescape(l:root . '/index' . l:ext)
|
let l:idx = get(l:w, 'index', 'index')
|
||||||
|
let l:diary_rel = get(l:w, 'diary_rel_path',
|
||||||
|
\ get(g:, 'nuwiki_diary_rel_path', 'diary'))
|
||||||
|
let l:diary_idx = get(l:w, 'diary_index', 'diary')
|
||||||
|
return { 'root': l:root, 'ext': l:ext, 'idx': l:idx,
|
||||||
|
\ 'diary_rel': l:diary_rel, 'diary_idx': l:diary_idx }
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! nuwiki#commands#open_wiki_path(tab) abort
|
||||||
|
let l:c = s:wiki_cfg(0)
|
||||||
|
execute (a:tab ? 'tabedit' : 'edit') . ' '
|
||||||
|
\ . fnameescape(l:c.root . '/' . l:c.idx . l:c.ext)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! nuwiki#commands#open_diary_path(day_offset) abort
|
function! nuwiki#commands#open_diary_path(day_offset) abort
|
||||||
let l:root = expand(get(g:, 'nuwiki_wiki_root', '~/vimwiki'))
|
let l:c = s:wiki_cfg(0)
|
||||||
let l:ext = get(g:, 'nuwiki_file_extension', '.wiki')
|
let l:date = strftime('%Y-%m-%d', localtime() + a:day_offset * 86400)
|
||||||
if l:ext[0] !=# '.' | let l:ext = '.' . l:ext | endif
|
execute 'edit ' . fnameescape(l:c.root . '/' . l:c.diary_rel
|
||||||
let l:diary = get(g:, 'nuwiki_diary_rel_path', 'diary')
|
\ . '/' . l:date . l:c.ext)
|
||||||
let l:date = strftime('%Y-%m-%d', localtime() + a:day_offset * 86400)
|
|
||||||
execute 'edit ' . fnameescape(l:root . '/' . l:diary . '/' . l:date . l:ext)
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! nuwiki#commands#open_diary_index_path() abort
|
function! nuwiki#commands#open_diary_index_path() abort
|
||||||
let l:root = expand(get(g:, 'nuwiki_wiki_root', '~/vimwiki'))
|
let l:c = s:wiki_cfg(0)
|
||||||
let l:ext = get(g:, 'nuwiki_file_extension', '.wiki')
|
execute 'edit ' . fnameescape(l:c.root . '/' . l:c.diary_rel
|
||||||
if l:ext[0] !=# '.' | let l:ext = '.' . l:ext | endif
|
\ . '/' . l:c.diary_idx . l:c.ext)
|
||||||
let l:diary = get(g:, 'nuwiki_diary_rel_path', 'diary')
|
|
||||||
execute 'edit ' . fnameescape(l:root . '/' . l:diary . '/diary' . l:ext)
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" ===== List + heading editing =====
|
" ===== List + heading editing =====
|
||||||
|
|||||||
+33
-17
@@ -18,32 +18,48 @@ local function _open(path, open_cmd)
|
|||||||
vim.cmd((open_cmd or 'edit') .. ' ' .. vim.fn.fnameescape(vim.fn.expand(path)))
|
vim.cmd((open_cmd or 'edit') .. ' ' .. vim.fn.fnameescape(vim.fn.expand(path)))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Return the config dict for wiki #n (0-based).
|
||||||
|
-- Priority: setup() opts.wikis → g:nuwiki_wikis (VimL) → scalar opts/g: vars.
|
||||||
|
local function _wiki_cfg(n)
|
||||||
|
local opts = config.options
|
||||||
|
local wikis = opts.wikis
|
||||||
|
or (vim.g.nuwiki_wikis and vim.g.nuwiki_wikis or nil)
|
||||||
|
local w = wikis and wikis[(n or 0) + 1] or nil
|
||||||
|
local root = (w and w.root)
|
||||||
|
or opts.wiki_root
|
||||||
|
or vim.g.nuwiki_wiki_root
|
||||||
|
or '~/vimwiki'
|
||||||
|
local ext = (w and w.file_extension)
|
||||||
|
or opts.file_extension
|
||||||
|
or vim.g.nuwiki_file_extension
|
||||||
|
or '.wiki'
|
||||||
|
if not ext:match('^%.') then ext = '.' .. ext end
|
||||||
|
return {
|
||||||
|
root = root,
|
||||||
|
ext = ext,
|
||||||
|
idx = (w and w.index) or 'index',
|
||||||
|
diary_rel = (w and w.diary_rel_path)
|
||||||
|
or vim.g.nuwiki_diary_rel_path
|
||||||
|
or 'diary',
|
||||||
|
diary_idx = (w and w.diary_index) or 'diary',
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
-- Compute the wiki index path for wiki #n (0-based) from config.
|
-- Compute the wiki index path for wiki #n (0-based) from config.
|
||||||
local function _wiki_index_path(n)
|
local function _wiki_index_path(n)
|
||||||
local opts = config.options
|
local c = _wiki_cfg(n)
|
||||||
local w = opts.wikis and opts.wikis[(n or 0) + 1]
|
return c.root .. '/' .. c.idx .. c.ext
|
||||||
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
|
end
|
||||||
|
|
||||||
-- Compute a diary file path from config. day_offset=0 → today.
|
-- Compute a diary file path from config. day_offset=0 → today.
|
||||||
-- Pass nil for the diary index file itself.
|
-- Pass nil to get the diary index file instead.
|
||||||
local function _diary_path(day_offset)
|
local function _diary_path(day_offset)
|
||||||
local opts = config.options
|
local c = _wiki_cfg(0)
|
||||||
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
|
if day_offset == nil then
|
||||||
local didx = (w and w.diary_index) or 'diary'
|
return c.root .. '/' .. c.diary_rel .. '/' .. c.diary_idx .. c.ext
|
||||||
return root .. '/' .. rel .. '/' .. didx .. ext
|
|
||||||
end
|
end
|
||||||
local t = os.time() + day_offset * 86400
|
local t = os.time() + day_offset * 86400
|
||||||
return root .. '/' .. rel .. '/' .. os.date('%Y-%m-%d', t) .. ext
|
return c.root .. '/' .. c.diary_rel .. '/' .. os.date('%Y-%m-%d', t) .. c.ext
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Register global (non-buffer-local) entry-point keymaps so users can open
|
-- Register global (non-buffer-local) entry-point keymaps so users can open
|
||||||
|
|||||||
Reference in New Issue
Block a user