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
20 setlocal softtabstop=4
25 \ '^\s*elif\>': ['if', 'elif'],
26 \ '^\s*else\>': ['if', 'elif', 'for', 'try', 'except'],
27 \ '^\s*except\>': ['try', 'except'],
28 \ '^\s*finally\>': ['try', 'except', 'else']
30 let s:paren_pairs = ['()', '{}', '[]']
31 let s:control_statement = '^\s*\(if\|while\|with\|for\|except\)\>'
32 let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
34 " Skip strings and comments
35 let s:skip = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
36 \ '=~? "string\\|comment"'
38 " compatibility with vim patch 7.3.629: 'sw' can be set to -1 to follow 'ts'
39 if exists('*shiftwidth')
49 function! s:pair_sort(x, y)
51 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
53 return a:x[0] > a:y[0] ? 1 : -1
57 " Find backwards the closest open parenthesis/bracket/brace.
58 function! s:find_opening_paren(...)
59 " optional arguments: line and column (defaults to 1) to search around
61 let view = winsaveview()
62 call cursor(a:1, a:0 > 1 ? a:2 : 1)
63 let ret = s:find_opening_paren()
64 call winrestview(view)
68 let stopline = max([0, line('.') - s:maxoff])
70 " Return if cursor is in a comment or string
71 exe 'if' s:skip '| return [0, 0] | endif'
74 for p in s:paren_pairs
75 call add(positions, searchpairpos(
76 \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip, stopline))
79 " Remove empty matches and return the type with the closest match
80 call filter(positions, 'v:val[0]')
81 call sort(positions, 's:pair_sort')
83 return get(positions, -1, [0, 0])
86 " Find the start of a multi-line statement
87 function! s:find_start_of_multiline_statement(lnum)
90 if getline(lnum - 1) =~ '\\$'
91 let lnum = prevnonblank(lnum - 1)
93 let [paren_lnum, _] = s:find_opening_paren(lnum)
103 " Find the block starter that matches the current line
104 function! s:find_start_of_block(lnum, types)
105 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
108 let last_indent = indent(lnum) + 1
109 while lnum > 0 && last_indent > 0
110 if indent(lnum) < last_indent
111 if getline(lnum) =~# re
114 let last_indent = indent(lnum)
116 let lnum = prevnonblank(lnum - 1)
121 " Line up with open parenthesis/bracket/brace.
122 function! s:indent_like_opening_paren(lnum)
123 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
127 let text = getline(paren_lnum)
128 let base = indent(paren_lnum)
130 let nothing_after_opening_paren = text =~ '\%'.(paren_col + 1).'c\s*$'
131 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
133 if nothing_after_opening_paren
134 if starts_with_closing_paren
137 let res = base + s:sw()
140 " Indent to match position of opening paren.
144 " If this line is the continuation of a control statement
145 " indent further to distinguish the continuation line
146 " from the next logical line.
147 if text =~# s:control_statement && res == base + s:sw()
148 return base + s:sw() * 2
154 " Match indent of first block of this type.
155 function! s:indent_like_block(lnum)
156 let text = getline(a:lnum)
158 for [line_re, blocks] in items(s:block_rules)
163 let lnum = s:find_start_of_block(a:lnum - 1, blocks)
174 function! s:indent_like_previous_line(lnum)
175 let lnum = prevnonblank(a:lnum - 1)
177 " No previous line, keep current indent.
182 let text = getline(lnum)
183 let start = s:find_start_of_multiline_statement(lnum)
184 let base = indent(start)
185 let current = indent(a:lnum)
187 " Jump to last character in previous line.
188 call cursor(lnum, len(text))
189 let ignore_last_char = eval(s:skip)
191 " Search for final colon that is not inside a string or comment.
192 while search(':\s*\%(#.*\)\?$', 'bcW', lnum)
200 if text =~ '\\$' && !ignore_last_char
201 " If this line is the continuation of a control statement
202 " indent further to distinguish the continuation line
203 " from the next logical line.
204 if getline(start) =~# s:control_statement
205 return base + s:sw() * 2
208 " Nest (other) explicit continuations only one level deeper.
212 " If the previous statement was a stop-execution statement or a pass
213 if getline(start) =~# s:stop_statement
214 " Remove one level of indentation if the user hasn't already dedented
215 if indent(a:lnum) > base - s:sw()
218 " Otherwise, trust the user
222 " If this line is dedented and the number of indent spaces is valid
223 " (multiple of the indentation size), trust the user
224 let dedent_size = current - base
225 if dedent_size < 0 && current % s:sw() == 0
229 " In all other cases, line up with the start of the previous statement.
233 function! GetPythonPEPIndent(lnum)
235 " First line has indent 0
240 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
241 let indent = s:indent_like_opening_paren(a:lnum)
246 " Blocks: Match indent of first block of this type.
247 let indent = s:indent_like_block(a:lnum)
252 return s:indent_like_previous_line(a:lnum)