如何在Emacs中实现代码折叠效果? [英] How to achieve code folding effects in Emacs?

查看:149
本文介绍了如何在Emacs中实现代码折叠效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现代码折叠或组织模式使用的循环类型的最佳方法是什么?创建此类行为的最佳解决方案是什么?

Whats the best way to achieve something like code folding, or the type of cycling that org-mode uses. What would be the best solution in elisp to create this type of behavior?

编辑:
对不起,我还不清楚。我想在elisp中编写一些与代码折叠非常相似的东西,或者实际上最喜欢具有可扩展层次结构的组织模式。我想知道实现这一影响的最佳途径。我想我听说过emacs的覆盖是一个很好的解决方案,但我不知道。

I'm sorry I was not clear. I want to program something in elisp that does things very similar to code folding, or actually most like org-mode with the hierarchy that can be expanded. I am wondering the best way to achieve this affect. I think I have heard emacs overlays are a good solution, but I dont know.

至于折叠我只是使用内置的set-selective-display

As far as folding I just use the builtin set-selective-display

编辑编号2:

感谢您的答案,但我认为我提出错误的问题,让我尝试更多清楚我正在努力做什么我想创建以下

Thanks for the answers but I think I am asking the wrong question so let me try to be more clear on what I am trying to do. I would like to create the following

当您将要点放在一个函数上并调用这个elisp函数时,它将把函数定义从任何位置(我在想只需使用find-tag即可)并在当前缓冲区中展开它。这个想法是,如果你必须跳转到不同的缓冲区来读取功能定义,我觉得它的上下文切换到另一个文件。所以我希望它的行为像代码折叠只是从缓冲区中提取外部代码。这会出现一些问题,因为它不能将代码粘贴到缓冲区中,或者如果有人保存它,它将保存代码中的代码。所以我想知道是否有一种方法可以在缓冲区内创建一个也不是缓冲区的一部分。我认为这是有道理的。

When you put your point on a function and call this elisp function it will put the function definition from wherever it is (I am thinking of just using find-tag for this) and unfold it in the current buffer. The idea is if you have to jump to a different buffer to read the function definition I feel like its a context switch to another file. So I would like it to behave like code folding only it pulls in code external from the buffer. This presents some problems as it can not acutally paste the code into the buffer or if someone saves it it will save the pulled in code. So I am wondering if there is a way to create an area inside a buffer that is also not part of the buffer. I think that makes sense.

推荐答案

emacs通常不需要折叠,因为它具有明确实现人们手动执行的操作的工具折叠代码。

Folding is generally unnecessary with emacs, as it has tools that explicitly implement the actions people do manually when folding code.

大多数人通过简单的增量搜索获得了很好的成功。看到某个地方的foo?键入 Cs foo ,找到定义,按回车键,读取它,然后按 Cx Cx 回到你所在的地方。简单而非常实用。

Most people have good success with simple incremental searches. See "foo" mentioned somewhere? Type C-sfoo, find the definition, press enter, read it, and then press C-x C-x to go back to where you were. Simple and very useful.

大多数模式支持 imenu M-x imenu 将让您按名称跳转到函数定义(等)。您还可以将其绑定到鼠标单击以获取功能菜单(或将其添加到菜单栏;有关详细信息,请参阅信息页面)。它提供了哪些功能模式的数据,这将让您看到模型中当前正在使用哪些功能。 (为什么你的功能很长,但是?)

Most modes support imenu. M-ximenu will let you jump to a function definition (etc.) by name. You can also bind it to a mouse click to get a menu of functions (or add it to the menubar; see the Info page for more detail). It provides data for which-function-mode, which will let you see which function you are currently inside in the modeline. (Why are your functions this long, though?)

还有一个速度栏,它以图形方式显示了imenu信息(和其他内容)。

There is also speedbar, which displays the imenu information (and other things) graphically.

如果您想了解您的文件概述,请尝试 Mx 发生给定一个正则表达式,它将创建一个新的缓冲区,每个匹配在当前缓冲区中,您可以搜索(defun)来获取当前文件实现的功能的概述,点击结果会将您移动到该位置该文件。

If you want to get an overview of your file, try M-xoccur". Given a regex, it will create a new buffer with each match in the current buffer. You can search for "(defun" to get an overview of the functions the current file implements. Clicking on the result will move you to that position in the file.

所以无论如何,想想你真正想要实现什么,Emacs可能实现了这一点。不要与不完美的工具打架,使你折叠和展开东西不断。

So anyway, think about what you really want to achieve, and Emacs probably implements that. Don't fight with imperfect tools that make you fold and unfold things constantly.

这篇关于如何在Emacs中实现代码折叠效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