[PATCH] WIP: use gnus supplied undisplay function to hide images

Subject: [PATCH] WIP: use gnus supplied undisplay function to hide images

Date: Sun, 3 Sep 2023 08:42:15 -0300

To: David Bremner, Jon Fineman, notmuch@notmuchmail.org


From: David Bremner

According to emacs upstream [1], we can't expect overlay invisibilityy
and images to get along. This is a first attempt to use the
"undisplayer" functions saved by gnus (specificially mm-insert-image)
to do the hiding.

There are some potential efficiency concerns. A second copy of all
parts is saved. That could (perhaps?) be optimized to only be done for
the toggled ones. Profile / test first, optimize later

[1]: https://lists.gnu.org/archive/html/emacs-devel/2023-08/msg00593.html
 emacs/notmuch-lib.el  |  1 +
 emacs/notmuch-show.el | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 84ba8c5e..a09f4ab8 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -703,6 +703,7 @@ current buffer, if possible."
 	  (when (mm-inlinable-p handle)
 	    (set-buffer display-buffer)
 	    (mm-display-part handle)
+	    (plist-put part :undisplayer (mm-handle-undisplayer handle))
 ;;; Generic Utilities
diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 36cce619..7911be5d 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -618,7 +618,7 @@ message at DEPTH in the current thread."
   (let ((button (or button (button-at (point)))))
     (when button
       (let ((overlay (button-get button 'overlay))
-	    (lazy-part (button-get button :notmuch-lazy-part)))
+	    (lazy-part 	(button-get button :notmuch-lazy-part)))
 	;; We have a part to toggle if there is an overlay or if there
 	;; is a lazy part.  If neither is present we cannot toggle the
 	;; part so we just return nil.
@@ -644,8 +644,25 @@ message at DEPTH in the current thread."
 		(when show
 		  (button-put button :notmuch-lazy-part nil)
 		  (notmuch-show-lazy-part lazy-part button))
-	      ;; else there must be an overlay.
-	      (overlay-put overlay 'invisible (not show))
+	      (let* ((part (plist-get properties :notmuch-part))
+		     (undisplayer (plist-get part :undisplayer))
+		     (mime-type (plist-get part :content-type))
+		     (redisplay-data (button-get button
+						 :notmuch-redisplay-data))
+		     ;; XXX FIXME use computed content type
+		     (imagep (string-match "^image/" mime-type)))
+		(cond
+		 ((and imagep (not show) undisplayer)
+		  ;; call undisplayer thunk created by gnus.
+		  (funcall undisplayer)
+		  ;; there is an extra newline left
+		  (delete-region
+		   (+ 1 (button-end button))
+		   (+ 2 (button-end button))))
+		 ((and imagep show redisplay-data)
+		  (notmuch-show-lazy-part redisplay-data button))
+		 (t
+		  (overlay-put overlay 'invisible (not show)))))
 ;;; Part content ID handling
@@ -1023,6 +1040,7 @@ will return nil if the CID is unknown or cannot be retrieved."
 	(narrow-to-region part-beg part-end)
 	(delete-region part-beg part-end)
+	(button-put button :notmuch-redisplay-data part-args)
 	(apply #'notmuch-show-insert-bodypart-internal part-args)
 	(indent-rigidly part-beg
@@ -1106,14 +1124,18 @@ is t, hide the part initially and show the button."
 			     (and deep button)
 			     (and high button)
 			     (and long button))))
-	 (content-beg (point)))
+	 (content-beg (point))
+	 (part-data (list msg part mime-type nth depth button)))
     ;; Store the computed mime-type for later use (e.g. by attachment handlers).
     (plist-put part :computed-type mime-type)
-    (if show-part
-	(notmuch-show-insert-bodypart-internal msg part mime-type nth depth button)
+    (cond
+     (show-part
+      (notmuch-show-insert-bodypart-internal msg part mime-type nth depth button)
+      (when button
+	(button-put button :notmuch-redisplay-data part-data)))
+     (t
       (when button
-	(button-put button :notmuch-lazy-part
-		    (list msg part mime-type nth depth button))))
+	(button-put button :notmuch-lazy-part part-data))))
     ;; Some of the body part handlers leave point somewhere up in the
     ;; part, so we make sure that we're down at the end.
     (goto-char (point-max))

notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org