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