Emacs persistent folding mode

前端 未结 2 1660
予麋鹿
予麋鹿 2021-02-19 13:59

There are plenty of ways to fold code in Emacs and I\'ve settled in on using the outline minor mode... it works great!

However, I really want my folding to be persisted

相关标签:
2条回答
  • 2021-02-19 14:11

    Edit: Now that I understand the question...

    How about something like the following nippet of code. It seems to work for me, though I haven't figured out how to avoid being prompted for the file local variable every time.

    (defvar omm-state nil
      "file local variable storing outline overlays")
    (defun omm-state-mode (&optional arg)
      "poor man's minor mode to re-apply the outline overlays "
      (interactive)
      (omm-re-enable-outline-state)
      (add-hook 'before-save-hook 'omm-state-save))
    (defun omm-get-all-overlays ()
      "return a list of outline information for all the current buffer"
      (save-excursion
        (let ((all-overlays (overlays-in (point-min) (point-max))))
          (mapcar (lambda (o)
                    (list (overlay-start o) (overlay-end o) (overlay-get o 'invisible)))
                  (reverse all-overlays)))))
    (defun omm-re-enable-outline-state (&optional arg)
      "turn on outline-minor-mode and re-apply the outline information"
      (outline-minor-mode 1)
      (when (listp omm-state)
        (mapcar (lambda (p)
                  (apply 'outline-flag-region p))
                omm-state)))
    (defun omm-state-save ()
      "save the outline state in a file local variable
    Note: this just replaces the existing value, you need to start
    it off by adding something like this to your file:
    
    # Local Variables:
    # omm-state:()
    # mode:omm-state
    # End:            
    "
      (ignore-errors
        (save-excursion
          (goto-char (point-max))
          (when (search-backward "omm-state:" nil t)
            (goto-char (match-end 0))
            (kill-sexp)
            (princ (omm-get-all-overlays) (current-buffer)))))
      nil)
    

    This solution requires you "seeding" your file with something like:

    # Local Variables:
    # omm-state:()
    # mode:omm-state
    # End:            
    
    0 讨论(0)
  • 2021-02-19 14:13

    I realize this is an old post but FWIW I created a minor mode that complements hs-minor-mode, outline-mode etc. I also "really want my folding to be persisted when I close and re-open files". :)

    The package is in MELPA as of today and called persistent-overlays.

    It is also available directly on github: https://github.com/mneilly/Emacs-Persistent-Overlays

    0 讨论(0)
提交回复
热议问题