Subject: Re: bug#1039: 23.0.60; emacs forgets EOL convention after revert-buffer

From: martin rudalics <[email-masked]>
Date: Sun, 28 Sep 2008 00:12:14 +0200

> If you load a file with DOS line endings, and revert it with M-x
 > revert-buffer, emacs forgets the used EOL convention and will use
 > Unix line endings when writing the file.

The inverse scenario of this is biting me for some time already.
I'm currently using the attached patch.

martin
*** files.el.~1.999.~	2008-09-22 07:39:31.937500000 +0200
--- files.el	2008-09-26 13:49:18.968750000 +0200
***************
*** 4437,4442 ****
--- 4437,4443 ----
  mechanism, this hook is not used.")
  
  (defvar revert-buffer-internal-hook)
+ (defvar revert-buffer-file-coding-system)
  
  (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes)
    "Replace current buffer text with the text of the visited file on disk.
***************
*** 4558,4571 ****
  		     ;; (called from insert-file-contents) to set
  		     ;; buffer-file-coding-system to a proper value.
  		     (kill-local-variable 'buffer-file-coding-system)
! 
  		     ;; Note that this preserves point in an intelligent way.
  		     (if preserve-modes
  			 (let ((buffer-file-format buffer-file-format))
  			   (insert-file-contents file-name (not auto-save-p)
  						 nil nil t))
  		       (insert-file-contents file-name (not auto-save-p)
! 					     nil nil t)))))
  		 ;; Recompute the truename in case changes in symlinks
  		 ;; have changed the truename.
  		 (setq buffer-file-truename
--- 4559,4578 ----
  		     ;; (called from insert-file-contents) to set
  		     ;; buffer-file-coding-system to a proper value.
  		     (kill-local-variable 'buffer-file-coding-system)
! 		     ;; But in case something goes wrong there provide a
! 		     ;; fallback coding system.
! 		     (setq revert-buffer-file-coding-system
! 			   (list revert-buffer-file-coding-system))
  		     ;; Note that this preserves point in an intelligent way.
  		     (if preserve-modes
  			 (let ((buffer-file-format buffer-file-format))
  			   (insert-file-contents file-name (not auto-save-p)
  						 nil nil t))
  		       (insert-file-contents file-name (not auto-save-p)
! 					     nil nil t))
! 		     ;; Remember this.
! 		     (setq revert-buffer-file-coding-system
! 			   buffer-file-coding-system))))
  		 ;; Recompute the truename in case changes in symlinks
  		 ;; have changed the truename.
  		 (setq buffer-file-truename

*** international/mule.el.~1.274.~	2008-07-31 07:33:47.000000000 +0200
--- international/mule.el	2008-09-26 13:55:52.640625000 +0200
***************
*** 1146,1151 ****
--- 1146,1163 ----
        (put (intern name) 'coding-system-define-form form)
        (setq coding-system-alist (cons (list name) coding-system-alist)))))
  
+ ;;; This is nil initially, set to `buffer-file-coding-system' when the
+ ;;; latter is set, and to a list with that value as its only element
+ ;;; when the buffer shall be reverted.  `after-insert-file-set-coding'
+ ;;; will use this value when `find-new-buffer-file-coding-system' does
+ ;;; not provide a useful value.
+ (defvar revert-buffer-file-coding-system nil
+   "The file coding system usable for reverting a buffer.
+ Internally used by when reverting a buffer and no other coding
+ system is provided.")
+ (make-variable-buffer-local 'revert-buffer-file-coding-system)
+ (put 'revert-buffer-file-coding-system 'permanent-local t)
+ 
  (defun set-buffer-file-coding-system (coding-system &optional force nomodify)
    "Set the file coding-system of the current buffer to CODING-SYSTEM.
  This means that when you save the buffer, it will be converted
***************
*** 1169,1174 ****
--- 1181,1188 ----
        (setq coding-system
  	    (merge-coding-systems coding-system buffer-file-coding-system)))
    (setq buffer-file-coding-system coding-system)
+   ;; And record its value here.
+   (setq revert-buffer-file-coding-system coding-system)
    ;; This is in case of an explicit call.  Normally, `normal-mode' and
    ;; `set-buffer-major-mode-hook' take care of setting the table.
    (if (fboundp 'ucs-set-table-for-input) ; don't lose when building
***************
*** 1860,1866 ****
        (setq buffer-file-coding-system-explicit coding-system-for-read))
    (if last-coding-system-used
        (let ((coding-system
! 	     (find-new-buffer-file-coding-system last-coding-system-used)))
  	(when coding-system
  	  ;; Tell set-buffer-file-coding-system not to mark the file
  	  ;; as modified; we just read it, and it's supposed to be unmodified.
--- 1874,1883 ----
        (setq buffer-file-coding-system-explicit coding-system-for-read))
    (if last-coding-system-used
        (let ((coding-system
! 	     (or (find-new-buffer-file-coding-system last-coding-system-used)
! 		 ;; Use the coding system before revertin the buffer if
! 		 ;; we have nothing newer.
! 		 (car-safe revert-buffer-file-coding-system))))
  	(when coding-system
  	  ;; Tell set-buffer-file-coding-system not to mark the file
  	  ;; as modified; we just read it, and it's supposed to be unmodified.