mirror of
https://github.com/vim/vim.git
synced 2025-09-23 03:43:49 -04:00
runtime(zip): add a generic Message function
Problem: the zip plugin duplicates a lot of code for displaying warnings/errors Solution: refactor common code into a generic Mess() function Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
@@ -29,12 +29,6 @@ if &cp || exists("g:loaded_zip")
|
|||||||
finish
|
finish
|
||||||
endif
|
endif
|
||||||
let g:loaded_zip= "v34"
|
let g:loaded_zip= "v34"
|
||||||
if v:version < 900
|
|
||||||
echohl WarningMsg
|
|
||||||
echomsg "***warning*** this version of zip needs vim 9.0 or later"
|
|
||||||
echohl Normal
|
|
||||||
finish
|
|
||||||
endif
|
|
||||||
let s:keepcpo= &cpo
|
let s:keepcpo= &cpo
|
||||||
set cpo&vim
|
set cpo&vim
|
||||||
|
|
||||||
@@ -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 < 900
|
||||||
|
call s:Mess('WarningMsg', "***warning*** this version of zip needs vim 9.0 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
|
||||||
|
|
||||||
@@ -87,16 +95,14 @@ fun! zip#Browse(zipfile)
|
|||||||
|
|
||||||
" sanity checks
|
" sanity checks
|
||||||
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 | echomsg "***error*** (zip#Browse) unzip not available on your system"
|
|
||||||
call s:RestoreOpts(dict)
|
call s:RestoreOpts(dict)
|
||||||
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 | echomsg "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
|
|
||||||
endif
|
endif
|
||||||
call s:RestoreOpts(dict)
|
call s:RestoreOpts(dict)
|
||||||
return
|
return
|
||||||
@@ -127,8 +133,7 @@ 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 | echomsg "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
|
|
||||||
keepj sil! %d
|
keepj sil! %d
|
||||||
let eikeep= &ei
|
let eikeep= &ei
|
||||||
set ei=BufReadCmd,FileReadCmd
|
set ei=BufReadCmd,FileReadCmd
|
||||||
@@ -166,8 +171,7 @@ fun! s: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 | echomsg "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
|
|
||||||
call s:RestoreOpts(dict)
|
call s:RestoreOpts(dict)
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
@@ -202,8 +206,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 | echomsg "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
|
|
||||||
call s:RestoreOpts(dict)
|
call s:RestoreOpts(dict)
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
@@ -234,14 +237,12 @@ fun! zip#Write(fname)
|
|||||||
|
|
||||||
" 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 | echomsg "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program" | echohl None
|
|
||||||
call s:RestoreOpts(dict)
|
call s:RestoreOpts(dict)
|
||||||
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 | echomsg "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
|
|
||||||
call s:RestoreOpts(dict)
|
call s:RestoreOpts(dict)
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
@@ -296,8 +297,7 @@ fun! zip#Write(fname)
|
|||||||
|
|
||||||
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 | echomsg "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
|
|
||||||
|
|
||||||
elseif s:zipfile_{winnr()} =~ '^\a\+://'
|
elseif s:zipfile_{winnr()} =~ '^\a\+://'
|
||||||
" support writing zipfiles across a network
|
" support writing zipfiles across a network
|
||||||
@@ -337,8 +337,7 @@ fun! 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 | echomsg "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
|
|
||||||
call s:RestoreOpts(dict)
|
call s:RestoreOpts(dict)
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
@@ -346,9 +345,9 @@ fun! zip#Extract()
|
|||||||
" 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)}")
|
||||||
if v:shell_error != 0
|
if v:shell_error != 0
|
||||||
echohl Error | echomsg "***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 | echomsg "***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
|
||||||
echomsg "***note*** successfully extracted ".fname
|
echomsg "***note*** successfully extracted ".fname
|
||||||
endif
|
endif
|
||||||
@@ -383,9 +382,9 @@ fun! s:ChgDir(newdir,errlvl,errmsg)
|
|||||||
if a:errlvl == s:NOTE
|
if a:errlvl == s:NOTE
|
||||||
echomsg "***note*** ".a:errmsg
|
echomsg "***note*** ".a:errmsg
|
||||||
elseif a:errlvl == s:WARNING
|
elseif a:errlvl == s:WARNING
|
||||||
echohl WarningMsg | echomsg "***warning*** ".a:errmsg | echohl NONE
|
call s:Mess("WarningMsg", "***warning*** ".a:errmsg)
|
||||||
elseif a:errlvl == s:ERROR
|
elseif a:errlvl == s:ERROR
|
||||||
echohl Error | echomsg "***error*** ".a:errmsg | echohl NONE
|
call s:Mess("Error", "***error*** ".a:errmsg)
|
||||||
endif
|
endif
|
||||||
return 1
|
return 1
|
||||||
endtry
|
endtry
|
||||||
|
Reference in New Issue
Block a user