Subject: Re: bug#4623: 23.1.50; rmail changes encoding of characters on save

From: Eli Zaretskii <[email-masked]>
Date: Sun, 06 Dec 2009 21:17:03 +0200

> Date: Sat, 05 Dec 2009 23:52:54 +0200
> From: Eli Zaretskii <[email-masked]>
> Cc: [email-masked]
> 
> I think it goes a tad too far.  What I had in mind was way simpler:
> record the encoding of the current message inside rmail-show-message,
> then restore it in after-save-hook.

Here's what I came up with.  This is relative to current CVS.  It
seems to work, but I'd appreciate more testing.  Markus, could you
please see if it works for you?  TIA

Index: lisp/mail/rmail.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/mail/rmail.el,v
retrieving revision 1.562
diff -u -r1.562 rmail.el
--- lisp/mail/rmail.el	4 Dec 2009 05:00:47 -0000	1.562
+++ lisp/mail/rmail.el	6 Dec 2009 19:16:05 -0000
@@ -642,6 +642,9 @@
 (defvar rmail-enable-mime-composing nil
   "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
 
+;; We use this to record the encoding of the last displayed message.
+(defvar rmail-message-encoding nil)
+
 ;; FIXME unused.
 (defvar rmail-show-mime-function nil
   "Function to show MIME decoded message of RMAIL file.
@@ -1316,13 +1319,19 @@
 This function preserves the current buffer's modified flag, and also
 sets the current buffer's `buffer-file-coding-system' to that of
 `rmail-view-buffer'."
-  (let ((modp (buffer-modified-p))
-	(coding
+  (let ((modp-this (buffer-modified-p))
+	(modp-that
+	 (with-current-buffer rmail-view-buffer (buffer-modified-p)))
+	(coding-this buffer-file-coding-system)
+	(coding-that
 	 (with-current-buffer rmail-view-buffer
 	   buffer-file-coding-system)))
     (buffer-swap-text rmail-view-buffer)
-    (setq buffer-file-coding-system coding)
-    (restore-buffer-modified-p modp)))
+    (setq buffer-file-coding-system coding-that)
+    (with-current-buffer rmail-view-buffer
+      (setq buffer-file-coding-system coding-this)
+      (restore-buffer-modified-p modp-that))
+    (restore-buffer-modified-p modp-this)))
 
 (defun rmail-buffers-swapped-p ()
   "Return non-nil if the message collection is in `rmail-view-buffer'."
@@ -2748,6 +2757,7 @@
 	  (rmail-highlight-headers)
 					;(rmail-activate-urls)
 					;(rmail-process-quoted-material)
+	  (setq rmail-message-encoding buffer-file-coding-system)
 	  )
 	;; Update the mode-line with message status information and swap
 	;; the view buffer/mail buffer contents.
@@ -4185,10 +4195,20 @@
     (set-buffer rmail-view-buffer)
     ;; Prevent viewing different messages from messing up the coding. (Bug#4623)
     ;; FIXME is there a better solution?
-    (set (make-local-variable 'coding-system-for-write) 'no-conversion)
+    ;(set (make-local-variable 'coding-system-for-write) 'no-conversion)
     (widen)
     nil))
 
+;; Used to restore the encoding of the buffer where we show the
+;; current message.
+(defun rmail-after-save-hook ()
+  (if (or (eq rmail-view-buffer (current-buffer))
+	  (eq rmail-buffer (current-buffer)))
+      (with-current-buffer
+	  (if (rmail-buffers-swapped-p) rmail-buffer rmail-view-buffer)
+	(setq buffer-file-coding-system rmail-message-encoding))))
+(add-hook 'after-save-hook 'rmail-after-save-hook)
+
 
 ;;; Start of automatically extracted autoloads.