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

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

问题描述

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

What's 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 effect. I think I have heard emacs overlays are a good solution, but I don't 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 it's 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 actually paste the code into the buffer or if someone saves 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"了吗?输入Csfoo,找到定义,回车,阅读,然后按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-ximenu 将让您按名称跳转到函数定义(等).您还可以将其绑定到鼠标单击以获取功能菜单(或将其添加到菜单栏;有关更多详细信息,请参阅信息页面).它提供了 which-function-mode 的数据,这将让你看到你当前在模式行中的哪个函数.(不过,为什么你的函数这么长?)

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

还有 speedbar,它以图形方式显示 imenu 信息(和其他内容).

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

如果您想获得文件的概览,请尝试 Mxoccur".给定一个正则表达式,它将为当前缓冲区中的每个匹配项创建一个新缓冲区. 您可以搜索(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天全站免登陆