Mercurial > vim
annotate runtime/autoload/gzip.vim @ 32770:4027cefc2aab
Farewell to Bram and dedicate upcoming Vim 9.1 to him (#12749)
Commit: https://github.com/vim/vim/commit/e978b4534a5e10471108259118c0ef791106fd92
Author: Christian Brabandt <cb@256bit.org>
Date: Sun Aug 13 10:33:05 2023 +0200
Farewell to Bram and dedicate upcoming Vim 9.1 to him (https://github.com/vim/vim/issues/12749)
* Dedicate upcoming Vim 9.1 to Bram
Also replace in a few more places Brams email address and mention new
maintainers.
* Remove Bram from any Maintainer role
* runtime: Align Header
* it's mailing list not mailinglist
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 13 Aug 2023 10:45:08 +0200 |
parents | 876fbdd84e52 |
children | 828bcb1a37e7 |
rev | line source |
---|---|
446 | 1 " Vim autoload file for editing compressed files. |
32770
4027cefc2aab
Farewell to Bram and dedicate upcoming Vim 9.1 to him (#12749)
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
2 " Maintainer: The Vim Project <https://github.com/vim/vim> |
4027cefc2aab
Farewell to Bram and dedicate upcoming Vim 9.1 to him (#12749)
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
3 " Last Change: 2023 Aug 10 |
4027cefc2aab
Farewell to Bram and dedicate upcoming Vim 9.1 to him (#12749)
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
4 " Former Maintainer: Bram Moolenaar <Bram@vim.org> |
446 | 5 |
6 " These functions are used by the gzip plugin. | |
7 | |
8 " Function to check that executing "cmd [-f]" works. | |
9 " The result is cached in s:have_"cmd" for speed. | |
10 fun s:check(cmd) | |
11 let name = substitute(a:cmd, '\(\S*\).*', '\1', '') | |
12 if !exists("s:have_" . name) | |
13 let e = executable(name) | |
14 if e < 0 | |
15 let r = system(name . " --version") | |
16 let e = (r !~ "not found" && r != "") | |
17 endif | |
18 exe "let s:have_" . name . "=" . e | |
19 endif | |
20 exe "return s:have_" . name | |
21 endfun | |
22 | |
23 " Set b:gzip_comp_arg to the gzip argument to be used for compression, based on | |
24 " the flags in the compressed file. | |
25 " The only compression methods that can be detected are max speed (-1) and max | |
26 " compression (-9). | |
27 fun s:set_compression(line) | |
28 " get the Compression Method | |
29 let l:cm = char2nr(a:line[2]) | |
30 " if it's 8 (DEFLATE), we can check for the compression level | |
31 if l:cm == 8 | |
32 " get the eXtra FLags | |
33 let l:xfl = char2nr(a:line[8]) | |
34 " max compression | |
35 if l:xfl == 2 | |
36 let b:gzip_comp_arg = "-9" | |
37 " min compression | |
38 elseif l:xfl == 4 | |
39 let b:gzip_comp_arg = "-1" | |
40 endif | |
41 endif | |
42 endfun | |
43 | |
44 | |
45 " After reading compressed file: Uncompress text in buffer with "cmd" | |
46 fun gzip#read(cmd) | |
47 " don't do anything if the cmd is not supported | |
48 if !s:check(a:cmd) | |
49 return | |
50 endif | |
51 | |
52 " for gzip check current compression level and set b:gzip_comp_arg. | |
53 silent! unlet b:gzip_comp_arg | |
54 if a:cmd[0] == 'g' | |
55 call s:set_compression(getline(1)) | |
56 endif | |
57 | |
58 " make 'patchmode' empty, we don't want a copy of the written file | |
59 let pm_save = &pm | |
60 set pm= | |
61 " remove 'a' and 'A' from 'cpo' to avoid the alternate file changes | |
62 let cpo_save = &cpo | |
63 set cpo-=a cpo-=A | |
64 " set 'modifiable' | |
65 let ma_save = &ma | |
66 setlocal ma | |
10244
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
6336
diff
changeset
|
67 " set 'write' |
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
6336
diff
changeset
|
68 let write_save = &write |
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
6336
diff
changeset
|
69 set write |
1190 | 70 " Reset 'foldenable', otherwise line numbers get adjusted. |
71 if has("folding") | |
72 let fen_save = &fen | |
73 setlocal nofen | |
74 endif | |
75 | |
446 | 76 " when filtering the whole buffer, it will become empty |
77 let empty = line("'[") == 1 && line("']") == line("$") | |
78 let tmp = tempname() | |
79 let tmpe = tmp . "." . expand("<afile>:e") | |
1592 | 80 if exists('*fnameescape') |
81 let tmp_esc = fnameescape(tmp) | |
82 let tmpe_esc = fnameescape(tmpe) | |
83 else | |
84 let tmp_esc = escape(tmp, ' ') | |
85 let tmpe_esc = escape(tmpe, ' ') | |
86 endif | |
446 | 87 " write the just read lines to a temp file "'[,']w tmp.gz" |
1592 | 88 execute "silent '[,']w " . tmpe_esc |
446 | 89 " uncompress the temp file: call system("gzip -dn tmp.gz") |
985 | 90 call system(a:cmd . " " . s:escape(tmpe)) |
446 | 91 if !filereadable(tmp) |
92 " uncompress didn't work! Keep the compressed file then. | |
93 echoerr "Error: Could not read uncompressed file" | |
1190 | 94 let ok = 0 |
446 | 95 else |
1190 | 96 let ok = 1 |
97 " delete the compressed lines; remember the line number | |
98 let l = line("'[") - 1 | |
99 if exists(":lockmarks") | |
100 lockmarks '[,']d _ | |
101 else | |
102 '[,']d _ | |
103 endif | |
104 " read in the uncompressed lines "'[-1r tmp" | |
105 " Use ++edit if the buffer was empty, keep the 'ff' and 'fenc' options. | |
106 setlocal nobin | |
107 if exists(":lockmarks") | |
108 if empty | |
1592 | 109 execute "silent lockmarks " . l . "r ++edit " . tmp_esc |
1190 | 110 else |
1592 | 111 execute "silent lockmarks " . l . "r " . tmp_esc |
1190 | 112 endif |
113 else | |
1592 | 114 execute "silent " . l . "r " . tmp_esc |
1190 | 115 endif |
116 | |
117 " if buffer became empty, delete trailing blank line | |
819 | 118 if empty |
1190 | 119 silent $delete _ |
120 1 | |
819 | 121 endif |
1190 | 122 " delete the temp file and the used buffers |
123 call delete(tmp) | |
1592 | 124 silent! exe "bwipe " . tmp_esc |
125 silent! exe "bwipe " . tmpe_esc | |
446 | 126 endif |
6336 | 127 " Store the OK flag, so that we can use it when writing. |
128 let b:uncompressOk = ok | |
446 | 129 |
1190 | 130 " Restore saved option values. |
446 | 131 let &pm = pm_save |
132 let &cpo = cpo_save | |
133 let &l:ma = ma_save | |
10244
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
6336
diff
changeset
|
134 let &write = write_save |
1190 | 135 if has("folding") |
136 let &l:fen = fen_save | |
137 endif | |
138 | |
446 | 139 " When uncompressed the whole buffer, do autocommands |
1190 | 140 if ok && empty |
1592 | 141 if exists('*fnameescape') |
142 let fname = fnameescape(expand("%:r")) | |
143 else | |
144 let fname = escape(expand("%:r"), " \t\n*?[{`$\\%#'\"|!<") | |
145 endif | |
446 | 146 if &verbose >= 8 |
1592 | 147 execute "doau BufReadPost " . fname |
446 | 148 else |
1592 | 149 execute "silent! doau BufReadPost " . fname |
446 | 150 endif |
151 endif | |
152 endfun | |
153 | |
154 " After writing compressed file: Compress written file with "cmd" | |
155 fun gzip#write(cmd) | |
6336 | 156 if exists('b:uncompressOk') && !b:uncompressOk |
157 echomsg "Not compressing file because uncompress failed; reset b:uncompressOk to compress anyway" | |
446 | 158 " don't do anything if the cmd is not supported |
6336 | 159 elseif s:check(a:cmd) |
446 | 160 " Rename the file before compressing it. |
161 let nm = resolve(expand("<afile>")) | |
162 let nmt = s:tempname(nm) | |
163 if rename(nm, nmt) == 0 | |
164 if exists("b:gzip_comp_arg") | |
1668 | 165 call system(a:cmd . " " . b:gzip_comp_arg . " -- " . s:escape(nmt)) |
446 | 166 else |
1668 | 167 call system(a:cmd . " -- " . s:escape(nmt)) |
446 | 168 endif |
169 call rename(nmt . "." . expand("<afile>:e"), nm) | |
170 endif | |
171 endif | |
172 endfun | |
173 | |
174 " Before appending to compressed file: Uncompress file with "cmd" | |
175 fun gzip#appre(cmd) | |
176 " don't do anything if the cmd is not supported | |
177 if s:check(a:cmd) | |
178 let nm = expand("<afile>") | |
179 | |
180 " for gzip check current compression level and set b:gzip_comp_arg. | |
181 silent! unlet b:gzip_comp_arg | |
182 if a:cmd[0] == 'g' | |
183 call s:set_compression(readfile(nm, "b", 1)[0]) | |
184 endif | |
185 | |
186 " Rename to a weird name to avoid the risk of overwriting another file | |
187 let nmt = expand("<afile>:p:h") . "/X~=@l9q5" | |
188 let nmte = nmt . "." . expand("<afile>:e") | |
189 if rename(nm, nmte) == 0 | |
190 if &patchmode != "" && getfsize(nm . &patchmode) == -1 | |
191 " Create patchmode file by creating the decompressed file new | |
1668 | 192 call system(a:cmd . " -c -- " . s:escape(nmte) . " > " . s:escape(nmt)) |
446 | 193 call rename(nmte, nm . &patchmode) |
194 else | |
1668 | 195 call system(a:cmd . " -- " . s:escape(nmte)) |
446 | 196 endif |
197 call rename(nmt, nm) | |
198 endif | |
199 endif | |
200 endfun | |
201 | |
202 " find a file name for the file to be compressed. Use "name" without an | |
203 " extension if possible. Otherwise use a weird name to avoid overwriting an | |
204 " existing file. | |
205 fun s:tempname(name) | |
206 let fn = fnamemodify(a:name, ":r") | |
207 if !filereadable(fn) && !isdirectory(fn) | |
208 return fn | |
209 endif | |
210 return fnamemodify(a:name, ":p:h") . "/X~=@l9q5" | |
211 endfun | |
212 | |
985 | 213 fun s:escape(name) |
214 " shellescape() was added by patch 7.0.111 | |
1132 | 215 if exists("*shellescape") |
985 | 216 return shellescape(a:name) |
217 endif | |
218 return "'" . a:name . "'" | |
219 endfun | |
220 | |
446 | 221 " vim: set sw=2 : |