1" Vim indent file 2" Language: Python 3" Maintainer: Bram Moolenaar <[email protected]> 4" Original Author: David Bustos <[email protected]> 5" Last Change: 2019 Feb 21 6 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 24let s:keepcpo= &cpo 25set cpo&vim 26 27" Come here when loading the script the first time. 28 29let s:maxoff = 50 " maximum number of lines to look backwards for () 30 31function GetPythonIndent(lnum) 32 33 " 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 39 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2)) 40 endif 41 42 " If the start of the line is in a string don't change the indent. 43 if has('syntax_items') 44 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" 45 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 call cursor(plnum, 1) 57 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 63 let plindent = indent(plnum) 64 let plnumstart = plnum 65 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) 70 71 " 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 87 88 " 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)) 111 endif 112 if plnumstart == p 113 return indent(plnum) 114 endif 115 return plindent 116 endif 117 118 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) 125 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. 129 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" 130 let min = 1 131 let max = pline_len 132 while min < max 133 let col = (min + max) / 2 134 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" 135 let max = col 136 else 137 let min = col + 1 138 endif 139 endwhile 140 let pline = strpart(pline, 0, min - 1) 141 endif 142 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 152 153 " If the previous line ended with a colon, indent this line 154 if pline =~ ':\s*$' 155 return plindent + shiftwidth() 156 endif 157 158 " If the previous line was a stop-execution statement... 159 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' 160 " See if the user has already dedented 161 if indent(a:lnum) > indent(plnum) - shiftwidth() 162 " If not, recommend one dedent 163 return indent(plnum) - shiftwidth() 164 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 173 if getline(lnum) =~ '^\s*\(try\|except\)\>' 174 let ind = indent(lnum) 175 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 182 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 194 if indent(a:lnum) <= plindent - shiftwidth() 195 return -1 196 endif 197 198 return plindent - shiftwidth() 199 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 213let &cpo = s:keepcpo 214unlet s:keepcpo 215 216" vim:sw=2 217