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 | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 5 | " Last Change: 2021 Sep 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 | |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 20 | let b:undo_indent = "setl ai< inde< indk< lisp<" |
| 21 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | " Only define the function once. |
| 23 | if exists("*GetPythonIndent") |
| 24 | finish |
| 25 | endif |
Bram Moolenaar | 8071607 | 2012-05-01 21:14:34 +0200 | [diff] [blame] | 26 | let s:keepcpo= &cpo |
| 27 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 29 | " Come here when loading the script the first time. |
| 30 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 31 | let s:maxoff = 50 " maximum number of lines to look backwards for () |
| 32 | |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 33 | " See if the specified line is already user-dedented from the expected value. |
| 34 | function s:Dedented(lnum, expected) |
| 35 | return indent(a:lnum) <= a:expected - shiftwidth() |
| 36 | endfunction |
| 37 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | function GetPythonIndent(lnum) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 39 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 40 | " 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 Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 46 | 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] | 47 | endif |
| 48 | |
| 49 | " If the start of the line is in a string don't change the indent. |
| 50 | if has('syntax_items') |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 51 | \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | call cursor(plnum, 1) |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 64 | |
| 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | let plindent = indent(plnum) |
| 71 | let plnumstart = plnum |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 72 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 78 | " 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 95 | " 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 118 | endif |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 119 | if plnumstart == p |
| 120 | return indent(plnum) |
| 121 | endif |
| 122 | return plindent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | endif |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 124 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 125 | 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 Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 132 | 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 Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 136 | if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 137 | let min = 1 |
| 138 | let max = pline_len |
| 139 | while min < max |
| 140 | let col = (min + max) / 2 |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 141 | if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 142 | let max = col |
| 143 | else |
| 144 | let min = col + 1 |
| 145 | endif |
| 146 | endwhile |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 147 | let pline = strpart(pline, 0, min - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 148 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 149 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | |
| 160 | " If the previous line ended with a colon, indent this line |
| 161 | if pline =~ ':\s*$' |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 162 | return plindent + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | endif |
| 164 | |
| 165 | " If the previous line was a stop-execution statement... |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 166 | if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 167 | " See if the user has already dedented |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 168 | if s:Dedented(a:lnum, indent(plnum)) |
| 169 | " If so, trust the user |
| 170 | return -1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 171 | endif |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 172 | " If not, recommend one dedent |
| 173 | return indent(plnum) - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 174 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | if getline(lnum) =~ '^\s*\(try\|except\)\>' |
| 181 | let ind = indent(lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | 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 Moolenaar | d2ea7cf | 2021-05-30 20:54:13 +0200 | [diff] [blame] | 196 | if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | return plindent |
| 198 | endif |
| 199 | |
| 200 | " Or the user has already dedented |
Bram Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 201 | if s:Dedented(a:lnum, plindent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | return -1 |
| 203 | endif |
| 204 | |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 205 | return plindent - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 206 | 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 Moolenaar | 47e1395 | 2020-05-12 22:49:12 +0200 | [diff] [blame] | 213 | " ...unless the user has already dedented |
| 214 | if s:Dedented(a:lnum, plindent) |
| 215 | return -1 |
| 216 | else |
| 217 | return plindent |
| 218 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | endif |
| 220 | |
| 221 | return -1 |
| 222 | |
| 223 | endfunction |
| 224 | |
Bram Moolenaar | 9a7224b | 2012-04-30 15:56:52 +0200 | [diff] [blame] | 225 | let &cpo = s:keepcpo |
| 226 | unlet s:keepcpo |
| 227 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 228 | " vim:sw=2 |