557
|
1 " tar.vim: Handles browsing tarfiles
|
|
2 " AUTOLOAD PORTION
|
|
3 " Date: Nov 18, 2005
|
|
4 " Version: 4
|
|
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
|
557
|
27 let g:loaded_tar= "v4"
|
|
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.">)")
|
446
|
49
|
557
|
50 " sanity checks
|
|
51 if !executable("tar")
|
|
52 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
|
|
53 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
|
54 " call Dret("tar#Browse")
|
|
55 return
|
|
56 endif
|
|
57 if !filereadable(a:tarfile)
|
|
58 if a:tarfile !~# '^\a\+://'
|
|
59 " if its an url, don't complain, let url-handlers such as vim do its thing
|
|
60 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
|
|
61 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
|
62 endif
|
|
63 " call Dret("tar#Browse : file<".a:tarfile."> not readable")
|
|
64 return
|
|
65 endif
|
|
66 if &ma != 1
|
|
67 set ma
|
|
68 endif
|
|
69 let w:tarfile= a:tarfile
|
446
|
70
|
557
|
71 setlocal noswapfile
|
|
72 setlocal buftype=nofile
|
|
73 setlocal bufhidden=hide
|
|
74 setlocal nobuflisted
|
|
75 setlocal nowrap
|
|
76 set ft=tar
|
446
|
77
|
557
|
78 " give header
|
|
79 exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'"
|
|
80 exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'"
|
|
81 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
|
|
82 0d
|
|
83 $
|
446
|
84
|
557
|
85 if a:tarfile =~# '\.\(gz\|tgz\)$'
|
|
86 exe "silent r! gzip -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
|
|
87 elseif a:tarfile =~# '\.bz2$'
|
|
88 exe "silent r! bzip2 -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
|
|
89 else
|
|
90 exe "silent r! tar -".g:tar_browseoptions." '".a:tarfile."'"
|
|
91 endif
|
|
92 %g@/$@d
|
446
|
93
|
557
|
94 setlocal noma nomod ro
|
|
95 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
|
|
96
|
|
97 " call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
|
527
|
98 endfun
|
|
99
|
|
100 " ---------------------------------------------------------------------
|
557
|
101 " TarBrowseSelect: {{{2
|
|
102 fun! s:TarBrowseSelect()
|
|
103 " call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
|
|
104 let fname= getline(".")
|
|
105 " call Decho("fname<".fname.">")
|
|
106
|
|
107 " sanity check
|
|
108 if fname =~ '^"'
|
|
109 " call Dret("TarBrowseSelect")
|
|
110 return
|
|
111 endif
|
|
112
|
|
113 " about to make a new window, need to use w:tarfile
|
|
114 let tarfile= w:tarfile
|
|
115 let curfile= expand("%")
|
|
116
|
|
117 new
|
|
118 wincmd _
|
|
119 let s:tblfile_{winnr()}= curfile
|
|
120 " call Decho("exe e tarfile:".tarfile.':'.fname)
|
|
121 exe "e tarfile:".tarfile.':'.fname
|
|
122 filetype detect
|
|
123
|
|
124 " call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
|
|
125 endfun
|
|
126
|
|
127 " ---------------------------------------------------------------------
|
|
128 " tar#Read: {{{2
|
|
129 fun! tar#Read(fname,mode)
|
|
130 " call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
|
|
131 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
|
|
132 let fname = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
|
|
133 " call Decho("tarfile<".tarfile."> fname<".fname.">")
|
|
134
|
|
135 if tarfile =~# '\.\(gz\|tgz\)$'
|
|
136 " call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -OPxf - '".fname."'")
|
|
137 exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
|
|
138 elseif a:fname =~# '\.bz2$'
|
|
139 " call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'")
|
|
140 exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
|
|
141 else
|
|
142 " call Decho("exe silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'")
|
|
143 exe "silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'"
|
|
144 endif
|
|
145 let w:tarfile= a:fname
|
|
146 exe "file tarfile:".fname
|
|
147
|
|
148 " cleanup
|
|
149 0d
|
|
150 set nomod
|
|
151
|
|
152 " call Dret("tar#Read : w:tarfile<".w:tarfile.">")
|
|
153 endfun
|
|
154
|
|
155 " ---------------------------------------------------------------------
|
|
156 " tar#Write: {{{2
|
|
157 fun! tar#Write(fname)
|
|
158 " call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
|
|
159
|
527
|
160 " sanity checks
|
|
161 if !executable("tar")
|
557
|
162 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
|
|
163 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
527
|
164 " call Dret("tar#Write")
|
|
165 return
|
|
166 endif
|
|
167 if !exists("*mkdir")
|
557
|
168 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
|
|
169 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
527
|
170 " call Dret("tar#Write")
|
|
171 return
|
|
172 endif
|
|
173
|
|
174 let curdir= getcwd()
|
|
175 let tmpdir= tempname()
|
|
176 " call Decho("orig tempname<".tmpdir.">")
|
|
177 if tmpdir =~ '\.'
|
|
178 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
|
|
179 endif
|
|
180 " call Decho("tmpdir<".tmpdir.">")
|
|
181 call mkdir(tmpdir,"p")
|
|
182
|
|
183 " attempt to change to the indicated directory
|
|
184 try
|
|
185 exe "cd ".escape(tmpdir,' \')
|
|
186 catch /^Vim\%((\a\+)\)\=:E344/
|
557
|
187 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
|
|
188 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
527
|
189 " call Dret("tar#Write")
|
|
190 return
|
|
191 endtry
|
|
192 " call Decho("current directory now: ".getcwd())
|
446
|
193
|
557
|
194 " place temporary files under .../_ZIPVIM_/
|
|
195 if isdirectory("_ZIPVIM_")
|
|
196 call s:Rmdir("_ZIPVIM_")
|
527
|
197 endif
|
557
|
198 call mkdir("_ZIPVIM_")
|
|
199 cd _ZIPVIM_
|
527
|
200 " call Decho("current directory now: ".getcwd())
|
|
201
|
557
|
202 let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
|
|
203 let fname = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
|
|
204
|
|
205 " handle compressed archives
|
|
206 if tarfile =~# '\.gz'
|
|
207 call system("gzip -d ".tarfile)
|
|
208 let tarfile = substitute(tarfile,'\.gz','','e')
|
|
209 let compress= "gzip '".tarfile."'"
|
|
210 elseif tarfile =~# '\.tgz'
|
|
211 call system("gzip -d ".tarfile)
|
|
212 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
|
|
213 let compress= "gzip '".tarfile."'"
|
|
214 let tgz = 1
|
|
215 elseif tarfile =~# '\.bz2'
|
|
216 call system("bzip2 -d ".tarfile)
|
|
217 let tarfile = substitute(tarfile,'\.bz2','','e')
|
|
218 let compress= "bzip2 '".tarfile."'"
|
527
|
219 endif
|
|
220
|
|
221 if v:shell_error != 0
|
557
|
222 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
|
|
223 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
527
|
224 else
|
557
|
225
|
|
226 " call Decho("tarfile<".tarfile."> fname<".fname.">")
|
|
227
|
|
228 let dirpath = substitute(fname,'/[^/]\+$','','e')
|
|
229 if tarfile !~ '/'
|
|
230 let tarfile= curdir.'/'.tarfile
|
|
231 endif
|
|
232 " call Decho("tarfile<".tarfile."> fname<".fname.">")
|
|
233
|
|
234 call mkdir(dirpath,"p")
|
|
235 exe "w! ".fname
|
|
236 if executable("cygpath")
|
|
237 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
|
|
238 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
|
|
239 endif
|
|
240
|
|
241 " delete old file from tarfile
|
|
242 " call Decho("tar --delete -f '".tarfile."' '".fname."'")
|
|
243 call system("tar --delete -f '".tarfile."' '".fname."'")
|
|
244 if v:shell_error != 0
|
|
245 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
|
|
246 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
|
247 else
|
|
248
|
|
249 " update tarfile with new file
|
|
250 " call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
|
|
251 call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
|
|
252 if v:shell_error != 0
|
|
253 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
|
|
254 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
|
255 elseif exists("compress")
|
|
256 " call Decho("call system(".compress.")")
|
|
257 call system(compress)
|
|
258 if exists("tgz")
|
|
259 " call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
|
|
260 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
|
|
261 endif
|
|
262 endif
|
|
263 endif
|
|
264
|
|
265 " support writing tarfiles across a network
|
|
266 if s:tblfile_{winnr()} =~ '^\a\+://'
|
|
267 " call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
|
|
268 let tblfile= s:tblfile_{winnr()}
|
|
269 1split|enew
|
|
270 let binkeep= &binary
|
|
271 let eikeep = &ei
|
|
272 set binary ei=all
|
|
273 exe "e! ".tarfile
|
|
274 call netrw#NetWrite(tblfile)
|
|
275 let &ei = eikeep
|
|
276 let &binary = binkeep
|
|
277 q!
|
|
278 unlet s:tblfile_{winnr()}
|
|
279 endif
|
527
|
280 endif
|
|
281
|
|
282 " cleanup and restore current directory
|
|
283 cd ..
|
557
|
284 call s:Rmdir("_ZIPVIM_")
|
527
|
285 exe "cd ".escape(curdir,' \')
|
|
286 setlocal nomod
|
|
287
|
|
288 " call Dret("tar#Write")
|
|
289 endfun
|
|
290
|
|
291 " ---------------------------------------------------------------------
|
557
|
292 " Rmdir: {{{2
|
527
|
293 fun! s:Rmdir(fname)
|
|
294 " call Dfunc("Rmdir(fname<".a:fname.">)")
|
|
295 if has("unix")
|
|
296 call system("/bin/rm -rf ".a:fname)
|
|
297 elseif has("win32") || has("win95") || has("win64") || has("win16")
|
|
298 if &shell =~? "sh$"
|
|
299 call system("/bin/rm -rf ".a:fname)
|
|
300 else
|
|
301 call system("del /S ".a:fname)
|
|
302 endif
|
|
303 endif
|
|
304 " call Dret("Rmdir")
|
|
305 endfun
|
|
306
|
557
|
307 " ------------------------------------------------------------------------
|
|
308 " Modelines And Restoration: {{{1
|
|
309 let &cpo= s:keepcpo
|
|
310 unlet s:keepcpo
|
|
311 " vim:ts=8 fdm=marker
|