Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2 | " Language: Python |
| 3 | " Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4 | " Original Author: David Bustos <bustos@caltech.edu> |
Bram Moolenaar | d2ea7cf | 2021-05-30 20:54:13 +0200 | [diff] [blame] | 5 | " Last Change: 2021 May 26 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6 | |
| 7 | " Only load this indent file when no other was loaded. |
| 8 | if exists("b:did_indent") |
| 9 | finish |
| 10 | endif |
| 11 | let b:did_indent = 1 |
| 12 | |
| 13 | " Some preliminary settings |
| 14 | setlocal nolisp " Make sure lisp indenting doesn't supersede us |
| 15 | setlocal autoindent " indentexpr isn't much help otherwise |
| 16 | |
| 17 | setlocal indentexpr=GetPythonIndent(v:lnum) |
| 18 | setlocal indentkeys+=<:>,=elif,=except |
| 19 | |
| 20 | " Only define the function once. |
| 21 | if exists("*GetPythonIndent") |
| 22 | finish |
| 23 | endif |
Bram Moolenaar | 8071607 | 2012-05-01 21:14:34 +0200 | [diff] [blame] | 24 | let s:keepcpo= &cpo |
| 25 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 27 | " Come here when loading the script the first time. |
| 28 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | let s:maxoff = 50 " maximum number of lines to look backwards for () |
| 30 | |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 31 | " See if the specified line is already user-dedented from the expected value. |
| 32 | function s:Dedented(lnum, expected) |
| 33 | return indent(a:lnum) <= a:expected - shiftwidth() |
| 34 | endfunction |
| 35 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 36 | function GetPythonIndent(lnum) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 37 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | " If this line is explicitly joined: If the previous line was also joined, |
| 39 | " line it up with that one, otherwise add two 'shiftwidth' |
| 40 | if getline(a:lnum - 1) =~ '\\$' |
| 41 | if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$' |
| 42 | return indent(a:lnum - 1) |
| 43 | endif |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 44 | return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | endif |
| 46 | |
| 47 | " If the start of the line is in a string don't change the indent. |
| 48 | if has('syntax_items') |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 49 | \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | return -1 |
| 51 | endif |
| 52 | |
| 53 | " Search backwards for the previous non-empty line. |
| 54 | let plnum = prevnonblank(v:lnum - 1) |
| 55 | |
| 56 | if plnum == 0 |
| 57 | " This is the first non-empty line, use zero indent. |
| 58 | return 0 |
| 59 | endif |
| 60 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | call cursor(plnum, 1) |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 62 | |
| 63 | " Identing inside parentheses can be very slow, regardless of the searchpair() |
| 64 | " timeout, so let the user disable this feature if he doesn't need it |
| 65 | let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0) |
| 66 | |
| 67 | if disable_parentheses_indenting == 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | let plindent = indent(plnum) |
| 69 | let plnumstart = plnum |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 70 | else |
| 71 | " searchpair() can be slow sometimes, limit the time to 150 msec or what is |
| 72 | " put in g:pyindent_searchpair_timeout |
| 73 | let searchpair_stopline = 0 |
| 74 | let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 76 | " If the previous line is inside parenthesis, use the indent of the starting |
| 77 | " line. |
| 78 | " Trick: use the non-existing "dummy" variable to break out of the loop when |
| 79 | " going too far back. |
| 80 | let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', |
| 81 | \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :" |
| 82 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
| 83 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", |
| 84 | \ searchpair_stopline, searchpair_timeout) |
| 85 | if parlnum > 0 |
| 86 | let plindent = indent(parlnum) |
| 87 | let plnumstart = parlnum |
| 88 | else |
| 89 | let plindent = indent(plnum) |
| 90 | let plnumstart = plnum |
| 91 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 92 | |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 93 | " When inside parenthesis: If at the first line below the parenthesis add |
| 94 | " two 'shiftwidth', otherwise same as previous line. |
| 95 | " i = (a |
| 96 | " + b |
| 97 | " + c) |
| 98 | call cursor(a:lnum, 1) |
| 99 | let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
| 100 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
| 101 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
| 102 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", |
| 103 | \ searchpair_stopline, searchpair_timeout) |
| 104 | if p > 0 |
| 105 | if p == plnum |
| 106 | " When the start is inside parenthesis, only indent one 'shiftwidth'. |
| 107 | let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
| 108 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
| 109 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
| 110 | \ . " =~ '\\(Comment\\|Todo\\|String\\)$'", |
| 111 | \ searchpair_stopline, searchpair_timeout) |
| 112 | if pp > 0 |
| 113 | return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth()) |
| 114 | endif |
| 115 | return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 116 | endif |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 117 | if plnumstart == p |
| 118 | return indent(plnum) |
| 119 | endif |
| 120 | return plindent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | endif |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 122 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | endif |
| 124 | |
| 125 | |
| 126 | " Get the line and remove a trailing comment. |
| 127 | " Use syntax highlighting attributes when possible. |
| 128 | let pline = getline(plnum) |
| 129 | let pline_len = strlen(pline) |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 130 | if has('syntax_items') |
| 131 | " If the last character in the line is a comment, do a binary search for |
| 132 | " the start of the comment. synID() is slow, a linear search would take |
| 133 | " too long on a long line. |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 134 | if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 135 | let min = 1 |
| 136 | let max = pline_len |
| 137 | while min < max |
| 138 | let col = (min + max) / 2 |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 139 | if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 140 | let max = col |
| 141 | else |
| 142 | let min = col + 1 |
| 143 | endif |
| 144 | endwhile |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 145 | let pline = strpart(pline, 0, min - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 146 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 147 | else |
| 148 | let col = 0 |
| 149 | while col < pline_len |
| 150 | if pline[col] == '#' |
| 151 | let pline = strpart(pline, 0, col) |
| 152 | break |
| 153 | endif |
| 154 | let col = col + 1 |
| 155 | endwhile |
| 156 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | |
| 158 | " If the previous line ended with a colon, indent this line |
| 159 | if pline =~ ':\s*$' |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 160 | return plindent + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | endif |
| 162 | |
| 163 | " If the previous line was a stop-execution statement... |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 164 | if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 165 | " See if the user has already dedented |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 166 | if s:Dedented(a:lnum, indent(plnum)) |
| 167 | " If so, trust the user |
| 168 | return -1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 169 | endif |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 170 | " If not, recommend one dedent |
| 171 | return indent(plnum) - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | endif |
| 173 | |
| 174 | " If the current line begins with a keyword that lines up with "try" |
| 175 | if getline(a:lnum) =~ '^\s*\(except\|finally\)\>' |
| 176 | let lnum = a:lnum - 1 |
| 177 | while lnum >= 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 178 | if getline(lnum) =~ '^\s*\(try\|except\)\>' |
| 179 | let ind = indent(lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | if ind >= indent(a:lnum) |
| 181 | return -1 " indent is already less than this |
| 182 | endif |
| 183 | return ind " line up with previous try or except |
| 184 | endif |
| 185 | let lnum = lnum - 1 |
| 186 | endwhile |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | return -1 " no matching "try"! |
| 188 | endif |
| 189 | |
| 190 | " If the current line begins with a header keyword, dedent |
| 191 | if getline(a:lnum) =~ '^\s*\(elif\|else\)\>' |
| 192 | |
| 193 | " Unless the previous line was a one-liner |
Bram Moolenaar | d2ea7cf | 2021-05-30 20:54:13 +0200 | [diff] [blame] | 194 | if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | return plindent |
| 196 | endif |
| 197 | |
| 198 | " Or the user has already dedented |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 199 | if s:Dedented(a:lnum, plindent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 200 | return -1 |
| 201 | endif |
| 202 | |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 203 | return plindent - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | endif |
| 205 | |
| 206 | " When after a () construct we probably want to go back to the start line. |
| 207 | " a = (b |
| 208 | " + c) |
| 209 | " here |
| 210 | if parlnum > 0 |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 211 | " ...unless the user has already dedented |
| 212 | if s:Dedented(a:lnum, plindent) |
| 213 | return -1 |
| 214 | else |
| 215 | return plindent |
| 216 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 217 | endif |
| 218 | |
| 219 | return -1 |
| 220 | |
| 221 | endfunction |
| 222 | |
Bram Moolenaar | 9a7224b | 2012-04-30 15:56:52 +0200 | [diff] [blame] | 223 | let &cpo = s:keepcpo |
| 224 | unlet s:keepcpo |
| 225 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 226 | " vim:sw=2 |