discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Subject: [PATCH v2 3/3] gnus, notmuch: Absorb now-shared bits into patch attachment helper
Date: Mon, 04 Jan 2021 01:54:48 GMT	[thread overview]
Message-ID: <20210104015435.18397-4-kyle@kyleam.com> (raw)
In-Reply-To: <20210104015435.18397-1-kyle@kyleam.com>

With the previous commit, -notmuch more closely follows -gnus in its
handling of attachments (e.g., getting the content with
mm-display-inline).  Replace piem-am-patch-attachment-p with a helper
that has this shared logic.
---
 piem-gnus.el    | 14 ++------------
 piem-notmuch.el | 10 ++--------
 piem.el         | 21 +++++++++++++--------
 3 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/piem-gnus.el b/piem-gnus.el
index c6b9d0c..5f10be8 100644
--- a/piem-gnus.el
+++ b/piem-gnus.el
@@ -66,18 +66,8 @@ (defun piem-gnus-am-ready-mbox ()
   (when (derived-mode-p 'gnus-article-mode 'gnus-summary-mode)
     (cond
      (gnus-article-mime-handles
-      (when-let ((patches
-                  (delq nil
-                        (mapcar (lambda (handle)
-                                  (and (listp handle)
-                                       (piem-am-patch-attachment-p
-                                        (mm-handle-media-type handle)
-                                        (mm-handle-filename handle))
-                                       (with-temp-buffer
-                                         (mm-display-inline handle)
-                                         (buffer-substring-no-properties
-                                          (point-min) (point-max)))))
-                                gnus-article-mime-handles))))
+      (when-let ((patches (delq nil (mapcar #'piem-am-extract-attached-patch
+                                            gnus-article-mime-handles))))
         (cons (lambda ()
                 (dolist (patch patches)
                   (insert patch)))
diff --git a/piem-notmuch.el b/piem-notmuch.el
index 915675e..37e695b 100644
--- a/piem-notmuch.el
+++ b/piem-notmuch.el
@@ -92,14 +92,8 @@ (defun piem-notmuch-am-ready-mbox ()
                               "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))))
+           (when-let ((patch (piem-am-extract-attached-patch p)))
+             (push patch patches)))
          handle)
         (when patches
           (setq patches (nreverse patches))
diff --git a/piem.el b/piem.el
index bf79813..8c23e7b 100644
--- a/piem.el
+++ b/piem.el
@@ -40,6 +40,7 @@
 (require 'cl-lib)
 (require 'mail-extr)
 (require 'message)
+(require 'mm-decode)
 (require 'piem-maildir)
 (require 'rfc2047)
 (require 'subr-x)
@@ -573,14 +574,18 @@ (defun piem-inject-thread-into-maildir (mid &optional message-only)
 \f
 ;;;; Patch handling
 
-(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"))
-      (and filename
-           (equal type "text/plain")
-           (string-match-p "\\.patch\\'" filename))))
+(defun piem-am-extract-attached-patch (handle)
+  "Return content for HANDLE if it looks like a patch."
+  (and (listp handle)
+       (let ((type (mm-handle-media-type handle))
+             (filename (mm-handle-filename handle)))
+         (or (member type '("text/x-diff" "text/x-patch"))
+             (and filename
+                  (equal type "text/plain")
+                  (string-match-p "\\.patch\\'" filename))))
+       (with-temp-buffer
+         (mm-display-inline handle)
+         (buffer-substring-no-properties (point-min) (point-max)))))
 
 (defun piem-extract-mbox-info (&optional buffer)
   "Collect information from message in BUFFER.
-- 
2.29.2


  parent reply	other threads:[~2021-01-04  1:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-03 18:09 [PATCH 0/3] notmuch: Improve handling of attached patches Kyle Meyer
2021-01-03 18:09 ` [PATCH 1/3] piem-notmuch--with-current-message: Declare debug and indent specs Kyle Meyer
2021-01-03 18:09 ` [PATCH 2/3] piem-notmuch-am-ready-mbox: Improve handling of attachments Kyle Meyer
2021-01-04  1:53   ` Kyle Meyer
2021-01-04  1:54     ` [PATCH v2 0/3] notmuch: Improve handling of attached patches Kyle Meyer
2021-01-04  1:54       ` [PATCH v2 1/3] piem-notmuch--with-current-message: Declare debug and indent specs Kyle Meyer
2021-01-04  1:54       ` [PATCH v2 2/3] piem-notmuch-am-ready-mbox: Improve handling of attachments Kyle Meyer
2021-01-04  1:54       ` Kyle Meyer [this message]
2021-01-03 18:09 ` [PATCH 3/3] gnus, notmuch: Absorb now-shared bits into patch attachment helper Kyle Meyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://git.kyleam.com/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210104015435.18397-4-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.kyleam.com/piem/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).