557
|
1 " tar.vim: Handles browsing tarfiles
|
|
2 " AUTOLOAD PORTION
|
624
|
3 " Date: Dec 24, 2005
|
|
4 " Version: 7
|
557
|
5 " Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz>
|
|
6 " License: Vim License (see vim's :help license)
|
446
|
7 "
|
557
|
8 " Contains many ideas from Michael Toren's <tar.vim>
|
446
|
9 "
|
557
|
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
|
|
22 let s:keepcpo= &cpo
|
|
23 set cpo&vim
|
|
24 if exists("g:loaded_tar")
|
527
|
25 finish
|
|
26 endif
|
624
|
27 let g:loaded_tar= "v7"
|
557
|
28
|
|
29 " ---------------------------------------------------------------------
|
|
30 " Default Settings: {{{1
|
|
31 if !exists("g:tar_browseoptions")
|
|
32 let g:tar_browseoptions= "Ptf"
|
|
33 endif
|
|
34 if !exists("g:tar_readoptions")
|
|
35 let g:tar_readoptions= "OPxf"
|
|
36 endif
|
|
37 if !exists("g:tar_writeoptions")
|
|
38 let g:tar_writeoptions= "uf"
|
|
39 endif
|
|
40
|
|
41 " ----------------
|
|
42 " Functions: {{{1
|
|
43 " ----------------
|
446
|
44
|
527
|
45 " ---------------------------------------------------------------------
|
557
|
46 " tar#Browse: {{{2
|
|
47 fun! tar#Browse(tarfile)
|
|
48 " call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
|
569
|
49 let repkeep= &report
|
|
50 set report=10
|
446
|
51
|
557
|
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()
|
569
|
56 let &report= repkeep
|
557
|
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
|
569
|
66 let &report= repkeep
|
557
|
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
|
446
|
74
|
557
|
75 setlocal noswapfile
|
|
76 setlocal buftype=nofile
|
|
77 setlocal bufhidden=hide
|
|
78 setlocal nobuflisted
|
|
79 setlocal nowrap
|
|
80 set ft=tar
|
446
|
81
|
557
|
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 $
|
446
|
88
|
557
|
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
|
624
|
96 silent %g@/$@d
|
446
|
97
|
557
|
98 setlocal noma nomod ro
|
|
99 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
|
|
100
|
569
|
101 let &report= repkeep
|
557
|
102 " call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
|
527
|
103 endfun
|
|
104
|
|
105 " ---------------------------------------------------------------------
|
557
|
106 " TarBrowseSelect: {{{2
|
|
107 fun! s:TarBrowseSelect()
|
|
108 " call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
|
569
|
109 let repkeep= &report
|
|
110 set report=10
|
557
|
111 let fname= getline(".")
|
|
112 " call Decho("fname<".fname.">")
|
|
113
|
|
114 " sanity check
|
|
115 if fname =~ '^"'
|
569
|
116 let &report= repkeep
|
557
|
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
|
569
|
132 let &report= repkeep
|
557
|
133 " call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
|
|
134 endfun
|
|
135
|
|
136 " ---------------------------------------------------------------------
|
|
137 " tar#Read: {{{2
|
|
138 fun! tar#Read(fname,mode)
|
|
139 " call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
|
569
|
140 let repkeep= &report
|
|
141 set report=10
|
557
|
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."'"
|
624
|
149 elseif tarfile =~# '\.bz2$'
|
557
|
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
|
569
|
163 let &report= repkeep
|
557
|
164 " call Dret("tar#Read : w:tarfile<".w:tarfile.">")
|
|
165 endfun
|
|
166
|
|
167 " ---------------------------------------------------------------------
|
|
168 " tar#Write: {{{2
|
|
169 fun! tar#Write(fname)
|
|
170 " call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
|
569
|
171 let repkeep= &report
|
|
172 set report=10
|
557
|
173
|
527
|
174 " sanity checks
|
|
175 if !executable("tar")
|
557
|
176 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
|
|
177 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
569
|
178 let &report= repkeep
|
527
|
179 " call Dret("tar#Write")
|
|
180 return
|
|
181 endif
|
|
182 if !exists("*mkdir")
|
557
|
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()
|
569
|
185 let &report= repkeep
|
527
|
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/
|
557
|
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()
|
569
|
205 let &report= repkeep
|
527
|
206 " call Dret("tar#Write")
|
|
207 return
|
|
208 endtry
|
|
209 " call Decho("current directory now: ".getcwd())
|
446
|
210
|
557
|
211 " place temporary files under .../_ZIPVIM_/
|
|
212 if isdirectory("_ZIPVIM_")
|
|
213 call s:Rmdir("_ZIPVIM_")
|
527
|
214 endif
|
557
|
215 call mkdir("_ZIPVIM_")
|
|
216 cd _ZIPVIM_
|
527
|
217 " call Decho("current directory now: ".getcwd())
|
|
218
|
557
|
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."'"
|
527
|
236 endif
|
|
237
|
|
238 if v:shell_error != 0
|
557
|
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()
|
527
|
241 else
|
557
|
242
|
|
243 " call Decho("tarfile<".tarfile."> fname<".fname.">")
|
|
244
|
624
|
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
|
557
|
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
|
527
|
301 endif
|
|
302
|
|
303 " cleanup and restore current directory
|
|
304 cd ..
|
557
|
305 call s:Rmdir("_ZIPVIM_")
|
527
|
306 exe "cd ".escape(curdir,' \')
|
|
307 setlocal nomod
|
|
308
|
569
|
309 let &report= repkeep
|
527
|
310 " call Dret("tar#Write")
|
|
311 endfun
|
|
312
|
|
313 " ---------------------------------------------------------------------
|
557
|
314 " Rmdir: {{{2
|
527
|
315 fun! 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")
|
|
327 endfun
|
|
328
|
557
|
329 " ------------------------------------------------------------------------
|
|
330 " Modelines And Restoration: {{{1
|
|
331 let &cpo= s:keepcpo
|
|
332 unlet s:keepcpo
|
|
333 " vim:ts=8 fdm=marker
|