[PATCH 2/2] add edit function to resume postponed emails

Austin Clements amdragon at mit.edu
Sat Jul 16 12:31:31 PDT 2011


I think this could be simplified a lot and many of the known issues
addressed if this were narrowed to *only* resuming from drafts.
message-mode draft files aren't MIME messages (or, at least, they're
never multipart, and message-mode has its own special annotations over
basic RFC 822), so rather than treating the draft as a MIME message
and trying to transform it back into a message-mode-compatible draft
(which, in full generality, would be somewhere between hard and
impossible), what about just dumping the raw contents of the draft
file into a buffer and pointing message-mode at it?  If the draft file
is available, you could even open it directly (this wouldn't work for
remote usage, but remote drafts introduce many other problems, too).

2011/7/16 Antoine Beaupré <anarcat at koumbit.org>:
> 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.
>
> "draft" 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
>  3. encoding is broken when files are reloaded, because we don't parse
>  MIME back
>  4. draft files are left around when mails are written, even if they
>  are not postponed
>
> Todo:
>
>  1. use the proper gnus hooks to resume emails:
>  https://www.gnu.org/software/emacs/manual/html_node/message/Message-Actions.html#index-message_002dpostpone_002dactions-334
>
>  2. write tests
>
> Signed-off-by: Antoine Beaupré <anarcat at koumbit.org>
> ---
>  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..11d014d 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 "+deleted")
> +  (notmuch-tag query-string "-draft")
> +)
> +
> +(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 c83b992..1efde1c 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)
> @@ -1165,6 +1166,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 f6fb07b..b522715 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)
> @@ -449,6 +450,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
>
> _______________________________________________
> notmuch mailing list
> notmuch at notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch
>


More information about the notmuch mailing list