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