feat(calendar): integrate calendar-vim with wiki diary paths
CI / cargo fmt --check (push) Successful in 34s
CI / cargo clippy (push) Successful in 43s
CI / cargo test (push) Successful in 41s
CI / editor keymaps (push) Successful in 1m29s

Wire g:calendar_action/g:calendar_sign to nuwiki diary hooks that
resolve diary paths from the wiki root config instead of calendar-vim's
default ~/diary. Auto-detected on FileType vimwiki when calendar-vim is
present; opt out with g:nuwiki_use_calendar=0 or g:nuwiki_no_calendar.

- autoload/nuwiki/diary.vim: calendar_action/calendar_sign using wiki cfg;
  open diary in the previous window and close the calendar window after.
- autoload/vimwiki/diary.vim: vimwiki# aliases for drop-in compatibility.
- plugin/nuwiki.vim + lua/nuwiki/init.lua: auto-wire hooks, overwriting
  calendar-vim's defaults while respecting user-set custom hooks.
- lua/nuwiki/config.lua: add use_calendar option (on by default).
- development/start-*.sh + _common.sh: clone calendar-vim, fix vimrc var
  expansion, pass wiki root to the Vim/Neovim clients.
This commit is contained in:
2026-05-31 21:34:41 -03:00
parent 39692ba99f
commit 7a3e11cfeb
9 changed files with 211 additions and 36 deletions
+97
View File
@@ -0,0 +1,97 @@
" autoload/nuwiki/diary.vim — diary-specific functions for nuwiki
" Includes calendar-vim integration functions
" ===== Calendar-Vim Integration =====
" 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. week and dir are not used.
" Opens or creates the diary entry for the given date.
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)
" 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
" Remember the calendar window so we can close it once the diary opens.
let l:cal_winid = win_getid()
" Open the diary file in a different window. If a previous window exists go
" there; otherwise split / vsplit / new depending on a:dir.
if winnr('#') > 0
execute 'wincmd p'
if !&hidden && &modified
execute 'new'
endif
elseif a:dir ==# 'V'
execute 'vsplit'
else
execute 'split'
endif
execute 'edit ' . fnameescape(l:file_path)
if !filereadable(l:file_path)
if l:c.ext[0] ==# '.'
execute 'setfiletype ' . l:c.ext[1:]
else
execute 'setfiletype ' . l:c.ext
endif
endif
" Close the calendar window now that the diary is open (keep at least one).
if win_getid() != l:cal_winid && win_id2win(l:cal_winid) > 0 && winnr('$') > 1
let l:diary_winid = win_getid()
if win_gotoid(l:cal_winid)
close
endif
call win_gotoid(l:diary_winid)
endif
endfunction
" Function: nuwiki#diary#calendar_sign(day, month, year)
" Called by calendar-vim to determine which dates show markers (*)
" day, month, year are numeric integers (1-based).
" Returns a single-character string to use as marker (e.g. '*' or ''),
" or 1/0 for 1-indexed calendars.
" calendar-vim's default expects the return value to be a single character
" to display; returning any non-empty string shows a marker.
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)
" Construct expected file path
let l:file_path = l:c.root . '/' . l:c.diary_rel . '/' . l:date_str . l:c.ext
" Return a marker if the file exists, empty string otherwise
if filereadable(l:file_path)
return '*'
endif
return ''
endfunction
" Helper: get wiki configuration for wiki n (0-based).
" Mirrors the same logic used by nuwiki#commands#open_diary_path etc.
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
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