【问题标题】:Emacs persistent folding modeEmacs 持久折叠模式
【发布时间】:2011-01-29 14:44:51
【问题描述】:

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

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

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

【问题讨论】:

标签: emacs folding


【解决方案1】:

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

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

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

【讨论】:

  • 很好的解决方案,我刚刚在spacemacs中使用过,效果很好。
【解决方案2】:

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

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

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

【讨论】:

  • 感谢特雷的反馈。需要明确的是 - 我可以使用钩子为我喜欢的任何文件类型启用大纲次要模式。我正在寻找的是折叠一个代码条目,关闭缓冲区,然后当我重新打开文件时,我希望该条目仍然被折叠。
  • @Chris 我现在明白了......不知道这是可能的。
  • 这种持久性将存储在哪里?在文件本身?馊主意;其他文件?我想是可能的,但目前还没有这样的钩子可以做到这一点。而且我仍然怀疑将这些文件丢弃在所有地方。如果收集在一个文件夹中……哎呀,那将是巨大的。
  • @OtherMichael 将其存储在当前文件本身中如何...我有一个似乎可以在上面工作的解决方案。
  • 特雷,这太棒了。现在我需要弄清楚如何使用 hs-minor-mode 进行这项工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2012-03-10
  • 2011-07-11
相关资源
最近更新 更多