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 | 8071607 | 2012-05-01 21:14:34 +0200 | [diff] [blame] | 5 | " Last Change: 2012 May 01 |
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 | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 39 | return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (&sw * 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 | |
| 56 | " If the previous line is inside parenthesis, use the indent of the starting |
| 57 | " line. |
| 58 | " Trick: use the non-existing "dummy" variable to break out of the loop when |
| 59 | " going too far back. |
| 60 | call cursor(plnum, 1) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 61 | let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :" |
| 63 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 64 | \ . " =~ '\\(Comment\\|String\\)$'") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | if parlnum > 0 |
| 66 | let plindent = indent(parlnum) |
| 67 | let plnumstart = parlnum |
| 68 | else |
| 69 | let plindent = indent(plnum) |
| 70 | let plnumstart = plnum |
| 71 | endif |
| 72 | |
| 73 | |
| 74 | " When inside parenthesis: If at the first line below the parenthesis add |
| 75 | " two 'shiftwidth', otherwise same as previous line. |
| 76 | " i = (a |
| 77 | " + b |
| 78 | " + c) |
| 79 | call cursor(a:lnum, 1) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 80 | let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
| 82 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 83 | \ . " =~ '\\(Comment\\|String\\)$'") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | if p > 0 |
| 85 | if p == plnum |
| 86 | " When the start is inside parenthesis, only indent one 'shiftwidth'. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 87 | let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
| 89 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 90 | \ . " =~ '\\(Comment\\|String\\)$'") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | if pp > 0 |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 92 | return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : &sw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | endif |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 94 | return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (&sw * 2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | endif |
| 96 | if plnumstart == p |
| 97 | return indent(plnum) |
| 98 | endif |
| 99 | return plindent |
| 100 | endif |
| 101 | |
| 102 | |
| 103 | " Get the line and remove a trailing comment. |
| 104 | " Use syntax highlighting attributes when possible. |
| 105 | let pline = getline(plnum) |
| 106 | let pline_len = strlen(pline) |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 107 | if has('syntax_items') |
| 108 | " If the last character in the line is a comment, do a binary search for |
| 109 | " the start of the comment. synID() is slow, a linear search would take |
| 110 | " too long on a long line. |
| 111 | if synIDattr(synID(plnum, pline_len, 1), "name") =~ "Comment$" |
| 112 | let min = 1 |
| 113 | let max = pline_len |
| 114 | while min < max |
| 115 | let col = (min + max) / 2 |
| 116 | if synIDattr(synID(plnum, col, 1), "name") =~ "Comment$" |
| 117 | let max = col |
| 118 | else |
| 119 | let min = col + 1 |
| 120 | endif |
| 121 | endwhile |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 122 | let pline = strpart(pline, 0, min - 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 124 | else |
| 125 | let col = 0 |
| 126 | while col < pline_len |
| 127 | if pline[col] == '#' |
| 128 | let pline = strpart(pline, 0, col) |
| 129 | break |
| 130 | endif |
| 131 | let col = col + 1 |
| 132 | endwhile |
| 133 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | |
| 135 | " If the previous line ended with a colon, indent this line |
| 136 | if pline =~ ':\s*$' |
| 137 | return plindent + &sw |
| 138 | endif |
| 139 | |
| 140 | " If the previous line was a stop-execution statement... |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 141 | if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 142 | " See if the user has already dedented |
| 143 | if indent(a:lnum) > indent(plnum) - &sw |
| 144 | " If not, recommend one dedent |
| 145 | return indent(plnum) - &sw |
| 146 | endif |
| 147 | " Otherwise, trust the user |
| 148 | return -1 |
| 149 | endif |
| 150 | |
| 151 | " If the current line begins with a keyword that lines up with "try" |
| 152 | if getline(a:lnum) =~ '^\s*\(except\|finally\)\>' |
| 153 | let lnum = a:lnum - 1 |
| 154 | while lnum >= 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | if getline(lnum) =~ '^\s*\(try\|except\)\>' |
| 156 | let ind = indent(lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | if ind >= indent(a:lnum) |
| 158 | return -1 " indent is already less than this |
| 159 | endif |
| 160 | return ind " line up with previous try or except |
| 161 | endif |
| 162 | let lnum = lnum - 1 |
| 163 | endwhile |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 164 | return -1 " no matching "try"! |
| 165 | endif |
| 166 | |
| 167 | " If the current line begins with a header keyword, dedent |
| 168 | if getline(a:lnum) =~ '^\s*\(elif\|else\)\>' |
| 169 | |
| 170 | " Unless the previous line was a one-liner |
| 171 | if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>' |
| 172 | return plindent |
| 173 | endif |
| 174 | |
| 175 | " Or the user has already dedented |
| 176 | if indent(a:lnum) <= plindent - &sw |
| 177 | return -1 |
| 178 | endif |
| 179 | |
| 180 | return plindent - &sw |
| 181 | endif |
| 182 | |
| 183 | " When after a () construct we probably want to go back to the start line. |
| 184 | " a = (b |
| 185 | " + c) |
| 186 | " here |
| 187 | if parlnum > 0 |
| 188 | return plindent |
| 189 | endif |
| 190 | |
| 191 | return -1 |
| 192 | |
| 193 | endfunction |
| 194 | |
Bram Moolenaar | 9a7224b | 2012-04-30 15:56:52 +0200 | [diff] [blame] | 195 | let &cpo = s:keepcpo |
| 196 | unlet s:keepcpo |
| 197 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 198 | " vim:sw=2 |