1" Vim filetype plugin file 2" Language: python 3" Maintainer: Tom Picton <[email protected]> 4" Previous Maintainer: James Sully <[email protected]> 5" Previous Maintainer: Johannes Zellner <[email protected]> 6" Last Change: Mon, 5 October 2020 7" https://github.com/tpict/vim-ftplugin-python 8 9if exists("b:did_ftplugin") | finish | endif 10let b:did_ftplugin = 1 11let s:keepcpo= &cpo 12set cpo&vim 13 14setlocal cinkeys-=0# 15setlocal indentkeys-=0# 16setlocal include=^\\s*\\(from\\\|import\\) 17setlocal define=^\\s*\\(def\\\|class\\) 18 19" For imports with leading .., append / and replace additional .s with ../ 20let b:grandparent_match = '^\(.\.\)\(\.*\)' 21let b:grandparent_sub = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))' 22 23" For imports with a single leading ., replace it with ./ 24let b:parent_match = '^\.\(\.\)\@!' 25let b:parent_sub = './' 26 27" Replace any . sandwiched between word characters with / 28let b:child_match = '\(\w\)\.\(\w\)' 29let b:child_sub = '\1/\2' 30 31setlocal includeexpr=substitute(substitute(substitute( 32 \v:fname, 33 \b:grandparent_match,b:grandparent_sub,''), 34 \b:parent_match,b:parent_sub,''), 35 \b:child_match,b:child_sub,'g') 36 37setlocal suffixesadd=.py 38setlocal comments=b:#,fb:- 39setlocal commentstring=#\ %s 40 41if has('python3') 42 setlocal omnifunc=python3complete#Complete 43elseif has('python') 44 setlocal omnifunc=pythoncomplete#Complete 45endif 46 47set wildignore+=*.pyc 48 49let b:next_toplevel='\v%$\|^(class\|def\|async def)>' 50let b:prev_toplevel='\v^(class\|def\|async def)>' 51let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)' 52let b:prev_endtoplevel='\v\S.*\n+(def\|class)' 53let b:next='\v%$\|^\s*(class\|def\|async def)>' 54let b:prev='\v^\s*(class\|def\|async def)>' 55let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)' 56let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)' 57 58if !exists('g:no_plugin_maps') && !exists('g:no_python_maps') 59 execute "nnoremap <silent> <buffer> ]] :call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>" 60 execute "nnoremap <silent> <buffer> [[ :call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>" 61 execute "nnoremap <silent> <buffer> ][ :call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>" 62 execute "nnoremap <silent> <buffer> [] :call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>" 63 execute "nnoremap <silent> <buffer> ]m :call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>" 64 execute "nnoremap <silent> <buffer> [m :call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>" 65 execute "nnoremap <silent> <buffer> ]M :call <SID>Python_jump('n', '". b:next_end."', 'W', v:count1, 0)<cr>" 66 execute "nnoremap <silent> <buffer> [M :call <SID>Python_jump('n', '". b:prev_end."', 'Wb', v:count1, 0)<cr>" 67 68 execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W', v:count1)<cr>" 69 execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb', v:count1)<cr>" 70 execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>" 71 execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>" 72 execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W', v:count1)<cr>" 73 execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb', v:count1)<cr>" 74 execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', v:count1, 0)<cr>" 75 execute "onoremap <silent> <buffer> [M :call <SID>Python_jump('o', '". b:prev_end."', 'Wb', v:count1, 0)<cr>" 76 77 execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W', v:count1)<cr>" 78 execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb', v:count1)<cr>" 79 execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>" 80 execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>" 81 execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W', v:count1)<cr>" 82 execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb', v:count1)<cr>" 83 execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', v:count1, 0)<cr>" 84 execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', v:count1, 0)<cr>" 85endif 86 87if !exists('*<SID>Python_jump') 88 fun! <SID>Python_jump(mode, motion, flags, count, ...) range 89 let l:startofline = (a:0 >= 1) ? a:1 : 1 90 91 if a:mode == 'x' 92 normal! gv 93 endif 94 95 if l:startofline == 1 96 normal! 0 97 endif 98 99 let cnt = a:count 100 mark ' 101 while cnt > 0 102 call search(a:motion, a:flags) 103 let cnt = cnt - 1 104 endwhile 105 106 if l:startofline == 1 107 normal! ^ 108 endif 109 endfun 110endif 111 112if has("browsefilter") && !exists("b:browsefilter") 113 let b:browsefilter = "Python Files (*.py)\t*.py\n" . 114 \ "All Files (*.*)\t*.*\n" 115endif 116 117if !exists("g:python_recommended_style") || g:python_recommended_style != 0 118 " As suggested by PEP8. 119 setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4 120endif 121 122" Use pydoc for keywordprg. 123" Unix users preferentially get pydoc3, then pydoc2. 124" Windows doesn't have a standalone pydoc executable in $PATH by default, nor 125" does it have separate python2/3 executables, so Windows users just get 126" whichever version corresponds to their installed Python version. 127if executable('python3') 128 setlocal keywordprg=python3\ -m\ pydoc 129elseif executable('python') 130 setlocal keywordprg=python\ -m\ pydoc 131endif 132 133" Script for filetype switching to undo the local stuff we may have changed 134let b:undo_ftplugin = 'setlocal cinkeys<' 135 \ . '|setlocal comments<' 136 \ . '|setlocal commentstring<' 137 \ . '|setlocal expandtab<' 138 \ . '|setlocal include<' 139 \ . '|setlocal includeexpr<' 140 \ . '|setlocal indentkeys<' 141 \ . '|setlocal keywordprg<' 142 \ . '|setlocal omnifunc<' 143 \ . '|setlocal shiftwidth<' 144 \ . '|setlocal softtabstop<' 145 \ . '|setlocal suffixesadd<' 146 \ . '|setlocal tabstop<' 147 \ . '|silent! nunmap <buffer> [M' 148 \ . '|silent! nunmap <buffer> [[' 149 \ . '|silent! nunmap <buffer> []' 150 \ . '|silent! nunmap <buffer> [m' 151 \ . '|silent! nunmap <buffer> ]M' 152 \ . '|silent! nunmap <buffer> ][' 153 \ . '|silent! nunmap <buffer> ]]' 154 \ . '|silent! nunmap <buffer> ]m' 155 \ . '|silent! ounmap <buffer> [M' 156 \ . '|silent! ounmap <buffer> [[' 157 \ . '|silent! ounmap <buffer> []' 158 \ . '|silent! ounmap <buffer> [m' 159 \ . '|silent! ounmap <buffer> ]M' 160 \ . '|silent! ounmap <buffer> ][' 161 \ . '|silent! ounmap <buffer> ]]' 162 \ . '|silent! ounmap <buffer> ]m' 163 \ . '|silent! xunmap <buffer> [M' 164 \ . '|silent! xunmap <buffer> [[' 165 \ . '|silent! xunmap <buffer> []' 166 \ . '|silent! xunmap <buffer> [m' 167 \ . '|silent! xunmap <buffer> ]M' 168 \ . '|silent! xunmap <buffer> ][' 169 \ . '|silent! xunmap <buffer> ]]' 170 \ . '|silent! xunmap <buffer> ]m' 171 \ . '|unlet! b:browsefilter' 172 \ . '|unlet! b:child_match' 173 \ . '|unlet! b:child_sub' 174 \ . '|unlet! b:grandparent_match' 175 \ . '|unlet! b:grandparent_sub' 176 \ . '|unlet! b:next' 177 \ . '|unlet! b:next_end' 178 \ . '|unlet! b:next_endtoplevel' 179 \ . '|unlet! b:next_toplevel' 180 \ . '|unlet! b:parent_match' 181 \ . '|unlet! b:parent_sub' 182 \ . '|unlet! b:prev' 183 \ . '|unlet! b:prev_end' 184 \ . '|unlet! b:prev_endtoplevel' 185 \ . '|unlet! b:prev_toplevel' 186 \ . '|unlet! b:undo_ftplugin' 187 188let &cpo = s:keepcpo 189unlet s:keepcpo 190