Files
nuwiki/plugin/nuwiki.vim
T

134 lines
6.5 KiB
VimL
Raw Normal View History

" plugin/nuwiki.vim — universal Vim/Neovim entry point.
"
" Neovim 0.11+ users are expected to call `require('nuwiki').setup({})` from
" their config (lazy.nvim does this automatically through `opts`). On Vim
" we start the LSP client when a vimwiki buffer is loaded.
"
" This file contains wiring only. The editor layers (VimL + Lua) are thin
" clients; the heavy lifting is done by the nuwiki-ls language server, which
" is built from the separate nuwiki-rs repo and downloaded at install time.
if exists('g:loaded_nuwiki')
finish
endif
let g:loaded_nuwiki = 1
" Plugin version, surfaced by :VimwikiShowVersion / :NuwikiShowVersion.
" Set before the Neovim early-return below so both clients see the global.
if !exists('g:nuwiki_version')
2026-06-29 11:38:14 +00:00
let g:nuwiki_version = '0.5.1'
endif
" Resolve the plugin root NOW, while this script is being sourced and
" <sfile> is still valid. Commands that need this path must reference
" s:plugin_root — re-expanding <sfile> inside a command body evaluates
" at execution time when there is no sourcing context, producing an
" empty path (hence the E484 "Cannot open ./scripts/…" error).
let s:plugin_root = fnamemodify(resolve(expand('<sfile>:p')), ':h:h')
" :NuwikiInstall — install the nuwiki-ls binary without needing a
" plugin-manager build hook. Available for both Vim and Neovim.
if has('nvim')
command! -nargs=0 NuwikiInstall lua require('nuwiki.install').install()
else
command! -nargs=0 NuwikiInstall execute 'source' fnameescape(s:plugin_root . '/scripts/download_bin.vim')
endif
" Calendar-Vim Integration — auto-detect when calendar-vim is loaded.
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)
return
endif
if exists('*calendar#open') || exists(':Calendar') == 2
" Wire nuwiki's diary hooks unless the user set a custom (non-default) one.
" calendar-vim's own default is 'calendar#diary' (writes to ~/diary); we
" overwrite that so paths come from the wiki root config instead.
let l:act = get(g:, 'calendar_action', '')
if l:act ==# '' || l:act ==# 'calendar#diary'
let g:calendar_action = 'vimwiki#diary#calendar_action'
endif
let l:sign = get(g:, 'calendar_sign', '')
if l:sign ==# '' || l:sign ==# 'calendar#sign'
let g:calendar_sign = 'vimwiki#diary#calendar_sign'
endif
endif
endfunction
if has('nvim')
" Neovim path is initialised by `require('nuwiki').setup()`. Nothing else
" to do at vimscript boot — wait until the user runs setup.
finish
endif
" Vim path: start the LSP client the first time a vimwiki buffer is opened.
augroup nuwiki_vim_lsp_start
autocmd!
autocmd FileType vimwiki call nuwiki#lsp#start()
augroup END
" vimwiki `auto_header` (default off): insert a level-1 header from the filename
" on new wiki pages. BufNewFile fires only for genuinely new files. Matches the
" configured extension(s).
if get(g:, 'nuwiki_auto_header', 0)
let s:awh_ext = get(g:, 'nuwiki_file_extension', '.wiki')
let s:awh_ext = s:awh_ext[0] ==# '.' ? s:awh_ext : '.' . s:awh_ext
augroup nuwiki_auto_header
autocmd!
execute 'autocmd BufNewFile *' . s:awh_ext . ' call nuwiki#commands#auto_header()'
augroup END
unlet s:awh_ext
endif
" Global wiki-picker commands — available from any buffer so a wiki can be
" chosen before a .wiki file exists. The picker reads its list from config
" (no LSP); opening the chosen index then auto-starts the server. The
" buffer-local :VimwikiUISelect in ftplugin/vimwiki.vim shadows these inside
" wiki buffers but dispatches to the same function.
command! -nargs=0 VimwikiUISelect call nuwiki#commands#wiki_ui_select()
command! -nargs=0 NuwikiUISelect call nuwiki#commands#wiki_ui_select()
" Print the nuwiki version + host editor (upstream's :VimwikiShowVersion).
command! -nargs=0 VimwikiShowVersion call nuwiki#commands#show_version()
command! -nargs=0 NuwikiShowVersion call nuwiki#commands#show_version()
" Global :VimwikiIndex / :VimwikiTabIndex entry points (upstream defines the
" Index family globally in plugin/). The buffer-local copies in
" ftplugin/vimwiki.vim shadow these inside wiki buffers; these let a wiki be
" opened from any buffer. `[count]` selects the wiki number.
command! -count=0 VimwikiIndex call nuwiki#commands#wiki_index(<count>)
command! -count=0 NuwikiIndex call nuwiki#commands#wiki_index(<count>)
command! -count=0 VimwikiTabIndex call nuwiki#commands#wiki_tab_index(<count>)
command! -count=0 NuwikiTabIndex call nuwiki#commands#wiki_tab_index(<count>)
" Get/set nuwiki client globals (upstream's :VimwikiVar).
command! -nargs=* VimwikiVar call nuwiki#commands#var(<q-args>)
command! -nargs=* NuwikiVar call nuwiki#commands#var(<q-args>)
" Global entry-point mappings — work from any buffer, not just .wiki files.
" Files are opened directly from config (no LSP required); the server
" auto-starts via the FileType autocmd once the vimwiki buffer loads.
if !get(g:, 'nuwiki_no_default_mappings', 0)
" Prefix mirrors vimwiki's g:vimwiki_map_prefix (default <Leader>w). Built
" with :exe so a custom prefix relocates the whole entry-point family.
let s:prefix = get(g:, 'nuwiki_map_prefix', '<Leader>w')
exe 'nnoremap <silent> ' . s:prefix . 'w :call nuwiki#commands#open_wiki_path(0)<CR>'
exe 'nnoremap <silent> ' . s:prefix . 't :call nuwiki#commands#open_wiki_path(1)<CR>'
exe 'nnoremap <silent> ' . s:prefix . 's :call nuwiki#commands#wiki_ui_select()<CR>'
exe 'nnoremap <silent> ' . s:prefix . 'i :call nuwiki#commands#open_diary_index_path()<CR>'
exe 'nnoremap <silent> ' . s:prefix . '<Leader>w :call nuwiki#commands#open_diary_path(0)<CR>'
exe 'nnoremap <silent> ' . s:prefix . '<Leader>y :call nuwiki#commands#open_diary_path(-1)<CR>'
exe 'nnoremap <silent> ' . s:prefix . '<Leader>t :tabnew<CR>:call nuwiki#commands#open_diary_path(0)<CR>'
exe 'nnoremap <silent> ' . s:prefix . '<Leader>m :call nuwiki#commands#open_diary_path(1)<CR>'
exe 'nnoremap <silent> ' . s:prefix . '<Leader>i :call nuwiki#commands#diary_generate_index()<CR>'
unlet s:prefix
endif