blob: 668122993e71d20eb9b0d70824b4873c60892f93 [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 Moolenaar6e649222021-10-04 21:32:54 +01005" Last Change: 2021 Sep 26
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
Bram Moolenaar6e649222021-10-04 21:32:54 +010020let b:undo_indent = "setl ai< inde< indk< lisp<"
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022" Only define the function once.
23if exists("*GetPythonIndent")
24 finish
25endif
Bram Moolenaar80716072012-05-01 21:14:34 +020026let s:keepcpo= &cpo
27set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
Bram Moolenaar05159a02005-02-26 23:04:13 +000029" Come here when loading the script the first time.
30
Bram Moolenaar071d4272004-06-13 20:20:40 +000031let s:maxoff = 50 " maximum number of lines to look backwards for ()
32
Bram Moolenaar47e13952020-05-12 22:49:12 +020033" See if the specified line is already user-dedented from the expected value.
34function s:Dedented(lnum, expected)
35 return indent(a:lnum) <= a:expected - shiftwidth()
36endfunction
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038function GetPythonIndent(lnum)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000039
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 " If this line is explicitly joined: If the previous line was also joined,
41 " line it up with that one, otherwise add two 'shiftwidth'
42 if getline(a:lnum - 1) =~ '\\$'
43 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
44 return indent(a:lnum - 1)
45 endif
Bram Moolenaar56b45b92013-06-24 22:22:18 +020046 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 endif
48
49 " If the start of the line is in a string don't change the indent.
50 if has('syntax_items')
Bram Moolenaared203462004-06-16 11:19:22 +000051 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 return -1
53 endif
54
55 " Search backwards for the previous non-empty line.
56 let plnum = prevnonblank(v:lnum - 1)
57
58 if plnum == 0
59 " This is the first non-empty line, use zero indent.
60 return 0
61 endif
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 call cursor(plnum, 1)
Bram Moolenaarf6b40102019-02-22 15:24:03 +010064
65 " Identing inside parentheses can be very slow, regardless of the searchpair()
66 " timeout, so let the user disable this feature if he doesn't need it
67 let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0)
68
69 if disable_parentheses_indenting == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 let plindent = indent(plnum)
71 let plnumstart = plnum
Bram Moolenaarf6b40102019-02-22 15:24:03 +010072 else
73 " searchpair() can be slow sometimes, limit the time to 150 msec or what is
74 " put in g:pyindent_searchpair_timeout
75 let searchpair_stopline = 0
76 let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
Bram Moolenaarf6b40102019-02-22 15:24:03 +010078 " If the previous line is inside parenthesis, use the indent of the starting
79 " line.
80 " Trick: use the non-existing "dummy" variable to break out of the loop when
81 " going too far back.
82 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
83 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
84 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
85 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
86 \ searchpair_stopline, searchpair_timeout)
87 if parlnum > 0
88 let plindent = indent(parlnum)
89 let plnumstart = parlnum
90 else
91 let plindent = indent(plnum)
92 let plnumstart = plnum
93 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaarf6b40102019-02-22 15:24:03 +010095 " When inside parenthesis: If at the first line below the parenthesis add
96 " two 'shiftwidth', otherwise same as previous line.
97 " i = (a
98 " + b
99 " + c)
100 call cursor(a:lnum, 1)
101 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
102 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
103 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
104 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
105 \ searchpair_stopline, searchpair_timeout)
106 if p > 0
107 if p == plnum
108 " When the start is inside parenthesis, only indent one 'shiftwidth'.
109 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
110 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
111 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
112 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
113 \ searchpair_stopline, searchpair_timeout)
114 if pp > 0
115 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
116 endif
117 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 endif
Bram Moolenaarf6b40102019-02-22 15:24:03 +0100119 if plnumstart == p
120 return indent(plnum)
121 endif
122 return plindent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 endif
Bram Moolenaarf6b40102019-02-22 15:24:03 +0100124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 endif
126
127
128 " Get the line and remove a trailing comment.
129 " Use syntax highlighting attributes when possible.
130 let pline = getline(plnum)
131 let pline_len = strlen(pline)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000132 if has('syntax_items')
133 " If the last character in the line is a comment, do a binary search for
134 " the start of the comment. synID() is slow, a linear search would take
135 " too long on a long line.
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200136 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000137 let min = 1
138 let max = pline_len
139 while min < max
140 let col = (min + max) / 2
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200141 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000142 let max = col
143 else
144 let min = col + 1
145 endif
146 endwhile
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000147 let pline = strpart(pline, 0, min - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000149 else
150 let col = 0
151 while col < pline_len
152 if pline[col] == '#'
153 let pline = strpart(pline, 0, col)
154 break
155 endif
156 let col = col + 1
157 endwhile
158 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160 " If the previous line ended with a colon, indent this line
161 if pline =~ ':\s*$'
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200162 return plindent + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 endif
164
165 " If the previous line was a stop-execution statement...
Bram Moolenaar9964e462007-05-05 17:54:07 +0000166 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 " See if the user has already dedented
Bram Moolenaar47e13952020-05-12 22:49:12 +0200168 if s:Dedented(a:lnum, indent(plnum))
169 " If so, trust the user
170 return -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 endif
Bram Moolenaar47e13952020-05-12 22:49:12 +0200172 " If not, recommend one dedent
173 return indent(plnum) - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 endif
175
176 " If the current line begins with a keyword that lines up with "try"
177 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
178 let lnum = a:lnum - 1
179 while lnum >= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 if getline(lnum) =~ '^\s*\(try\|except\)\>'
181 let ind = indent(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if ind >= indent(a:lnum)
183 return -1 " indent is already less than this
184 endif
185 return ind " line up with previous try or except
186 endif
187 let lnum = lnum - 1
188 endwhile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 return -1 " no matching "try"!
190 endif
191
192 " If the current line begins with a header keyword, dedent
193 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
194
195 " Unless the previous line was a one-liner
Bram Moolenaard2ea7cf2021-05-30 20:54:13 +0200196 if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 return plindent
198 endif
199
200 " Or the user has already dedented
Bram Moolenaar47e13952020-05-12 22:49:12 +0200201 if s:Dedented(a:lnum, plindent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 return -1
203 endif
204
Bram Moolenaar56b45b92013-06-24 22:22:18 +0200205 return plindent - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 endif
207
208 " When after a () construct we probably want to go back to the start line.
209 " a = (b
210 " + c)
211 " here
212 if parlnum > 0
Bram Moolenaar47e13952020-05-12 22:49:12 +0200213 " ...unless the user has already dedented
214 if s:Dedented(a:lnum, plindent)
215 return -1
216 else
217 return plindent
218 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 endif
220
221 return -1
222
223endfunction
224
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200225let &cpo = s:keepcpo
226unlet s:keepcpo
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228" vim:sw=2