summaryrefslogtreecommitdiffhomepage
path: root/notmuch.org
blob: 11358f2ab79d112bec4f5dd35f9fa8b756ab6516 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#+property: header-args:emacs-lisp :lexical t :tangle yes :comments yes :results silent :shebang ";; -*- lexical-binding: t; -*-" :tangle-mode (identity #o644)
#+title: notmuch configuration

* minibuffer
  #+begin_src emacs-lisp
    (defvar jao-notmuch-minibuffer-string "")

    (defvar jao-notmuch-minibuffer-queries
      '((:name "" :query "tag:new" :face jao-themes-f00)
        (:name "B" :query "tag:new and tag:bigml and tag:inbox")
        (:name "b" :query "tag:new and tag:bigml and tag:bugs"
               :face jao-themes-error)
        (:name "S" :query "tag:new and tag:bigml and tag:support")
        (:name "W" :query "tag:new and tag:bigml" :face jao-themes-dimm)
        (:name "H" :query "tag:new and tag:jao and tag:hacking")
        (:name "E" :query "tag:new and tag:\"/emacs/\"" :face jao-themes-dimm)
        (:name "l" :query "tag:new and tag:local" :face jao-themes-dimm)
        (:name "J" :query
               "tag:new and tag:jao and not tag:\"/local|emacs|hacking|feeds/\""
               :face jao-themes-dimm)
        (:name "F" :query "tag:new and tag:feeds and not tag:\"/emacs/\""
               :face jao-themes-dimm)))

    (defun jao-notmuch-notify ()
      (let ((cnts (notmuch-hello-query-counts jao-notmuch-minibuffer-queries)))
        (setq jao-notmuch-minibuffer-string
              (mapconcat (lambda (c)
                           (propertize (format "%s%s"
                                               (plist-get c :name)
                                               (plist-get c :count))
                                       'face (plist-get c :face)))
                         cnts
                         " "))
        (jao-minibuffer-refresh)))

    (when (eq jao-afio-mail-function 'notmuch)
      (jao-minibuffer-add-variable 'jao-notmuch-minibuffer-string -20))
  #+end_src
* searches
  #+begin_src emacs-lisp
    (setq notmuch-tree-result-format
          '(("date" . "%12s  ")
            ("authors" . "%-35s")
            ((("tree" . "%s ") ("subject" . "%s")) . "  %-100s")
            ("tags" . "  (%s)"))
          notmuch-search-result-format
          '(("date" . "%12s ")
            ("count" . "%-7s ")
            ("authors" . "%-35s")
            ("subject" . "%-100s")
            ("tags" . "(%s)"))
          notmuch-unthreaded-result-format notmuch-tree-result-format)

    (defvar jao-notmuch--new "tag:\"/^(unread|new)$/\"")
    (defvar jao-notmuch--newa (concat jao-notmuch--new " AND "))

    (defun jao-notmuch--q (d0 d1 &optional k qs st)
      (let ((q (or (when qs (mapconcat #'identity qs " AND "))
                   (concat jao-notmuch--newa
                           (mapconcat (lambda (d) (when d (concat "tag:" d)))
                                      (list d0 d1) " AND ")))))
        (list :name (concat d0 (when d1 "/") d1)
              :key k :query q :search-type (or st 'tree)
              :sort-order 'oldest-first)))

    (defun jao-notmuch--qn (d0 d1 k qs &optional st)
      (jao-notmuch--q d0 d1 k (cons jao-notmuch--new qs) st))

    (defun jao-notmuch--mboxes-search (box &rest excluded)
      (let ((ms (seq-difference (jao-list-mailboxes box) excluded))
            (bp (substring box 0 1)))
        (mapcar (lambda (m) (jao-notmuch--q box (car m) (concat bp (cdr m))))
                (shorten-strings (sort ms #'string<)))))

    (setq notmuch-saved-searches
          `(,(jao-notmuch--q "bigml" "inbox" "bi")
            ,(jao-notmuch--q "bigml" "support" "bs")
            ,(jao-notmuch--q "bigml" "bugs" "bb")
            ,@(jao-notmuch--mboxes-search "bigml" "inbox" "support")
            ,@(jao-notmuch--mboxes-search "jao")
            ,(jao-notmuch--qn "local" nil "l" '("tag:local"))
            ,(jao-notmuch--qn "emacs" "feeds" "ee" '("tag:emacs"))
            ,(jao-notmuch--qn "emacs" "github" "eg" '("tag:emacs-github"))
            ,(jao-notmuch--qn "emacs" "devel" "ed" '("tag:emacs-devel"))
            ,(jao-notmuch--qn "emacs" "bugs" "eb" '("tag:emacs-bugs"))
            ,(jao-notmuch--qn "emacs" "diffs" "ec" '("tag:emacs-diffs"))
            ,(jao-notmuch--qn "feeds" "lobsters" "fl" '("tag:lobsters"))
            ,(jao-notmuch--qn "feeds" "prog" "fp" '("tag:prog" "not tag:lobsters"))
            ,@(jao-notmuch--mboxes-search "feeds" "emacs" "prog")
            ,(jao-notmuch--q "bml" "today" "tb" '("tag:bigml" "date:24h..") t)
            ,(jao-notmuch--q "jao" "today" "tj"
                             '("tag:jao" "date:24h.."
                               "not tag:\"/(feeds|spam|local)/\"")
                             t)
            ,(jao-notmuch--q "bml" "flagged" "rb" '("tag:flagged" "tag:bigml") t)
            ,(jao-notmuch--q "jao" "flagged" "rj" '("tag:flagged" "not tag:bigml") t)
            ,(jao-notmuch--q "new" nil "n" '("tag:new"))))

    (defun jao-notmuch-tree-widen-search ()
      (interactive)
      (when-let ((query (notmuch-tree-get-query)))
        (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
          (notmuch-tree-close-message-window)
          (notmuch-tree (string-replace jao-notmuch--newa "" query)))))

    (defun jao-notmuch-widen-searches (searches)
      (mapcar (lambda (s)
                (let ((q (plist-get s :query)))
                  (plist-put (copy-sequence s)
                             :query (string-replace jao-notmuch--newa "" q))))
              searches))

    (defvar jao-notmuch-widened-searches
      (jao-notmuch-widen-searches notmuch-saved-searches))

    (defun jao-notmuch-jump-search (&optional widen)
      (interactive "P")
      (let ((notmuch-saved-searches
             (if widen jao-notmuch-widened-searches notmuch-saved-searches)))
        (notmuch-jump-search)))

    (setq nnnm-saved-searches
          (mapcar (lambda (s)
                    (list :query (plist-get s :query) :name (plist-get s :name)))
                  jao-notmuch-widened-searches))

  #+end_src
* enclosures
  #+begin_src emacs-lisp
    (with-eval-after-load "notmuch-show"
      (defun jao-notmuch-open-enclosure (add)
        (interactive "P")
        (with-current-notmuch-show-message
         (goto-char (point-min))
         (if (not (search-forward "Enclosure:" nil t))
             (user-error "No enclosure in message body")
           (re-search-forward "https?://" nil t)
           (if-let (url (thing-at-point-url-at-point))
               (progn
                 (message "%s %s ..." (if add "Adding" "Playing") url)
                 (unless add (jao-mpc-clear))
                 (jao-mpc-add-url url)
                 (unless add (jao-mpc-play)))
             (error "Found an enclosure, but not a link!"))))))
  #+end_src
* package
  #+begin_src emacs-lisp
    (if (< emacs-major-version 28)
      (jao-load-path "notmuch")
      (add-to-list 'load-path "/usr/local/share/emacs/site-lisp/"))

    (use-package notmuch
      :init
      (setq notmuch-draft-folder "jao/drafts"
            notmuch-draft-quoted-tags '("part")
            notmuch-column-control t
            notmuch-hello-sections '(jao-notmuch-hello-insert-searches
                                     notmuch-hello-insert-alltags
                                     notmuch-hello-insert-header)
            notmuch-hello-thousands-separator ","
            notmuch-hello-auto-refresh t
            notmuch-show-all-tags-list t
            notmuch-show-logo nil
            notmuch-show-empty-saved-searches nil
            notmuch-show-only-matching-messages t
            notmuch-show-part-button-default-action 'notmuch-show-view-part
            notmuch-show-mark-read-tags '("-new" "-unread")
            notmuch-archive-tags '("+trove" "-new")
            notmuch-address-internal-completion '(received nil)
            notmuch-fcc-dirs
            '(("\\(support\\|education\\)@bigml.com" . nil)
              (".*@bigml.com" . "bigml/sent +bigml +sent -new")
              (".*" . "jao/trove +jao +sent +trove -new"))
            notmuch-maildir-use-notmuch-insert t
            notmuch-message-headers '("Subject" "To" "Cc" "Date" "List-Id")
            notmuch-wash-signature-lines-max 0
            notmuch-wash-wrap-lines-length 80
            notmuch-wash-citation-lines-prefix 10
            notmuch-wash-citation-lines-suffix 20)

      (setq gnus-blocked-images ".")
      (setq notmuch-show-text/html-blocked-images ".")

      :config

      (defun jao-notmuch-hello-insert-searches ()
        (when-let (searches (notmuch-hello-query-counts notmuch-saved-searches))
          (widget-insert "Saved searches:\n\n")
          (let ((start (point)))
            (let ((notmuch-column-control t))
              (notmuch-hello-insert-buttons searches))
	    (indent-rigidly start (point) notmuch-hello-indent))))

      (when (eq 'notmuch jao-afio-mail-function)
        (setq message-directory "~/var/mail/"
              message-auto-save-directory "/tmp")
        (with-eval-after-load "notmuch-message"
          (define-key message-mode-map (kbd "C-c C-d")
            #'notmuch-draft-postpone)))

      (defconst jao-mail-clean-rx
        (regexp-opt '("ElDiario.es - ElDiario.es: " "The Guardian: "
                      "The Conversation – Articles (UK): ")))

      (defun jao-mail-clean-address (fun address)
        (let ((address
               (if (string-match ".+ updates on arXiv.org: \\(.+\\)" address)
                   (with-temp-buffer
                     (insert (match-string 1 address))
                     (let ((shr-width 1000))
                       (shr-render-region (point-min) (point-max)))
                     (replace-regexp-in-string "\"" "" (buffer-string)))
                 (replace-regexp-in-string jao-mail-clean-rx "" address))))
          (funcall fun address)))

      (advice-add 'notmuch-clean-address :around #'jao-mail-clean-address)

      (defun jao-notmuch-refresh-agenda ()
        (interactive)
        (save-window-excursion (org-agenda-list))
        (with-current-buffer "*Calendar*" (calendar-goto-today)))

      (defun jao-notmuch-hello-first ()
        (interactive)
        (let ((inhibit-message t))
          (beginning-of-buffer)
          (widget-forward 1)))

      (defun jao-notmuch-refresh-hello ()
        (interactive)
        (ignore-errors
          (when (and (string= "Mail" (jao-afio-current-frame))
                     (derived-mode-p 'notmuch-hello-mode))
            (when (not (string-blank-p jao-notmuch-minibuffer-string))
              (let ((notmuch-hello-auto-refresh nil)) (notmuch-hello)))
            (jao-notmuch-hello-first))))

      :hook ((notmuch-hello-refresh . jao-notmuch-notify)
             (jao-afio-switch . jao-notmuch-refresh-hello))

      :bind (:map notmuch-hello-mode-map
             (("a" . jao-notmuch-refresh-agenda)
              ("j" . jao-notmuch-jump-search)
              ("n" . widget-forward)
              ("p" . widget-backward)
              ("S" . consult-notmuch)
              ("g" . jao-notmuch-refresh-hello)
              ("." . jao-notmuch-hello-first)
              ("SPC" . widget-button-press))
             :map notmuch-common-keymap
             (("E" . jao-notmuch-open-enclosure))))

    (use-package jao-notmuch
      :demand t
      :config (setq jao-notmuch-mailboxes (jao-mailbox-folders)))

  #+end_src
* search/tree view
  #+begin_src emacs-lisp
    (use-package notmuch-tree
      :config

      (let ((fg (face-attribute 'jao-themes-dimm :foreground)))
        (dolist (f '(notmuch-tree-match-tree-face
                     notmuch-tree-no-match-tree-face))
          (set-face-attribute f nil :family "Fira Code" :foreground fg)))

      (defun jao-notmuch--format-field (fun field &rest args)
        (let ((rs (apply fun field args)))
          (cond ((and (stringp field) (string= field "tree"))
                 (replace-regexp-in-string "►" "" rs)) ;; "→"
                ((not (stringp field)) (truncate-string-to-width rs 100))
                (t rs))))

      (advice-add 'notmuch-tree-format-field
                  :around #'jao-notmuch--format-field)

      (defun jao-notmuch-show-setup ()
        (setq header-line-format nil))

      (add-hook 'notmuch-show-hook #'jao-notmuch-show-setup)
      (jao-notmuch-show-prefer-html)

      (defun jao-notmuch-tree-setup ()
        (setq-local scroll-preserve-screen-position nil
                    scroll-margin 2
                    scroll-step 1))

      (define-key notmuch-tree-mode-map (kbd "T") notmuch-tree-thread-map)

      :bind (:map notmuch-tree-mode-map
                  (("C" . jao-notmuch-echo-count)
                   ("d" . jao-notmuch-tree-delete-message)
                   ("D" . jao-notmuch-tree-delete-thread)
                   ("h" . jao-notmuch-goto-message-buffer)
                   ("H" . jao-notmuch-click-message-buffer)
                   ("i" . jao-notmuch-show-images)
                   ("K" . jao-notmuch-tag-jump-and-next)
                   ("k" . jao-notmuch-tree-read-thread)
                   ("M" . jao-notmuch-move-message)
                   ("n" . jao-notmuch-tree-next)
                   ("O" . notmuch-tree-toggle-order)
                   ("o" . jao-notmuch-tree-widen-search)
                   ("p" . jao-notmuch-tree-previous)
                   ("s" . jao-notmuch-tree-spam)
                   ("u" . jao-notmuch-tree-flag)
                   ("v" . notmuch-tree-scroll-message-window)
                   ("V" . notmuch-tree-scroll-message-window-back)
                   ("<" . jao-notmuch-tree-beginning-of-buffer)
                   (">" . jao-notmuch-tree-end-of-buffer)
                   ("/" . notmuch-tree-view-raw-message)
                   ("." . jao-notmuch-toggle-mime-parts)
                   ("=" . jao-notmuch-tree-toggle-message)
                   ("RET" . jao-notmuch-tree-show-or-scroll)
                   ("SPC" . jao-notmuch-tree-scroll-or-next))
                  :map notmuch-search-mode-map
                  (("RET" . notmuch-tree-from-search-thread)
                   ("M-RET" . notmuch-search-show-thread))
                  :map notmuch-show-mode-map
                  (("h" . jao-notmuch-goto-tree-buffer)
                   ("TAB" . jao-notmuch-show-next-button)
                   ("S-TAB" . jao-notmuch-show-previous-button)
                   ("RET" . jao-notmuch-show-ret))
                  :map notmuch-common-keymap
                  (("B" . jao-notmuch-browse-urls))))
  #+end_src
* notmuch-addr
  #+begin_src emacs-lisp
    (when (jao-load-path "notmuch-addr")
      (with-eval-after-load 'notmuch-address
        (jao-corfu-no-auto notmuch-message)
        (defun jao-notmuch-message-corfu-setup ()
          (setq-local corfu-quit-no-match nil
                      corfu-commit-predicate t
                      orderless-component-separator " +"))
        (add-hook 'notmuch-message-mode-hook #'jao-notmuch-message-corfu-setup)
        (require 'notmuch-addr)
        (notmuch-addr-setup)))
  #+end_src
* hydras
  #+begin_src emacs-lisp
    (major-mode-hydra-define notmuch-search-mode nil
      ("Tagging"
       (("*" notmuch-search-tag-all "tag all")
        ("+" notmuch-search-add-tag "add tag")
        ("-" notmuch-search-remove-tag "add tag")
        ("k" notmuch-tag-jump "jump to tag"))
       "Search"
       (("l" notmuch-search-filter "filter with additional query")
        ("t" notmuch-search-filter-by-tag "filter by tag")
        ("y" notmuch-stash-query "stash current query"))
       "Moving around"
       (("b" notmuch-search-scroll-down "scroll down")
        ("<" notmuch-search-first-thread "first thread")
        (">" notmuch-search-last-thread "last thread"))))

    (major-mode-hydra-define notmuch-tree-mode nil
      ("View"
       (("." jao-notmuch-toggle-mime-parts "toggle mime parts")
        ("i" jao-notmuch-show-images "toggle images")
        ("a" notmuch-tree-archive-thread-then-next "archive thread")
        ("C" jao-notmuch-echo-count "echo unread count"))
       "Mark"
       (("d" jao-notmuch-tree-delete-message "delete message")
        ("D" jao-notmuch-tree-delete-thread "delete thread")
        ("u" (jao-notmuch-tree-delete-message t) "undelete message")
        ("k" jao-notmuch-tree-read-thread "kill thread"))
       "Edit/send"
       (("r" notmuch-tree-reply-sender "reply sender")
        ("R" notmuch-tree-reply "reply all")
        ("f" notmuch-tree-forward-message "forward")
        ("e" notmuch-tree-resume-message "edit draft"))))
  #+end_src
* consult
  #+begin_src emacs-lisp
    (jao-load-path "consult-notmuch")
    (setq consult-notmuch-authors-width 30)
    (require 'consult-notmuch)
    (consult-customize consult-notmuch :preview-key 'any)

    (defvar jao-consult-notmuch-history nil)

    (defun jao-consult-notmuch-folder (&optional tree folder)
      (interactive "P")
      (let* ((root "~/var/mail/")
             (folder (if folder
                         (file-name-as-directory folder)
                       (completing-read "Folder: "
                                        jao-mailbox-folders
                                        nil nil nil
                                        jao-consult-notmuch-history
                                        ".")))
             (folder (replace-regexp-in-string "/\\(.\\)" ".\\1" folder))
             (init (read-string "Initial query: "))
             (init (format "folder:/%s/ %s" folder init)))
        (if tree (consult-notmuch-tree init) (consult-notmuch init))))

    (with-eval-after-load "notmuch-hello"
      (define-key notmuch-hello-mode-map "f" #'jao-consult-notmuch-folder))
  #+end_src
* org mode
    Stolen and adapted from [[https://gist.github.com/fedxa/fac592424473f1b70ea489cc64e08911][Fedor Bezrukov]].
    #+begin_src emacs-lisp
      (with-eval-after-load "org"
        (with-eval-after-load "notmuch"
          (org-link-set-parameters "notmuch"
                                   :follow 'org-notmuch-open
                                   :store 'org-notmuch-store-link)

         (defun org-notmuch-open (id)
           "Visit the notmuch message or thread with id ID."
           (notmuch-show id))

         (defun org-notmuch-store-link ()
           "Store a link to a notmuch mail message."
           (case major-mode
             ((notmuch-show-mode notmuch-tree-mode)
              ;; Store link to the current message
              (let* ((id (notmuch-show-get-message-id))
                     (link (concat "notmuch:" id))
                     (description (format "Mail: %s"
                                          (notmuch-show-get-subject))))
                (org-store-link-props
	         :type "notmuch"
	         :link link
	         :description description)))
             (notmuch-search-mode
              ;; Store link to the thread on the current line
              (let* ((id (notmuch-search-find-thread-id))
                     (link (concat "notmuch:" id))
                     (description (format "Mail: %s"
                                          (notmuch-search-find-subject))))
                (org-store-link-props
                 :type "notmuch"
                 :link link
                 :description description)))))))
    #+end_src