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

From: Stefan Monnier <[email-masked]>
Date: Wed, 06 Aug 2014 20:45:59 -0400

>> > -	      (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
> The problem is the binding of coding-system-for-write based on
> incorrect interpretation of buffer-file-name.  Why that causes the bug
> was explained in the text you elided.  The code avoids the binding,
> and thus fixes its adverse effects.

Actually, the code doesn't really avoid the binding: there's still
a let-binding.  So the variable's value is still restored when we finish.

Also, while I understand that the binding is wrong, I don't understand
why the "non-binding" is right.

>> but I'm having a hard time seeing why the above is "right".
> Do you see why the binding is "wrong"?

The other problem I see is with the filename-is-magic condition which
seems overly general.

> I agree that reverting the original change, which introduced this
> binding, is a better solution.  Again, the text you elided said
> precisely that.  I wonder why you didn't comment on that at all.

Because you said it very well, so I had nothing further to add.

> As for unintended consequences, I don't see how can any come out of
> that, since this just restores the code that was working for years.

Hmm... so you're saying this reverts part of the change?
[ I'm not very familiar with this code, in case you haven't noticed yet.  ]

> Of course, if you have a better suggestion that would be safe enough
> for the release branch, I'm all ears.

Don't know yet what to do for the release branch, but I suspect reverting
is the better option since this problem has been with us for many many
years already.

As for the right fix: move the backup-generation to a later point.
This means either to split write-region into several sub-elements that
basic-save-buffer-2 can call separately.  Or to add some kind of hook to
write-region so basic-save-buffer-2 can use it to create the backup at
the right time.
I'd prefer the "split" direction since write-region is much too large
for my taste.

        Stefan