From b96e82d6cc4b3512e15d2becb32ca13ce7e0cbd3 Mon Sep 17 00:00:00 2001 From: Patrick Stockwell Date: Sat, 9 May 2020 10:45:32 +1000 Subject: [PATCH] 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. --- autoload/vimwiki/base.vim | 19 ++++++++++++------- autoload/vimwiki/diary.vim | 5 ++--- autoload/vimwiki/path.vim | 5 ++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim index d83b031..e5e5028 100644 --- a/autoload/vimwiki/base.vim +++ b/autoload/vimwiki/base.vim @@ -391,9 +391,11 @@ function! vimwiki#base#generate_links(create) abort call sort(links) 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 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' let link_tpl = vimwiki#vars#get_syntaxlocal('Weblink1Template') else @@ -2133,12 +2135,15 @@ function! s:clean_url(url) abort return join(url_l, ' ') endfunction - -function! vimwiki#base#is_diary_file(filename) abort - let file_path = vimwiki#path#path_norm(a:filename) - let rel_path = vimwiki#vars#get_wikilocal('diary_rel_path') - let diary_path = vimwiki#path#path_norm(vimwiki#vars#get_wikilocal('path') . rel_path) - return rel_path !=? '' && file_path =~# '^'.vimwiki#u#escape(diary_path) +" An optional second argument allows you to pass in a list of diary files rather +" than generating a list on each call to the function. +function! vimwiki#base#is_diary_file(filename, ...) abort + let l:diary_file_paths = a:0 > 0 ? a:1 : vimwiki#diary#get_diary_files() + let l:normalised_file_paths = + \ 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 diff --git a/autoload/vimwiki/diary.vim b/autoload/vimwiki/diary.vim index b918a1e..cd983ea 100644 --- a/autoload/vimwiki/diary.vim +++ b/autoload/vimwiki/diary.vim @@ -44,7 +44,7 @@ function! s:get_position_links(link) abort let idx = -1 let links = [] 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 if index(links, vimwiki#diary#diary_date_link()) == -1 call add(links, vimwiki#diary#diary_date_link()) @@ -157,7 +157,7 @@ function! s:read_captions(files) abort endfunction -function! s:get_diary_files() abort +function! vimwiki#diary#get_diary_files() abort let rx = '^\d\{4}-\d\d-\d\d' let s_files = glob(vimwiki#vars#get_wikilocal('path'). \ vimwiki#vars#get_wikilocal('diary_rel_path').'*'.vimwiki#vars#get_wikilocal('ext')) @@ -201,7 +201,6 @@ function! s:sort(lst) abort endif endfunction - " 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 " is used. diff --git a/autoload/vimwiki/path.vim b/autoload/vimwiki/path.vim index 4150f94..6a7673e 100644 --- a/autoload/vimwiki/path.vim +++ b/autoload/vimwiki/path.vim @@ -20,12 +20,11 @@ else endfunction endif - -" collapse sections like /a/b/../c to /a/c +" collapse sections like /a/b/../c to /a/c and /a/b/./c to /a/b/c function! vimwiki#path#normalize(path) abort let path = a:path while 1 - let result = substitute(path, '/[^/]\+/\.\.', '', '') + let result = substitute(path, '/[^/]\+/\.\.\|\./', '', '') if result ==# path break endif