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)
105 " First line has indent 0
110 " If we can find an open parenthesis/bracket/brace, line up with it.
111 call cursor(a:lnum, 1)
112 let parlnum = s:SearchParensPair()
114 let parcol = col('.')
115 let closing_paren = match(getline(a:lnum), '^\s*[])}]') != -1
116 if match(getline(parlnum), '[([{]\s*$', parcol - 1) != -1
118 return indent(parlnum)
120 return indent(parlnum) + &shiftwidth
128 let thisline = getline(a:lnum)
129 let thisindent = indent(a:lnum)
131 " If the line starts with 'elif' or 'else', line up with 'if' or 'elif'
132 if thisline =~ '^\s*\(elif\|else\)\>'
133 let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>')
135 return indent(bslnum)
141 " If the line starts with 'except' or 'finally', line up with 'try'
143 if thisline =~ '^\s*\(except\|finally\)\>'
144 let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>')
146 return indent(bslnum)
152 " Examine previous line
153 let plnum = a:lnum - 1
154 let pline = getline(plnum)
155 let sslnum = s:StatementStart(plnum)
157 " If the previous line is blank, keep the same indentation
162 " If this line is explicitly joined, find the first indentation that is a
163 " multiple of four and will distinguish itself from next logical line.
165 let maybe_indent = indent(sslnum) + &sw
166 let control_structure = '^\s*\(if\|while\|for\s.*\sin\|except\)\s*'
167 if match(getline(sslnum), control_structure) != -1
168 " add extra indent to avoid E125
169 return maybe_indent + &sw
171 " control structure not found
176 " If the previous line ended with a colon and is not a comment, indent
177 " relative to statement start.
178 let pline = substitute(pline, '\\\\', '', 'g')
179 if v:version > 703 || (v:version == 703 && has('patch1037'))
180 let pline = substitute(pline, '".\{-}\\\@1<!"\|''.\{-}\\\@1<!''', '', 'g')
182 let pline = substitute(pline, '".\{-}\\\@<!"\|''.\{-}\\\@<!''', '', 'g')
184 if pline =~ '^[^#]*:\s*\(#.*\)\?$'
185 return indent(sslnum) + &sw
188 " If the previous line was a stop-execution statement or a pass
189 if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
190 " See if the user has already dedented
191 if indent(a:lnum) > indent(sslnum) - &sw
192 " If not, recommend one dedent
193 return indent(sslnum) - &sw
195 " Otherwise, trust the user
199 " If this line is dedented and the number of indent spaces is valid
200 " (multiple of the indentation size), trust the user
201 let dedent_size = thisindent - indent(plnum)
202 if dedent_size < 0 && thisindent % &sw == 0
206 " In all other cases, line up with the start of the previous statement.
207 return indent(sslnum)