Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 196 lines (172 sloc) 5.645 kb
6a166b33 »
2012-01-30 add pathogen and vundle. Start being pep8 compliant.
1 " Python indent file
2 " Language: Python
3 " Maintainer: Eric Mc Sween <em@tomcom.de>
4 " Original Author: David Bustos <bustos@caltech.edu>
5 " Last Change: 2004 Jun 07
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 setlocal expandtab
14 setlocal nolisp
15 setlocal autoindent
16 setlocal indentexpr=GetPythonIndent(v:lnum)
17 setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except
18
19 let s:maxoff = 50
20
21 " Find backwards the closest open parenthesis/bracket/brace.
22 function! s:SearchParensPair()
23 let line = line('.')
24 let col = col('.')
25
26 " Skip strings and comments and don't look too far
27 let skip = "line('.') < " . (line - s:maxoff) . " ? dummy :" .
28 \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' .
29 \ '"string\\|comment"'
30
31 " Search for parentheses
32 call cursor(line, col)
33 let parlnum = searchpair('(', '', ')', 'bW', skip)
34 let parcol = col('.')
35
36 " Search for brackets
37 call cursor(line, col)
38 let par2lnum = searchpair('\[', '', '\]', 'bW', skip)
39 let par2col = col('.')
40
41 " Search for braces
42 call cursor(line, col)
43 let par3lnum = searchpair('{', '', '}', 'bW', skip)
44 let par3col = col('.')
45
46 " Get the closest match
47 if par2lnum > parlnum || (par2lnum == parlnum && par2col > parcol)
48 let parlnum = par2lnum
49 let parcol = par2col
50 endif
51 if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol)
52 let parlnum = par3lnum
53 let parcol = par3col
54 endif
55
56 " Put the cursor on the match
57 if parlnum > 0
58 call cursor(parlnum, parcol)
59 endif
60 return parlnum
61 endfunction
62
63 " Find the start of a multi-line statement
64 function! s:StatementStart(lnum)
65 let lnum = a:lnum
66 while 1
67 if getline(lnum - 1) =~ '\\$'
68 let lnum = lnum - 1
69 else
70 call cursor(lnum, 1)
71 let maybe_lnum = s:SearchParensPair()
72 if maybe_lnum < 1
73 return lnum
74 else
75 let lnum = maybe_lnum
76 endif
77 endif
78 endwhile
79 endfunction
80
81 " Find the block starter that matches the current line
82 function! s:BlockStarter(lnum, block_start_re)
83 let lnum = a:lnum
84 let maxindent = 10000 " whatever
85 while lnum > 1
86 let lnum = prevnonblank(lnum - 1)
87 if indent(lnum) < maxindent
88 if getline(lnum) =~ a:block_start_re
89 return lnum
90 else
91 let maxindent = indent(lnum)
92 " It's not worth going further if we reached the top level
93 if maxindent == 0
94 return -1
95 endif
96 endif
97 endif
98 endwhile
99 return -1
100 endfunction
101
102 function! GetPythonIndent(lnum)
103
104 " First line has indent 0
105 if a:lnum == 1
106 return 0
107 endif
108
109 " If we can find an open parenthesis/bracket/brace, line up with it.
110 call cursor(a:lnum, 1)
111 let parlnum = s:SearchParensPair()
112 if parlnum > 0
113 let parcol = col('.')
114 let closing_paren = match(getline(a:lnum), '^\s*[])}]') != -1
115 if match(getline(parlnum), '[([{]\s*$', parcol - 1) != -1
116 if closing_paren
117 return indent(parlnum)
118 else
119 return indent(parlnum) + &shiftwidth
120 endif
121 else
122 if closing_paren
123 return parcol - 1
124 else
125 return parcol
126 endif
127 endif
128 endif
129
130 " Examine this line
131 let thisline = getline(a:lnum)
132 let thisindent = indent(a:lnum)
133
134 " If the line starts with 'elif' or 'else', line up with 'if' or 'elif'
135 if thisline =~ '^\s*\(elif\|else\)\>'
136 let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>')
137 if bslnum > 0
138 return indent(bslnum)
139 else
140 return -1
141 endif
142 endif
143
144 " If the line starts with 'except' or 'finally', line up with 'try'
145 " or 'except'
146 if thisline =~ '^\s*\(except\|finally\)\>'
147 let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>')
148 if bslnum > 0
149 return indent(bslnum)
150 else
151 return -1
152 endif
153 endif
154
155 " Examine previous line
156 let plnum = a:lnum - 1
157 let pline = getline(plnum)
158 let sslnum = s:StatementStart(plnum)
159
160 " If the previous line is blank, keep the same indentation
161 if pline =~ '^\s*$'
162 return -1
163 endif
164
165 " If this line is explicitly joined, try to find an indentation that looks
166 " good.
167 if pline =~ '\\$'
168 let compound_statement = '^\s*\(if\|while\|for\s.*\sin\|except\)\s*'
169 let maybe_indent = matchend(getline(sslnum), compound_statement)
170 if maybe_indent != -1
171 return maybe_indent
172 else
173 return indent(sslnum) + &sw * 2
174 endif
175 endif
176
177 " If the previous line ended with a colon, indent relative to
178 " statement start.
179 if pline =~ ':\s*$'
180 return indent(sslnum) + &sw
181 endif
182
183 " If the previous line was a stop-execution statement or a pass
184 if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
185 " See if the user has already dedented
186 if indent(a:lnum) > indent(sslnum) - &sw
187 " If not, recommend one dedent
188 return indent(sslnum) - &sw
189 endif
190 " Otherwise, trust the user
191 return -1
192 endif
193
194 " In all other cases, line up with the start of the previous statement.
195 return indent(sslnum)
196 endfunction
Something went wrong with that request. Please try again.