blob: 38ef1d259e338ec88c6e2ee4f6f5d7214de68477 [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 Moolenaar05159a02005-02-26 23:04:13 +00005" Last Change: 2005 Feb 24
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
Bram Moolenaar05159a02005-02-26 23:04:13 +000025" Come here when loading the script the first time.
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027let s:maxoff = 50 " maximum number of lines to look backwards for ()
28
29function GetPythonIndent(lnum)
Bram Moolenaar05159a02005-02-26 23:04:13 +000030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 " 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
Bram Moolenaar05159a02005-02-26 23:04:13 +000037 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (&sw * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 endif
39
40 " If the start of the line is in a string don't change the indent.
41 if has('syntax_items')
Bram Moolenaared203462004-06-16 11:19:22 +000042 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 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')"
Bram Moolenaared203462004-06-16 11:19:22 +000062 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 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')"
Bram Moolenaared203462004-06-16 11:19:22 +000081 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 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')"
Bram Moolenaared203462004-06-16 11:19:22 +000088 \ . " =~ '\\(Comment\\|String\\)$'")
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 if pp > 0
Bram Moolenaar05159a02005-02-26 23:04:13 +000090 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : &sw)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000092 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (&sw * 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 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)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000105 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 echomsg min
121 let pline = strpart(pline, 0, min - 1)
122 echomsg pline
123 sleep 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000125 else
126 let col = 0
127 while col < pline_len
128 if pline[col] == '#'
129 let pline = strpart(pline, 0, col)
130 break
131 endif
132 let col = col + 1
133 endwhile
134 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 " If the previous line ended with a colon, indent this line
137 if pline =~ ':\s*$'
138 return plindent + &sw
139 endif
140
141 " If the previous line was a stop-execution statement...
142 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\)\>'
143 " See if the user has already dedented
144 if indent(a:lnum) > indent(plnum) - &sw
145 " If not, recommend one dedent
146 return indent(plnum) - &sw
147 endif
148 " Otherwise, trust the user
149 return -1
150 endif
151
152 " If the current line begins with a keyword that lines up with "try"
153 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
154 let lnum = a:lnum - 1
155 while lnum >= 1
156 echomsg 'got here'
157 if getline(lnum) =~ '^\s*\(try\|except\)\>'
158 let ind = indent(lnum)
159 echomsg 'got here, indent is ' . ind
160 if ind >= indent(a:lnum)
161 return -1 " indent is already less than this
162 endif
163 return ind " line up with previous try or except
164 endif
165 let lnum = lnum - 1
166 endwhile
167 echomsg 'got to the end'
168 return -1 " no matching "try"!
169 endif
170
171 " If the current line begins with a header keyword, dedent
172 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
173
174 " Unless the previous line was a one-liner
175 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
176 return plindent
177 endif
178
179 " Or the user has already dedented
180 if indent(a:lnum) <= plindent - &sw
181 return -1
182 endif
183
184 return plindent - &sw
185 endif
186
187 " When after a () construct we probably want to go back to the start line.
188 " a = (b
189 " + c)
190 " here
191 if parlnum > 0
192 return plindent
193 endif
194
195 return -1
196
197endfunction
198
199" vim:sw=2