about summary refs log tree commit diff
path: root/tools/emacs/config/mail-setup.el
blob: 71cfb92ff53cd3fe7a1aeeea6a3d7bd4673da0cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(require 'notmuch)
(require 'counsel-notmuch)

(global-set-key (kbd "C-c m") 'notmuch-hello)
(global-set-key (kbd "C-c C-m") 'counsel-notmuch)
(global-set-key (kbd "C-c C-e n") 'notmuch-mua-new-mail)

(setq notmuch-cache-dir (format "%s/.cache/notmuch" (getenv "HOME")))
(make-directory notmuch-cache-dir t)

;; Cache addresses for completion:
(setq notmuch-address-save-filename (concat notmuch-cache-dir "/addresses"))

;; Don't spam my home folder with drafts:
(setq notmuch-draft-folder "drafts") ;; relative to notmuch database

;; Mark things as read when archiving them:
(setq notmuch-archive-tags '("-inbox" "-unread" "+archive"))

;; Show me saved searches that I care about:
(setq notmuch-saved-searches
      '((:name "inbox" :query "tag:inbox" :count-query "tag:inbox AND tag:unread" :key "i")
        (:name "sent" :query "tag:sent" :key "t")
        (:name "drafts" :query "tag:draft")))
(setq notmuch-show-empty-saved-searches t)

;; Mail sending configuration
(setq send-mail-function 'sendmail-send-it) ;; sendmail provided by MSMTP
(setq notmuch-always-prompt-for-sender t)
(setq notmuch-mua-user-agent-function
      (lambda () (format "Emacs %s; notmuch.el %s" emacs-version notmuch-emacs-version)))
(setq mail-host-address (system-name))
(setq notmuch-mua-cite-function #'message-cite-original-without-signature)

;; Close mail buffers after sending mail
(setq message-kill-buffer-on-exit t)

;; Ensure sender is correctly passed to msmtp
(setq mail-specify-envelope-from t
      message-sendmail-envelope-from 'header
      mail-envelope-from 'header)

;; Store sent mail in the correct folder per account
(setq notmuch-maildir-use-notmuch-insert nil)
(setq notmuch-fcc-dirs '(("mail@tazj.in" . "tazjin/Sent")))

;; I don't use drafts but I instinctively hit C-x C-s constantly, lets
;; handle that gracefully.
(define-key notmuch-message-mode-map (kbd "C-x C-s") #'ignore)

;; MSMTP decrypts passwords using pass, but pinentry doesn't work
;; correctly in that setup. This forces a warmup of the GPG agent
;; before sending the message.
;;
;; Note that the sending function is advised because the provided hook
;; for this seems to run at the wrong time.
(advice-add 'notmuch-mua-send-common :before 'warmup-gpg-agent)

;; Define a telephone-line segment for displaying the count of unread,
;; important mails in the last window's mode-line:
(defvar *last-notmuch-count-redraw* 0)
(defvar *current-notmuch-count* nil)

(defun update-display-notmuch-counts ()
  "Update and render the current state of the notmuch unread
  count for display in the mode-line.

  The offlineimap-timer runs every 2 minutes, so it does not make
  sense to refresh this much more often than that."

  (when (> (- (float-time) *last-notmuch-count-redraw*) 30)
    (setq *last-notmuch-count-redraw* (float-time))
    (let* ((inbox-unread (notmuch-saved-search-count "tag:inbox and tag:unread"))
           (notmuch-count (format "I: %s; D: %s" inbox-unread)))
      (setq *current-notmuch-count* notmuch-count)))

  (when (and (bottom-right-window-p)
             ;; Only render if the initial update is done and there
             ;; are unread mails:
             *current-notmuch-count*
             (not (equal *current-notmuch-count* "I: 0; D: 0")))
    *current-notmuch-count*))

(telephone-line-defsegment telephone-line-notmuch-counts ()
  "This segment displays the count of unread notmuch messages in
  the last window's mode-line (if unread messages are present)."

  (update-display-notmuch-counts))

(provide 'mail-setup)