vim expression to fold latex documents

78 views
Skip to first unread message

Antonio Recio

unread,
Feb 20, 2012, 10:49:29 AM2/20/12
to vim...@googlegroups.com
When I open latex files I would like to be able to fold with one vim expression on the top of the file from \htitle to \clearpage.

I have a lot of blocks like this:
\htitle{Latex Title}
bla bla bla
bla bla bla
\clearpage

And I would like to get this
+ Latex Title

Christian Brabandt

unread,
Feb 20, 2012, 11:55:33 AM2/20/12
to vim...@googlegroups.com
Really, this is only very little tested, but seems to work so far:

fu! FoldTeXLines(lnum)
if !exists("s:fold")
let s:fold = 0
endif
let line = getline(a:lnum)
if line =~ '^\s*\\htitle'
let s:fold = 1
return s:fold
elseif line =~ '^\s*\\clearpage'
let s:fold = 0
return 1
endif
return s:fold
endfu

fu! FoldText(line)
return '+ '. matchstr(getline(a:line),'^\s*\\htitle{\zs.*\ze}')
endfu

setl fdm=expr fde=FoldTeXLines(v:lnum) fdt=FoldText(v:foldstart)


regards,
Christian

Antonio Recio

unread,
Feb 20, 2012, 1:06:00 PM2/20/12
to vim...@googlegroups.com
That's works great!

I would like to include too the folding of \subtitle->\clearpage.

How I can fold these lines:
htitle 
----
\clearpage

htitle
subtitle 
----
\clearpage

subtitle 
----
\clearpage

To this?:
+ htitle
+ htitle
+ subtitle

Christian Brabandt

unread,
Feb 20, 2012, 2:25:59 PM2/20/12
to vim...@googlegroups.com
Hi Antonio!

Try this:

fu! FoldTeXLines(lnum)
if !exists("s:fold")
let s:fold = '0'
endif
let line = getline(a:lnum)
if line =~ '^\s*\\htitle'
let s:fold = '>1'
return s:fold

elseif line =~ '^\s*\\subtitle' && s:fold != '>1'


let s:fold = '>1'
return s:fold
elseif line =~ '^\s*\\clearpage'

let s:fold = '<1'
return 1
endif
return '='
endfu

fu! FoldText(line)
return '+ '. matchstr(getline(a:line),'^\s*\\\(h\|sub\)title{\zs.*\ze}')
endfu

setl fdm=expr fde=FoldTeXLines(v:lnum) fdt=FoldText(v:foldstart)


regards,
Christian
--
Wie man sein Kind nicht nennen sollte:
Asta Lavista

Christian Brabandt

unread,
Feb 20, 2012, 3:06:29 PM2/20/12
to vim...@googlegroups.com
Hi vim_use!

That looks a little bit odd. Better is this:


fu! FoldTeXLines(lnum)
if !exists("s:fold")
let s:fold = '0'
endif
let line = getline(a:lnum)
if line =~ '^\s*\\htitle'
let s:fold = '>1'

elseif line =~ '^\s*\\subtitle' && s:fold != '>1'
let s:fold = '>1'

elseif line =~ '^\s*\\clearpage'
let s:fold = '<1'

else
let s:fold = '='


endif
return s:fold
endfu


regards,
Christian
--
Ein Stotterer und ein Mann mit Glatze unterhalten sich.
Stotterer: "Wwwiiie tteuer wa wa war dddenn Ddeinnne Fri Frisur?"
Glatzkopf: "Oh, nur 26 Mark, genau so teuer wie bei Dir ein Ortsgespr�ch!"

Reply all
Reply to author
Forward
0 new messages