annotate runtime/autoload/gzip.vim @ 1045:7f01e52cc97a v7.0.171

updated for version 7.0-171
author vimboss
date Tue, 28 Nov 2006 16:43:58 +0000
parents 656853d9c014
children 857a5e3e1d34
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
1 " Vim autoload file for editing compressed files.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
3 " Last Change: 2006 Oct 03
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
4
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
5 " These functions are used by the gzip plugin.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
6
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
7 " Function to check that executing "cmd [-f]" works.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
8 " The result is cached in s:have_"cmd" for speed.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
9 fun s:check(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
10 let name = substitute(a:cmd, '\(\S*\).*', '\1', '')
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
11 if !exists("s:have_" . name)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
12 let e = executable(name)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
13 if e < 0
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
14 let r = system(name . " --version")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
15 let e = (r !~ "not found" && r != "")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
16 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
17 exe "let s:have_" . name . "=" . e
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
18 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
19 exe "return s:have_" . name
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
20 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
21
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
22 " Set b:gzip_comp_arg to the gzip argument to be used for compression, based on
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
23 " the flags in the compressed file.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
24 " The only compression methods that can be detected are max speed (-1) and max
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
25 " compression (-9).
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
26 fun s:set_compression(line)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
27 " get the Compression Method
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
28 let l:cm = char2nr(a:line[2])
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
29 " if it's 8 (DEFLATE), we can check for the compression level
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
30 if l:cm == 8
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
31 " get the eXtra FLags
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
32 let l:xfl = char2nr(a:line[8])
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
33 " max compression
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
34 if l:xfl == 2
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
35 let b:gzip_comp_arg = "-9"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
36 " min compression
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
37 elseif l:xfl == 4
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
38 let b:gzip_comp_arg = "-1"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
39 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
40 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
41 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
42
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
43
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
44 " After reading compressed file: Uncompress text in buffer with "cmd"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
45 fun gzip#read(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
46 " don't do anything if the cmd is not supported
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
47 if !s:check(a:cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
48 return
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
49 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
50
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
51 " for gzip check current compression level and set b:gzip_comp_arg.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
52 silent! unlet b:gzip_comp_arg
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
53 if a:cmd[0] == 'g'
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
54 call s:set_compression(getline(1))
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
55 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
56
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
57 " make 'patchmode' empty, we don't want a copy of the written file
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
58 let pm_save = &pm
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
59 set pm=
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
60 " remove 'a' and 'A' from 'cpo' to avoid the alternate file changes
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
61 let cpo_save = &cpo
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
62 set cpo-=a cpo-=A
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
63 " set 'modifiable'
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
64 let ma_save = &ma
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
65 setlocal ma
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
66 " when filtering the whole buffer, it will become empty
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
67 let empty = line("'[") == 1 && line("']") == line("$")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
68 let tmp = tempname()
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
69 let tmpe = tmp . "." . expand("<afile>:e")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
70 " write the just read lines to a temp file "'[,']w tmp.gz"
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
71 execute "silent '[,']w " . escape(tmpe, ' ')
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
72 " uncompress the temp file: call system("gzip -dn tmp.gz")
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
73 call system(a:cmd . " " . s:escape(tmpe))
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
74 if !filereadable(tmp)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
75 " uncompress didn't work! Keep the compressed file then.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
76 echoerr "Error: Could not read uncompressed file"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
77 return
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
78 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
79 " delete the compressed lines; remember the line number
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
80 let l = line("'[") - 1
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
81 if exists(":lockmarks")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
82 lockmarks '[,']d _
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
83 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
84 '[,']d _
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
85 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
86 " read in the uncompressed lines "'[-1r tmp"
819
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 446
diff changeset
87 " Use ++edit if the buffer was empty, keep the 'ff' and 'fenc' options.
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
88 setlocal nobin
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
89 if exists(":lockmarks")
819
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 446
diff changeset
90 if empty
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 446
diff changeset
91 execute "silent lockmarks " . l . "r ++edit " . tmp
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 446
diff changeset
92 else
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 446
diff changeset
93 execute "silent lockmarks " . l . "r " . tmp
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 446
diff changeset
94 endif
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
95 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
96 execute "silent " . l . "r " . tmp
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
97 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
98
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
99 " if buffer became empty, delete trailing blank line
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
100 if empty
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
101 silent $delete _
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
102 1
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
103 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
104 " delete the temp file and the used buffers
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
105 call delete(tmp)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
106 silent! exe "bwipe " . tmp
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
107 silent! exe "bwipe " . tmpe
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
108 let &pm = pm_save
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
109 let &cpo = cpo_save
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
110 let &l:ma = ma_save
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
111 " When uncompressed the whole buffer, do autocommands
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
112 if empty
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
113 if &verbose >= 8
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
114 execute "doau BufReadPost " . expand("%:r")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
115 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
116 execute "silent! doau BufReadPost " . expand("%:r")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
117 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
118 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
119 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
120
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
121 " After writing compressed file: Compress written file with "cmd"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
122 fun gzip#write(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
123 " don't do anything if the cmd is not supported
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
124 if s:check(a:cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
125 " Rename the file before compressing it.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
126 let nm = resolve(expand("<afile>"))
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
127 let nmt = s:tempname(nm)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
128 if rename(nm, nmt) == 0
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
129 if exists("b:gzip_comp_arg")
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
130 call system(a:cmd . " " . b:gzip_comp_arg . " " . s:escape(nmt))
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
131 else
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
132 call system(a:cmd . " " . s:escape(nmt))
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
133 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
134 call rename(nmt . "." . expand("<afile>:e"), nm)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
135 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
136 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
137 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
138
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
139 " Before appending to compressed file: Uncompress file with "cmd"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
140 fun gzip#appre(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
141 " don't do anything if the cmd is not supported
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
142 if s:check(a:cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
143 let nm = expand("<afile>")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
144
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
145 " for gzip check current compression level and set b:gzip_comp_arg.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
146 silent! unlet b:gzip_comp_arg
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
147 if a:cmd[0] == 'g'
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
148 call s:set_compression(readfile(nm, "b", 1)[0])
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
149 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
150
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
151 " Rename to a weird name to avoid the risk of overwriting another file
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
152 let nmt = expand("<afile>:p:h") . "/X~=@l9q5"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
153 let nmte = nmt . "." . expand("<afile>:e")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
154 if rename(nm, nmte) == 0
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
155 if &patchmode != "" && getfsize(nm . &patchmode) == -1
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
156 " Create patchmode file by creating the decompressed file new
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
157 call system(a:cmd . " -c " . s:escape(nmte) . " > " . s:escape(nmt))
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
158 call rename(nmte, nm . &patchmode)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
159 else
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
160 call system(a:cmd . " " . s:escape(nmte))
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
161 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
162 call rename(nmt, nm)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
163 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
164 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
165 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
166
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
167 " find a file name for the file to be compressed. Use "name" without an
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
168 " extension if possible. Otherwise use a weird name to avoid overwriting an
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
169 " existing file.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
170 fun s:tempname(name)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
171 let fn = fnamemodify(a:name, ":r")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
172 if !filereadable(fn) && !isdirectory(fn)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
173 return fn
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
174 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
175 return fnamemodify(a:name, ":p:h") . "/X~=@l9q5"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
176 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
177
985
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
178 fun s:escape(name)
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
179 " shellescape() was added by patch 7.0.111
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
180 if v:version > 700 || (v:version == 700 && has('patch111'))
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
181 return shellescape(a:name)
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
182 endif
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
183 return "'" . a:name . "'"
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
184 endfun
656853d9c014 updated for version 7.0-111
vimboss
parents: 922
diff changeset
185
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
186 " vim: set sw=2 :