Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 1 | " vim:set ts=8 sts=4 sw=4: |
| 2 | " |
| 3 | " tar.vim -- a Vim plugin for browsing tarfiles |
| 4 | " Copyright (c) 2002, Michael C. Toren <mct@toren.net> |
| 5 | " Distributed under the GNU General Public License. |
| 6 | " |
| 7 | " Version: 1.01 |
| 8 | " Last Change: 2005 Jul 26 |
| 9 | " |
| 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. |
| 16 | |
| 17 | let s:version = "1.01" |
| 18 | |
| 19 | function! tar#Write(argument) |
| 20 | echo "ERROR: Sorry, no write support for tarfiles yet" |
| 21 | endfunction |
| 22 | |
| 23 | function! tar#Read(argument, cleanup) |
| 24 | let l:argument = a:argument |
| 25 | let l:argument = substitute(l:argument, '^tarfile:', '', '') |
| 26 | let l:argument = substitute(l:argument, '^\~', $HOME, '') |
| 27 | |
| 28 | let l:tarfile = l:argument |
| 29 | while 1 |
| 30 | if (l:tarfile == "" || l:tarfile == "/") |
| 31 | echo "ERROR: Could not find a readable tarfile in path:" l:argument |
| 32 | return |
| 33 | endif |
| 34 | |
| 35 | if filereadable(l:tarfile) " found it! |
| 36 | break |
| 37 | endif |
| 38 | |
| 39 | let l:tarfile = fnamemodify(l:tarfile, ":h") |
| 40 | endwhile |
| 41 | |
| 42 | let l:toextract = strpart(l:argument, strlen(l:tarfile) + 1) |
| 43 | |
| 44 | if (l:toextract == "") |
| 45 | return |
| 46 | endif |
| 47 | |
| 48 | let l:cat = s:TarCatCommand(l:tarfile) |
| 49 | execute "r !" . l:cat . " < '" . l:tarfile . "'" |
| 50 | \ " | tar OPxf - '" . l:toextract . "'" |
| 51 | |
| 52 | if (a:cleanup) |
| 53 | 0d "blank line |
| 54 | execute "doautocmd BufReadPost " . expand("%") |
| 55 | setlocal readonly |
| 56 | silent preserve |
| 57 | endif |
| 58 | endfunction |
| 59 | |
| 60 | function! tar#Browse(tarfile) |
| 61 | setlocal noswapfile |
| 62 | setlocal buftype=nofile |
| 63 | setlocal bufhidden=hide |
| 64 | setlocal filetype= |
| 65 | setlocal nobuflisted |
| 66 | setlocal buftype=nofile |
| 67 | setlocal wrap |
| 68 | setlocal syntax=tar |
| 69 | |
| 70 | let l:tarfile = a:tarfile |
| 71 | let b:tarfile = l:tarfile |
| 72 | let l:cat = s:TarCatCommand(l:tarfile) |
| 73 | |
| 74 | if ! filereadable(l:tarfile) |
| 75 | let l:tarfile = substitute(l:tarfile, '^tarfile:', '', '') |
| 76 | endif |
| 77 | |
| 78 | if ! filereadable(l:tarfile) |
| 79 | echo "ERROR: File not readable:" l:tarfile |
| 80 | return |
| 81 | endif |
| 82 | |
| 83 | call s:Say("\" tar.vim version " . s:version) |
| 84 | call s:Say("\" Browsing tarfile " . l:tarfile) |
| 85 | call s:Say("\" Hit ENTER to view a file in a new window") |
| 86 | call s:Say("") |
| 87 | |
| 88 | silent execute "r!" . l:cat . "<'" . l:tarfile . "'| tar Ptf - " |
| 89 | 0d "blank line |
| 90 | /^$/1 |
| 91 | |
| 92 | setlocal readonly |
| 93 | setlocal nomodifiable |
| 94 | noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr> |
| 95 | endfunction |
| 96 | |
| 97 | function! s:TarBrowseSelect() |
| 98 | let l:line = getline(".") |
| 99 | |
| 100 | if (l:line =~ '^" ') |
| 101 | return |
| 102 | endif |
| 103 | |
| 104 | if (l:line =~ '/$') |
| 105 | echo "Please specify a file, not a directory" |
| 106 | return |
| 107 | endif |
| 108 | |
| 109 | let l:selection = "tarfile:" . b:tarfile . "/" . l:line |
| 110 | new |
| 111 | wincmd _ |
| 112 | execute "e " . l:selection |
| 113 | endfunction |
| 114 | |
| 115 | " kludge to deal with compressed archives |
| 116 | function! s:TarCatCommand(tarfile) |
| 117 | if a:tarfile =~# '\.\(gz\|tgz\|Z\)$' |
| 118 | let l:cat = "gzip -d -c" |
| 119 | elseif a:tarfile =~# '\.bz2$' |
| 120 | let l:cat = "bzip2 -d -c" |
| 121 | else |
| 122 | let l:cat = "cat" |
| 123 | endif |
| 124 | return l:cat |
| 125 | endfunction |
| 126 | |
| 127 | function! s:Say(string) |
| 128 | let @" = a:string |
| 129 | $ put |
| 130 | endfunction |