为什么 emacs 中没有代码折叠?

发布于 2024-08-24 09:45:28 字数 250 浏览 6 评论 0原文

关于如何在 emacs 中进行代码折叠,有几个问题,而不必在注释中添加任何特殊字符,例如“标记”。有人说“没有完美的解决方案。”
似乎可以通过解析正在编写的程序的源代码并查找匹配的括号或方括号来完成,或者基于缩进来完成。您还可以使用使用不同方法的脚本组合。
那么,为什么人们普遍认为在 emac 中没有“完美”且直接的方法来实现代码折叠呢? emacs 或其架构中是否有某些东西导致编程变得困难?如果它很容易的话,经过这么多年聪明人使用 emacs,你会认为有人会写它。

There are several questions on SO about how to get code folding in emacs, without having to add any special characters like "markers" in the comments for example. Someone said that there was "no perfect solution."
It seems that it could be done by parsing the source of the program being written and look for matching parenthesis or bracket, or to do it based on indentation. You could also use a combination of scripts that use different methods.
So why is it commonly accepted that there is no "perfect" and straightforward way to get code-folding in emac? Is there something in emacs or its architecture that makes it hard to program? If it were easy, after so many years of smart people using emacs you would think that someone would have wrote it.

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

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

发布评论

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

评论(7

花落人断肠 2024-08-31 09:45:28

您应该结合使用 Hideshow (hs-minor-mode)与 fold-dwim.el。它完全按照您的建议进行操作 - 查找匹配的大括号/括号,并且可以设置为依靠缩进。

对于大多数常见语言都有一个强大的折叠解决方案,如果没有,所有折叠包都是高度可定制的。事实上,唯一的缺点是折叠方法的激增(fold-dwim对此有很大帮助);我曾经认为,因为没有人能给我指出一个明确的解决方案,所以折叠是困难的或不可能的——事实上,事实恰恰相反。您只需进行一些尝试,看看什么最适合您。

我使用过folding.el(例如,将我的.emacs中的内容分组)、outline-minor-mode,现在又使用了Hideshow。有可能它们中没有一个能够完全按照您想要的开箱即用的方式工作(例如,您可能需要设置大纲正则表达式,或为 folding.el 定义折叠标记),但它事实证明很容易。默认的键绑定可能有点巴洛克风格,但这可以通过 fold-dwim 和/或 hideshow-org 来弥补(强烈推荐 Hideshow,参见 Emacswiki hideshow 页面;您可以还通过一些快速而肮脏的 elisp 和 fold-dwim 来模仿 hideshow-org 的其他折叠模式行为。一旦你确定了你的首选设置,只需通过钩子或缓冲区局部变量自动打开它,然后看着你的代码折叠起来:)

You should play with Hideshow (hs-minor-mode) combined with fold-dwim.el. It does exactly what you suggested -- looks for matching braces/parens, and can be set up to fall back on the indentation.

There's a robust folding solution out there for most common languages, and if there isn't, all the folding packages are highly customizable. In fact, the only downside is the proliferation of folding methods (fold-dwim helps quite a bit with that); I used to think that because nobody could point me to a definitive solution, folding was hard or impossible — in fact, the opposite is true. You just have to experiment a little to see what works best for you.

I have used folding.el (e.g. to group stuff in my .emacs), outline-minor-mode, and now Hideshow. There's some chance that none of them would work exactly the way you want right out of the box (e.g. you might need to set up an outline regex, or define folding marks for folding.el), but it turns out to be easy. The default keybindings can be somewhat baroque, but this is remedied by fold-dwim and/or hideshow-org (highly recommended for Hideshow, cf the Emacswiki hideshow page; you can also mimic hideshow-org's behavior for other folding modes with some quick-and-dirty elisp and fold-dwim). Once you figure out your preferred setup, just turn it on automatically via hooks or buffer-local variables, and watch your code fold away :)

暮倦 2024-08-31 09:45:28

