summaryrefslogtreecommitdiffhomepage
path: root/lib/net/jao-notmuch.el
blob: fa33b37002650a0510275e3a61c82d304d80ae43 (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
438
439
440
441
442
443
;;; jao-notmuch.el --- Extensions for notmuch        -*- lexical-binding: t; -*-

;; Copyright (C) 2021, 2022  jao

;; Author: jao <mail@jao.io>
;; Keywords: mail

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;  Extensions to vanilla notmuch, mostly for tree view

;;; Code:

(require 'outline)
(require 'mm-decode)
(require 'mm-view)

(require 'notmuch)
(require 'notmuch-tree)
(require 'notmuch-show)


;;;; Targetting the displayed message from the tree view

(defvar-local jao-notmuch--tree-buffer nil)
(declare eww--url-at-point "eww")

(defun jao-notmuch-goto-message-buffer (&optional and-click no-record)
  (interactive "P")
  (when (window-live-p notmuch-tree-message-window)
    (let ((b (current-buffer)))
      (select-window notmuch-tree-message-window no-record)
      (setq-local jao-notmuch--tree-buffer b)
      (or (not and-click)
          (cond ((eww--url-at-point) (shr-browse-url) 'url)
                ((button-at (point)) (push-button) 'button))))))

(defun jao-notmuch-tree-toggle-message ()
  (interactive)
  (if (window-live-p notmuch-tree-message-window)
      (notmuch-tree-close-message-window)
    (notmuch-tree-show-message nil)))

(defun jao-notmuch-click-message-buffer ()
  (interactive)
  (let ((b (current-buffer)))
    (unless (eq 'url (jao-notmuch-goto-message-buffer t t))
      (pop-to-buffer b))))

(defun jao-notmuch-tree--find-tree-buffer ()
  (or jao-notmuch--tree-buffer
      (let ((mb (current-buffer)))
        (seq-find (lambda (b)
                    (with-current-buffer b
                      (and (derived-mode-p 'notmuch-tree-mode)
                           (eq notmuch-tree-message-buffer mb))))
                  (buffer-list)))))

(defun jao-notmuch-goto-tree-buffer (&optional no-record)
  (interactive)
  (setq jao-notmuch--tree-buffer (jao-notmuch-tree--find-tree-buffer))
  (if (buffer-live-p jao-notmuch--tree-buffer)
      (pop-to-buffer jao-notmuch--tree-buffer nil no-record)
    (user-error "No tree view for this buffer")))

(defun jao-notmuch-tree--looking-at-message ()
  (when-let (id (plist-get (notmuch-tree-get-message-properties) :id))
    (equal (concat "*notmuch-id:" id "*")
           (buffer-name (window-buffer notmuch-tree-message-window)))))

(defun jao-notmuch-tree-scroll-or-next ()
  "Scroll or next message in forest or exit if none."
  (interactive)
  (if (not (jao-notmuch-tree--looking-at-message))
      (jao-notmuch-tree-show-or-scroll t)
    (if (notmuch-tree-scroll-message-window)
        (jao-notmuch-tree-next nil)
      (when (not (window-live-p notmuch-tree-message-window))
        (notmuch-tree-show-message nil)))))

(defun jao-notmuch-tree-show-or-scroll (force)
  "Show current message, or scroll it if visible."
  (interactive "P")
  (if (and (not force)
           (window-live-p notmuch-tree-message-window)
           (jao-notmuch-tree--looking-at-message))
      (scroll-other-window 1)
    (notmuch-tree-show-message nil)))

(notmuch-tree--define-do-in-message-window
 jao-notmuch-tree-end-of-buffer
 end-of-buffer)

(notmuch-tree--define-do-in-message-window
 jao-notmuch-tree-beginning-of-buffer
 beginning-of-buffer)

(defun jao-notmuch--page-urls (res)
  (save-excursion
    (goto-char (point-min))
    (let ((pos (point)))
      (while (setq pos (next-single-property-change pos 'w3m-href-anchor))
        (when-let ((url (get-text-property pos 'w3m-href-anchor)))
          (when (stringp url) (cl-pushnew url res :test #'string=))))
      (seq-uniq res #'string=))))

(defun jao-notmuch-message-urls ()
  (save-window-excursion
    (when (or (derived-mode-p 'notmuch-show-mode)
              (jao-notmuch-goto-message-buffer))
      (jao-notmuch--page-urls (notmuch-show--gather-urls)))))

(defun jao-notmuch-browse-urls (&optional external)
  (interactive "P")
  (if-let ((urls (jao-notmuch-message-urls)))
      (funcall (if external browse-url-secondary-browser-function #'browse-url)
               (completing-read "Browse URL: " urls))
    (message "No URLs in this message")))


;;;; Navigating URLs

(require 'ffap)

(defun jao-notmuch-show-next-button ()
  (interactive)
  (when (get-text-property (point) 'w3m-href-anchor)
    (goto-char (next-single-property-change (point) 'w3m-href-anchor)))
  (if-let (pos (next-single-property-change (point) 'w3m-href-anchor))
      (goto-char pos)
    (or (forward-button 1 nil t t)
        (ffap-next-guess))))

(defun jao-notmuch-show-previous-button ()
  (interactive)
  (if-let (pos (previous-single-property-change (point) 'w3m-href-anchor))
      (goto-char (previous-single-property-change pos 'w3m-href-anchor))
    (or (backward-button 1 nil t t)
        (ffap-next-guess t))))

(defun jao-notmuch-show-ret ()
  (interactive)
  (when-let (url (or (get-text-property (point) 'w3m-href-anchor)
                     (thing-at-point-url-at-point)))
    (browse-url url)))


;;;; Toggling mime parts and images

(defun jao-notmuch--toggle-mime ()
  (save-excursion
    (goto-char (point-min))
    (while (and (re-search-forward "^\\[ text/\\(html\\|plain\\) " nil t)
                (button-at (point)))
      (notmuch-show-toggle-part-invisibility))))

(defun jao-notmuch--show-hidden-html ()
  (when (save-excursion
          (goto-char (point-min))
          (re-search-forward "^\\[ text/html (hidden) \\]" nil t))
    (jao-notmuch--toggle-mime)))

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

(defun jao-notmuch-toggle-mime-parts ()
  (interactive)
  (when (jao-notmuch-goto-message-buffer nil t)
    (goto-char (point-min))
    (jao-notmuch--toggle-mime)
    (jao-notmuch-goto-tree-buffer t)))

(defvar-local jao-notmuch--showing-images nil)

(defun jao-notmuch--setup-w3m-images (&optional activate)
  (when (eq mm-text-html-renderer 'w3m)
    (setq-local w3m-ignored-image-url-regexp
                (unless jao-notmuch--showing-images
                  notmuch-show-text/html-blocked-images))
    (when activate
      (setq-local scroll-margin 0)
      (w3m-toggle-inline-images (if jao-notmuch--showing-images t 'turnoff)))))

(defun jao-notmuch--w3m-toggle-images ()
  (save-window-excursion
    (when (or (derived-mode-p 'notmuch-show-mode)
              (jao-notmuch-goto-message-buffer nil t))
      (goto-char (point-min))
      (when (re-search-forward "^\\[ text/html " nil t)
        (when (looking-at-p "(hidden)")
          (notmuch-show-toggle-part-invisibility))
        (forward-line 1)
        (setq jao-notmuch--showing-images (not jao-notmuch--showing-images))
        (jao-notmuch--setup-w3m-images t)))))

(defun jao-notmuch--shr-toggle-images ()
  (notmuch-tree-close-message-window)
  (let ((mm-text-html-renderer 'shr)
        (shr-blocked-images nil)
        (shr-inhibit-images nil)
        (notmuch-show-text/html-blocked-images nil))
    (notmuch-tree-show-message nil)))

(defun jao-notmuch-show-images ()
  (interactive)
  (if (eq mm-text-html-renderer 'w3m)
      (jao-notmuch--w3m-toggle-images)
    (jao-notmuch--shr-toggle-images)))

(add-hook 'notmuch-show-mode-hook #'jao-notmuch--setup-w3m-images)


;;;; Keeping track of unread messages in current tree view

(defun jao-notmuch--looking-at-new-p (&optional p)
  (when-let (ts (if p (plist-get p :tags) (notmuch-show-get-tags)))
    (or (member "unread" ts) (member "new" ts))))

(defsubst jao-notmuch-tree--first-p (&optional msg)
  (plist-get (or msg (notmuch-tree-get-message-properties)) :first))

(defun jao-notmuch--message-counts (&optional thread)
  (let ((cnt) (total 0) (match 0) (msg))
    (save-excursion
      (if thread
          (while (and (not (jao-notmuch-tree--first-p))
                      (zerop (forward-line -1))))
        (goto-char (point-min)))
      (while (and (setq msg (notmuch-tree-get-message-properties))
                  (or (not cnt)
                      (not thread)
                      (not (jao-notmuch-tree--first-p msg))))
        (unless cnt (setq cnt 0))
        (setq total (1+ total))
        (when (plist-get msg :match) (setq match (1+ match)))
        (when (jao-notmuch--looking-at-new-p msg) (setq cnt (1+ cnt)))
        (forward-line 1)))
    (when cnt (list total match cnt))))

(defvar jao-notmuch-header-line-format "%S  -  %Q  [%N / %M / %T] %n / %m / %t")

(defun jao-notmuch--format-counts (sb query total match new ttotal tmatch tnew)
  (format-spec jao-notmuch-header-line-format
               `((?S . ,sb) (?Q . ,query) (?T . ,total) (?N . ,new) (?M . ,match)
                 (?t . ,ttotal) (?n . ,tnew) (?m . ,tmatch))))

(defun jao-notmuch--update-header-line (mb)
  (let* ((n (or (jao-notmuch--message-counts) '(0 0 0)))
         (nc (append n (or (jao-notmuch--message-counts t) '(0 0 0))))
         (q (buffer-name)))
    (with-current-buffer mb
      (let ((s (thread-last (notmuch-show-get-subject)
                            (notmuch-show-strip-re)
                            (notmuch-sanitize))))
        (setq-local header-line-format
                    (apply 'jao-notmuch--format-counts s q nc))))))

(defun jao-notmuch-tree--find-update-header-line (&rest _args)
  (when-let ((mb (if (derived-mode-p 'notmuch-show-mode)
                     (current-buffer)
                   (window-buffer notmuch-tree-message-window))))
    (seq-find (lambda (b)
                (with-current-buffer b
                  (and (derived-mode-p 'notmuch-tree-mode)
                       (or (null notmuch-tree-message-buffer)
                           (eq notmuch-tree-message-buffer mb))
                       (jao-notmuch--update-header-line mb))))
              (buffer-list))))

(add-hook 'notmuch-after-tag-hook #'jao-notmuch-tree--find-update-header-line)
(add-hook 'notmuch-show-hook #'jao-notmuch-tree--find-update-header-line)


;;;; Outline mode for tree view

(defun jao-notmuch-tree--msg-prefix (msg)
  (insert (propertize (if (plist-get msg :first) "> " "  ") 'display "")))

(defun jao-notmuch-tree--mode-setup ()
  (setq-local outline-regexp "^> \\|^En")
  (outline-minor-mode t))

(defun jao-notmuch-tree-hide-others (&optional and-show)
  (interactive)
  (outline-hide-body)
  (outline-show-entry)
  (when and-show (notmuch-tree-show-message nil)))

(defun jao-notmuch-tree--next (prev thread no-exit &optional ignore-new)
  (let ((line-move-ignore-invisible nil))
    (cond ((and (not ignore-new) (jao-notmuch--looking-at-new-p)))
          (thread
           (notmuch-tree-next-thread prev)
           (unless (or (not (notmuch-tree-get-message-properties))
                       (jao-notmuch--looking-at-new-p))
             (notmuch-tree-matching-message prev (not no-exit))))
          (t (notmuch-tree-matching-message prev (not no-exit)))))
  (when (notmuch-tree-get-message-id)
    (jao-notmuch-tree-hide-others t))
  (when prev (forward-char 2)))

(defvar jao-notmuch-tree--prefix-map
  (let ((m (make-keymap "Thread operations")))
    (define-key m (kbd "TAB") #'outline-cycle)
    (define-key m (kbd "t") #'outline-toggle-children)
    (define-key m (kbd "s") #'outline-show-entry)
    (define-key m (kbd "S") #'outline-show-all)
    (define-key m (kbd "h") #'outline-hide-entry)
    (define-key m (kbd "H") #'outline-hide-body)
    (define-key m (kbd "o") #'jao-notmuch-tree-hide-others)
    (define-key m (kbd "n") #'outline-hide-other)
    m))

(defun jao-notmuch-tree-outline-setup (&optional prefix)
  (define-key notmuch-tree-mode-map (kbd (or prefix "T"))
    jao-notmuch-tree--prefix-map)
  (define-key notmuch-tree-mode-map (kbd "TAB") #'outline-cycle)
  (define-key notmuch-tree-mode-map (kbd "M-TAB") #'outline-cycle-buffer)
  (add-hook 'notmuch-tree-mode-hook #'jao-notmuch-tree--mode-setup)
  (advice-add 'notmuch-tree-insert-msg :before #'jao-notmuch-tree--msg-prefix))

(defun jao-notmuch-tree-next (thread &optional no-exit)
  "Next message or thread in forest, taking care of thread visibility."
  (interactive "P")
  (jao-notmuch-tree--next nil thread no-exit))

(defun jao-notmuch-tree-next-thread (&optional exit)
  "Next thread in forest, taking care of thread visibility."
  (interactive "P")
  (jao-notmuch-tree--next nil t exit))

(defun jao-notmuch-tree-previous (thread)
  "Previous message or thread in forest, taking care of thread visibility."
  (interactive "P")
  (jao-notmuch-tree--next t thread t))

(defun jao-notmuch-tree-previous-thread (&optional exit)
  "Previous thread in forest, taking care of thread visibility."
  (interactive "P")
  (jao-notmuch-tree--next t t exit))


;;;; Updating the tree window after insertion

(defun jao-notmuch--tree-sentinel (proc &rest _)
  (when (eq (process-status proc) 'exit)
    (jao-notmuch-tree-hide-others)))

(defun jao-notmuch-tree-setup (&optional prefix)
  "Set up display of trees, with PREFIX key for outline commands."
  (jao-notmuch-tree-outline-setup prefix)
  (advice-add 'notmuch-tree-process-sentinel :after #'jao-notmuch--tree-sentinel))


;;;; Tagging

(defsubst jao-notmuch--has-tag (tag)
  (member tag (notmuch-tree-get-tags)))

(defun jao-notmuch-tag-jump-and-next (reverse)
  (interactive "P")
  (notmuch-tag-jump reverse)
  (jao-notmuch-tree-next nil t))

(defun jao-notmuch-tree--tag (tags reverse whole-thread)
  (let ((c (notmuch-tag-change-list tags reverse)))
    (if whole-thread (notmuch-tree-tag-thread c) (notmuch-tree-tag c))))

(defun jao-notmuch-tree--tag-and-next (tags reverse whole-thread)
  (jao-notmuch-tree--tag tags reverse whole-thread)
  (jao-notmuch-tree-next whole-thread t))

(defun jao-notmuch-tree-toggle-delete ()
  (interactive)
  (let ((undo (jao-notmuch--has-tag "deleted")))
    (jao-notmuch-tree--tag-and-next '("+deleted" "-new" "-unread") undo nil)))

(defun jao-notmuch-tree-toggle-delete-thread ()
  (interactive)
  (let ((undo (jao-notmuch--has-tag "deleted")))
    (jao-notmuch-tree--tag-and-next '("+deleted" "-new"  "-unread") undo t)))

(defun jao-notmuch-tree-read-thread (unread)
  (interactive "P")
  (jao-notmuch-tree--tag-and-next '("-unread" "-new") unread t))

(defun jao-notmuch-tree-toggle-flag ()
  (interactive)
  (let ((tags (if (jao-notmuch--has-tag "flagged")
                  '("-flagged")
                '("-unread" "-new" "-deleted" "+flagged"))))
    (jao-notmuch-tree--tag-and-next tags nil nil)))

(defun jao-notmuch-tree-toggle-spam ()
  (interactive)
  (let ((tags (if (jao-notmuch--has-tag "spam")
                  '("-spam")
                '("-unread" "-new" "+spam"))))
    (jao-notmuch-tree--tag-and-next tags nil nil)))

(defun jao-notmuch-tree-reset-tags ()
  (interactive)
  (let ((tags (plist-get (notmuch-tree-get-message-properties) :orig-tags)))
    (jao-notmuch-tree--tag tags nil nil)
    (jao-notmuch-tree--next nil nil t t)))


;;;; Results formatters

(defun jao-notmuch-format-tags (fmt msg)
  (let ((ts (thread-last (notmuch-tree-format-field "tags" "%s" msg)
              (split-string)
              (seq-sort-by #'length #'<))))
    (format-spec fmt `((?s . ,(mapconcat #'identity ts " "))))))

(defun jao-notmuch-tree-and-subject (fmt msg)
  (let ((tr (notmuch-tree-format-field "tree" " %s" msg))
        (sb (notmuch-tree-format-field "subject" " %s" msg)))
    (format-spec fmt `((?s . ,(concat tr sb))))))

(defun jao-notmuch-msg-ticks (mails-rx msg)
  (let ((headers (plist-get msg :headers)))
    (cond ((string-match-p mails-rx (or (plist-get headers :To) ""))
           (propertize " »" 'face 'notmuch-tree-match-tree-face))
          ((string-match-p mails-rx (or (plist-get headers :Cc) ""))
           (propertize " ¬" 'face 'notmuch-tree-match-tree-face))
          (t "  "))))

(provide 'jao-notmuch)
;;; jao-notmuch.el ends here