vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

gzip.vim (6777B)


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