unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jani Nikula <jani@nikula.org>
To: notmuch@notmuchmail.org
Subject: [PATCH v2 1/2] emacs: create patch filename from subject for inline patch fake parts
Date: Mon, 26 Dec 2011 00:00:05 +0200	[thread overview]
Message-ID: <aa55383e8609a2e0d376744ff3e982fd072c58d6.1324849534.git.jani@nikula.org> (raw)
In-Reply-To: <cover.1324849534.git.jani@nikula.org>
In-Reply-To: <cover.1324849534.git.jani@nikula.org>

Use the mail subject line for creating a descriptive filename for the wash
generated inline patch fake parts. The names are similar to the ones
created by 'git format-patch'.

If the user has notmuch-wash-convert-inline-patch-to-part hook enabled in
notmuch-show-insert-text/plain-hook, this will change the old default
filename of "inline patch":

[ inline patch: text/x-diff ]

into, for example:

[ 0001-emacs-create-patch-filename-from-subject-for-inline.patch: text/x-diff ]

which is typically the same filename the sender had if he was using 'git
format-patch' and 'git send-email'.

Signed-off-by: Jani Nikula <jani@nikula.org>
---
 emacs/notmuch-wash.el |   43 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 42 insertions(+), 1 deletions(-)

diff --git a/emacs/notmuch-wash.el b/emacs/notmuch-wash.el
index 1f420b2..7d037f5 100644
--- a/emacs/notmuch-wash.el
+++ b/emacs/notmuch-wash.el
@@ -290,6 +290,44 @@ When doing so, maintaining citation leaders in the wrapped text."
 
 (defvar diff-file-header-re) ; From `diff-mode.el'.
 
+(defun notmuch-wash-subject-to-filename (subject &optional maxlen)
+  "Convert a mail SUBJECT into a filename.
+
+The resulting filename is similar to the names generated by \"git
+format-patch\", without the leading patch sequence number
+\"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
+style strings are removed prior to conversion.
+
+Optional argument MAXLEN is the maximum length of the resulting
+filename, before trimming any trailing . and - characters."
+  (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
+	 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
+	 (s (replace-regexp-in-string "\\.+" "." s))
+	 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
+	 (s (replace-regexp-in-string "[.-]*$" "" s)))
+    s))
+
+(defun notmuch-wash-subject-to-patch-sequence-number (subject)
+  "Convert a patch mail SUBJECT into a patch sequence number.
+
+Return the patch sequence number N from the last \"[PATCH N/M]\"
+style prefix in SUBJECT, or nil if such a prefix can't be found."
+  (when (string-match
+	 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
+	 subject)
+      (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
+
+(defun notmuch-wash-subject-to-patch-filename (subject)
+  "Convert a patch mail SUBJECT into a filename.
+
+The resulting filename is similar to the names generated by \"git
+format-patch\". If the patch mail was generated and sent using
+\"git format-patch/send-email\", this should re-create the
+original filename the sender had."
+  (let* ((n (notmuch-wash-subject-to-patch-sequence-number subject))
+	 (n (if n n 1)))
+    (format "%04d-%s.patch" n (notmuch-wash-subject-to-filename subject 52))))
+
 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
   "Convert an inline patch into a fake 'text/x-diff' attachment.
 
@@ -316,7 +354,10 @@ for error."
 	    (setq part (plist-put part :content-type "text/x-diff"))
 	    (setq part (plist-put part :content (buffer-string)))
 	    (setq part (plist-put part :id -1))
-	    (setq part (plist-put part :filename "inline patch"))
+	    (setq part (plist-put part :filename
+				  (notmuch-wash-subject-to-patch-filename
+				   (plist-get
+				    (plist-get msg :headers) :Subject))))
 	    (delete-region (point-min) (point-max))
 	    (notmuch-show-insert-bodypart nil part depth))))))
 
-- 
1.7.5.4

  reply	other threads:[~2011-12-25 22:00 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-18 23:02 [PATCH] emacs: create patch filename from subject for inline patch fake parts Jani Nikula
2011-12-20 20:05 ` Jani Nikula
2011-12-20 20:11   ` Dmitry Kurochkin
2011-12-20 21:52   ` Austin Clements
2011-12-21  9:21     ` David Edmondson
2011-12-21 14:40       ` Austin Clements
2011-12-21 20:21         ` Jani Nikula
2011-12-25 22:00 ` [PATCH v2 0/2] emacs: patch filename from subject Jani Nikula
2011-12-25 22:00   ` Jani Nikula [this message]
2011-12-26 12:06     ` [PATCH v2 1/2] emacs: create patch filename from subject for inline patch fake parts David Edmondson
2011-12-26 12:24       ` Jani Nikula
2011-12-26 20:38         ` Jameson Graef Rollins
2011-12-26 21:52           ` Jani Nikula
2011-12-26 22:05             ` David Edmondson
2011-12-25 22:00   ` [PATCH v2 2/2] test: emacs: test notmuch-wash-subject-to-* functions Jani Nikula
2011-12-27 16:04 ` [PATCH v3 0/3] emacs: patch filename from subject Jani Nikula
2011-12-27 16:04   ` [PATCH v3 1/3] emacs: add inline patch fake parts through a special handler Jani Nikula
2011-12-27 16:04   ` [PATCH v3 2/3] emacs: create patch filename from subject for inline patch fake parts Jani Nikula
2011-12-27 16:04   ` [PATCH v3 3/3] test: emacs: test notmuch-wash-subject-to-* functions Jani Nikula
2011-12-28 12:22   ` [PATCH v3 0/3] emacs: patch filename from subject David Bremner

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://notmuchmail.org/

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

  git send-email \
    --in-reply-to=aa55383e8609a2e0d376744ff3e982fd072c58d6.1324849534.git.jani@nikula.org \
    --to=jani@nikula.org \
    --cc=notmuch@notmuchmail.org \
    /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://yhetil.org/notmuch.git/

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).