blob: 838865f87f7087852b83f12dded594da34a0ff8b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002" Language: Python
3" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004" Original Author: David Bustos <bustos@caltech.edu>
Bram Moolenaar9a7224b2012-04-30 15:56:52 +02005" Last Change: 2012 Apr 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
7" Only load this indent file when no other was loaded.
8if exists("b:did_indent")
9 finish
10endif
11let b:did_indent = 1
Bram Moolenaar9a7224b2012-04-30 15:56:52 +020012let s:keepcpo= &cpo
13set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15" Some preliminary settings
16setlocal nolisp " Make sure lisp indenting doesn't supersede us
17setlocal autoindent " indentexpr isn't much help otherwise
18
19setlocal indentexpr=GetPythonIndent(v:lnum)
20setlocal indentkeys+=<:>,=elif,=except
21
22" Only define the function once.
23if exists("*GetPythonIndent")
24 finish
25endif
26
Bram Moolenaar05159a02005-02-26 23:04:13 +000027" Come here when loading the script the first time.
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029let s:maxoff = 50 " maximum number of lines to look backwards for ()
30
31function GetPythonIndent(lnum)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000032
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 " If this line is explicitly joined: If the previous line was also joined,
34 " line it up with that one, otherwise add two 'shiftwidth'
35 if getline(a:lnum - 1) =~ '\\$'
36 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
37 return indent(a:lnum - 1)
38 endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000039 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (&sw * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 endif
41
42 " If the start of the line is in a string don't change the indent.
43 if has('syntax_items')
Bram Moolenaared203462004-06-16 11:19:22 +000044 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 return -1
46 endif
47
48 " Search backwards for the previous non-empty line.
49 let plnum = prevnonblank(v:lnum - 1)
50
51 if plnum == 0
52 " This is the first non-empty line, use zero indent.
53 return 0
54 endif
55
56 " If the previous line is inside parenthesis, use the indent of the starting
57 " line.
58 " Trick: use the non-existing "dummy" variable to break out of the loop when
59 " going too far back.
60 call cursor(plnum, 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000061 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
63 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
Bram Moolenaared203462004-06-16 11:19:22 +000064 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 if parlnum > 0
66 let plindent = indent(parlnum)
67 let plnumstart = parlnum
68 else
69 let plindent = indent(plnum)
70 let plnumstart = plnum
71 endif
72
73
74 " When inside parenthesis: If at the first line below the parenthesis add
75 " two 'shiftwidth', otherwise same as previous line.
76 " i = (a
77 " + b
78 " + c)
79 call cursor(a:lnum, 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000080 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
Bram Moolenaar071d4272004-06-13 20:20:40 +000081 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
82 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
Bram Moolenaared203462004-06-16 11:19:22 +000083 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 if p > 0
85 if p == plnum
86 " When the start is inside parenthesis, only indent one 'shiftwidth'.
Bram Moolenaar910f66f2006-04-05 20:41:53 +000087 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
89 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
Bram Moolenaared203462004-06-16 11:19:22 +000090 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 if pp > 0
Bram Moolenaar05159a02005-02-26 23:04:13 +000092 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : &sw)
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000094 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (&sw * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 endif
96 if plnumstart == p
97 return indent(plnum)
98 endif
99 return plindent
100 endif
101
102
103 " Get the line and remove a trailing comment.
104 " Use syntax highlighting attributes when possible.
105 let pline = getline(plnum)
106 let pline_len = strlen(pline)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000107 if has('syntax_items')
108 " If the last character in the line is a comment, do a binary search for
109 " the start of the comment. synID() is slow, a linear search would take
110 " too long on a long line.
111 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "Comment$"
112 let min = 1
113 let max = pline_len
114 while min < max
115 let col = (min + max) / 2
116 if synIDattr(synID(plnum, col, 1), "name") =~ "Comment$"
117 let max = col
118 else
119 let min = col + 1
120 endif
121 endwhile
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000122 let pline = strpart(pline, 0, min - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000124 else
125 let col = 0
126 while col < pline_len
127 if pline[col] == '#'
128 let pline = strpart(pline, 0, col)
129 break
130 endif
131 let col = col + 1
132 endwhile
133 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
135 " If the previous line ended with a colon, indent this line
136 if pline =~ ':\s*$'
137 return plindent + &sw
138 endif
139
140 " If the previous line was a stop-execution statement...
Bram Moolenaar9964e462007-05-05 17:54:07 +0000141 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 " See if the user has already dedented
143 if indent(a:lnum) > indent(plnum) - &sw
144 " If not, recommend one dedent
145 return indent(plnum) - &sw
146 endif
147 " Otherwise, trust the user
148 return -1
149 endif
150
151 " If the current line begins with a keyword that lines up with "try"
152 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
153 let lnum = a:lnum - 1
154 while lnum >= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if getline(lnum) =~ '^\s*\(try\|except\)\>'
156 let ind = indent(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 if ind >= indent(a:lnum)
158 return -1 " indent is already less than this
159 endif
160 return ind " line up with previous try or except
161 endif
162 let lnum = lnum - 1
163 endwhile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 return -1 " no matching "try"!
165 endif
166
167 " If the current line begins with a header keyword, dedent
168 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
169
170 " Unless the previous line was a one-liner
171 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
172 return plindent
173 endif
174
175 " Or the user has already dedented
176 if indent(a:lnum) <= plindent - &sw
177 return -1
178 endif
179
180 return plindent - &sw
181 endif
182
183 " When after a () construct we probably want to go back to the start line.
184 " a = (b
185 " + c)
186 " here
187 if parlnum > 0
188 return plindent
189 endif
190
191 return -1
192
193endfunction
194
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200195let &cpo = s:keepcpo
196unlet s:keepcpo
197
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198" vim:sw=2