]> git.notmuchmail.org Git - notmuch/blobdiff - emacs/notmuch-show.el
emacs: Use `mm-display-part' when possible
[notmuch] / emacs / notmuch-show.el
index 8668481f9829c2550c18c6d30ace6fd8186c6aed..7d25dd570f4e600eedfc1563a5eec37e932494a2 100644 (file)
@@ -24,6 +24,7 @@
 (require 'cl)
 (require 'mm-view)
 (require 'message)
+(require 'mm-decode)
 
 (require 'notmuch-lib)
 (require 'notmuch-query)
@@ -202,17 +203,42 @@ message at DEPTH in the current thread."
        (narrow-to-region start (point-max))
        (run-hooks 'notmuch-show-markup-headers-hook)))))
 
-(defun notmuch-show-insert-part-header (content-type)
+(defun notmuch-show-insert-part-header (content-type &optional name)
   (let ((start (point)))
     ;; XXX dme: Make this a more useful button (save the part, display
     ;; external, etc.)
-    (insert "[ Part of type " content-type ". ]\n")
+    (insert "[ Part of type "
+           content-type
+           (if name (concat " named " name) "")
+           ". ]\n")
     (overlay-put (make-overlay start (point)) 'face 'bold)))
 
 ;; Functions handling particular MIME parts.
 
-(defun notmuch-show-insert-part-text/plain (part content-type depth)
+(defun notmuch-show-mm-display-part-inline (part content-type)
+  "Use the mm-decode/mm-view functions to display a part inline, if possible."
+  (let ((handle (mm-make-handle nil (list content-type))))
+    (if (and (mm-inlinable-p handle)
+            (mm-inlined-p handle))
+       (progn
+         (insert (with-temp-buffer
+                   (let ((display-buffer (current-buffer)))
+                     (with-temp-buffer
+                       (let ((work-buffer (current-buffer)))
+                         (insert (plist-get part :content))
+                         (set-buffer display-buffer)
+                         (mm-display-part (mm-make-handle work-buffer
+                                                          (list content-type)))
+                         (buffer-string))))))
+         t)))
+  nil)
+
+(defun notmuch-show-insert-part-text/plain (part content-type nth depth)
   (let ((start (point)))
+    ;; If this text/plain part is not the first part in the message,
+    ;; insert a header to make this clear.
+    (if (> nth 1)
+       (notmuch-show-insert-part-header content-type (plist-get part :filename)))
     (insert (plist-get part :content))
     (save-excursion
       (save-restriction
@@ -220,16 +246,11 @@ message at DEPTH in the current thread."
        (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
   t)
 
-(defun notmuch-show-insert-part-text/* (part content-type depth)
-  ;; Handle all text types other than text/html.
-  (if (string-equal "text/html" content-type)
-      nil
-    (notmuch-show-insert-part-header content-type)
-    (insert (plist-get part :content))
-    t))
-
-(defun notmuch-show-insert-part-*/* (part content-type depth)
-  (notmuch-show-insert-part-header content-type)
+(defun notmuch-show-insert-part-*/* (part content-type nth depth)
+  (notmuch-show-insert-part-header content-type (plist-get part :filename))
+  ;; If we have the content for the part, attempt to inline it.
+  (if (plist-get part :content)
+      (notmuch-show-mm-display-part-inline part content-type))
   t)
 
 ;; Functions for determining how to handle MIME parts.
@@ -257,11 +278,12 @@ message at DEPTH in the current thread."
 (defun notmuch-show-insert-bodypart (part depth)
   "Insert the body part PART at depth DEPTH in the current thread."
   (let* ((content-type (downcase (plist-get part :content-type)))
-        (handlers (notmuch-show-handlers-for content-type)))
+        (handlers (notmuch-show-handlers-for content-type))
+        (nth (plist-get part :id)))
     ;; Run the content handlers until one of them returns a non-nil
     ;; value.
     (while (and handlers
-               (not (funcall (car handlers) part content-type depth)))
+               (not (funcall (car handlers) part content-type nth depth)))
       (setq handlers (cdr handlers))))
   ;; Ensure that the part ends with a carriage return.
   (if (not (bolp))