vim-patch:afea6b9: runtime(zip): use defer to restore old settings

Problem:  RestoreOpts() plugin called too often
Solution: use :defer to have the RestoreOpts() function
          called when the function returns automatically

afea6b9468

Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq 2024-08-07 07:15:59 +08:00
parent 221c489edd
commit 450d49660f

View File

@ -92,11 +92,11 @@ fun! zip#Browse(zipfile)
endif endif
let dict = s:SetSaneOpts() let dict = s:SetSaneOpts()
defer s:RestoreOpts(dict)
" sanity checks " sanity checks
if !executable(g:zip_unzipcmd) if !executable(g:zip_unzipcmd)
call s:Mess('Error', "***error*** (zip#Browse) unzip not available on your system") call s:Mess('Error', "***error*** (zip#Browse) unzip not available on your system")
call s:RestoreOpts(dict)
return return
endif endif
if !filereadable(a:zipfile) if !filereadable(a:zipfile)
@ -104,7 +104,6 @@ fun! zip#Browse(zipfile)
" 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
call s:Mess('Error', "***error*** (zip#Browse) File not readable <".a:zipfile.">") call s:Mess('Error', "***error*** (zip#Browse) File not readable <".a:zipfile.">")
endif endif
call s:RestoreOpts(dict)
return return
endif endif
if &ma != 1 if &ma != 1
@ -140,7 +139,6 @@ 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
@ -152,27 +150,24 @@ fun! zip#Browse(zipfile)
noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr> noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
endif endif
call s:RestoreOpts(dict)
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" ZipBrowseSelect: {{{2 " ZipBrowseSelect: {{{2
fun! s:ZipBrowseSelect() fun! s:ZipBrowseSelect()
let dict = s:SetSaneOpts() let dict = s:SetSaneOpts()
defer s:RestoreOpts(dict)
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 =~ '^"'
call s:RestoreOpts(dict)
return return
endif endif
if fname =~ '/$' if fname =~ '/$'
call s:Mess('Error', "***error*** (zip#Browse) Please specify a file, not a directory") call s:Mess('Error', "***error*** (zip#Browse) Please specify a file, not a directory")
call s:RestoreOpts(dict)
return return
endif endif
@ -188,13 +183,13 @@ fun! s:ZipBrowseSelect()
exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname) exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
filetype detect filetype detect
call s:RestoreOpts(dict)
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" zip#Read: {{{2 " zip#Read: {{{2
fun! zip#Read(fname,mode) fun! zip#Read(fname,mode)
let dict = s:SetSaneOpts() let dict = s:SetSaneOpts()
defer s:RestoreOpts(dict)
if has("unix") if has("unix")
let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','') let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
@ -207,7 +202,6 @@ fun! zip#Read(fname,mode)
" sanity check " sanity check
if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','','')) if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
call s:Mess('Error', "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program") call s:Mess('Error', "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program")
call s:RestoreOpts(dict)
return return
endif endif
@ -227,23 +221,21 @@ fun! zip#Read(fname,mode)
" cleanup " cleanup
set nomod set nomod
call s:RestoreOpts(dict)
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" zip#Write: {{{2 " zip#Write: {{{2
fun! zip#Write(fname) fun! zip#Write(fname)
let dict = s:SetSaneOpts() let dict = s:SetSaneOpts()
defer s:RestoreOpts(dict)
" sanity checks " sanity checks
if !executable(substitute(g:zip_zipcmd,'\s\+.*$','','')) if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
call s:Mess('Error', "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program") call s:Mess('Error', "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program")
call s:RestoreOpts(dict)
return return
endif endif
if !exists("*mkdir") if !exists("*mkdir")
call s:Mess('Error', "***error*** (zip#Write) sorry, mkdir() doesn't work on your system") call s:Mess('Error', "***error*** (zip#Write) sorry, mkdir() doesn't work on your system")
call s:RestoreOpts(dict)
return return
endif endif
@ -256,7 +248,6 @@ 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")
call s:RestoreOpts(dict)
return return
endif endif
@ -321,7 +312,6 @@ fun! zip#Write(fname)
call delete(tmpdir, "rf") call delete(tmpdir, "rf")
setlocal nomod setlocal nomod
call s:RestoreOpts(dict)
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
@ -329,16 +319,15 @@ endfun
fun! zip#Extract() fun! zip#Extract()
let dict = s:SetSaneOpts() let dict = s:SetSaneOpts()
defer s:RestoreOpts(dict)
let fname= getline(".") let fname= getline(".")
" sanity check " sanity check
if fname =~ '^"' if fname =~ '^"'
call s:RestoreOpts(dict)
return return
endif endif
if fname =~ '/$' if fname =~ '/$'
call s:Mess('Error', "***error*** (zip#Extract) Please specify a file, not a directory") call s:Mess('Error', "***error*** (zip#Extract) Please specify a file, not a directory")
call s:RestoreOpts(dict)
return return
endif endif
@ -352,9 +341,6 @@ fun! zip#Extract()
echomsg "***note*** successfully extracted ".fname echomsg "***note*** successfully extracted ".fname
endif endif
" restore option
call s:RestoreOpts(dict)
endfun endfun
" --------------------------------------------------------------------- " ---------------------------------------------------------------------