blob: e0e8e71f1f36a00df8f7f49c7aaeaa090e104456 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002" Language: Python
3" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar071d4272004-06-13 20:20:40 +00004" Original Author: David Bustos <bustos@caltech.edu>
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005" Last Change: 2005 Feb 08
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
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')
Bram Moolenaared203462004-06-16 11:19:22 +000039 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 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')"
Bram Moolenaared203462004-06-16 11:19:22 +000059 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 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')"
Bram Moolenaared203462004-06-16 11:19:22 +000078 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 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')"
Bram Moolenaared203462004-06-16 11:19:22 +000085 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 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)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000102 if has('syntax_items')
103 " If the last character in the line is a comment, do a binary search for
104 " the start of the comment. synID() is slow, a linear search would take
105 " too long on a long line.
106 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "Comment$"
107 let min = 1
108 let max = pline_len
109 while min < max
110 let col = (min + max) / 2
111 if synIDattr(synID(plnum, col, 1), "name") =~ "Comment$"
112 let max = col
113 else
114 let min = col + 1
115 endif
116 endwhile
117 echomsg min
118 let pline = strpart(pline, 0, min - 1)
119 echomsg pline
120 sleep 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000122 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 Moolenaar071d4272004-06-13 20:20:40 +0000132
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
153 echomsg 'got here'
154 if getline(lnum) =~ '^\s*\(try\|except\)\>'
155 let ind = indent(lnum)
156 echomsg 'got here, indent is ' . ind
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 echomsg 'got to the end'
165 return -1 " no matching "try"!
166 endif
167
168 " If the current line begins with a header keyword, dedent
169 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
170
171 " Unless the previous line was a one-liner
172 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
173 return plindent
174 endif
175
176 " Or the user has already dedented
177 if indent(a:lnum) <= plindent - &sw
178 return -1
179 endif
180
181 return plindent - &sw
182 endif
183
184 " When after a () construct we probably want to go back to the start line.
185 " a = (b
186 " + c)
187 " here
188 if parlnum > 0
189 return plindent
190 endif
191
192 return -1
193
194endfunction
195
196" vim:sw=2