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 \ '^\s*elif\>': ['if', 'elif'],
23 \ '^\s*else\>': ['if', 'elif', 'for', 'try', 'except'],
24 \ '^\s*except\>': ['try', 'except'],
25 \ '^\s*finally\>': ['try', 'except', 'else']
27 let s:paren_pairs = ['()', '{}', '[]']
28 let s:control_statement = '^\s*\(if\|while\|with\|for\|except\)\>'
29 let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
31 " Skip strings and comments
32 let s:skip = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
33 \ '=~? "string\\|comment"'
35 " compatibility with vim patch 7.3.629: 'sw' can be set to -1 to follow 'ts'
36 if exists('*shiftwidth')
46 function! s:pair_sort(x, y)
48 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
50 return a:x[0] > a:y[0] ? 1 : -1
54 " Find backwards the closest open parenthesis/bracket/brace.
55 function! s:find_opening_paren(...)
56 " optional arguments: line and column (defaults to 1) to search around
58 let view = winsaveview()
59 call cursor(a:1, a:0 > 1 ? a:2 : 1)
60 let ret = s:find_opening_paren()
61 call winrestview(view)
65 let stopline = max([0, line('.') - s:maxoff])
67 " Return if cursor is in a comment or string
68 exe 'if' s:skip '| return [0, 0] | endif'
71 for p in s:paren_pairs
72 call add(positions, searchpairpos(
73 \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip, stopline))
76 " Remove empty matches and return the type with the closest match
77 call filter(positions, 'v:val[0]')
78 call sort(positions, 's:pair_sort')
80 return get(positions, -1, [0, 0])
83 " Find the start of a multi-line statement
84 function! s:find_start_of_multiline_statement(lnum)
87 if getline(lnum - 1) =~ '\\$'
88 let lnum = prevnonblank(lnum - 1)
90 let [paren_lnum, _] = s:find_opening_paren(lnum)
100 " Find the block starter that matches the current line
101 function! s:find_start_of_block(lnum, types)
102 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
105 let last_indent = indent(lnum) + 1
106 while lnum > 0 && last_indent > 0
107 if indent(lnum) < last_indent
108 if getline(lnum) =~# re
111 let last_indent = indent(lnum)
113 let lnum = prevnonblank(lnum - 1)
118 " Line up with open parenthesis/bracket/brace.
119 function! s:indent_like_opening_paren(lnum)
120 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
124 let text = getline(paren_lnum)
125 let base = indent(paren_lnum)
127 let nothing_after_opening_paren = text =~ '\%'.(paren_col + 1).'c\s*$'
128 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
130 if nothing_after_opening_paren
131 if starts_with_closing_paren
134 let res = base + s:sw()
137 " Indent to match position of opening paren.
141 " If this line is the continuation of a control statement
142 " indent further to distinguish the continuation line
143 " from the next logical line.
144 if text =~# s:control_statement && res == base + s:sw()
145 return base + s:sw() * 2
151 " Match indent of first block of this type.
152 function! s:indent_like_block(lnum)
153 let text = getline(a:lnum)
155 for [line_re, blocks] in items(s:block_rules)
160 let lnum = s:find_start_of_block(a:lnum - 1, blocks)
171 function! s:indent_like_previous_line(lnum)
172 let lnum = prevnonblank(a:lnum - 1)
174 " No previous line, keep current indent.
179 let text = getline(lnum)
180 let start = s:find_start_of_multiline_statement(lnum)
181 let base = indent(start)
182 let current = indent(a:lnum)
184 " Jump to last character in previous line.
185 call cursor(lnum, len(text))
186 let ignore_last_char = eval(s:skip)
188 " Search for final colon that is not inside a string or comment.
189 while search(':\s*\%(#.*\)\?$', 'bcW', lnum)
197 if text =~ '\\$' && !ignore_last_char
198 " If this line is the continuation of a control statement
199 " indent further to distinguish the continuation line
200 " from the next logical line.
201 if getline(start) =~# s:control_statement
202 return base + s:sw() * 2
205 " Nest (other) explicit continuations only one level deeper.
209 " If the previous statement was a stop-execution statement or a pass
210 if getline(start) =~# s:stop_statement
211 " Remove one level of indentation if the user hasn't already dedented
212 if indent(a:lnum) > base - s:sw()
215 " Otherwise, trust the user
219 " If this line is dedented and the number of indent spaces is valid
220 " (multiple of the indentation size), trust the user
221 let dedent_size = current - base
222 if dedent_size < 0 && current % s:sw() == 0
226 " In all other cases, line up with the start of the previous statement.
230 function! GetPythonPEPIndent(lnum)
232 " First line has indent 0
237 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
238 let indent = s:indent_like_opening_paren(a:lnum)
243 " Blocks: Match indent of first block of this type.
244 let indent = s:indent_like_block(a:lnum)
249 return s:indent_like_previous_line(a:lnum)