Subject: EOL encoding and C-x i

From: Eli Zaretskii <[email-masked]>
Date: Mon, 29 Sep 1997 10:43:12 +0200 (IST)

`C-x i' changes the EOL encoding in a way that I find unexpected.  This 
is in Emacs 20.2 on MS-DOS.

To reproduce:

	emacs -q
	C-x C-f foobar.txt

(I assume `foobar.txt' doesn't exist, but I don't think that it matters.)
Put some text into the buffer, then save it.  The file foobar.txt is 
saved with DOS EOLs, like it should.

Now insert another file into the buffer:

	C-x i foo.bar

If `foo.bar' has Unix EOLs, the coding system of the current buffer is 
changed to *-dos, and the file is saved as such.

Is this done on purpose?  If so, I would like to know the reason.  I
would expect that, at least for a buffer which has been saved already in 
a file with DOS EOLs and have enough of them to qualify as a DOS text 
file, `C-x i' won't change the EOL encodings.

---

From: Kenichi Handa <[email-masked]>
Date: Mon, 29 Sep 1997 20:28:54 +0900

> Is this done on purpose?  If so, I would like to know the reason.

This is not done on purpose.  But, the behaviour of this reason is
simple; buffer-file-coding-system is not set locally just by saving
it.

> I would expect that, at least for a buffer which has been saved
> already in a file with DOS EOLs and have enough of them to qualify
> as a DOS text file, `C-x i' won't change the EOL encodings.

I agree that what you expect is quite reasonable, and agree that the
buffer-file-coding-system of a buffer once saved should not be changed
by inserting something later.  This can be achieved by binding
buffer-file-coding-system locally in that buffer.

The question is at which point we should bind it.  How about the
following change to basic-save-buffer?

---lisp/ChangeLog---------------------------------------------------------
	* files.el (basic-save-buffer): Set buffer-file-coding-system to
	the coding system actually used for saving.
---patch for lisp/files.el------------------------------------------------
diff -acrN --exclude=ChangeLog --exclude=*.elc --exclude=*~ --exclude=TAGS --exclude=loaddefs.el ../emacs-20.2.fsf/lisp/files.el ../emacs-20.2/lisp/files.el
*** ../emacs-20.2.fsf/lisp/files.el	Tue Sep  9 14:32:49 1997
--- ../emacs-20.2/lisp/files.el	Mon Sep 29 19:59:39 1997
***************
*** 2181,2186 ****
--- 2181,2190 ----
  		;; If a hook returned t, file is already "written".
  		;; Otherwise, write it the usual way now.
  		(setq setmodes (basic-save-buffer-1)))
+ 	    ;; Now we have saved the current buffer.  Let's make sure
+ 	    ;; that buffer-file-coding-system is fixed to what
+ 	    ;; actually used for saving by binding it locally.
+ 	    (setq buffer-file-coding-system last-coding-system-used)
  	    (setq buffer-file-number
  		  (nthcdr 10 (file-attributes buffer-file-name)))
  	    (if setmodes
------------------------------------------------------------------------

I've just tested this change on Unix by:
(1) At first, set default value of buffer-file-coding-system to
undecided-dos.

(2) Then, I visisted a new file.  At this moment,
buffer-file-coding-system was not set locally.

(3) Entered "abc\n" in the buffer and saved it.  Now
buffer-file-coding-system ws set to undecided-dos locally.

(4) Inserted some ascii file of Unix-like end-of-line codes.
buffer-file-coding-system was still undecided-dos.

(5) Inserted a file of iso-latin-1-unix.  Then
buffer-file-coding-system was changed to iso-latin-1-dos.

So, it seems that this change works well.

If you test it in your environment and find no problem, someone please
update FSF's code.  Now, I'm using a very narrow line, and it's quite
difficult to do the job of updating.

---
Ken'ichi HANDA
[email-masked]

---

From: Richard Stallman <[email-masked]>
Date: Tue, 30 Sep 1997 22:15:00 -0400

C-x i foo.bar

Do you mean it is changed to *-unix?

In that situation you are mixing the two kinds of EOLs, which means
that it is hard to be sre that either alternative is really right.
But I tend to agree with you.  Handa, how hard would this be?