-
Notifications
You must be signed in to change notification settings - Fork 384
/
mu4e-thread.el
295 lines (254 loc) · 10.4 KB
/
mu4e-thread.el
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
;;; mu4e-thread.el --- Thread folding support -*- lexical-binding: t -*-
;; Copyright (C) 2023 Nicolas P. Rougier
;; Author: Nicolas P. Rougier <Nicolas.Rougier@inria.fr>
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; mu4e-thread.el is a library that allows to fold and unfold threads in mu4e
;; headers mode. Folding works by creating an overlay over thread children that
;; display a summary (number of hidden messages and possibly number of unread
;; messages).
;; Folding is performed just-in-time such that it is quite fast to
;; fold/unfold threads. When a thread has unread messages, the folding stops at
;; the first unread message unless `mu4e-thread-fold-unread` has been set to t.
;; Similarly, when a thread has marked messages, the folding stops at the first
;; marked message.
;; Note, you can only use these functions when threads are available, roughly
;; when `mu4e-search-threads' in non-nil.
;;; Usage example:
;;
;; After a search, mu4e-thread-mode will be enable when threads
;; are available; so, to automatically sort them:
;; (add-hook 'mu4e-thread-mode-hook #'mu4e-thread-fold-apply-all)
;;; Code:
(require 'mu4e-vars)
(require 'mu4e-message)
(require 'mu4e-mark)
(defcustom mu4e-thread-fold-unread nil
"Whether to fold unread messages in a thread."
:type 'boolean
:group 'mu4e-headers)
(defcustom mu4e-thread-fold-single-children nil
"When non-nil, fold a thread even if there is only a single child.
Otherwise, do not not fold single children since would simply
hide the single child."
:type 'boolean
:group 'mu4e-headers)
(defface mu4e-thread-fold-face
`((t :inherit mu4e-highlight-face))
"Face for the information line of a folded thread."
:group 'mu4e-faces)
(defvar-local mu4e-thread--fold-status nil
"Global folding status.")
(defvar-local mu4e-thread--docids nil
"Thread list whose folding has been set individually.")
(defvar mu4e-headers-fields) ;; defined in mu4e-headers.el
(defun mu4e-thread-fold-info (count unread)
"Text to be displayed for a folded thread.
There are COUNT hidden and UNREAD messages overall."
(let ((size (+ 2 (apply #'+ (mapcar (lambda (item) (or (cdr item) 0))
mu4e-headers-fields))))
(msg (concat (format"[%d hidden messages%s]\n" count
(if (> unread 0)
(format ", %d unread" unread)
"")))))
(propertize (concat " " (make-string size ?•) " " msg))))
(defun mu4e-thread-message-folded-p ()
"Is point in a folded area?"
(when-let* ((overlay (mu4e-thread-is-folded))
(beg (overlay-start overlay))
(end (overlay-end overlay)))
(and (>= (point) beg) (< (point) end))))
(declare-function 'mu4e~headers-thread-root-p "mu4e-headers")
(defalias 'mu4e-thread-is-root 'mu4e~headers-thread-root-p)
(defun mu4e-thread-goto-root ()
"Go to the root of the current thread."
(interactive)
(goto-char (mu4e-thread-root))
(beginning-of-line))
(defun mu4e-thread-root ()
"Get the root of the current thread."
(interactive)
(let ((point))
(save-excursion
(while (and (not (bobp))
(not (mu4e-thread-is-root)))
(forward-line -1))
(setq point (point)))
point))
(declare-function 'mu4e-headers-prev-thread "mu4e-headers")
(declare-function 'mu4e-headers-next-thread "mu4e-headers")
(defalias 'mu4e-thread-goto-prev 'mu4e-headers-prev-thread)
(defalias 'mu4e-thread-goto-next 'mu4e-headers-next-thread)
(defun mu4e-thread-prev ()
"Get the root of the previous thread (if any)."
(save-excursion
(when (mu4e-thread-goto-prev)
(mu4e-thread-root))))
(defun mu4e-thread-next()
"Get the root of the next thread (if any)."
(save-excursion
(when (mu4e-thread-goto-next)
(mu4e-thread-root))))
(defun mu4e-thread-is-folded ()
"Test if thread at point is folded."
(interactive)
(let* ((thread-beg (mu4e-thread-root))
(thread-end (or (mu4e-thread-next) (point-max)))
(overlays (overlays-in thread-beg thread-end)))
(catch 'folded
(dolist (overlay overlays)
(when (overlay-get overlay 'mu4e-thread-folded)
(throw 'folded overlay))))))
(defun mu4e-thread-fold-toggle-all ()
"Toggle all threads folding unconditionally.
Reset individual folding states."
(interactive)
(setq mu4e-thread--docids nil)
(if mu4e-thread--fold-status
(mu4e-thread-unfold-all)
(mu4e-thread-fold-all)))
(defun mu4e-thread-fold-apply-all ()
"Apply global folding status to all threads not set individually."
(interactive)
;; Global fold status
(if mu4e-thread--fold-status
(mu4e-thread-fold-all)
(mu4e-thread-unfold-all))
;; Individual fold status
(save-excursion
(goto-char (point-min))
(catch 'end-search
(while (not (eobp))
(when-let* ((msg (get-text-property (point) 'msg))
(docid (mu4e-message-field msg :docid))
(state (cdr (assoc docid mu4e-thread--docids))))
(if (eq state 'folded)
(mu4e-thread-fold)
(mu4e-thread-unfold)))
(unless (mu4e-thread-next)
(throw 'end-search t))
(mu4e-thread-goto-next)))))
(defun mu4e-thread-fold-all ()
"Fold all threads unconditionally."
(interactive)
(setq mu4e-thread--fold-status t)
(save-excursion
(goto-char (point-min))
(catch 'done
(while (not (eobp))
(mu4e-thread-fold t)
(unless (mu4e-thread-goto-next)
(throw 'done t))))))
(defun mu4e-thread-unfold-all ()
"Unfold all threads unconditionally."
(interactive)
(setq mu4e-thread--fold-status nil)
(remove-overlays (point-min) (point-max) 'mu4e-thread-folded t))
(defun mu4e-thread-fold-toggle ()
"Toggle folding for thread at point."
(interactive)
(if (mu4e-thread-is-folded)
(mu4e-thread-unfold)
(mu4e-thread-fold)))
(defun mu4e-thread-fold-toggle-goto-next ()
"Toggle folding for thread at point and go to next thread."
(interactive)
(if (mu4e-thread-is-folded)
(mu4e-thread-unfold-goto-next)
(mu4e-thread-fold-goto-next)))
(defun mu4e-thread-unfold (&optional no-save)
"Unfold thread at point and store state unless NO-SAVE is t."
(interactive)
(unless (eq (line-end-position) (point-max))
(when-let ((overlay (mu4e-thread-is-folded)))
(unless no-save
(mu4e-thread--save-state 'unfolded))
(delete-overlay overlay))))
(defun mu4e-thread--save-state (state)
"Save the folding STATE of thread at point."
(save-excursion
(mu4e-thread-goto-root)
(when-let* ((msg (get-text-property (point) 'msg))
(docid (mu4e-message-field msg :docid)))
(setf (alist-get docid mu4e-thread--docids) state))))
(defun mu4e-thread-fold (&optional no-save)
"Fold thread at point and store state unless NO-SAVE is t."
(interactive)
(unless (eq (line-end-position) (point-max))
(let* ((thread-beg (mu4e-thread-root))
(thread-end (mu4e-thread-next))
(thread-end (if thread-end (1- thread-end) (point-max)))
(unread-count 0)
(fold-beg (save-excursion
(goto-char thread-beg)
(forward-line)
(point)))
(fold-end (save-excursion
(goto-char thread-beg)
(forward-line)
(catch 'fold-end
(while (and (not (eobp))
(get-text-property (point) 'msg)
(and thread-end (< (point) thread-end)))
(let* ((msg (get-text-property (point) 'msg))
(docid (mu4e-message-field msg :docid))
(flags (mu4e-message-field msg :flags))
(unread (memq 'unread flags)))
(when (mu4e-mark-docid-marked-p docid)
(throw 'fold-end (point)))
(when unread
(unless mu4e-thread-fold-unread
(throw 'fold-end (point)))
(setq unread-count (+ 1 unread-count))))
(forward-line)))
(point))))
(unless no-save
(mu4e-thread--save-state 'folded))
(let ((child-count (count-lines fold-beg fold-end))
(unread-count (if mu4e-thread-fold-unread unread-count 0)))
(when (> child-count (if mu4e-thread-fold-single-children 0 1))
(let ((inhibit-read-only t)
(overlay (make-overlay fold-beg fold-end))
(info (mu4e-thread-fold-info child-count unread-count)))
(add-text-properties fold-beg (+ fold-beg 1)
'(face mu4e-thread-fold-face))
(overlay-put overlay 'mu4e-thread-folded t)
(overlay-put overlay 'display info)))))))
(defun mu4e-thread-fold-goto-next ()
"Fold the thread at point and go to next thread."
(interactive)
(unless (eq (line-end-position) (point-max))
(mu4e-thread-fold)
(mu4e-thread-goto-next)))
(defun mu4e-thread-unfold-goto-next ()
"Unfold the thread at point and go to next thread."
(interactive)
(unless (eq (line-end-position) (point-max))
(mu4e-thread-unfold)
(mu4e-thread-goto-next)))
(define-minor-mode mu4e-thread-mode
"Mode for thread-support."
:global nil
:init-value nil ;; disabled by default
:group 'mu4e
:lighter ""
:keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "<S-left>") #'mu4e-thread-goto-root)
(define-key map (kbd "<tab>") #'mu4e-thread-fold-toggle-goto-next)
(define-key map (kbd "<C-tab>") #'mu4e-thread-fold-toggle-goto-next)
(define-key map (kbd "<backtab>") #'mu4e-thread-fold-toggle-all)
map))
(provide 'mu4e-thread)
;;; mu4e-thread.el ends here