I have used foldmethod=ignore
almost exclusively. However, my desire to have ignored lines default to the higher fold level of the above or below lines, instead of the lower, inspired the following:
" Default foldmethod
" Similar to fdm=indent, but lets blank and comment lines default high.
set fen fdm=expr fdi=
set foldexpr=EswaldFoldLevel(v:lnum)
function! EswaldFoldLevel(lnum)
let ignored = ^s*([#/*]|$)
if getline(a:lnum) !~ ignored
" In the general case, just use the indent level.
" It would be nice if it didn t skip several levels...
return indent(a:lnum) / &sw
endif
let previndent = 0
let prevnum = a:lnum - 1
while prevnum > 0
if getline(prevnum) =~ ignored
let prevnum = prevnum - 1
else
let previndent = indent(prevnum) / &sw
break
endif
endwhile
let nextindent = 0
let maxline = line( $ )
let nextnum = a:lnum + 1
while nextnum <= maxline
if getline(nextnum) =~ ignored
let nextnum = nextnum + 1
else
let nextindent = indent(nextnum) / &sw
break
endif
endwhile
return max([previndent, nextindent])
endfunction
(Sorry about the syntax highlighting...)
I use that as a custom plugin, letting individual filetypes override it. Python, for example, doesn t want to look at previous lines, just following ones.