Skip to content

Commit acb5615

Browse files
committed
Add pymode indentation
1 parent 5d2c857 commit acb5615

File tree

6 files changed

+221
-42
lines changed

6 files changed

+221
-42
lines changed

README.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ Default values: ::
287287
" Autoremove unused whitespaces
288288
let g:pymode_utils_whitespaces = 1
289289

290-
" Set default pymode python indent options
291-
let g:pymode_options_indent = 1
290+
" Enable pymode indentation
291+
let g:pymode_indent = 1
292292

293-
" Set default pymode python other options
294-
let g:pymode_options_other = 1
293+
" Set default pymode python options
294+
let g:pymode_options = 1
295295

296296

297297
Syntax highlight
@@ -478,6 +478,10 @@ Copyright (C) 2012 Kirill Klenov (klen_)
478478
Copyright (c) 2010 Dmitry Vasiliev
479479
http://www.hlabs.spb.ru/vim/python.vim
480480

481+
**PEP8 VIM indentation**
482+
Copyright (c) 2012 Hynek Schlawack <hs@ox.cx>
483+
http://github.com/hynek/vim-python-pep8-indent
484+
481485

482486
License
483487
=======

after/indent/python.vim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if pymode#Default('b:pymode_indent', 1) || !g:pymode_indent
2+
finish
3+
endif
4+
5+
6+
setlocal nolisp
7+
setlocal tabstop=4
8+
setlocal softtabstop=4
9+
setlocal shiftwidth=4
10+
setlocal shiftround
11+
setlocal expandtab
12+
setlocal autoindent
13+
setlocal indentexpr=pymode#indent#Indent(v:lnum)
14+
setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except

autoload/pymode/indent.vim

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

doc/pymode.txt

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ PythonMode. These options should be set in your vimrc.
100100

101101
|'pymode_syntax'| Turns off the custom syntax highlighting
102102

103-
|'pymode_options_indent'| Set default pymode options for
104-
python indentation
103+
|'pymode_indent'| Enable/Disable pymode PEP8 indentation
105104

106-
|'pymode_options_other'| Set default pymode options for
105+
|'pymode_options'| Set default pymode options for
107106
python codding
108107

109108
|'pymode_motion'| Enable pymode motion stuff
@@ -346,24 +345,12 @@ If this option is set to 0 then the custom syntax highlighting will
346345
not be used.
347346

348347
------------------------------------------------------------------------------
349-
*'pymode_options_indent'*
348+
*'pymode_indent'*
350349
Values: 0 or 1.
351350
Default: 1.
352351

353-
If this option is set to 1, pymode will enable the following options for python
354-
buffers: >
352+
If this option is set to 1, pymode will enable python indentation support
355353

356-
setlocal cinwords=if,elif,else,for,while,try,except,finally,def,class
357-
setlocal cindent
358-
setlocal tabstop=4
359-
setlocal softtabstop=4
360-
setlocal shiftwidth=4
361-
setlocal shiftround
362-
setlocal smartindent
363-
setlocal smarttab
364-
setlocal expandtab
365-
setlocal autoindent
366-
<
367354
------------------------------------------------------------------------------
368355
*'pymode_folding'*
369356
Values: 0 or 1.
@@ -372,7 +359,7 @@ Default: 1.
372359
If this option is set to 1, pymode will enable python-folding.
373360

374361
------------------------------------------------------------------------------
375-
*'pymode_options_other'*
362+
*'pymode_options'*
376363
Values: 0 or 1.
377364
Default: 1.
378365

@@ -535,6 +522,10 @@ from `.vimrc` from your projects directories.
535522
Copyright (c) 2010 Dmitry Vasiliev
536523
http://www.hlabs.spb.ru/vim/python.vim
537524

525+
PEP8 VIM indentation
526+
Copyright (c) 2012 Hynek Schlawack <hs@ox.cx>
527+
http://github.com/hynek/vim-python-pep8-indent
528+
538529

539530
==============================================================================
540531
7. License ~

ftplugin/python/pymode.vim

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,8 @@ endif
1515

1616
" Options {{{
1717

18-
" Python indent options
19-
if pymode#Option('options_indent')
20-
setlocal cinwords=if,elif,else,for,while,try,except,finally,def,class
21-
setlocal cindent
22-
setlocal tabstop=4
23-
setlocal softtabstop=4
24-
setlocal shiftwidth=4
25-
setlocal shiftround
26-
setlocal smartindent
27-
setlocal smarttab
28-
setlocal expandtab
29-
setlocal autoindent
30-
endif
31-
3218
" Python other options
33-
if pymode#Option('options_other')
19+
if pymode#Option('options')
3420
setlocal complete+=t
3521
setlocal formatoptions-=t
3622
if v:version > 702 && !&relativenumber

plugin/pymode.vim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,13 @@ call pymode#Default("g:pymode_folding", 1)
281281
" OPTION: g:pymode_syntax -- bool. Enable python-mode syntax for pyfiles.
282282
call pymode#Default("g:pymode_syntax", 1)
283283

284+
" OPTION: g:pymode_indent -- bool. Enable/Disable pymode PEP8 indentation
285+
call pymode#Default("g:pymode_indent", 1)
286+
284287
" OPTION: g:pymode_utils_whitespaces -- bool. Remove unused whitespaces on save
285288
call pymode#Default("g:pymode_utils_whitespaces", 1)
286289

287-
" OPTION: g:pymode_options_indent -- bool. To set indent options.
288-
call pymode#Default("g:pymode_options_indent", 1)
289-
290-
" OPTION: g:pymode_options_other -- bool. To set other options.
291-
call pymode#Default("g:pymode_options_other", 1)
290+
" OPTION: g:pymode_options -- bool. To set some python options.
291+
call pymode#Default("g:pymode_options", 1)
292292

293293
" vim: fdm=marker:fdl=0

0 commit comments

Comments
 (0)