summaryrefslogtreecommitdiffhomepage
path: root/lib/net/jao-notmuch.el
blob: f0659f6f5f9b25b97d3d128e4798bc16eac72436 (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
;;; jao-notmuch.el --- Extensions for notmuch        -*- lexical-binding: t; -*-

;; Copyright (C) 2021  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 'eww)

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


;; Moving messages around

(defvar jao-notmuch-mailboxes nil)
(defvar jao-notmuch-mailboxes-rx nil)

(defun jao-notmuch--path-to-mailbox (full-path)
  (unless jao-notmuch-mailboxes-rx
    (setq jao-notmuch-mailboxes-rx (regexp-opt jao-notmuch-mailboxes)))
  (if (string-match jao-notmuch-mailboxes-rx full-path)
      (match-string 0 full-path)
    (user-error "Message not in any registered mailbox!")))

(defun jao-notmuch--msg-props ()
  (if-let ((p (save-excursion
                (beginning-of-line)
                (text-property-search-forward :notmuch-message-properties))))
      (prop-match-value p)
    (user-error "No message at point")))

(defun jao-notmuch--full-path ()
  (seq-find #'file-exists-p (plist-get (jao-notmuch--msg-props) :filename)))

(defun jao-notmuch--move (&optional full-path d)
  (let* ((full-path (or full-path (jao-notmuch--full-path)))
         (ff (jao-notmuch--path-to-mailbox full-path))
         (d (or d (completing-read (format "From %s to: " ff)
                                   (remove ff jao-notmuch-mailboxes) nil t)))
         (dest (string-replace ff d full-path))
         (dest (replace-regexp-in-string ",U=.+$" "m:2,S" dest))
         (ftags (split-string ff "/"))
         (ttags (split-string d "/")))
    (when (y-or-n-p (format "%s -> %s? " ftags ttags))
      (notmuch-tree-close-message-window)
      (notmuch-tree-tag (append (notmuch-tag-change-list ftags t)
                                (notmuch-tag-change-list ttags)))
      (rename-file (jao-notmuch--full-path) dest)
      (shell-command-to-string "notmuch new")
      (notmuch-refresh-this-buffer))))

(defun jao-notmuch-move-message ()
  "Move message at point to another folder."
  (interactive)
  (jao-notmuch--move))


;; Targetting the displayed message from the tree view

(defvar-local jao-notmuch--tree-buffer nil)

(defun jao-notmuch-goto-message-buffer (&optional and-click)
  (interactive "P")
  (when (window-live-p notmuch-tree-message-window)
    (let ((b (current-buffer)))
      (select-window notmuch-tree-message-window)
      (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-click-message-buffer ()
  (interactive)
  (let ((b (current-buffer)))
    (unless (eq 'url (jao-notmuch-goto-message-buffer t))
      (pop-to-buffer b))))

(defun jao-notmuch-goto-tree-buffer ()
  (interactive)
  (if (buffer-live-p jao-notmuch--tree-buffer)
      (pop-to-buffer jao-notmuch--tree-buffer)
    (user-error "No tree view for this buffer")))

(defun jao-notmuch-browse-urls ()
  (interactive)
  (when (or (derived-mode-p 'notmuch-show-mode)
            (jao-notmuch-goto-message-buffer))
    (notmuch-show-browse-urls)))

(defun jao-notmuch-toggle-mime-parts ()
  (interactive)
  (when (jao-notmuch-goto-message-buffer)
    (goto-char (point-min))
    (let ((notmuch-show-text/html-blocked-images nil)
          (shr-inhibit-images nil)
          (shr-blocked-images nil))
      (save-excursion
        (when (re-search-forward "\\[ multipart/alternative \\]" nil t)
          (while (forward-button 1 nil nil t)
            (notmuch-show-toggle-part-invisibility)))))
    (jao-notmuch-goto-tree-buffer)))

(defun jao-notmuch-toggle-images ()
  (interactive)
  (save-window-excursion
    (jao-notmuch-goto-message-buffer)
    (when (derived-mode-p 'notmuch-show-mode)
      (let ((notmuch-show-text/html-blocked-images nil)
            (shr-inhibit-images nil)
            (shr-blocked-images nil))
        (notmuch-refresh-this-buffer)))))


;; Keeping track of unread messages in current tree view

(defvar-local jao-notmuch--query-name nil)

(defun jao-notmuch--query-name ()
  (when notmuch-tree-basic-query
    (or jao-notmuch--query-name
        (setq jao-notmuch--query-name
              (let ((q (seq-find (lambda (q)
                                   (string= (or (plist-get q :query) "")
                                            notmuch-tree-basic-query))
                                 notmuch-saved-searches)))
                (or (plist-get q :name)  notmuch-tree-basic-query))))))

(defun jao-notmuch--unread-count ()
  (save-excursion
    (goto-char (point-min))
    (let ((p) (cnt) (total))
      (while (setq p (text-property-search-forward :notmuch-message-properties))
        (unless cnt (setq cnt 0 total 0))
        (let ((tags (plist-get (prop-match-value p) :tags)))
          (setq total (1+ total))
          (when (or (member "unread" tags) (member "new" tags))
            (setq cnt (1+ cnt)))))
      (when cnt (format "%s out of %s messages left" cnt total)))))

(defun jao-notmuch--tree-update-buffer-name (&optional n)
  (when-let ((n (or n (jao-notmuch--unread-count))))
    (rename-buffer (format "{%s - %s}" (jao-notmuch--query-name) n))
    (jao-minibuffer-refresh)))

(defun jao-notmuch--tree-sentinel (proc &rest _)
  (let ((buffer (process-buffer proc))
        (status (process-status proc)))
    (when (and (eq status 'exit) (buffer-live-p buffer))
      (with-current-buffer buffer (jao-notmuch--tree-update-buffer-name)))))

(advice-add 'notmuch-tree-process-sentinel :after #'jao-notmuch--tree-sentinel)

(defun jao-notmuch-echo-count ()
  "Show the number of unread messages left in this tree view."
  (interactive)
  (when-let ((n (jao-notmuch--unread-count)))
    (jao-notmuch--tree-update-buffer-name n)
    (message n)))

(defun jao-notmuch-tree-next (thread &optional no-exit)
  "Next message or thread in forest or exit if none."
  (interactive "P")
  (if thread
      (progn (notmuch-tree-next-thread)
             (notmuch-tree-show-message nil))
    (notmuch-tree-next-matching-message (not no-exit)))
  (jao-notmuch--tree-update-buffer-name))

;; Tagging

(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-and-next (tags reverse whole-thread)
  (let ((c (notmuch-tag-change-list tags reverse)))
    (if whole-thread (notmuch-tree-tag-thread c) (notmuch-tree-tag c)))
  (jao-notmuch-tree-next whole-thread t))

(defun jao-notmuch-tree-delete-message (undelete)
  (interactive "P")
  (jao-notmuch-tree--tag-and-next '("+deleted" "-new" "-unread") undelete nil))

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

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

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

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

;; Scrolling the shown message

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

(defun jao-notmuch-tree-show-or-scroll ()
  "Show current message, or scroll it if visible."
  (interactive)
  (if (window-live-p notmuch-tree-message-window)
      (scroll-other-window 1)
    (notmuch-tree-show-message nil)
    (jao-notmuch--tree-update-buffer-name)))


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