11" Vim folding file
22" Language: Python
33" Author: Jorrit Wiersma
4- " Last Change: 2002 Dec 19
4+ " Last Change: 2003 May 08
55
66
77setlocal foldmethod = expr
8- setlocal foldexpr = GetPythonFold ()
8+ setlocal foldexpr = GetPythonFold (v: lnum )
99
1010" Only define function once
1111if exists (" *GetPythonFold" )
1212 finish
1313endif
1414
15- function GetPythonFold ()
15+ function GetPythonFold (lnum )
1616 " Determine folding level in Python source
1717 "
18- let pnum = prevnonblank (v: lnum - 1 )
19-
20- if pnum == 0
21- " Hit start of file
22- return 0
23- endif
24-
25- let line = getline (v: lnum )
26- let ind = indent (v: lnum )
18+ let line = getline (a: lnum )
19+ let ind = indent (a: lnum )
2720
2821 " Ignore blank lines
2922 if line = ~ ' ^\s*$'
@@ -35,6 +28,11 @@ function GetPythonFold()
3528 return " ="
3629 endif
3730
31+ " Ignore continuation lines (this should be done better)
32+ if line = ~ ' \\$'
33+ return ' ='
34+ endif
35+
3836 " Support markers
3937 if line = ~ ' {{{'
4038 return " a1"
@@ -47,14 +45,28 @@ function GetPythonFold()
4745 return " >" . (ind / &sw + 1 )
4846 endif
4947
48+ let pnum = prevnonblank (a: lnum - 1 )
49+
50+ if pnum == 0
51+ " Hit start of file
52+ return 0
53+ endif
54+
5055 " The end of a fold is determined through a difference in indentation
5156 " between this line and the next.
5257 " So first look for next line
53- let nnum = nextnonblank (v : lnum + 1 )
58+ let nnum = nextnonblank (a : lnum + 1 )
5459 if nnum == 0
5560 return " ="
5661 endif
5762
63+ " First I check for some common cases where this algorithm would
64+ " otherwise fail. (This is all a hack)
65+ let nline = getline (nnum)
66+ if nline = ~ ' ^\s*\(except\|else\|elif\)'
67+ return " ="
68+ endif
69+
5870 " If next line has less indentation we end a fold.
5971 " This ends folds that aren't there a lot of the time, and this sometimes
6072 " confuses vim. Luckily only rarely.
0 commit comments