[PATCH] emacs: Introduce notmuch-draftify-buffer command.

Subject: [PATCH] emacs: Introduce notmuch-draftify-buffer command.

Date: Wed, 11 Sep 2024 12:09:49 -0400

To: notmuch@notmuchmail.org

Cc:

From: Brennan Vincent


This command inserts the current buffer as a draft. It optionally
begins editing the draft.

This can be useful for email-based Git contribution workflows. For
example, one can use git format-patch to generate a patch, then
notmuch-draftify-buffer on the generated patch to immediately begin
editing a message for submission to an appropriate mailing list.
---
 emacs/notmuch-draft.el       | 10 ++++++++++
 emacs/notmuch-lib.el         | 12 ++++++++----
 emacs/notmuch-maildir-fcc.el | 16 ++++++++++------
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/emacs/notmuch-draft.el b/emacs/notmuch-draft.el
index fcc45503..4bff68f2 100644
--- a/emacs/notmuch-draft.el
+++ b/emacs/notmuch-draft.el
@@ -278,6 +278,16 @@ applied to newly inserted messages)."
       ;; message is resaved or sent.
       (setq notmuch-draft-id (and draft id)))))
 
+(defun notmuch-draftify-buffer (arg)
+  "Inserts the current buffer as a draft. With prefix argument, begins editing the draft."
+  (interactive "P")
+  (let ((message-id (notmuch-maildir-notmuch-insert-current-buffer
+                     notmuch-draft-folder
+                     t
+                     notmuch-draft-tags)))
+    (when (and arg message-id)
+      (notmuch-draft-resume (format "id:%s" message-id)))))
+
 ;;; _
 
 (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index bf9c4a53..1e371f67 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -935,11 +935,15 @@ supported keyword arguments are:
 
 If notmuch exits with a non-zero status, output from the process
 will appear in a buffer named \"*Notmuch errors*\" and an error
-will be signaled."
+will be signaled.
+
+Otherwise, returns the output from the process as a string."
   (with-temp-buffer
-    (let ((status (notmuch-call-notmuch--helper t args)))
-      (notmuch-check-exit-status status (cons notmuch-command args)
-				 (buffer-string)))))
+    (let ((status (notmuch-call-notmuch--helper t args))
+	  (output (buffer-string)))
+      (and (notmuch-check-exit-status status (cons notmuch-command args)
+				      output)
+	   output))))
 
 (defun notmuch-call-notmuch-sexp (&rest args)
   "Invoke `notmuch-command' with ARGS and return the parsed S-exp output.
diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index c7b403cf..09fc832d 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -215,12 +215,16 @@ normal fcc."
 This inserts the current buffer as a message into the notmuch
 database in folder FOLDER. If CREATE is non-nil it will supply
 the --create-folder flag to create the folder if necessary. TAGS
-should be a list of tag changes to apply to the inserted message."
-  (apply 'notmuch-call-notmuch-process
-	 :stdin-string (buffer-string) "insert"
-	 (append (and create (list "--create-folder"))
-		 (list (concat "--folder=" folder))
-		 tags)))
+should be a list of tag changes to apply to the inserted message.
+
+Returns the message ID of the inserted message."
+  (let ((output (apply 'notmuch-call-notmuch-process
+		       :stdin-string (buffer-string) "insert"
+		       (append (and create (list "--create-folder"))
+			       (list (concat "--folder=" folder)
+				     "--emit-message-id")
+			       tags))))
+    (string-trim output)))
 
 (defun notmuch-maildir-fcc-with-notmuch-insert (fcc-header &optional create)
   "Store message with notmuch insert.
-- 
2.45.2


_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: