- "Can be added to message-send-hook and will set the FCC header
- based on the values of notmuch-fcc-directories (see the
- variable customization there for examples). It uses the
- first entry as default fallback if no From address
- matches."
- ; only do something if notmuch-fcc-dirs is set
- (if notmuch-fcc-dirs
- (let ((subdir
- (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t))))
- (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
- (message-remove-header "Fcc")
- (message-add-header (concat "Fcc: " message-directory subdir)))))
+ "Add an Fcc header to the current message buffer.
+
+Sets the Fcc header based on the values of `notmuch-fcc-dirs'.
+
+Originally intended to be use a hook function, but now called directly
+by notmuch-mua-mail"
+
+ (let ((subdir
+ (cond
+ ((or (not notmuch-fcc-dirs)
+ (message-field-value "Fcc"))
+ ;; Nothing set or an existing header.
+ nil)
+
+ ((stringp notmuch-fcc-dirs)
+ notmuch-fcc-dirs)
+
+ ((and (listp notmuch-fcc-dirs)
+ (stringp (car notmuch-fcc-dirs)))
+ ;; Old style - no longer works.
+ (error "Invalid `notmuch-fcc-dirs' setting (old style)"))
+
+ ((listp notmuch-fcc-dirs)
+ (let* ((from (message-field-value "From"))
+ (match
+ (catch 'first-match
+ (dolist (re-folder notmuch-fcc-dirs)
+ (when (string-match-p (car re-folder) from)
+ (throw 'first-match re-folder))))))
+ (if match
+ (cdr match)
+ (message "No Fcc header added.")
+ nil)))