Showing the changes to lib/vimfiles/indent/python.vim (1 of 1336 files). Show all
Unified · Split
| @@ -0,0 +1,193 @@ | ||
| 1 | +" Vim indent file | |
| 2 | +" Language: Python | |
| 3 | +" Maintainer: Bram Moolenaar <Bram@vim.org> | |
| 4 | +" Original Author: David Bustos <bustos@caltech.edu> | |
| 5 | +" Last Change: 2006 Jun 18 | |
| 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 | + | |
| 25 | +" Come here when loading the script the first time. | |
| 26 | + | |
| 27 | +let s:maxoff = 50 " maximum number of lines to look backwards for () | |
| 28 | + | |
| 29 | +function GetPythonIndent(lnum) | |
| 30 | + | |
| 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 | |
| 37 | + return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (&sw * 2)) | |
| 38 | + endif | |
| 39 | + | |
| 40 | + " If the start of the line is in a string don't change the indent. | |
| 41 | + if has('syntax_items') | |
| 42 | + \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" | |
| 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) | |
| 59 | + let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', | |
| 60 | + \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :" | |
| 61 | + \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
| 62 | + \ . " =~ '\\(Comment\\|String\\)$'") | |
| 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) | |
| 78 | + let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', | |
| 79 | + \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" | |
| 80 | + \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
| 81 | + \ . " =~ '\\(Comment\\|String\\)$'") | |
| 82 | + if p > 0 | |
| 83 | + if p == plnum | |
| 84 | + " When the start is inside parenthesis, only indent one 'shiftwidth'. | |
| 85 | + let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', | |
| 86 | + \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" | |
| 87 | + \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
| 88 | + \ . " =~ '\\(Comment\\|String\\)$'") | |
| 89 | + if pp > 0 | |
| 90 | + return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : &sw) | |
| 91 | + endif | |
| 92 | + return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (&sw * 2)) | |
| 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) | |
| 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 | |
| 120 | + let pline = strpart(pline, 0, min - 1) | |
| 121 | + endif | |
| 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 | |
| 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\|pass\)\>' | |
| 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 | |
| 153 | + if getline(lnum) =~ '^\s*\(try\|except\)\>' | |
| 154 | + let ind = indent(lnum) | |
| 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 | |
| 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 | |