blob: ee271efaaf19acde418eab35379760ea8df22a65 [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 Moolenaar98ef2332018-03-18 14:44:37 +01006" Last Change: Sun, 18 March 2018
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 Moolenaar18144c82006-04-12 21:52:12 +000040setlocal omnifunc=pythoncomplete#Complete
Bram Moolenaar40962ec2018-01-28 22:47:25 +010041if has('python3')
42 setlocal omnifunc=python3complete#Complete
43endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000044
Bram Moolenaar071d4272004-06-13 20:20:40 +000045set wildignore+=*.pyc
46
Bram Moolenaardc083282016-10-11 08:57:33 +020047let b:next_toplevel='\v%$\|^(class\|def\|async def)>'
48let b:prev_toplevel='\v^(class\|def\|async def)>'
Bram Moolenaar01164a62017-11-02 22:58:42 +010049let b:next_endtoplevel='\v%$\|\S.*\n+(def\|class)'
50let b:prev_endtoplevel='\v\S.*\n+(def\|class)'
Bram Moolenaardc083282016-10-11 08:57:33 +020051let b:next='\v%$\|^\s*(class\|def\|async def)>'
52let b:prev='\v^\s*(class\|def\|async def)>'
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010053let 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)'
Bram Moolenaare18dbe82016-07-02 21:42:23 +020055
Bram Moolenaarabd468e2016-09-08 22:22:43 +020056execute "nnoremap <silent> <buffer> ]] :call <SID>Python_jump('n', '". b:next_toplevel."', 'W')<cr>"
57execute "nnoremap <silent> <buffer> [[ :call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb')<cr>"
Bram Moolenaar01164a62017-11-02 22:58:42 +010058execute "nnoremap <silent> <buffer> ][ :call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', 0)<cr>"
59execute "nnoremap <silent> <buffer> [] :call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020060execute "nnoremap <silent> <buffer> ]m :call <SID>Python_jump('n', '". b:next."', 'W')<cr>"
61execute "nnoremap <silent> <buffer> [m :call <SID>Python_jump('n', '". b:prev."', 'Wb')<cr>"
Bram Moolenaar01164a62017-11-02 22:58:42 +010062execute "nnoremap <silent> <buffer> ]M :call <SID>Python_jump('n', '". b:next_end."', 'W', 0)<cr>"
63execute "nnoremap <silent> <buffer> [M :call <SID>Python_jump('n', '". b:prev_end."', 'Wb', 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020064
65execute "onoremap <silent> <buffer> ]] :call <SID>Python_jump('o', '". b:next_toplevel."', 'W')<cr>"
66execute "onoremap <silent> <buffer> [[ :call <SID>Python_jump('o', '". b:prev_toplevel."', 'Wb')<cr>"
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010067execute "onoremap <silent> <buffer> ][ :call <SID>Python_jump('o', '". b:next_endtoplevel."', 'W', 0)<cr>"
68execute "onoremap <silent> <buffer> [] :call <SID>Python_jump('o', '". b:prev_endtoplevel."', 'Wb', 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020069execute "onoremap <silent> <buffer> ]m :call <SID>Python_jump('o', '". b:next."', 'W')<cr>"
70execute "onoremap <silent> <buffer> [m :call <SID>Python_jump('o', '". b:prev."', 'Wb')<cr>"
Bram Moolenaar01164a62017-11-02 22:58:42 +010071execute "onoremap <silent> <buffer> ]M :call <SID>Python_jump('o', '". b:next_end."', 'W', 0)<cr>"
72execute "onoremap <silent> <buffer> [M :call <SID>Python_jump('o', '". b:prev_end."', 'Wb', 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020073
74execute "xnoremap <silent> <buffer> ]] :call <SID>Python_jump('x', '". b:next_toplevel."', 'W')<cr>"
75execute "xnoremap <silent> <buffer> [[ :call <SID>Python_jump('x', '". b:prev_toplevel."', 'Wb')<cr>"
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010076execute "xnoremap <silent> <buffer> ][ :call <SID>Python_jump('x', '". b:next_endtoplevel."', 'W', 0)<cr>"
77execute "xnoremap <silent> <buffer> [] :call <SID>Python_jump('x', '". b:prev_endtoplevel."', 'Wb', 0)<cr>"
Bram Moolenaarabd468e2016-09-08 22:22:43 +020078execute "xnoremap <silent> <buffer> ]m :call <SID>Python_jump('x', '". b:next."', 'W')<cr>"
79execute "xnoremap <silent> <buffer> [m :call <SID>Python_jump('x', '". b:prev."', 'Wb')<cr>"
Bram Moolenaar01164a62017-11-02 22:58:42 +010080execute "xnoremap <silent> <buffer> ]M :call <SID>Python_jump('x', '". b:next_end."', 'W', 0)<cr>"
81execute "xnoremap <silent> <buffer> [M :call <SID>Python_jump('x', '". b:prev_end."', 'Wb', 0)<cr>"
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
Bram Moolenaar36782082013-11-28 13:53:34 +010083if !exists('*<SID>Python_jump')
Bram Moolenaar01164a62017-11-02 22:58:42 +010084 fun! <SID>Python_jump(mode, motion, flags, ...) range
85 let l:startofline = (a:0 >= 1) ? a:1 : 1
86
Bram Moolenaare18dbe82016-07-02 21:42:23 +020087 if a:mode == 'x'
88 normal! gv
89 endif
90
Bram Moolenaar01164a62017-11-02 22:58:42 +010091 if l:startofline == 1
92 normal! 0
93 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020094
Bram Moolenaar36782082013-11-28 13:53:34 +010095 let cnt = v:count1
Bram Moolenaar36782082013-11-28 13:53:34 +010096 mark '
97 while cnt > 0
Bram Moolenaare18dbe82016-07-02 21:42:23 +020098 call search(a:motion, a:flags)
99 let cnt = cnt - 1
Bram Moolenaar36782082013-11-28 13:53:34 +0100100 endwhile
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200101
Bram Moolenaar01164a62017-11-02 22:58:42 +0100102 if l:startofline == 1
103 normal! ^
104 endif
Bram Moolenaar36782082013-11-28 13:53:34 +0100105 endfun
106endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar36782082013-11-28 13:53:34 +0100108if has("browsefilter") && !exists("b:browsefilter")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 let b:browsefilter = "Python Files (*.py)\t*.py\n" .
Bram Moolenaare18dbe82016-07-02 21:42:23 +0200110 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111endif
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200112
Bram Moolenaarabd468e2016-09-08 22:22:43 +0200113if !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
Bram Moolenaarb1332082013-10-06 14:22:40 +0200117
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
Bram Moolenaar9a7224b2012-04-30 15:56:52 +0200131let &cpo = s:keepcpo
132unlet s:keepcpo