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=GetPythonIndent(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! GetPythonIndent(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, try to find an indentation that looks
166 let compound_statement = '^\s*\(if\|while\|for\s.*\sin\|except\)\s*'
167 let maybe_indent = matchend(getline(sslnum), compound_statement)
168 if maybe_indent != -1
171 return indent(sslnum) + &sw * 2
175 " If the previous line ended with a colon and is not a comment, indent
176 " relative to statement start.
177 if pline =~ ':\s*$' && pline !~ '^\s*#'
178 return indent(sslnum) + &sw
181 " If the previous line was a stop-execution statement or a pass
182 if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
183 " See if the user has already dedented
184 if indent(a:lnum) > indent(sslnum) - &sw
185 " If not, recommend one dedent
186 return indent(sslnum) - &sw
188 " Otherwise, trust the user
192 " In all other cases, line up with the start of the previous statement.
193 return indent(sslnum)