;;; notmuch-message.el --- message-mode functions specific to notmuch ;; ;; Copyright © Jesse Rosenthal ;; ;; This file is part of Notmuch. ;; ;; Notmuch is free software: you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; ;; Notmuch is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with Notmuch. If not, see . ;; ;; Authors: Jesse Rosenthal ;;; Code: (require 'message) (require 'notmuch-tag) (require 'notmuch-mua) (declare-function notmuch-show-get-message-id "notmuch-show" (&optional bare)) (defcustom notmuch-message-replied-tags '("+replied") "List of tag changes to apply to a message when it has been replied to. Tags starting with \"+\" (or not starting with either \"+\" or \"-\") in the list will be added, and tags starting with \"-\" will be removed from the message being replied to. For example, if you wanted to add a \"replied\" tag and remove the \"inbox\" and \"todo\" tags, you would set: (\"+replied\" \"-inbox\" \"-todo\"\)" :type '(repeat string) :group 'notmuch-send) (defcustom notmuch-message-draft-tags '("+draft") "List of tags changes to apply to a draft message when it is saved in the database. Tags starting with \"+\" (or not starting with either \"+\" or \"-\") in the list will be added, and tags starting with \"-\" will be removed from the message being stored. For example, if you wanted to give the message a \"draft\" tag but not the (normally added by default) \"inbox\" tag, you would set: (\"+draft\" \"-inbox\")" :type '(repeat string) :group 'notmuch-send) (defcustom notmuch-message-draft-folder "drafts" "Folder to save draft messages in. This should be specified relative to the root of the notmuch database. It will be created if necessary." :type 'string :group 'notmuch-send) (defcustom notmuch-message-quoted-tags '("secure") "Mml tags to quote. This should be a list of mml tags to quote before saving. It is recommended that the list includes \"secure\". If you include \"part\" then attachments will not be saved with the draft -- if not then they will be saved with the draft. The former means the attachments may not still exist when you resume the message, the latter means that the attachments as they were when you postponed will be sent with the resumed message. Note you may get strange results if you change this between postponing and resuming a message." :type '(repeat string) :group 'notmuch-send) (defvar notmuch-message-draft-id nil "Message-id of the most recent saved draft of this message") (make-variable-buffer-local 'notmuch-message-draft-id) (defun notmuch-message-mark-replied () ;; get the in-reply-to header and parse it for the message id. (let ((rep (mail-header-parse-addresses (message-field-value "In-Reply-To")))) (when (and notmuch-message-replied-tags rep) (notmuch-tag (notmuch-id-to-query (car (car rep))) (notmuch-tag-change-list notmuch-message-replied-tags))))) (defun notmuch-message-mark-draft-deleted () "Tag the last saved draft deleted. Used when a new version is saved, or the message is sent." (when notmuch-message-draft-id (notmuch-tag notmuch-message-draft-id '("+deleted")))) (defun notmuch-message-quote-some-mml () "Quote the mml tags in `notmuch-message-quoted-tags`." ;; This is copied from mml-quote-region but only quotes the ;; specified tags. (when notmuch-message-quoted-tags (save-excursion (let ((re (concat "<#!*/?\\(" (mapconcat 'identity notmuch-message-quoted-tags "\\|") "\\)"))) (message-goto-body) (while (re-search-forward re nil t) ;; Insert ! after the #. (goto-char (+ (match-beginning 0) 2)) (insert "!")))))) (defun notmuch-message-unquote-some-mml () "Unquote the mml tags in `notmuch-message-quoted-tags`." (when notmuch-message-quoted-tags (save-excursion (let ((re (concat "<#!+/?\\(" (mapconcat 'identity notmuch-message-quoted-tags "\\|") "\\)"))) (message-goto-body) (while (re-search-forward re nil t) ;; Remove one ! from after the #. (goto-char (+ (match-beginning 0) 2)) (delete-char 1)))))) (defun notmuch-message-save-draft () "Save the current draft message in the notmuch database. This saves the current message in the database with tags `notmuch-message-draft-tags` (in addition to any default tags applied to newly inserted messages)." (interactive) ;; This is based on message-do-fcc but modified for our needs. (let ((case-fold-search t) (buf (current-buffer)) (mml-externalize-attachments nil) ;; We generate a message id now as we will need it later. Note ;; message-make-message-id gives the id inside a "<" ">" pair, ;; but notmuch doesn't want that form, so remove them. (id (concat "draft-" (substring (message-make-message-id) 1 -1)))) (with-current-buffer (get-buffer-create " *message temp*") (erase-buffer) (insert-buffer-substring buf) ;; We insert a Date header and a Message-ID header, the former ;; so that it is easier to search for the message, and the ;; latter so we have a way of accessing the saved message (for ;; example to delete it at a later time). We check that the ;; user has these in `message-deletable-headers` (the default) ;; as otherwise they are doing something strange and we ;; shouldn't interfere. Note, since we are doing this in a new ;; buffer we don't change the version in the compose buffer. (if (member 'Message-ID message-deletable-headers) (progn (message-remove-header "Message-ID") (message-add-header (concat "Message-ID: <" id ">"))) (message "You have customized emacs so Message-ID is not a deletable header, so not changing it") (setq id nil)) (if (member 'Date message-deletable-headers) (progn (message-remove-header "Date") (message-add-header (concat "Date: " (message-make-date)))) (message "You have customized emacs so Date is not a deletable header, so not changing it")) (notmuch-message-quote-some-mml) ;; Back to following message-do-fcc (message-encode-message-body) (save-restriction (message-narrow-to-headers) (let ((mail-parse-charset message-default-charset) (rfc2047-header-encoding-alist (cons '("Newsgroups" . default) rfc2047-header-encoding-alist))) (mail-encode-encoded-word-buffer))) (goto-char (point-min)) (when (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$") nil t) (replace-match "" t t )) (apply 'notmuch-call-notmuch-process :stdin-string (buffer-string) "insert" "--create-folder" (concat "--folder=" notmuch-message-draft-folder) notmuch-message-draft-tags)) ;; We are now back in the original compose buffer. Note the ;; function notmuch-call-notmuch-process signals an error on ;; failure, so to get to this point it must have succeeded. Note ;; notmuch-message-draft-id is still the id of the previous draft, ;; so it is safe to mark it deleted. (notmuch-message-mark-draft-deleted) (setq notmuch-message-draft-id (concat "id:" id)) (set-buffer-modified-p nil))) (defun notmuch-message-postpone () "Save the draft message in the notmuch database and exit buffer." (interactive) (notmuch-message-save-draft) (kill-buffer)) (defun notmuch-message-resume (id) "Resume editing of message with id ID." (let* ((tags (process-lines notmuch-command "search" "--output=tags" "--exclude=false" id)) (draft (equal tags (notmuch-update-tags tags notmuch-message-draft-tags)))) (when (or draft (yes-or-no-p "Message does not appear to be a draft: really resume? ")) (switch-to-buffer (get-buffer-create (concat "*notmuch-draft-" id "*"))) (setq buffer-read-only nil) (erase-buffer) (let ((coding-system-for-read 'no-conversion)) (call-process notmuch-command nil t nil "show" "--format=raw" id)) (mime-to-mml) (goto-char (point-min)) (when (re-search-forward "^$" nil t) (replace-match mail-header-separator t t)) ;; Remove our added Date and Message-ID headers (unless the user has ;; explicitly customized emacs to tell us not to). (save-restriction (message-narrow-to-headers) (when (member 'Message-ID message-deletable-headers) (message-remove-header "Message-ID")) (when (member 'Date message-deletable-headers) (message-remove-header "Date"))) ;; If the message does not appear to be a draft, the postpone ;; code probably didn't write it, so it should not be unquoted. (when draft (notmuch-message-unquote-some-mml)) (notmuch-message-mode) (set-buffer-modified-p nil) ;; If the resumed message was a draft then set the draft ;; message-id so that we can delete the current saved draft if the ;; message is resaved or sent. (setq notmuch-message-draft-id (when draft id))))) (add-hook 'message-send-hook 'notmuch-message-mark-replied) (add-hook 'message-send-hook 'notmuch-message-mark-draft-deleted) (provide 'notmuch-message) ;;; notmuch-message.el ends here