blob: c9ea793eedc6189563397ff6f35f6bbb147b906c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin
Konfekt094494b2025-02-23 16:03:30 +01002" Language: Vim
3" Maintainer: Doug Kearns <dougkearns@gmail.com>
4" Last Change: 2025 Feb 23
5" Former Maintainer: Bram Moolenaar <Bram@vim.org>
6" Contributors: Riley Bruins <ribru17@gmail.com> ('commentstring'),
7" @Konfekt
8" @tpope (s:Help())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009
10" Only do this when not done yet for this buffer
11if exists("b:did_ftplugin")
12 finish
13endif
14
15" Don't load another plugin for this buffer
16let b:did_ftplugin = 1
17
Bram Moolenaar5c736222010-01-06 20:54:52 +010018let s:cpo_save = &cpo
Bram Moolenaar388a5d42020-05-26 21:20:45 +020019set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
Bram Moolenaarfd358112018-07-07 23:21:31 +020021if !exists('*VimFtpluginUndo')
22 func VimFtpluginUndo()
Konfekt094494b2025-02-23 16:03:30 +010023 setl fo< isk< com< tw< commentstring< include< define< keywordprg<
24 sil! delc -buffer VimKeywordPrg
Bram Moolenaard473c8c2018-08-11 18:00:22 +020025 if exists('b:did_add_maps')
26 silent! nunmap <buffer> [[
Konfekt094494b2025-02-23 16:03:30 +010027 silent! xunmap <buffer> [[
Bram Moolenaard473c8c2018-08-11 18:00:22 +020028 silent! nunmap <buffer> ]]
Konfekt094494b2025-02-23 16:03:30 +010029 silent! xunmap <buffer> ]]
Bram Moolenaard473c8c2018-08-11 18:00:22 +020030 silent! nunmap <buffer> []
Konfekt094494b2025-02-23 16:03:30 +010031 silent! xunmap <buffer> []
Bram Moolenaard473c8c2018-08-11 18:00:22 +020032 silent! nunmap <buffer> ][
Konfekt094494b2025-02-23 16:03:30 +010033 silent! xunmap <buffer> ][
Bram Moolenaard473c8c2018-08-11 18:00:22 +020034 silent! nunmap <buffer> ]"
Konfekt094494b2025-02-23 16:03:30 +010035 silent! xunmap <buffer> ]"
Bram Moolenaard473c8c2018-08-11 18:00:22 +020036 silent! nunmap <buffer> ["
Konfekt094494b2025-02-23 16:03:30 +010037 silent! xunmap <buffer> ["
Bram Moolenaarfd358112018-07-07 23:21:31 +020038 endif
39 unlet! b:match_ignorecase b:match_words b:match_skip b:did_add_maps
40 endfunc
41endif
42
43let b:undo_ftplugin = "call VimFtpluginUndo()"
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45" Set 'formatoptions' to break comment lines but not other lines,
46" and insert the comment leader when hitting <CR> or using "o".
47setlocal fo-=t fo+=croql
48
Bram Moolenaar5c736222010-01-06 20:54:52 +010049" To allow tag lookup via CTRL-] for autoload functions, '#' must be a
50" keyword character. E.g., for netrw#Nread().
51setlocal isk+=#
52
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010053" Use :help to lookup the keyword under the cursor with K.
Konfekt094494b2025-02-23 16:03:30 +010054" Distinguish between commands, options and functions.
55if !exists("*" .. expand("<SID>") .. "Help")
56 function s:Help(topic) abort
57 let topic = a:topic
58
59 if get(g:, 'syntax_on', 0)
60 let syn = synIDattr(synID(line('.'), col('.'), 1), 'name')
61 if syn ==# 'vimFuncName'
62 return topic.'()'
63 elseif syn ==# 'vimOption'
64 return "'".topic."'"
65 elseif syn ==# 'vimUserAttrbKey'
66 return ':command-'.topic
67 elseif syn =~# 'vimCommand'
68 return ':'.topic
69 endif
70 endif
71
72 let col = col('.') - 1
73 while col && getline('.')[col] =~# '\k'
74 let col -= 1
75 endwhile
76 let pre = col == 0 ? '' : getline('.')[0 : col]
77
78 let col = col('.') - 1
79 while col && getline('.')[col] =~# '\k'
80 let col += 1
81 endwhile
82 let post = getline('.')[col : -1]
83
84 if pre =~# '^\s*:\=$'
85 return ':'.topic
86 elseif pre =~# '\<v:$'
87 return 'v:'.topic
88 elseif topic ==# 'v' && post =~# ':\w\+'
89 return 'v'.matchstr(post, ':\w\+')
90 else
91 return topic
92 endif
93 endfunction
94endif
95command! -buffer -nargs=1 VimKeywordPrg :exe 'help' s:Help(<q-args>)
96setlocal keywordprg=:VimKeywordPrg
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +010097
Bram Moolenaar71b6d332022-09-10 13:13:14 +010098" Comments starts with # in Vim9 script. We have to guess which one to use.
dkearns04e53632024-04-11 06:18:37 +100099if "\n" .. getline(1, 32)->join("\n") =~# '\n\s*vim9\%[script]\>'
Riley Bruins0a083062024-06-03 20:40:45 +0200100 setlocal commentstring=#\ %s
Bram Moolenaar82be4842021-01-11 19:40:15 +0100101else
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100102 setlocal commentstring=\"%s
Bram Moolenaar82be4842021-01-11 19:40:15 +0100103endif
104
Bram Moolenaar71b6d332022-09-10 13:13:14 +0100105" Set 'comments' to format dashed lists in comments, both in Vim9 and legacy
106" script.
dkearns21ce1592024-01-29 04:54:08 +1100107setlocal com=sO:#\ -,mO:#\ \ ,eO:##,:#\\\ ,:#,sO:\"\ -,mO:\"\ \ ,eO:\"\",:\"\\\ ,:\"
Bram Moolenaar71b6d332022-09-10 13:13:14 +0100108
Bram Moolenaar5ed11532022-07-06 13:18:11 +0100109" set 'include' to recognize import commands
110setlocal include=\\v^\\s*import\\s*(autoload)?
111
112" set 'define' to recognize export commands
113setlocal define=\\v^\\s*export\\s*(def\|const\|var\|final)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
115" Format comments to be up to 78 characters long
116if &tw == 0
117 setlocal tw=78
118endif
119
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100120if !exists("no_plugin_maps") && !exists("no_vim_maps")
Bram Moolenaarfd358112018-07-07 23:21:31 +0200121 let b:did_add_maps = 1
122
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100123 " Move around functions.
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +0100124 nnoremap <silent><buffer> [[ m':call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100125 xnoremap <silent><buffer> [[ m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +0100126 nnoremap <silent><buffer> ]] m':call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100127 xnoremap <silent><buffer> ]] m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +0100128 nnoremap <silent><buffer> [] m':call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100129 xnoremap <silent><buffer> [] m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +0100130 nnoremap <silent><buffer> ][ m':call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100131 xnoremap <silent><buffer> ][ m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100133 " Move around comments
dkearnsfea96c02023-10-24 03:16:44 +1100134 nnoremap <silent><buffer> ]" :call search('\%(^\s*".*\n\)\@<!\%(^\s*"\)', "W")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100135 xnoremap <silent><buffer> ]" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\@<!\%(^\s*"\)', "W")<CR>
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100136 nnoremap <silent><buffer> [" :call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100137 xnoremap <silent><buffer> [" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100138endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
140" Let the matchit plugin know what items can be matched.
141if exists("loaded_matchit")
142 let b:match_ignorecase = 0
Bram Moolenaare0e39172021-01-25 21:14:57 +0100143 " "func" can also be used as a type:
144 " var Ref: func
145 " or to list functions:
146 " func name
147 " require a parenthesis following, then there can be an "endfunc".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 let b:match_words =
Konfekt094494b2025-02-23 16:03:30 +0100149 \ '\<\%(fu\%[nction]\|def\)!\=\s\+\S\+\s*(:\%(\%(^\||\)\s*\)\@<=\<retu\%[rn]\>:\%(\%(^\||\)\s*\)\@<=\<\%(endf\%[unction]\|enddef\)\>,' ..
150 \ '\<\%(wh\%[ile]\|for\)\>:\%(\%(^\||\)\s*\)\@<=\<brea\%[k]\>:\%(\%(^\||\)\s*\)\@<=\<con\%[tinue]\>:\%(\%(^\||\)\s*\)\@<=\<end\%(w\%[hile]\|fo\%[r]\)\>,' ..
151 \ '\<if\>:\%(\%(^\||\)\s*\)\@<=\<el\%[seif]\>:\%(\%(^\||\)\s*\)\@<=\<en\%[dif]\>,' ..
152 \ '{:},' ..
153 \ '\<try\>:\%(\%(^\||\)\s*\)\@<=\<cat\%[ch]\>:\%(\%(^\||\)\s*\)\@<=\<fina\%[lly]\>:\%(\%(^\||\)\s*\)\@<=\<endt\%[ry]\>,' ..
154 \ '\<aug\%[roup]\s\+\%(END\>\)\@!\S:\<aug\%[roup]\s\+END\>,' ..
155 \ '\<class\>:\<endclass\>,' ..
156 \ '\<interface\>:\<endinterface\>,' ..
157 \ '\<enum\>:\<endenum\>'
Bram Moolenaar938ae282023-02-20 20:44:55 +0000158
Bram Moolenaar6e932462014-09-09 18:48:09 +0200159 " Ignore syntax region commands and settings, any 'en*' would clobber
160 " if-endif.
161 " - set spl=de,en
162 " - au! FileType javascript syntax region foldBraces start=/{/ end=/}/ …
Bram Moolenaar938ae282023-02-20 20:44:55 +0000163 " Also ignore here-doc and dictionary keys (vimVar).
164 let b:match_skip = 'synIDattr(synID(line("."), col("."), 1), "name")
Konfekt094494b2025-02-23 16:03:30 +0100165 \ =~? "comment\\|string\\|vimSynReg\\|vimSet\\|vimLetHereDoc\\|vimVar"'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166endif
167
Bram Moolenaar5c736222010-01-06 20:54:52 +0100168let &cpo = s:cpo_save
169unlet s:cpo_save
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000170
171" removed this, because 'cpoptions' is a global option.
172" setlocal cpo+=M " makes \%( match \)
Konfekt094494b2025-02-23 16:03:30 +0100173"
174" vim: sw=2 et