Patch: plugin/gzip.vim, maintain compression levels

jamessan@jamessan.com jamessan@jamessan.com
Wed, 18 May 2005 13:29:43 -0400


--osDK9TLjxFScVI/L
Content-Type: multipart/mixed; boundary="IpbVkmxF4tDyP/Kb"
Content-Disposition: inline


--IpbVkmxF4tDyP/Kb
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline

On Mon, May 16, 2005 at 05:11:42PM +0200, Bram Moolenaar wrote:
> > The patch adds support for maintaining the compression level when
> > editing a .gz file.  If the gzip file was compressed using "max speed"
> > or "max compression", Vim will detect and use that compression level.
> > Otherwise, the default compression level is used (as before).
>
> Using an external command is a weird solution, the text is right there
> in the Vim buffer.  You can use Vim commands to get the byte.

Thanks for pointing out the (now obvious) idea of using Vim to get the
correct bytes.  I've attached an updated patch which does this.

James
--
GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@jamessan.com>

--IpbVkmxF4tDyP/Kb
Content-Type: text/plain; charset=utf-8
Content-Disposition: attachment; filename="125_gzip.vim.diff"
Content-Transfer-Encoding: quoted-printable

diff -ur vim63.orig/runtime/plugin/gzip.vim vim63/runtime/plugin/gzip.vim
--- vim63.orig/runtime/plugin/gzip.vim	2005-05-18 13:15:05.000000000 -0400
+++ vim63/runtime/plugin/gzip.vim	2005-05-18 13:21:01.000000000 -0400
@@ -48,12 +48,39 @@
   exe "return s:have_" . name
 endfun
=20
+" Set a buffer local variable which contains the compression method used w=
hen
+" this file was compressed.  This can then be used later when we recompress
+" the file in s:write().  This only matters if we're using gzip.  The only
+" compression methods that can be detected are max speed (-1) and max
+" compression (-9).
+fun s:set_compression()
+  let b:compression =3D ""
+  if !s:have_gzip
+    return
+  endif
+  " get the Compression Method
+  let l:cm =3D char2nr(getline('1')[2])
+  " if it's 8 (DEFLATE), we can check for the compression level
+  if l:cm =3D=3D 8
+    " get the eXtra FLags
+    let l:xfl =3D char2nr(getline('1')[8])
+    " max compression
+    if l:xfl =3D=3D 2
+      let b:compression =3D "-9"
+    " min compression
+    elseif l:xfl =3D=3D 4
+      let b:compression =3D "-1"
+    endif
+  endif
+endfun
+
 " After reading compressed file: Uncompress text in buffer with "cmd"
 fun s:read(cmd)
   " don't do anything if the cmd is not supported
   if !s:check(a:cmd)
     return
   endif
+  call s:set_compression()
   " make 'patchmode' empty, we don't want a copy of the written file
   let pm_save =3D &pm
   set pm=3D
@@ -116,7 +143,7 @@
     let nm =3D resolve(expand("<afile>"))
     let nmt =3D s:tempname(nm)
     if rename(nm, nmt) =3D=3D 0
-      call system(a:cmd . " " . nmt)
+      call system(a:cmd . " " . b:compression . " " . nmt)
       call rename(nmt . "." . expand("<afile>:e"), nm)
     endif
   endif
@@ -126,6 +153,7 @@
 fun s:appre(cmd)
   " don't do anything if the cmd is not supported
   if s:check(a:cmd)
+    call s:set_compression()
     " Rename to a weird name to avoid the risk of overwriting another file
     let nm =3D expand("<afile>")
     let nmt =3D expand("<afile>:p:h") . "/X~=3D@l9q5"

--IpbVkmxF4tDyP/Kb--

--osDK9TLjxFScVI/L
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iEYEARECAAYFAkKLe4cACgkQDb3UpmEybUDhowCcCM24cnDugMZ6JgVdq7FUPkEB
xjkAn0SqqS+svuz1nZmgT9evCbJVkTMr
=WT7S
-----END PGP SIGNATURE-----

--osDK9TLjxFScVI/L--