3 " Maintainer: Bram Moolenaar <Bram@vim.org>
4 " Original Author: David Bustos <bustos@caltech.edu>
5 " Last Change: 2006 Apr 30
7 " Only load this indent file when no other was loaded.
8 if exists("b:did_indent")
13 " Some preliminary settings
14 setlocal nolisp " Make sure lisp indenting doesn't supersede us
15 setlocal autoindent " indentexpr isn't much help otherwise
17 setlocal indentexpr=GetPythonIndent(v:lnum)
18 setlocal indentkeys+=<:>,=elif,=except
20 " Only define the function once.
21 if exists("*GetPythonIndent")
25 " Come here when loading the script the first time.
27 let s:maxoff = 50 " maximum number of lines to look backwards for ()
29 function GetPythonIndent(lnum)
31 " If this line is explicitly joined: If the previous line was also joined,
32 " line it up with that one, otherwise add two 'shiftwidth'
33 if getline(a:lnum - 1) =~ '\\$'
34 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
35 return indent(a:lnum - 1)
37 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (&sw * 2))
40 " If the start of the line is in a string don't change the indent.
41 if has('syntax_items')
42 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
46 " Search backwards for the previous non-empty line.
47 let plnum = prevnonblank(v:lnum - 1)
50 " This is the first non-empty line, use zero indent.
54 " If the previous line is inside parenthesis, use the indent of the starting
56 " Trick: use the non-existing "dummy" variable to break out of the loop when
59 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
60 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
61 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
62 \ . " =~ '\\(Comment\\|String\\)$'")
64 let plindent = indent(parlnum)
65 let plnumstart = parlnum
67 let plindent = indent(plnum)
68 let plnumstart = plnum
72 " When inside parenthesis: If at the first line below the parenthesis add
73 " two 'shiftwidth', otherwise same as previous line.
77 call cursor(a:lnum, 1)
78 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
79 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
80 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
81 \ . " =~ '\\(Comment\\|String\\)$'")
84 " When the start is inside parenthesis, only indent one 'shiftwidth'.
85 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
86 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
87 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
88 \ . " =~ '\\(Comment\\|String\\)$'")
90 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : &sw)
92 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (&sw * 2))
101 " Get the line and remove a trailing comment.
102 " Use syntax highlighting attributes when possible.
103 let pline = getline(plnum)
104 let pline_len = strlen(pline)
105 if has('syntax_items')
106 " If the last character in the line is a comment, do a binary search for
107 " the start of the comment. synID() is slow, a linear search would take
108 " too long on a long line.
109 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "Comment$"
113 let col = (min + max) / 2
114 if synIDattr(synID(plnum, col, 1), "name") =~ "Comment$"
120 let pline = strpart(pline, 0, min - 1)
124 while col < pline_len
126 let pline = strpart(pline, 0, col)
133 " If the previous line ended with a colon, indent this line
135 return plindent + &sw
138 " If the previous line was a stop-execution statement...
139 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\)\>'
140 " See if the user has already dedented
141 if indent(a:lnum) > indent(plnum) - &sw
142 " If not, recommend one dedent
143 return indent(plnum) - &sw
145 " Otherwise, trust the user
149 " If the current line begins with a keyword that lines up with "try"
150 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
151 let lnum = a:lnum - 1
153 if getline(lnum) =~ '^\s*\(try\|except\)\>'
154 let ind = indent(lnum)
155 if ind >= indent(a:lnum)
156 return -1 " indent is already less than this
158 return ind " line up with previous try or except
162 return -1 " no matching "try"!
165 " If the current line begins with a header keyword, dedent
166 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
168 " Unless the previous line was a one-liner
169 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
173 " Or the user has already dedented
174 if indent(a:lnum) <= plindent - &sw
178 return plindent - &sw
181 " When after a () construct we probably want to go back to the start line.