Skip to content

Instantly share code, notes, and snippets.

@dustinlacewell-wk
Last active February 7, 2018 07:58
Show Gist options
  • Save dustinlacewell-wk/2c56aa029729c327b4f8dd6ab51c6ded to your computer and use it in GitHub Desktop.
Save dustinlacewell-wk/2c56aa029729c327b4f8dd6ab51c6ded to your computer and use it in GitHub Desktop.

Tidy Mode

As the point moves around, headings will be automatically folded.

(defun org-tidy--tidy (&rest args)
  (save-excursion
    (org-global-cycle 1)
    (org-cycle)
    (let ((last org-cycle-subtree-status))
      (while (not (eq 'folded org-cycle-subtree-status))
        (org-cycle)
        (if (eq last org-cycle-subtree-status)
            (setq org-cycle-subtree-status 'folded)
          (setq last org-cycle-subtree-status))))
    (recenter)))

(defun org-tidy-tidy-up ()
  (interactive)
  (org-tidy--tidy))

(defun org-tidy-on ()
  (interactive)
  (advice-add 'org-backward-heading-same-level :after #'org-tidy--tidy)
  (advice-add 'org-forward-heading-same-level :after #'org-tidy--tidy))

(defun org-tidy-off ()
  (interactive)
  (advice-remove 'org-backward-heading-same-level #'org-tidy--tidy)
  (advice-remove 'org-forward-heading-same-level #'org-tidy--tidy))

(defun org-tidy-p ()
  (if (and (advice-member-p 'org-tidy--tidy #'org-backward-heading-same-level)
           (advice-member-p 'org-tidy--tidy #'org-forward-heading-same-level))
      t nil))

(defun org-tidy ()
  (interactive)
  (if (org-tidy-p)
      (call-interactively 'org-tidy-off)
    (call-interactively 'org-tidy-on))
  (org-tidy-p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment