Subject: Re: ^M problem in copying from NT4.0 to SUN

From: Chris Weikart <[email-masked]>
Date: Sat, 26 Apr 1997 11:26:31 -0400

>	I use Emacs on NT4.0, and sometimes save a text file to a
>SUN/UNIX machine (Samba connection) whose drive can be seen by our NT
>4.0. Later when I read it in Emacs on the UNIX, there are ^M's all over.

This is the CR/LF translation problem. There is a section in
http://www.cs.washington.edu/homes/voelker/ntemacs.html about this entitled
"How can I control CR/LF translation (e.g., to access UNIX files via NFS)?".
But it does not contain the latest wisdom on the subject.

May I suggest the following? It was cobbled together from various posts to
the mailing list. 'check-buffer-file-type' puts a buffer into text-mode or
not according to the contents of the file being loaded.
'toggle-buffer-file-type' allows you to change the mode by hand.

Both of these work like a charm for me. It would be great if Geoff would
put them in the FAQ.

-Chris

      ;; Associate the universal match regexp "" with the
      ;; function check-buffer-file-type, so any file will be
      ;; examined to automatically select the appropriate mode.
      ;; Add this check only after known filename patterns are
      ;; treated the way they should be.  (That's why we append
      ;; to the list, instead of replacing it).  You might want
      ;; to use more more restrictive pattern(s) for doing this
      ;; check.
      (setq file-name-buffer-file-type-alist
	    (append file-name-buffer-file-type-alist
		    '(("" . check-buffer-file-type))))

      (defun check-buffer-file-type (filename)
	"Examines the actual contents of the loaded file to see if
it should use text mode or binary."
	(if (and
	     (looking-at ".*\r\n")		          ;; has CR-LF sequence
	     (not (re-search-forward "[^\r]\n]" nil t)))  ;; and no LF w/o CR
	    nil					          ;; so use text mode
	  t))						  ;; else binary mode

      (defun toggle-buffer-file-type (arg)
	"Alternate value of buffer-file-type.

If you have loaded a file as binary that actually has the ^M's in it,
then switching to text mode will remove them in the buffer.  Of course
now that it's in text mode, it will save with the ^M's inserted.
Switching to binary mode does NOT have a reverse effect.  If you want
to disable that change on entering text mode, then use a negative
prefix argument, as described below.

A prefix argument will force the mode change in a particular
direction.  A positive prefix argument forces it to binary.  A zero
prefix argument forces text mode allowing the removal of ^M's (only
preceding ^J's).  A negative prefix argument forces text mode
disallowing the removal of ^M's.  

When the mode is changed the state of modification of the buffer is
preserved, even if the ^M's are removed."
	(interactive "P")
	(let ((old buffer-file-type)
	      (mod (buffer-modified-p))
	      (buffer-read-only nil))
	  (setq buffer-file-type
		(if arg (>= arg 1)
		  (not buffer-file-type)))
	  (if (and old
		   (not buffer-file-type)
		   (or (not arg)
		       (> arg -2)))
	      (save-excursion
		(beginning-of-buffer)
		(while (search-forward "\r\n" nil t)
		  (replace-match "\n" nil t))
		(set-buffer-modified-p mod))))
	(force-mode-line-update))

_______________________________________________________________________
Chris Weikart <[email-masked]>     Digital Equipment Corporation
Speech Interaction Group (Web Apps)     Cambridge Research Laboratory
tel: (617) 692-7649, (DTN) 259-7649     One Kendall Square Bldg. 700
fax: (617) 692-7650, (DTN) 259-7650     Cambridge MA 02139-1562 USA

http://www.research.digital.com/CRL/personal/weikart/