2020-02-11 12:15:10 +01:00
|
|
|
|
;;; mu4e-draft.el -- part of mu4e, the mu mail user agent for emacs -*- lexical-binding: t -*-
|
2012-10-31 19:33:46 +01:00
|
|
|
|
;;
|
2020-02-06 19:28:24 +01:00
|
|
|
|
;; Copyright (C) 2011-2020 Dirk-Jan C. Binnema
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
;; Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
|
;; Maintainer: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
2020-02-11 12:23:40 +01:00
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
;; GNU Emacs 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.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; In this file, various functions to create draft messages
|
|
|
|
|
|
2019-05-19 21:04:53 +02:00
|
|
|
|
;;; Code:
|
|
|
|
|
|
2018-09-18 02:53:27 +02:00
|
|
|
|
(require 'cl-lib)
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(require 'mu4e-vars)
|
|
|
|
|
(require 'mu4e-utils)
|
2013-06-03 20:09:20 +02:00
|
|
|
|
(require 'mu4e-message)
|
2013-10-20 14:41:47 +02:00
|
|
|
|
(require 'message) ;; mail-header-separator
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
2020-02-11 15:10:35 +01:00
|
|
|
|
;;; Options
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
2013-04-23 18:54:47 +02:00
|
|
|
|
(defcustom mu4e-compose-dont-reply-to-self nil
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"If non-nil, don't include self.
|
2020-02-06 19:28:24 +01:00
|
|
|
|
\(that is, member of `(mu4e-personal-addresses)') in replies."
|
2013-04-23 18:54:47 +02:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'mu4e-compose)
|
|
|
|
|
|
2013-10-20 14:41:47 +02:00
|
|
|
|
(defcustom mu4e-compose-cite-function
|
|
|
|
|
(or message-cite-function 'message-cite-original-without-signature)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"The function for citing message in replies and forwards.
|
|
|
|
|
This is the mu4e-specific version of
|
2013-10-20 14:41:47 +02:00
|
|
|
|
`message-cite-function'."
|
|
|
|
|
:type 'function
|
|
|
|
|
:group 'mu4e-compose)
|
|
|
|
|
|
|
|
|
|
(defcustom mu4e-compose-signature
|
2015-10-16 10:20:48 +02:00
|
|
|
|
(or message-signature "Sent with my mu4e")
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"The message signature.
|
|
|
|
|
\(i.e. the blob at the bottom of messages). This is the
|
|
|
|
|
mu4e-specific version of `message-signature'."
|
2020-01-11 14:10:37 +01:00
|
|
|
|
:type '(choice string
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(const :tag "None" nil)
|
|
|
|
|
(const :tag "Contents of signature file" t)
|
|
|
|
|
function sexp)
|
2020-01-11 14:10:37 +01:00
|
|
|
|
:risky t
|
2013-10-20 14:41:47 +02:00
|
|
|
|
:group 'mu4e-compose)
|
|
|
|
|
|
2013-12-21 19:58:55 +01:00
|
|
|
|
(defcustom mu4e-compose-signature-auto-include t
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Whether to automatically include a message-signature."
|
2013-12-21 19:58:55 +01:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'mu4e-compose)
|
|
|
|
|
|
2019-10-20 15:12:11 +02:00
|
|
|
|
(make-obsolete-variable 'mu4e-compose-auto-include-date
|
2020-02-11 12:00:46 +01:00
|
|
|
|
"This is done unconditionally now" "1.3.5")
|
2014-09-02 15:10:38 +02:00
|
|
|
|
|
2014-09-24 16:06:26 +02:00
|
|
|
|
(defcustom mu4e-compose-in-new-frame nil
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Whether to compose messages in a new frame."
|
2014-09-24 16:06:26 +02:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'mu4e-compose)
|
|
|
|
|
|
2015-11-22 10:25:05 +01:00
|
|
|
|
(defvar mu4e-user-agent-string
|
|
|
|
|
(format "mu4e %s; emacs %s" mu4e-mu-version emacs-version)
|
2017-03-09 00:27:44 +01:00
|
|
|
|
"The User-Agent string for mu4e, or nil.")
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
2019-09-16 19:54:59 +02:00
|
|
|
|
(defvar mu4e-view-date-format)
|
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(defun mu4e~draft-cite-original (msg)
|
2012-11-10 14:01:17 +01:00
|
|
|
|
"Return a cited version of the original message MSG as a plist.
|
2016-01-15 20:02:35 +01:00
|
|
|
|
This function uses `mu4e-compose-cite-function', and as such all
|
|
|
|
|
its settings apply."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(when (fboundp 'mu4e-view-message-text) ;; keep bytecompiler happy
|
2014-03-09 19:08:55 +01:00
|
|
|
|
(let ((mu4e-view-date-format "%Y-%m-%dT%T%z"))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(insert (mu4e-view-message-text msg)))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(message-yank-original)
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(push-mark (point-max))
|
2013-10-20 14:41:47 +02:00
|
|
|
|
;; set the the signature separator to 'loose', since in the real world,
|
|
|
|
|
;; many message don't follow the standard...
|
2014-03-03 18:54:36 +01:00
|
|
|
|
(let ((message-signature-separator "^-- *$")
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(message-signature-insert-empty-line t))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(funcall mu4e-compose-cite-function))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(pop-mark)
|
2013-10-20 14:41:47 +02:00
|
|
|
|
(goto-char (point-min))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(buffer-string))))
|
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-header (hdr val)
|
2012-11-10 14:01:17 +01:00
|
|
|
|
"Return a header line of the form \"HDR: VAL\".
|
|
|
|
|
If VAL is nil, return nil."
|
2013-10-19 11:07:37 +02:00
|
|
|
|
;; note: the propertize here is currently useless, since gnus sets its own
|
|
|
|
|
;; later.
|
|
|
|
|
(when val (format "%s: %s\n"
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(propertize hdr 'face 'mu4e-header-key-face)
|
|
|
|
|
(propertize val 'face 'mu4e-header-value-face))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
2016-06-05 17:49:04 +02:00
|
|
|
|
(defconst mu4e~max-reference-num 21
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Specifies the maximum number of References:.
|
|
|
|
|
As suggested by `message-shorten-references'.")
|
2016-06-05 17:49:04 +02:00
|
|
|
|
|
|
|
|
|
(defun mu4e~shorten-1 (list cut surplus)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Cut SURPLUS elements out of LIST.
|
|
|
|
|
Beginning with CUTth
|
2016-06-05 17:49:04 +02:00
|
|
|
|
one. Code borrowed from `message-shorten-1'."
|
|
|
|
|
(setcdr (nthcdr (- cut 2) list)
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(nthcdr (+ (- cut 2) surplus 1) list)))
|
2016-06-05 17:49:04 +02:00
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(defun mu4e~draft-references-construct (msg)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Construct the value of the References: header based on MSG.
|
|
|
|
|
This assumes a comma-separated string. Normally, this the concatenation of the
|
2012-12-28 13:48:20 +01:00
|
|
|
|
existing References + In-Reply-To (which may be empty, an note
|
|
|
|
|
that :references includes the old in-reply-to as well) and the
|
|
|
|
|
message-id. If the message-id is empty, returns the old
|
|
|
|
|
References. If both are empty, return nil."
|
2012-12-28 11:49:31 +01:00
|
|
|
|
(let* ( ;; these are the ones from the message being replied to / forwarded
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(refs (mu4e-message-field msg :references))
|
|
|
|
|
(msgid (mu4e-message-field msg :message-id))
|
|
|
|
|
;; now, append in
|
|
|
|
|
(refs (if (and msgid (not (string= msgid "")))
|
|
|
|
|
(append refs (list msgid)) refs))
|
|
|
|
|
;; no doubles
|
|
|
|
|
(refs (cl-delete-duplicates refs :test #'equal))
|
|
|
|
|
(refnum (length refs))
|
|
|
|
|
(cut 2))
|
2016-06-05 17:49:04 +02:00
|
|
|
|
;; remove some refs when there are too many
|
|
|
|
|
(when (> refnum mu4e~max-reference-num)
|
|
|
|
|
(let ((surplus (- refnum mu4e~max-reference-num)))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(mu4e~shorten-1 refs cut surplus)))
|
2016-06-21 19:43:59 +02:00
|
|
|
|
(mapconcat (lambda (id) (format "<%s>" id)) refs " ")))
|
2016-06-05 17:49:04 +02:00
|
|
|
|
|
|
|
|
|
|
2020-02-11 15:10:35 +01:00
|
|
|
|
;;; Determine the recipient fields for new messages
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-recipients-list-to-string (lst)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Convert a lst LST of address cells into a string.
|
|
|
|
|
This is specified as a comma-separated list of e-mail addresses.
|
|
|
|
|
If LST is nil, returns nil."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(when lst
|
|
|
|
|
(mapconcat
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(lambda (addrcell)
|
|
|
|
|
(let ((name (car addrcell))
|
|
|
|
|
(email (cdr addrcell)))
|
|
|
|
|
(if name
|
|
|
|
|
(format "%s <%s>" (mu4e~rfc822-quoteit name) email)
|
|
|
|
|
(format "%s" email))))
|
|
|
|
|
lst ", ")))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-address-cell-equal (cell1 cell2)
|
2012-11-10 14:01:17 +01:00
|
|
|
|
"Return t if CELL1 and CELL2 have the same e-mail address.
|
|
|
|
|
The comparison is done case-insensitively. If the cells done
|
|
|
|
|
match return nil. CELL1 and CELL2 are cons cells of the
|
|
|
|
|
form (NAME . EMAIL)."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(string=
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(downcase (or (cdr cell1) ""))
|
|
|
|
|
(downcase (or (cdr cell2) ""))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-create-to-lst (origmsg)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Create a list of address for the To: in a new message.
|
|
|
|
|
This is based on the original message ORIGMSG. If the Reply-To
|
|
|
|
|
address is set, use that, otherwise use the From address. Note,
|
|
|
|
|
whatever was in the To: field before, goes to the Cc:-list (if
|
|
|
|
|
we're doing a reply-to-all). Special case: if we were the sender
|
|
|
|
|
of the original, we simple copy the list form the original."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(let ((reply-to
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(or (plist-get origmsg :reply-to) (plist-get origmsg :from))))
|
2018-09-18 02:53:27 +02:00
|
|
|
|
(cl-delete-duplicates reply-to :test #'mu4e~draft-address-cell-equal)
|
2013-04-23 18:54:47 +02:00
|
|
|
|
(if mu4e-compose-dont-reply-to-self
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(cl-delete-if
|
|
|
|
|
(lambda (to-cell)
|
|
|
|
|
(cl-member-if
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(lambda (addr)
|
|
|
|
|
(string= (downcase addr) (downcase (cdr to-cell))))
|
2020-02-06 19:28:24 +01:00
|
|
|
|
(mu4e-personal-addresses)))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
reply-to)
|
2013-04-23 18:54:47 +02:00
|
|
|
|
reply-to)))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
|
2017-02-20 00:37:44 +01:00
|
|
|
|
(defun mu4e~strip-ignored-addresses (addrs)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Return all addresses that are not to be ignored.
|
|
|
|
|
I.e. return all the addresses in ADDRS not matching
|
2017-02-20 00:37:44 +01:00
|
|
|
|
`mu4e-compose-reply-ignore-address'."
|
|
|
|
|
(cond
|
2020-02-11 12:00:46 +01:00
|
|
|
|
((null mu4e-compose-reply-ignore-address)
|
|
|
|
|
addrs)
|
|
|
|
|
((functionp mu4e-compose-reply-ignore-address)
|
|
|
|
|
(cl-remove-if
|
|
|
|
|
(lambda (elt)
|
|
|
|
|
(funcall mu4e-compose-reply-ignore-address (cdr elt)))
|
|
|
|
|
addrs))
|
|
|
|
|
(t
|
|
|
|
|
;; regexp or list of regexps
|
|
|
|
|
(let* ((regexp mu4e-compose-reply-ignore-address)
|
|
|
|
|
(regexp (if (listp regexp)
|
|
|
|
|
(mapconcat (lambda (elt) (concat "\\(" elt "\\)"))
|
|
|
|
|
regexp "\\|")
|
|
|
|
|
regexp)))
|
2018-09-18 02:53:27 +02:00
|
|
|
|
(cl-remove-if
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(lambda (elt)
|
|
|
|
|
(string-match regexp (cdr elt)))
|
|
|
|
|
addrs)))))
|
2017-02-20 00:37:44 +01:00
|
|
|
|
|
2020-01-25 21:21:56 +01:00
|
|
|
|
(defun mu4e~draft-create-cc-lst (origmsg &optional reply-all include-from)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Create a list of address for the Cc: in a new message.
|
|
|
|
|
This is based on the original message ORIGMSG, and whether it's a
|
|
|
|
|
REPLY-ALL."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(when reply-all
|
|
|
|
|
(let* ((cc-lst ;; get the cc-field from the original, remove dups
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(cl-delete-duplicates
|
|
|
|
|
(append
|
|
|
|
|
(plist-get origmsg :to)
|
|
|
|
|
(plist-get origmsg :cc)
|
|
|
|
|
(when include-from(plist-get origmsg :from))
|
|
|
|
|
(plist-get origmsg :list-post))
|
|
|
|
|
:test #'mu4e~draft-address-cell-equal))
|
|
|
|
|
;; now we have the basic list, but we must remove
|
|
|
|
|
;; addresses also in the To: list
|
|
|
|
|
(cc-lst
|
|
|
|
|
(cl-delete-if
|
|
|
|
|
(lambda (cc-cell)
|
|
|
|
|
(cl-find-if
|
|
|
|
|
(lambda (to-cell)
|
|
|
|
|
(mu4e~draft-address-cell-equal cc-cell to-cell))
|
|
|
|
|
(mu4e~draft-create-to-lst origmsg)))
|
|
|
|
|
cc-lst))
|
|
|
|
|
;; remove ignored addresses
|
|
|
|
|
(cc-lst (mu4e~strip-ignored-addresses cc-lst))
|
|
|
|
|
;; finally, we need to remove ourselves from the cc-list
|
|
|
|
|
;; unless mu4e-compose-keep-self-cc is non-nil
|
|
|
|
|
(cc-lst
|
|
|
|
|
(if (or mu4e-compose-keep-self-cc (null user-mail-address))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
cc-lst
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(cl-delete-if
|
|
|
|
|
(lambda (cc-cell)
|
|
|
|
|
(cl-member-if
|
|
|
|
|
(lambda (addr)
|
|
|
|
|
(string= (downcase addr) (downcase (cdr cc-cell))))
|
|
|
|
|
(mu4e-personal-addresses)))
|
|
|
|
|
cc-lst))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
cc-lst)))
|
|
|
|
|
|
2020-01-25 21:21:56 +01:00
|
|
|
|
(defun mu4e~draft-recipients-construct (field origmsg &optional reply-all include-from)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Create value (a string) for the recipient FIELD.
|
|
|
|
|
\(which is a symbol, :to or :cc), based on the original message ORIGMSG,
|
2012-10-31 19:33:46 +01:00
|
|
|
|
and (optionally) REPLY-ALL which indicates this is a reply-to-all
|
|
|
|
|
message. Return nil if there are no recipients for the particular field."
|
|
|
|
|
(mu4e~draft-recipients-list-to-string
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(cl-case field
|
|
|
|
|
(:to
|
|
|
|
|
(mu4e~draft-create-to-lst origmsg))
|
|
|
|
|
(:cc
|
|
|
|
|
(mu4e~draft-create-cc-lst origmsg reply-all include-from))
|
|
|
|
|
(otherwise
|
|
|
|
|
(mu4e-error "Unsupported field")))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
2019-05-11 12:35:22 +02:00
|
|
|
|
;;; RFC2822 handling of phrases in mail-addresses
|
2020-02-11 15:10:35 +01:00
|
|
|
|
;;
|
|
|
|
|
;; The optional display-name contains a phrase, it sits before the
|
|
|
|
|
;; angle-addr as specified in RFC2822 for email-addresses in header
|
|
|
|
|
;; fields. Contributed by jhelberg.
|
2019-05-11 12:35:22 +02:00
|
|
|
|
|
|
|
|
|
(defun mu4e~rfc822-phrase-type (ph)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Return an atom or quoted-string for the phrase PH.
|
|
|
|
|
This checks for empty string first. Then quotes around the phrase
|
|
|
|
|
\(returning 'rfc822-quoted-string). Then whether there is a quote
|
|
|
|
|
inside the phrase (returning 'rfc822-containing-quote).
|
|
|
|
|
The reverse of the RFC atext definition is then tested.
|
|
|
|
|
If it matches, nil is returned, if not, it is an 'rfc822-atom, which
|
|
|
|
|
is returned."
|
2019-05-11 12:35:22 +02:00
|
|
|
|
(cond
|
2020-02-11 12:00:46 +01:00
|
|
|
|
((= (length ph) 0) 'rfc822-empty)
|
|
|
|
|
((= (aref ph 0) ?\")
|
|
|
|
|
(if (string-match "\"\\([^\"\\\n]\\|\\\\.\\|\\\\\n\\)*\"" ph)
|
2020-01-08 22:25:37 +01:00
|
|
|
|
'rfc822-quoted-string
|
2020-02-11 12:00:46 +01:00
|
|
|
|
'rfc822-containing-quote)) ; starts with quote, but doesn't end with one
|
|
|
|
|
((string-match-p "[\"]" ph) 'rfc822-containing-quote)
|
|
|
|
|
((string-match-p "[\000-\037()\*<>@,;:\\\.]+" ph) nil)
|
|
|
|
|
(t 'rfc822-atom)))
|
2019-05-11 12:35:22 +02:00
|
|
|
|
|
|
|
|
|
(defun mu4e~rfc822-quoteit (ph)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Quote an RFC822 phrase PH only if necessary.
|
|
|
|
|
Atoms and quoted strings don't need quotes. The rest do. In
|
|
|
|
|
case a phrase contains a quote, it will be escaped."
|
2019-05-11 12:35:22 +02:00
|
|
|
|
(let ((type (mu4e~rfc822-phrase-type ph)))
|
|
|
|
|
(cond
|
2020-02-11 12:00:46 +01:00
|
|
|
|
((eq type 'rfc822-atom) ph)
|
|
|
|
|
((eq type 'rfc822-quoted-string) ph)
|
|
|
|
|
((eq type 'rfc822-containing-quote)
|
|
|
|
|
(format "\"%s\""
|
|
|
|
|
(replace-regexp-in-string "\"" "\\\\\"" ph)))
|
|
|
|
|
(t (format "\"%s\"" ph)))))
|
2019-05-11 12:35:22 +02:00
|
|
|
|
|
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(defun mu4e~draft-from-construct ()
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Construct a value for the From:-field of the reply.
|
|
|
|
|
This is based on the variable `user-full-name' and
|
|
|
|
|
`user-mail-address'; if the latter is nil, function returns nil."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(when user-mail-address
|
|
|
|
|
(if user-full-name
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(format "%s <%s>" (mu4e~rfc822-quoteit user-full-name) user-mail-address)
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(format "%s" user-mail-address))))
|
|
|
|
|
|
|
|
|
|
|
2020-02-18 22:39:30 +01:00
|
|
|
|
;;; Header separators
|
2020-02-11 15:10:35 +01:00
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(defun mu4e~draft-insert-mail-header-separator ()
|
2012-11-10 14:01:17 +01:00
|
|
|
|
"Insert `mail-header-separator' in the first empty line of the message.
|
|
|
|
|
`message-mode' needs this line to know where the headers end and
|
|
|
|
|
the body starts. Note, in `mu4e-compose-mode', we use
|
2012-10-31 19:33:46 +01:00
|
|
|
|
`before-save-hook' and `after-save-hook' to ensure that this
|
|
|
|
|
separator is never written to the message file. Also see
|
|
|
|
|
`mu4e-remove-mail-header-separator'."
|
2012-11-04 19:41:09 +01:00
|
|
|
|
;; we set this here explicitly, since (as it has happened) a wrong
|
|
|
|
|
;; value for this (such as "") breaks address completion and other things
|
2015-12-15 06:02:24 +01:00
|
|
|
|
(set (make-local-variable 'mail-header-separator) "--text follows this line--")
|
2012-11-08 20:35:01 +01:00
|
|
|
|
(put 'mail-header-separator 'permanent-local t)
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(save-excursion
|
2013-05-21 07:05:06 +02:00
|
|
|
|
;; make sure there's not one already
|
|
|
|
|
(mu4e~draft-remove-mail-header-separator)
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(let ((sepa (propertize mail-header-separator
|
2020-02-11 12:00:46 +01:00
|
|
|
|
'intangible t
|
|
|
|
|
;; don't make this read-only, message-mode
|
|
|
|
|
;; seems to require it being writable in some cases
|
|
|
|
|
;;'read-only "Can't touch this"
|
|
|
|
|
'rear-nonsticky t
|
|
|
|
|
'font-lock-face 'mu4e-compose-separator-face)))
|
2013-05-20 05:53:55 +02:00
|
|
|
|
(widen)
|
2012-10-31 19:33:46 +01:00
|
|
|
|
;; search for the first empty line
|
2013-05-21 07:05:06 +02:00
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(if (search-forward-regexp "^$" nil t)
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(progn
|
|
|
|
|
(replace-match sepa)
|
|
|
|
|
;; `message-narrow-to-headers` searches for a
|
|
|
|
|
;; `mail-header-separator` followed by a new line. Therefore, we
|
|
|
|
|
;; must insert a newline if on the last line of the buffer.
|
|
|
|
|
(when (= (point) (point-max))
|
|
|
|
|
(insert "\n")))
|
2019-05-19 21:04:53 +02:00
|
|
|
|
(progn ;; no empty line? then prepend one
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(insert "\n" sepa))))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-remove-mail-header-separator ()
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Remove `mail-header-separator'.
|
|
|
|
|
We do this before saving a
|
2012-10-31 19:33:46 +01:00
|
|
|
|
file (and restore it afterwards), to ensure that the separator
|
2019-05-19 21:04:53 +02:00
|
|
|
|
never hits the disk. Also see
|
|
|
|
|
`mu4e~draft-insert-mail-header-separator."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(save-excursion
|
2013-05-20 05:53:55 +02:00
|
|
|
|
(widen)
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
;; remove the --text follows this line-- separator
|
2013-05-21 07:05:06 +02:00
|
|
|
|
(when (search-forward-regexp (concat "^" mail-header-separator) nil t)
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(let ((inhibit-read-only t))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(replace-match "")))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
2016-01-03 12:29:19 +01:00
|
|
|
|
(defun mu4e~draft-reply-all-p (origmsg)
|
2012-11-10 14:01:17 +01:00
|
|
|
|
"Ask user whether she wants to reply to *all* recipients.
|
|
|
|
|
If there is just one recipient of ORIGMSG do nothing."
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(let* ((recipnum
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(+ (length (mu4e~draft-create-to-lst origmsg))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(length (mu4e~draft-create-cc-lst origmsg t))))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(response
|
|
|
|
|
(if (< recipnum 2)
|
2020-01-08 22:25:37 +01:00
|
|
|
|
'all ;; with less than 2 recipients, we can reply to 'all'
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e-read-option
|
|
|
|
|
"Reply to "
|
|
|
|
|
`( (,(format "all %d recipients" recipnum) . all)
|
|
|
|
|
("sender only" . sender-only))))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(eq response 'all)))
|
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-message-filename-construct (&optional flagstr)
|
2012-11-10 14:01:17 +01:00
|
|
|
|
"Construct a randomized name for a message file with flags FLAGSTR.
|
|
|
|
|
It looks something like
|
2012-10-31 19:33:46 +01:00
|
|
|
|
<time>-<random>.<hostname>:2,
|
|
|
|
|
You can append flags."
|
2015-10-09 01:02:58 +02:00
|
|
|
|
(let* ((sysname (if (fboundp 'system-name)
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(system-name)
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(with-no-warnings system-name)))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(sysname (if (string= sysname "") "localhost" sysname))
|
|
|
|
|
(hostname (downcase
|
|
|
|
|
(save-match-data
|
|
|
|
|
(substring sysname
|
|
|
|
|
(string-match "^[^.]+" sysname)
|
|
|
|
|
(match-end 0))))))
|
2015-10-02 16:43:38 +02:00
|
|
|
|
(format "%s.%04x%04x%04x%04x.%s:2,%s"
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(format-time-string "%s" (current-time))
|
|
|
|
|
(random 65535) (random 65535) (random 65535) (random 65535)
|
|
|
|
|
hostname (or flagstr ""))))
|
2016-06-05 17:49:04 +02:00
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(defun mu4e~draft-common-construct ()
|
|
|
|
|
"Construct the common headers for each message."
|
2015-07-24 08:23:24 +02:00
|
|
|
|
(concat
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(when mu4e-user-agent-string
|
|
|
|
|
(mu4e~draft-header "User-agent" mu4e-user-agent-string))
|
|
|
|
|
(mu4e~draft-header "Date" (message-make-date))))
|
2014-05-24 19:30:13 +02:00
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(defconst mu4e~draft-reply-prefix "Re: "
|
|
|
|
|
"String to prefix replies with.")
|
|
|
|
|
|
2020-01-11 14:10:37 +01:00
|
|
|
|
(defun mu4e~draft-reply-construct-recipients (origmsg)
|
|
|
|
|
"Determine the to/cc recipients for a reply message."
|
2013-07-17 20:13:42 +02:00
|
|
|
|
(let* ((reply-to-self (mu4e-message-contact-field-matches-me origmsg :from))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
;; reply-to-self implies reply-all
|
|
|
|
|
(reply-all (or reply-to-self
|
|
|
|
|
(eq mu4e-compose-reply-recipients 'all)
|
|
|
|
|
(and (not (eq mu4e-compose-reply-recipients 'sender))
|
|
|
|
|
(mu4e~draft-reply-all-p origmsg)))))
|
2013-07-07 17:30:30 +02:00
|
|
|
|
(concat
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(if reply-to-self
|
|
|
|
|
;; When we're replying to ourselves, simply keep the same headers.
|
|
|
|
|
(concat
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(mu4e~draft-header "To" (mu4e~draft-recipients-list-to-string
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e-message-field origmsg :to)))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
(mu4e~draft-header "Cc" (mu4e~draft-recipients-list-to-string
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e-message-field origmsg :cc))))
|
2020-01-08 22:25:37 +01:00
|
|
|
|
|
2020-02-11 12:00:46 +01:00
|
|
|
|
;; if there's no-one in To, copy the CC-list
|
|
|
|
|
(if (zerop (length (mu4e~draft-create-to-lst origmsg)))
|
|
|
|
|
(mu4e~draft-header "To" (mu4e~draft-recipients-construct
|
2020-01-08 22:25:37 +01:00
|
|
|
|
:cc origmsg reply-all))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
;; otherwise...
|
|
|
|
|
(concat
|
|
|
|
|
(mu4e~draft-header "To" (mu4e~draft-recipients-construct :to origmsg))
|
|
|
|
|
(mu4e~draft-header "Cc" (mu4e~draft-recipients-construct :cc origmsg reply-all))))))))
|
2020-01-11 14:10:37 +01:00
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-reply-construct-recipients-list (origmsg)
|
|
|
|
|
"Determine the to/cc recipients for a reply message to a
|
|
|
|
|
mailing-list."
|
|
|
|
|
(let* ( ;; reply-to-self implies reply-all
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(list-post (plist-get origmsg :list-post))
|
|
|
|
|
(from (plist-get origmsg :from))
|
|
|
|
|
(recipnum
|
|
|
|
|
(+ (length (mu4e~draft-create-to-lst origmsg))
|
|
|
|
|
(length (mu4e~draft-create-cc-lst origmsg t t))))
|
|
|
|
|
(reply-type
|
|
|
|
|
(mu4e-read-option
|
|
|
|
|
"Reply to mailing-list "
|
|
|
|
|
`( (,(format "all %d recipient(s)" recipnum) . all)
|
|
|
|
|
(,(format "list-only (%s)" (cdar list-post)) . list-only)
|
|
|
|
|
(,(format "sender-only (%s)" (cdar from)) . sender-only)))))
|
2020-01-11 14:10:37 +01:00
|
|
|
|
(cl-case reply-type
|
|
|
|
|
(all
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(concat
|
|
|
|
|
(mu4e~draft-header "To" (mu4e~draft-recipients-construct :to origmsg))
|
|
|
|
|
(mu4e~draft-header "Cc" (mu4e~draft-recipients-construct :cc origmsg t t))))
|
2020-01-11 14:10:37 +01:00
|
|
|
|
(list-only
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e~draft-header "To"
|
|
|
|
|
(mu4e~draft-recipients-list-to-string list-post)))
|
2020-01-11 14:10:37 +01:00
|
|
|
|
(sender-only
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e~draft-header "To"
|
|
|
|
|
(mu4e~draft-recipients-list-to-string from))))))
|
2020-01-11 14:10:37 +01:00
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-reply-construct (origmsg)
|
|
|
|
|
"Create a draft message as a reply to ORIGMSG.
|
|
|
|
|
Replying-to-self is special; in that case, the To and Cc fields
|
|
|
|
|
will be the same as in the original."
|
|
|
|
|
(let* ((old-msgid (plist-get origmsg :message-id))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(subject (concat mu4e~draft-reply-prefix
|
|
|
|
|
(message-strip-subject-re
|
|
|
|
|
(or (plist-get origmsg :subject) ""))))
|
|
|
|
|
(list-post (plist-get origmsg :list-post)))
|
2020-01-11 14:10:37 +01:00
|
|
|
|
(concat
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e~draft-header "From" (or (mu4e~draft-from-construct) ""))
|
|
|
|
|
(mu4e~draft-header "Reply-To" mu4e-compose-reply-to-address)
|
|
|
|
|
|
|
|
|
|
(if list-post ;; mailing-lists are a bit special.
|
|
|
|
|
(mu4e~draft-reply-construct-recipients-list origmsg)
|
|
|
|
|
(mu4e~draft-reply-construct-recipients origmsg))
|
|
|
|
|
|
|
|
|
|
(mu4e~draft-header "Subject" subject)
|
|
|
|
|
(mu4e~draft-header "References"
|
|
|
|
|
(mu4e~draft-references-construct origmsg))
|
|
|
|
|
(mu4e~draft-common-construct)
|
|
|
|
|
(when old-msgid
|
|
|
|
|
(mu4e~draft-header "In-reply-to" (format "<%s>" old-msgid)))
|
|
|
|
|
"\n\n"
|
|
|
|
|
(mu4e~draft-cite-original origmsg))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
(defconst mu4e~draft-forward-prefix "Fwd: "
|
|
|
|
|
"String to prefix replies with.")
|
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-forward-construct (origmsg)
|
|
|
|
|
"Create a draft forward message for original message ORIGMSG."
|
|
|
|
|
(let ((subject
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(or (plist-get origmsg :subject) "")))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(concat
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e~draft-header "From" (or (mu4e~draft-from-construct) ""))
|
|
|
|
|
(mu4e~draft-header "Reply-To" mu4e-compose-reply-to-address)
|
|
|
|
|
(mu4e~draft-header "To" "")
|
|
|
|
|
(mu4e~draft-common-construct)
|
|
|
|
|
(mu4e~draft-header "References"
|
|
|
|
|
(mu4e~draft-references-construct origmsg))
|
|
|
|
|
(mu4e~draft-header "Subject"
|
|
|
|
|
(concat
|
|
|
|
|
;; if there's no Fwd: yet, prepend it
|
|
|
|
|
(if (string-match "^Fwd:" subject)
|
|
|
|
|
""
|
|
|
|
|
mu4e~draft-forward-prefix)
|
|
|
|
|
subject))
|
|
|
|
|
(unless mu4e-compose-forward-as-attachment
|
|
|
|
|
(concat
|
|
|
|
|
"\n\n"
|
|
|
|
|
(mu4e~draft-cite-original origmsg))))))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-newmsg-construct ()
|
|
|
|
|
"Create a new message."
|
|
|
|
|
(concat
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e~draft-header "From" (or (mu4e~draft-from-construct) ""))
|
|
|
|
|
(mu4e~draft-header "Reply-To" mu4e-compose-reply-to-address)
|
|
|
|
|
(mu4e~draft-header "To" "")
|
|
|
|
|
(mu4e~draft-header "Subject" "")
|
|
|
|
|
(mu4e~draft-common-construct)))
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
|
|
|
|
(defvar mu4e~draft-drafts-folder nil
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"The drafts-folder for this compose buffer.
|
|
|
|
|
This is based on `mu4e-drafts-folder', which is evaluated once.")
|
2012-10-31 19:33:46 +01:00
|
|
|
|
|
2016-02-16 07:13:03 +01:00
|
|
|
|
(defun mu4e~draft-open-file (path)
|
|
|
|
|
"Open the the draft file at PATH."
|
2016-02-20 11:25:21 +01:00
|
|
|
|
(if mu4e-compose-in-new-frame
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(find-file-other-frame path)
|
2016-02-16 07:13:03 +01:00
|
|
|
|
(find-file path)))
|
|
|
|
|
|
|
|
|
|
(defun mu4e~draft-determine-path (draft-dir)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Determines the path for a new draft file in DRAFT-DIR."
|
2016-02-16 07:13:03 +01:00
|
|
|
|
(format "%s/%s/cur/%s"
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mu4e-root-maildir) draft-dir (mu4e~draft-message-filename-construct "DS")))
|
2016-02-16 07:13:03 +01:00
|
|
|
|
|
|
|
|
|
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(defun mu4e-draft-open (compose-type &optional msg)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
"Open a draft file for a message MSG.
|
|
|
|
|
In case of a new message (when COMPOSE-TYPE is `reply', `forward'
|
|
|
|
|
or `new'), open an existing draft (when COMPOSE-TYPE is `edit'),
|
|
|
|
|
or re-send an existing message (when COMPOSE-TYPE is `resend').
|
2016-02-16 07:13:03 +01:00
|
|
|
|
|
|
|
|
|
The name of the draft folder is constructed from the
|
2020-02-06 19:28:24 +01:00
|
|
|
|
concatenation of `(mu4e-root-maildir)' and `mu4e-drafts-folder' (the
|
2016-02-16 07:13:03 +01:00
|
|
|
|
latter will be evaluated). The message file name is a unique name
|
|
|
|
|
determined by `mu4e-send-draft-file-name'. The initial contents
|
|
|
|
|
will be created from either `mu4e~draft-reply-construct', or
|
2013-05-21 07:05:06 +02:00
|
|
|
|
`mu4e~draft-forward-construct' or `mu4e~draft-newmsg-construct'."
|
2016-02-16 07:13:03 +01:00
|
|
|
|
(let ((draft-dir nil))
|
2018-09-18 02:53:27 +02:00
|
|
|
|
(cl-case compose-type
|
2016-06-05 17:49:04 +02:00
|
|
|
|
|
2016-02-16 07:13:03 +01:00
|
|
|
|
(edit
|
2020-02-11 12:00:46 +01:00
|
|
|
|
;; case-1: re-editing a draft messages. in this case, we do know the
|
|
|
|
|
;; full path, but we cannot really know 'drafts folder'... we make a
|
|
|
|
|
;; guess
|
|
|
|
|
(setq draft-dir (mu4e~guess-maildir (mu4e-message-field msg :path)))
|
|
|
|
|
(mu4e~draft-open-file (mu4e-message-field msg :path)))
|
2016-02-16 07:13:03 +01:00
|
|
|
|
|
|
|
|
|
(resend
|
2020-02-11 12:00:46 +01:00
|
|
|
|
;; case-2: copy some exisisting message to a draft message, then edit
|
|
|
|
|
;; that.
|
|
|
|
|
(setq draft-dir (mu4e~guess-maildir (mu4e-message-field msg :path)))
|
|
|
|
|
(let ((draft-path (mu4e~draft-determine-path draft-dir)))
|
|
|
|
|
(copy-file (mu4e-message-field msg :path) draft-path)
|
|
|
|
|
(mu4e~draft-open-file draft-path)))
|
2016-06-05 17:49:04 +02:00
|
|
|
|
|
2016-02-16 07:13:03 +01:00
|
|
|
|
((reply forward new)
|
2020-02-11 12:00:46 +01:00
|
|
|
|
;; case-3: creating a new message; in this case, we can determine
|
|
|
|
|
;; mu4e-get-drafts-folder
|
|
|
|
|
(setq draft-dir (mu4e-get-drafts-folder msg))
|
|
|
|
|
(let ((draft-path (mu4e~draft-determine-path draft-dir))
|
|
|
|
|
(initial-contents
|
|
|
|
|
(cl-case compose-type
|
|
|
|
|
(reply (mu4e~draft-reply-construct msg))
|
|
|
|
|
(forward (mu4e~draft-forward-construct msg))
|
|
|
|
|
(new (mu4e~draft-newmsg-construct)))))
|
|
|
|
|
(mu4e~draft-open-file draft-path)
|
|
|
|
|
(insert initial-contents)
|
|
|
|
|
(newline)
|
|
|
|
|
;; include the message signature (if it's set)
|
|
|
|
|
(if (and mu4e-compose-signature-auto-include mu4e-compose-signature)
|
|
|
|
|
(let ((message-signature mu4e-compose-signature))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(message-insert-signature)
|
|
|
|
|
(mu4e~fontify-signature))))))
|
2019-05-19 21:04:53 +02:00
|
|
|
|
(t (mu4e-error "Unsupported compose-type %S" compose-type)))
|
2016-07-03 09:00:27 +02:00
|
|
|
|
;; if we didn't find a draft folder yet, try some default
|
|
|
|
|
(unless draft-dir
|
|
|
|
|
(setq draft-dir (mu4e-get-drafts-folder msg)))
|
|
|
|
|
;; evaluate mu4e~drafts-drafts-folder once, here, and use that value
|
|
|
|
|
;; throughout.
|
2013-05-21 07:05:06 +02:00
|
|
|
|
(set (make-local-variable 'mu4e~draft-drafts-folder) draft-dir)
|
|
|
|
|
(put 'mu4e~draft-drafts-folder 'permanent-local t)
|
|
|
|
|
(unless mu4e~draft-drafts-folder
|
2019-05-19 21:04:53 +02:00
|
|
|
|
(mu4e-error "Failed to determine drafts folder"))))
|
2013-05-21 07:05:06 +02:00
|
|
|
|
|
2020-02-11 13:26:45 +01:00
|
|
|
|
;;; _
|
2012-10-31 19:33:46 +01:00
|
|
|
|
(provide 'mu4e-draft)
|
2019-05-19 21:04:53 +02:00
|
|
|
|
;;; mu4e-draft.el ends here
|