[nvim] Fix zettelkasten wiki fw/bw navigation

Fixed navigating backwards ignoring any files jumped to via the
Zettelkasten function and only moving to the last wiki.vim jumped-to
file. Now correctly navigates to any file in the chain, be that zk or
wiki by invoking the correct wiki.vim page opening function.
This commit is contained in:
Marty Oehme 2020-06-25 09:43:57 +02:00
parent 6a22991026
commit e913d1c176
No known key found for this signature in database
GPG key ID: 0CCB0526EFB9611A
2 changed files with 35 additions and 31 deletions

View file

@ -1,5 +1,5 @@
" PLUGIN: wiki.vim
if $WIKIROOT ==? ""
if $WIKIROOT ==? ''
let g:wiki_root = expand('~/documents/notes')
else
let g:wiki_root = $WIKIROOT
@ -28,61 +28,49 @@ let g:wiki_file_open = 'WikiFileOpen'
" Zettelkasten functionality
" Creates a Zettelkasten compatible unique datestamp
function s:Getdatestamp()
if !exists("*strftime")
:echoerr "Date Stamp creation needs strftime available on system!"
endif
return strftime("%y%m%d%H%M")
endfunction
command! Datestamp :echom s:Getdatestamp()
" Appends Zettelkasten timestamp to beginning of links
function! ZettelLink(text) abort
function! ZettelLinkCreate(text) abort
" lowercase and replace spaces with dashes (-) to make links uniform
let l:text=substitute(tolower(a:text), '\s\+', '-', 'g')
" prepend datestamp
let l:text=substitute(l:text, '^', s:Getdatestamp() . ' '. '\0', 'g')
let l:text=substitute(l:text, '^', s:Datestamp() . ' '. '\0', 'g')
return l:text
endfunction
" sets wiki.vim to automatically apply datestamp function to created links
let g:wiki_map_link_create = "ZettelLink"
let g:wiki_map_link_create = 'ZettelLinkCreate'
let g:zettel_anchor_separator = ' '
let g:zettel_anchor_pattern = '[:/]\d\{10,14} '
" Returns the full path to the Zettel owning the anchor id passed in
function! s:GetZettelPath(anchor) abort
" match first 10-digit-beggining file ending with .md
" e.g. 1906061330 My example-Zettel.md
let l:tomatch='/' . trim(a:anchor) . ' .*\.md'
let l:notes=split(globpath(g:wiki_root, '**'), '\n')
let l:tomatch='/' . trim(a:anchor) . g:zettel_anchor_separator . '.*\.md'
let l:allnotes=split(globpath(g:wiki_root, '**'), '\n')
let l:match=match(l:notes, l:tomatch)
if l:match == -1
return ""
else
return fnameescape(l:notes[l:match])
endif
" get absolute path
let l:matched = matchstr(l:allnotes, l:tomatch)
" make path relative to root to pass back to wiki.vim
let l:curfpath = expand('%:p:h')
let l:matched = substitute(l:matched, l:curfpath, '', '')
return l:matched
endfunction
command! -nargs=1 ZettelOpen execute ":e " . s:GetZettelPath(<q-args>)
" Returns only the anchor id in a file name
" - returns empty string if nothing was found
function! s:ExtractAnchor(file) abort
return trim(matchstr(a:file, '[:/]\d\{10,14} '), ' \t\r:/')
endfunction
" Uses the Zettel Anchor ID instead of the whole link structure to traverse
" the whole Wiki directory from its root and opens first fitting Zettel
function! ZettelOpenAtCursor(...) abort
let l:link = wiki#link#get_at_cursor()
try
let l:zettel = s:GetZettelPath( s:ExtractAnchor(l:link.url) )
let l:zettel = s:GetZettelPath( s:ExtractZettelAnchor(l:link.url) )
" fall back to normal link opening otherwise
if l:zettel ==? ""
call call(l:link.open, a:000, l:link)
if l:zettel ==? ''
call wiki#link#open()
else
execute ":e " . l:zettel
" call call(l:link.open, a:000, l:link)
call wiki#page#open(l:zettel)
endif
catch /E716:/
call wiki#link#toggle(l:link)
@ -91,3 +79,18 @@ function! ZettelOpenAtCursor(...) abort
endtry
endfunction
command! ZettelOpenAtCursor :call ZettelOpenAtCursor()
" Returns only the anchor id in a file name
" - returns empty string if nothing was found
function! s:ExtractZettelAnchor(file) abort
return trim(matchstr(a:file, g:zettel_anchor_pattern), ' \t\r:/')
endfunction
" Creates a Zettelkasten compatible unique datestamp
function s:Datestamp()
if !exists('*strftime')
:echoerr 'Date Stamp creation needs strftime available on system!'
endif
return strftime('%y%m%d%H%M')
endfunction
command! Datestamp :echom s:Datestamp()

View file

@ -37,3 +37,4 @@ UNSC
plugin
Solidarność
transnationalism
heroization