blob: 7ab3cb9f50f4c2beec175e77fb17a20e6b7b94be [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 Moolenaar9ba7e172013-07-17 22:37:26 +02005" Last Change: 2013 Jul 9
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
12
13" Some preliminary settings
14setlocal nolisp " Make sure lisp indenting doesn't supersede us
15setlocal autoindent " indentexpr isn't much help otherwise
16
17setlocal indentexpr=GetPythonIndent(v:lnum)
18setlocal indentkeys+=<:>,=elif,=except
19
20" Only define the function once.
21if exists("*GetPythonIndent")
22 finish
23endif
Bram Moolenaar80716072012-05-01 21:14:34 +020024let s:keepcpo= &cpo
25set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
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 Moolenaar56b45b92013-06-24 22:22:18 +020039 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 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
Bram Moolenaar2c64ca12018-10-19 16:22:31 +020056 " searchpair() can be slow sometimes, limit the time to 100 msec or what is
57 " put in g:pyindent_searchpair_timeout
58 let searchpair_stopline = 0
59 let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150)
60
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 " If the previous line is inside parenthesis, use the indent of the starting
62 " line.
63 " Trick: use the non-existing "dummy" variable to break out of the loop when
64 " going too far back.
65 call cursor(plnum, 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000066 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
68 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
Bram Moolenaar2c64ca12018-10-19 16:22:31 +020069 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
70 \ searchpair_stopline, searchpair_timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 if parlnum > 0
72 let plindent = indent(parlnum)
73 let plnumstart = parlnum
74 else
75 let plindent = indent(plnum)
76 let plnumstart = plnum
77 endif
78
79
80 " When inside parenthesis: If at the first line below the parenthesis add
81 " two 'shiftwidth', otherwise same as previous line.
82 " i = (a
83 " + b
84 " + c)
85 call cursor(a:lnum, 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000086 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
88 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
Bram Moolenaar2c64ca12018-10-19 16:22:31 +020089 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
90 \ searchpair_stopline, searchpair_timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 if p > 0
92 if p == plnum
93 " When the start is inside parenthesis, only indent one 'shiftwidth'.
Bram Moolenaar910f66f2006-04-05 20:41:53 +000094 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
96 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
Bram Moolenaar2c64ca12018-10-19 16:22:31 +020097 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
98 \ searchpair_stopline, searchpair_timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 if pp > 0
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200100 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 endif
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200102 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 endif
104 if plnumstart == p
105 return indent(plnum)
106 endif
107 return plindent
108 endif
109
110
111 " Get the line and remove a trailing comment.
112 " Use syntax highlighting attributes when possible.
113 let pline = getline(plnum)
114 let pline_len = strlen(pline)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000115 if has('syntax_items')
116 " If the last character in the line is a comment, do a binary search for
117 " the start of the comment. synID() is slow, a linear search would take
118 " too long on a long line.
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200119 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000120 let min = 1
121 let max = pline_len
122 while min < max
123 let col = (min + max) / 2
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200124 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000125 let max = col
126 else
127 let min = col + 1
128 endif
129 endwhile
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000130 let pline = strpart(pline, 0, min - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000132 else
133 let col = 0
134 while col < pline_len
135 if pline[col] == '#'
136 let pline = strpart(pline, 0, col)
137 break
138 endif
139 let col = col + 1
140 endwhile
141 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143 " If the previous line ended with a colon, indent this line
144 if pline =~ ':\s*$'
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200145 return plindent + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 endif
147
148 " If the previous line was a stop-execution statement...
Bram Moolenaar9964e462007-05-05 17:54:07 +0000149 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 " See if the user has already dedented
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200151 if indent(a:lnum) > indent(plnum) - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 " If not, recommend one dedent
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200153 return indent(plnum) - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 endif
155 " Otherwise, trust the user
156 return -1
157 endif
158
159 " If the current line begins with a keyword that lines up with "try"
160 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
161 let lnum = a:lnum - 1
162 while lnum >= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 if getline(lnum) =~ '^\s*\(try\|except\)\>'
164 let ind = indent(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 if ind >= indent(a:lnum)
166 return -1 " indent is already less than this
167 endif
168 return ind " line up with previous try or except
169 endif
170 let lnum = lnum - 1
171 endwhile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 return -1 " no matching "try"!
173 endif
174
175 " If the current line begins with a header keyword, dedent
176 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
177
178 " Unless the previous line was a one-liner
179 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
180 return plindent
181 endif
182
183 " Or the user has already dedented
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200184 if indent(a:lnum) <= plindent - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 return -1
186 endif
187
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200188 return plindent - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 endif
190
191 " When after a () construct we probably want to go back to the start line.
192 " a = (b
193 " + c)
194 " here
195 if parlnum > 0
196 return plindent
197 endif
198
199 return -1
200
201endfunction
202
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200203let &cpo = s:keepcpo
204unlet s:keepcpo
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206" vim:sw=2