summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2021-01-04 01:54:47 +0000
committerKyle Meyer <kyle@kyleam.com>2021-01-03 22:44:19 -0500
commitf5d67001ed706407b8fcda5447da64a4c215f0c2 (patch)
tree66d7f5bdef11c844961c2a9d1080cb7f9e7ae006
parent80388fab7c14fb09e8b1d20c2d44770b2036861a (diff)
downloadpiem-f5d67001ed706407b8fcda5447da64a4c215f0c2.tar.gz
piem-notmuch-am-ready-mbox: Improve handling of attachments
piem-notmuch-am-ready-mbox tries to get attached patches from the plist returned by notmuch-show-get-message-properties. This works okay for simple cases, but it doesn't find a patch if the content is encoded or if the MIME tree isn't structured as expected. Instead use mm-decode functions to get a list of handles and display their content. This new approach, as well as the previous one, probably isn't compatible with `format-patch --attach' patches, but I'm not going to worry about that until I actually see such a patch in the wild. Message-Id: <20210104015435.18397-3-kyle@kyleam.com>
-rw-r--r--piem-notmuch.el52
1 files changed, 29 insertions, 23 deletions
diff --git a/piem-notmuch.el b/piem-notmuch.el
index ff8654c..915675e 100644
--- a/piem-notmuch.el
+++ b/piem-notmuch.el
@@ -28,6 +28,7 @@
;;; Code:
+(require 'mm-decode)
(require 'notmuch)
(require 'piem)
(require 'subr-x)
@@ -78,29 +79,34 @@ If the buffer has any MIME parts that look like a patch, use
those parts' contents (in order) as the mbox. Otherwise, use the
message itself if it looks like a patch."
(when (derived-mode-p 'notmuch-show-mode)
- (let ((body (car (plist-get (notmuch-show-get-message-properties)
- :body))))
- (pcase (plist-get body :content-type)
- ((and "text/plain"
- (guard (string-match-p piem-patch-subject-re
- (notmuch-show-get-subject))))
- (let ((id (notmuch-show-get-message-id)))
- (lambda ()
- (call-process notmuch-command nil t nil
- "show" "--format=mbox" id))))
- ("multipart/mixed"
- (when-let ((patches
- (delq nil
- (mapcar (lambda (part)
- (and (piem-am-patch-attachment-p
- (plist-get part :content-type)
- (plist-get part :filename))
- (plist-get part :content)))
- (plist-get body :content)))))
- (cons (lambda ()
- (dolist (patch patches)
- (insert patch)))
- "mbox")))))))
+ (let* ((handle (piem-notmuch--with-current-message
+ (mm-dissect-buffer)))
+ (n-attachments (notmuch-count-attachments handle))
+ patches)
+ (if (= n-attachments 0)
+ (when (string-match-p piem-patch-subject-re
+ (notmuch-show-get-subject))
+ (let ((id (notmuch-show-get-message-id)))
+ (lambda ()
+ (call-process notmuch-command nil t nil
+ "show" "--format=mbox" id))))
+ (notmuch-foreach-mime-part
+ (lambda (p)
+ (and (piem-am-patch-attachment-p
+ (mm-handle-media-type p)
+ (mm-handle-filename p))
+ (with-temp-buffer
+ (mm-display-inline p)
+ (push (buffer-substring-no-properties
+ (point-min) (point-max))
+ patches))))
+ handle)
+ (when patches
+ (setq patches (nreverse patches))
+ (cons (lambda ()
+ (dolist (patch patches)
+ (insert patch)))
+ "mbox"))))))
;;;###autoload
(define-minor-mode piem-notmuch-mode