[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.
This commit is contained in:
Marty Oehme 2020-05-26 22:35:42 +02:00
parent 701d97389b
commit d96870eadc
No known key found for this signature in database
GPG Key ID: 0CCB0526EFB9611A
2 changed files with 80 additions and 42 deletions

View File

@ -199,23 +199,18 @@ nnoremap <localleader>l :WikiFzfToc<cr>
" 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 = {
\ '<plug>(wiki-journal)' : '<leader>===',
\ '<plug>(wiki-code-run)' : '<leader>====',
\ '<plug>(wiki-graph-in)' : '<leader>====',
\ '<plug>(wiki-graph-out)' : '<leader>=====',
\ '<plug>(wiki-link-toggle)' : '<leader>=======',
\ '<plug>(wiki-page-toc)' : '<leader>=========',
\ '<plug>(wiki-page-toc-local)' : '<leader>=============',
\ '<plug>(wiki-export)' : '<leader>==============',
\ '<plug>(wiki-list-uniq)' : '<leader>===============',
\ '<plug>(wiki-list-uniq-local)' : '<leader>================',
\ '<plug>(wiki-tag-list)' : '<leader>=================',
\ '<plug>(wiki-tag-reload)' : '<leader>==================',
\ '<plug>(wiki-tag-search)' : '<leader>=====================',
\ '<plug>(wiki-link-toggle-operator)' : '<leader>======================',
let g:wiki_mappings_local = {
\ '<plug>(wiki-link-next)' : '<tab>',
\ '<plug>(wiki-link-prev)' : '<s-tab>',
\ '<plug>(wiki-link-return)' : '<bs>',
\ 'v_<plug>(wiki-link-toggle-visual)' : '<cr>',
\ '<plug>(wiki-graph-find-backlinks)' : '<leader>wb',
\ '<plug>(wiki-graph-in)' : '<leader>wg',
\ '<plug>(wiki-graph-out)' : '<leader>wG',
\}
au Filetype markdown,pandoc nnoremap gf :ZettelOpenAtCursor<cr>
" additional zettelkasten mapping
au Filetype markdown,pandoc nnoremap <cr> :ZettelOpenAtCursor<cr>
" Mostly dealing with Prose writing from here on out
" Format current Paragraph (esp useful in prose writing)

View File

@ -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(<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) )
" 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()