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\)\>'
30 if v:version >= 704 || (v:version == 703 && has('patch1037'))
31 let s:string_literal = '".\{-}\\\@1<!"\|''.\{-}\\\@1<!'''
33 let s:string_literal = '".\{-}\\\@<!"\|''.\{-}\\\@<!'''
36 " compatibility with vim patch 7.3.629: 'sw' can be set to -1 to follow 'ts'
37 if exists('*shiftwidth')
47 function! s:pair_sort(x, y)
49 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
51 return a:x[0] > a:y[0] ? 1 : -1
55 " Find backwards the closest open parenthesis/bracket/brace.
56 function! s:find_opening_paren(...)
57 " optional arguments: line and column (defaults to 1) to search around
59 let view = winsaveview()
60 call cursor(a:1, a:0 > 1 ? a:2 : 1)
61 let ret = s:find_opening_paren()
62 call winrestview(view)
66 let stopline = max([0, line('.') - s:maxoff])
68 " Skip strings and comments
69 let skip = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
70 \ '=~? "string\\|comment"'
72 " Return if cursor is in a comment or string
73 exe 'if' skip '| return [0, 0] | endif'
76 for p in s:paren_pairs
77 call add(positions, searchpairpos('\V'.p[0], '', '\V'.p[1], 'bnW', skip, stopline))
80 " Remove empty matches and return the type with the closest match
81 call filter(positions, 'v:val[0]')
82 call sort(positions, 's:pair_sort')
84 return get(positions, -1, [0, 0])
87 " Find the start of a multi-line statement
88 function! s:find_start_of_multiline_statement(lnum)
91 if getline(lnum - 1) =~ '\\$'
92 let lnum = prevnonblank(lnum - 1)
94 let [paren_lnum, _] = s:find_opening_paren(lnum)
104 " Find the block starter that matches the current line
105 function! s:find_start_of_block(lnum, types)
106 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
109 let last_indent = indent(lnum) + 1
110 while lnum > 0 && last_indent > 0
111 if indent(lnum) < last_indent
112 if getline(lnum) =~# re
115 let last_indent = indent(lnum)
117 let lnum = prevnonblank(lnum - 1)
122 " Line up with open parenthesis/bracket/brace.
123 function! s:indent_like_opening_paren(lnum)
124 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
128 let text = getline(paren_lnum)
129 let base = indent(paren_lnum)
131 let nothing_after_opening_paren = text =~ '\%'.(paren_col + 1).'c\s*$'
132 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
134 if nothing_after_opening_paren
135 if starts_with_closing_paren
138 let res = base + s:sw()
141 " Indent to match position of opening paren.
145 " If this line is the continuation of a control statement
146 " indent further to distinguish the continuation line
147 " from the next logical line.
148 if text =~# s:control_statement && res == base + s:sw()
149 return base + s:sw() * 2
155 " Match indent of first block of this type.
156 function! s:indent_like_block(lnum)
157 let text = getline(a:lnum)
159 for [line_re, blocks] in items(s:block_rules)
164 let lnum = s:find_start_of_block(a:lnum - 1, blocks)
175 function! s:indent_like_previous_line(lnum)
176 let lnum = prevnonblank(a:lnum - 1)
177 let text = getline(lnum)
178 let start = s:find_start_of_multiline_statement(lnum)
179 let base = indent(start)
181 " Remove string literals.
182 let text = substitute(text, s:string_literal, '', 'g')
184 " If the previous line ended with a colon and is not a comment, indent
185 " relative to statement start.
186 if text =~ '^[^#]*:\s*\(#.*\)\?$'
192 " If this line is the continuation of a control statement
193 " indent further to distinguish the continuation line
194 " from the next logical line.
195 if getline(start) =~# s:control_statement
196 return base + s:sw() * 2
199 " Nest (other) explicit continuations only one level deeper.
203 " If the previous statement was a stop-execution statement or a pass
204 if getline(start) =~# s:stop_statement
205 " Remove one level of indentation if the user hasn't already dedented
206 if indent(a:lnum) > base - s:sw()
209 " Otherwise, trust the user
213 " In all other cases, line up with the start of the previous statement.
217 function! GetPythonPEPIndent(lnum)
219 " First line has indent 0
224 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
225 let indent = s:indent_like_opening_paren(a:lnum)
230 " Blocks: Match indent of first block of this type.
231 let indent = s:indent_like_block(a:lnum)
236 return s:indent_like_previous_line(a:lnum)