Star and unstar articles in elfeed

Following on from a couple of previous posts on elfeed, the excellent feed reader for Emacs, I wanted to share some code to enable the starring and unstarring of articles for future reference.

This code is closely based on this article, so all credit should go in that direction. My only addition is to add the ability to unstar an article.

;; code to add and remove a starred tag to elfeed article
;; based on http://matt.hackinghistory.ca/2015/11/22/elfeed/

;; add a star
(defun bjm/elfeed-star ()
  "Apply starred to all selected entries."
  (interactive )
  (let* ((entries (elfeed-search-selected))
         (tag (intern "starred")))

    (cl-loop for entry in entries do (elfeed-tag entry tag))
    (mapc #'elfeed-search-update-entry entries)
    (unless (use-region-p) (forward-line))))

;; remove a start
(defun bjm/elfeed-unstar ()
  "Remove starred tag from all selected entries."
  (interactive )
  (let* ((entries (elfeed-search-selected))
         (tag (intern "starred")))

    (cl-loop for entry in entries do (elfeed-untag entry tag))
    (mapc #'elfeed-search-update-entry entries)
    (unless (use-region-p) (forward-line))))

;; face for starred articles
(defface elfeed-search-starred-title-face
  '((t :foreground "#f77"))
  "Marks a starred Elfeed entry.")

(push '(starred elfeed-search-starred-title-face) elfeed-search-face-alist)

This code sets up the required functions to add and remove the “starred” tag from articles, and sets a face colour to indicate articles that have been starred.

I bind these to the keys “*” to add a star and “8” to remove the star (easy to remember):

;; add keybindings
(eval-after-load 'elfeed-search
  '(define-key elfeed-search-mode-map (kbd "*") 'bjm/elfeed-star))
(eval-after-load 'elfeed-search
  '(define-key elfeed-search-mode-map (kbd "8") 'bjm/elfeed-unstar))

Now, entering the search filter “+starred” in elfeed will show the starred articles, and as described previously you can save a bookmark to that filter (I called mine “elfeed-starred”) and assign a key like “S” to jump directly to the starred articles:

;;shortcut to jump to starred bookmark
(defun bjm/elfeed-show-starred ()
  (interactive)
  (bookmark-jump "elfeed-starred"))
(define-key elfeed-search-mode-map (kbd "S") 'bjm/elfeed-show-starred)

Update 2016-09-27

Commenter Galrog has a much simpler implementation. I’ve copied it here so the formatting is nicer:

(defalias 'elfeed-toggle-star
  (elfeed-expose #'elfeed-search-toggle-all 'star))

(eval-after-load 'elfeed-search
  '(define-key elfeed-search-mode-map (kbd "m") 'elfeed-toggle-star))
  • I know you got it from the other article, but instead of (intern “starred”) you can just say ‘starred (i.e. the quoted symbol). The s-exp reader will intern the name automatically. Manual interning is needed when constructing the symbol’s name at run time, but if it’s just a string literal then you already know the complete name.

  • Galrog

    Nice article.

    I was trying to do the same thing and came up with a single line of code for tagging and untagging entries. And the best thing is that it is a toggle. 🙂 So you only need one keybinding for it.

    (defalias ‘elfeed-toggle-star
    (elfeed-expose #’elfeed-search-toggle-all ‘star))

    You can find the elfeed-search-toggle-all function in the elfeed-search.el file.

    (eval-after-load ‘elfeed-search
    ‘(define-key elfeed-search-mode-map (kbd “m”) ‘elfeed-toggle-star))

    What do you think?

    • That looks wonderfully concise! I’ll have to replace my version!