xref: /vim-8.2.3635/runtime/ftplugin/python.vim (revision b691de05)
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:	Sun, 15 April 2018
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\\)
17
18" For imports with leading .., append / and replace additional .s with ../
19let b:grandparent_match = '^\(.\.\)\(\.*\)'
20let b:grandparent_sub = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))'
21
22" For imports with a single leading ., replace it with ./
23let b:parent_match = '^\.\(\.\)\@!'
24let b:parent_sub = './'
25
26" Replace any . sandwiched between word characters with /
27let b:child_match = '\(\w\)\.\(\w\)'
28let b:child_sub = '\1/\2'
29
30setlocal includeexpr=substitute(substitute(substitute(
31      \v:fname,
32      \b:grandparent_match,b:grandparent_sub,''),
33      \b:parent_match,b:parent_sub,''),
34      \b:child_match,b:child_sub,'g')
35
36setlocal suffixesadd=.py
37setlocal comments=b:#,fb:-
38setlocal commentstring=#\ %s
39
40setlocal omnifunc=pythoncomplete#Complete
41if has('python3')
42       setlocal omnifunc=python3complete#Complete
43endif
44
45set wildignore+=*.pyc
46
47let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
48let b:prev_toplevel='\v^(class\|def\|async def)>'
49let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
50let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
51let b:next='\v%$\|^\s*(class\|def\|async def)>'
52let b:prev='\v^\s*(class\|def\|async def)>'
53let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)'
54let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)'
55
56execute "nnoremap <silent> <buffer> ]] :call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>"
57execute "nnoremap <silent> <buffer> [[ :call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
58execute "nnoremap <silent> <buffer> ][ :call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', 0, v:count1)<cr>"
59execute "nnoremap <silent> <buffer> [] :call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', 0, v:count1)<cr>"
60execute "nnoremap <silent> <buffer> ]m :call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>"
61execute "nnoremap <silent> <buffer> [m :call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>"
62execute "nnoremap <silent> <buffer> ]M :call <SID>Python_jump('n', '". b:next_end."', 'W', 0, v:count1)<cr>"
63execute "nnoremap <silent> <buffer> [M :call <SID>Python_jump('n', '". b:prev_end."', 'Wb', 0, v:count1)<cr>"
64
65execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W', v:count1)<cr>"
66execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
67execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', 0, v:count1)<cr>"
68execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', 0, v:count1)<cr>"
69execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W', v:count1)<cr>"
70execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb', v:count1)<cr>"
71execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', 0, v:count1)<cr>"
72execute "onoremap <silent> <buffer> [M :call <SID>Python_jump('o', '". b:prev_end."', 'Wb', 0, v:count1)<cr>"
73
74execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W', v:count1)<cr>"
75execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
76execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', 0, v:count1)<cr>"
77execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', 0, v:count1)<cr>"
78execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W', v:count1)<cr>"
79execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb', v:count1)<cr>"
80execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', 0, v:count1)<cr>"
81execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', 0, v:count1)<cr>"
82
83if !exists('*<SID>Python_jump')
84  fun! <SID>Python_jump(mode, motion, flags, count, ...) range
85      let l:startofline = (a:0 >= 1) ? a:1 : 1
86
87      if a:mode == 'x'
88          normal! gv
89      endif
90
91      if l:startofline == 1
92          normal! 0
93      endif
94
95      let cnt = a:count
96      mark '
97      while cnt > 0
98          call search(a:motion, a:flags)
99          let cnt = cnt - 1
100      endwhile
101
102      if l:startofline == 1
103          normal! ^
104      endif
105  endfun
106endif
107
108if has("browsefilter") && !exists("b:browsefilter")
109    let b:browsefilter = "Python Files (*.py)\t*.py\n" .
110                \ "All Files (*.*)\t*.*\n"
111endif
112
113if !exists("g:python_recommended_style") || g:python_recommended_style != 0
114    " As suggested by PEP8.
115    setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
116endif
117
118" First time: try finding "pydoc".
119if !exists('g:pydoc_executable')
120    if executable('pydoc')
121        let g:pydoc_executable = 1
122    else
123        let g:pydoc_executable = 0
124    endif
125endif
126" If "pydoc" was found use it for keywordprg.
127if g:pydoc_executable
128    setlocal keywordprg=pydoc
129endif
130
131let &cpo = s:keepcpo
132unlet s:keepcpo
133