diff --git a/autoload/nuwiki/diary.vim b/autoload/nuwiki/diary.vim index acfebea..55acc25 100644 --- a/autoload/nuwiki/diary.vim +++ b/autoload/nuwiki/diary.vim @@ -3,6 +3,33 @@ " ===== Calendar-Vim Integration ===== +" The wiki index (0-based) of the most recently entered wiki buffer. The +" calendar shows / creates diary entries for this wiki, so opening the calendar +" while editing `ifood_wiki` diaries into ifood_wiki — not always the first +" wiki. Updated by the BufEnter autocmd in plugin/nuwiki.vim; defaults to 0. +" (The LSP-driven diary commands already follow the current buffer via its URI; +" this mirrors that for the VimL calendar-vim callbacks.) +let s:current_wiki_idx = 0 + +" Record the wiki that owns `file` as the current one. Called on entering a +" wiki buffer. No-op when the file is under no configured wiki. +function! nuwiki#diary#track_wiki(file) abort + let l:file = fnamemodify(a:file, ':p') + if empty(l:file) + return + endif + let l:n = 0 + for l:w in nuwiki#commands#wiki_list() + let l:root = fnamemodify(expand(l:w.root), ':p') + if l:root[len(l:root) - 1:] !=# '/' | let l:root .= '/' | endif + if l:file[: len(l:root) - 1] ==# l:root + let s:current_wiki_idx = l:n + return + endif + let l:n += 1 + endfor +endfunction + " Function: nuwiki#diary#calendar_action(day, month, year, week, dir) " Called by calendar-vim when user presses Enter on a date. " day/month/year are numeric integers; dir is 'V' for a vertical split (any @@ -12,8 +39,8 @@ function! nuwiki#diary#calendar_action(day, month, year, week, dir) abort " Build YYYY-MM-DD from the parts passed by calendar-vim let l:date_str = printf('%04d-%02d-%02d', a:year, a:month, a:day) - " Get wiki configuration for the first wiki (index 0) - let l:c = s:wiki_cfg(0) + " Wiki of the buffer the calendar was opened from (falls back to the first). + let l:c = s:wiki_cfg(s:current_wiki_idx) " Construct file path: {root}/{diary_rel}/{YYYY-MM-DD}.{ext} let l:file_path = l:c.root . '/' . l:c.diary_rel . '/' . l:date_str . l:c.ext @@ -62,8 +89,8 @@ function! nuwiki#diary#calendar_sign(day, month, year) abort " Build YYYY-MM-DD from the parts passed by calendar-vim let l:date_str = printf('%04d-%02d-%02d', a:year, a:month, a:day) - " Get wiki configuration for the first wiki (index 0) - let l:c = s:wiki_cfg(0) + " Wiki of the buffer the calendar was opened from (falls back to the first). + let l:c = s:wiki_cfg(s:current_wiki_idx) " Construct expected file path let l:file_path = l:c.root . '/' . l:c.diary_rel . '/' . l:date_str . l:c.ext diff --git a/development/tests/test-calendar-vim.vim b/development/tests/test-calendar-vim.vim index d50dabc..435c704 100644 --- a/development/tests/test-calendar-vim.vim +++ b/development/tests/test-calendar-vim.vim @@ -89,6 +89,24 @@ call s:record(winnr('$') == s:wins_before - 1, 'action.window_count_decremented' call s:record(&filetype ==# 'vimwiki', 'action.sets_filetype', \ 'filetype=' . &filetype) +" ===== calendar follows the current wiki (multi-wiki) ===== +" Two wikis, each with its own diary entry. track_wiki() (driven by the +" BufEnter autocmd in real use) switches which wiki the calendar reads. +let s:wA = tempname() | call mkdir(s:wA . '/diary', 'p') +let s:wB = tempname() | call mkdir(s:wB . '/diary', 'p') +call writefile(['a'], s:wA . '/diary/2026-06-02.wiki') +call writefile(['b'], s:wB . '/diary/2026-06-10.wiki') +let g:nuwiki_wikis = [{'root': s:wA}, {'root': s:wB}] + +call nuwiki#diary#track_wiki(s:wA . '/index.wiki') +call s:record(nuwiki#diary#calendar_sign(2, 6, 2026) ==# '*', 'track.wikiA_own_entry', '') +call s:record(nuwiki#diary#calendar_sign(10, 6, 2026) ==# '', 'track.wikiA_not_wikiB_entry', '') + +call nuwiki#diary#track_wiki(s:wB . '/index.wiki') +call s:record(nuwiki#diary#calendar_sign(10, 6, 2026) ==# '*', 'track.wikiB_own_entry', '') +call s:record(nuwiki#diary#calendar_sign(2, 6, 2026) ==# '', 'track.wikiB_not_wikiA_entry', '') +unlet g:nuwiki_wikis + " ===== Wrap up ===== call add(s:results, '') diff --git a/plugin/nuwiki.vim b/plugin/nuwiki.vim index 86e1b51..3b6ca56 100644 --- a/plugin/nuwiki.vim +++ b/plugin/nuwiki.vim @@ -37,6 +37,11 @@ endif augroup nuwiki_calendar_detect autocmd! autocmd FileType vimwiki call s:nuwiki_setup_calendar() + " Track the wiki of the active buffer so the calendar diaries into the wiki + " you're editing (not always the first one). FileType covers initial load; + " BufEnter covers switching back to an already-open wiki buffer. + autocmd FileType vimwiki call nuwiki#diary#track_wiki(expand('%:p')) + autocmd BufEnter * if &filetype ==# 'vimwiki' | call nuwiki#diary#track_wiki(expand('%:p')) | endif augroup END function! s:nuwiki_setup_calendar() abort if !get(g:, 'nuwiki_use_calendar', 1) || get(g:, 'nuwiki_no_calendar', 0)