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*\(class\|def\|if\|while\|with\|for\|except\)\>'
44 let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
46 " Skip strings and comments. Return 1 for chars to skip.
47 " jedi* refers to syntax definitions from jedi-vim for call signatures, which
48 " are inserted temporarily into the buffer.
49 let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
50 \ '=~? "\\vstring|comment|jedi\\S"'
52 let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
53 \ '=~? "\\vcomment|jedi\\S"'
55 " Also ignore anything concealed.
56 " Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI).
57 function! s:is_concealed(line, col)
58 let concealed = synconcealed(a:line, a:col)
59 return len(concealed) && concealed[0]
62 let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))'
66 let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
69 " Use 'shiftwidth()' instead of '&sw'.
70 " (Since Vim patch 7.3.629, 'shiftwidth' can be set to 0 to follow 'tabstop').
71 if exists('*shiftwidth')
81 function! s:pair_sort(x, y)
83 return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1
85 return a:x[0] > a:y[0] ? 1 : -1
89 " Find backwards the closest open parenthesis/bracket/brace.
90 function! s:find_opening_paren(...)
91 " optional arguments: line and column (defaults to 1) to search around
93 let view = winsaveview()
94 call cursor(a:1, a:0 > 1 ? a:2 : 1)
95 let ret = s:find_opening_paren()
96 call winrestview(view)
100 let stopline = max([0, line('.') - s:maxoff])
102 " Return if cursor is in a comment.
103 exe 'if' s:skip_search '| return [0, 0] | endif'
106 for p in s:paren_pairs
107 call add(positions, searchpairpos(
108 \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline))
111 " Remove empty matches and return the type with the closest match
112 call filter(positions, 'v:val[0]')
113 call sort(positions, 's:pair_sort')
115 return get(positions, -1, [0, 0])
118 " Find the start of a multi-line statement
119 function! s:find_start_of_multiline_statement(lnum)
122 if getline(lnum - 1) =~ '\\$'
123 let lnum = prevnonblank(lnum - 1)
125 let [paren_lnum, _] = s:find_opening_paren(lnum)
129 let lnum = paren_lnum
135 " Find the block starter that matches the current line
136 function! s:find_start_of_block(lnum, types)
137 let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>'
140 let last_indent = indent(lnum) + 1
141 while lnum > 0 && last_indent > 0
142 if indent(lnum) < last_indent
143 if getline(lnum) =~# re
146 let last_indent = indent(lnum)
148 let lnum = prevnonblank(lnum - 1)
153 " Is "expr" true for every position in "lnum", beginning at "start"?
154 " (optionally up to a:1 / 4th argument)
155 function! s:match_expr_on_line(expr, lnum, start, ...)
156 let text = getline(a:lnum)
157 let end = a:0 ? a:1 : len(text)
161 let save_pos = getpos('.')
163 for i in range(a:start, end)
164 call cursor(a:lnum, i)
165 if !(eval(a:expr) || text[i-1] =~ '\s')
170 call setpos('.', save_pos)
174 " Line up with open parenthesis/bracket/brace.
175 function! s:indent_like_opening_paren(lnum)
176 let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum)
180 let text = getline(paren_lnum)
181 let base = indent(paren_lnum)
183 let nothing_after_opening_paren = s:match_expr_on_line(
184 \ s:skip_after_opening_paren, paren_lnum, paren_col+1)
185 let starts_with_closing_paren = getline(a:lnum) =~ '^\s*[])}]'
187 if nothing_after_opening_paren
188 if starts_with_closing_paren
191 let res = base + s:sw()
194 " Indent to match position of opening paren.
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 text =~# s:control_statement && res == base + s:sw()
202 return base + s:sw() * 2
208 " Match indent of first block of this type.
209 function! s:indent_like_block(lnum)
210 let text = getline(a:lnum)
212 for [line_re, blocks] in items(s:block_rules)
217 let lnum = s:find_start_of_block(a:lnum - 1, blocks)
228 function! s:indent_like_previous_line(lnum)
229 let lnum = prevnonblank(a:lnum - 1)
231 " No previous line, keep current indent.
236 let text = getline(lnum)
237 let start = s:find_start_of_multiline_statement(lnum)
238 let base = indent(start)
239 let current = indent(a:lnum)
241 " Jump to last character in previous line.
242 call cursor(lnum, len(text))
243 let ignore_last_char = eval(s:skip_special_chars)
245 " Search for final colon that is not inside something to be ignored.
247 let curpos = getpos(".")[2]
248 if curpos == 1 | break | endif
249 if eval(s:skip_special_chars) || text[curpos-1] =~ '\s'
252 elseif text[curpos-1] == ':'
258 if text =~ '\\$' && !ignore_last_char
259 " If this line is the continuation of a control statement
260 " indent further to distinguish the continuation line
261 " from the next logical line.
262 if getline(start) =~# s:control_statement
263 return base + s:sw() * 2
266 " Nest (other) explicit continuations only one level deeper.
270 " If the previous statement was a stop-execution statement or a pass
271 if getline(start) =~# s:stop_statement
272 " Remove one level of indentation if the user hasn't already dedented
273 if indent(a:lnum) > base - s:sw()
276 " Otherwise, trust the user
280 " If this line is dedented and the number of indent spaces is valid
281 " (multiple of the indentation size), trust the user
282 let dedent_size = current - base
283 if dedent_size < 0 && current % s:sw() == 0
287 " In all other cases, line up with the start of the previous statement.
291 " Is the syntax at lnum (and optionally cnum) a python string?
292 function! s:is_python_string(lnum, ...)
293 let line = getline(a:lnum)
294 let linelen = len(line)
298 let cols = a:0 ? type(a:1) != type([]) ? [a:1] : a:1 : range(1, linelen)
300 if match(map(synstack(a:lnum, cnum),
301 \ 'synIDattr(v:val,"name")'), 'python\S*String') == -1
308 function! GetPythonPEPIndent(lnum)
310 " First line has indent 0
315 " Multilinestrings: continous, docstring or starting.
316 if s:is_python_string(a:lnum)
317 if s:is_python_string(a:lnum-1)
318 " Previous line is (completely) a string.
319 return s:indent_like_previous_line(a:lnum)
322 if match(getline(a:lnum-1), '^\s*\%("""\|''''''\)') != -1
324 return s:indent_like_previous_line(a:lnum)
327 if s:is_python_string(a:lnum-1, len(getline(a:lnum-1)))
328 " String started in previous line.
333 " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
334 let indent = s:indent_like_opening_paren(a:lnum)
339 " Blocks: Match indent of first block of this type.
340 let indent = s:indent_like_block(a:lnum)
345 return s:indent_like_previous_line(a:lnum)