python.vim 7.8 KB

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