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_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
36 \ '=~? "string\\|comment"'
38 let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
41 " compatibility with vim patch 7.3.629: 'sw' can be set to -1 to follow 'ts'
42 if exists('*shiftwidth')
52 function! s:pair_sort(x, y)
54 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
56 return a:x[0] > a:y[0] ? 1 : -1
60 " Find backwards the closest open parenthesis/bracket/brace.
61 function! s:find_opening_paren(...)
62 " optional arguments: line and column (defaults to 1) to search around
64 let view = winsaveview()
65 call cursor(a:1, a:0 > 1 ? a:2 : 1)
66 let ret = s:find_opening_paren()
67 call winrestview(view)
71 let stopline = max([0, line('.') - s:maxoff])
73 " Return if cursor is in a comment or string
74 exe 'if' s:skip_search '| return [0, 0] | endif'
77 for p in s:paren_pairs
78 call add(positions, searchpairpos(
79 \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline))
82 " Remove empty matches and return the type with the closest match
83 call filter(positions, 'v:val[0]')
84 call sort(positions, 's:pair_sort')
86 return get(positions, -1, [0, 0])
89 " Find the start of a multi-line statement
90 function! s:find_start_of_multiline_statement(lnum)
93 if getline(lnum - 1) =~ '\\$'
94 let lnum = prevnonblank(lnum - 1)
96 let [paren_lnum, _] = s:find_opening_paren(lnum)
100 let lnum = paren_lnum
106 " Find the block starter that matches the current line
107 function! s:find_start_of_block(lnum, types)
108 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
111 let last_indent = indent(lnum) + 1
112 while lnum > 0 && last_indent > 0
113 if indent(lnum) < last_indent
114 if getline(lnum) =~# re
117 let last_indent = indent(lnum)
119 let lnum = prevnonblank(lnum - 1)
124 " Line up with open parenthesis/bracket/brace.
125 function! s:indent_like_opening_paren(lnum)
126 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
130 let text = getline(paren_lnum)
131 let base = indent(paren_lnum)
133 let nothing_after_opening_paren = text =~ '\%'.(paren_col + 1).'c\s*$'
134 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
136 if nothing_after_opening_paren
137 if starts_with_closing_paren
140 let res = base + s:sw()
143 " Indent to match position of opening paren.
147 " If this line is the continuation of a control statement
148 " indent further to distinguish the continuation line
149 " from the next logical line.
150 if text =~# s:control_statement && res == base + s:sw()
151 return base + s:sw() * 2
157 " Match indent of first block of this type.
158 function! s:indent_like_block(lnum)
159 let text = getline(a:lnum)
161 for [line_re, blocks] in items(s:block_rules)
166 let lnum = s:find_start_of_block(a:lnum - 1, blocks)
177 function! s:indent_like_previous_line(lnum)
178 let lnum = prevnonblank(a:lnum - 1)
180 " No previous line, keep current indent.
185 let text = getline(lnum)
186 let start = s:find_start_of_multiline_statement(lnum)
187 let base = indent(start)
188 let current = indent(a:lnum)
190 " Jump to last character in previous line.
191 call cursor(lnum, len(text))
192 let ignore_last_char = eval(s:skip_special_chars)
194 " Search for final colon that is not inside a string or comment.
195 while search(':\s*\%(#.*\)\?$', 'bcW', lnum)
196 if eval(s:skip_special_chars)
203 if text =~ '\\$' && !ignore_last_char
204 " If this line is the continuation of a control statement
205 " indent further to distinguish the continuation line
206 " from the next logical line.
207 if getline(start) =~# s:control_statement
208 return base + s:sw() * 2
211 " Nest (other) explicit continuations only one level deeper.
215 " If the previous statement was a stop-execution statement or a pass
216 if getline(start) =~# s:stop_statement
217 " Remove one level of indentation if the user hasn't already dedented
218 if indent(a:lnum) > base - s:sw()
221 " Otherwise, trust the user
225 " If this line is dedented and the number of indent spaces is valid
226 " (multiple of the indentation size), trust the user
227 let dedent_size = current - base
228 if dedent_size < 0 && current % s:sw() == 0
232 " In all other cases, line up with the start of the previous statement.
236 function! GetPythonPEPIndent(lnum)
238 " First line has indent 0
243 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
244 let indent = s:indent_like_opening_paren(a:lnum)
249 " Blocks: Match indent of first block of this type.
250 let indent = s:indent_like_block(a:lnum)
255 return s:indent_like_previous_line(a:lnum)