Generate links when diary & wiki dir are the same

When generating links, we first check that the file is not a diary file
as we don't want to include those in the list. That work is delegated to
the `is_diary_file` function. Prior to this change, the function always
returned true if the file was in the diary directory.
This approach gives false positives for a wiki which has a flat structure
and the wiki files and diary files share a directory. eg:

let wiki.diary_rel_path = './'

This change reuses existing diary functions from the diary.vim module to
get an exact list of diary files to check against.
This commit is contained in:
Patrick Stockwell 2020-05-09 10:45:32 +10:00 committed by Tinmarino
parent 1020ac51bf
commit b96e82d6cc
3 changed files with 16 additions and 13 deletions

View File

@ -391,9 +391,11 @@ function! vimwiki#base#generate_links(create) abort
call sort(links) call sort(links)
let bullet = repeat(' ', vimwiki#lst#get_list_margin()) . vimwiki#lst#default_symbol().' ' let bullet = repeat(' ', vimwiki#lst#get_list_margin()) . vimwiki#lst#default_symbol().' '
let l:diary_file_paths = vimwiki#diary#get_diary_files()
for link in links for link in links
let link_infos = vimwiki#base#resolve_link(link) let link_infos = vimwiki#base#resolve_link(link)
if !vimwiki#base#is_diary_file(link_infos.filename) if !vimwiki#base#is_diary_file(link_infos.filename, copy(l:diary_file_paths))
if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown' if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown'
let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template') let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template')
else else
@ -2133,12 +2135,15 @@ function! s:clean_url(url) abort
return join(url_l, ' ') return join(url_l, ' ')
endfunction endfunction
" An optional second argument allows you to pass in a list of diary files rather
function! vimwiki#base#is_diary_file(filename) abort " than generating a list on each call to the function.
let file_path = vimwiki#path#path_norm(a:filename) function! vimwiki#base#is_diary_file(filename, ...) abort
let rel_path = vimwiki#vars#get_wikilocal('diary_rel_path') let l:diary_file_paths = a:0 > 0 ? a:1 : vimwiki#diary#get_diary_files()
let diary_path = vimwiki#path#path_norm(vimwiki#vars#get_wikilocal('path') . rel_path) let l:normalised_file_paths =
return rel_path !=? '' && file_path =~# '^'.vimwiki#u#escape(diary_path) \ map(l:diary_file_paths, {_, path -> vimwiki#path#normalize(path)})
let l:matching_files =
\ filter(l:normalised_file_paths, {index, file -> file =~# a:filename})
return len(l:matching_files) > 0 " filename is a diary file if match is found
endfunction endfunction

View File

@ -44,7 +44,7 @@ function! s:get_position_links(link) abort
let idx = -1 let idx = -1
let links = [] let links = []
if a:link =~# '^\d\{4}-\d\d-\d\d' if a:link =~# '^\d\{4}-\d\d-\d\d'
let links = map(s:get_diary_files(), 'fnamemodify(v:val, ":t:r")') let links = map(vimwiki#diary#get_diary_files(), 'fnamemodify(v:val, ":t:r")')
" include 'today' into links " include 'today' into links
if index(links, vimwiki#diary#diary_date_link()) == -1 if index(links, vimwiki#diary#diary_date_link()) == -1
call add(links, vimwiki#diary#diary_date_link()) call add(links, vimwiki#diary#diary_date_link())
@ -157,7 +157,7 @@ function! s:read_captions(files) abort
endfunction endfunction
function! s:get_diary_files() abort function! vimwiki#diary#get_diary_files() abort
let rx = '^\d\{4}-\d\d-\d\d' let rx = '^\d\{4}-\d\d-\d\d'
let s_files = glob(vimwiki#vars#get_wikilocal('path'). let s_files = glob(vimwiki#vars#get_wikilocal('path').
\ vimwiki#vars#get_wikilocal('diary_rel_path').'*'.vimwiki#vars#get_wikilocal('ext')) \ vimwiki#vars#get_wikilocal('diary_rel_path').'*'.vimwiki#vars#get_wikilocal('ext'))
@ -201,7 +201,6 @@ function! s:sort(lst) abort
endif endif
endfunction endfunction
" The given wiki number a:wnum is 1 for the first wiki, 2 for the second and so on. This is in " The given wiki number a:wnum is 1 for the first wiki, 2 for the second and so on. This is in
" contrast to most other places, where counting starts with 0. When a:wnum is 0, the current wiki " contrast to most other places, where counting starts with 0. When a:wnum is 0, the current wiki
" is used. " is used.

View File

@ -20,12 +20,11 @@ else
endfunction endfunction
endif endif
" collapse sections like /a/b/../c to /a/c and /a/b/./c to /a/b/c
" collapse sections like /a/b/../c to /a/c
function! vimwiki#path#normalize(path) abort function! vimwiki#path#normalize(path) abort
let path = a:path let path = a:path
while 1 while 1
let result = substitute(path, '/[^/]\+/\.\.', '', '') let result = substitute(path, '/[^/]\+/\.\.\|\./', '', '')
if result ==# path if result ==# path
break break
endif endif