- "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)))))
-
+ "Adds an appropriate fcc header to the current mail buffer
+
+ 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
+ (when notmuch-fcc-dirs
+ (let (subdir)
+ (if (stringp notmuch-fcc-dirs)
+ ;; notmuch-fcc-dirs is a string, just use it as subdir
+ (setq subdir notmuch-fcc-dirs)
+ ;; else: it's a list of alists (("sent") ("name1" . "sent1"))
+ (setq subdir (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t)))
+ ;; if we found no hit, use the first entry as default fallback
+ (unless subdir (setq subdir (car (car notmuch-fcc-dirs)))))
+
+ ;; if there is no fcc header yet, add ours
+ (unless (message-fetch-field "fcc")
+ (message-add-header (concat "Fcc: "
+ (if (= (elt subdir 0) ?/)
+ subdir
+ (concat (notmuch-database-path) "/" subdir)))))
+
+ ;; finally test if fcc points to a valid maildir
+ (let ((fcc-header (message-fetch-field "fcc")))
+ (unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
+ (cond ((not (file-writable-p fcc-header))
+ (error (format "%s is not a maildir, but you don't have permission to create one." fcc-header)))
+ ((y-or-n-p (format "%s is not a maildir. Create it? "
+ fcc-header))
+ (notmuch-maildir-fcc-create-maildir fcc-header))
+ (t
+ (error "Not sending message."))))))))
+