Newsgroups: comp.emacs

>>>>> "MF" == mickeyf@vnet.ibm.com (Mickey Ferguson) writes:

MF> I've got a bunch of DOS-format files which I need to be able to edit here
MF> on my Unix (SunOS 4.1.2) workstation.  By DOS-format, I mean that if I
MF> bring the file into a buffer, it would have a ^M at the end of every line.

MF> I'm looking to see if anyone has created something which could read in the
MF> file, filter it, allow the user to save, and the file gets written back
MF> out in the format it was created.  This would mean that the ^M's at the
MF> end of each line would not be there (or at least seen) while the user is
MF> editing, and also the ^Z end-of-file marker at the end of the file would
MF> be properly preserved.

Try this hack...  It doesn't play with ^Z, but does the rest of what you ask.

					-- ange -- <><

					ange@hplb.hpl.hp.com

; -*-Emacs-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; File:         dos-mode.el
; RCS:          $Header: dos-mode.el,v 1.5 92/08/15 20:18:31 ange Exp $
; Description:  MSDOS minor mode for GNU Emacs
; Author:       Andy Norman, Dawn
; Created:      Wed May  6 16:42:29 1992
; Modified:     Sat Aug 15 20:18:02 1992 (Ange) ange@hplb.hpl.hp.com
; Language:     Emacs-Lisp
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(provide 'dos-mode)

(defvar dos-mode-distance 200
  "Number of characters to search for RETURN when looking for a DOS file.")

(defvar dos-mode nil
  "This buffer is to be converted to/from DOS format when read/written.")

(make-variable-buffer-local 'dos-mode)
(setq-default dos-mode nil)

(or (assq 'dos-mode minor-mode-alist)
    (setq minor-mode-alist (cons '(dos-mode " DOS") minor-mode-alist)))

(defun dos-convert-buffer ()
  "Converts the current buffer from DOS format to UNIX format."
  (if (or dos-mode
	  (save-excursion
	    (goto-char (point-min))
	    (re-search-forward "\r\n"
			       (min (point-max) dos-mode-distance)
			       t)))
      (progn
	(let ((mod-p (buffer-modified-p))
	      (buffer-read-only nil))
	  (save-excursion
	    (goto-char (point-min))
	    (while (re-search-forward "\r" nil t)
	      (replace-match "")))
	  (set-buffer-modified-p mod-p)
	  (setq dos-mode t)))))

(defun dos-write-buffer ()
  "Write the buffer as a DOS format buffer."
  (if dos-mode
      (let* ((buffer-read-only nil)
	     (window (get-buffer-window (current-buffer)))
	     (p (point))
	     (m (mark))
	     (ws (and window (window-start window))))
	(goto-char (point-min))
	(while (re-search-forward "\n" nil t)
	  (replace-match "\r\n"))
	(unwind-protect
	    (write-region (point-min)
			  (point-max)
			  buffer-file-name
			  nil
			  t)
	  (dos-convert-buffer)	;convert back again!
	  (and window
	       (set-window-start window
				 (save-excursion (goto-char ws)
						 (beginning-of-line)
						 (point))))
	  (goto-char p)
	  (set-mark m))
	t)))

(or (memq 'dos-write-buffer write-file-hooks)
    (setq write-file-hooks
	  (append write-file-hooks
		  (list 'dos-write-buffer)))) ;stick it on the end

(or (memq 'dos-convert-buffer find-file-hooks)
    (setq find-file-hooks
	  (append find-file-hooks
		  (list 'dos-convert-buffer))))
Newsgroups: comp.emacs