summary refs log tree commit diff stats
path: root/lisp/acdw-mail.el
blob: ae78fa05f7171d35470171e7462d24cb212c580e (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
;;; acdw-mail.el --- My email configuration  -*- lexical-binding: t; -*-

;;; Code:

(require 'cl-lib)
(require 'message)

;;; Variables

(defcustom +message-send-dispatch-rules nil
  "Alist to set variables based on the current from address."
  :group 'message
  :type '(alist :key-type (string :tag "From address")
                :value-type (alist :tag "Rules"
                                   :key-type (symbol :tag "Variable")
                                   :value-type (sexp :tag "Value"))))

(defcustom +notmuch-spam-tags '("+spam")
  "List of tag changes to apply when marking a thread as spam."
  :group 'notmuch
  :type '(repeat string))

;;; Functions

(defun +message-send-set-variables ()
  "Set variables for `message-send' depending on the From: header.
Useful in `message-send-hook'."
  (let ((from (message-fetch-field "from")))
    (cl-loop for (var . val) in (cl-loop for (address . bindings)
                                         in +message-send-dispatch-rules
                                         if (string-match-p address from)
                                         return bindings)
             do (set (make-local-variable var) val))))

;; Thanks to Alex Schroeder!
;; https://www.emacswiki.org/emacs/Change_Signature_Dynamically
(defun +message-check-for-signature-change (&rest ignore)
  "Check for a change in the To: or Cc: fields"
  (when (and (message--in-tocc-p)
             (not (buffer-narrowed-p)))
    (save-excursion
      (goto-char (point-max))
      (let ((end (point)))
        (when (re-search-backward message-signature-separator nil t)
          (delete-region (1- (match-beginning 0)) end)))
      (message-insert-signature))))

(defun +message-signature-setup ()
  (make-local-variable 'after-change-functions)
  (push '+message-check-for-signature-change after-change-functions))

(defun +notmuch-field-match-p (field regexp)
  "Return whether message FIELD matches REGEXP."
  (string-match-p regexp (or (message-fetch-field field) "")))

(defun +notmuch-query-concat (&rest queries)
  "Concatenate `notmuch' QUERIES with AND."
  (mapconcat #'identity queries " AND "))

;;;###autoload
(defun +notmuch-goto (&optional prefix)
  "Perform a saved `notmuch' search.
Without a PREFIX argument, perform the first search in
`notmuch-saved-searches'.  With a single PREFIX argument
(\\[universal-argument]), prompt the user as to which saved
search to perform.  With two PREFIX arguments, prompt the user
for a free-form search.  With any other PREFIX argument, open
`notmuch-hello'."
  (interactive "P")
  (pcase prefix
    ('nil (notmuch-search (plist-get (car notmuch-saved-searches) :query)))
    ('(4) (notmuch-search
           (plist-get (cl-find (completing-read "Saved search: "
                                                (mapcar (lambda (elt)
                                                          (plist-get elt :name))
                                                        notmuch-saved-searches))
                               notmuch-saved-searches
                               :key (lambda (elt) (plist-get elt :name))
                               :test #'equal)
                      :query)))
    ('(16) (notmuch-search))
    (_ (notmuch-hello))))

(defun +notmuch-search-mark-spam (&optional ham start end)
  "Mark the current thread or region as spam.
That is, add the tags in `+notmuch-spam-tags' to the message.
With an optional HAM argument (interactively,
\\[universal-argument]), mark the message as not-spam, or ham, by
reversing the tag changes."
  (interactive (cons current-prefix-arg (notmuch-interactive-region)))
  (when +notmuch-spam-tags
    (notmuch-search-tag (notmuch-tag-change-list +notmuch-spam-tags ham)
                        start end))
  (when (eq start end)
    (notmuch-search-next-thread)))

(defun +notmuch-tree-mark-spam (&optional ham _ _)
  "Mark the current message as spam.
That is, add the tags in `+notmuch-spam-tags' to the message.
With an optional HAM argument (interactively,
\\[universal-argument]), mark the message as not-spam, or ham, by
reversing the tag changes."
  (interactive (cons current-prefix-arg (notmuch-interactive-region)))
  (when +notmuch-spam-tags
    (notmuch-tree-tag (notmuch-tag-change-list +notmuch-spam-tags ham)))
  (notmuch-tree-next-matching-message))

(defun +notmuch-define-saved-search (name key search-type &rest queries)
  "Wrapper to ease `notmuch-saved-searches' defining.
NAME, KEY, and SEARCH-TYPE are paired with the corresponding keywords in
`notmuch-saved-searches', which see.  QUERIES are all concatenated together with
AND.  If QUERIES is prepended with more keyword arguments, those are added to
the saved search as well."
  (declare (indent 3))
  (let (extra-keywords)
    (while (keywordp (car queries))
      (push (cadr queries) extra-keywords)
      (push (car queries) extra-keywords)
      (setf queries (cddr queries)))
    (add-to-list 'notmuch-saved-searches
                 (append (list :name name
                               :key key
                               :search-type search-type
                               :query (apply #'+notmuch-query-concat queries))
                         (reverse extra-keywords))
                 :append
                 (lambda (a b)
                   (equal (plist-get a :name)
                          (plist-get b :name))))))

(defun notmuch-async-poll ()
  "Run `notmuch-poll' in an async process."
  (interactive)
  (if (require 'async nil t)
      (progn
        (message "Polling mail async...")
        (async-start
         (lambda ()
           (push "~/usr/share/emacs/site-lisp/" load-path)
           (require 'notmuch-lib)
           (notmuch-poll))
         (lambda (result)
           (message "%s" result))))
    (user-error "Feature `async' not found!")))


;;; Packages

(use-package bbdb
  :ensure t
  :config
  (setopt bbdb-complete-mail-allow-cycling t
          bbdb-file (private/ "bbdb"))
  (add-hook 'custom-allowed-after-load-hook
            (defun bbdb@after-custom ()
              (require 'bbdb)
              (require 'bbdb-message)
              (bbdb-initialize 'message))))

(use-package bbdb-vcard
  :ensure t
  :after bbdb)

(use-package notmuch
  :when (executable-find "notmuch")
  :load-path "~/usr/share/emacs/site-lisp/"
  :defer 30
  :commands (notmuch-mua-new-mail
             notmuch-search
             notmuch-hello)
  :preface (defdir notmuch/ (sync/ "emacs/notmuch/")
             "Notmuch configuration directory."
             :makedir)
  :bind (("C-c m" . notmuch-mua-new-mail))
  :config
  ;; Options
  (setopt notmuch-init-file (notmuch/ "notmuch-init.el" t)
          notmuch-address-save-filename (notmuch/ "addresses" t)
          notmuch-address-use-company (featurep 'company)
          notmuch-search-oldest-first nil
          notmuch-archive-tags '("-inbox" "-unread")
          notmuch-draft-tags '("+draft" "-inbox" "-unread")
          +notmuch-spam-tags '("+spam" "+Spam")
          mail-user-agent 'notmuch-user-agent
          message-mail-user-agent t
          notmuch-show-indent-content nil
          message-kill-buffer-on-exit t
          message-auto-save-directory nil
          message-signature "Case Duckworth\nhttps://www.acdw.net"
          send-mail-function #'sendmail-send-it
          mail-specify-envelope-from t
          message-sendmail-envelope-from 'header
          message-envelope-from 'header
          notmuch-saved-searches nil
          notmuch-poll-script "~/usr/scripts/syncmail" ; XXX: Deprecated option
          )
  ;; Key bindings
  (keymap-set notmuch-search-mode-map "!" #'+notmuch-search-mark-spam)
  (keymap-set notmuch-search-mode-map "RET" #'notmuch-search-show-thread)
  (keymap-set notmuch-search-mode-map "M-RET" #'notmuch-tree-from-search-thread)
  (keymap-set notmuch-tree-mode-map "!" #'+notmuch-tree-mark-spam)
  ;; Saved searches
  (+notmuch-define-saved-search "inbox+unread" "m" 'tree
    "tag:inbox" "tag:unread" "NOT tag:Spam")
  (+notmuch-define-saved-search "inbox" "i" 'tree
    "tag:inbox" "NOT tag:Spam")
  (+notmuch-define-saved-search "lists+unread" "l" 'tree
    "tag:/List/" "tag:unread")
  (+notmuch-define-saved-search "lists" "L" 'tree
    "tag:/List/")
  (+notmuch-define-saved-search "unread" "u" 'tree
    "tag:unread" "NOT tag:Spam")
  (+notmuch-define-saved-search "flagged" "f" 'tree
    "tag:flagged")
  (+notmuch-define-saved-search "sent" "t" 'tree
    "tag:sent")
  (+notmuch-define-saved-search "drafts" "d" 'tree
    "tag:draft")
  (+notmuch-define-saved-search "all mail" "a" 'tree "*")
  ;; Hooks and advice
  (add-hook 'message-send-hook #'+message-send-set-variables)
  (add-hook 'message-setup-hook #'+message-signature-setup)
  (autoload 'visual-fill-column-mode "visual-fill-column" nil t)
  (add-hook 'notmuch-message-mode-hook #'visual-fill-column-mode)
  (add-hook 'notmuch-show-mode-hook #'visual-fill-column-mode)

  (define-advice notmuch-bury-or-kill-this-buffer (:after (&rest _) poll-async)
    (notmuch-async-poll))

  (define-advice notmuch-address-selection-function
      (:override (prompt collection _) no-initial-input)
    "Call `completing-read' with `notmuch-address-history'.
This version doesn't add any initial-input."
    (completing-read prompt collection nil nil nil 'notmuch-address-history))

  (add-to-list 'notmuch-message-headers "List-Post" :append #'equal)
  (define-advice notmuch-mua-new-reply (:around (orig &rest r) list-aware)
    "Make `notmuch-mua-new-reply' list-aware."
    (let ((ml (notmuch-show-get-header :List-Post)))
      (apply orig r)
      (require 'message)
      (when ml
        (with-buffer-modified-unmodified
         (message-remove-header "To")
         (message-add-header
          (format "To: %s" (replace-regexp-in-string "<mailto:\\(.*\\)>" "\\1"
                                                     ml)))
         (message-goto-body)))))

  (define-advice notmuch-tag (:filter-args (args) trim)
    "Trim whitespace from ends of tags."
    (list (car args) (mapcar #'string-trim (cadr args))))
  ;; Load init file
  (load notmuch-init-file :noerror))

(provide 'acdw-mail)
;;; acdw-mail.el ends here