xref: /vim-8.2.3635/runtime/autoload/tar.vim (revision 899dddf8)
1" tar.vim: Handles browsing tarfiles
2"            AUTOLOAD PORTION
3" Date:			Dec 24, 2005
4" Version:		7
5" Maintainer:	Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz>
6" License:		Vim License  (see vim's :help license)
7"
8"	Contains many ideas from Michael Toren's <tar.vim>
9"
10" Copyright:    Copyright (C) 2005 Charles E. Campbell, Jr. {{{1
11"               Permission is hereby granted to use and distribute this code,
12"               with or without modifications, provided that this copyright
13"               notice is copied with it. Like anything else that's free,
14"               tarPlugin.vim is provided *as is* and comes with no warranty
15"               of any kind, either expressed or implied. By using this
16"               plugin, you agree that in no event will the copyright
17"               holder be liable for any damages resulting from the use
18"               of this software.
19
20" ---------------------------------------------------------------------
21" Initialization: {{{1
22let s:keepcpo= &cpo
23set cpo&vim
24if exists("g:loaded_tar")
25 finish
26endif
27let g:loaded_tar= "v7"
28
29" ---------------------------------------------------------------------
30"  Default Settings: {{{1
31if !exists("g:tar_browseoptions")
32 let g:tar_browseoptions= "Ptf"
33endif
34if !exists("g:tar_readoptions")
35 let g:tar_readoptions= "OPxf"
36endif
37if !exists("g:tar_writeoptions")
38 let g:tar_writeoptions= "uf"
39endif
40
41" ----------------
42"  Functions: {{{1
43" ----------------
44
45" ---------------------------------------------------------------------
46" tar#Browse: {{{2
47fun! tar#Browse(tarfile)
48"  call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
49  let repkeep= &report
50  set report=10
51
52  " sanity checks
53  if !executable("tar")
54   echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
55   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
56   let &report= repkeep
57"   call Dret("tar#Browse")
58   return
59  endif
60  if !filereadable(a:tarfile)
61   if a:tarfile !~# '^\a\+://'
62    " if its an url, don't complain, let url-handlers such as vim do its thing
63    echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
64    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
65   endif
66   let &report= repkeep
67"   call Dret("tar#Browse : file<".a:tarfile."> not readable")
68   return
69  endif
70  if &ma != 1
71   set ma
72  endif
73  let w:tarfile= a:tarfile
74
75  setlocal noswapfile
76  setlocal buftype=nofile
77  setlocal bufhidden=hide
78  setlocal nobuflisted
79  setlocal nowrap
80  set ft=tar
81
82  " give header
83  exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'"
84  exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'"
85  exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
86  0d
87  $
88
89  if a:tarfile =~# '\.\(gz\|tgz\)$'
90   exe "silent r! gzip -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
91  elseif a:tarfile =~# '\.bz2$'
92   exe "silent r! bzip2 -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
93  else
94   exe "silent r! tar -".g:tar_browseoptions." '".a:tarfile."'"
95  endif
96  silent %g@/$@d
97
98  setlocal noma nomod ro
99  noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
100
101  let &report= repkeep
102"  call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
103endfun
104
105" ---------------------------------------------------------------------
106" TarBrowseSelect: {{{2
107fun! s:TarBrowseSelect()
108"  call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
109  let repkeep= &report
110  set report=10
111  let fname= getline(".")
112"  call Decho("fname<".fname.">")
113
114  " sanity check
115  if fname =~ '^"'
116   let &report= repkeep
117"   call Dret("TarBrowseSelect")
118   return
119  endif
120
121  " about to make a new window, need to use w:tarfile
122  let tarfile= w:tarfile
123  let curfile= expand("%")
124
125  new
126  wincmd _
127  let s:tblfile_{winnr()}= curfile
128"  call Decho("exe e tarfile:".tarfile.':'.fname)
129  exe "e tarfile:".tarfile.':'.fname
130  filetype detect
131
132  let &report= repkeep
133"  call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
134endfun
135
136" ---------------------------------------------------------------------
137" tar#Read: {{{2
138fun! tar#Read(fname,mode)
139"  call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
140  let repkeep= &report
141  set report=10
142  let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
143  let fname   = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
144"  call Decho("tarfile<".tarfile."> fname<".fname.">")
145
146  if tarfile =~# '\.\(gz\|tgz\)$'
147"   call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -OPxf - '".fname."'")
148   exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
149  elseif tarfile =~# '\.bz2$'
150"   call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'")
151   exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
152  else
153"   call Decho("exe silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'")
154   exe "silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'"
155  endif
156  let w:tarfile= a:fname
157  exe "file tarfile:".fname
158
159  " cleanup
160  0d
161  set nomod
162
163  let &report= repkeep
164"  call Dret("tar#Read : w:tarfile<".w:tarfile.">")
165endfun
166
167" ---------------------------------------------------------------------
168" tar#Write: {{{2
169fun! tar#Write(fname)
170"  call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
171  let repkeep= &report
172  set report=10
173
174  " sanity checks
175  if !executable("tar")
176   echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
177   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
178   let &report= repkeep
179"   call Dret("tar#Write")
180   return
181  endif
182  if !exists("*mkdir")
183   echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
184   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
185   let &report= repkeep
186"   call Dret("tar#Write")
187   return
188  endif
189
190  let curdir= getcwd()
191  let tmpdir= tempname()
192"  call Decho("orig tempname<".tmpdir.">")
193  if tmpdir =~ '\.'
194   let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
195  endif
196"  call Decho("tmpdir<".tmpdir.">")
197  call mkdir(tmpdir,"p")
198
199  " attempt to change to the indicated directory
200  try
201   exe "cd ".escape(tmpdir,' \')
202  catch /^Vim\%((\a\+)\)\=:E344/
203   echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
204   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
205   let &report= repkeep
206"   call Dret("tar#Write")
207   return
208  endtry
209"  call Decho("current directory now: ".getcwd())
210
211  " place temporary files under .../_ZIPVIM_/
212  if isdirectory("_ZIPVIM_")
213   call s:Rmdir("_ZIPVIM_")
214  endif
215  call mkdir("_ZIPVIM_")
216  cd _ZIPVIM_
217"  call Decho("current directory now: ".getcwd())
218
219  let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
220  let fname   = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
221
222  " handle compressed archives
223  if tarfile =~# '\.gz'
224   call system("gzip -d ".tarfile)
225   let tarfile = substitute(tarfile,'\.gz','','e')
226   let compress= "gzip '".tarfile."'"
227  elseif tarfile =~# '\.tgz'
228   call system("gzip -d ".tarfile)
229   let tarfile = substitute(tarfile,'\.tgz','.tar','e')
230   let compress= "gzip '".tarfile."'"
231   let tgz     = 1
232  elseif tarfile =~# '\.bz2'
233   call system("bzip2 -d ".tarfile)
234   let tarfile = substitute(tarfile,'\.bz2','','e')
235   let compress= "bzip2 '".tarfile."'"
236  endif
237
238  if v:shell_error != 0
239   echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
240   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
241  else
242
243"   call Decho("tarfile<".tarfile."> fname<".fname.">")
244
245   if fname =~ '/'
246    let dirpath = substitute(fname,'/[^/]\+$','','e')
247    if executable("cygpath")
248     let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
249    endif
250    call mkdir(dirpath,"p")
251   endif
252   if tarfile !~ '/'
253    let tarfile= curdir.'/'.tarfile
254   endif
255"   call Decho("tarfile<".tarfile."> fname<".fname.">")
256
257   exe "w! ".fname
258   if executable("cygpath")
259    let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
260   endif
261
262   " delete old file from tarfile
263"   call Decho("tar --delete -f '".tarfile."' '".fname."'")
264   call system("tar --delete -f '".tarfile."' '".fname."'")
265   if v:shell_error != 0
266    echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
267    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
268   else
269
270    " update tarfile with new file
271"    call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
272    call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
273    if v:shell_error != 0
274     echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
275     call inputsave()|call input("Press <cr> to continue")|call inputrestore()
276    elseif exists("compress")
277"     call Decho("call system(".compress.")")
278     call system(compress)
279     if exists("tgz")
280"      call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
281      call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
282     endif
283    endif
284   endif
285
286   " support writing tarfiles across a network
287   if s:tblfile_{winnr()} =~ '^\a\+://'
288"    call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
289    let tblfile= s:tblfile_{winnr()}
290    1split|enew
291    let binkeep= &binary
292    let eikeep = &ei
293    set binary ei=all
294    exe "e! ".tarfile
295    call netrw#NetWrite(tblfile)
296    let &ei     = eikeep
297    let &binary = binkeep
298    q!
299    unlet s:tblfile_{winnr()}
300   endif
301  endif
302
303  " cleanup and restore current directory
304  cd ..
305  call s:Rmdir("_ZIPVIM_")
306  exe "cd ".escape(curdir,' \')
307  setlocal nomod
308
309  let &report= repkeep
310"  call Dret("tar#Write")
311endfun
312
313" ---------------------------------------------------------------------
314" Rmdir: {{{2
315fun! s:Rmdir(fname)
316"  call Dfunc("Rmdir(fname<".a:fname.">)")
317  if has("unix")
318   call system("/bin/rm -rf ".a:fname)
319  elseif has("win32") || has("win95") || has("win64") || has("win16")
320   if &shell =~? "sh$"
321    call system("/bin/rm -rf ".a:fname)
322   else
323    call system("del /S ".a:fname)
324   endif
325  endif
326"  call Dret("Rmdir")
327endfun
328
329" ------------------------------------------------------------------------
330" Modelines And Restoration: {{{1
331let &cpo= s:keepcpo
332unlet s:keepcpo
333" vim:ts=8 fdm=marker
334