blob: 64c1a87a6912a97c305c5a7c91eac55cf1d37957 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file
2" Language: python
Bram Moolenaar01164a62017-11-02 22:58:42 +01003" Maintainer: Tom Picton <tom@tompicton.co.uk>
4" Previous Maintainer: James Sully <sullyj3@gmail.com>
Bram Moolenaarc95a3022016-06-12 23:01:46 +02005" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
Bram Moolenaar63b74a82019-03-24 15:09:13 +01006" Last Change: Sun 17 Mar 2019
Bram Moolenaar01164a62017-11-02 22:58:42 +01007" https://github.com/tpict/vim-ftplugin-python
Bram Moolenaar071d4272004-06-13 20:20:40 +00008
9if exists("b:did_ftplugin") | finish | endif
10let b:did_ftplugin = 1
Bram Moolenaar9a7224b2012-04-30 15:56:52 +020011let s:keepcpo= &cpo
12set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14setlocal cinkeys-=0#
15setlocal indentkeys-=0#
Bram Moolenaar92dff182014-02-11 19:15:50 +010016setlocal include=^\\s*\\(from\\\|import\\)
Bram Moolenaar98ef2332018-03-18 14:44:37 +010017
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000036setlocal suffixesadd=.py
Bram Moolenaar36782082013-11-28 13:53:34 +010037setlocal comments=b:#,fb:-
38setlocal commentstring=#\ %s
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaar40962ec2018-01-28 22:47:25 +010040if has('python3')
Bram Moolenaar63b74a82019-03-24 15:09:13 +010041 setlocal omnifunc=python3complete#Complete
42elseif has('python')
43 setlocal omnifunc=pythoncomplete#Complete
Bram Moolenaar40962ec2018-01-28 22:47:25 +010044endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000045
Bram Moolenaar071d4272004-06-13 20:20:40 +000046set wildignore+=*.pyc
47
Bram Moolenaardc083282016-10-11 08:57:33 +020048let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
49let b:prev_toplevel='\v^(class\|def\|async def)>'
Bram Moolenaar01164a62017-11-02 22:58:42 +010050let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
51let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
Bram Moolenaardc083282016-10-11 08:57:33 +020052let b:next='\v%$\|^\s*(class\|def\|async def)>'
53let b:prev='\v^\s*(class\|def\|async def)>'
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010054let 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)'
Bram Moolenaare18dbe82016-07-02 21:42:23 +020056
Bram Moolenaar63b74a82019-03-24 15:09:13 +010057if !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>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020066
Bram Moolenaar63b74a82019-03-24 15:09:13 +010067 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>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020075
Bram Moolenaar63b74a82019-03-24 15:09:13 +010076 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaar36782082013-11-28 13:53:34 +010086if !exists('*<SID>Python_jump')
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020087 fun! <SID>Python_jump(mode, motion, flags, count, ...) range
Bram Moolenaar01164a62017-11-02 22:58:42 +010088 let l:startofline = (a:0 >= 1) ? a:1 : 1
89
Bram Moolenaare18dbe82016-07-02 21:42:23 +020090 if a:mode == 'x'
91 normal! gv
92 endif
93
Bram Moolenaar01164a62017-11-02 22:58:42 +010094 if l:startofline == 1
95 normal! 0
96 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020097
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020098 let cnt = a:count
Bram Moolenaar36782082013-11-28 13:53:34 +010099 mark '
100 while cnt > 0
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200101 call search(a:motion, a:flags)
102 let cnt = cnt - 1
Bram Moolenaar36782082013-11-28 13:53:34 +0100103 endwhile
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200104
Bram Moolenaar01164a62017-11-02 22:58:42 +0100105 if l:startofline == 1
106 normal! ^
107 endif
Bram Moolenaar36782082013-11-28 13:53:34 +0100108 endfun
109endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaar36782082013-11-28 13:53:34 +0100111if has("browsefilter") && !exists("b:browsefilter")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 let b:browsefilter = "Python Files (*.py)\t*.py\n" .
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200113 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114endif
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200115
Bram Moolenaarabd468e2016-09-08 22:22:43 +0200116if !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
Bram Moolenaarb1332082013-10-06 14:22:40 +0200120
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
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100129
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
Bram Moolenaarb1332082013-10-06 14:22:40 +0200140" If "pydoc" was found use it for keywordprg.
141if g:pydoc_executable
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100142 if has('win32') || has('win64')
143 setlocal keywordprg=python\ -m\ pydoc\
144 else
145 setlocal keywordprg=pydoc
146 endif
Bram Moolenaarb1332082013-10-06 14:22:40 +0200147endif
148
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100149" 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
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200204let &cpo = s:keepcpo
205unlet s:keepcpo