13

在 Emacs 中有很多折叠代码的方法,我已经决定使用大纲次要模式......它工作得很好!

但是,我真的希望我的折叠在我关闭和重新打开文件时保持不变。以我喜欢的方式在文件中设置折叠是非常令人沮丧的,只是在我重新启动 Emacs 时丢失了。

有没有人找到一种方法来保持文件的折叠状态持久?

4

2 回答 2

6

编辑:现在我明白了这个问题......

像下面的代码片段怎么样。它似乎对我有用,尽管我还没有弄清楚如何避免每次都被提示输入文件局部变量。

(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)

此解决方案要求您使用以下内容“播种”您的文件:

# Local Variables:
# omm-state:()
# mode:omm-state
# End:            
于 2010-03-19T19:48:53.740 回答
3

我意识到这是一篇旧帖子,但 FWIW 我创建了一个次要模式来补充 hs-minor-mode、outline-mode 等。我也“真的希望我的折叠在我关闭和重新打开文件时保持不变”。:)

该软件包截至今天在 MELPA 中,称为持久覆盖。

它也可以直接在 github 上获得:https ://github.com/mneilly/Emacs-Persistent-Overlays

于 2016-03-12T07:22:04.787 回答