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 | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 5 | " Last Change: 2019 Feb 21 |
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 | |
| 31 | function GetPythonIndent(lnum) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 32 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 39 | 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] | 40 | endif |
| 41 | |
| 42 | " If the start of the line is in a string don't change the indent. |
| 43 | if has('syntax_items') |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 44 | \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | call cursor(plnum, 1) |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | let plindent = indent(plnum) |
| 64 | let plnumstart = plnum |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 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) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 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)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | endif |
Bram Moolenaar | f6b4010 | 2019-02-22 15:24:03 +0100 | [diff] [blame] | 112 | if plnumstart == p |
| 113 | return indent(plnum) |
| 114 | endif |
| 115 | return plindent |
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 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 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. |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 129 | if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 130 | let min = 1 |
| 131 | let max = pline_len |
| 132 | while min < max |
| 133 | let col = (min + max) / 2 |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 134 | if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 135 | let max = col |
| 136 | else |
| 137 | let min = col + 1 |
| 138 | endif |
| 139 | endwhile |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 140 | let pline = strpart(pline, 0, min - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | |
| 153 | " If the previous line ended with a colon, indent this line |
| 154 | if pline =~ ':\s*$' |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 155 | return plindent + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | endif |
| 157 | |
| 158 | " If the previous line was a stop-execution statement... |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 159 | if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | " See if the user has already dedented |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 161 | if indent(a:lnum) > indent(plnum) - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 162 | " If not, recommend one dedent |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 163 | return indent(plnum) - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | if getline(lnum) =~ '^\s*\(try\|except\)\>' |
| 174 | let ind = indent(lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 194 | if indent(a:lnum) <= plindent - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | return -1 |
| 196 | endif |
| 197 | |
Bram Moolenaar | 56b45b9 | 2013-06-24 22:22:18 +0200 | [diff] [blame] | 198 | return plindent - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
| 211 | endfunction |
| 212 | |
Bram Moolenaar | 9a7224b | 2012-04-30 15:56:52 +0200 | [diff] [blame] | 213 | let &cpo = s:keepcpo |
| 214 | unlet s:keepcpo |
| 215 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | " vim:sw=2 |