From 24d690f32dd7b88dfb24ae9e36207f31345a0119 Mon Sep 17 00:00:00 2001 From: EinfachToll Date: Tue, 16 Sep 2014 10:18:40 +0200 Subject: [PATCH] Use relative paths in omnicomplete Fix #70 Ref #72 --- autoload/vimwiki/u.vim | 27 +++++++++++++++++++++++++++ ftplugin/vimwiki.vim | 21 +++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/autoload/vimwiki/u.vim b/autoload/vimwiki/u.vim index f838988..9104e64 100644 --- a/autoload/vimwiki/u.vim +++ b/autoload/vimwiki/u.vim @@ -83,6 +83,33 @@ function! vimwiki#u#escape(string) "{{{ return escape(a:string, '.*[]\^$') endfunction "}}} +function! vimwiki#u#wikify_path(path) "{{{ + let result = resolve(expand(a:path, ':p')) + if vimwiki#u#is_windows() + let result = substitute(result, '\\', '/', 'g') + endif + let result = vimwiki#u#chomp_slash(result) + return result +endfunction "}}} + +" Returns: the relative path from a:dir to a:file +function! vimwiki#u#relpath(dir, file) "{{{ + let result = [] + let dir = split(a:dir, '/') + let file = split(a:file, '/') + while (len(dir) > 0 && len(file) > 0) && dir[0] == file[0] + call remove(dir, 0) + call remove(file, 0) + endwhile + for segment in dir + let result += ['..'] + endfor + for segment in file + let result += [segment] + endfor + return join(result, '/') +endfunction "}}} + " Load concrete Wiki syntax: sets regexes and templates for headers and links function vimwiki#u#reload_regexes() "{{{ execute 'runtime! syntax/vimwiki_'.VimwikiGet('syntax').'.vim' diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim index 45dcacd..9781a67 100644 --- a/ftplugin/vimwiki.vim +++ b/ftplugin/vimwiki.vim @@ -69,16 +69,21 @@ function! Complete_wikifiles(findstart, base) if a:base =~# '^wiki\d:' let wikinumber = eval(matchstr(a:base, '^wiki\zs\d')) + if wikinumber >= len(g:vimwiki_list) + return [] + endif let directory = VimwikiGet('path', wikinumber) let ext = VimwikiGet('ext', wikinumber) let prefix = matchstr(a:base, '^wiki\d:\zs.*') let scheme = matchstr(a:base, '^wiki\d:\ze') elseif a:base =~# '^diary:' + let wikinumber = g:vimwiki_current_idx let directory = VimwikiGet('path').'/'.VimwikiGet('diary_rel_path') let ext = VimwikiGet('ext') let prefix = matchstr(a:base, '^diary:\zs.*') let scheme = matchstr(a:base, '^diary:\ze') - else + else " current wiki + let wikinumber = g:vimwiki_current_idx let directory = VimwikiGet('path') let ext = VimwikiGet('ext') let prefix = a:base @@ -86,12 +91,16 @@ function! Complete_wikifiles(findstart, base) endif let result = [] + if wikinumber == g:vimwiki_current_idx + let cwd = vimwiki#u#wikify_path(expand('%:p:h')) + else + let cwd = vimwiki#u#wikify_path(directory) + endif for wikifile in split(globpath(directory, '**/*'.ext), '\n') - " get the filename relative to the wiki path: - let subdir_filename = substitute(fnamemodify(wikifile, ':p:r'), - \ '\V'.fnamemodify(directory, ':p'), '', '') - if subdir_filename =~ '^'.vimwiki#u#escape(prefix) - call add(result, scheme . subdir_filename) + let wikifile = vimwiki#u#wikify_path(fnamemodify(wikifile, ':r')) + let relative_filename = vimwiki#u#relpath(cwd, wikifile) + if relative_filename =~ '^'.vimwiki#u#escape(prefix) + call add(result, scheme . relative_filename) endif endfor return result