Use relative paths in omnicomplete

Fix #70
Ref #72
This commit is contained in:
EinfachToll 2014-09-16 10:18:40 +02:00
parent 21b3f63a47
commit 24d690f32d
2 changed files with 42 additions and 6 deletions

View File

@ -83,6 +83,33 @@ function! vimwiki#u#escape(string) "{{{
return escape(a:string, '.*[]\^$') return escape(a:string, '.*[]\^$')
endfunction "}}} 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 " Load concrete Wiki syntax: sets regexes and templates for headers and links
function vimwiki#u#reload_regexes() "{{{ function vimwiki#u#reload_regexes() "{{{
execute 'runtime! syntax/vimwiki_'.VimwikiGet('syntax').'.vim' execute 'runtime! syntax/vimwiki_'.VimwikiGet('syntax').'.vim'

View File

@ -69,16 +69,21 @@ function! Complete_wikifiles(findstart, base)
if a:base =~# '^wiki\d:' if a:base =~# '^wiki\d:'
let wikinumber = eval(matchstr(a:base, '^wiki\zs\d')) let wikinumber = eval(matchstr(a:base, '^wiki\zs\d'))
if wikinumber >= len(g:vimwiki_list)
return []
endif
let directory = VimwikiGet('path', wikinumber) let directory = VimwikiGet('path', wikinumber)
let ext = VimwikiGet('ext', wikinumber) let ext = VimwikiGet('ext', wikinumber)
let prefix = matchstr(a:base, '^wiki\d:\zs.*') let prefix = matchstr(a:base, '^wiki\d:\zs.*')
let scheme = matchstr(a:base, '^wiki\d:\ze') let scheme = matchstr(a:base, '^wiki\d:\ze')
elseif a:base =~# '^diary:' elseif a:base =~# '^diary:'
let wikinumber = g:vimwiki_current_idx
let directory = VimwikiGet('path').'/'.VimwikiGet('diary_rel_path') let directory = VimwikiGet('path').'/'.VimwikiGet('diary_rel_path')
let ext = VimwikiGet('ext') let ext = VimwikiGet('ext')
let prefix = matchstr(a:base, '^diary:\zs.*') let prefix = matchstr(a:base, '^diary:\zs.*')
let scheme = matchstr(a:base, '^diary:\ze') let scheme = matchstr(a:base, '^diary:\ze')
else else " current wiki
let wikinumber = g:vimwiki_current_idx
let directory = VimwikiGet('path') let directory = VimwikiGet('path')
let ext = VimwikiGet('ext') let ext = VimwikiGet('ext')
let prefix = a:base let prefix = a:base
@ -86,12 +91,16 @@ function! Complete_wikifiles(findstart, base)
endif endif
let result = [] 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') for wikifile in split(globpath(directory, '**/*'.ext), '\n')
" get the filename relative to the wiki path: let wikifile = vimwiki#u#wikify_path(fnamemodify(wikifile, ':r'))
let subdir_filename = substitute(fnamemodify(wikifile, ':p:r'), let relative_filename = vimwiki#u#relpath(cwd, wikifile)
\ '\V'.fnamemodify(directory, ':p'), '', '') if relative_filename =~ '^'.vimwiki#u#escape(prefix)
if subdir_filename =~ '^'.vimwiki#u#escape(prefix) call add(result, scheme . relative_filename)
call add(result, scheme . subdir_filename)
endif endif
endfor endfor
return result return result