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