xref: /vim-8.2.3635/runtime/ftplugin/vim.vim (revision 56994d21)
1" Vim filetype plugin
2" Language:	Vim
3" Maintainer:	Bram Moolenaar <[email protected]>
4" Last Change:	2021 Apr 11
5
6" Only do this when not done yet for this buffer
7if exists("b:did_ftplugin")
8  finish
9endif
10
11" Don't load another plugin for this buffer
12let b:did_ftplugin = 1
13
14let s:cpo_save = &cpo
15set cpo&vim
16
17if !exists('*VimFtpluginUndo')
18  func VimFtpluginUndo()
19    setl fo< isk< com< tw< commentstring<
20    if exists('b:did_add_maps')
21      silent! nunmap <buffer> [[
22      silent! vunmap <buffer> [[
23      silent! nunmap <buffer> ]]
24      silent! vunmap <buffer> ]]
25      silent! nunmap <buffer> []
26      silent! vunmap <buffer> []
27      silent! nunmap <buffer> ][
28      silent! vunmap <buffer> ][
29      silent! nunmap <buffer> ]"
30      silent! vunmap <buffer> ]"
31      silent! nunmap <buffer> ["
32      silent! vunmap <buffer> ["
33    endif
34    unlet! b:match_ignorecase b:match_words b:match_skip b:did_add_maps
35  endfunc
36endif
37
38let b:undo_ftplugin = "call VimFtpluginUndo()"
39
40" Set 'formatoptions' to break comment lines but not other lines,
41" and insert the comment leader when hitting <CR> or using "o".
42setlocal fo-=t fo+=croql
43
44" To allow tag lookup via CTRL-] for autoload functions, '#' must be a
45" keyword character.  E.g., for netrw#Nread().
46setlocal isk+=#
47
48" Use :help to lookup the keyword under the cursor with K.
49setlocal keywordprg=:help
50
51if "\n" .. getline(1, 10)->join("\n") =~# '\n\s*vim9\%[script]\>'
52  " Set 'comments' to format dashed lists in comments
53  setlocal com=sO:#\ -,mO:#\ \ ,eO:##,:#
54  " Comments starts with # in Vim9 script
55  setlocal commentstring=#%s
56else
57  setlocal com=sO:\"\ -,mO:\"\ \ ,eO:\"\",:\"
58  " Comments starts with a double quote in legacy script
59  setlocal commentstring=\"%s
60endif
61
62
63" Format comments to be up to 78 characters long
64if &tw == 0
65  setlocal tw=78
66endif
67
68if !exists("no_plugin_maps") && !exists("no_vim_maps")
69  let b:did_add_maps = 1
70
71  " Move around functions.
72  nnoremap <silent><buffer> [[ m':call search('^\s*\(fu\%[nction]\\|def\)\>', "bW")<CR>
73  vnoremap <silent><buffer> [[ m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|def\)\>', "bW")<CR>
74  nnoremap <silent><buffer> ]] m':call search('^\s*\(fu\%[nction]\\|def\)\>', "W")<CR>
75  vnoremap <silent><buffer> ]] m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|def\)\>', "W")<CR>
76  nnoremap <silent><buffer> [] m':call search('^\s*end\(f\%[unction]\\|def\)\>', "bW")<CR>
77  vnoremap <silent><buffer> [] m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|def\)\>', "bW")<CR>
78  nnoremap <silent><buffer> ][ m':call search('^\s*end\(f\%[unction]\\|def\)\>', "W")<CR>
79  vnoremap <silent><buffer> ][ m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|def\)\>', "W")<CR>
80
81  " Move around comments
82  nnoremap <silent><buffer> ]" :call search('^\(\s*".*\n\)\@<!\(\s*"\)', "W")<CR>
83  vnoremap <silent><buffer> ]" :<C-U>exe "normal! gv"<Bar>call search('^\(\s*".*\n\)\@<!\(\s*"\)', "W")<CR>
84  nnoremap <silent><buffer> [" :call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
85  vnoremap <silent><buffer> [" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
86endif
87
88" Let the matchit plugin know what items can be matched.
89if exists("loaded_matchit")
90  let b:match_ignorecase = 0
91  " "func" can also be used as a type:
92  "   var Ref: func
93  " or to list functions:
94  "   func name
95  " require a parenthesis following, then there can be an "endfunc".
96  let b:match_words =
97	\ '\<\%(fu\%[nction]\|def\)!\=\s\+\S\+(:\%(\%(^\||\)\s*\)\@<=\<retu\%[rn]\>:\%(\%(^\||\)\s*\)\@<=\<\%(endf\%[unction]\|enddef\)\>,' .
98 	\ '\<\(wh\%[ile]\|for\)\>:\%(\%(^\||\)\s*\)\@<=\<brea\%[k]\>:\%(\%(^\||\)\s*\)\@<=\<con\%[tinue]\>:\%(\%(^\||\)\s*\)\@<=\<end\(w\%[hile]\|fo\%[r]\)\>,' .
99	\ '\<if\>:\%(\%(^\||\)\s*\)\@<=\<el\%[seif]\>:\%(\%(^\||\)\s*\)\@<=\<en\%[dif]\>,' .
100	\ '{:},' .
101	\ '\<try\>:\%(\%(^\||\)\s*\)\@<=\<cat\%[ch]\>:\%(\%(^\||\)\s*\)\@<=\<fina\%[lly]\>:\%(\%(^\||\)\s*\)\@<=\<endt\%[ry]\>,' .
102	\ '\<aug\%[roup]\s\+\%(END\>\)\@!\S:\<aug\%[roup]\s\+END\>,'
103  " Ignore syntax region commands and settings, any 'en*' would clobber
104  " if-endif.
105  " - set spl=de,en
106  " - au! FileType javascript syntax region foldBraces start=/{/ end=/}/ …
107  let b:match_skip = 'synIDattr(synID(line("."),col("."),1),"name")
108        \ =~? "comment\\|string\\|vimSynReg\\|vimSet"'
109endif
110
111let &cpo = s:cpo_save
112unlet s:cpo_save
113
114" removed this, because 'cpoptions' is a global option.
115" setlocal cpo+=M		" makes \%( match \)
116