From d96870eadc319e1314c700c3a94a8ddb30e06ec8 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 26 May 2020 22:35:42 +0200 Subject: [PATCH] [nvim] Add Zettelkasten movement Can move through wiki by Zettel anchor IDs. Replaces standard wiki movement. Zettel IDs do not care about the directory they are in, as long as the ID is the same the Zettel can be located anywhere within the wiki root directory. Will need refinement and speed improvements in the future. --- nvim/.config/nvim/maps.vim | 27 ++++----- nvim/.config/nvim/plugin/wiki.vim | 95 ++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 42 deletions(-) diff --git a/nvim/.config/nvim/maps.vim b/nvim/.config/nvim/maps.vim index 0157d35..73edac6 100644 --- a/nvim/.config/nvim/maps.vim +++ b/nvim/.config/nvim/maps.vim @@ -199,23 +199,18 @@ nnoremap l :WikiFzfToc " overwrites some default mappings I don't use, or that interfere with my own " mappings TODO: currently this just assigns bogus shortcuts, since the plugin grumbles " when setting to an empty string -let g:wiki_mappings_global = { - \ '(wiki-journal)' : '===', - \ '(wiki-code-run)' : '====', - \ '(wiki-graph-in)' : '====', - \ '(wiki-graph-out)' : '=====', - \ '(wiki-link-toggle)' : '=======', - \ '(wiki-page-toc)' : '=========', - \ '(wiki-page-toc-local)' : '=============', - \ '(wiki-export)' : '==============', - \ '(wiki-list-uniq)' : '===============', - \ '(wiki-list-uniq-local)' : '================', - \ '(wiki-tag-list)' : '=================', - \ '(wiki-tag-reload)' : '==================', - \ '(wiki-tag-search)' : '=====================', - \ '(wiki-link-toggle-operator)' : '======================', + +let g:wiki_mappings_local = { + \ '(wiki-link-next)' : '', + \ '(wiki-link-prev)' : '', + \ '(wiki-link-return)' : '', + \ 'v_(wiki-link-toggle-visual)' : '', + \ '(wiki-graph-find-backlinks)' : 'wb', + \ '(wiki-graph-in)' : 'wg', + \ '(wiki-graph-out)' : 'wG', \} -au Filetype markdown,pandoc nnoremap gf :ZettelOpenAtCursor +" additional zettelkasten mapping +au Filetype markdown,pandoc nnoremap :ZettelOpenAtCursor " Mostly dealing with Prose writing from here on out " Format current Paragraph (esp useful in prose writing) diff --git a/nvim/.config/nvim/plugin/wiki.vim b/nvim/.config/nvim/plugin/wiki.vim index 5093a21..af9228f 100644 --- a/nvim/.config/nvim/plugin/wiki.vim +++ b/nvim/.config/nvim/plugin/wiki.vim @@ -7,7 +7,8 @@ endif " filetypes to automatically enable the plugin for, seems to take file endings " rather than vim ft let g:wiki_filetypes = ['md', 'mkd', 'markdown', 'wiki'] -let g:wiki_mappings_use_defaults = 'all' +let g:wiki_mappings_use_defaults = 'global' +let g:wiki_list_todos = ['[ ]', '[x]'] " wiki automatic link creation " add .md to the end of links by default @@ -16,31 +17,7 @@ let g:wiki_link_extension = '.md' " not in wiki format [[link|descr]] let g:wiki_link_target_type = 'md' -" 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! PrependDatestamp(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') - return l:text -endfunction - -" sets wiki.vim to automatically apply datestamp function to links -let g:wiki_map_link_target = "PrependDatestamp" - -let g:wiki_list_todos = ['[ ]', '[x]'] - -" set file opening schemes +" change file opening scheme of external wiki links function! WikiFileOpen(...) abort dict " use xdg-open for now -- can still customize if need be, see " :help wiki-config -> g:wiki_file_open for an example @@ -48,3 +25,69 @@ function! WikiFileOpen(...) abort dict return 1 endfunction 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 + " 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') + return l:text +endfunction +" sets wiki.vim to automatically apply datestamp function to created links +let g:wiki_map_link_create = "ZettelLink" + +" 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:match=match(l:notes, l:tomatch) + if l:match == -1 + return "" + else + return fnameescape(l:notes[l:match]) + endif +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) ) + + " fall back to normal link opening otherwise + if l:zettel ==? "" + call call(l:link.open, a:000, l:link) + else + execute ":e " . l:zettel + " call call(l:link.open, a:000, l:link) + endif + catch /E716:/ + call wiki#link#toggle(l:link) + catch /E37:/ + echoerr 'E37: Can''t open link before you''ve saved the current buffer.' + endtry +endfunction +command! ZettelOpenAtCursor :call ZettelOpenAtCursor()