Vim 中的紧凑型 C 折叠
我正在尝试制作一个简单的 Vim 脚本,它可以为 c 文件创建非常紧凑的顶级折叠。 理想情况下,如果它在这段代码上运行:
static void funca(...)
{
...
}
/* Example comment */
static void funcb(...)
{
...
}
那么它会创建折叠,关闭时看起来像这样:
+-- x Lines: static void funca(...)----------------------
+-- x Lines: static void funcb(...)----------------------
所以基本上它会像 Foldmethod=syntax 和 Foldlevel=1 一样,除了每个折叠都会向上一行开始,并且会进一步向下延伸以包含以下所有空白行。
我知道如何进行这些折叠之一(假设foldmethod=manual):
/^{<cr>kVnn?^$<cr>zf
但我不确定如何将其放入函数中。 这是我的努力:
function Cfold()
set foldmethod=manual " Manual folds
ggzE " Delete all folds
while (/^{<cr>) " Somehow loop through each match
kVnn?^$<cr>zf " This would work fine except for the last function
endwhile
endfunction
map <Leader>f :call Cfold()<cr>
但它无效,我不完全确定函数是如何工作的。 此外,它不适用于文件中的最后一个函数,因为它不会再次找到“^{”。 如果有人可以帮助我完成这项工作,并以某种方式为文件中的最后一个函数添加一个案例,我将非常感激。
提前致谢 :)
I'm trying to make a simple Vim script that would create very compact top-level folds for c files. Ideally, if it was run on this code:
static void funca(...)
{
...
}
/* Example comment */
static void funcb(...)
{
...
}
Then it would create folds which would look like this when closed:
+-- x Lines: static void funca(...)----------------------
+-- x Lines: static void funcb(...)----------------------
So basically it would be like foldmethod=syntax with foldlevel=1, except that each fold would start one line further up, and would extend further down to include all following blank lines.
I know how to make one of these folds (assuming foldmethod=manual):
/^{<cr>kVnn?^lt;cr>zf
But I'm not sure how to put it into a function. This is my effort:
function Cfold()
set foldmethod=manual " Manual folds
ggzE " Delete all folds
while (/^{<cr>) " Somehow loop through each match
kVnn?^lt;cr>zf " This would work fine except for the last function
endwhile
endfunction
map <Leader>f :call Cfold()<cr>
But it isn't valid, I'm not entirely sure how functions work. Also, it won't work for the last function in the file, since it won't find '^{' again. If someone could help me get this working, and somehow add a case for the last function in a file, I would be extremely grateful.
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
foldexpr
和foldtext
以编程方式创建折叠。 尝试一下这个,尽管您可能需要调整CFoldLevel
以便它不会吞掉代码的非功能部分:请参阅
:help 'foldexpr'
了解更多详细信息。You can create folds programmatically using the
foldexpr
andfoldtext
. Try this, though you may have to tweakCFoldLevel
so it doesn't swallow non-function parts of the code:See
:help 'foldexpr'
for more details.