From e913d1c176bb4e39bdb7cb916f9497a98d5122b1 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 25 Jun 2020 09:43:57 +0200 Subject: [PATCH] [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. --- nvim/.config/nvim/plugin/wiki.vim | 65 +++++++++++++++------------- nvim/.config/nvim/spell/en.utf-8.add | 1 + 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/nvim/.config/nvim/plugin/wiki.vim b/nvim/.config/nvim/plugin/wiki.vim index af9228f..289ba82 100644 --- a/nvim/.config/nvim/plugin/wiki.vim +++ b/nvim/.config/nvim/plugin/wiki.vim @@ -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() -" 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() diff --git a/nvim/.config/nvim/spell/en.utf-8.add b/nvim/.config/nvim/spell/en.utf-8.add index 0d9fdb8..e21e8f5 100644 --- a/nvim/.config/nvim/spell/en.utf-8.add +++ b/nvim/.config/nvim/spell/en.utf-8.add @@ -37,3 +37,4 @@ UNSC plugin Solidarność transnationalism +heroization