123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- let s:cpo_save=&cpo
- set cpo&vim
-
-
-
-
- let s:k_version = 310
- function! lh#path#version()
- return s:k_version
- endfunction
-
-
- let s:verbose = 0
- function! lh#path#verbose(...)
- if a:0 > 0 | let s:verbose = a:1 | endif
- return s:verbose
- endfunction
-
- function! s:Verbose(expr)
- if s:verbose
- echomsg a:expr
- endif
- endfunction
-
- function! lh#path#debug(expr)
- return eval(a:expr)
- endfunction
-
-
-
-
-
-
- function! lh#path#simplify(pathname, ...)
- let make_relative_to_pwd = a:0 == 0 || a:1 == 1
- let pathname = simplify(a:pathname)
- let pathname = substitute(pathname, '^\%(\.[/\\]\)\+', '', '')
- let pathname = substitute(pathname, '\([/\\]\)\%(\.[/\\]\)\+', '\1', 'g')
- if make_relative_to_pwd
- let pwd = getcwd().'/'
- let pathname = substitute(pathname, '^'.lh#path#to_regex(pwd), '', 'g')
- endif
- return pathname
- endfunction
- function! lh#path#Simplify(pathname)
- return lh#path#simplify(a:pathname)
- endfunction
-
-
-
- function! lh#path#common(pathnames)
-
- let common = a:pathnames[0]
- let i = 1
- while i < len(a:pathnames)
- let fcrt = a:pathnames[i]
-
-
- let common = matchstr(common.'@@'.fcrt, '^\zs\(.*\>\)\ze.\{-}@@\1\>.*$')
- if strlen(common) == 0
-
- break
- endif
- let i += 1
- endwhile
- return common
- endfunction
-
-
-
- function! lh#path#strip_common(pathnames)
-
- let common = lh#path#common(a:pathnames)
- let common = lh#path#to_dirname(common)
- let l = strlen(common)
- if l == 0
- return a:pathnames
- else
- let pathnames = a:pathnames
- call map(pathnames, 'strpart(v:val, '.l.')' )
- return pathnames
- endif
- endfunction
- function! lh#path#StripCommon(pathnames)
- return lh#path#strip_common(a:pathnames)
- endfunction
-
-
- function! lh#path#is_absolute_path(path)
- return a:path =~ '^/'
- \ . '\|^[a-zA-Z]:[/\\]'
- \ . '\|^[/\\]\{2}'
-
-
-
- endfunction
- function! lh#path#IsAbsolutePath(path)
- return lh#path#is_absolute_path(a:path)
- endfunction
-
-
- function! lh#path#is_url(path)
-
- return a:path =~ '^\%(https\=\|s\=ftp\|dav\|fetch\|file\|rcp\|rsynch\|scp\)://'
- endfunction
- function! lh#path#IsURL(path)
- return lh#path#is_url(a:path)
- endfunction
-
-
- function! lh#path#select_one(pathnames, prompt)
- if len(a:pathnames) > 1
- let simpl_pathnames = deepcopy(a:pathnames)
- let simpl_pathnames = lh#path#strip_common(simpl_pathnames)
- let simpl_pathnames = [ '&Cancel' ] + simpl_pathnames
-
- let selection = confirm(a:prompt, join(simpl_pathnames,"\n"), 1, 'Question')
- let file = (selection == 1) ? '' : a:pathnames[selection-2]
- return file
- elseif len(a:pathnames) == 0
- return ''
- else
- return a:pathnames[0]
- endif
- endfunction
- function! lh#path#SelectOne(pathnames, prompt)
- return lh#path#select_one(a:pathnames, a:prompt)
- endfunction
-
-
- function! lh#path#to_relative(pathname)
- let newpath = fnamemodify(a:pathname, ':p:.')
- let newpath = simplify(newpath)
- return newpath
- endfunction
- function! lh#path#ToRelative(pathname)
- return lh#path#to_relative(a:pathname)
- endfunction
-
-
-
- function! lh#path#to_dirname(dirname)
- let dirname = a:dirname . (empty(a:dirname) || a:dirname[-1:] =~ '[/\\]' ? '' : '/')
- return dirname
- endfunction
-
-
- " todo: make a choice about "negative" paths like "../../foo
- function! lh#path#depth(dirname)
- if empty(a:dirname) | return 0 | endif
- let dirname = lh#path#to_dirname(a:dirname)
- let dirname = lh#path#simplify(dirname)
- if lh#path#is_absolute_path(dirname)
- let dirname = matchstr(dirname, '.\{-}[/\\]\zs.*')
- endif
- let depth = len(substitute(dirname, '[^/\\]\+[/\\]', '#', 'g'))
- return depth
- endfunction
-
-
-
-
-
- function! lh#path#relative_to(from, to)
-
-
- let from = lh#path#to_dirname(a:from)
- let to = lh#path#to_dirname(a:to )
- let [from, to] = lh#path#strip_common([from, to])
- let nb_up = lh#path#depth(from)
- return repeat('../', nb_up).to
-
-
-
- let pwd = getcwd()
- exe 'cd '.a:to
- let res = lh#path#to_relative(a:from)
- exe 'cd '.pwd
- return res
- endfunction
-
-
- function! s:GlobAsList(pathslist, expr, mustSort)
- let pathslist = type(a:pathslist) == type([]) ? join(a:pathslist, ',') : a:pathslist
- let sResult = globpath(pathslist, a:expr)
- let lResult = split(sResult, '\n')
-
- for ignored_pattern in split(&wildignore,',')
- if stridx(ignored_pattern,'/') != -1
- call filter(lResult, 'v:val !~ '.string(ignored_pattern))
- endif
- endfor
- return a:mustSort ? lh#list#unique_sort(lResult) : lResult
- endfunction
-
- function! lh#path#glob_as_list(pathslist, expr, ...)
- let mustSort = (a:0 > 0) ? (a:1) : 0
- if type(a:expr) == type('string')
- return s:GlobAsList(a:pathslist, a:expr, mustSort)
- elseif type(a:expr) == type([])
- let res = []
- for expr in a:expr
- call extend(res, s:GlobAsList(a:pathslist, expr, mustSort))
- endfor
- return res
- else
- throw "Unexpected type for a:expression"
- endif
- endfunction
- function! lh#path#GlobAsList(pathslist, expr)
- return lh#path#glob_as_list(a:pathslist, a:expr)
- endfunction
-
-
-
-
-
- " separated by ",
- function! lh#path#strip_start(pathname, pathslist)
- if type(a:pathslist) == type('string')
-
-
- let pathslist = split(a:pathslist, ',')
- elseif type(a:pathslist) == type([])
- let pathslist = deepcopy(a:pathslist)
- else
- throw "Unexpected type for a:pathname"
- endif
-
-
- let nb_paths = len(pathslist)
- let i = 0
- while i != nb_paths
- if pathslist[i] =~ '^\.\%(/\|$\)'
- let path2 = getcwd().pathslist[i][1:]
- call add(pathslist, path2)
- endif
- let i += 1
- endwhile
-
- call map(pathslist, 'substitute(v:val, "[\\\\/]", "[\\\\/]", "g")')
-
-
- call map(pathslist, '"^".escape(v:val, ".")')
- " handle "**
- call map(pathslist, 'substitute(v:val, "\\*\\*", "\\\\%([^\\\\/]*[\\\\/]\\\\)*", "g")')
- " reverse the list to use the real best match, which is "after
- call reverse(pathslist)
- if 0
-
- let strip_re = join(pathslist, '\|')
-
- let best_match = substitute(a:pathname, '\%('.strip_re.'\)[/\\]\=', '', '')
- else
- let best_match = ''
- for path in pathslist
- let a_match = substitute(a:pathname, '\%('.path.'\)[/\\]\=', '', '')
- if len(a_match) < len(best_match) || empty(best_match)
- let best_match = a_match
- endif
- endfor
- endif
- return best_match
- endfunction
- function! lh#path#StripStart(pathname, pathslist)
- return lh#path#strip_start(a:pathname, a:pathslist)
- endfunction
-
-
- function! lh#path#to_regex(path)
- let regex = substitute(a:path, '[/\\]', '[/\\\\]', 'g')
- return regex
- endfunction
-
-
- function! lh#path#find(paths, regex)
- let paths = (type(a:paths) == type([]))
- \ ? (a:paths)
- \ : split(a:paths,',')
- for path in paths
- if match(path ,a:regex) != -1
- return path
- endif
- endfor
- return ''
- endfunction
-
-
- function! lh#path#vimfiles()
- let expected_win = $HOME . '/vimfiles'
- let expected_nix = $HOME . '/.vim'
- let what = lh#path#to_regex($HOME.'/').'\(vimfiles\|.vim\)'
-
- let z = lh#path#find(&rtp,what)
- return z
- endfunction
-
-
- let &cpo=s:cpo_save
-
|