Mercurial > vim
annotate runtime/indent/python.vim @ 5770:839cca5ec18d v7.4.229
updated for version 7.4.229
Problem: Using ":let" for listing variables and the second one is a curly
braces expression may fail.
Solution: Check for an "=" in a better way. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 30 Mar 2014 16:49:09 +0200 |
parents | 050893d44c33 |
children | 4ee65b4150fd |
rev | line source |
---|---|
7 | 1 " Vim indent file |
164 | 2 " Language: Python |
3 " Maintainer: Bram Moolenaar <Bram@vim.org> | |
7 | 4 " Original Author: David Bustos <bustos@caltech.edu> |
5220 | 5 " Last Change: 2013 Jul 9 |
7 | 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 | |
3507
8201108e9cf0
More runtime file fixes for 'compatible' mode.
Bram Moolenaar <bram@vim.org>
parents:
3496
diff
changeset
|
24 let s:keepcpo= &cpo |
8201108e9cf0
More runtime file fixes for 'compatible' mode.
Bram Moolenaar <bram@vim.org>
parents:
3496
diff
changeset
|
25 set cpo&vim |
7 | 26 |
170 | 27 " Come here when loading the script the first time. |
28 | |
7 | 29 let s:maxoff = 50 " maximum number of lines to look backwards for () |
30 | |
31 function GetPythonIndent(lnum) | |
856 | 32 |
7 | 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 | |
4992 | 39 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2)) |
7 | 40 endif |
41 | |
42 " If the start of the line is in a string don't change the indent. | |
43 if has('syntax_items') | |
8 | 44 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$" |
7 | 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) | |
819 | 61 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', |
7 | 62 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :" |
63 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
5220 | 64 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'") |
7 | 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) | |
819 | 80 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
7 | 81 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
82 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
5220 | 83 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'") |
7 | 84 if p > 0 |
85 if p == plnum | |
86 " When the start is inside parenthesis, only indent one 'shiftwidth'. | |
819 | 87 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
7 | 88 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
89 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" | |
5220 | 90 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'") |
7 | 91 if pp > 0 |
4992 | 92 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth()) |
7 | 93 endif |
4992 | 94 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2)) |
7 | 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) | |
20 | 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. | |
5220 | 111 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
20 | 112 let min = 1 |
113 let max = pline_len | |
114 while min < max | |
115 let col = (min + max) / 2 | |
5220 | 116 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$" |
20 | 117 let max = col |
118 else | |
119 let min = col + 1 | |
120 endif | |
121 endwhile | |
122 let pline = strpart(pline, 0, min - 1) | |
7 | 123 endif |
20 | 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 | |
7 | 134 |
135 " If the previous line ended with a colon, indent this line | |
136 if pline =~ ':\s*$' | |
4992 | 137 return plindent + shiftwidth() |
7 | 138 endif |
139 | |
140 " If the previous line was a stop-execution statement... | |
1121 | 141 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' |
7 | 142 " See if the user has already dedented |
4992 | 143 if indent(a:lnum) > indent(plnum) - shiftwidth() |
7 | 144 " If not, recommend one dedent |
4992 | 145 return indent(plnum) - shiftwidth() |
7 | 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 | |
155 if getline(lnum) =~ '^\s*\(try\|except\)\>' | |
156 let ind = indent(lnum) | |
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 | |
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 | |
4992 | 176 if indent(a:lnum) <= plindent - shiftwidth() |
7 | 177 return -1 |
178 endif | |
179 | |
4992 | 180 return plindent - shiftwidth() |
7 | 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 | |
3496
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
195 let &cpo = s:keepcpo |
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
196 unlet s:keepcpo |
d1e4abe8342c
Fixed compatible mode in most runtime files.
Bram Moolenaar <bram@vim.org>
parents:
1121
diff
changeset
|
197 |
7 | 198 " vim:sw=2 |