Subject: [PATCH] [WIP] Fix mm multibyte

From: Manuel Giraud <[email-masked]>
Date: Wed, 03 Sep 2025 11:33:33 +0200

Tags: patch

Hi,

I'm trying to fix an issue in Gnus where some Atom sources (namely
planet.emacslife.com/atom.xml, here) are not rendered correctly.

This seems to be related to multibyte/unibyte buffer.  Here is a minimal
exemple to reproduce what I see:

--8<---------------cut here---------------start------------->8---
(defun my/gen-handle ()
  (with-current-buffer (get-buffer-create " foo")
    (erase-buffer)
    (insert "’…")
    (list (current-buffer) '("text/html"))))

(defun my/test ()
  (let ((handle (my/gen-handle)))
    (mm-with-part handle
      (buffer-string))))
--8<---------------cut here---------------end--------------->8---

When evaluating (my/test), see that the buffer string content does not
have the correct characters.

I get the behaviour I wanted with the attached patch but I don't know if
this is the way to handle this.

In GNU Emacs 31.0.50 (build 36, x86_64-unknown-openbsd7.7) of 2025-09-03
 built on computer
Repository revision: 6762ffca6b387df73b62db1adcec127317328604
Repository branch: mgi/mm-multibyte-wip
Windowing system distributor 'The X.Org Foundation', version 11.0.12101018
System Description: OpenBSD computer 7.7 GENERIC.MP#10 amd64

Configured using:
 'configure CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib
 MAKEINFO=gmakeinfo --prefix=/home/manuel/emacs
 --bindir=/home/manuel/bin --with-x-toolkit=no
 --with-toolkit-scroll-bars=no --without-cairo --without-dbus
 --without-gconf --without-gsettings --without-compress-install'

>From 6762ffca6b387df73b62db1adcec127317328604 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Wed, 3 Sep 2025 10:36:13 +0200
Subject: [PATCH] [WIP] Fix mm multibyte

---
 lisp/gnus/mm-decode.el | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lisp/gnus/mm-decode.el b/lisp/gnus/mm-decode.el
index 759d19a047e..ba4e4983ab3 100644
--- a/lisp/gnus/mm-decode.el
+++ b/lisp/gnus/mm-decode.el
@@ -1306,12 +1306,13 @@ mm-with-part
   ;; handle-buffer is multibyte for some reason, in which case now is a good
   ;; time to adjust it, since we know at this point that it should
   ;; be unibyte.
-  `(let* ((handle ,handle))
-     (when (and (mm-handle-buffer handle)
-		(buffer-name (mm-handle-buffer handle)))
+  `(let* ((handle ,handle)
+          (handle-buffer (mm-handle-buffer handle)))
+     (when (and handle-buffer (buffer-name handle-buffer))
        (with-temp-buffer
-	 (mm-disable-multibyte)
-	 (insert-buffer-substring (mm-handle-buffer handle))
+         (unless (with-current-buffer handle-buffer (mm-multibyte-p))
+	   (mm-disable-multibyte))
+	 (insert-buffer-substring handle-buffer)
 	 (mm-decode-content-transfer-encoding
 	  (mm-handle-encoding handle)
 	  (mm-handle-media-type handle))
-- 
2.51.0

-- 
Manuel Giraud