abo-abo / hydra

make Emacs bindings that stick around
1.85k stars 113 forks source link

How to close hydra when switching or closing buffer #419

Open RudolfVonKrugstein opened 1 year ago

RudolfVonKrugstein commented 1 year ago

To make me remember keybindings while in dired mode, I made this hydra (well I copied most of it from somewhere):

  (defhydra hydra-dired (:hint nil :color pink)
    "dired"
    ("\\" dired-do-ispell "flyspell")
    ("(" dired-hide-details-mode "details")
    (")" dired-omit-mode "omit-mode")
    ("+" dired-create-directory "mkdir")
    ("=" diredp-ediff "pdiff")         ;; smart diff
    ("?" dired-summary "summay")
    ("$" diredp-hide-subdir-nomove "hide-subdir")
    ("A" dired-do-find-regexp "find regexp")
    ("C" dired-do-copy "Copy")        ;; Copy all marked files
    ("D" dired-do-delete "Delete")
    ("E" dired-mark-extension "Extension mark")
    ("e" dired-ediff-files "ediff")
    ("F" dired-do-find-marked-files "find marked")
    ("G" dired-do-chgrp "chgrp")
    ("g" revert-buffer "revert buf")        ;; read all directories again (refresh)
    ("i" dired-maybe-insert-subdir "insert subdir")
    ("l" dired-do-redisplay "redisplay")   ;; relist the marked or singel directory
    ("M" dired-do-chmod "chmod")
    ("m" dired-mark "mark")
    ("O" dired-display-file "view other")
    ("o" dired-find-file-other-window "open other")
    ("Q" dired-do-find-regexp-and-replace "replace regex")
    ("R" dired-do-rename "rename")
    ("r" dired-do-rsynch "rsync")
    ("S" dired-do-symlink "symlink")
    ("s" dired-sort-toggle-or-edit "sort")
    ("t" dired-toggle-marks "toggle marks")
    ("U" dired-unmark-all-marks "unmark all")
    ("u" dired-unmark "unmark")
    ("v" dired-view-file "view")      ;; q to exit, s to search, = gets line #
    ("w" dired-kill-subdir "kill subdir")
    ("Y" dired-do-relsymlink "rel symlink")
    ("z" diredp-compress-this-file "compress files")
    ("Z" dired-do-compress "compress")
    ("^" dired-up-directory "up directory")
    ("<backspace>" dired-up-directory)
    ("q" +dired/quit-all "quit" :color blue)
    ("." nil "toggle hydra" :color blue))

Works most of the time! But if I exit the dired buffer in some unexpedted way (i.E. I suddenly decide that dired was not the way to go to my file but recent files are) the hydra stays. That is anyoing because the keybindings don't make sense in the new context.

Is there a way to close hydra when the current buffer is closed or unfocused in any way? Thanks!