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