2018-04-20 07:03:53 +02:00
|
|
|
" vim:tabstop=2:shiftwidth=2:expandtab:textwidth=99
|
2014-12-04 21:12:04 +01:00
|
|
|
" Vimwiki autoload plugin file
|
2018-04-20 07:03:53 +02:00
|
|
|
" Description: Path manipulation functions
|
2015-02-23 12:10:42 +01:00
|
|
|
" Home: https://github.com/vimwiki/vimwiki/
|
2014-12-04 21:12:04 +01:00
|
|
|
|
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#chomp_slash(str) abort
|
2014-12-04 21:12:04 +01:00
|
|
|
return substitute(a:str, '[/\\]\+$', '', '')
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
|
|
|
|
2014-12-04 21:12:04 +01:00
|
|
|
|
2015-02-06 23:50:04 +01:00
|
|
|
" Define path-compare function, either case-sensitive or not, depending on OS.
|
2015-02-09 09:20:30 +01:00
|
|
|
if vimwiki#u#is_windows()
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#is_equal(p1, p2) abort
|
2015-02-06 23:50:04 +01:00
|
|
|
return a:p1 ==? a:p2
|
|
|
|
endfunction
|
|
|
|
else
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#is_equal(p1, p2) abort
|
2015-02-09 20:05:25 +01:00
|
|
|
return a:p1 ==# a:p2
|
2015-02-06 23:50:04 +01:00
|
|
|
endfunction
|
2018-04-20 07:03:53 +02:00
|
|
|
endif
|
|
|
|
|
2020-05-09 02:45:32 +02:00
|
|
|
" collapse sections like /a/b/../c to /a/c and /a/b/./c to /a/b/c
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#normalize(path) abort
|
2014-12-04 21:12:04 +01:00
|
|
|
let path = a:path
|
|
|
|
while 1
|
2020-05-09 09:35:29 +02:00
|
|
|
let intermediateResult = substitute(path, '/[^/]\+/\.\.', '', '')
|
|
|
|
let result = substitute(intermediateResult, '/\./', '/', '')
|
2015-02-09 20:05:25 +01:00
|
|
|
if result ==# path
|
2014-12-04 21:12:04 +01:00
|
|
|
break
|
|
|
|
endif
|
|
|
|
let path = result
|
|
|
|
endwhile
|
|
|
|
return result
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
|
|
|
|
2014-12-04 21:12:04 +01:00
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#path_norm(path) abort
|
2014-12-04 21:12:04 +01:00
|
|
|
" /-slashes
|
|
|
|
if a:path !~# '^scp:'
|
|
|
|
let path = substitute(a:path, '\', '/', 'g')
|
|
|
|
" treat multiple consecutive slashes as one path separator
|
|
|
|
let path = substitute(path, '/\+', '/', 'g')
|
|
|
|
" ensure that we are not fooled by a symbolic link
|
|
|
|
return resolve(path)
|
|
|
|
else
|
|
|
|
return a:path
|
|
|
|
endif
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
|
|
|
|
2014-12-04 21:12:04 +01:00
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#is_link_to_dir(link) abort
|
2014-12-04 21:12:04 +01:00
|
|
|
" Check if link is to a directory.
|
|
|
|
" It should be ended with \ or /.
|
2015-03-13 16:31:15 +01:00
|
|
|
return a:link =~# '\m[/\\]$'
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
2014-12-04 21:12:04 +01:00
|
|
|
|
2018-04-20 07:03:53 +02:00
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#abs_path_of_link(link) abort
|
|
|
|
return vimwiki#path#normalize(expand('%:p:h').'/'.a:link)
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
|
|
|
|
2014-12-04 21:12:04 +01:00
|
|
|
|
|
|
|
" return longest common path prefix of 2 given paths.
|
|
|
|
" '~/home/usrname/wiki', '~/home/usrname/wiki/shmiki' => '~/home/usrname/wiki'
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#path_common_pfx(path1, path2) abort
|
2014-12-04 21:12:04 +01:00
|
|
|
let p1 = split(a:path1, '[/\\]', 1)
|
|
|
|
let p2 = split(a:path2, '[/\\]', 1)
|
|
|
|
|
|
|
|
let idx = 0
|
|
|
|
let minlen = min([len(p1), len(p2)])
|
2015-02-06 23:50:04 +01:00
|
|
|
while (idx < minlen) && vimwiki#path#is_equal(p1[idx], p2[idx])
|
2014-12-04 21:12:04 +01:00
|
|
|
let idx = idx + 1
|
|
|
|
endwhile
|
|
|
|
if idx == 0
|
|
|
|
return ''
|
|
|
|
else
|
|
|
|
return join(p1[: idx-1], '/')
|
|
|
|
endif
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
|
|
|
|
2014-12-04 21:12:04 +01:00
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#wikify_path(path) abort
|
2018-02-16 17:47:32 +01:00
|
|
|
let result = resolve(fnamemodify(a:path, ':p'))
|
2014-12-04 21:12:04 +01:00
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
let result = substitute(result, '\\', '/', 'g')
|
|
|
|
endif
|
|
|
|
let result = vimwiki#path#chomp_slash(result)
|
|
|
|
return result
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
2014-12-04 21:12:04 +01:00
|
|
|
|
2018-02-16 17:47:32 +01:00
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#current_wiki_file() abort
|
2018-02-16 17:47:32 +01:00
|
|
|
return vimwiki#path#wikify_path(expand('%:p'))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-12-04 21:12:04 +01:00
|
|
|
" Returns: the relative path from a:dir to a:file
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#relpath(dir, file) abort
|
2020-01-05 05:21:52 +01:00
|
|
|
" Check if dir here ('.') -> return file
|
|
|
|
if empty(a:dir) || a:dir =~# '^\.[/\\]\?$'
|
|
|
|
return a:file
|
|
|
|
endif
|
2014-12-04 21:12:04 +01:00
|
|
|
let result = []
|
2019-04-20 17:28:32 +02:00
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
" TODO temporary fix see #478
|
|
|
|
" not sure why paths get converted back to using forward slash
|
|
|
|
" when passed to the function in the form C:\path\to\file
|
|
|
|
let dir = substitute(a:dir, '/', '\', 'g')
|
|
|
|
let file = substitute(a:file, '/', '\', 'g')
|
|
|
|
let dir = split(dir, '\')
|
|
|
|
let file = split(file, '\')
|
|
|
|
else
|
|
|
|
let dir = split(a:dir, '/')
|
|
|
|
let file = split(a:file, '/')
|
|
|
|
endif
|
2015-02-06 23:50:04 +01:00
|
|
|
while (len(dir) > 0 && len(file) > 0) && vimwiki#path#is_equal(dir[0], file[0])
|
2014-12-04 21:12:04 +01:00
|
|
|
call remove(dir, 0)
|
|
|
|
call remove(file, 0)
|
|
|
|
endwhile
|
2015-04-09 14:48:26 +02:00
|
|
|
if empty(dir) && empty(file)
|
2019-04-20 17:28:32 +02:00
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
" TODO temporary fix see #478
|
|
|
|
return '.\'
|
|
|
|
else
|
|
|
|
return './'
|
|
|
|
endif
|
2015-04-09 14:48:26 +02:00
|
|
|
endif
|
2014-12-04 21:12:04 +01:00
|
|
|
for segment in dir
|
|
|
|
let result += ['..']
|
|
|
|
endfor
|
|
|
|
for segment in file
|
|
|
|
let result += [segment]
|
|
|
|
endfor
|
2019-04-20 17:28:32 +02:00
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
" TODO temporary fix see #478
|
|
|
|
let result_path = join(result, '\')
|
2019-12-21 04:41:03 +01:00
|
|
|
if a:file =~? '\m\\$'
|
2019-04-20 17:28:32 +02:00
|
|
|
let result_path .= '\'
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let result_path = join(result, '/')
|
2019-12-21 04:41:03 +01:00
|
|
|
if a:file =~? '\m/$'
|
2019-04-20 17:28:32 +02:00
|
|
|
let result_path .= '/'
|
|
|
|
endif
|
2015-04-09 14:48:26 +02:00
|
|
|
endif
|
|
|
|
return result_path
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
|
|
|
|
2014-12-04 21:12:04 +01:00
|
|
|
|
|
|
|
" If the optional argument provided and nonzero,
|
2018-04-20 07:03:53 +02:00
|
|
|
" it will ask before creating a directory
|
2014-12-04 21:12:04 +01:00
|
|
|
" Returns: 1 iff directory exists or successfully created
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#mkdir(path, ...) abort
|
2014-12-04 21:12:04 +01:00
|
|
|
let path = expand(a:path)
|
|
|
|
|
|
|
|
if path =~# '^scp:'
|
|
|
|
" we can not do much, so let's pretend everything is ok
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if isdirectory(path)
|
|
|
|
return 1
|
|
|
|
else
|
2019-12-21 04:41:03 +01:00
|
|
|
if !exists('*mkdir')
|
2014-12-04 21:12:04 +01:00
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
let path = vimwiki#path#chomp_slash(path)
|
2016-12-21 22:11:44 +01:00
|
|
|
if vimwiki#u#is_windows() && !empty(vimwiki#vars#get_global('w32_dir_enc'))
|
2019-12-21 04:41:03 +01:00
|
|
|
let path = iconv(path, &encoding, vimwiki#vars#get_global('w32_dir_enc'))
|
2014-12-04 21:12:04 +01:00
|
|
|
endif
|
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
if a:0 && a:1 && input('Vimwiki: Make new directory: '.path."\n [y]es/[N]o? ") !~? '^y'
|
2014-12-04 21:12:04 +01:00
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
call mkdir(path, 'p')
|
2014-12-04 21:12:04 +01:00
|
|
|
return 1
|
|
|
|
endif
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
|
|
|
|
2015-04-09 14:48:26 +02:00
|
|
|
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#is_absolute(path) abort
|
2015-04-09 14:48:26 +02:00
|
|
|
if vimwiki#u#is_windows()
|
|
|
|
return a:path =~? '\m^\a:'
|
|
|
|
else
|
|
|
|
return a:path =~# '\m^/\|\~/'
|
|
|
|
endif
|
2018-04-20 07:03:53 +02:00
|
|
|
endfunction
|
2016-01-21 13:27:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
" Combine a directory and a file into one path, doesn't generate duplicate
|
|
|
|
" path separator in case the directory is also having an ending / or \. This
|
|
|
|
" is because on windows ~\vimwiki//.tags is invalid but ~\vimwiki/.tags is a
|
|
|
|
" valid path.
|
|
|
|
if vimwiki#u#is_windows()
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#join_path(directory, file) abort
|
2016-01-21 13:27:08 +01:00
|
|
|
let directory = vimwiki#path#chomp_slash(a:directory)
|
|
|
|
let file = substitute(a:file, '\m^[\\/]\+', '', '')
|
|
|
|
return directory . '/' . file
|
|
|
|
endfunction
|
|
|
|
else
|
2019-12-21 04:41:03 +01:00
|
|
|
function! vimwiki#path#join_path(directory, file) abort
|
2016-01-21 13:27:08 +01:00
|
|
|
let directory = substitute(a:directory, '\m/\+$', '', '')
|
|
|
|
let file = substitute(a:file, '\m^/\+', '', '')
|
|
|
|
return directory . '/' . file
|
|
|
|
endfunction
|
|
|
|
endif
|
2018-04-20 07:03:53 +02:00
|
|
|
|