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 if &ft == 'pyrex' || &ft == 'cython'
44 let b:control_statement = '\v^\s*(class|def|if|while|with|for|except|cdef|cpdef)>'
46 let b:control_statement = '\v^\s*(class|def|if|while|with|for|except)>'
48 let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
50 " Skip strings and comments. Return 1 for chars to skip.
51 " jedi* refers to syntax definitions from jedi-vim for call signatures, which
52 " are inserted temporarily into the buffer.
53 let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
54 \ '=~? "\\vstring|comment|jedi\\S"'
56 let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
57 \ '=~? "\\vcomment|jedi\\S"'
59 " Also ignore anything concealed.
60 " Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI).
61 function! s:is_concealed(line, col)
62 let concealed = synconcealed(a:line, a:col)
63 return len(concealed) && concealed[0]
66 let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))'
70 let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
73 " Use 'shiftwidth()' instead of '&sw'.
74 " (Since Vim patch 7.3.629, 'shiftwidth' can be set to 0 to follow 'tabstop').
75 if exists('*shiftwidth')
85 function! s:pair_sort(x, y)
87 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
89 return a:x[0] > a:y[0] ? 1 : -1
93 " Find backwards the closest open parenthesis/bracket/brace.
94 function! s:find_opening_paren(...)
95 " optional arguments: line and column (defaults to 1) to search around
97 let view = winsaveview()
98 call cursor(a:1, a:0 > 1 ? a:2 : 1)
99 let ret = s:find_opening_paren()
100 call winrestview(view)
104 let stopline = max([0, line('.') - s:maxoff])
106 " Return if cursor is in a comment.
107 exe 'if' s:skip_search '| return [0, 0] | endif'
110 for p in s:paren_pairs
111 call add(positions, searchpairpos(
112 \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline))
115 " Remove empty matches and return the type with the closest match
116 call filter(positions, 'v:val[0]')
117 call sort(positions, 's:pair_sort')
119 return get(positions, -1, [0, 0])
122 " Find the start of a multi-line statement
123 function! s:find_start_of_multiline_statement(lnum)
126 if getline(lnum - 1) =~ '\\$'
127 let lnum = prevnonblank(lnum - 1)
129 let [paren_lnum, _] = s:find_opening_paren(lnum)
133 let lnum = paren_lnum
139 " Find the block starter that matches the current line
140 function! s:find_start_of_block(lnum, types)
141 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
144 let last_indent = indent(lnum) + 1
145 while lnum > 0 && last_indent > 0
146 if indent(lnum) < last_indent
147 if getline(lnum) =~# re
150 let last_indent = indent(lnum)
152 let lnum = prevnonblank(lnum - 1)
157 " Is "expr" true for every position in "lnum", beginning at "start"?
158 " (optionally up to a:1 / 4th argument)
159 function! s:match_expr_on_line(expr, lnum, start, ...)
160 let text = getline(a:lnum)
161 let end = a:0 ? a:1 : len(text)
165 let save_pos = getpos('.')
167 for i in range(a:start, end)
168 call cursor(a:lnum, i)
169 if !(eval(a:expr) || text[i-1] =~ '\s')
174 call setpos('.', save_pos)
178 " Line up with open parenthesis/bracket/brace.
179 function! s:indent_like_opening_paren(lnum)
180 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
184 let text = getline(paren_lnum)
185 let base = indent(paren_lnum)
187 let nothing_after_opening_paren = s:match_expr_on_line(
188 \ s:skip_after_opening_paren, paren_lnum, paren_col+1)
189 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
191 if nothing_after_opening_paren
192 if starts_with_closing_paren
195 let res = base + s:sw()
198 " Indent to match position of opening paren.
202 " If this line is the continuation of a control statement
203 " indent further to distinguish the continuation line
204 " from the next logical line.
205 if text =~# b:control_statement && res == base + s:sw()
206 return base + s:sw() * 2
212 " Match indent of first block of this type.
213 function! s:indent_like_block(lnum)
214 let text = getline(a:lnum)
216 for [line_re, blocks] in items(s:block_rules)
221 let lnum = s:find_start_of_block(a:lnum - 1, blocks)
232 function! s:indent_like_previous_line(lnum)
233 let lnum = prevnonblank(a:lnum - 1)
235 " No previous line, keep current indent.
240 let text = getline(lnum)
241 let start = s:find_start_of_multiline_statement(lnum)
242 let base = indent(start)
243 let current = indent(a:lnum)
245 " Jump to last character in previous line.
246 call cursor(lnum, len(text))
247 let ignore_last_char = eval(s:skip_special_chars)
249 " Search for final colon that is not inside something to be ignored.
251 let curpos = getpos(".")[2]
252 if curpos == 1 | break | endif
253 if eval(s:skip_special_chars) || text[curpos-1] =~ '\s'
256 elseif text[curpos-1] == ':'
262 if text =~ '\\$' && !ignore_last_char
263 " If this line is the continuation of a control statement
264 " indent further to distinguish the continuation line
265 " from the next logical line.
266 if getline(start) =~# b:control_statement
267 return base + s:sw() * 2
270 " Nest (other) explicit continuations only one level deeper.
274 " If the previous statement was a stop-execution statement or a pass
275 if getline(start) =~# s:stop_statement
276 " Remove one level of indentation if the user hasn't already dedented
277 if indent(a:lnum) > base - s:sw()
280 " Otherwise, trust the user
284 " If this line is dedented and the number of indent spaces is valid
285 " (multiple of the indentation size), trust the user
286 let dedent_size = current - base
287 if dedent_size < 0 && current % s:sw() == 0
291 " In all other cases, line up with the start of the previous statement.
295 " Is the syntax at lnum (and optionally cnum) a python string?
296 function! s:is_python_string(lnum, ...)
297 let line = getline(a:lnum)
298 let linelen = len(line)
302 let cols = a:0 ? type(a:1) != type([]) ? [a:1] : a:1 : range(1, linelen)
304 if match(map(synstack(a:lnum, cnum),
305 \ 'synIDattr(v:val,"name")'), 'python\S*String') == -1
312 function! GetPythonPEPIndent(lnum)
314 " First line has indent 0
319 " Multilinestrings: continous, docstring or starting.
320 if s:is_python_string(a:lnum)
321 if s:is_python_string(a:lnum-1)
322 " Previous line is (completely) a string.
323 return s:indent_like_previous_line(a:lnum)
326 if match(getline(a:lnum-1), '^\s*\%("""\|''''''\)') != -1
328 return s:indent_like_previous_line(a:lnum)
331 if s:is_python_string(a:lnum-1, len(getline(a:lnum-1)))
332 " String started in previous line.
337 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
338 let indent = s:indent_like_opening_paren(a:lnum)
343 " Blocks: Match indent of first block of this type.
344 let indent = s:indent_like_block(a:lnum)
349 return s:indent_like_previous_line(a:lnum)