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 " Last Change: 2012-06-21
7 " License: Public Domain
9 " Only load this indent file when no other was loaded.
10 if exists("b:did_indent")
18 setlocal indentexpr=GetPythonPEPIndent(v:lnum)
19 setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except
23 " Find backwards the closest open parenthesis/bracket/brace.
24 function! s:SearchParensPair()
28 " Skip strings and comments and don't look too far
29 let skip = "line('.') < " . (line - s:maxoff) . " ? dummy :" .
30 \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' .
31 \ '"string\\|comment"'
33 " Search for parentheses
34 call cursor(line, col)
35 let parlnum = searchpair('(', '', ')', 'bW', skip)
39 call cursor(line, col)
40 let par2lnum = searchpair('\[', '', '\]', 'bW', skip)
41 let par2col = col('.')
44 call cursor(line, col)
45 let par3lnum = searchpair('{', '', '}', 'bW', skip)
46 let par3col = col('.')
48 " Get the closest match
49 if par2lnum > parlnum || (par2lnum == parlnum && par2col > parcol)
50 let parlnum = par2lnum
53 if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol)
54 let parlnum = par3lnum
58 " Put the cursor on the match
60 call cursor(parlnum, parcol)
65 " Find the start of a multi-line statement
66 function! s:StatementStart(lnum)
69 if getline(lnum - 1) =~ '\\$'
73 let maybe_lnum = s:SearchParensPair()
83 " Find the block starter that matches the current line
84 function! s:BlockStarter(lnum, block_start_re)
86 let maxindent = 10000 " whatever
88 let lnum = prevnonblank(lnum - 1)
89 if indent(lnum) < maxindent
90 if getline(lnum) =~ a:block_start_re
93 let maxindent = indent(lnum)
94 " It's not worth going further if we reached the top level
104 function! GetPythonPEPIndent(lnum)
106 " First line has indent 0
111 " If we can find an open parenthesis/bracket/brace, line up with it.
112 call cursor(a:lnum, 1)
113 let parlnum = s:SearchParensPair()
115 let parcol = col('.')
116 let closing_paren = match(getline(a:lnum), '^\s*[])}]') != -1
117 if match(getline(parlnum), '[([{]\s*$', parcol - 1) != -1
119 return indent(parlnum)
121 return indent(parlnum) + &shiftwidth
129 let thisline = getline(a:lnum)
130 let thisindent = indent(a:lnum)
132 " If the line starts with 'elif' or 'else', line up with 'if' or 'elif'
133 if thisline =~ '^\s*\(elif\|else\)\>'
134 let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>')
136 return indent(bslnum)
142 " If the line starts with 'except' or 'finally', line up with 'try'
144 if thisline =~ '^\s*\(except\|finally\)\>'
145 let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>')
147 return indent(bslnum)
153 " Examine previous line
154 let plnum = a:lnum - 1
155 let pline = getline(plnum)
156 let sslnum = s:StatementStart(plnum)
158 " If the previous line is blank, keep the same indentation
163 " If this line is explicitly joined, find the first indentation that is a
164 " multiple of four and will distinguish itself from next logical line.
166 let maybe_indent = indent(sslnum) + &sw
167 let control_structure = '^\s*\(if\|while\|for\s.*\sin\|except\)\s*'
168 if match(getline(sslnum), control_structure) != -1
169 " add extra indent to avoid E125
170 return maybe_indent + &sw
172 " control structure not found
177 " If the previous line ended with a colon and is not a comment, indent
178 " relative to statement start.
179 if pline =~ '^[^#]*:\s*\(#.*\)\?$'
180 return indent(sslnum) + &sw
183 " If the previous line was a stop-execution statement or a pass
184 if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
185 " See if the user has already dedented
186 if indent(a:lnum) > indent(sslnum) - &sw
187 " If not, recommend one dedent
188 return indent(sslnum) - &sw
190 " Otherwise, trust the user
194 " If this line is dedented and the number of indent spaces is valid
195 " (multiple of the indentation size), trust the user
196 let dedent_size = thisindent - indent(plnum)
197 if dedent_size < 0 && thisindent % &sw == 0
201 " In all other cases, line up with the start of the previous statement.
202 return indent(sslnum)