blob: 0234059b0f14b5714a2be48fdb74f53531697d07 [file] [log] [blame]
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001"
2" tar.vim -- a Vim plugin for browsing tarfiles
3" Copyright (c) 2002, Michael C. Toren <mct@toren.net>
4" Distributed under the GNU General Public License.
5"
Bram Moolenaarab194812005-09-14 21:40:12 +00006" Version: 2
7" Date: Sep 14, 2005
8" Modified By: Charles E. Campbell, Jr.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009"
10" Updates are available from <http://michael.toren.net/code/>. If you
11" find this script useful, or have suggestions for improvements, please
12" let me know.
13" Also look there for further comments and documentation.
14"
15" This part defines the functions. The autocommands are in plugin/tar.vim.
Bram Moolenaarab194812005-09-14 21:40:12 +000016if exists("g:loaded_tar") || &cp
17 finish
18endif
19let g:loaded_tar= "v2"
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020
Bram Moolenaarab194812005-09-14 21:40:12 +000021" ---------------------------------------------------------------------
22" tar#Read: {{{1
23fun! tar#Read(argument, cleanup)
24" call Dfunc("tar#Read(argument<".a:argument."> cleanup=".a:cleanup.")")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025 let l:argument = a:argument
26 let l:argument = substitute(l:argument, '^tarfile:', '', '')
27 let l:argument = substitute(l:argument, '^\~', $HOME, '')
28
29 let l:tarfile = l:argument
30 while 1
31 if (l:tarfile == "" || l:tarfile == "/")
Bram Moolenaarab194812005-09-14 21:40:12 +000032 echo "***error*** (tar#Read) Could not find a readable tarfile in path:" l:argument
33" call Dret("tar#Read")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000034 return
35 endif
36
37 if filereadable(l:tarfile) " found it!
38 break
39 endif
40
41 let l:tarfile = fnamemodify(l:tarfile, ":h")
42 endwhile
43
44 let l:toextract = strpart(l:argument, strlen(l:tarfile) + 1)
45
46 if (l:toextract == "")
Bram Moolenaarab194812005-09-14 21:40:12 +000047" call Dret("tar#Read")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000048 return
49 endif
50
51 let l:cat = s:TarCatCommand(l:tarfile)
52 execute "r !" . l:cat . " < '" . l:tarfile . "'"
53 \ " | tar OPxf - '" . l:toextract . "'"
54
55 if (a:cleanup)
56 0d "blank line
57 execute "doautocmd BufReadPost " . expand("%")
Bram Moolenaarab194812005-09-14 21:40:12 +000058 setlocal nomod
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000059 silent preserve
60 endif
Bram Moolenaarab194812005-09-14 21:40:12 +000061" call Dret("tar#Read")
62endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000063
Bram Moolenaarab194812005-09-14 21:40:12 +000064" ---------------------------------------------------------------------
65" tar#Write: {{{1
66fun! tar#Write(argument)
67" call Dfunc("tar#Write(argument<".a:argument.">)")
68"
69 " sanity checks
70 if !executable("tar")
71 echo "***error*** (TarWrite) sorry, your system doesn't appear to have the tar pgm"
72" call Dret("tar#Write")
73 return
74 endif
75 if !exists("*mkdir")
76 echo "***error*** (TarWrite) sorry, mkdir() doesn't work on your system"
77" call Dret("tar#Write")
78 return
79 endif
80
81 let curdir= getcwd()
82 let tmpdir= tempname()
83" call Decho("orig tempname<".tmpdir.">")
84 if tmpdir =~ '\.'
85 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
86 endif
87" call Decho("tmpdir<".tmpdir.">")
88 call mkdir(tmpdir,"p")
89
90 " attempt to change to the indicated directory
91 try
92 exe "cd ".escape(tmpdir,' \')
93 catch /^Vim\%((\a\+)\)\=:E344/
94 echo "***error*** (TarWrite) cannot cd to temporary directory"
95" call Dret("tar#Write")
96 return
97 endtry
98" call Decho("current directory now: ".getcwd())
99
100 " place temporary files under .../_TARVIM_/
101 if isdirectory("_TARVIM_")
102 call s:Rmdir("_TARVIM_")
103 endif
104 call mkdir("_TARVIM_")
105 cd _TARVIM_
106" call Decho("current directory now: ".getcwd())
107
108 let tarfile = curdir."/".substitute(a:argument,'tarfile:\([^/]\{-}\)/.*$','\1','')
109 let path = substitute(a:argument,'^.\{-}/','','')
110 let dirpath = substitute(path,'/\=[^/]\+$','','')
111" call Decho("path <".path.">")
112" call Decho("dirpath<".dirpath.">")
113 call mkdir(dirpath,"p")
114 exe "w! ".path
115 if executable("cygpath")
116 let path = substitute(system("cygpath ".path),'\n','','e')
117 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
118 endif
119
120" call Decho("tar --delete -f ".tarfile." ".path)
121 call system("tar --delete -f ".tarfile." ".path)
122 if v:shell_error != 0
123 echo "***error*** (TarWrite) sorry, your tar pgm doesn't support deletion of ".path
124 else
125" call Decho("tar -rf ".tarfile." ".path)
126 call system("tar -rf ".tarfile." ".path)
127 endif
128
129 " cleanup and restore current directory
130 cd ..
131 call s:Rmdir("_TARVIM_")
132 exe "cd ".escape(curdir,' \')
133 setlocal nomod
134
135" call Dret("tar#Write")
136endfun
137
138" ---------------------------------------------------------------------
139" tar#Browse: {{{1
140fun! tar#Browse(tarfile)
141" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000142 setlocal noswapfile
143 setlocal buftype=nofile
144 setlocal bufhidden=hide
145 setlocal filetype=
146 setlocal nobuflisted
147 setlocal buftype=nofile
148 setlocal wrap
149 setlocal syntax=tar
150
151 let l:tarfile = a:tarfile
152 let b:tarfile = l:tarfile
153 let l:cat = s:TarCatCommand(l:tarfile)
154
155 if ! filereadable(l:tarfile)
156 let l:tarfile = substitute(l:tarfile, '^tarfile:', '', '')
157 endif
158
159 if ! filereadable(l:tarfile)
Bram Moolenaarab194812005-09-14 21:40:12 +0000160 echo "***error*** (tar#Browse) File not readable:" l:tarfile
161" call Dret("tar#Browse")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000162 return
163 endif
164
Bram Moolenaarab194812005-09-14 21:40:12 +0000165 call s:Say("\" tar.vim version " . g:loaded_tar)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000166 call s:Say("\" Browsing tarfile " . l:tarfile)
167 call s:Say("\" Hit ENTER to view a file in a new window")
168 call s:Say("")
169
170 silent execute "r!" . l:cat . "<'" . l:tarfile . "'| tar Ptf - "
171 0d "blank line
172 /^$/1
173
Bram Moolenaarab194812005-09-14 21:40:12 +0000174 setlocal noma nomod ro
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000175
Bram Moolenaarab194812005-09-14 21:40:12 +0000176 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
177" call Dret("tar#Browse")
178endfun
179
180" ---------------------------------------------------------------------
181" TarBrowseSelect: {{{1
182fun! s:TarBrowseSelect()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000183 let l:line = getline(".")
184
185 if (l:line =~ '^" ')
186 return
187 endif
188
189 if (l:line =~ '/$')
190 echo "Please specify a file, not a directory"
191 return
192 endif
193
194 let l:selection = "tarfile:" . b:tarfile . "/" . l:line
195 new
196 wincmd _
197 execute "e " . l:selection
Bram Moolenaarab194812005-09-14 21:40:12 +0000198endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000199
Bram Moolenaarab194812005-09-14 21:40:12 +0000200" ---------------------------------------------------------------------
201" TarCatCommand: kludge to deal with compressed archives {{{1
202fun! s:TarCatCommand(tarfile)
203" call Dfunc("s:TarCatCommand(tarfile<".a:tarfile.">)")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000204 if a:tarfile =~# '\.\(gz\|tgz\|Z\)$'
205 let l:cat = "gzip -d -c"
206 elseif a:tarfile =~# '\.bz2$'
207 let l:cat = "bzip2 -d -c"
208 else
209 let l:cat = "cat"
210 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000211" call Dret("s:TarCatCommand ".l:cat)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000212 return l:cat
Bram Moolenaarab194812005-09-14 21:40:12 +0000213endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000214
Bram Moolenaarab194812005-09-14 21:40:12 +0000215" ---------------------------------------------------------------------
216" Say: {{{1
217fun! s:Say(string)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000218 let @" = a:string
219 $ put
Bram Moolenaarab194812005-09-14 21:40:12 +0000220endfun
221
222" ---------------------------------------------------------------------
223" Rmdir: {{{1
224fun! s:Rmdir(fname)
225" call Dfunc("Rmdir(fname<".a:fname.">)")
226 if has("unix")
227 call system("/bin/rm -rf ".a:fname)
228 elseif has("win32") || has("win95") || has("win64") || has("win16")
229 if &shell =~? "sh$"
230 call system("/bin/rm -rf ".a:fname)
231 else
232 call system("del /S ".a:fname)
233 endif
234 endif
235" call Dret("Rmdir")
236endfun
237
238" ---------------------------------------------------------------------
239" Modelines: {{{1
240" vim:set ts=8 sts=4 sw=4 fdm=marker: