blob: e53987a0de8e0834478ebe6f7f044ceb7f280a64 [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 Moolenaarf6b40102019-02-22 15:24:03 +01005" Last Change: 2019 Feb 21
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 Moolenaar071d4272004-06-13 20:20:40 +000056 call cursor(plnum, 1)
Bram Moolenaarf6b40102019-02-22 15:24:03 +010057
58 " Identing inside parentheses can be very slow, regardless of the searchpair()
59 " timeout, so let the user disable this feature if he doesn't need it
60 let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0)
61
62 if disable_parentheses_indenting == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 let plindent = indent(plnum)
64 let plnumstart = plnum
Bram Moolenaarf6b40102019-02-22 15:24:03 +010065 else
66 " searchpair() can be slow sometimes, limit the time to 150 msec or what is
67 " put in g:pyindent_searchpair_timeout
68 let searchpair_stopline = 0
69 let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarf6b40102019-02-22 15:24:03 +010071 " If the previous line is inside parenthesis, use the indent of the starting
72 " line.
73 " Trick: use the non-existing "dummy" variable to break out of the loop when
74 " going too far back.
75 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
76 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
77 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
78 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
79 \ searchpair_stopline, searchpair_timeout)
80 if parlnum > 0
81 let plindent = indent(parlnum)
82 let plnumstart = parlnum
83 else
84 let plindent = indent(plnum)
85 let plnumstart = plnum
86 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
Bram Moolenaarf6b40102019-02-22 15:24:03 +010088 " When inside parenthesis: If at the first line below the parenthesis add
89 " two 'shiftwidth', otherwise same as previous line.
90 " i = (a
91 " + b
92 " + c)
93 call cursor(a:lnum, 1)
94 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
95 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
96 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
97 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
98 \ searchpair_stopline, searchpair_timeout)
99 if p > 0
100 if p == plnum
101 " When the start is inside parenthesis, only indent one 'shiftwidth'.
102 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
103 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
104 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
105 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
106 \ searchpair_stopline, searchpair_timeout)
107 if pp > 0
108 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
109 endif
110 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 endif
Bram Moolenaarf6b40102019-02-22 15:24:03 +0100112 if plnumstart == p
113 return indent(plnum)
114 endif
115 return plindent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 endif
Bram Moolenaarf6b40102019-02-22 15:24:03 +0100117
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 endif
119
120
121 " Get the line and remove a trailing comment.
122 " Use syntax highlighting attributes when possible.
123 let pline = getline(plnum)
124 let pline_len = strlen(pline)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000125 if has('syntax_items')
126 " If the last character in the line is a comment, do a binary search for
127 " the start of the comment. synID() is slow, a linear search would take
128 " too long on a long line.
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200129 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000130 let min = 1
131 let max = pline_len
132 while min < max
133 let col = (min + max) / 2
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200134 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000135 let max = col
136 else
137 let min = col + 1
138 endif
139 endwhile
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000140 let pline = strpart(pline, 0, min - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000142 else
143 let col = 0
144 while col < pline_len
145 if pline[col] == '#'
146 let pline = strpart(pline, 0, col)
147 break
148 endif
149 let col = col + 1
150 endwhile
151 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153 " If the previous line ended with a colon, indent this line
154 if pline =~ ':\s*$'
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200155 return plindent + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 endif
157
158 " If the previous line was a stop-execution statement...
Bram Moolenaar9964e462007-05-05 17:54:07 +0000159 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 " See if the user has already dedented
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200161 if indent(a:lnum) > indent(plnum) - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 " If not, recommend one dedent
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200163 return indent(plnum) - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 endif
165 " Otherwise, trust the user
166 return -1
167 endif
168
169 " If the current line begins with a keyword that lines up with "try"
170 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
171 let lnum = a:lnum - 1
172 while lnum >= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if getline(lnum) =~ '^\s*\(try\|except\)\>'
174 let ind = indent(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 if ind >= indent(a:lnum)
176 return -1 " indent is already less than this
177 endif
178 return ind " line up with previous try or except
179 endif
180 let lnum = lnum - 1
181 endwhile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 return -1 " no matching "try"!
183 endif
184
185 " If the current line begins with a header keyword, dedent
186 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
187
188 " Unless the previous line was a one-liner
189 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
190 return plindent
191 endif
192
193 " Or the user has already dedented
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200194 if indent(a:lnum) <= plindent - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 return -1
196 endif
197
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200198 return plindent - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 endif
200
201 " When after a () construct we probably want to go back to the start line.
202 " a = (b
203 " + c)
204 " here
205 if parlnum > 0
206 return plindent
207 endif
208
209 return -1
210
211endfunction
212
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200213let &cpo = s:keepcpo
214unlet s:keepcpo
215
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216" vim:sw=2