3

I want to combine the commands hs-hide-all and hs-show-all.

It would be great if I'd have a function that detects the "status" of all blocks. If there are blocks that aren't hidden, the function should hide them, thus all blocks are hidden. Applying it when all blocks are hidden, the function ought to show all of them.

bertfred
  • 1,699
  • 1
  • 11
  • 23

1 Answers1

4

hideshow uses overlays to hide text. Because of this you can make the semi-maybe-safe assumption that if the number of overlays in the current buffer doesn't increase after running hs-hide-all, then all top level blocks must have been already hidden.

(defun my-hs-toggle-all ()
  "If anything isn't hidden, run `hs-hide-all', else run `hs-show-all'."
  (interactive)
  (let ((starting-ov-count (length (overlays-in (point-min) (point-max)))))
    (hs-hide-all)
    (when (equal (length (overlays-in (point-min) (point-max))) starting-ov-count)
      (hs-show-all))))

I couldn't see an obvious "right" way to do this in hideshow but this hack seems to work.

Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62