blob: 3a7190201f87210e6e2d3839f8e1e0f1c3f72c38 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file
2" Language: python
Tom Picton86f6e2c2024-05-11 14:26:06 -04003" Maintainer: Tom Picton <tom@tompicton.com>
Bram Moolenaar01164a62017-11-02 22:58:42 +01004" Previous Maintainer: James Sully <sullyj3@gmail.com>
Bram Moolenaarc95a3022016-06-12 23:01:46 +02005" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
Tom Picton86f6e2c2024-05-11 14:26:06 -04006" Last Change: 2024/05/11
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\\)
Tom Picton86f6e2c2024-05-11 14:26:06 -040017setlocal define=^\\s*\\([async ]\\?def\\\|class\\)
Bram Moolenaar98ef2332018-03-18 14:44:37 +010018
19" For imports with leading .., append / and replace additional .s with ../
20let b:grandparent_match = '^\(.\.\)\(\.*\)'
21let b:grandparent_sub = '\=submatch(1)."/".repeat("../",strlen(submatch(2)))'
22
23" For imports with a single leading ., replace it with ./
24let b:parent_match = '^\.\(\.\)\@!'
25let b:parent_sub = './'
26
27" Replace any . sandwiched between word characters with /
28let b:child_match = '\(\w\)\.\(\w\)'
29let b:child_sub = '\1/\2'
30
31setlocal includeexpr=substitute(substitute(substitute(
32 \v:fname,
33 \b:grandparent_match,b:grandparent_sub,''),
34 \b:parent_match,b:parent_sub,''),
35 \b:child_match,b:child_sub,'g')
36
Bram Moolenaar071d4272004-06-13 20:20:40 +000037setlocal suffixesadd=.py
Bram Moolenaar36782082013-11-28 13:53:34 +010038setlocal comments=b:#,fb:-
39setlocal commentstring=#\ %s
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar40962ec2018-01-28 22:47:25 +010041if has('python3')
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +020042 setlocal omnifunc=python3complete#Complete
Bram Moolenaar63b74a82019-03-24 15:09:13 +010043elseif has('python')
44 setlocal omnifunc=pythoncomplete#Complete
Bram Moolenaar40962ec2018-01-28 22:47:25 +010045endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000046
Bram Moolenaar071d4272004-06-13 20:20:40 +000047set wildignore+=*.pyc
48
Bram Moolenaardc083282016-10-11 08:57:33 +020049let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
50let b:prev_toplevel='\v^(class\|def\|async def)>'
Bram Moolenaar01164a62017-11-02 22:58:42 +010051let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
52let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
Bram Moolenaardc083282016-10-11 08:57:33 +020053let b:next='\v%$\|^\s*(class\|def\|async def)>'
54let b:prev='\v^\s*(class\|def\|async def)>'
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010055let b:next_end='\v\S\n*(%$\|^(\s*\n*)*(class\|def\|async def)\|^\S)'
56let b:prev_end='\v\S\n*(^(\s*\n*)*(class\|def\|async def)\|^\S)'
Bram Moolenaare18dbe82016-07-02 21:42:23 +020057
Bram Moolenaar63b74a82019-03-24 15:09:13 +010058if !exists('g:no_plugin_maps') && !exists('g:no_python_maps')
Tom Picton86f6e2c2024-05-11 14:26:06 -040059 execute "nnoremap <silent> <buffer> ]] :<C-U>call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>"
60 execute "nnoremap <silent> <buffer> [[ :<C-U>call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
61 execute "nnoremap <silent> <buffer> ][ :<C-U>call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
62 execute "nnoremap <silent> <buffer> [] :<C-U>call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
63 execute "nnoremap <silent> <buffer> ]m :<C-U>call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>"
64 execute "nnoremap <silent> <buffer> [m :<C-U>call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>"
65 execute "nnoremap <silent> <buffer> ]M :<C-U>call <SID>Python_jump('n', '". b:next_end."', 'W', v:count1, 0)<cr>"
66 execute "nnoremap <silent> <buffer> [M :<C-U>call <SID>Python_jump('n', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020067
Bram Moolenaar63b74a82019-03-24 15:09:13 +010068 execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W', v:count1)<cr>"
69 execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
70 execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
71 execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
72 execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W', v:count1)<cr>"
73 execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb', v:count1)<cr>"
74 execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', v:count1, 0)<cr>"
75 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 +020076
Bram Moolenaar63b74a82019-03-24 15:09:13 +010077 execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W', v:count1)<cr>"
78 execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
79 execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
80 execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
81 execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W', v:count1)<cr>"
82 execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb', v:count1)<cr>"
83 execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', v:count1, 0)<cr>"
84 execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"
85endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar36782082013-11-28 13:53:34 +010087if !exists('*<SID>Python_jump')
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020088 fun! <SID>Python_jump(mode, motion, flags, count, ...) range
Bram Moolenaar01164a62017-11-02 22:58:42 +010089 let l:startofline = (a:0 >= 1) ? a:1 : 1
90
Bram Moolenaare18dbe82016-07-02 21:42:23 +020091 if a:mode == 'x'
92 normal! gv
93 endif
94
Bram Moolenaar01164a62017-11-02 22:58:42 +010095 if l:startofline == 1
96 normal! 0
97 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020098
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020099 let cnt = a:count
Bram Moolenaar36782082013-11-28 13:53:34 +0100100 mark '
101 while cnt > 0
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200102 call search(a:motion, a:flags)
103 let cnt = cnt - 1
Bram Moolenaar36782082013-11-28 13:53:34 +0100104 endwhile
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200105
Bram Moolenaar01164a62017-11-02 22:58:42 +0100106 if l:startofline == 1
107 normal! ^
108 endif
Bram Moolenaar36782082013-11-28 13:53:34 +0100109 endfun
110endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
Bram Moolenaar36782082013-11-28 13:53:34 +0100112if has("browsefilter") && !exists("b:browsefilter")
Doug Kearns93197fd2024-01-14 20:59:02 +0100113 let b:browsefilter = "Python Files (*.py)\t*.py\n"
114 if has("win32")
115 let b:browsefilter .= "All Files (*.*)\t*\n"
116 else
117 let b:browsefilter .= "All Files (*)\t*\n"
118 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119endif
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200120
Bram Moolenaarabd468e2016-09-08 22:22:43 +0200121if !exists("g:python_recommended_style") || g:python_recommended_style != 0
122 " As suggested by PEP8.
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200123 setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4
Bram Moolenaarabd468e2016-09-08 22:22:43 +0200124endif
Bram Moolenaarb1332082013-10-06 14:22:40 +0200125
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200126" Use pydoc for keywordprg.
127" Unix users preferentially get pydoc3, then pydoc2.
128" Windows doesn't have a standalone pydoc executable in $PATH by default, nor
129" does it have separate python2/3 executables, so Windows users just get
130" whichever version corresponds to their installed Python version.
131if executable('python3')
132 setlocal keywordprg=python3\ -m\ pydoc
133elseif executable('python')
134 setlocal keywordprg=python\ -m\ pydoc
Bram Moolenaarb1332082013-10-06 14:22:40 +0200135endif
136
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100137" Script for filetype switching to undo the local stuff we may have changed
138let b:undo_ftplugin = 'setlocal cinkeys<'
139 \ . '|setlocal comments<'
140 \ . '|setlocal commentstring<'
141 \ . '|setlocal expandtab<'
142 \ . '|setlocal include<'
143 \ . '|setlocal includeexpr<'
144 \ . '|setlocal indentkeys<'
145 \ . '|setlocal keywordprg<'
146 \ . '|setlocal omnifunc<'
147 \ . '|setlocal shiftwidth<'
148 \ . '|setlocal softtabstop<'
149 \ . '|setlocal suffixesadd<'
150 \ . '|setlocal tabstop<'
151 \ . '|silent! nunmap <buffer> [M'
152 \ . '|silent! nunmap <buffer> [['
153 \ . '|silent! nunmap <buffer> []'
154 \ . '|silent! nunmap <buffer> [m'
155 \ . '|silent! nunmap <buffer> ]M'
156 \ . '|silent! nunmap <buffer> ]['
157 \ . '|silent! nunmap <buffer> ]]'
158 \ . '|silent! nunmap <buffer> ]m'
159 \ . '|silent! ounmap <buffer> [M'
160 \ . '|silent! ounmap <buffer> [['
161 \ . '|silent! ounmap <buffer> []'
162 \ . '|silent! ounmap <buffer> [m'
163 \ . '|silent! ounmap <buffer> ]M'
164 \ . '|silent! ounmap <buffer> ]['
165 \ . '|silent! ounmap <buffer> ]]'
166 \ . '|silent! ounmap <buffer> ]m'
167 \ . '|silent! xunmap <buffer> [M'
168 \ . '|silent! xunmap <buffer> [['
169 \ . '|silent! xunmap <buffer> []'
170 \ . '|silent! xunmap <buffer> [m'
171 \ . '|silent! xunmap <buffer> ]M'
172 \ . '|silent! xunmap <buffer> ]['
173 \ . '|silent! xunmap <buffer> ]]'
174 \ . '|silent! xunmap <buffer> ]m'
175 \ . '|unlet! b:browsefilter'
176 \ . '|unlet! b:child_match'
177 \ . '|unlet! b:child_sub'
178 \ . '|unlet! b:grandparent_match'
179 \ . '|unlet! b:grandparent_sub'
180 \ . '|unlet! b:next'
181 \ . '|unlet! b:next_end'
182 \ . '|unlet! b:next_endtoplevel'
183 \ . '|unlet! b:next_toplevel'
184 \ . '|unlet! b:parent_match'
185 \ . '|unlet! b:parent_sub'
186 \ . '|unlet! b:prev'
187 \ . '|unlet! b:prev_end'
188 \ . '|unlet! b:prev_endtoplevel'
189 \ . '|unlet! b:prev_toplevel'
190 \ . '|unlet! b:undo_ftplugin'
191
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200192let &cpo = s:keepcpo
193unlet s:keepcpo