From 3adcf542289a0883924ae9b9be8b898c36702c95 Mon Sep 17 00:00:00 2001 From: Sam Chudnick Date: Sun, 11 Jun 2023 07:54:59 -0400 Subject: Add some Vim plugins --- .vim/plugin/python_autopep8.vim | 174 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 .vim/plugin/python_autopep8.vim (limited to '.vim/plugin') 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 @@ +"========================================================= +" File: python_autopep8.vim +" Author: tell-k +" Last Change: 3-Jun-2018. +" Version: 1.2.0 +" WebPage: https://github.com/tell-k/vim-autopep8 +" License: MIT Licence +"========================================================== +" see also README.rst + +" Only do this when not done yet for this buffer +if exists("b:loaded_autopep8_ftplugin") + finish +endif +let b:loaded_autopep8_ftplugin=1 +let b:autopep8_current_cursor = [0, 1, 1, 0] + +if !exists("*Autopep8(...)") + function Autopep8(...) range + + let l:args = get(a:, 1, '') + + if exists("g:autopep8_cmd") + let autopep8_cmd=g:autopep8_cmd + else + let autopep8_cmd="autopep8" + endif + + if !executable(autopep8_cmd) + echoerr "File " . autopep8_cmd . " not found. Please install it first." + return + endif + + if exists("g:autopep8_ignore") + let autopep8_ignores=" --ignore=".g:autopep8_ignore + else + let autopep8_ignores="" + endif + + if exists("g:autopep8_select") + let autopep8_selects=" --select=".g:autopep8_select + else + let autopep8_selects="" + endif + + if exists("g:autopep8_pep8_passes") + let autopep8_pep8_passes=" --pep8-passes=".g:autopep8_pep8_passes + else + let autopep8_pep8_passes="" + endif + + if exists("g:autopep8_max_line_length") + let autopep8_max_line_length=" --max-line-length=".g:autopep8_max_line_length + else + let autopep8_max_line_length="" + endif + + let autopep8_range = "" + let current_cursor = b:autopep8_current_cursor + if l:args != "" + let autopep8_range = " ".l:args + let current_cursor = getpos(".") + elseif a:firstline == a:lastline + let autopep8_range = "" + let current_cursor = [0, a:firstline, 1, 0] + elseif a:firstline != 1 || a:lastline != line('$') + let autopep8_range = " --range ".a:firstline." ".a:lastline + let current_cursor = [0, a:firstline, 1, 0] + endif + + if exists("g:autopep8_aggressive") + if g:autopep8_aggressive == 2 + let autopep8_aggressive=" --aggressive --aggressive " + else + let autopep8_aggressive=" --aggressive " + endif + else + let autopep8_aggressive="" + endif + + if exists("g:autopep8_indent_size") + let autopep8_indent_size=" --indent-size=".g:autopep8_indent_size + else + let autopep8_indent_size="" + endif + + if exists("g:autopep8_diff_type") && g:autopep8_diff_type == "vertical" + let autopep8_diff_type="vertical" + else + let autopep8_diff_type="horizontal" + endif + + let execmdline=autopep8_cmd.autopep8_pep8_passes.autopep8_selects.autopep8_ignores.autopep8_max_line_length.autopep8_aggressive.autopep8_indent_size.autopep8_range + + " current cursor + " show diff if not explicitly disabled + if !exists("g:autopep8_disable_show_diff") + let tmpfile = tempname() + try + " write buffer contents to tmpfile because autopep8 --diff + " does not work with standard input + silent execute "0,$w! " . tmpfile + let diff_cmd = execmdline . " --diff \"" . tmpfile . "\"" + let diff_output = system(diff_cmd) + finally + " file close + if filewritable(tmpfile) + call delete(tmpfile) + endif + endtry + endif + " execute autopep8 passing buffer contents as standard input + silent execute "0,$!" . execmdline . " -" + " restore cursor + call setpos('.', current_cursor) + + " show diff + if !exists("g:autopep8_disable_show_diff") + if autopep8_diff_type == "vertical" + vertical botright new autopep8 + else + botright new autopep8 + endif + setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap + silent execute ':put =diff_output' + setlocal nomodifiable + setlocal nu + setlocal filetype=diff + endif + + hi Green ctermfg=green + echohl Green + echon "Fixed with autopep8 this file." + echohl + + endfunction +endif + +if !exists("no_plugin_maps") && !exists("no_autopep8_maps") + if !hasmapto('Autopep8(') + command! -range=% -nargs=? -bar Autopep8 let b:autopep8_current_cursor = getpos(".") | ,call Autopep8() + endif +endif + + +" This function saves the current window state, and executes Autopep8() with +" the user's existing options. After Autopep8 call, the initial window +" settings are restored. Undo recording is also paused during Autopep8 call +function! s:autopep8_on_save() + if get(g:, "autopep8_on_save", 0) + + " Save cursor position and many other things. + let l:curw = winsaveview() + + " stop undo recording + try | silent undojoin | catch | endtry + + call Autopep8() + + " close the fixes window automatically + silent execute ':q' + + " Restore our cursor/windows positions. + call winrestview(l:curw) + + endif +endfunction + +" During every save, also reformat the file with respect to the existing +" autopep8 settings. +augroup vim-python-autopep8 + autocmd! + autocmd BufWritePre *.py call s:autopep8_on_save() +augroup END -- cgit v1.2.3