All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " PEP8 compatible Python indent file
3 " Maintainer: Hynek Schlawack <hs@ox.cx>
4 " Prev Maintainer: Eric Mc Sween <em@tomcom.de> (address invalid)
5 " Original Author: David Bustos <bustos@caltech.edu> (address invalid)
6 " License: Public Domain
8 " Only load this indent file when no other was loaded.
9 if exists("b:did_indent")
17 setlocal indentexpr=GetPythonPEPIndent(v:lnum)
18 setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except
22 " Find backwards the closest open parenthesis/bracket/brace.
23 function! s:SearchParensPair()
27 " Skip strings and comments and don't look too far
28 let skip = "line('.') < " . (line - s:maxoff) . " ? dummy :" .
29 \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' .
30 \ '"string\\|comment"'
32 " Search for parentheses
33 call cursor(line, col)
34 let parlnum = searchpair('(', '', ')', 'bW', skip)
38 call cursor(line, col)
39 let par2lnum = searchpair('\[', '', '\]', 'bW', skip)
40 let par2col = col('.')
43 call cursor(line, col)
44 let par3lnum = searchpair('{', '', '}', 'bW', skip)
45 let par3col = col('.')
47 " Get the closest match
48 if par2lnum > parlnum || (par2lnum == parlnum && par2col > parcol)
49 let parlnum = par2lnum
52 if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol)
53 let parlnum = par3lnum
57 " Put the cursor on the match
59 call cursor(parlnum, parcol)
64 " Find the start of a multi-line statement
65 function! s:StatementStart(lnum)
68 if getline(lnum - 1) =~ '\\$'
72 let maybe_lnum = s:SearchParensPair()
82 " Find the block starter that matches the current line
83 function! s:BlockStarter(lnum, block_start_re)
85 let maxindent = 10000 " whatever
87 let lnum = prevnonblank(lnum - 1)
88 if indent(lnum) < maxindent
89 if getline(lnum) =~ a:block_start_re
92 let maxindent = indent(lnum)
93 " It's not worth going further if we reached the top level
103 function! GetPythonPEPIndent(lnum)
104 " First line has indent 0
109 " If we can find an open parenthesis/bracket/brace, line up with it.
110 call cursor(a:lnum, 1)
111 let parlnum = s:SearchParensPair()
113 let parcol = col('.')
114 let closing_paren = match(getline(a:lnum), '^\s*[])}]') != -1
115 if match(getline(parlnum), '[([{]\s*$', parcol - 1) != -1
117 return indent(parlnum)
119 return indent(parlnum) + &shiftwidth
127 let thisline = getline(a:lnum)
128 let thisindent = indent(a:lnum)
130 " If the line starts with 'elif', line up with 'if' or 'elif'
131 if thisline =~ '^\s*elif\>'
132 let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>')
134 return indent(bslnum)
140 " If the line starts with 'except' line up with 'try' or 'except'.
141 if thisline =~ '^\s*except\>'
142 let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>')
144 return indent(bslnum)
150 " If the line starts with 'finally', line up with 'try', 'else', or
152 if thisline =~ '^\s*finally\>'
153 let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\|else\)\>')
155 return indent(bslnum)
161 " If the line starts with 'else', line it up with 'try', 'except', 'for',
163 if thisline =~ '^\s*else\>'
165 let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\|if\|elif\|for\)\>')
167 return indent(bslnum)
173 " Examine previous line
174 let plnum = a:lnum - 1
175 let pline = getline(plnum)
176 let sslnum = s:StatementStart(plnum)
178 " If the previous line is blank, keep the same indentation
183 " If this line is explicitly joined, find the first indentation that is a
184 " multiple of four and will distinguish itself from next logical line.
186 let maybe_indent = indent(sslnum) + &sw
187 let control_structure = '^\s*\(if\|while\|for\s.*\sin\|except\)\s*'
188 if match(getline(sslnum), control_structure) != -1
189 " add extra indent to avoid E125
190 return maybe_indent + &sw
192 " control structure not found
197 " If the previous line ended with a colon and is not a comment, indent
198 " relative to statement start.
199 let pline = substitute(pline, '\\\\', '', 'g')
200 if v:version > 703 || (v:version == 703 && has('patch1037'))
201 let pline = substitute(pline, '".\{-}\\\@1<!"\|''.\{-}\\\@1<!''', '', 'g')
203 let pline = substitute(pline, '".\{-}\\\@<!"\|''.\{-}\\\@<!''', '', 'g')
205 if pline =~ '^[^#]*:\s*\(#.*\)\?$'
206 return indent(sslnum) + &sw
209 " If the previous line was a stop-execution statement or a pass
210 if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
211 " See if the user has already dedented
212 if indent(a:lnum) > indent(sslnum) - &sw
213 " If not, recommend one dedent
214 return indent(sslnum) - &sw
216 " Otherwise, trust the user
220 " If this line is dedented and the number of indent spaces is valid
221 " (multiple of the indentation size), trust the user
222 let dedent_size = thisindent - indent(plnum)
223 if dedent_size < 0 && thisindent % &sw == 0
227 " In all other cases, line up with the start of the previous statement.
228 return indent(sslnum)