Using Emacs - 30 - elfeed part 2 - Hydras

In part 1, I talked about elfeed, a really awesome feed reader for emacs. Generally, I'm really liking it but there's been one problem - not being able to navigate quickly between groups of feeds with a keystroke or two.

It's emacs so there has to be a solution.

Enter hydra - a terrific emacs package from the same guy who brought us swiper, another one of my favorite emacs packages.

Basically, Hydra allows us to create Hydras - a collection of emacs commands tied o a single prefix along with a nice interface and help system.

Truth be told, I don't use hydra as much anymore since which-key does such a great job most of the time. Which-key, however, is no help here.

Here's the code to install hydra along with a some of sample Hydra's I use:

(use-package hydra 
  :ensure t)

;; Hydra for modes that toggle on and off
 (kbd "C-x t")
 (defhydra toggle (:color blue)
   ("a" abbrev-mode "abbrev")
   ("s" flyspell-mode "flyspell")
   ("d" toggle-debug-on-error "debug")
   ("c" fci-mode "fCi")
   ("f" auto-fill-mode "fill")
   ("t" toggle-truncate-lines "truncate")
   ("w" whitespace-mode "whitespace")
   ("q" nil "cancel")))

;; Hydra for navigation
 (kbd "C-x j")
 (defhydra gotoline 
   ( :pre (linum-mode 1)
	  :post (linum-mode -1))
   ("t" (lambda () (interactive)(move-to-window-line-top-bottom 0)) "top")
   ("b" (lambda () (interactive)(move-to-window-line-top-bottom -1)) "bottom")
   ("m" (lambda () (interactive)(move-to-window-line-top-bottom)) "middle")
   ("e" (lambda () (interactive)(end-of-buffer)) "end")
   ("c" recenter-top-bottom "recenter")
   ("n" next-line "down")
   ("p" (lambda () (interactive) (forward-line -1))  "up")
   ("g" goto-line "goto-line")

;; Hydra for some org-mode stuff
 (kbd "C-c t")
 (defhydra hydra-global-org (:color blue)
   ("t" org-timer-start "Start Timer")
   ("s" org-timer-stop "Stop Timer")
   ("r" org-timer-set-timer "Set Timer") ; This one requires you be in an orgmode doc, as it sets the timer for the header
   ("p" org-timer "Print Timer") ; output timer value to buffer
   ("w" (org-clock-in '(4)) "Clock-In") ; used with (org-clock-persistence-insinuate) (setq org-clock-persist t)
   ("o" org-clock-out "Clock-Out") ; you might also want (setq org-log-note-clock-out t)
   ("j" org-clock-goto "Clock Goto") ; global visit the clocked task
   ("c" org-capture "Capture") ; Don't forget to define the captures you want
   ("l" (or )rg-capture-goto-last-stored "Last Capture"))

Take a look at the Hydra home page for detailed information.

With Hydra installed, I can creat one for navigating in elfeed:

`(defhydra mz/hydra-elfeed ()
   ("c" (elfeed-search-set-filter "@6-months-ago +cs") "cs")
   ("e" (elfeed-search-set-filter "@6-months-ago +emacs") "emacs")
   ("d" (elfeed-search-set-filter "@6-months-ago +education") "education")
   ("*" (elfeed-search-set-filter "@6-months-ago +star") "Starred")
   ("M" elfeed-toggle-star "Mark")
   ("A" (elfeed-search-set-filter "@6-months-ago") "All")
   ("T" (elfeed-search-set-filter "@1-day-ago") "Today")
   ("Q" bjm/elfeed-save-db-and-bury "Quit Elfeed" :color blue)
   ("q" nil "quit" :color blue)

and add a binding to the elfeed key map:

(use-package elfeed
  :ensure t
  :bind (:map elfeed-search-mode-map
	      ("q" . bjm/elfeed-save-db-and-bury)
	      ("Q" . bjm/elfeed-save-db-and-bury)
	      ("m" . elfeed-toggle-star)
	      ("M" . elfeed-toggle-star)
	      ("j" . mz/hydra-elfeed/body)
	      ("J" . mz/hydra-elfeed/body)))

This isn't the actual Hydra I use – we'll get to that in the next installment where I'll show how to make a Hydra on the fly that contains keys for all your elfeed tags but this is a reasonable example.

Here's the video:



Comments powered by Disqus

Enter your email address:

Delivered by FeedBurner

Google Analytics Alternative