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)
8 " vim-python-pep8-indent - A nicer Python indentation style for vim.
9 " Written in 2004 by David Bustos <bustos@caltech.edu>
10 " Maintained from 2004-2005 by Eric Mc Sween <em@tomcom.de>
11 " Maintained from 2013 by Hynek Schlawack <hs@ox.cx>
13 " To the extent possible under law, the author(s) have dedicated all copyright
14 " and related and neighboring rights to this software to the public domain
15 " worldwide. This software is distributed without any warranty.
16 " You should have received a copy of the CC0 Public Domain Dedication along
17 " with this software. If not, see
18 " <http://creativecommons.org/publicdomain/zero/1.0/>.
20 " Only load this indent file when no other was loaded.
21 if exists("b:did_indent")
29 setlocal indentexpr=GetPythonPEPIndent(v:lnum)
30 setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except
32 setlocal softtabstop=4
37 \ '^\s*elif\>': ['if', 'elif'],
38 \ '^\s*else\>': ['if', 'elif', 'for', 'try', 'except'],
39 \ '^\s*except\>': ['try', 'except'],
40 \ '^\s*finally\>': ['try', 'except', 'else']
42 let s:paren_pairs = ['()', '{}', '[]']
43 let s:control_statement = '^\s*\(if\|while\|with\|for\|except\)\>'
44 let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
46 " Skip strings and comments
47 let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
48 \ '=~? "string\\|comment"'
50 let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
53 " compatibility with vim patch 7.3.629: 'sw' can be set to -1 to follow 'ts'
54 if exists('*shiftwidth')
64 function! s:pair_sort(x, y)
66 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
68 return a:x[0] > a:y[0] ? 1 : -1
72 " Find backwards the closest open parenthesis/bracket/brace.
73 function! s:find_opening_paren(...)
74 " optional arguments: line and column (defaults to 1) to search around
76 let view = winsaveview()
77 call cursor(a:1, a:0 > 1 ? a:2 : 1)
78 let ret = s:find_opening_paren()
79 call winrestview(view)
83 let stopline = max([0, line('.') - s:maxoff])
85 " Return if cursor is in a comment or string
86 exe 'if' s:skip_search '| return [0, 0] | endif'
89 for p in s:paren_pairs
90 call add(positions, searchpairpos(
91 \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline))
94 " Remove empty matches and return the type with the closest match
95 call filter(positions, 'v:val[0]')
96 call sort(positions, 's:pair_sort')
98 return get(positions, -1, [0, 0])
101 " Find the start of a multi-line statement
102 function! s:find_start_of_multiline_statement(lnum)
105 if getline(lnum - 1) =~ '\\$'
106 let lnum = prevnonblank(lnum - 1)
108 let [paren_lnum, _] = s:find_opening_paren(lnum)
112 let lnum = paren_lnum
118 " Find the block starter that matches the current line
119 function! s:find_start_of_block(lnum, types)
120 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
123 let last_indent = indent(lnum) + 1
124 while lnum > 0 && last_indent > 0
125 if indent(lnum) < last_indent
126 if getline(lnum) =~# re
129 let last_indent = indent(lnum)
131 let lnum = prevnonblank(lnum - 1)
136 " Line up with open parenthesis/bracket/brace.
137 function! s:indent_like_opening_paren(lnum)
138 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
142 let text = getline(paren_lnum)
143 let base = indent(paren_lnum)
145 let nothing_after_opening_paren = text =~ '\%'.(paren_col + 1).'c\s*$'
146 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
148 if nothing_after_opening_paren
149 if starts_with_closing_paren
152 let res = base + s:sw()
155 " Indent to match position of opening paren.
159 " If this line is the continuation of a control statement
160 " indent further to distinguish the continuation line
161 " from the next logical line.
162 if text =~# s:control_statement && res == base + s:sw()
163 return base + s:sw() * 2
169 " Match indent of first block of this type.
170 function! s:indent_like_block(lnum)
171 let text = getline(a:lnum)
173 for [line_re, blocks] in items(s:block_rules)
178 let lnum = s:find_start_of_block(a:lnum - 1, blocks)
189 function! s:indent_like_previous_line(lnum)
190 let lnum = prevnonblank(a:lnum - 1)
192 " No previous line, keep current indent.
197 let text = getline(lnum)
198 let start = s:find_start_of_multiline_statement(lnum)
199 let base = indent(start)
200 let current = indent(a:lnum)
202 " Jump to last character in previous line.
203 call cursor(lnum, len(text))
204 let ignore_last_char = eval(s:skip_special_chars)
206 " Search for final colon that is not inside a string or comment.
207 while search(':\s*\%(#.*\)\?$', 'bcW', lnum)
208 if eval(s:skip_special_chars)
215 if text =~ '\\$' && !ignore_last_char
216 " If this line is the continuation of a control statement
217 " indent further to distinguish the continuation line
218 " from the next logical line.
219 if getline(start) =~# s:control_statement
220 return base + s:sw() * 2
223 " Nest (other) explicit continuations only one level deeper.
227 " If the previous statement was a stop-execution statement or a pass
228 if getline(start) =~# s:stop_statement
229 " Remove one level of indentation if the user hasn't already dedented
230 if indent(a:lnum) > base - s:sw()
233 " Otherwise, trust the user
237 " If this line is dedented and the number of indent spaces is valid
238 " (multiple of the indentation size), trust the user
239 let dedent_size = current - base
240 if dedent_size < 0 && current % s:sw() == 0
244 " In all other cases, line up with the start of the previous statement.
248 function! GetPythonPEPIndent(lnum)
250 " First line has indent 0
255 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
256 let indent = s:indent_like_opening_paren(a:lnum)
261 " Blocks: Match indent of first block of this type.
262 let indent = s:indent_like_block(a:lnum)
267 return s:indent_like_previous_line(a:lnum)