Vim logo vim online Vim Book Ad

 Tip #333: Syntax-based folding for c/c++/java

 tip karma   Rating 19/10, Viewed by 2150 

created:   September 23, 2002 18:32      complexity:   intermediate
author:   Kartik Agaram      as of Vim:   6.0

Here's a function to toggle the use of syntax-based folding for a c/c++/java file. It also handles folding markers.

function! OutlineToggle()
    if (! exists ("b:outline_mode"))
        let b:outline_mode = 0
    endif

    if (b:outline_mode == 0)
        syn region myFold start="{" end="}" transparent fold
        syn sync fromstart
        set foldmethod=syntax

        silent! exec "%s/{{{/<<</"
        silent! exec "%s/}}}/>>>/"

        let b:outline_mode = 1
    else
        set foldmethod=marker

        silent! exec "%s/<<</{{{/"
        silent! exec "%s/>>>/}}}/"

        let b:outline_mode = 0
    endif
endfunction

 rate this tip  Life Changing Helpful Unfulfilling 

<<make footnotes in vim | Loading scripts in .vimrc safely >>

Additional Notes

[email protected], September 24, 2002 5:47
I took Kartik's implementation and played with it, then played with it some more and finally got it to work.
The "syn region myFold start="{" end="}" transparent fold" method does not like to work when you have other syn groups for {,}. I found this annoying (as I like my colored Braces!) so I figured a pure marker method would work nearly (or just as) good, and it does at least for my uses.

Hopefully everything works just did v0.03 and I have run out of time, enjoy!

outlinetoggle.vim from my plugin dir>>>
" vim:ff=unix ts=4 ss=4
" vim60:fdm=marker
" \file outlinetoggle.vim
"
" \brief VIM-Tip #333: Syntax-based folding for c/c++/java
"
" \author author:   Kartik Agaram as of Vim:   6.0
" \author Mangled by [email protected]
" \date Tue, 24 Sep 2002 05:44 Pacific Daylight Time
" \version $Id$
" Version: 0.03
" History:
" [Feral:267/02@05:43] v0.03:
" saves the old marker method now.
" stoped trying to be clever (just do the command twice heh)
" v0.02
" trys to be cleaver and start in the proper outline mode based on if it
" finds a "{>>" in the file.


"if exists("loaded_outlinetoggle")
" finish
"endif
"let loaded_outlinetoggle = 1


" Tip #333: Syntax-based folding for c/c++/java
" tip karma Rating 0/0, Viewed by 88
"
"created:   September 23, 2002 18:32 complexity:   intermediate
"author:   Kartik Agaram as of Vim:   6.0
"
"Here's a function to toggle the use of syntax-based folding for a c/c++/java file. It also handles folding markers.

function! <SID>OutlineToggle()
let OldLine = line(".")
let OldCol = virtcol(".")
if (! exists ("b:outline_mode"))
let b:outline_mode = 0
let b:OldMarker = &foldmarker;
" :echo confirm(b:OldMarker)
" if search("{>>", 'w') == 0
" " no modifed marker found, must be normal marker mode
" let b:outline_mode = 0
" else
" let b:outline_mode = 1
" endif
endif


if (b:outline_mode == 0)
let b:outline_mode = 1
" syn region myFold start="{" end="}" transparent fold
" syn sync fromstart
" set foldmethod=syntax
" set foldmethod=indent

set foldmethod=marker
set foldmarker={,}

silent! exec "%s/{{{/{<</"
silent! exec "%s/}}}/}>>/"
else
let b:outline_mode = 0
set foldmethod=marker
let &foldmarker;=b:OldMarker
" set foldmarker={{{,}}}

silent! exec "%s/{<</{{{/"
silent! exec "%s/}>>/}}}/"
endif

execute "normal! ".OldLine."G"
execute "normal! ".OldCol."|"
unlet OldLine
unlet OldCol
execute "normal! zv"
endfunction

"*****************************************************************
"* Commands
"*****************************************************************
:command! -nargs=0 OUTLINE call <SID>OutlineToggle()

"
"EOF
<<<
Kartik Agaram, September 25, 2002 20:52
Nice. Not changing foldmethod is definitely more elegant.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo