diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/+elfeed.el | 116 |
1 files changed, 61 insertions, 55 deletions
diff --git a/lisp/+elfeed.el b/lisp/+elfeed.el index 8a0376d..85d9c3b 100644 --- a/lisp/+elfeed.el +++ b/lisp/+elfeed.el | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | ;;; Code: | 3 | ;;; Code: |
4 | 4 | ||
5 | (require 'elfeed) | ||
6 | |||
5 | ;; https://karthinks.com/software/lazy-elfeed/ | 7 | ;; https://karthinks.com/software/lazy-elfeed/ |
6 | (defun +elfeed-scroll-up-command (&optional arg) | 8 | (defun +elfeed-scroll-up-command (&optional arg) |
7 | "Scroll up or go to next feed item in Elfeed" | 9 | "Scroll up or go to next feed item in Elfeed" |
@@ -37,73 +39,78 @@ | |||
37 | 'ignore) | 39 | 'ignore) |
38 | 40 | ||
39 | (defvar +elfeed--update-running nil "Whether an update is currently running.") | 41 | (defvar +elfeed--update-running nil "Whether an update is currently running.") |
42 | (defvar +elfeed--update-count 0 "How many times `+elfeed-update-command' has run.") | ||
40 | 43 | ||
41 | (defun +elfeed-update-command () | 44 | (defun +elfeed-update-command () |
42 | (interactive) | 45 | (interactive) |
43 | (unless +elfeed--update-running | 46 | (unless (or +elfeed--update-running |
44 | (let ((script (expand-file-name "~/.local/bin/elfeed-update.el")) | 47 | (derived-mode-p 'elfeed-show-mode 'elfeed-search-mode)) |
48 | (let ((script (expand-file-name "/tmp/elfeed-update.el")) | ||
45 | (update-message-format "[Elfeed] Background update: %s")) | 49 | (update-message-format "[Elfeed] Background update: %s")) |
46 | (message update-message-format "start") | ||
47 | (setq +elfeed--update-running t) | 50 | (setq +elfeed--update-running t) |
48 | (elfeed-db-save) | 51 | (elfeed-db-save) |
49 | (advice-add 'elfeed :override #'+elfeed--update-message) | 52 | (advice-add 'elfeed :override #'+elfeed--update-message) |
50 | (ignore-errors (kill-buffer "*elfeed-search*")) | 53 | (ignore-errors (kill-buffer "*elfeed-search*")) |
51 | (ignore-errors (kill-buffer "*elfeed-log*")) | 54 | (ignore-errors (kill-buffer "*elfeed-log*")) |
52 | (elfeed-db-unload) | 55 | (elfeed-db-unload) |
53 | (unless (file-exists-p script) | 56 | (make-directory (file-name-directory script) :parents) |
54 | (make-directory (file-name-directory script) :parents) | 57 | (with-temp-buffer |
55 | (with-temp-buffer | 58 | (insert |
56 | (insert | 59 | (prin1-to-string ;; Print the following s-expression to a string |
57 | (nconcat nil | 60 | `(progn |
58 | "#!/usr/bin/env -S emacs --script" | 61 | ;; Set up the environment |
59 | "(setq lexical-binding t)" | 62 | (setq lexical-binding t) |
60 | ;; I have to load the necessary files | 63 | (load (locate-user-emacs-file "early-init")) |
61 | "(load (locate-user-emacs-file \"early-init\"))" | 64 | (dolist (pkg '(elfeed elfeed-org)) |
62 | "(straight-use-package 'elfeed)" | 65 | (straight-use-package pkg) |
63 | "(straight-use-package 'elfeed-org)" | 66 | (require pkg)) |
64 | "(require 'elfeed)" | 67 | ;; Copy variables from current environment |
65 | "(require 'elfeed-org)" | 68 | (progn |
66 | ;; And set needed variables | 69 | ,@(cl-loop for copy-var in '(rmh-elfeed-org-files |
67 | (let ((copy-environment)) | 70 | elfeed-db-directory |
68 | (dolist (var '(rmh-elfeed-org-files | 71 | elfeed-curl-program-name |
69 | elfeed-db-directory | 72 | elfeed-use-curl |
70 | elfeed-curl-program-name | 73 | elfeed-curl-extra-arguments |
71 | elfeed-use-curl | 74 | elfeed-enclosure-default-dir) |
72 | elfeed-curl-extra-arguments | 75 | collect `(progn (message "%S = %S" ',copy-var ',(symbol-value copy-var)) |
73 | elfeed-enclosure-default-dir)) | 76 | (setq ,copy-var ',(symbol-value copy-var))))) |
74 | (push (concat "(setq " (symbol-name var) | 77 | ;; Define new variables for this environment |
75 | " '" (prin1-to-string (symbol-value var)) ")\n") | 78 | (progn |
76 | copy-environment)) | 79 | ,@(cl-loop for (new-var . new-val) in '((elfeed-curl-max-connections . 4)) |
77 | (nreverse copy-environment)) | 80 | collect `(progn (message "%S = %S" ',new-var ',new-val) |
78 | ;; Overwrite log function to go to stdout | 81 | (setq ,new-var ',new-val)))) |
79 | "(defun elfeed-log (level fmt &rest objects)" | 82 | ;; Redefine `elfeed-log' to log everything |
80 | " (princ (format \"[%s] [%s]: %s\\n\"" | 83 | (defun elfeed-log (level fmt &rest objects) |
81 | " (format-time-string \"%F %T\")" | 84 | (princ (format "[%s] [%s]: %s\n" |
82 | " level" | 85 | (format-time-string "%F %T") |
83 | " (apply #'format fmt objects))))" | 86 | level |
84 | ;; Load elfeed | 87 | (apply #'format fmt objects)))) |
85 | "(elfeed-org)" | 88 | ;; Run elfeed |
86 | "(elfeed-db-load)" | 89 | (elfeed-org) |
87 | "(elfeed)" | 90 | (elfeed) |
88 | ;; Update elfeed | 91 | (elfeed-db-load) |
89 | "(elfeed-update)" | 92 | (elfeed-update) |
90 | ;; Wait to finish ... I think. | 93 | ;; Wait for `elfeed-update' to finish |
91 | "(while (> (elfeed-queue-count-total) 0)" | 94 | (while (> (elfeed-queue-count-total) 0) |
92 | " (sleep-for 5)" | 95 | (sleep-for 5) |
93 | " (message \"%s\" (elfeed-queue-count-total))" | 96 | (message "%s" (elfeed-queue-count-total)) |
94 | " (accept-process-output))" | 97 | (accept-process-output)) |
95 | ;; Save and garbage-collect | 98 | ;; Garbage collect and save the database |
96 | "(elfeed-db-save)" | 99 | (elfeed-db-gc) |
97 | "(elfeed-db-gc)")) | 100 | (elfeed-db-save) |
98 | (write-file script)) | 101 | (princ (format ,update-message-format "done."))))) |
99 | (chmod script #o777)) | 102 | (write-file script)) |
103 | (chmod script #o777) | ||
104 | (message update-message-format "start") | ||
100 | (set-process-sentinel (start-process-shell-command | 105 | (set-process-sentinel (start-process-shell-command |
101 | "Elfeed" nil script) | 106 | "Elfeed" "*+elfeed-update-background*" |
102 | (lambda (a b) | 107 | (format "nice -%d %s %s" 15 "emacs -Q --script" script)) |
108 | (lambda (proc stat) | ||
103 | (advice-remove 'elfeed #'+elfeed--update-message) | 109 | (advice-remove 'elfeed #'+elfeed--update-message) |
104 | (setq +elfeed--update-running nil) | 110 | (setq +elfeed--update-running nil) |
105 | (message update-message-format | 111 | (unless (string= stat "killed") |
106 | (string-trim b))))))) | 112 | (setq +elfeed--update-count (1+ +elfeed--update-count))) |
113 | (message update-message-format (string-trim stat))))))) | ||
107 | 114 | ||
108 | (defvar +elfeed--update-timer nil "Timer for `elfeed-update-command'.") | 115 | (defvar +elfeed--update-timer nil "Timer for `elfeed-update-command'.") |
109 | (defvar +elfeed--update-first-time 6 "How long to wait for the first time.") | 116 | (defvar +elfeed--update-first-time 6 "How long to wait for the first time.") |
@@ -130,7 +137,6 @@ | |||
130 | :global t | 137 | :global t |
131 | (if +elfeed-update-async-mode | 138 | (if +elfeed-update-async-mode |
132 | (progn ; enable | 139 | (progn ; enable |
133 | (+elfeed--cancel-update-timer) ; first disable any hangers-on | ||
134 | (+elfeed--reinstate-update-timer) | 140 | (+elfeed--reinstate-update-timer) |
135 | (advice-add 'elfeed :before '+elfeed--cancel-update-timer) | 141 | (advice-add 'elfeed :before '+elfeed--cancel-update-timer) |
136 | (advice-add 'elfeed-search-quit-window :after '+elfeed--reinstate-update-timer)) | 142 | (advice-add 'elfeed-search-quit-window :after '+elfeed--reinstate-update-timer)) |