Patch: plugin/gzip.vim, maintain compression levels

jamessan@jamessan.com jamessan@jamessan.com
Sun, 8 May 2005 03:46:27 -0400


--xesSdrSSBC0PokLI
Content-Type: multipart/mixed; boundary="5/uDoXvLw7AC5HRs"
Content-Disposition: inline


--5/uDoXvLw7AC5HRs
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline

Bram,

I'm a member of the maintainer group for Debian's Vim package.  Attached
is a patch for plugin/gzip.vim to address the wishlist bug reported in
<http://bugs.debian.org/280388>.

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).

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

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

--- vim63.old/runtime/plugin/gzip.vim	2005-05-07 22:17:54.000000000 -0400
+++ vim63/runtime/plugin/gzip.vim	2005-05-08 02:58:38.674896896 -0400
@@ -37,6 +37,9 @@
 " The result is cached in s:have_"cmd" for speed.
 fun s:check(cmd)
   let name =3D substitute(a:cmd, '\(\S*\).*', '\1', '')
+  if name =3D~ "gzip"
+    call s:check("od")
+  endif
   if !exists("s:have_" . name)
     let e =3D executable(name)
     if e < 0
@@ -48,12 +51,34 @@
   exe "return s:have_" . name
 endfun
=20
+fun s:set_compression()
+  if !s:have_od
+    let b:compression =3D ""
+    return
+  endif
+  " get the Compression Method
+  let cm =3D system("od -j2 -N1 -d " . expand("<afile>"))
+  " if it's 8 (DEFLATE), we can check for the compression level
+  if match(cm, '\s8$')
+    " get the eXtra FLags
+    let xfl =3D system("od -j8 -N1 -d " . expand("<afile>"))
+    " max compression
+    if match(xfl, '\s2$') !=3D -1
+      let b:compression =3D "-9"
+    " min compression
+    elseif match(xfl, '\s4$') !=3D -1
+      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 +141,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 +151,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"

--5/uDoXvLw7AC5HRs--

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

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

iEYEARECAAYFAkJ9w9MACgkQDb3UpmEybUDJQwCgk0feni6iVqaP9tUDlbVuGwEV
upwAnAzrQOUMkjnhF4jkjVVauTSzt+rk
=Q/7Z
-----END PGP SIGNATURE-----

--xesSdrSSBC0PokLI--