[PATCH] add edit function to resume postponed emails (v2)

Antoine Beaupré anarcat at koumbit.org
Sat Jul 16 02:21:15 PDT 2011


Add a new function to allow editing a new message starting from an
existing one, roughly the equivalent of Mutt's resend-message
functionality.

Hooks into the search and show views through the "e" keybinding.

"postponed" tag is removed after the email is sent and the target thread
is marked as deleted.

Known issues:

 1. only the first MIME part of the email is used
 2. running this on a thread with more than one message has not been
 tested

Signed-off-by: Antoine Beaupré <anarcat at koumbit.org>
---
Previous patch actually had a parse error, thanks to eval-defun...

 emacs/notmuch-mua.el  |   50 +++++++++++++++++++++++++++++++++++++++++++++++++
 emacs/notmuch-show.el |    6 +++++
 emacs/notmuch.el      |    7 ++++++
 3 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
index 274c5da..cd32c55 100644
--- a/emacs/notmuch-mua.el
+++ b/emacs/notmuch-mua.el
@@ -201,6 +201,56 @@ the From: address first."
 	   (list (cons 'from (notmuch-mua-prompt-for-sender))))))
     (notmuch-mua-mail nil nil other-headers)))
 
+(defun notmuch-mua-delete-postponed (query-string)
+  "Delete postponed mail after sending."
+  (notmuch-tag query-string "+delete")
+  (notmuch-tag query-string "-postponed")
+)
+
+(defun notmuch-mua-edit-mail (query-string)
+  "Create a new mail composition window based on the current mail."
+  (interactive)
+  (let (headers
+	body
+	(args '("show" "--format=raw")))
+    (if notmuch-show-process-crypto
+	(setq args (append args '("--decrypt"))))
+    (setq args (append args (list query-string)))
+    ;; This make assumptions about the output of `notmuch show', but
+    ;; really only that the headers come first followed by a blank
+    ;; line and then the body.
+    (with-temp-buffer
+      (apply 'call-process (append (list notmuch-command nil (list t t) nil) args))
+      (goto-char (point-min))
+      (if (re-search-forward "^$" nil t)
+	  (save-excursion
+	    (save-restriction
+	      (narrow-to-region (point-min) (point))
+	      (goto-char (point-min))
+	      (setq headers (mail-header-extract))))
+	  )
+      (forward-line 1)
+      (setq body (buffer-substring (point) (point-max)))
+      )
+
+    (let ((message-signature nil))
+      (notmuch-mua-mail (mail-header 'to headers)
+			(mail-header 'subject headers)
+			(message-headers-to-generate headers t '(to subject))
+			t nil nil (notmuch-mua-delete-postponed query-string))
+    )
+
+    ;; insert the message body - but put it in front of the signature
+    ;; if one is present
+    (goto-char (point-max))
+    (if (re-search-backward message-signature-separator nil t)
+	  (forward-line -1)
+      (goto-char (point-max)))
+    (insert body))
+  (set-buffer-modified-p nil)
+
+  (message-goto-body))
+
 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
   "Invoke the notmuch message forwarding window.
 
diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index f96743b..3698767 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -865,6 +865,7 @@ function is used. "
 	(define-key map "m" 'notmuch-mua-new-mail)
 	(define-key map "f" 'notmuch-show-forward-message)
 	(define-key map "r" 'notmuch-show-reply)
+	(define-key map "e" 'notmuch-show-edit)
 	(define-key map "|" 'notmuch-show-pipe-message)
 	(define-key map "w" 'notmuch-show-save-attachments)
 	(define-key map "V" 'notmuch-show-view-raw-message)
@@ -1164,6 +1165,11 @@ any effects from previous calls to
   (interactive "P")
   (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender))
 
+(defun notmuch-show-edit ()
+  "Edit the current message as new."
+  (interactive)
+  (notmuch-mua-edit-mail (notmuch-show-get-message-id)))
+
 (defun notmuch-show-forward-message (&optional prompt-for-sender)
   "Forward the current message."
   (interactive "P")
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index f11ec24..f18b739 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -204,6 +204,7 @@ For a mouse binding, return nil."
     (define-key map "p" 'notmuch-search-previous-thread)
     (define-key map "n" 'notmuch-search-next-thread)
     (define-key map "r" 'notmuch-search-reply-to-thread)
+    (define-key map "e" 'notmuch-search-edit)
     (define-key map "m" 'notmuch-mua-new-mail)
     (define-key map "s" 'notmuch-search)
     (define-key map "o" 'notmuch-search-toggle-order)
@@ -448,6 +449,12 @@ Complete list of currently available key bindings:
   (let ((message-id (notmuch-search-find-thread-id)))
     (notmuch-mua-new-reply message-id prompt-for-sender)))
 
+(defun notmuch-search-edit ()
+  "Edit the current message as new."
+  (interactive)
+  (let ((message-id (notmuch-search-find-thread-id)))
+    (notmuch-mua-edit-mail message-id)))
+
 (defun notmuch-call-notmuch-process (&rest args)
   "Synchronously invoke \"notmuch\" with the given list of arguments.
 
-- 
1.7.5.4



More information about the notmuch mailing list