Merge pull request #29999 from zeertzjq/vim-a63f66e

vim-patch: zip plugin updates
This commit is contained in:
zeertzjq 2024-08-07 07:34:27 +08:00 committed by GitHub
commit 0c99ce0e89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 120 deletions

View File

@ -1,7 +1,7 @@
" zip.vim: Handles browsing zipfiles " zip.vim: Handles browsing zipfiles
" AUTOLOAD PORTION " AUTOLOAD PORTION
" Date: Aug 05, 2024 " Date: Aug 05, 2024
" Version: 33 " Version: 34
" Maintainer: This runtime file is looking for a new maintainer. " Maintainer: This runtime file is looking for a new maintainer.
" Former Maintainer: Charles E Campbell " Former Maintainer: Charles E Campbell
" Last Change: " Last Change:
@ -11,6 +11,7 @@
" 2024 Jul 30 by Vim Project: fix opening remote zipfile " 2024 Jul 30 by Vim Project: fix opening remote zipfile
" 2024 Aug 04 by Vim Project: escape '[' in name of file to be extracted " 2024 Aug 04 by Vim Project: escape '[' in name of file to be extracted
" 2024 Aug 05 by Vim Project: workaround for the FreeBSD's unzip " 2024 Aug 05 by Vim Project: workaround for the FreeBSD's unzip
" 2024 Aug 05 by Vim Project: clean-up and make it work with shellslash on Windows
" License: Vim License (see vim's :help license) " License: Vim License (see vim's :help license)
" Copyright: Copyright (C) 2005-2019 Charles E. Campbell {{{1 " Copyright: Copyright (C) 2005-2019 Charles E. Campbell {{{1
" Permission is hereby granted to use and distribute this code, " Permission is hereby granted to use and distribute this code,
@ -27,16 +28,9 @@
if &cp || exists("g:loaded_zip") if &cp || exists("g:loaded_zip")
finish finish
endif endif
let g:loaded_zip= "v33" let g:loaded_zip= "v34"
if v:version < 702
echohl WarningMsg
echo "***warning*** this version of zip needs vim 7.2 or later"
echohl Normal
finish
endif
let s:keepcpo= &cpo let s:keepcpo= &cpo
set cpo&vim set cpo&vim
"DechoTabOn
let s:zipfile_escape = ' ?&;\' let s:zipfile_escape = ' ?&;\'
let s:ERROR = 2 let s:ERROR = 2
@ -64,8 +58,22 @@ if !exists("g:zip_extractcmd")
let g:zip_extractcmd= g:zip_unzipcmd let g:zip_extractcmd= g:zip_unzipcmd
endif endif
" ---------------------------------------------------------------------
" required early
" s:Mess: {{{2
fun! s:Mess(group, msg)
redraw!
exe "echohl " . a:group
echomsg a:msg
echohl Normal
endfun
if v:version < 702
call s:Mess('WarningMsg', "***warning*** this version of zip needs vim 7.2 or later")
finish
endif
if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd) if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd)
echoerr "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!" call s:Mess('Error', "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!")
finish finish
endif endif
@ -83,29 +91,19 @@ fun! zip#Browse(zipfile)
return return
endif endif
let repkeep= &report let dict = s:SetSaneOpts()
set report=10 defer s:RestoreOpts(dict)
" sanity checks " sanity checks
if !exists("*fnameescape")
if &verbose > 1
echoerr "the zip plugin is not available (your vim doesn't support fnameescape())"
endif
return
endif
if !executable(g:zip_unzipcmd) if !executable(g:zip_unzipcmd)
redraw! call s:Mess('Error', "***error*** (zip#Browse) unzip not available on your system")
echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
let &report= repkeep
return return
endif endif
if !filereadable(a:zipfile) if !filereadable(a:zipfile)
if a:zipfile !~# '^\a\+://' if a:zipfile !~# '^\a\+://'
" if it's an url, don't complain, let url-handlers such as vim do its thing " if it's an url, don't complain, let url-handlers such as vim do its thing
redraw! call s:Mess('Error', "***error*** (zip#Browse) File not readable <".a:zipfile.">")
echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
endif endif
let &report= repkeep
return return
endif endif
if &ma != 1 if &ma != 1
@ -134,16 +132,13 @@ fun! zip#Browse(zipfile)
exe $"keepj sil r! {g:zip_unzipcmd} -Z1 -- {s:Escape(a:zipfile, 1)}" exe $"keepj sil r! {g:zip_unzipcmd} -Z1 -- {s:Escape(a:zipfile, 1)}"
if v:shell_error != 0 if v:shell_error != 0
redraw! call s:Mess('WarningMsg', "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file")
echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
keepj sil! %d keepj sil! %d
let eikeep= &ei let eikeep= &ei
set ei=BufReadCmd,FileReadCmd set ei=BufReadCmd,FileReadCmd
exe "keepj r ".fnameescape(a:zipfile) exe "keepj r ".fnameescape(a:zipfile)
let &ei= eikeep let &ei= eikeep
keepj 1d keepj 1d
" call Dret("zip#Browse")
return return
endif endif
@ -155,63 +150,46 @@ fun! zip#Browse(zipfile)
noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr> noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
endif endif
let &report= repkeep
" call Dret("zip#Browse")
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" ZipBrowseSelect: {{{2 " ZipBrowseSelect: {{{2
fun! s:ZipBrowseSelect() fun! s:ZipBrowseSelect()
" call Dfunc("ZipBrowseSelect() zipfile<".((exists("b:zipfile"))? b:zipfile : "n/a")."> curfile<".expand("%").">") let dict = s:SetSaneOpts()
let repkeep= &report defer s:RestoreOpts(dict)
set report=10
let fname= getline(".") let fname= getline(".")
if !exists("b:zipfile") if !exists("b:zipfile")
" call Dret("ZipBrowseSelect : b:zipfile doesn't exist!")
return return
endif endif
" sanity check " sanity check
if fname =~ '^"' if fname =~ '^"'
let &report= repkeep
" call Dret("ZipBrowseSelect")
return return
endif endif
if fname =~ '/$' if fname =~ '/$'
redraw! call s:Mess('Error', "***error*** (zip#Browse) Please specify a file, not a directory")
echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
let &report= repkeep
" call Dret("ZipBrowseSelect")
return return
endif endif
" call Decho("fname<".fname.">")
" get zipfile to the new-window " get zipfile to the new-window
let zipfile = b:zipfile let zipfile = b:zipfile
let curfile = expand("%") let curfile = expand("%")
" call Decho("zipfile<".zipfile.">")
" call Decho("curfile<".curfile.">")
noswapfile new noswapfile new
if !exists("g:zip_nomax") || g:zip_nomax == 0 if !exists("g:zip_nomax") || g:zip_nomax == 0
wincmd _ wincmd _
endif endif
let s:zipfile_{winnr()}= curfile let s:zipfile_{winnr()}= curfile
" call Decho("exe e ".fnameescape("zipfile://".zipfile.'::'.fname))
exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname) exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
filetype detect filetype detect
let &report= repkeep
" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" zip#Read: {{{2 " zip#Read: {{{2
fun! zip#Read(fname,mode) fun! zip#Read(fname,mode)
let repkeep= &report let dict = s:SetSaneOpts()
set report=10 defer s:RestoreOpts(dict)
if has("unix") if has("unix")
let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','') let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
@ -223,10 +201,7 @@ fun! zip#Read(fname,mode)
let fname = substitute(fname, '[', '[[]', 'g') let fname = substitute(fname, '[', '[[]', 'g')
" sanity check " sanity check
if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','','')) if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
redraw! call s:Mess('Error', "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program")
echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
let &report= repkeep
return return
endif endif
@ -246,50 +221,35 @@ fun! zip#Read(fname,mode)
" cleanup " cleanup
set nomod set nomod
let &report= repkeep
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" zip#Write: {{{2 " zip#Write: {{{2
fun! zip#Write(fname) fun! zip#Write(fname)
" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">") let dict = s:SetSaneOpts()
let repkeep= &report defer s:RestoreOpts(dict)
set report=10
" sanity checks " sanity checks
if !executable(substitute(g:zip_zipcmd,'\s\+.*$','','')) if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
redraw! call s:Mess('Error', "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program")
echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program" | echohl None
" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
let &report= repkeep
" call Dret("zip#Write")
return return
endif endif
if !exists("*mkdir") if !exists("*mkdir")
redraw! call s:Mess('Error', "***error*** (zip#Write) sorry, mkdir() doesn't work on your system")
echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
let &report= repkeep
" call Dret("zip#Write")
return return
endif endif
let curdir= getcwd() let curdir= getcwd()
let tmpdir= tempname() let tmpdir= tempname()
" call Decho("orig tempname<".tmpdir.">")
if tmpdir =~ '\.' if tmpdir =~ '\.'
let tmpdir= substitute(tmpdir,'\.[^.]*$','','e') let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
endif endif
" call Decho("tmpdir<".tmpdir.">")
call mkdir(tmpdir,"p") call mkdir(tmpdir,"p")
" attempt to change to the indicated directory " attempt to change to the indicated directory
if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory") if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
let &report= repkeep
" call Dret("zip#Write")
return return
endif endif
" call Decho("current directory now: ".getcwd())
" place temporary files under .../_ZIPVIM_/ " place temporary files under .../_ZIPVIM_/
if isdirectory("_ZIPVIM_") if isdirectory("_ZIPVIM_")
@ -297,7 +257,6 @@ fun! zip#Write(fname)
endif endif
call mkdir("_ZIPVIM_") call mkdir("_ZIPVIM_")
cd _ZIPVIM_ cd _ZIPVIM_
" call Decho("current directory now: ".getcwd())
if has("unix") if has("unix")
let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','') let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
@ -306,21 +265,17 @@ fun! zip#Write(fname)
let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','') let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','') let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
endif endif
" call Decho("zipfile<".zipfile.">")
" call Decho("fname <".fname.">")
if fname =~ '/' if fname =~ '/'
let dirpath = substitute(fname,'/[^/]\+$','','e') let dirpath = substitute(fname,'/[^/]\+$','','e')
if has("win32unix") && executable("cygpath") if has("win32unix") && executable("cygpath")
let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e') let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
endif endif
" call Decho("mkdir(dirpath<".dirpath.">,p)")
call mkdir(dirpath,"p") call mkdir(dirpath,"p")
endif endif
if zipfile !~ '/' if zipfile !~ '/'
let zipfile= curdir.'/'.zipfile let zipfile= curdir.'/'.zipfile
endif endif
" call Decho("zipfile<".zipfile."> fname<".fname.">")
exe "w! ".fnameescape(fname) exe "w! ".fnameescape(fname)
if has("win32unix") && executable("cygpath") if has("win32unix") && executable("cygpath")
@ -331,17 +286,13 @@ fun! zip#Write(fname)
let fname = substitute(fname, '[', '[[]', 'g') let fname = substitute(fname, '[', '[[]', 'g')
endif endif
" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0)) call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
if v:shell_error != 0 if v:shell_error != 0
redraw! call s:Mess('Error', "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname)
echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
elseif s:zipfile_{winnr()} =~ '^\a\+://' elseif s:zipfile_{winnr()} =~ '^\a\+://'
" support writing zipfiles across a network " support writing zipfiles across a network
let netzipfile= s:zipfile_{winnr()} let netzipfile= s:zipfile_{winnr()}
" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
1split|enew 1split|enew
let binkeep= &binary let binkeep= &binary
let eikeep = &ei let eikeep = &ei
@ -361,49 +312,35 @@ fun! zip#Write(fname)
call delete(tmpdir, "rf") call delete(tmpdir, "rf")
setlocal nomod setlocal nomod
let &report= repkeep
" call Dret("zip#Write")
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" zip#Extract: extract a file from a zip archive {{{2 " zip#Extract: extract a file from a zip archive {{{2
fun! zip#Extract() fun! zip#Extract()
" call Dfunc("zip#Extract()")
let repkeep= &report let dict = s:SetSaneOpts()
set report=10 defer s:RestoreOpts(dict)
let fname= getline(".") let fname= getline(".")
" call Decho("fname<".fname.">")
" sanity check " sanity check
if fname =~ '^"' if fname =~ '^"'
let &report= repkeep
" call Dret("zip#Extract")
return return
endif endif
if fname =~ '/$' if fname =~ '/$'
redraw! call s:Mess('Error', "***error*** (zip#Extract) Please specify a file, not a directory")
echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
let &report= repkeep
" call Dret("zip#Extract")
return return
endif endif
" extract the file mentioned under the cursor " extract the file mentioned under the cursor
call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}") call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}")
" call Decho("zipfile<".b:zipfile.">")
if v:shell_error != 0 if v:shell_error != 0
echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE call s:Mess('Error', "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!")
elseif !filereadable(fname) elseif !filereadable(fname)
echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!" call s:Mess('Error', "***error*** attempted to extract ".fname." but it doesn't appear to be present!")
else else
echo "***note*** successfully extracted ".fname echomsg "***note*** successfully extracted ".fname
endif endif
" restore option
let &report= repkeep
" call Dret("zip#Extract")
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
@ -418,38 +355,50 @@ fun! s:Escape(fname,isfilt)
else else
let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
endif endif
if exists("+shellslash") && &shellslash && &shell =~ "cmd.exe"
" renormalize directory separator on Windows
let qnameq=substitute(qnameq, '/', '\\', 'g')
endif
return qnameq return qnameq
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" ChgDir: {{{2 " s:ChgDir: {{{2
fun! s:ChgDir(newdir,errlvl,errmsg) fun! s:ChgDir(newdir,errlvl,errmsg)
" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
try try
exe "cd ".fnameescape(a:newdir) exe "cd ".fnameescape(a:newdir)
catch /^Vim\%((\a\+)\)\=:E344/ catch /^Vim\%((\a\+)\)\=:E344/
redraw! redraw!
if a:errlvl == s:NOTE if a:errlvl == s:NOTE
echo "***note*** ".a:errmsg echomsg "***note*** ".a:errmsg
elseif a:errlvl == s:WARNING elseif a:errlvl == s:WARNING
echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE call s:Mess("WarningMsg", "***warning*** ".a:errmsg)
elseif a:errlvl == s:ERROR elseif a:errlvl == s:ERROR
echohl Error | echo "***error*** ".a:errmsg | echohl NONE call s:Mess("Error", "***error*** ".a:errmsg)
endif endif
" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
" call Dret("ChgDir 1")
return 1 return 1
endtry endtry
" call Dret("ChgDir 0")
return 0 return 0
endfun endfun
" ---------------------------------------------------------------------
" s:SetSaneOpts: {{{2
fun! s:SetSaneOpts()
let dict = {}
let dict.report = &report
let dict.shellslash = &shellslash
let &report = 10
let &shellslash = 0
return dict
endfun
" ---------------------------------------------------------------------
" s:RestoreOpts: {{{2
fun! s:RestoreOpts(dict)
for [key, val] in items(a:dict)
exe $"let &{key} = {val}"
endfor
endfun
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
" Modelines And Restoration: {{{1 " Modelines And Restoration: {{{1
let &cpo= s:keepcpo let &cpo= s:keepcpo

View File

@ -10,8 +10,6 @@ endif
runtime plugin/zipPlugin.vim runtime plugin/zipPlugin.vim
func Test_zip_basic() func Test_zip_basic()
let _sl = &shellslash
set noshellslash
"## get our zip file "## get our zip file
if !filecopy("samples/test.zip", "X.zip") if !filecopy("samples/test.zip", "X.zip")
@ -134,6 +132,4 @@ func Test_zip_basic()
bw bw
let &shellslash = _sl
endfunc endfunc