Files
gffranco ddb9d0b916
CI / cargo fmt --check (push) Successful in 39s
CI / cargo clippy (push) Successful in 28s
CI / cargo test (push) Successful in 29s
CI / editor keymaps (push) Successful in 1m45s
fix(vars): E121 in get_wikilocal no-index path; add regression test
s:resolve_index referenced an undefined `a:file` (a copy-paste leftover
from wiki_root_for, whose parameter is named a:file) inside the
buffer-owning-wiki loop. Any get_wikilocal() call with no explicit index
while a wiki buffer was current — i.e. the ftplugin BufRead path — hit
`E121: undefined variable a:file`. The local is `l:file`, and the
surrounding `!empty(l:file)` guard already covers emptiness, so the
clause was both wrong and redundant; removed it.

Add development/tests/test-vars-vim.{sh,vim} covering the shim:
per-wiki-index resolution, global-default fallback, out-of-range clamp,
the no-index owning-wiki path (direct guard for this E121), and the
single-wiki shorthand. Wired into CI. 14/14 pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 14:57:14 +00:00

81 lines
2.7 KiB
VimL

" autoload/vimwiki/vars.vim — compatibility shim for plugins that depend on
" the original vimwiki API (e.g. vimwiki-sync, vim-zettel).
"
" Only the subset of `vimwiki#vars#get_wikilocal` keys that third-party
" plugins are known to call is implemented here; add entries as needed.
" Values are read from nuwiki's own config variables so users don't have
" to duplicate settings.
" Resolve which wiki a `get_wikilocal` call refers to, mirroring upstream:
" - an explicit numeric second argument selects that wiki (0-based);
" - otherwise the wiki owning the current buffer is used
" (upstream's `g:vimwiki_current_idx`);
" - failing that, the first/shorthand wiki (index 0).
function! s:resolve_index(...) abort
if a:0 >= 1 && type(a:1) == type(0)
return a:1
endif
let l:file = expand('%:p')
if !empty(l:file)
let l:n = 0
for l:w in nuwiki#commands#wiki_list()
let l:wroot = fnamemodify(expand(l:w.root), ':p')
if l:wroot[len(l:wroot) - 1:] !=# '/' | let l:wroot .= '/' | endif
if l:file[: len(l:wroot) - 1] ==# l:wroot
return l:n
endif
let l:n += 1
endfor
endif
return 0
endfunction
" Per-wiki local config values.
"
" Supported keys:
" path — wiki root directory (with trailing slash)
" is_temporary_wiki — always 0 for a configured wiki
" ext / extension — file extension (default '.wiki')
" syntax — syntax name (default 'vimwiki')
"
" The optional second argument is the wiki index (upstream's signature);
" when omitted, the wiki owning the current buffer is used. Values are
" pulled from the matching `g:nuwiki_wikis` entry, falling back to the
" scalar `g:nuwiki_*` globals for keys that entry doesn't set.
function! vimwiki#vars#get_wikilocal(key, ...) abort
if a:key ==# 'is_temporary_wiki'
return 0
endif
let l:idx = call('s:resolve_index', a:000)
let l:wikis = nuwiki#commands#wiki_list()
if l:idx < 0 || l:idx >= len(l:wikis)
let l:idx = 0
endif
let l:w = l:wikis[l:idx]
if a:key ==# 'path'
let l:root = expand(l:w.root)
" Ensure trailing slash to match what the original vimwiki returns.
return l:root =~# '/$' ? l:root : l:root . '/'
elseif a:key ==# 'ext' || a:key ==# 'extension'
" wiki_list() already normalises the leading dot.
return l:w.ext
elseif a:key ==# 'syntax'
" wiki_list() doesn't carry `syntax`; read the per-wiki entry, then the
" global default.
let l:cfg = (exists('g:nuwiki_wikis') && len(g:nuwiki_wikis) > l:idx)
\ ? g:nuwiki_wikis[l:idx] : {}
return get(l:cfg, 'syntax', get(g:, 'nuwiki_syntax', 'vimwiki'))
endif
return ''
endfunction
" Global (non-wiki-specific) config values.
function! vimwiki#vars#get_global(key) abort
return ''
endfunction