summaryrefslogtreecommitdiff
path: root/.vim/plugin
diff options
context:
space:
mode:
Diffstat (limited to '.vim/plugin')
-rw-r--r--.vim/plugin/python_autopep8.vim174
1 files changed, 174 insertions, 0 deletions
diff --git a/.vim/plugin/python_autopep8.vim b/.vim/plugin/python_autopep8.vim
new file mode 100644
index 0000000..b7176e1
--- /dev/null
+++ b/.vim/plugin/python_autopep8.vim
@@ -0,0 +1,174 @@
1"=========================================================
2" File: python_autopep8.vim
3" Author: tell-k <ffk2005[at]gmail.com>
4" Last Change: 3-Jun-2018.
5" Version: 1.2.0
6" WebPage: https://github.com/tell-k/vim-autopep8
7" License: MIT Licence
8"==========================================================
9" see also README.rst
10
11" Only do this when not done yet for this buffer
12if exists("b:loaded_autopep8_ftplugin")
13 finish
14endif
15let b:loaded_autopep8_ftplugin=1
16let b:autopep8_current_cursor = [0, 1, 1, 0]
17
18if !exists("*Autopep8(...)")
19 function Autopep8(...) range
20
21 let l:args = get(a:, 1, '')
22
23 if exists("g:autopep8_cmd")
24 let autopep8_cmd=g:autopep8_cmd
25 else
26 let autopep8_cmd="autopep8"
27 endif
28
29 if !executable(autopep8_cmd)
30 echoerr "File " . autopep8_cmd . " not found. Please install it first."
31 return
32 endif
33
34 if exists("g:autopep8_ignore")
35 let autopep8_ignores=" --ignore=".g:autopep8_ignore
36 else
37 let autopep8_ignores=""
38 endif
39
40 if exists("g:autopep8_select")
41 let autopep8_selects=" --select=".g:autopep8_select
42 else
43 let autopep8_selects=""
44 endif
45
46 if exists("g:autopep8_pep8_passes")
47 let autopep8_pep8_passes=" --pep8-passes=".g:autopep8_pep8_passes
48 else
49 let autopep8_pep8_passes=""
50 endif
51
52 if exists("g:autopep8_max_line_length")
53 let autopep8_max_line_length=" --max-line-length=".g:autopep8_max_line_length
54 else
55 let autopep8_max_line_length=""
56 endif
57
58 let autopep8_range = ""
59 let current_cursor = b:autopep8_current_cursor
60 if l:args != ""
61 let autopep8_range = " ".l:args
62 let current_cursor = getpos(".")
63 elseif a:firstline == a:lastline
64 let autopep8_range = ""
65 let current_cursor = [0, a:firstline, 1, 0]
66 elseif a:firstline != 1 || a:lastline != line('$')
67 let autopep8_range = " --range ".a:firstline." ".a:lastline
68 let current_cursor = [0, a:firstline, 1, 0]
69 endif
70
71 if exists("g:autopep8_aggressive")
72 if g:autopep8_aggressive == 2
73 let autopep8_aggressive=" --aggressive --aggressive "
74 else
75 let autopep8_aggressive=" --aggressive "
76 endif
77 else
78 let autopep8_aggressive=""
79 endif
80
81 if exists("g:autopep8_indent_size")
82 let autopep8_indent_size=" --indent-size=".g:autopep8_indent_size
83 else
84 let autopep8_indent_size=""
85 endif
86
87 if exists("g:autopep8_diff_type") && g:autopep8_diff_type == "vertical"
88 let autopep8_diff_type="vertical"
89 else
90 let autopep8_diff_type="horizontal"
91 endif
92
93 let execmdline=autopep8_cmd.autopep8_pep8_passes.autopep8_selects.autopep8_ignores.autopep8_max_line_length.autopep8_aggressive.autopep8_indent_size.autopep8_range
94
95 " current cursor
96 " show diff if not explicitly disabled
97 if !exists("g:autopep8_disable_show_diff")
98 let tmpfile = tempname()
99 try
100 " write buffer contents to tmpfile because autopep8 --diff
101 " does not work with standard input
102 silent execute "0,$w! " . tmpfile
103 let diff_cmd = execmdline . " --diff \"" . tmpfile . "\""
104 let diff_output = system(diff_cmd)
105 finally
106 " file close
107 if filewritable(tmpfile)
108 call delete(tmpfile)
109 endif
110 endtry
111 endif
112 " execute autopep8 passing buffer contents as standard input
113 silent execute "0,$!" . execmdline . " -"
114 " restore cursor
115 call setpos('.', current_cursor)
116
117 " show diff
118 if !exists("g:autopep8_disable_show_diff")
119 if autopep8_diff_type == "vertical"
120 vertical botright new autopep8
121 else
122 botright new autopep8
123 endif
124 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
125 silent execute ':put =diff_output'
126 setlocal nomodifiable
127 setlocal nu
128 setlocal filetype=diff
129 endif
130
131 hi Green ctermfg=green
132 echohl Green
133 echon "Fixed with autopep8 this file."
134 echohl
135
136 endfunction
137endif
138
139if !exists("no_plugin_maps") && !exists("no_autopep8_maps")
140 if !hasmapto('Autopep8(')
141 command! -range=% -nargs=? -bar Autopep8 let b:autopep8_current_cursor = getpos(".") | <line1>,<line2>call Autopep8(<f-args>)
142 endif
143endif
144
145
146" This function saves the current window state, and executes Autopep8() with
147" the user's existing options. After Autopep8 call, the initial window
148" settings are restored. Undo recording is also paused during Autopep8 call
149function! s:autopep8_on_save()
150 if get(g:, "autopep8_on_save", 0)
151
152 " Save cursor position and many other things.
153 let l:curw = winsaveview()
154
155 " stop undo recording
156 try | silent undojoin | catch | endtry
157
158 call Autopep8()
159
160 " close the fixes window automatically
161 silent execute ':q'
162
163 " Restore our cursor/windows positions.
164 call winrestview(l:curw)
165
166 endif
167endfunction
168
169" During every save, also reformat the file with respect to the existing
170" autopep8 settings.
171augroup vim-python-autopep8
172 autocmd!
173 autocmd BufWritePre *.py call s:autopep8_on_save()
174augroup END