From 55857d74417eeb5bb64a2f8c0642000ffdd607be Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 16 Feb 2022 23:18:55 -0600 Subject: Customize elfeed --- lisp/+elfeed.el | 116 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 55 deletions(-) (limited to 'lisp') 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 @@ ;;; Code: +(require 'elfeed) + ;; https://karthinks.com/software/lazy-elfeed/ (defun +elfeed-scroll-up-command (&optional arg) "Scroll up or go to next feed item in Elfeed" @@ -37,73 +39,78 @@ 'ignore) (defvar +elfeed--update-running nil "Whether an update is currently running.") +(defvar +elfeed--update-count 0 "How many times `+elfeed-update-command' has run.") (defun +elfeed-update-command () (interactive) - (unless +elfeed--update-running - (let ((script (expand-file-name "~/.local/bin/elfeed-update.el")) + (unless (or +elfeed--update-running + (derived-mode-p 'elfeed-show-mode 'elfeed-search-mode)) + (let ((script (expand-file-name "/tmp/elfeed-update.el")) (update-message-format "[Elfeed] Background update: %s")) - (message update-message-format "start") (setq +elfeed--update-running t) (elfeed-db-save) (advice-add 'elfeed :override #'+elfeed--update-message) (ignore-errors (kill-buffer "*elfeed-search*")) (ignore-errors (kill-buffer "*elfeed-log*")) (elfeed-db-unload) - (unless (file-exists-p script) - (make-directory (file-name-directory script) :parents) - (with-temp-buffer - (insert - (nconcat nil - "#!/usr/bin/env -S emacs --script" - "(setq lexical-binding t)" - ;; I have to load the necessary files - "(load (locate-user-emacs-file \"early-init\"))" - "(straight-use-package 'elfeed)" - "(straight-use-package 'elfeed-org)" - "(require 'elfeed)" - "(require 'elfeed-org)" - ;; And set needed variables - (let ((copy-environment)) - (dolist (var '(rmh-elfeed-org-files - elfeed-db-directory - elfeed-curl-program-name - elfeed-use-curl - elfeed-curl-extra-arguments - elfeed-enclosure-default-dir)) - (push (concat "(setq " (symbol-name var) - " '" (prin1-to-string (symbol-value var)) ")\n") - copy-environment)) - (nreverse copy-environment)) - ;; Overwrite log function to go to stdout - "(defun elfeed-log (level fmt &rest objects)" - " (princ (format \"[%s] [%s]: %s\\n\"" - " (format-time-string \"%F %T\")" - " level" - " (apply #'format fmt objects))))" - ;; Load elfeed - "(elfeed-org)" - "(elfeed-db-load)" - "(elfeed)" - ;; Update elfeed - "(elfeed-update)" - ;; Wait to finish ... I think. - "(while (> (elfeed-queue-count-total) 0)" - " (sleep-for 5)" - " (message \"%s\" (elfeed-queue-count-total))" - " (accept-process-output))" - ;; Save and garbage-collect - "(elfeed-db-save)" - "(elfeed-db-gc)")) - (write-file script)) - (chmod script #o777)) + (make-directory (file-name-directory script) :parents) + (with-temp-buffer + (insert + (prin1-to-string ;; Print the following s-expression to a string + `(progn + ;; Set up the environment + (setq lexical-binding t) + (load (locate-user-emacs-file "early-init")) + (dolist (pkg '(elfeed elfeed-org)) + (straight-use-package pkg) + (require pkg)) + ;; Copy variables from current environment + (progn + ,@(cl-loop for copy-var in '(rmh-elfeed-org-files + elfeed-db-directory + elfeed-curl-program-name + elfeed-use-curl + elfeed-curl-extra-arguments + elfeed-enclosure-default-dir) + collect `(progn (message "%S = %S" ',copy-var ',(symbol-value copy-var)) + (setq ,copy-var ',(symbol-value copy-var))))) + ;; Define new variables for this environment + (progn + ,@(cl-loop for (new-var . new-val) in '((elfeed-curl-max-connections . 4)) + collect `(progn (message "%S = %S" ',new-var ',new-val) + (setq ,new-var ',new-val)))) + ;; Redefine `elfeed-log' to log everything + (defun elfeed-log (level fmt &rest objects) + (princ (format "[%s] [%s]: %s\n" + (format-time-string "%F %T") + level + (apply #'format fmt objects)))) + ;; Run elfeed + (elfeed-org) + (elfeed) + (elfeed-db-load) + (elfeed-update) + ;; Wait for `elfeed-update' to finish + (while (> (elfeed-queue-count-total) 0) + (sleep-for 5) + (message "%s" (elfeed-queue-count-total)) + (accept-process-output)) + ;; Garbage collect and save the database + (elfeed-db-gc) + (elfeed-db-save) + (princ (format ,update-message-format "done."))))) + (write-file script)) + (chmod script #o777) + (message update-message-format "start") (set-process-sentinel (start-process-shell-command - "Elfeed" nil script) - (lambda (a b) + "Elfeed" "*+elfeed-update-background*" + (format "nice -%d %s %s" 15 "emacs -Q --script" script)) + (lambda (proc stat) (advice-remove 'elfeed #'+elfeed--update-message) (setq +elfeed--update-running nil) - (message update-message-format - (string-trim b))))))) + (unless (string= stat "killed") + (setq +elfeed--update-count (1+ +elfeed--update-count))) + (message update-message-format (string-trim stat))))))) (defvar +elfeed--update-timer nil "Timer for `elfeed-update-command'.") (defvar +elfeed--update-first-time 6 "How long to wait for the first time.") @@ -130,7 +137,6 @@ :global t (if +elfeed-update-async-mode (progn ; enable - (+elfeed--cancel-update-timer) ; first disable any hangers-on (+elfeed--reinstate-update-timer) (advice-add 'elfeed :before '+elfeed--cancel-update-timer) (advice-add 'elfeed-search-quit-window :after '+elfeed--reinstate-update-timer)) -- cgit 1.4.1-21-gabe81