1 " Vim autoload file for browsing debian package.
2 " copyright (C) 2007-2008, arno renevier <arenevier@fdn.fr>
3 " Distributed under the GNU General Public License (version 2 or above)
4 " Last Change: 2008 april 1
6 " Inspired by autoload/tar.vim by Charles E Campbell
8 " Latest version of that file can be found at
9 " http://www.fdn.fr/~arenevier/vim/autoload/deb.vim
10 " It should also be available at
11 " http://www.vim.org/scripts/script.php?script_id=1970
13 if &cp || exists("g:loaded_deb") || v:version < 700
16 let g:loaded_deb= "v1.4"
18 fun! deb#read(debfile, member)
20 " checks if ar and tar are installed
21 if !s:hascmd("ar") || !s:hascmd("tar")
25 let l:target = a:member
27 let l:archmember = s:dataFileName(a:debfile) " default archive member to extract
29 echohl WarningMsg | echo "***error*** (deb#read) no valid data file found in debian archive"
31 elseif l:archmember == "data.tar.gz"
32 let l:unpcmp = "tar zxfO "
33 elseif l:archmember == "data.tar.bz2"
34 let l:unpcmp = "tar jxfO "
35 elseif l:archmember == "data.tar.lzma"
39 let l:unpcmp = "lzma -d | tar xfO "
40 elseif l:archmember == "data.tar"
41 let l:unpcmp = "tar xfO "
44 if a:member =~ '^\* ' " information control file
45 let l:archmember = "control.tar.gz"
46 let l:target = substitute(l:target, "^\* ", "", "")
47 let l:unpcmp = "tar zxfO "
48 elseif a:member =~ ' -> ' " symbolic link
49 let l:target = split(a:member,' -> ')[0]
50 let l:linkname = split(a:member,' -> ')[1]
52 if l:linkname =~ "^\/" " direct symlink: path is already absolute
53 let l:target = ".".l:linkname
56 " transform relative path to absolute path
58 " first, get basename for target
59 let l:target = substitute(l:target, "\/[^/]*$", "", "")
61 " while it begins with ../
62 while l:linkname =~ "^\.\.\/"
64 " removes one level of ../ in linkname
65 let l:linkname = substitute(l:linkname, "^\.\.\/", "", "")
67 " go one directory up in target
68 let l:target = substitute(l:target, "\/[^/]*$", "", "")
71 let l:target = l:target."/".l:linkname
75 " we may preprocess some files (such as man pages, or changelogs)
81 if l:target =~ "\.\/usr\/share\/man\/.*\.gz$"
83 " try to fail gracefully if a command is not available
86 elseif !s:hascmd("nroff")
87 let l:preproccmd = "| gzip -cd"
88 elseif !s:hascmd("col")
89 let l:preproccmd = "| gzip -cd | nroff -mandoc"
91 let l:preproccmd = "| gzip -cd | nroff -mandoc | col -b"
95 " unzip other .gz files
97 elseif l:target =~ '.*\.gz$'
101 let l:preproccmd = "| gzip -cd"
105 exe "silent r! ar p " . s:QuoteFile(a:debfile) . " " . s:QuoteFile(l:archmember) . " | " . l:unpcmp . " - " . s:QuoteFile(l:target) . l:preproccmd
106 " error will be treated in calling function
107 if v:shell_error != 0
111 exe "file deb:".l:target
115 setlocal nomodifiable nomodified readonly
119 fun! deb#browse(file)
121 " checks if necessary utils are installed
122 if !s:hascmd("dpkg") || !s:hascmd("ar") || !s:hascmd("tar")
126 " checks if file is readable
127 if !filereadable(a:file)
131 echohl WarningMsg | echo "***error*** (deb#Browse) filename cannot contain quote character (" . a:file . ")"
135 let keepmagic = &magic
138 " set filetype to "deb"
141 setlocal modifiable noreadonly
144 exe "$put ='".'\"'." deb.vim version ".g:loaded_deb."'"
145 exe "$put ='".'\"'." Browsing debian package ".a:file."'"
149 "exe "silent read! dpkg -I ".a:file
152 " display information control files
153 let l:infopos = line(".")
154 exe "silent read! ar p " . s:QuoteFile(a:file) . " control.tar.gz | tar zt"
159 let l:listpos = line(".")
160 exe "silent read! dpkg -c ". s:QuoteFile(a:file)
162 " format information control list
163 " removes '* ./' line
164 exe (l:infopos + 1). 'd'
165 " add a star before each line
166 exe "silent " . (l:infopos + 1). ',' . (l:listpos - 2) . 's/^/\* /'
169 exe "silent " . l:listpos . ',$s/^.*\s\(\.\/\(\S\|\).*\)$/\1/'
171 if v:shell_error != 0
172 echohl WarningMsg | echo "***warning*** (deb#Browse) error when listing content of " . a:file
173 let &magic = keepmagic
179 setlocal nomodifiable readonly
180 noremap <silent> <buffer> <cr> :call <SID>DebBrowseSelect()<cr>
181 let &magic = keepmagic
185 fun! s:DebBrowseSelect()
186 let l:fname= getline(".")
189 if (l:fname !~ '^\.\/') && (l:fname !~ '^\* \.\/')
193 echohl WarningMsg | echo "***error*** (DebBrowseSelect) filename cannot contain quote character (" . l:fname . ")"
197 " do nothing on directories
198 " TODO: find a way to detect symlinks to directories, to be able not to
200 if (l:fname =~ '\/$')
204 " need to get it now since a new window will open
205 let l:curfile= expand("%")
211 call deb#read(l:curfile, l:fname)
213 if v:shell_error != 0
214 echohl WarningMsg | echo "***warning*** (DebBrowseSelect) error when reading " . l:fname
220 " zipped files, are unziped in deb#read, but filetype may not
221 " automatically work.
222 if l:fname =~ "\.\/usr\/share\/man\/.*\.gz$"
224 elseif l:fname =~ "\.\/usr\/share\/doc\/.*\/changelog.Debian.gz$"
225 set filetype=debchangelog
230 " return data file name for debian package. This can be either data.tar.gz,
231 " data.tar.bz2 or data.tar.lzma
232 fun s:dataFileName(deb)
233 for fn in ["data.tar.gz", "data.tar.bz2", "data.tar.lzma", "data.tar"]
234 " [0:-2] is to remove trailing null character from command output
235 if (system("ar t " . "'" . a:deb . "'" . " " . fn))[0:-2] == fn
239 return "" " no debian data format in this archive
242 fun s:QuoteFile(file)
243 " we need to escape %, #, <, and >
244 " see :help cmdline-specialk
245 return "'" . substitute(a:file, '\([%#<>]\)', '\\\1', 'g') . "'"
248 " return 1 if cmd exists
249 " display error message and return 0 otherwise
254 echohl Error | echo "***error*** " . a:cmd . " not available on your system"