File: deb.vim

package info (click to toggle)
vim-deb 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 96 kB
  • sloc: makefile: 2
file content (224 lines) | stat: -rw-r--r-- 5,928 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
" Vim autoload file for browsing debian package.
" copyright (C) 2007-2008, arno renevier <arenevier@fdn.fr>
" Distributed under the GNU General Public License (version 2 or above)
" Last Change: 2008 april 1
" 
" Inspired by autoload/tar.vim by Charles E Campbell
"
" Latest version of that file can be found at
" http://www.fdn.fr/~arenevier/vim/autoload/deb.vim
" It should also be available at
" http://www.vim.org/scripts/script.php?script_id=1970

if &cp || exists("g:loaded_deb") || v:version < 700
    finish
endif
let g:loaded_deb= "v1.4"

fun! deb#read(debfile, member)

    " checks if ar and tar are installed
    if !s:hascmd("tar")
        return
    endif

    let l:target = a:member
    let l:archmember = '--fsys-tarfile'

    if a:member =~ '^\* ' " information control file
        let l:target = substitute(l:target, "^\* ", "", "")
        let l:archmember = '--ctrl-tarfile'
    elseif a:member =~ ' -> ' " symbolic link
        let l:target = split(a:member,' -> ')[0]
        let l:linkname = split(a:member,' -> ')[1]

        if l:linkname =~ "^\/" " direct symlink: path is already absolute
            let l:target = ".".l:linkname

        else 
        " transform relative path to absolute path

            " first, get basename for target
            let l:target = substitute(l:target, "\/[^/]*$", "", "")

            " while it begins with ../
            while l:linkname =~ "^\.\.\/" 

                " removes one level of ../ in linkname
                let l:linkname = substitute(l:linkname, "^\.\.\/", "", "")

                " go one directory up in target
                let l:target = substitute(l:target, "\/[^/]*$", "", "")
            endwhile

            let l:target = l:target."/".l:linkname
        endif
    endif

    " we may preprocess some files (such as man pages, or changelogs)
    let l:preproccmd = ""

    "
    " unzip man pages
    "
    if l:target =~ "\.\/usr\/share\/man\/.*\.gz$"
        
        " try to fail gracefully if a command is not available
        if !s:hascmd("gzip")
            return
        elseif !s:hascmd("nroff") 
            let l:preproccmd = "| gzip -cd"
        elseif !s:hascmd("col")
            let l:preproccmd = "| gzip -cd | nroff -mandoc"
        else
            let l:preproccmd = "| gzip -cd | nroff -mandoc | col -b"
        endif
    
    "
    " unzip other .gz files
    "
    elseif l:target =~ '.*\.gz$'
        if !s:hascmd("gzip")
            return
        endif
        let l:preproccmd = "| gzip -cd"
    endif

    " read content
    exe "silent r !dpkg-deb " . l:archmember . ' ' . shellescape(a:debfile, 1) . " | tar xfO - " . shellescape(l:target, 1) . l:preproccmd
    " error will be treated in calling function
    if v:shell_error != 0
        return
    endif

    exe "file deb:".l:target

    0d

    setlocal nomodifiable nomodified readonly

endfun

fun! deb#browse(file)

    " checks if necessary utils are installed
    if !s:hascmd("dpkg") || !s:hascmd("tar")
        return
    endif

    " checks if file is readable
    if !filereadable(a:file)
        return
    endif
    if a:file =~ "'"
        echohl WarningMsg | echo "***error*** (deb#Browse) filename cannot contain quote character (" . a:file . ")"
        return
    endif

    let keepmagic = &magic
    set magic

    " set filetype to "deb"
    set ft=deb

    setlocal modifiable noreadonly

    " set header
    exe "$put ='".'\"'." deb.vim version ".g:loaded_deb."'"
    exe "$put ='".'\"'." Browsing debian package ".a:file."'"
    $put=''

    " package info
    "exe "silent read! dpkg -I ".a:file
    "$put=''

    " display information control files
    let l:infopos = line(".")

    exe "silent read !dpkg-deb --ctrl-tarfile " . shellescape(a:file, 1) . ' | tar t'

    $put=''

    " display data files
    let l:listpos = line(".")
    exe "silent read !dpkg -c ". shellescape(a:file, 1)

    " format information control list
    " removes '* ./' line
    exe (l:infopos + 1). 'd'
    " add a star before each line
    exe "silent " . (l:infopos + 1). ',' . (l:listpos - 2) . 's/^/\* /'
    
    " format data list
    exe "silent " . l:listpos . ',$s/^.*\s\(\.\/\(\S\|\).*\)$/\1/'
    
    if v:shell_error != 0
        echohl WarningMsg | echo "***warning*** (deb#Browse) error when listing content of " . a:file
        let &magic = keepmagic
        return
    endif

    0d

    setlocal nomodifiable readonly
    noremap <silent> <buffer> <cr> :call <SID>DebBrowseSelect()<cr>
    let &magic = keepmagic

endfun

fun! s:DebBrowseSelect()
    let l:fname= getline(".")

    " sanity check
    if (l:fname !~ '^\.\/') && (l:fname !~ '^\* \.\/')
        return
    endif
    if l:fname =~ "'"
        echohl WarningMsg | echo "***error*** (DebBrowseSelect) filename cannot contain quote character (" . l:fname . ")"
        return
    endif

    " do nothing on directories
    " TODO: find a way to detect symlinks to directories, to be able not to
    " open them
    if (l:fname =~ '\/$')
        return
    endif

    " need to get it now since a new window will open
    let l:curfile= expand("%")
   
    " open new window
    new
    wincmd _

    call deb#read(l:curfile, l:fname)

    if v:shell_error != 0
        echohl WarningMsg | echo "***warning*** (DebBrowseSelect) error when reading " . l:fname
        return
    endif

    filetype detect

    " zipped files, are unziped in deb#read, but filetype may not
    " automatically work.
    if l:fname =~ "\.\/usr\/share\/man\/.*\.gz$"
        set filetype=man
    elseif l:fname =~ "\.\/usr\/share\/doc\/.*\/changelog.Debian.gz$"
        set filetype=debchangelog
    endif

endfun

" return 1 if cmd exists
" display error message and return 0 otherwise
fun s:hascmd(cmd)
    if executable(a:cmd)
        return 1
    else
        echohl Error | echo "***error*** " . a:cmd . " not available on your system"
        return 0
    else
endfu