I've been unable to use piem-am a few times because the attached patch was text/plain, but piem-notmuch-am-ready-mbox and piem-gnus-am-ready-mbox consider only text/x-diff and text/x-patch. [1/4] gnus: Prefer mm-handle-media-type over direct access [2/4] gnus, notmuch: Share "attachment is patch?" logic [3/4] Use when-let in a few more spots [4/4] gnus, notmuch: Consider text/plain patch attachments piem-gnus.el | 51 ++++++++++++++++++++++++------------------------- piem-notmuch.el | 24 +++++++++++------------ piem.el | 9 +++++++++ 3 files changed, 46 insertions(+), 38 deletions(-) base-commit: 88b445e0e10b4c59f385989218a4f05824da2b75 -- 2.29.2
--- piem-gnus.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/piem-gnus.el b/piem-gnus.el index 4afa747..237eeb1 100644 --- a/piem-gnus.el +++ b/piem-gnus.el @@ -69,7 +69,7 @@ (defun piem-gnus-am-ready-mbox () (delq nil (mapcar (lambda (handle) (and (listp handle) - (member (caar (cdr handle)) + (member (mm-handle-media-type handle) '("text/x-diff" "text/x-patch")) (with-temp-buffer (mm-display-inline handle) -- 2.29.2
This will gain another condition. Avoid repeating it across two spots. --- piem-gnus.el | 4 ++-- piem-notmuch.el | 4 ++-- piem.el | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/piem-gnus.el b/piem-gnus.el index 237eeb1..c1551ce 100644 --- a/piem-gnus.el +++ b/piem-gnus.el @@ -69,8 +69,8 @@ (defun piem-gnus-am-ready-mbox () (delq nil (mapcar (lambda (handle) (and (listp handle) - (member (mm-handle-media-type handle) - '("text/x-diff" "text/x-patch")) + (piem-am-patch-attachment-p + (mm-handle-media-type handle)) (with-temp-buffer (mm-display-inline handle) (buffer-substring-no-properties diff --git a/piem-notmuch.el b/piem-notmuch.el index cd3153a..ccfae75 100644 --- a/piem-notmuch.el +++ b/piem-notmuch.el @@ -91,8 +91,8 @@ (defun piem-notmuch-am-ready-mbox () (let ((patches (delq nil (mapcar (lambda (part) - (and (member (plist-get part :content-type) - '("text/x-diff" "text/x-patch")) + (and (piem-am-patch-attachment-p + (plist-get part :content-type)) (plist-get part :content))) (plist-get body :content))))) (when patches diff --git a/piem.el b/piem.el index 784d0b3..9cc6a88 100644 --- a/piem.el +++ b/piem.el @@ -561,6 +561,11 @@ (defun piem-inject-thread-into-maildir (mid &optional message-only) \f ;;;; Patch handling +(defun piem-am-patch-attachment-p (type) + "Return non-nil if an attachment should be treated as a patch. +TYPE is a media type such as \"text/x-patch\"." + (member type '("text/x-diff" "text/x-patch"))) + (defun piem-extract-mbox-info (&optional buffer) "Collect information from message in BUFFER. If BUFFER is nil, the current buffer is used. Any message after -- 2.29.2
--- piem-gnus.el | 50 ++++++++++++++++++++++++------------------------- piem-notmuch.el | 23 +++++++++++------------ 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/piem-gnus.el b/piem-gnus.el index c1551ce..ca128fb 100644 --- a/piem-gnus.el +++ b/piem-gnus.el @@ -65,33 +65,31 @@ (defun piem-gnus-am-ready-mbox () (when (derived-mode-p 'gnus-article-mode 'gnus-summary-mode) (cond (gnus-article-mime-handles - (let ((patches - (delq nil - (mapcar (lambda (handle) - (and (listp handle) - (piem-am-patch-attachment-p - (mm-handle-media-type handle)) - (with-temp-buffer - (mm-display-inline handle) - (buffer-substring-no-properties - (point-min) (point-max))))) - gnus-article-mime-handles)))) - (when patches - (cons (lambda () - (dolist (patch patches) - (insert patch))) - "mbox")))) + (when-let ((patches + (delq nil + (mapcar (lambda (handle) + (and (listp handle) + (piem-am-patch-attachment-p + (mm-handle-media-type handle)) + (with-temp-buffer + (mm-display-inline handle) + (buffer-substring-no-properties + (point-min) (point-max))))) + gnus-article-mime-handles)))) + (cons (lambda () + (dolist (patch patches) + (insert patch))) + "mbox"))) (gnus-article-buffer - (let ((patch (with-current-buffer gnus-article-buffer - (save-restriction - (widen) - (and (string-match-p piem-patch-subject-re - (message-field-value "subject")) - (buffer-substring-no-properties - (point-min) (point-max))))))) - (when patch - (cons (lambda () (insert patch)) - "mbox"))))))) + (when-let ((patch (with-current-buffer gnus-article-buffer + (save-restriction + (widen) + (and (string-match-p piem-patch-subject-re + (message-field-value "subject")) + (buffer-substring-no-properties + (point-min) (point-max))))))) + (cons (lambda () (insert patch)) + "mbox")))))) ;;;###autoload (define-minor-mode piem-gnus-mode diff --git a/piem-notmuch.el b/piem-notmuch.el index ccfae75..8f5ef77 100644 --- a/piem-notmuch.el +++ b/piem-notmuch.el @@ -88,18 +88,17 @@ (defun piem-notmuch-am-ready-mbox () (call-process notmuch-command nil t nil "show" "--format=mbox" id)))) ("multipart/mixed" - (let ((patches - (delq nil - (mapcar (lambda (part) - (and (piem-am-patch-attachment-p - (plist-get part :content-type)) - (plist-get part :content))) - (plist-get body :content))))) - (when patches - (cons (lambda () - (dolist (patch patches) - (insert patch))) - "mbox")))))))) + (when-let ((patches + (delq nil + (mapcar (lambda (part) + (and (piem-am-patch-attachment-p + (plist-get part :content-type)) + (plist-get part :content))) + (plist-get body :content))))) + (cons (lambda () + (dolist (patch patches) + (insert patch))) + "mbox"))))))) ;;;###autoload (define-minor-mode piem-notmuch-mode -- 2.29.2
When inspecting attachments for generating an am-ready mbox, both -notmuch and -gnus limit the operation to attachments with text/x-diff or text/x-patch content types. That has worked okay for me, though I've run into a few cases where I couldn't apply a patch attachment because it had a text/plain content type. To do something useful in this case, check the file name to see whether it looks like a patch. --- piem-gnus.el | 3 ++- piem-notmuch.el | 3 ++- piem.el | 10 +++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/piem-gnus.el b/piem-gnus.el index ca128fb..9dc4ae8 100644 --- a/piem-gnus.el +++ b/piem-gnus.el @@ -70,7 +70,8 @@ (defun piem-gnus-am-ready-mbox () (mapcar (lambda (handle) (and (listp handle) (piem-am-patch-attachment-p - (mm-handle-media-type handle)) + (mm-handle-media-type handle) + (mm-handle-filename handle)) (with-temp-buffer (mm-display-inline handle) (buffer-substring-no-properties diff --git a/piem-notmuch.el b/piem-notmuch.el index 8f5ef77..58e4d88 100644 --- a/piem-notmuch.el +++ b/piem-notmuch.el @@ -92,7 +92,8 @@ (defun piem-notmuch-am-ready-mbox () (delq nil (mapcar (lambda (part) (and (piem-am-patch-attachment-p - (plist-get part :content-type)) + (plist-get part :content-type) + (plist-get part :filename)) (plist-get part :content))) (plist-get body :content))))) (cons (lambda () diff --git a/piem.el b/piem.el index 9cc6a88..bee17c0 100644 --- a/piem.el +++ b/piem.el @@ -561,10 +561,14 @@ (defun piem-inject-thread-into-maildir (mid &optional message-only) \f ;;;; Patch handling -(defun piem-am-patch-attachment-p (type) +(defun piem-am-patch-attachment-p (type filename) "Return non-nil if an attachment should be treated as a patch. -TYPE is a media type such as \"text/x-patch\"." - (member type '("text/x-diff" "text/x-patch"))) +TYPE is a media type such as \"text/x-patch\". FILENAME is the +attachment file name, if any." + (or (member type '("text/x-diff" "text/x-patch")) + (and filename + (equal type "text/plain") + (string-match-p "\\.patch\\'" filename)))) (defun piem-extract-mbox-info (&optional buffer) "Collect information from message in BUFFER. -- 2.29.2
Kyle Meyer writes: > -(defun piem-am-patch-attachment-p (type) > +(defun piem-am-patch-attachment-p (type filename) > "Return non-nil if an attachment should be treated as a patch. > -TYPE is a media type such as \"text/x-patch\"." > - (member type '("text/x-diff" "text/x-patch"))) > +TYPE is a media type such as \"text/x-patch\". FILENAME is the > +attachment file name, if any." > + (or (member type '("text/x-diff" "text/x-patch")) > + (and filename > + (equal type "text/plain") > + (string-match-p "\\.patch\\'" filename)))) Oops, the indentation is off here. Will squash this in. diff --git a/piem.el b/piem.el index bee17c0..cf43503 100644 --- a/piem.el +++ b/piem.el @@ -565,7 +565,7 @@ (defun piem-am-patch-attachment-p (type filename) "Return non-nil if an attachment should be treated as a patch. TYPE is a media type such as \"text/x-patch\". FILENAME is the attachment file name, if any." - (or (member type '("text/x-diff" "text/x-patch")) + (or (member type '("text/x-diff" "text/x-patch")) (and filename (equal type "text/plain") (string-match-p "\\.patch\\'" filename))))