blob: 3dbbe10a68f2197da2e0ef89b611d659abdd2ac6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Python
3" Maintainer: Bram Moolenaar <Bram@vim.org>
4" Original Author: David Bustos <bustos@caltech.edu>
5" Last Change: 2003 Sep 08
6
7" Only load this indent file when no other was loaded.
8if exists("b:did_indent")
9 finish
10endif
11let b:did_indent = 1
12
13" Some preliminary settings
14setlocal nolisp " Make sure lisp indenting doesn't supersede us
15setlocal autoindent " indentexpr isn't much help otherwise
16
17setlocal indentexpr=GetPythonIndent(v:lnum)
18setlocal indentkeys+=<:>,=elif,=except
19
20" Only define the function once.
21if exists("*GetPythonIndent")
22 finish
23endif
24
25let s:maxoff = 50 " maximum number of lines to look backwards for ()
26
27function GetPythonIndent(lnum)
28 " If this line is explicitly joined: If the previous line was also joined,
29 " line it up with that one, otherwise add two 'shiftwidth'
30 if getline(a:lnum - 1) =~ '\\$'
31 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
32 return indent(a:lnum - 1)
33 endif
34 return indent(a:lnum - 1) + (&sw * 2)
35 endif
36
37 " If the start of the line is in a string don't change the indent.
38 if has('syntax_items')
39 \ && synIDattr(synID(a:lnum, 1, 1), "name") == "pythonString"
40 return -1
41 endif
42
43 " Search backwards for the previous non-empty line.
44 let plnum = prevnonblank(v:lnum - 1)
45
46 if plnum == 0
47 " This is the first non-empty line, use zero indent.
48 return 0
49 endif
50
51 " If the previous line is inside parenthesis, use the indent of the starting
52 " line.
53 " Trick: use the non-existing "dummy" variable to break out of the loop when
54 " going too far back.
55 call cursor(plnum, 1)
56 let parlnum = searchpair('(', '', ')', 'nbW',
57 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
58 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
59 \ . " =~ 'python\\(Comment\\|String\\)'")
60 if parlnum > 0
61 let plindent = indent(parlnum)
62 let plnumstart = parlnum
63 else
64 let plindent = indent(plnum)
65 let plnumstart = plnum
66 endif
67
68
69 " When inside parenthesis: If at the first line below the parenthesis add
70 " two 'shiftwidth', otherwise same as previous line.
71 " i = (a
72 " + b
73 " + c)
74 call cursor(a:lnum, 1)
75 let p = searchpair('(', '', ')', 'bW',
76 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
77 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
78 \ . " =~ 'python\\(Comment\\|String\\)'")
79 if p > 0
80 if p == plnum
81 " When the start is inside parenthesis, only indent one 'shiftwidth'.
82 let pp = searchpair('(', '', ')', 'bW',
83 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
84 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
85 \ . " =~ 'python\\(Comment\\|String\\)'")
86 if pp > 0
87 return indent(plnum) + &sw
88 endif
89 return indent(plnum) + (&sw * 2)
90 endif
91 if plnumstart == p
92 return indent(plnum)
93 endif
94 return plindent
95 endif
96
97
98 " Get the line and remove a trailing comment.
99 " Use syntax highlighting attributes when possible.
100 let pline = getline(plnum)
101 let pline_len = strlen(pline)
102 let col = 0
103 while col < pline_len
104 if pline[col] == '#' && (!has('syntax_items')
105 \ || synIDattr(synID(plnum, col + 1, 1), "name") == "pythonComment")
106 let pline = strpart(pline, 0, col)
107 break
108 endif
109 let col = col + 1
110 endwhile
111
112 " If the previous line ended with a colon, indent this line
113 if pline =~ ':\s*$'
114 return plindent + &sw
115 endif
116
117 " If the previous line was a stop-execution statement...
118 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\)\>'
119 " See if the user has already dedented
120 if indent(a:lnum) > indent(plnum) - &sw
121 " If not, recommend one dedent
122 return indent(plnum) - &sw
123 endif
124 " Otherwise, trust the user
125 return -1
126 endif
127
128 " If the current line begins with a keyword that lines up with "try"
129 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
130 let lnum = a:lnum - 1
131 while lnum >= 1
132 echomsg 'got here'
133 if getline(lnum) =~ '^\s*\(try\|except\)\>'
134 let ind = indent(lnum)
135 echomsg 'got here, indent is ' . ind
136 if ind >= indent(a:lnum)
137 return -1 " indent is already less than this
138 endif
139 return ind " line up with previous try or except
140 endif
141 let lnum = lnum - 1
142 endwhile
143 echomsg 'got to the end'
144 return -1 " no matching "try"!
145 endif
146
147 " If the current line begins with a header keyword, dedent
148 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
149
150 " Unless the previous line was a one-liner
151 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
152 return plindent
153 endif
154
155 " Or the user has already dedented
156 if indent(a:lnum) <= plindent - &sw
157 return -1
158 endif
159
160 return plindent - &sw
161 endif
162
163 " When after a () construct we probably want to go back to the start line.
164 " a = (b
165 " + c)
166 " here
167 if parlnum > 0
168 return plindent
169 endif
170
171 return -1
172
173endfunction
174
175" vim:sw=2