vim-patch:19636be: runtime(zip): refactor save and restore of options

Problem:  zip plugin has no way to set/restore option values
Solution: Add the SetSaneOpts() and RestoreOpts() functions,
          so options that cause issues are set to sane values
          and restored back to their initial values later on.
          (this affects the 'shellslash' option on windows, which also
          changes how the shellescape() function works)

19636be55e

Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq 2024-08-07 07:07:29 +08:00
parent 11c57c25ef
commit 92981c8e0b

View File

@ -83,14 +83,13 @@ fun! zip#Browse(zipfile)
return return
endif endif
let repkeep= &report let dict = s:SetSaneOpts()
set report=10
" sanity checks " sanity checks
if !executable(g:zip_unzipcmd) if !executable(g:zip_unzipcmd)
redraw! redraw!
echohl Error | echomsg "***error*** (zip#Browse) unzip not available on your system" echohl Error | echomsg "***error*** (zip#Browse) unzip not available on your system"
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
if !filereadable(a:zipfile) if !filereadable(a:zipfile)
@ -99,7 +98,7 @@ fun! zip#Browse(zipfile)
redraw! redraw!
echohl Error | echomsg "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None echohl Error | echomsg "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
endif endif
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
if &ma != 1 if &ma != 1
@ -136,6 +135,7 @@ fun! zip#Browse(zipfile)
exe "keepj r ".fnameescape(a:zipfile) exe "keepj r ".fnameescape(a:zipfile)
let &ei= eikeep let &ei= eikeep
keepj 1d keepj 1d
call s:RestoreOpts(dict)
return return
endif endif
@ -147,28 +147,28 @@ 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 s:RestoreOpts(dict)
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" ZipBrowseSelect: {{{2 " ZipBrowseSelect: {{{2
fun! s:ZipBrowseSelect() fun! s:ZipBrowseSelect()
let repkeep= &report let dict = s:SetSaneOpts()
set report=10
let fname= getline(".") let fname= getline(".")
if !exists("b:zipfile") if !exists("b:zipfile")
call s:RestoreOpts(dict)
return return
endif endif
" sanity check " sanity check
if fname =~ '^"' if fname =~ '^"'
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
if fname =~ '/$' if fname =~ '/$'
redraw! redraw!
echohl Error | echomsg "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None echohl Error | echomsg "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
@ -184,14 +184,13 @@ fun! s:ZipBrowseSelect()
exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname) exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
filetype detect filetype detect
let &report= repkeep call s:RestoreOpts(dict)
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
if has("unix") if has("unix")
let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','') let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
@ -205,7 +204,7 @@ fun! zip#Read(fname,mode)
if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','','')) if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
redraw! redraw!
echohl Error | echomsg "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None echohl Error | echomsg "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
@ -225,26 +224,25 @@ fun! zip#Read(fname,mode)
" cleanup " cleanup
set nomod set nomod
let &report= repkeep call s:RestoreOpts(dict)
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" zip#Write: {{{2 " zip#Write: {{{2
fun! zip#Write(fname) fun! zip#Write(fname)
let repkeep= &report let dict = s:SetSaneOpts()
set report=10
" sanity checks " sanity checks
if !executable(substitute(g:zip_zipcmd,'\s\+.*$','','')) if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
redraw! redraw!
echohl Error | echomsg "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program" | echohl None echohl Error | echomsg "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program" | echohl None
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
if !exists("*mkdir") if !exists("*mkdir")
redraw! redraw!
echohl Error | echomsg "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None echohl Error | echomsg "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
@ -257,7 +255,7 @@ fun! zip#Write(fname)
" 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 s:RestoreOpts(dict)
return return
endif endif
@ -323,26 +321,25 @@ fun! zip#Write(fname)
call delete(tmpdir, "rf") call delete(tmpdir, "rf")
setlocal nomod setlocal nomod
let &report= repkeep call s:RestoreOpts(dict)
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()
let repkeep= &report let dict = s:SetSaneOpts()
set report=10
let fname= getline(".") let fname= getline(".")
" sanity check " sanity check
if fname =~ '^"' if fname =~ '^"'
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
if fname =~ '/$' if fname =~ '/$'
redraw! redraw!
echohl Error | echomsg "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None echohl Error | echomsg "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
let &report= repkeep call s:RestoreOpts(dict)
return return
endif endif
@ -357,7 +354,7 @@ fun! zip#Extract()
endif endif
" restore option " restore option
let &report= repkeep call s:RestoreOpts(dict)
endfun endfun
@ -373,15 +370,11 @@ 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)
try try
exe "cd ".fnameescape(a:newdir) exe "cd ".fnameescape(a:newdir)
@ -400,6 +393,27 @@ fun! s:ChgDir(newdir,errlvl,errmsg)
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