Subject: Re: bug#18141: 24.4.50; saving .gz file breaks file coding

From: Stefan Monnier <[email-masked]>
Date: Wed, 06 Aug 2014 13:48:27 -0400

> --- lisp/files.el~0	2014-06-29 06:54:20 +0300
> +++ lisp/files.el	2014-08-06 17:26:42 +0300
> @@ -4835,13 +4835,17 @@
>  						   (nth 1 setmodes)))
>  		 (set-file-modes buffer-file-name
>  				 (logior (car setmodes) 128))))))
> -	(let (success)
> +	(let ((filename-is-magic (find-file-name-handler buffer-file-name
> +							 'write-region))
> +	      success)
>  	  (unwind-protect
>                  ;; Pass in nil&nil rather than point-min&max to indicate
>                  ;; we're saving the buffer rather than just a region.
>                  ;; write-region-annotate-functions may make us of it.
> -	      (let ((coding-system-for-write writecoding)
> -		    (coding-system-require-warning nil))
> +	      (let ((coding-system-for-write
> +		     (if filename-is-magic coding-system-for-write writecoding))
> +		    (coding-system-require-warning
> +		     (if filename-is-magic coding-system-require-warning)))
>  		(write-region nil nil
>  			      buffer-file-name nil t buffer-file-truename)
>  		(setq success t))

I can vaguely guess why that avoids the problem, but I'm having a hard
time seeing why the above is "right".  IOW it seems like an ugly
workaround which may itself come with unintended consequences.

        Stefan