diff options
Diffstat (limited to 'lisp/+elfeed.el')
-rw-r--r-- | lisp/+elfeed.el | 185 |
1 files changed, 0 insertions, 185 deletions
diff --git a/lisp/+elfeed.el b/lisp/+elfeed.el deleted file mode 100644 index c3e5301..0000000 --- a/lisp/+elfeed.el +++ /dev/null | |||
@@ -1,185 +0,0 @@ | |||
1 | ;;; +elfeed.el -*- lexical-binding: t; -*- | ||
2 | |||
3 | ;;; Code: | ||
4 | |||
5 | (require 'elfeed) | ||
6 | |||
7 | ;; https://karthinks.com/software/lazy-elfeed/ | ||
8 | (defun +elfeed-scroll-up-command (&optional arg) | ||
9 | "Scroll up or go to next feed item in Elfeed" | ||
10 | (interactive "^P") | ||
11 | (let ((scroll-error-top-bottom nil)) | ||
12 | (condition-case-unless-debug nil | ||
13 | (scroll-up-command arg) | ||
14 | (error (elfeed-show-next))))) | ||
15 | |||
16 | (defun +elfeed-scroll-down-command (&optional arg) | ||
17 | "Scroll up or go to next feed item in Elfeed" | ||
18 | (interactive "^P") | ||
19 | (let ((scroll-error-top-bottom nil)) | ||
20 | (condition-case-unless-debug nil | ||
21 | (scroll-down-command arg) | ||
22 | (error (elfeed-show-prev))))) | ||
23 | |||
24 | (defun +elfeed-search-browse-generic () | ||
25 | "Browse a url with `browse-url-generic-browser'." | ||
26 | (interactive) | ||
27 | (elfeed-search-browse-url t)) | ||
28 | |||
29 | (defun +elfeed-show-browse-generic () | ||
30 | "Browse a url with `browse-url-generic-browser'." | ||
31 | (interactive) | ||
32 | (elfeed-show-visit t)) | ||
33 | |||
34 | (defun +elfeed-show-mark-read-and-advance () | ||
35 | "Mark an item as read and advance to the next item. | ||
36 | If multiple items are selected, don't advance." | ||
37 | (interactive) | ||
38 | (call-interactively #'elfeed-search-untag-all-unread) | ||
39 | (unless (region-active-p) | ||
40 | (call-interactively #'next-line))) | ||
41 | |||
42 | ;;; Fetch feeds async | ||
43 | ;; https://github.com/skeeto/elfeed/issues/367 | ||
44 | |||
45 | (defun +elfeed--update-message () | ||
46 | (message "[Elfeed] Update in progress") | ||
47 | 'ignore) | ||
48 | |||
49 | (defvar +elfeed--update-running-p nil "Whether an update is currently running.") | ||
50 | (defvar +elfeed--update-count 0 "How many times `+elfeed-update-command' has run.") | ||
51 | (defcustom +elfeed-update-niceness 15 | ||
52 | "How \"nice\" `+elfeed-update-command' should be." | ||
53 | :type 'integer | ||
54 | :group 'elfeed) | ||
55 | |||
56 | (defcustom +elfeed-update-lockfile | ||
57 | (expand-file-name "+elfeed-update-lock" (temporary-file-directory)) | ||
58 | "The file to ") | ||
59 | |||
60 | (defun +elfeed-update-command () | ||
61 | (interactive) | ||
62 | (unless (or +elfeed--update-running-p | ||
63 | (derived-mode-p 'elfeed-show-mode 'elfeed-search-mode)) | ||
64 | (let ((script (expand-file-name "/tmp/elfeed-update.el")) | ||
65 | (update-message-format "[Elfeed] Background update: %s")) | ||
66 | (setq +elfeed--update-running-p t) | ||
67 | (elfeed-db-save) | ||
68 | (advice-add 'elfeed :override #'+elfeed--update-message) | ||
69 | (ignore-errors (kill-buffer "*elfeed-search*")) | ||
70 | (ignore-errors (kill-buffer "*elfeed-log*")) | ||
71 | (elfeed-db-unload) | ||
72 | (make-directory (file-name-directory script) :parents) | ||
73 | (with-temp-buffer | ||
74 | (insert | ||
75 | (let ((print-level nil) | ||
76 | (print-length nil)) | ||
77 | (prin1-to-string ;; Print the following s-expression to a string | ||
78 | `(progn | ||
79 | ;; Set up the environment | ||
80 | (setq lexical-binding t) | ||
81 | (load (locate-user-emacs-file "early-init")) | ||
82 | (dolist (pkg '(elfeed elfeed-org)) | ||
83 | (straight-use-package pkg) | ||
84 | (require pkg)) | ||
85 | ;; Copy variables from current environment | ||
86 | (progn | ||
87 | ,@(cl-loop for copy-var in '(rmh-elfeed-org-files | ||
88 | elfeed-db-directory | ||
89 | elfeed-curl-program-name | ||
90 | elfeed-use-curl | ||
91 | elfeed-curl-extra-arguments | ||
92 | elfeed-enclosure-default-dir) | ||
93 | collect `(progn (message "%S = %S" ',copy-var ',(symbol-value copy-var)) | ||
94 | (setq ,copy-var ',(symbol-value copy-var))))) | ||
95 | ;; Define new variables for this environment | ||
96 | (progn | ||
97 | ,@(cl-loop for (new-var . new-val) in '((elfeed-curl-max-connections . 4)) | ||
98 | collect `(progn (message "%S = %S" ',new-var ',new-val) | ||
99 | (setq ,new-var ',new-val)))) | ||
100 | ;; Redefine `elfeed-log' to log everything | ||
101 | (defun elfeed-log (level fmt &rest objects) | ||
102 | (princ (format "[%s] [%s]: %s\n" | ||
103 | (format-time-string "%F %T") | ||
104 | level | ||
105 | (apply #'format fmt objects)))) | ||
106 | ;; Run elfeed | ||
107 | (elfeed-org) | ||
108 | (elfeed) | ||
109 | (elfeed-db-load) | ||
110 | (elfeed-update) | ||
111 | ;; Wait for `elfeed-update' to finish | ||
112 | (let ((q<5-count 0)) | ||
113 | (while (and (> (elfeed-queue-count-total) 0) | ||
114 | (< q<5-count 5)) | ||
115 | (sleep-for 5) | ||
116 | (message "Elfeed queue count total: %s" (elfeed-queue-count-total)) | ||
117 | (when (< (elfeed-queue-count-total) 5) | ||
118 | (cl-incf q<5-count)) | ||
119 | (accept-process-output))) | ||
120 | ;; Garbage collect and save the database | ||
121 | (elfeed-db-gc) | ||
122 | (elfeed-db-save) | ||
123 | (princ (format ,update-message-format "done.")))))) | ||
124 | (write-file script)) | ||
125 | (chmod script #o777) | ||
126 | (message update-message-format "start") | ||
127 | (set-process-sentinel (start-process-shell-command | ||
128 | "Elfeed" "*+elfeed-update-background*" | ||
129 | (format "nice -n %d %s %s" | ||
130 | +elfeed-update-niceness | ||
131 | "emacs -Q --script" | ||
132 | script)) | ||
133 | (lambda (proc stat) | ||
134 | (advice-remove 'elfeed #'+elfeed--update-message) | ||
135 | (setq +elfeed--update-running-p nil) | ||
136 | (unless (string= stat "killed") | ||
137 | (setq +elfeed--update-count (1+ +elfeed--update-count))) | ||
138 | (message update-message-format (string-trim stat))))))) | ||
139 | |||
140 | (defvar +elfeed--update-timer nil "Timer for `elfeed-update-command'.") | ||
141 | (defvar +elfeed--update-first-time 6 "How long to wait for the first time.") | ||
142 | (defvar +elfeed--update-repeat (* 60 15) "How long between updates.") | ||
143 | |||
144 | (defcustom +elfeed-update-proceed-hook nil | ||
145 | "Predicates to query before running `+elfeed-update-command'. | ||
146 | Each hook is passed no arguments." | ||
147 | :type 'hook) | ||
148 | |||
149 | (defun +elfeed-update-command-wrapper () | ||
150 | "Run `+elfeed-update-command', but only sometimes. | ||
151 | If any of the predicates in `+elfeed-update-proceed-hook' return | ||
152 | nil, don't run `+elfeed-update-command'. If they all return | ||
153 | non-nil, proceed." | ||
154 | (when (run-hook-with-args-until-failure '+elfeed-update-proceed-hook) | ||
155 | (+elfeed-update-command))) | ||
156 | |||
157 | (defun +elfeed--cancel-update-timer () | ||
158 | "Cancel `+elfeed--update-timer'." | ||
159 | (unless +elfeed--update-running-p | ||
160 | (ignore-errors (cancel-timer +elfeed--update-timer)) | ||
161 | (setq +elfeed--update-timer nil))) | ||
162 | |||
163 | (defun +elfeed--reinstate-update-timer () | ||
164 | "Reinstate `+elfeed--update-timer'." | ||
165 | ;; First, unload the db | ||
166 | (setq +elfeed--update-timer | ||
167 | (run-at-time +elfeed--update-first-time | ||
168 | +elfeed--update-repeat | ||
169 | #'+elfeed-update-command-wrapper))) | ||
170 | |||
171 | (define-minor-mode +elfeed-update-async-mode | ||
172 | "Minor mode to update elfeed async-style." | ||
173 | :global t | ||
174 | (if +elfeed-update-async-mode | ||
175 | (progn ; enable | ||
176 | (+elfeed--reinstate-update-timer) | ||
177 | (advice-add 'elfeed :before '+elfeed--cancel-update-timer) | ||
178 | (advice-add 'elfeed-search-quit-window :after '+elfeed--reinstate-update-timer)) | ||
179 | (progn ; disable | ||
180 | (advice-remove 'elfeed '+elfeed--cancel-update-timer) | ||
181 | (advice-remove 'elfeed-search-quit-window '+elfeed--reinstate-update-timer) | ||
182 | (+elfeed--cancel-update-timer)))) | ||
183 | |||
184 | (provide '+elfeed) | ||
185 | ;;; +elfeed.el ends here | ||