vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

fs.vim (5808B)


      1 " FUNCTIONS IN THIS FILE ARE MEANT TO BE USED BY NETRW.VIM AND NETRW.VIM ONLY.
      2 " THESE FUNCTIONS DON'T COMMIT TO ANY BACKWARDS COMPATIBILITY. SO CHANGES AND
      3 " BREAKAGES IF USED OUTSIDE OF NETRW.VIM ARE EXPECTED.
      4 
      5 
      6 " netrw#fs#PathJoin: Appends a new part to a path taking different systems into consideration {{{
      7 
      8 function! netrw#fs#PathJoin(...)
      9     const slash = !exists('+shellslash') || &shellslash ? '/' : '\'
     10     let path = ""
     11 
     12     for arg in a:000
     13         if empty(path)
     14             let path = arg
     15         else
     16             let path .= slash . arg
     17         endif
     18     endfor
     19 
     20     return path
     21 endfunction
     22 
     23 " }}}
     24 " netrw#fs#ComposePath: Appends a new part to a path taking different systems into consideration {{{
     25 
     26 function! netrw#fs#ComposePath(base, subdir)
     27     const slash = !exists('+shellslash') || &shellslash ? '/' : '\'
     28     if has('amiga')
     29         let ec = a:base[strdisplaywidth(a:base)-1]
     30         if ec != '/' && ec != ':'
     31             let ret = a:base . '/' . a:subdir
     32         else
     33             let ret = a:base.a:subdir
     34         endif
     35 
     36         " COMBAK: test on windows with changing to root directory: :e C:/
     37     elseif a:subdir =~ '^\a:[/\\]\([^/\\]\|$\)' && has('win32')
     38         let ret = a:subdir
     39 
     40     elseif a:base =~ '^\a:[/\\]\([^/\\]\|$\)' && has('win32')
     41         if a:base =~ '[/\\]$'
     42             let ret = a:base . a:subdir
     43         else
     44             let ret = a:base . slash . a:subdir
     45         endif
     46 
     47     elseif a:base =~ '^\a\{3,}://'
     48         let urlbase = substitute(a:base, '^\(\a\+://.\{-}/\)\(.*\)$', '\1', '')
     49         let curpath = substitute(a:base, '^\(\a\+://.\{-}/\)\(.*\)$', '\2', '')
     50         if a:subdir == '../'
     51             if curpath =~ '[^/]/[^/]\+/$'
     52                 let curpath = substitute(curpath, '[^/]\+/$', '', '')
     53             else
     54                 let curpath = ''
     55             endif
     56             let ret = urlbase.curpath
     57         else
     58             let ret = urlbase.curpath.a:subdir
     59         endif
     60 
     61     else
     62         let ret = substitute(a:base . '/' .a:subdir, '//', '/', 'g')
     63         if a:base =~ '^//'
     64             " keeping initial '//' for the benefit of network share listing support
     65             let ret = '/' . ret
     66         endif
     67         let ret = simplify(ret)
     68     endif
     69 
     70     return ret
     71 endfunction
     72 
     73 " }}}
     74 " netrw#fs#AbsPath: returns the full path to a directory and/or file {{{
     75 
     76 function! netrw#fs#AbsPath(path)
     77     let path = a:path->substitute('[\/]$', '', 'e')
     78 
     79     " Nothing to do
     80     if isabsolutepath(path)
     81         return path
     82     endif
     83 
     84     return path->fnamemodify(':p')->substitute('[\/]$', '', 'e')
     85 endfunction
     86 
     87 " }}}
     88 " netrw#fs#Cwd: get the current directory. {{{
     89 "   Change backslashes to forward slashes, if any.
     90 "   If doesc is true, escape certain troublesome characters
     91 
     92 function! netrw#fs#Cwd(doesc)
     93     let curdir = substitute(getcwd(), '\\', '/', 'ge')
     94 
     95     if curdir !~ '[\/]$'
     96         let curdir .= '/'
     97     endif
     98 
     99     if a:doesc
    100         let curdir = fnameescape(curdir)
    101     endif
    102 
    103     return curdir
    104 endfunction
    105 
    106 " }}}
    107 " netrw#fs#Glob: does glob() if local, remote listing otherwise {{{
    108 "     direntry: this is the name of the directory.  Will be fnameescape'd to prevent wildcard handling by glob()
    109 "     expr    : this is the expression to follow the directory.  Will use netrw#fs#ComposePath()
    110 "     pare    =1: remove the current directory from the resulting glob() filelist
    111 "             =0: leave  the current directory   in the resulting glob() filelist
    112 
    113 function! netrw#fs#Glob(direntry, expr, pare)
    114     if netrw#CheckIfRemote()
    115         keepalt 1sp
    116         keepalt enew
    117         let keep_liststyle = w:netrw_liststyle
    118         let w:netrw_liststyle = netrw#Expose('THINLIST')
    119         if netrw#Call('NetrwRemoteListing') == 0
    120             keepj keepalt %s@/@@
    121             let filelist = getline(1,$)
    122             q!
    123         else
    124             " remote listing error -- leave treedict unchanged
    125             let filelist = w:netrw_treedict[a:direntry]
    126         endif
    127         let w:netrw_liststyle = keep_liststyle
    128     else
    129         let path= netrw#fs#ComposePath(fnameescape(a:direntry), a:expr)
    130         if has("win32")
    131             " escape [ so it is not detected as wildcard character, see :h wildcard
    132             let path = substitute(path, '[', '[[]', 'g')
    133         endif
    134         let filelist = glob(path, 0, 1, 1)
    135         if a:pare
    136             let filelist = map(filelist,'substitute(v:val, "^.*/", "", "")')
    137         endif
    138     endif
    139 
    140     return filelist
    141 endfunction
    142 
    143 " }}}
    144 " netrw#fs#WinPath: tries to insure that the path is windows-acceptable, whether cygwin is used or not {{{
    145 
    146 function! netrw#fs#WinPath(path)
    147     if (!g:netrw_cygwin || &shell !~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$') && has("win32")
    148         " remove cygdrive prefix, if present
    149         let path = substitute(a:path, g:netrw_cygdrive . '/\(.\)', '\1:', '')
    150         " remove trailing slash (Win95)
    151         let path = substitute(path, '\(\\\|/\)$', '', 'g')
    152         " remove escaped spaces
    153         let path = substitute(path, '\ ', ' ', 'g')
    154         " convert slashes to backslashes
    155         let path = substitute(path, '/', '\', 'g')
    156     else
    157         let path = a:path
    158     endif
    159 
    160     return path
    161 endfunction
    162 
    163 " }}}
    164 " netrw#fs#Remove: deletes a file. {{{
    165 "           Uses Steve Hall's idea to insure that Windows paths stay
    166 "           acceptable.  No effect on Unix paths.
    167 
    168 function! netrw#fs#Remove(path)
    169     let path = netrw#fs#WinPath(a:path)
    170 
    171     if !g:netrw_cygwin && has("win32") && exists("+shellslash")
    172         let sskeep = &shellslash
    173         setl noshellslash
    174         let result = delete(path)
    175         let &shellslash = sskeep
    176     else
    177         let result = delete(path)
    178     endif
    179 
    180     if result < 0
    181         call netrw#msg#Notify('WARNING', printf('delete("%s") failed!', path))
    182     endif
    183 
    184     return result
    185 endfunction
    186 
    187 " }}}
    188 
    189 " vim:ts=8 sts=4 sw=4 et fdm=marker