xref: /vim-8.2.3635/runtime/ftplugin/python.vim (revision 68e6560b)
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 17 Mar 2019
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
40if has('python3')
41  setlocal omnifunc=python3complete#Complete
42elseif has('python')
43  setlocal omnifunc=pythoncomplete#Complete
44endif
45
46set wildignore+=*.pyc
47
48let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
49let b:prev_toplevel='\v^(class\|def\|async def)>'
50let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
51let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
52let b:next='\v%$\|^\s*(class\|def\|async def)>'
53let b:prev='\v^\s*(class\|def\|async def)>'
54let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)'
55let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)'
56
57if !exists('g:no_plugin_maps') && !exists('g:no_python_maps')
58    execute "nnoremap <silent> <buffer> ]] :call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>"
59    execute "nnoremap <silent> <buffer> [[ :call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
60    execute "nnoremap <silent> <buffer> ][ :call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
61    execute "nnoremap <silent> <buffer> [] :call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
62    execute "nnoremap <silent> <buffer> ]m :call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>"
63    execute "nnoremap <silent> <buffer> [m :call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>"
64    execute "nnoremap <silent> <buffer> ]M :call <SID>Python_jump('n', '". b:next_end."', 'W', v:count1, 0)<cr>"
65    execute "nnoremap <silent> <buffer> [M :call <SID>Python_jump('n', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
66
67    execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W', v:count1)<cr>"
68    execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
69    execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
70    execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
71    execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W', v:count1)<cr>"
72    execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb', v:count1)<cr>"
73    execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', v:count1, 0)<cr>"
74    execute "onoremap <silent> <buffer> [M :call <SID>Python_jump('o', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
75
76    execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W', v:count1)<cr>"
77    execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
78    execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
79    execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
80    execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W', v:count1)<cr>"
81    execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb', v:count1)<cr>"
82    execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', v:count1, 0)<cr>"
83    execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
84endif
85
86if !exists('*<SID>Python_jump')
87  fun! <SID>Python_jump(mode, motion, flags, count, ...) range
88      let l:startofline = (a:0 >= 1) ? a:1 : 1
89
90      if a:mode == 'x'
91          normal! gv
92      endif
93
94      if l:startofline == 1
95          normal! 0
96      endif
97
98      let cnt = a:count
99      mark '
100      while cnt > 0
101          call search(a:motion, a:flags)
102          let cnt = cnt - 1
103      endwhile
104
105      if l:startofline == 1
106          normal! ^
107      endif
108  endfun
109endif
110
111if has("browsefilter") && !exists("b:browsefilter")
112    let b:browsefilter = "Python Files (*.py)\t*.py\n" .
113                \ "All Files (*.*)\t*.*\n"
114endif
115
116if !exists("g:python_recommended_style") || g:python_recommended_style != 0
117    " As suggested by PEP8.
118    setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
119endif
120
121" First time: try finding "pydoc".
122if !exists('g:pydoc_executable')
123    if executable('pydoc')
124        let g:pydoc_executable = 1
125    else
126        let g:pydoc_executable = 0
127    endif
128endif
129
130" Windows-specific pydoc setup
131if has('win32') || has('win64')
132    if executable('python')
133        " available as Tools\scripts\pydoc.py
134        let g:pydoc_executable = 1
135    else
136        let g:pydoc_executable = 0
137    endif
138endif
139
140" If "pydoc" was found use it for keywordprg.
141if g:pydoc_executable
142    if has('win32') || has('win64')
143        setlocal keywordprg=python\ -m\ pydoc\
144    else
145        setlocal keywordprg=pydoc
146    endif
147endif
148
149" Script for filetype switching to undo the local stuff we may have changed
150let b:undo_ftplugin = 'setlocal cinkeys<'
151      \ . '|setlocal comments<'
152      \ . '|setlocal commentstring<'
153      \ . '|setlocal expandtab<'
154      \ . '|setlocal include<'
155      \ . '|setlocal includeexpr<'
156      \ . '|setlocal indentkeys<'
157      \ . '|setlocal keywordprg<'
158      \ . '|setlocal omnifunc<'
159      \ . '|setlocal shiftwidth<'
160      \ . '|setlocal softtabstop<'
161      \ . '|setlocal suffixesadd<'
162      \ . '|setlocal tabstop<'
163      \ . '|silent! nunmap <buffer> [M'
164      \ . '|silent! nunmap <buffer> [['
165      \ . '|silent! nunmap <buffer> []'
166      \ . '|silent! nunmap <buffer> [m'
167      \ . '|silent! nunmap <buffer> ]M'
168      \ . '|silent! nunmap <buffer> ]['
169      \ . '|silent! nunmap <buffer> ]]'
170      \ . '|silent! nunmap <buffer> ]m'
171      \ . '|silent! ounmap <buffer> [M'
172      \ . '|silent! ounmap <buffer> [['
173      \ . '|silent! ounmap <buffer> []'
174      \ . '|silent! ounmap <buffer> [m'
175      \ . '|silent! ounmap <buffer> ]M'
176      \ . '|silent! ounmap <buffer> ]['
177      \ . '|silent! ounmap <buffer> ]]'
178      \ . '|silent! ounmap <buffer> ]m'
179      \ . '|silent! xunmap <buffer> [M'
180      \ . '|silent! xunmap <buffer> [['
181      \ . '|silent! xunmap <buffer> []'
182      \ . '|silent! xunmap <buffer> [m'
183      \ . '|silent! xunmap <buffer> ]M'
184      \ . '|silent! xunmap <buffer> ]['
185      \ . '|silent! xunmap <buffer> ]]'
186      \ . '|silent! xunmap <buffer> ]m'
187      \ . '|unlet! b:browsefilter'
188      \ . '|unlet! b:child_match'
189      \ . '|unlet! b:child_sub'
190      \ . '|unlet! b:grandparent_match'
191      \ . '|unlet! b:grandparent_sub'
192      \ . '|unlet! b:next'
193      \ . '|unlet! b:next_end'
194      \ . '|unlet! b:next_endtoplevel'
195      \ . '|unlet! b:next_toplevel'
196      \ . '|unlet! b:parent_match'
197      \ . '|unlet! b:parent_sub'
198      \ . '|unlet! b:prev'
199      \ . '|unlet! b:prev_end'
200      \ . '|unlet! b:prev_endtoplevel'
201      \ . '|unlet! b:prev_toplevel'
202      \ . '|unlet! b:undo_ftplugin'
203
204let &cpo = s:keepcpo
205unlet s:keepcpo
206