你应该调查一下 CEDET。它的代码折叠功能很好,如果您从 IDE 切换到 Emacs,您可能正在寻找许多其他精美的功能。

http://cedet.sourceforge.net/

具体来说,查找 `global-semantic-tag-folding-模式'

You should look into CEDET. It does code-folding just fine, and many other fancy features that you're probably looking for if you're switching from an IDE to Emacs.

http://cedet.sourceforge.net/

Specifically, look for `global-semantic-tag-folding-mode'

江南烟雨〆相思醉 2024-08-31 09:45:28

您不需要任何额外的内容,只需为要折叠的文件类型启用大纲次要模式即可。

You don't need anything extra, just enable outline-minor-mode for file types you want to fold.

瞳孔里扚悲伤 2024-08-31 09:45:28

但事实上,Emacs 有多种解决方案;我在 http://en.wikipedia.org/w/index.php?title=Code_folding&oldid=375300945#cite_note-2

不过,我遗漏了一些东西:在某些情况下,我想结合几种机制:例如,对于降价,我想使用基于轮廓的折叠(对于部分)和基于缩进的折叠(对于引用、代码块等)——以便不必为 Markdown 实现完整的解析器而烦恼。

它们是:

Emacs 中基于令牌的折叠

Emacs 中基于令牌的折叠是通过 折叠来实现的次要模式

Emacs 中基于缩进的折叠

人们可以使用 Emacs 中的 set-selective-display 函数来根据缩进级别隐藏行,如 通用代码折叠注释。

Emacs 中的语法相关折叠

Emacs 中的语法相关折叠受以下支持:

几种折叠机构统一为
fold-dwim 界面。

另请参阅 http://www.emacswiki.org/emacs/CategoryHideStuff

Emacs 中用户选择区域的折叠

Emacs 中用户选择区域的折叠是通过 hide 实现的-region-hide 命令。

But in fact, there ARE various solutions for Emacs; I have listed some of them (those I have happened to come across) at http://en.wikipedia.org/w/index.php?title=Code_folding&oldid=375300945#cite_note-2.

Though, there are things I'm missing: in some cases, I'd like to combine several mechanisms: for example, for markdown, I'd like to use outline-based folding (for sections) and indentation-based folding (for quotations, code blocks etc.) -- in order not bother with implementing a complete parser for markdown.

Here they are:

Token-based folding in Emacs

Token-based folding in Emacs is impemented by the folding minor mode.

Indentation-based folding in Emacs

One can use the set-selective-display function in Emacs to hide lines based on the indentation level, as suggested in the Universal code folding note.

Syntax-dependent folding in Emacs

Syntax-dependent folding in Emacs is supported by:

Several folding mechanisms are unified by the
fold-dwim interface.

See also http://www.emacswiki.org/emacs/CategoryHideStuff.

Folding of user-selected regions in Emacs

Folding of user-selected regions in Emacs is implemented by the hide-region-hide command.

吹泡泡o 2024-08-31 09:45:28

我已经使用 折叠模式有一段时间了。使用自动插入模板和缩写,它对我来说非常适合一些漂亮的代码块。

能够生产折叠的缓冲区(用于打印/电子邮件)一直是我的愿望。我的一些折叠标签用于安全/密码隐藏。

I have been using folding-mode for quite some time. With auto-insert template and abrevs it works quite well for me for for some nice bricks of code.

Being able to produce the buffer folded (for printing/emailing) has always been a desire of mine. Some of my folding tags are for secure / password hiding.

墨小墨 2024-08-31 09:45:28

我知道这有点旧,但对我来说 origami.el 开箱即用,效果很好。

I know this is a bit old but for me origami.el works perfectly well out of the box.

虐人心 2024-08-31 09:45:28

是的,emacs 中终于有了代码折叠。尝试 melpa.org 包库中的 yafolding。

Yes Finally code folding is there in emacs. Try yafolding present at melpa.org package library.

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