-
Notifications
You must be signed in to change notification settings - Fork 0
/
elfeed-navi.el
312 lines (258 loc) · 12.3 KB
/
elfeed-navi.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
;;; elfeed-navi.el --- Navigation for *elfeed-search* -*- lexical-binding: t; -*-
;; Copyright 2024 Stefano Merlo
;; 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 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/>.
;; Author: Stefano Merlo <[email protected]>
;; Created: 2024-01-17
;;; Commentary:
;; A navigation interface for *elfeed-search*.
;;; Code:
(require 'elfeed)
;;; source data from elfeed-search
(defvar elfeed-navi-entries ()
"List of the entries displayed in `*elfeed-search*'.
It is a copy of `elfeed-search-entries' refreshed as needed.")
(defvar elfeed-navi-entries-last-update 0
"The last time the entries list was updated in epoch seconds.")
(defvar elfeed-navi-search-filter-default "@4-days-ago +unread"
"Default filter for the `*elfeed-navi*' buffer.")
(defvar elfeed-navi-search-filter elfeed-navi-search-filter-default
"Copy of the active filter in the `*elfeed-search*' buffer.")
(defun elfeed-navi-store-filter ()
(setq elfeed-navi-search-filter (substring elfeed-search-filter)))
(defun elfeed-navi-entries--update-list ()
"Update `elfeed-navi-entries' list based on current filter."
(let* ((filter (elfeed-search-parse-filter elfeed-navi-search-filter))
(head (list nil))
(tail head)
(count 0))
(if elfeed-search-compile-filter
;; Force lexical bindings regardless of the current
;; buffer-local value. Lexical scope uses the faster
;; stack-ref opcode instead of the traditional varref opcode.
(let ((lexical-binding t)
(func (byte-compile (elfeed-search-compile-filter filter))))
(with-elfeed-db-visit (entry feed)
(when (funcall func entry feed count)
(setf (cdr tail) (list entry)
tail (cdr tail)
count (1+ count)))))
(with-elfeed-db-visit (entry feed)
(when (elfeed-search-filter filter entry feed count)
(setf (cdr tail) (list entry)
tail (cdr tail)
count (1+ count)))))
(setf elfeed-navi-entries (cdr head))
(setf elfeed-navi-entries-last-update (float-time))))
;;; create summary
(defvar elfeed-navi-aggregator 'feed-title
"Symbol to specify the aggregation on with compute the summary.
Currently only implemented for default `'feed-title'.")
(defvar elfeed-navi-summary-entries-alist ()
"List of the summary entries computed on `elfeed-navi-entries'.
The alist is scalable to allow different aggregations methods.
((:aggregator symbol :title str :count int :most-recent epoch))")
(defvar elfeed-navi-summary-entries-alist-last-update 0
"Last update of the alist, in epoch.")
(defun elfeed-navi-list-aggregator-values (aggregator)
"List values of AGGREGATOR in `elfeed-navi-entries'."
(let* ((extract-func (pcase aggregator
('feed-title (lambda (entry)
(let ((feed (elfeed-entry-feed entry)))
(or (elfeed-meta feed :title)
(elfeed-feed-title feed)))))))
(all-values (seq-map extract-func elfeed-navi-entries))
(dedup-values (seq-uniq all-values)))
dedup-values))
(defun elfeed-navi-create-summary (aggregator &optional force)
"Populate the `elfeed-navi-summary-entries-alist' with AGGREGATOR.
Sort descending for count.
When FORCE not-nil, update list even if the entries-list did not change."
(when (or force (> elfeed-navi-entries-last-update
elfeed-navi-summary-entries-alist-last-update))
(let ((unsorted))
(dolist (title (elfeed-navi-list-aggregator-values aggregator))
;; TODO adapt for other aggregators like tags
(push (cl-loop for entry in elfeed-navi-entries
for feed = (elfeed-entry-feed entry)
for feed-title = (or (elfeed-meta feed :title)
(elfeed-feed-title feed))
for date = (elfeed-entry-date entry)
if (string= feed-title title)
count t into p
and maximize date into latest-update
finally return (list :aggregator aggregator
:title title
:count p
:most-recent latest-update))
unsorted))
(setq elfeed-navi-summary-entries-alist
(seq-sort (lambda (a b) (> (plist-get a :count)
(plist-get b :count))) unsorted)))
(setq elfeed-navi-summary-entries-alist-last-update (float-time))))
(defun elfeed-navi-summary-entry-get-title (summary-entry)
"Get title from the plist SUMMARY-ENTRY."
(plist-get summary-entry :title))
(defun elfeed-navi-summary-entry-get-count (summary-entry)
"Get count from the plist SUMMARY-ENTRY and return a string."
(format "%s" (plist-get summary-entry :count)))
(defun elfeed-navi-summary-entry-get-most-recent (summary-entry)
"Return a meaningful string for date.
Returning `Today' for today's date, `Yesterday' for yesterday, and
`n days ago' for other dates."
(let* ((date-epoch (plist-get summary-entry :most-recent))
;; (elfeed-search-date-format (list "%a %b-%d" date-width :left))
(date-str (elfeed-search-format-date date-epoch))
(delta (time-subtract (float-time) date-epoch))
(delta-days (- (time-to-days (float-time)) (time-to-days date-epoch)))
(time-str (cond ((equal delta-days 0) "Today")
((equal delta-days 1) "Yesterday")
((format "%2s days ago" delta-days)))))
(concat (format "%11s" time-str) ", " date-str)))
;;; rendering results
(defface elfeed-navi-today-face
'((t :foreground "red1" :weight bold))
"Marks today.")
(defface elfeed-navi-yesterday-face
'((t :foreground "orange1"))
"Marks yesterday.")
(defvar elfeed-navi-buffer-last-update 0
"The last time the buffer was redrawn in epoch seconds.")
(defvar elfeed-navi-buffer-filter ""
"Filter with which the buffer is built.")
;; (defvar elfeed-navi-title-width 30
;; "Width of the title column.")
;; (defvar elfeed-navi-count-width 5
;; "Width of the count column.")
(defun elfeed-navi-buffer ()
"Get or create the summary buffer."
(get-buffer-create "*elfeed-navi*"))
(defun elfeed-navi-line-format (aggregator)
"Return format string for lines and header, appropriate for AGGREGATOR."
(pcase aggregator
('feed-title "%30s %7s %26s")))
(defun elfeed-navi-title-face (summary-entry)
"Return face depending on entry age."
(let ((date (elfeed-navi-summary-entry-get-most-recent summary-entry)))
(cond ((string-prefix-p "Yesterday" (string-clean-whitespace date))
'elfeed-navi-yesterday-face)
((string-prefix-p "Today" (string-clean-whitespace date))
'elfeed-navi-today-face)
(t 'elfeed-search-date-face))))
(defun elfeed-navi-print-line-function (summary-entry aggregator)
"Print SUMMARY-ENTRY line in the `*elfeed-navi-buffer*'."
(let* ((title (elfeed-navi-summary-entry-get-title summary-entry))
(count (elfeed-navi-summary-entry-get-count summary-entry))
(most-recent (elfeed-navi-summary-entry-get-most-recent summary-entry))
(date-face (elfeed-navi-title-face summary-entry)))
(insert
(format (elfeed-navi-line-format aggregator)
(propertize title 'face 'elfeed-search-feed-face)
(propertize count 'face 'elfeed-search-tag-face)
(propertize most-recent 'face date-face)))))
(defun elfeed-navi-header-line (aggregator)
"Set the header line appropriate for AGGREGATOR."
(setq header-line-format
(format (elfeed-navi-line-format aggregator)
"Feed" "Count" "Most recent")))
(defun elfeed-navi-buffer-update (&optional force)
"Update the elfeed-navi buffer listing to match the database.
When FORCE is non-nil, redraw even when the database hasn't changed."
(interactive)
;; update entries, considering filter if any
(elfeed-navi-entries--update-list)
;; update summary entries
;; TODO consider other aggregators
(let ((aggregator 'feed-title))
(elfeed-navi-create-summary aggregator)
(with-current-buffer (elfeed-navi-buffer)
(when (or force
(and (not elfeed-search-filter-active)
(< elfeed-navi-buffer-last-update (elfeed-db-last-update))))
(elfeed-save-excursion
(let ((inhibit-read-only t)
(standard-output (current-buffer))
(summary-entries elfeed-navi-summary-entries-alist))
(erase-buffer)
(setq elfeed-navi-buffer-filter elfeed-navi-search-filter)
(elfeed-navi-header-line aggregator)
;; (insert (format "Current filter: %s\n" elfeed-navi-buffer-filter))
;; (insert (format-time-string "Buffer update: %H:%M:%S\n"
;; elfeed-navi-buffer-last-update))
;; (insert (format-time-string "Entries list update update: %H:%M:%S\n\n"
;; elfeed-navi-entries-last-update))
(dolist (summary-entry summary-entries)
(funcall #'elfeed-navi-print-line-function summary-entry aggregator)
(insert "\n"))
(setf elfeed-navi-buffer-last-update (float-time))))
(when (zerop (buffer-size))
;; If nothing changed, force a header line update
(force-mode-line-update))
))))
(defun elfeed-navi-buffer-update--force ()
"Force update the `*elfeed-navi*' buffer to match the database."
(interactive)
(elfeed-navi-buffer-update t))
(defun elfeed-navi-buffer-update--force-reset ()
"Update the `*elfeed-navi*' buffer using default filter."
(interactive)
(setq elfeed-navi-search-filter elfeed-navi-search-filter-default)
(elfeed-navi-buffer-update--force))
(defvar elfeed-navi-mode-map
(let ((map (make-sparse-keymap)))
(prog1 map
(suppress-keymap map)
(define-key map "h" #'describe-mode)
(define-key map "q" #'quit-window)
(define-key map "e" #'elfeed)
(define-key map "n" #'next-line)
(define-key map "g" #'elfeed-navi-buffer-update--force)
(define-key map "G" #'elfeed-navi-buffer-update--force-reset)
(define-key map (kbd "RET") #'elfeed-navi-goto-feed)
(define-key map "p" #'previous-line))) "Keymap for elfeed-navi-mode.")
(defun elfeed-navi-mode ()
"Major mode for listing elfeed feed entries occurrences.
\\{elfeed-navi-mode-map}"
(interactive)
(kill-all-local-variables)
(use-local-map elfeed-navi-mode-map)
(setq major-mode 'elfeed-navi-mode
mode-name "elfeed-navi"
truncate-lines t
buffer-read-only t)
(buffer-disable-undo)
(hl-line-mode)
(run-mode-hooks 'elfeed-navi-mode-hook))
;;;###autoload
(defun elfeed-navi ()
"Enter elfeed-navi."
(interactive)
(switch-to-buffer (elfeed-navi-buffer))
(unless (eq major-mode 'elfeed-navi-mode)
(elfeed-navi-mode)))
(defun elfeed-navi-selected-summary-entry ()
(let* ((line (line-number-at-pos (point))))
(nth (- line 1) elfeed-navi-summary-entries-alist)))
(defun elfeed-navi-sanitize-regexp-filter (str)
"Replace spaces with [[:space:]] in STR.
See `https://github.com/skeeto/elfeed/issues/336'"
(string-replace " " "[[:space:]]" str))
(defun elfeed-navi-goto-feed (summary-entry)
"Display the elfeed-search buffer filtered by FEED-TITLE at point"
(interactive (list (elfeed-navi-selected-summary-entry)))
(let* ((feed-title (elfeed-navi-summary-entry-get-title summary-entry))
(feed-title-sanitized (elfeed-navi-sanitize-regexp-filter feed-title)))
(elfeed-search-set-filter
(format "@4-days-ago +unread =%s" feed-title-sanitized)) ;; TODO review this filter
(elfeed)))
(provide 'elfeed-navi)
;;; elfeed-navi ends here