python.vim 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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: Sun 17 Mar 2019
  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. " For imports with leading .., append / and replace additional .s with ../
  16. let b:grandparent_match = '^\(.\.\)\(\.*\)'
  17. let b:grandparent_sub = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))'
  18. " For imports with a single leading ., replace it with ./
  19. let b:parent_match = '^\.\(\.\)\@!'
  20. let b:parent_sub = './'
  21. " Replace any . sandwiched between word characters with /
  22. let b:child_match = '\(\w\)\.\(\w\)'
  23. let b:child_sub = '\1/\2'
  24. setlocal includeexpr=substitute(substitute(substitute(
  25. \v:fname,
  26. \b:grandparent_match,b:grandparent_sub,''),
  27. \b:parent_match,b:parent_sub,''),
  28. \b:child_match,b:child_sub,'g')
  29. setlocal suffixesadd=.py
  30. setlocal comments=b:#,fb:-
  31. setlocal commentstring=#\ %s
  32. if has('python3')
  33. setlocal omnifunc=python3complete#Complete
  34. elseif has('python')
  35. setlocal omnifunc=pythoncomplete#Complete
  36. endif
  37. set wildignore+=*.pyc
  38. let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
  39. let b:prev_toplevel='\v^(class\|def\|async def)>'
  40. let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
  41. let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
  42. let b:next='\v%$\|^\s*(class\|def\|async def)>'
  43. let b:prev='\v^\s*(class\|def\|async def)>'
  44. let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)'
  45. let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)'
  46. if !exists('g:no_plugin_maps') && !exists('g:no_python_maps')
  47. execute "nnoremap <silent> <buffer> ]] :call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>"
  48. execute "nnoremap <silent> <buffer> [[ :call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
  49. execute "nnoremap <silent> <buffer> ][ :call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
  50. execute "nnoremap <silent> <buffer> [] :call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
  51. execute "nnoremap <silent> <buffer> ]m :call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>"
  52. execute "nnoremap <silent> <buffer> [m :call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>"
  53. execute "nnoremap <silent> <buffer> ]M :call <SID>Python_jump('n', '". b:next_end."', 'W', v:count1, 0)<cr>"
  54. execute "nnoremap <silent> <buffer> [M :call <SID>Python_jump('n', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
  55. execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W', v:count1)<cr>"
  56. execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
  57. execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
  58. execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
  59. execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W', v:count1)<cr>"
  60. execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb', v:count1)<cr>"
  61. execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', v:count1, 0)<cr>"
  62. execute "onoremap <silent> <buffer> [M :call <SID>Python_jump('o', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
  63. execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W', v:count1)<cr>"
  64. execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
  65. execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
  66. execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
  67. execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W', v:count1)<cr>"
  68. execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb', v:count1)<cr>"
  69. execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', v:count1, 0)<cr>"
  70. execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
  71. endif
  72. if !exists('*<SID>Python_jump')
  73. fun! <SID>Python_jump(mode, motion, flags, count, ...) range
  74. let l:startofline = (a:0 >= 1) ? a:1 : 1
  75. if a:mode == 'x'
  76. normal! gv
  77. endif
  78. if l:startofline == 1
  79. normal! 0
  80. endif
  81. let cnt = a:count
  82. mark '
  83. while cnt > 0
  84. call search(a:motion, a:flags)
  85. let cnt = cnt - 1
  86. endwhile
  87. if l:startofline == 1
  88. normal! ^
  89. endif
  90. endfun
  91. endif
  92. if has("browsefilter") && !exists("b:browsefilter")
  93. let b:browsefilter = "Python Files (*.py)\t*.py\n" .
  94. \ "All Files (*.*)\t*.*\n"
  95. endif
  96. if !exists("g:python_recommended_style") || g:python_recommended_style != 0
  97. " As suggested by PEP8.
  98. setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
  99. endif
  100. " First time: try finding "pydoc".
  101. if !exists('g:pydoc_executable')
  102. if executable('pydoc')
  103. let g:pydoc_executable = 1
  104. else
  105. let g:pydoc_executable = 0
  106. endif
  107. endif
  108. " Windows-specific pydoc setup
  109. if has('win32') || has('win64')
  110. if executable('python')
  111. " available as Tools\scripts\pydoc.py
  112. let g:pydoc_executable = 1
  113. else
  114. let g:pydoc_executable = 0
  115. endif
  116. endif
  117. " If "pydoc" was found use it for keywordprg.
  118. if g:pydoc_executable
  119. if has('win32') || has('win64')
  120. setlocal keywordprg=python\ -m\ pydoc\
  121. else
  122. setlocal keywordprg=pydoc
  123. endif
  124. endif
  125. " Script for filetype switching to undo the local stuff we may have changed
  126. let b:undo_ftplugin = 'setlocal cinkeys<'
  127. \ . '|setlocal comments<'
  128. \ . '|setlocal commentstring<'
  129. \ . '|setlocal expandtab<'
  130. \ . '|setlocal include<'
  131. \ . '|setlocal includeexpr<'
  132. \ . '|setlocal indentkeys<'
  133. \ . '|setlocal keywordprg<'
  134. \ . '|setlocal omnifunc<'
  135. \ . '|setlocal shiftwidth<'
  136. \ . '|setlocal softtabstop<'
  137. \ . '|setlocal suffixesadd<'
  138. \ . '|setlocal tabstop<'
  139. \ . '|silent! nunmap <buffer> [M'
  140. \ . '|silent! nunmap <buffer> [['
  141. \ . '|silent! nunmap <buffer> []'
  142. \ . '|silent! nunmap <buffer> [m'
  143. \ . '|silent! nunmap <buffer> ]M'
  144. \ . '|silent! nunmap <buffer> ]['
  145. \ . '|silent! nunmap <buffer> ]]'
  146. \ . '|silent! nunmap <buffer> ]m'
  147. \ . '|silent! ounmap <buffer> [M'
  148. \ . '|silent! ounmap <buffer> [['
  149. \ . '|silent! ounmap <buffer> []'
  150. \ . '|silent! ounmap <buffer> [m'
  151. \ . '|silent! ounmap <buffer> ]M'
  152. \ . '|silent! ounmap <buffer> ]['
  153. \ . '|silent! ounmap <buffer> ]]'
  154. \ . '|silent! ounmap <buffer> ]m'
  155. \ . '|silent! xunmap <buffer> [M'
  156. \ . '|silent! xunmap <buffer> [['
  157. \ . '|silent! xunmap <buffer> []'
  158. \ . '|silent! xunmap <buffer> [m'
  159. \ . '|silent! xunmap <buffer> ]M'
  160. \ . '|silent! xunmap <buffer> ]['
  161. \ . '|silent! xunmap <buffer> ]]'
  162. \ . '|silent! xunmap <buffer> ]m'
  163. \ . '|unlet! b:browsefilter'
  164. \ . '|unlet! b:child_match'
  165. \ . '|unlet! b:child_sub'
  166. \ . '|unlet! b:grandparent_match'
  167. \ . '|unlet! b:grandparent_sub'
  168. \ . '|unlet! b:next'
  169. \ . '|unlet! b:next_end'
  170. \ . '|unlet! b:next_endtoplevel'
  171. \ . '|unlet! b:next_toplevel'
  172. \ . '|unlet! b:parent_match'
  173. \ . '|unlet! b:parent_sub'
  174. \ . '|unlet! b:prev'
  175. \ . '|unlet! b:prev_end'
  176. \ . '|unlet! b:prev_endtoplevel'
  177. \ . '|unlet! b:prev_toplevel'
  178. \ . '|unlet! b:undo_ftplugin'
  179. let &cpo = s:keepcpo
  180. unlet s:keepcpo