diff --git a/autoload/nuwiki/commands.vim b/autoload/nuwiki/commands.vim index 6e9d3d6..b8a57d4 100644 --- a/autoload/nuwiki/commands.vim +++ b/autoload/nuwiki/commands.vim @@ -348,28 +348,44 @@ endfunction " 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') +" Return a dict with {root, ext, idx, diary_rel, diary_idx} for wiki #n +" (0-based). Prefers g:nuwiki_wikis[n]; falls back to scalar g:nuwiki_* +" 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 - 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 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) + let l:c = s:wiki_cfg(0) + let l:date = strftime('%Y-%m-%d', localtime() + a:day_offset * 86400) + execute 'edit ' . fnameescape(l:c.root . '/' . l:c.diary_rel + \ . '/' . l:date . l:c.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) + let l:c = s:wiki_cfg(0) + execute 'edit ' . fnameescape(l:c.root . '/' . l:c.diary_rel + \ . '/' . l:c.diary_idx . l:c.ext) endfunction " ===== List + heading editing ===== diff --git a/lua/nuwiki/init.lua b/lua/nuwiki/init.lua index 9ac94f5..06638d1 100644 --- a/lua/nuwiki/init.lua +++ b/lua/nuwiki/init.lua @@ -18,32 +18,48 @@ local function _open(path, open_cmd) vim.cmd((open_cmd or 'edit') .. ' ' .. vim.fn.fnameescape(vim.fn.expand(path))) 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. 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 + local c = _wiki_cfg(n) + return c.root .. '/' .. c.idx .. c.ext end -- 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 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 + local c = _wiki_cfg(0) if day_offset == nil then - local didx = (w and w.diary_index) or 'diary' - return root .. '/' .. rel .. '/' .. didx .. ext + return c.root .. '/' .. c.diary_rel .. '/' .. c.diary_idx .. c.ext end 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 -- Register global (non-buffer-local) entry-point keymaps so users can open