Emacs 持久折叠模式

发布于 2024-08-26 08:52:08 字数 157 浏览 6 评论 0原文

在 Emacs 中折叠代码的方法有很多,我决定使用大纲次要模式……它效果很好!

但是,我真的希望在关闭并重新打开文件时保留我的折叠。按照我喜欢的方式在文件中设置折叠,但在重新启动 Emacs 时却丢失了,这真是令人沮丧。

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

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 when I close and re-open files. It is quite frustrating to have folding set up in a file the way I like it, only to have that lost when I restart Emacs.

Has anyone found a way to keep the folding state of a file persistent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2024-09-02 08:52:08

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

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

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

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:            
乖乖兔^ω^ 2024-09-02 08:52:08

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文