Subject: Re: toggle binary/text mode of current buffer

From: Geoff Odhner <[email-masked]>
Date: Tue, 25 Mar 1997 20:20:21 -0500

Don Erway wrote:
> > The one funny is that files to which I have only read-only access come up as
> > writeable.
> 
> I spoke too soon.  It appears that the visiting read-only files works on NT,
> but under unix, a read-only file is not translated correctly.
> check-buffer-file-type works, but translation does not occur.
> 
> On NT, the files do get translated, and do not come up as writeable.

Try my latest version.  It should address this problem.  It works on 
win95,
but I haven't yet tested it on unix, though I'm expecting no problem.

BTW, one caveat about using this on unix:  Though this code works to
toggle the buffer type, the mode line indicator doesn't work on unix,
at least not on SunOS.  If you add the mode line %t indicator, it always
indicates T on the mode line.  I expect that requires a fix to the
C code and a recompile.  I guess they figured noone would ever use it
on unix. :-)

Happy editing...

-Geoff

And here's the new version, as promised:

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

(defun toggle-buffer-file-type (arg)
  "Alternate value of buffer-file-type"
  (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))

;; And my preferred key binding:
(global-set-key [?\A-t]	'toggle-buffer-file-type)