config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

notmuch-mua.el (26660B)


      1 ;;; notmuch-mua.el --- emacs style mail-user-agent  -*- lexical-binding: t -*-
      2 ;;
      3 ;; Copyright © David Edmondson
      4 ;;
      5 ;; This file is part of Notmuch.
      6 ;;
      7 ;; Notmuch is free software: you can redistribute it and/or modify it
      8 ;; under the terms of the GNU General Public License as published by
      9 ;; the Free Software Foundation, either version 3 of the License, or
     10 ;; (at your option) any later version.
     11 ;;
     12 ;; Notmuch is distributed in the hope that it will be useful, but
     13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
     14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15 ;; General Public License for more details.
     16 ;;
     17 ;; You should have received a copy of the GNU General Public License
     18 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
     19 ;;
     20 ;; Authors: David Edmondson <dme@dme.org>
     21 
     22 ;;; Code:
     23 
     24 (eval-when-compile (require 'subr-x))
     25 
     26 (require 'message)
     27 (require 'gmm-utils)
     28 (require 'mm-view)
     29 (require 'format-spec)
     30 
     31 (require 'notmuch-lib)
     32 (require 'notmuch-address)
     33 (require 'notmuch-draft)
     34 (require 'notmuch-message)
     35 
     36 (declare-function notmuch-show-insert-body "notmuch-show" (msg body depth))
     37 (declare-function notmuch-fcc-header-setup "notmuch-maildir-fcc" ())
     38 (declare-function notmuch-maildir-message-do-fcc "notmuch-maildir-fcc" ())
     39 (declare-function notmuch-draft-postpone "notmuch-draft" ())
     40 (declare-function notmuch-draft-save "notmuch-draft" ())
     41 
     42 (defvar notmuch-show-indent-multipart)
     43 (defvar notmuch-show-insert-header-p-function)
     44 (defvar notmuch-show-max-text-part-size)
     45 (defvar notmuch-show-insert-text/plain-hook)
     46 
     47 ;;; Options
     48 
     49 (defcustom notmuch-mua-send-hook nil
     50   "Hook run before sending messages."
     51   :type 'hook
     52   :group 'notmuch-send
     53   :group 'notmuch-hooks)
     54 
     55 (defcustom notmuch-mua-compose-in 'current-window
     56   "Where to create the mail buffer used to compose a new message.
     57 Possible values are `current-window' (default), `new-window' and
     58 `new-frame'. If set to `current-window', the mail buffer will be
     59 displayed in the current window, so the old buffer will be
     60 restored when the mail buffer is killed. If set to `new-window'
     61 or `new-frame', the mail buffer will be displayed in a new
     62 window/frame that will be destroyed when the buffer is killed.
     63 You may want to customize `message-kill-buffer-on-exit'
     64 accordingly."
     65   :group 'notmuch-send
     66   :type '(choice (const :tag "Compose in the current window" current-window)
     67 		 (const :tag "Compose mail in a new window"  new-window)
     68 		 (const :tag "Compose mail in a new frame"   new-frame)))
     69 
     70 (defcustom notmuch-mua-user-agent-function nil
     71   "Function used to generate a `User-Agent:' string.
     72 If this is `nil' then no `User-Agent:' will be generated."
     73   :type '(choice (const :tag "No user agent string" nil)
     74 		 (const :tag "Full" notmuch-mua-user-agent-full)
     75 		 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
     76 		 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
     77 		 (function :tag "Custom user agent function"
     78 			   :value notmuch-mua-user-agent-full))
     79   :group 'notmuch-send)
     80 
     81 (defcustom notmuch-mua-hidden-headers nil
     82   "Headers that are added to the `message-mode' hidden headers list."
     83   :type '(repeat string)
     84   :group 'notmuch-send)
     85 
     86 (defcustom notmuch-identities nil
     87   "Identities that can be used as the From: address when composing a new message.
     88 
     89 If this variable is left unset, then a list will be constructed from the
     90 name and addresses configured in the notmuch configuration file."
     91   :type '(repeat string)
     92   :group 'notmuch-send)
     93 
     94 (defcustom notmuch-always-prompt-for-sender nil
     95   "Always prompt for the From: address when composing or forwarding a message.
     96 
     97 This is not taken into account when replying to a message, because in that case
     98 the From: header is already filled in by notmuch."
     99   :type 'boolean
    100   :group 'notmuch-send)
    101 
    102 (defgroup notmuch-reply nil
    103   "Replying to messages in notmuch."
    104   :group 'notmuch)
    105 
    106 (defcustom notmuch-mua-cite-function 'message-cite-original
    107   "Function for citing an original message.
    108 
    109 Predefined functions include `message-cite-original' and
    110 `message-cite-original-without-signature'.  Note that these
    111 functions use `mail-citation-hook' if that is non-nil."
    112   :type '(radio (function-item message-cite-original)
    113 		(function-item message-cite-original-without-signature)
    114 		(function-item sc-cite-original)
    115 		(function :tag "Other"))
    116   :link '(custom-manual "(message)Insertion Variables")
    117   :group 'notmuch-reply)
    118 
    119 (defcustom notmuch-mua-reply-insert-header-p-function
    120   'notmuch-show-reply-insert-header-p-never
    121   "Function to decide which parts get a header when replying.
    122 
    123 This function specifies which parts of a mime message with
    124 multiple parts get a header."
    125   :type '(radio (const :tag "No part headers"
    126 		       notmuch-show-reply-insert-header-p-never)
    127 		(const :tag "All except multipart/* and hidden parts"
    128 		       notmuch-show-reply-insert-header-p-trimmed)
    129 		(const :tag "Only for included text parts"
    130 		       notmuch-show-reply-insert-header-p-minimal)
    131 		(const :tag "Exactly as in show view"
    132 		       notmuch-show-insert-header-p)
    133 		(function :tag "Other"))
    134   :group 'notmuch-reply)
    135 
    136 (defcustom notmuch-mua-attachment-regexp
    137   "\\b\\(attache\?ment\\|attached\\|attach\\|pi[èe]ce\s+jointe?\\)\\b"
    138   "Message body text indicating that an attachment is expected.
    139 
    140 This is not used unless `notmuch-mua-attachment-check' is added
    141 to `notmuch-mua-send-hook'."
    142   :type 'regexp
    143   :group 'notmuch-send)
    144 
    145 (defcustom notmuch-mua-subject-regexp
    146   "[[:blank:]]*$"
    147   "Message subject indicating that something may be amiss.
    148 By default, this checks for empty subject lines.
    149 
    150 This is not used unless `notmuch-mua-subject-check' is added to
    151 `notmuch-mua-send-hook'."
    152   :type 'regexp
    153   :group 'notmuch-send)
    154 
    155 ;;; Various functions
    156 
    157 (defun notmuch-mua-attachment-check ()
    158   "Signal an error an attachement is expected but missing.
    159 
    160 Signal an error if the message text indicates that an attachment
    161 is expected but no MML referencing an attachment is found.
    162 
    163 Typically this is added to `notmuch-mua-send-hook'."
    164   (when (and
    165 	 ;; When the message mentions attachment...
    166 	 (save-excursion
    167 	   (message-goto-body)
    168 	   ;; Limit search from reaching other possible parts of the message
    169 	   (let ((search-limit (search-forward "\n<#" nil t)))
    170 	     (message-goto-body)
    171 	     (cl-loop while (re-search-forward notmuch-mua-attachment-regexp
    172 					       search-limit t)
    173 		      ;; For every instance of the "attachment" string
    174 		      ;; found, examine the text properties. If the text
    175 		      ;; has either a `face' or `syntax-table' property
    176 		      ;; then it is quoted text and should *not* cause the
    177 		      ;; user to be asked about a missing attachment.
    178 		      if (let ((props (text-properties-at (match-beginning 0))))
    179 			   (not (or (memq 'syntax-table props)
    180 				    (memq 'face props))))
    181 		      return t
    182 		      finally return nil)))
    183 	 ;; ...but doesn't have a part with a filename...
    184 	 (save-excursion
    185 	   (message-goto-body)
    186 	   (not (re-search-forward "^<#part [^>]*filename=" nil t)))
    187 	 ;; ...and that's not okay...
    188 	 (not (y-or-n-p "Attachment mentioned, but no attachment - is that okay?")))
    189     ;; ...signal an error.
    190     (error "Missing attachment")))
    191 
    192 (defun notmuch-mua-subject-check ()
    193   "Signal an error if the subject seems amiss.
    194 More precisely, if the subject conforms to
    195 `notmuch-mua-subject-regexp'.
    196 
    197 Typically this is added to `notmuch-mua-send-hook'."
    198   (or (save-excursion
    199         (message-goto-subject)
    200         (message-beginning-of-header t)
    201         (not (looking-at-p notmuch-mua-subject-regexp)))
    202       (y-or-n-p "Subject may be erroneous – is that okay?")
    203       (error "Erroneous subject")))
    204 
    205 (defun notmuch-mua-get-switch-function ()
    206   "Get a switch function according to `notmuch-mua-compose-in'."
    207   (pcase notmuch-mua-compose-in
    208     ('current-window 'switch-to-buffer)
    209     ('new-window     'switch-to-buffer-other-window)
    210     ('new-frame      'switch-to-buffer-other-frame)
    211     (_ (error "Invalid value for `notmuch-mua-compose-in'"))))
    212 
    213 (defun notmuch-mua-maybe-set-window-dedicated ()
    214   "Set the selected window as dedicated according to `notmuch-mua-compose-in'."
    215   (when (or (eq notmuch-mua-compose-in 'new-frame)
    216 	    (eq notmuch-mua-compose-in 'new-window))
    217     (set-window-dedicated-p (selected-window) t)))
    218 
    219 (defun notmuch-mua-user-agent-full ()
    220   "Generate a `User-Agent:' string suitable for notmuch."
    221   (concat (notmuch-mua-user-agent-notmuch)
    222 	  " "
    223 	  (notmuch-mua-user-agent-emacs)))
    224 
    225 (defun notmuch-mua-user-agent-notmuch ()
    226   "Generate a `User-Agent:' string suitable for notmuch."
    227   (let ((notmuch-version (if (string= notmuch-emacs-version "unknown")
    228 			     (notmuch-cli-version)
    229 			   notmuch-emacs-version)))
    230     (concat "Notmuch/" notmuch-version " (https://notmuchmail.org)")))
    231 
    232 (defun notmuch-mua-user-agent-emacs ()
    233   "Generate a `User-Agent:' string suitable for notmuch."
    234   (concat "Emacs/" emacs-version " (" system-configuration ")"))
    235 
    236 (defun notmuch-mua-add-more-hidden-headers ()
    237   "Add some headers to the list that are hidden by default."
    238   (mapc (lambda (header)
    239 	  (unless (member header message-hidden-headers)
    240 	    (push header message-hidden-headers)))
    241 	notmuch-mua-hidden-headers))
    242 
    243 (defun notmuch-mua-reply-crypto (parts)
    244   "Add mml sign-encrypt flag if any part of original message is encrypted."
    245   (cl-loop for part in parts
    246 	   for type = (plist-get part :content-type)
    247 	   if (notmuch-match-content-type type "multipart/encrypted")
    248 	   do (mml-secure-message-sign-encrypt)
    249 	   else if (notmuch-match-content-type type "multipart/*")
    250 	   do (notmuch-mua-reply-crypto (plist-get part :content))))
    251 
    252 ;; There is a bug in Emacs' message.el that results in a newline
    253 ;; not being inserted after the References header, so the next header
    254 ;; is concatenated to the end of it. This function fixes the problem,
    255 ;; while guarding against the possibility that some current or future
    256 ;; version of emacs has the bug fixed.
    257 (defun notmuch-mua-insert-references (original-func header references)
    258   (funcall original-func header references)
    259   (unless (bolp) (insert "\n")))
    260 
    261 ;;; Mua reply
    262 
    263 (defun notmuch-mua-reply (query-string &optional sender reply-all duplicate)
    264   (let* ((duparg (and duplicate (list (format "--duplicate=%d" duplicate))))
    265 	 (args `("reply" "--format=sexp" "--format-version=5" ,@duparg))
    266 	 (process-crypto notmuch-show-process-crypto)
    267 	 reply
    268 	 original)
    269     (when process-crypto
    270       (setq args (append args '("--decrypt=true"))))
    271     (if reply-all
    272 	(setq args (append args '("--reply-to=all")))
    273       (setq args (append args '("--reply-to=sender"))))
    274     (setq args (append args (list query-string)))
    275     ;; Get the reply object as SEXP, and parse it into an elisp object.
    276     (setq reply (apply #'notmuch-call-notmuch-sexp args))
    277     ;; Extract the original message to simplify the following code.
    278     (setq original (plist-get reply :original))
    279     ;; Extract the headers of both the reply and the original message.
    280     (let* ((original-headers (plist-get original :headers))
    281 	   (reply-headers (plist-get reply :reply-headers)))
    282       ;; If sender is non-nil, set the From: header to its value.
    283       (when sender
    284 	(plist-put reply-headers :From sender))
    285       (let
    286 	  ;; Overlay the composition window on that being used to read
    287 	  ;; the original message.
    288 	  ((same-window-regexps '("\\*mail .*")))
    289 	;; We modify message-header-format-alist to get around
    290 	;; a bug in message.el.  See the comment above on
    291 	;; notmuch-mua-insert-references.
    292 	(let ((message-header-format-alist
    293 	       (cl-loop for pair in message-header-format-alist
    294 			if (eq (car pair) 'References)
    295 			collect (cons 'References
    296 				      (apply-partially
    297 				       'notmuch-mua-insert-references
    298 				       (cdr pair)))
    299 			else
    300 			collect pair)))
    301 	  (notmuch-mua-mail (plist-get reply-headers :To)
    302 			    (notmuch-sanitize (plist-get reply-headers :Subject))
    303 			    (notmuch-headers-plist-to-alist reply-headers)
    304 			    nil (notmuch-mua-get-switch-function))))
    305       ;; Create a buffer-local queue for tag changes triggered when
    306       ;; sending the reply.
    307       (when notmuch-message-replied-tags
    308 	(setq notmuch-message-queued-tag-changes
    309 	      (list (cons query-string notmuch-message-replied-tags))))
    310       ;; Insert the message body - but put it in front of the signature
    311       ;; if one is present, and after any other content
    312       ;; message*setup-hooks may have added to the message body already.
    313       (save-restriction
    314 	(message-goto-body)
    315 	(narrow-to-region (point) (point-max))
    316 	(goto-char (point-max))
    317 	(if (re-search-backward message-signature-separator nil t)
    318 	    (when message-signature-insert-empty-line
    319 	      (forward-line -1))
    320 	  (goto-char (point-max))))
    321       (let ((from (plist-get original-headers :From))
    322 	    (date (plist-get original-headers :Date))
    323 	    (start (point)))
    324 	;; notmuch-mua-cite-function constructs a citation line based
    325 	;; on the From and Date headers of the original message, which
    326 	;; are assumed to be in the buffer.
    327 	(insert "From: " from "\n")
    328 	(insert "Date: " date "\n\n")
    329 	(insert
    330 	 (with-temp-buffer
    331 	   (let
    332 	       ;; Don't attempt to clean up messages, excerpt
    333 	       ;; citations, etc. in the original message before
    334 	       ;; quoting.
    335 	       ((notmuch-show-insert-text/plain-hook nil)
    336 		;; Don't omit long parts.
    337 		(notmuch-show-max-text-part-size 0)
    338 		;; Insert headers for parts as appropriate for replying.
    339 		(notmuch-show-insert-header-p-function
    340 		 notmuch-mua-reply-insert-header-p-function)
    341 		;; Ensure that any encrypted parts are
    342 		;; decrypted during the generation of the reply
    343 		;; text.
    344 		(notmuch-show-process-crypto process-crypto)
    345 		;; Don't indent multipart sub-parts.
    346 		(notmuch-show-indent-multipart nil)
    347 		;; Stop certain mime types from being inlined
    348 		(mm-inline-override-types (notmuch--inline-override-types)))
    349 	     ;; We don't want sigstatus buttons (an information leak and usually wrong anyway).
    350 	     (cl-letf (((symbol-function 'notmuch-crypto-insert-sigstatus-button) #'ignore)
    351 		       ((symbol-function 'notmuch-crypto-insert-encstatus-button) #'ignore))
    352 	       (notmuch-show-insert-body original (plist-get original :body) 0)
    353 	       (buffer-substring-no-properties (point-min) (point-max))))))
    354 	(set-mark (point))
    355 	(goto-char start)
    356 	;; Quote the original message according to the user's configured style.
    357 	(funcall notmuch-mua-cite-function)))
    358     ;; Crypto processing based crypto content of the original message
    359     (when process-crypto
    360       (notmuch-mua-reply-crypto (plist-get original :body))))
    361   ;; Push mark right before signature, if any.
    362   (message-goto-signature)
    363   (unless (eobp)
    364     (end-of-line -1))
    365   (push-mark)
    366   (message-goto-body)
    367   (set-buffer-modified-p nil))
    368 
    369 ;;; Mode and keymap
    370 
    371 (defvar notmuch-message-mode-map
    372   (let ((map (make-sparse-keymap)))
    373     (define-key map [remap message-send-and-exit] #'notmuch-mua-send-and-exit)
    374     (define-key map [remap message-send] #'notmuch-mua-send)
    375     (define-key map (kbd "C-c C-p") #'notmuch-draft-postpone)
    376     (define-key map (kbd "C-x C-s") #'notmuch-draft-save)
    377     map)
    378   "Keymap for `notmuch-message-mode'.")
    379 
    380 (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"
    381   "Notmuch message composition mode. Mostly like `message-mode'."
    382   (notmuch-address-setup))
    383 
    384 (put 'notmuch-message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
    385 
    386 ;;; New messages
    387 
    388 (defun notmuch-mua-pop-to-buffer (name switch-function)
    389   "Pop to buffer NAME, and warn if it already exists and is modified.
    390 Like `message-pop-to-buffer' but enable `notmuch-message-mode'
    391 instead of `message-mode' and SWITCH-FUNCTION is mandatory."
    392   (let ((buffer (get-buffer name)))
    393     (if (and buffer
    394 	     (buffer-name buffer))
    395 	(let ((window (get-buffer-window buffer 0)))
    396 	  (if window
    397 	      ;; Raise the frame already displaying the message buffer.
    398 	      (progn
    399 		(select-frame-set-input-focus (window-frame window))
    400 		(select-window window))
    401 	    (funcall switch-function buffer)
    402 	    (set-buffer buffer))
    403 	  (when (buffer-modified-p)
    404 	    (if (y-or-n-p "Message already being composed; erase? ")
    405 		(message nil)
    406 	      (error "Message being composed"))))
    407       (funcall switch-function name)
    408       (set-buffer name))
    409     (erase-buffer)
    410     (notmuch-message-mode)))
    411 
    412 (defun notmuch-mua--remove-dont-reply-to-names ()
    413   (when-let* ((nr (if (functionp message-dont-reply-to-names)
    414 		      message-dont-reply-to-names
    415 		    (gmm-regexp-concat message-dont-reply-to-names)))
    416 	      (nr-filter
    417 	       (if (functionp nr)
    418 		   (lambda (mail) (and (not (funcall nr mail)) mail))
    419 		 (lambda (mail) (and (not (string-match-p nr mail)) mail)))))
    420     (dolist (header '("To" "Cc"))
    421       (when-let ((v (message-fetch-field header)))
    422 	(let* ((tokens (mapcar #'string-trim (message-tokenize-header v)))
    423 	       (good-tokens (delq nil (mapcar nr-filter tokens)))
    424 	       (addr (and good-tokens (mapconcat #'identity good-tokens ", "))))
    425 	  (message-replace-header header addr))))))
    426 
    427 ;;;###autoload
    428 (defun notmuch-mua-mail (&optional to subject other-headers _continue
    429 				   switch-function yank-action send-actions
    430 				   return-action &rest _ignored)
    431   "Invoke the notmuch mail composition window.
    432 
    433 The position of point when the function returns differs depending
    434 on the values of TO and SUBJECT.  If both are non-nil, point is
    435 moved to the message's body.  If SUBJECT is nil but TO isn't,
    436 point is moved to the \"Subject:\" header.  Otherwise, point is
    437 moved to the \"To:\" header."
    438   (interactive)
    439   (when notmuch-mua-user-agent-function
    440     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
    441       (unless (string-empty-p user-agent)
    442 	(push (cons 'User-Agent user-agent) other-headers))))
    443   (notmuch-mua-pop-to-buffer (message-buffer-name "mail" to)
    444 			     (or switch-function
    445 				 (notmuch-mua-get-switch-function)))
    446   (let ((headers
    447 	 (append
    448 	  ;; The following is copied from `message-mail'
    449 	  `((To . ,(or to "")) (Subject . ,(or subject "")))
    450 	  ;; C-h f compose-mail says that headers should be specified as
    451 	  ;; (string . value); however all the rest of message expects
    452 	  ;; headers to be symbols, not strings (eg message-header-format-alist).
    453 	  ;; https://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
    454 	  ;; We need to convert any string input, eg from rmail-start-mail.
    455 	  (dolist (h other-headers other-headers)
    456 	    (when (stringp (car h))
    457 	      (setcar h (intern (capitalize (car h))))))))
    458 	;; Cause `message-setup-1' to do things relevant for mail,
    459 	;; such as observe `message-default-mail-headers'.
    460 	(message-this-is-mail t))
    461     (unless (assq 'From headers)
    462       (push (cons 'From (message-make-from
    463 			 (notmuch-user-name)
    464 			 (notmuch-user-primary-email)))
    465 	    headers))
    466     (message-setup-1 headers yank-action send-actions return-action))
    467   (notmuch-fcc-header-setup)
    468   (notmuch-mua--remove-dont-reply-to-names)
    469   (message-sort-headers)
    470   (message-hide-headers)
    471   (set-buffer-modified-p nil)
    472   (notmuch-mua-maybe-set-window-dedicated)
    473   (cond
    474    ((and to subject) (message-goto-body))
    475    (to (message-goto-subject))
    476    (t (message-goto-to))))
    477 
    478 (defvar notmuch-mua-sender-history nil)
    479 
    480 (defun notmuch-mua-prompt-for-sender ()
    481   "Prompt for a sender from the user's configured identities."
    482   (if notmuch-identities
    483       (completing-read "Send mail from: " notmuch-identities
    484 		       nil nil nil 'notmuch-mua-sender-history
    485 		       (car notmuch-identities))
    486     (let* ((name (notmuch-user-name))
    487 	   (addrs (cons (notmuch-user-primary-email)
    488 			(notmuch-user-other-email)))
    489 	   (address
    490 	    (completing-read (concat "Sender address for " name ": ") addrs
    491 			     nil nil nil 'notmuch-mua-sender-history
    492 			     (car addrs))))
    493       (message-make-from name address))))
    494 
    495 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
    496 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
    497   "Compose new mail.
    498 
    499 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
    500 the From: address first."
    501   (interactive "P")
    502   (let ((other-headers
    503 	 (and (or prompt-for-sender notmuch-always-prompt-for-sender)
    504 	      (list (cons 'From (notmuch-mua-prompt-for-sender))))))
    505     (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
    506 
    507 (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
    508   "Compose a new message forwarding MESSAGES.
    509 
    510 If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
    511 the From: address."
    512   (let* ((other-headers
    513 	  (and (or prompt-for-sender notmuch-always-prompt-for-sender)
    514 	       (list (cons 'From (notmuch-mua-prompt-for-sender)))))
    515 	 ;; Comes from the first message and is applied later.
    516 	 forward-subject
    517 	 ;; List of accumulated message-references of forwarded messages.
    518 	 forward-references
    519 	 ;; List of corresponding message-query.
    520 	 forward-queries)
    521     ;; Generate the template for the outgoing message.
    522     (notmuch-mua-mail nil "" other-headers nil (notmuch-mua-get-switch-function))
    523     (save-excursion
    524       ;; Insert all of the forwarded messages.
    525       (mapc (lambda (id)
    526 	      (let ((temp-buffer (get-buffer-create
    527 				  (concat "*notmuch-fwd-raw-" id "*"))))
    528 		;; Get the raw version of this message in the buffer.
    529 		(with-current-buffer temp-buffer
    530 		  (erase-buffer)
    531 		  (let ((coding-system-for-read 'no-conversion))
    532 		    (notmuch--call-process notmuch-command nil t nil
    533 				  "show" "--format=raw" id))
    534 		  ;; Because we process the messages in reverse order,
    535 		  ;; always generate a forwarded subject, then use the
    536 		  ;; last (i.e. first) one.
    537 		  (setq forward-subject (message-make-forward-subject))
    538 		  (push (message-fetch-field "Message-ID") forward-references)
    539 		  (push id forward-queries))
    540 		;; Make a copy ready to be forwarded in the
    541 		;; composition buffer.
    542 		(message-forward-make-body temp-buffer)
    543 		;; Kill the temporary buffer.
    544 		(kill-buffer temp-buffer)))
    545 	    ;; `message-forward-make-body' always puts the message at
    546 	    ;; the top, so do them in reverse order.
    547 	    (reverse messages))
    548       ;; Add in the appropriate subject.
    549       (save-restriction
    550 	(message-narrow-to-headers)
    551 	(message-remove-header "Subject")
    552 	(message-add-header (concat "Subject: " forward-subject))
    553 	(message-remove-header "References")
    554 	(message-add-header (concat "References: "
    555 				    (mapconcat 'identity forward-references " "))))
    556       ;; Create a buffer-local queue for tag changes triggered when
    557       ;; sending the message.
    558       (when notmuch-message-forwarded-tags
    559 	(setq notmuch-message-queued-tag-changes
    560 	      (cl-loop for id in forward-queries
    561 		       collect
    562 		       (cons id notmuch-message-forwarded-tags))))
    563       ;; `message-forward-make-body' shows the User-agent header.  Hide
    564       ;; it again.
    565       (message-hide-headers)
    566       (set-buffer-modified-p nil))))
    567 
    568 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all duplicate)
    569   "Compose a reply to the message identified by QUERY-STRING.
    570 
    571 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
    572 the From: address first.  If REPLY-ALL is non-nil, the message
    573 will be addressed to all recipients of the source message.  If
    574 DUPLICATE is non-nil, based the reply on that duplicate file"
    575   ;; `select-active-regions' is t by default. The reply insertion code
    576   ;; sets the region to the quoted message to make it easy to delete
    577   ;; (kill-region or C-w). These two things combine to put the quoted
    578   ;; message in the primary selection.
    579   ;;
    580   ;; This is not what the user wanted and is a privacy risk (accidental
    581   ;; pasting of the quoted message). We can avoid some of the problems
    582   ;; by let-binding select-active-regions to nil. This fixes if the
    583   ;; primary selection was previously in a non-emacs window but not if
    584   ;; it was in an emacs window. To avoid the problem in the latter case
    585   ;; we deactivate mark.
    586   (let ((sender (and prompt-for-sender
    587 		     (notmuch-mua-prompt-for-sender)))
    588 	(select-active-regions nil))
    589     (notmuch-mua-reply query-string sender reply-all duplicate)
    590     (deactivate-mark)))
    591 
    592 ;;; Checks
    593 
    594 (defun notmuch-mua-check-no-misplaced-secure-tag ()
    595   "Query user if there is a misplaced secure mml tag.
    596 
    597 Emacs message-send will (probably) ignore a secure mml tag unless
    598 it is at the start of the body. Returns t if there is no such
    599 tag, or the user confirms they mean it."
    600   (save-excursion
    601     (let ((body-start (progn (message-goto-body) (point))))
    602       (goto-char (point-max))
    603       (or
    604        ;; We are always fine if there is no secure tag.
    605        (not (search-backward "<#secure" nil t))
    606        ;; There is a secure tag, so it must be at the start of the
    607        ;; body, with no secure tag earlier (i.e., in the headers).
    608        (and (= (point) body-start)
    609 	    (not (search-backward "<#secure" nil t)))
    610        ;; The user confirms they means it.
    611        (yes-or-no-p "\
    612 There is a <#secure> tag not at the start of the body. It is
    613 likely that the message will be sent unsigned and unencrypted.
    614 Really send? ")))))
    615 
    616 (defun notmuch-mua-check-secure-tag-has-newline ()
    617   "Query if the secure mml tag has a newline following it.
    618 
    619 Emacs message-send will (probably) ignore a correctly placed
    620 secure mml tag unless it is followed by a newline. Returns t if
    621 any secure tag is followed by a newline, or the user confirms
    622 they mean it."
    623   (save-excursion
    624     (message-goto-body)
    625     (or
    626      ;; There is no (correctly placed) secure tag.
    627      (not (looking-at "<#secure"))
    628      ;; The secure tag is followed by a newline.
    629      (looking-at "<#secure[^\n>]*>\n")
    630      ;; The user confirms they means it.
    631      (yes-or-no-p "\
    632 The <#secure> tag at the start of the body is not followed by a
    633 newline. It is likely that the message will be sent unsigned and
    634 unencrypted.  Really send? "))))
    635 
    636 ;;; Finishing commands
    637 
    638 (defun notmuch-mua-send-common (arg &optional exit)
    639   (interactive "P")
    640   (run-hooks 'notmuch-mua-send-hook)
    641   (when (and (notmuch-mua-check-no-misplaced-secure-tag)
    642 	     (notmuch-mua-check-secure-tag-has-newline))
    643     (cl-letf (((symbol-function 'message-do-fcc)
    644 	       #'notmuch-maildir-message-do-fcc))
    645       (if exit
    646 	  (message-send-and-exit arg)
    647 	(message-send arg)))))
    648 
    649 ;;;###autoload
    650 (defun notmuch-mua-send-and-exit (&optional arg)
    651   (interactive "P")
    652   (notmuch-mua-send-common arg t))
    653 
    654 ;;;###autoload
    655 (defun notmuch-mua-send (&optional arg)
    656   (interactive "P")
    657   (notmuch-mua-send-common arg))
    658 
    659 ;;;###autoload
    660 (defun notmuch-mua-kill-buffer ()
    661   (interactive)
    662   (message-kill-buffer))
    663 
    664 ;;; _
    665 
    666 ;;;###autoload
    667 (define-mail-user-agent 'notmuch-user-agent
    668   'notmuch-mua-mail
    669   'notmuch-mua-send-and-exit
    670   'notmuch-mua-kill-buffer
    671   'notmuch-mua-send-hook)
    672 
    673 ;; Add some more headers to the list that `message-mode' hides when
    674 ;; composing a message.
    675 (notmuch-mua-add-more-hidden-headers)
    676 
    677 (provide 'notmuch-mua)
    678 
    679 ;;; notmuch-mua.el ends here