blob: 99ce0bc586dbff1a26bcec5441bed3540f087888 [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>
Konfekt08a410f2025-03-05 20:33:00 +01004" Last Change: 2025 Mar 05
Konfekt094494b2025-02-23 16:03:30 +01005" 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
Konfekt08a410f2025-03-05 20:33:00 +010088 elseif pre =~# '<$'
89 return '<'.topic.'>'
Konfekt580e4572025-02-25 20:53:55 +010090 elseif pre =~# '\\$'
91 return '/\'.topic
Konfekt094494b2025-02-23 16:03:30 +010092 elseif topic ==# 'v' && post =~# ':\w\+'
93 return 'v'.matchstr(post, ':\w\+')
94 else
95 return topic
96 endif
97 endfunction
98endif
99command! -buffer -nargs=1 VimKeywordPrg :exe 'help' s:Help(<q-args>)
100setlocal keywordprg=:VimKeywordPrg
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100101
Bram Moolenaar71b6d332022-09-10 13:13:14 +0100102" Comments starts with # in Vim9 script. We have to guess which one to use.
dkearns04e53632024-04-11 06:18:37 +1000103if "\n" .. getline(1, 32)->join("\n") =~# '\n\s*vim9\%[script]\>'
Riley Bruins0a083062024-06-03 20:40:45 +0200104 setlocal commentstring=#\ %s
Bram Moolenaar82be4842021-01-11 19:40:15 +0100105else
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100106 setlocal commentstring=\"%s
Bram Moolenaar82be4842021-01-11 19:40:15 +0100107endif
108
Bram Moolenaar71b6d332022-09-10 13:13:14 +0100109" Set 'comments' to format dashed lists in comments, both in Vim9 and legacy
110" script.
dkearns21ce1592024-01-29 04:54:08 +1100111setlocal com=sO:#\ -,mO:#\ \ ,eO:##,:#\\\ ,:#,sO:\"\ -,mO:\"\ \ ,eO:\"\",:\"\\\ ,:\"
Bram Moolenaar71b6d332022-09-10 13:13:14 +0100112
Bram Moolenaar5ed11532022-07-06 13:18:11 +0100113" set 'include' to recognize import commands
114setlocal include=\\v^\\s*import\\s*(autoload)?
115
116" set 'define' to recognize export commands
117setlocal define=\\v^\\s*export\\s*(def\|const\|var\|final)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119" Format comments to be up to 78 characters long
120if &tw == 0
121 setlocal tw=78
122endif
123
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100124if !exists("no_plugin_maps") && !exists("no_vim_maps")
Bram Moolenaarfd358112018-07-07 23:21:31 +0200125 let b:did_add_maps = 1
126
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100127 " Move around functions.
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +0100128 nnoremap <silent><buffer> [[ m':call search('^\s*\(fu\%[nction]\\|\(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*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +0100130 nnoremap <silent><buffer> ]] m':call search('^\s*\(fu\%[nction]\\|\(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*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +0100132 nnoremap <silent><buffer> [] m':call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100133 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 +0100134 nnoremap <silent><buffer> ][ m':call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100135 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 +0000136
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100137 " Move around comments
dkearnsfea96c02023-10-24 03:16:44 +1100138 nnoremap <silent><buffer> ]" :call search('\%(^\s*".*\n\)\@<!\%(^\s*"\)', "W")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100139 xnoremap <silent><buffer> ]" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\@<!\%(^\s*"\)', "W")<CR>
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100140 nnoremap <silent><buffer> [" :call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
Konfekt094494b2025-02-23 16:03:30 +0100141 xnoremap <silent><buffer> [" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100142endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144" Let the matchit plugin know what items can be matched.
145if exists("loaded_matchit")
146 let b:match_ignorecase = 0
Bram Moolenaare0e39172021-01-25 21:14:57 +0100147 " "func" can also be used as a type:
148 " var Ref: func
149 " or to list functions:
150 " func name
151 " require a parenthesis following, then there can be an "endfunc".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 let b:match_words =
Konfekt094494b2025-02-23 16:03:30 +0100153 \ '\<\%(fu\%[nction]\|def\)!\=\s\+\S\+\s*(:\%(\%(^\||\)\s*\)\@<=\<retu\%[rn]\>:\%(\%(^\||\)\s*\)\@<=\<\%(endf\%[unction]\|enddef\)\>,' ..
154 \ '\<\%(wh\%[ile]\|for\)\>:\%(\%(^\||\)\s*\)\@<=\<brea\%[k]\>:\%(\%(^\||\)\s*\)\@<=\<con\%[tinue]\>:\%(\%(^\||\)\s*\)\@<=\<end\%(w\%[hile]\|fo\%[r]\)\>,' ..
155 \ '\<if\>:\%(\%(^\||\)\s*\)\@<=\<el\%[seif]\>:\%(\%(^\||\)\s*\)\@<=\<en\%[dif]\>,' ..
156 \ '{:},' ..
157 \ '\<try\>:\%(\%(^\||\)\s*\)\@<=\<cat\%[ch]\>:\%(\%(^\||\)\s*\)\@<=\<fina\%[lly]\>:\%(\%(^\||\)\s*\)\@<=\<endt\%[ry]\>,' ..
158 \ '\<aug\%[roup]\s\+\%(END\>\)\@!\S:\<aug\%[roup]\s\+END\>,' ..
159 \ '\<class\>:\<endclass\>,' ..
160 \ '\<interface\>:\<endinterface\>,' ..
161 \ '\<enum\>:\<endenum\>'
Bram Moolenaar938ae282023-02-20 20:44:55 +0000162
Bram Moolenaar6e932462014-09-09 18:48:09 +0200163 " Ignore syntax region commands and settings, any 'en*' would clobber
164 " if-endif.
165 " - set spl=de,en
166 " - au! FileType javascript syntax region foldBraces start=/{/ end=/}/ …
Bram Moolenaar938ae282023-02-20 20:44:55 +0000167 " Also ignore here-doc and dictionary keys (vimVar).
168 let b:match_skip = 'synIDattr(synID(line("."), col("."), 1), "name")
Konfekt094494b2025-02-23 16:03:30 +0100169 \ =~? "comment\\|string\\|vimSynReg\\|vimSet\\|vimLetHereDoc\\|vimVar"'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170endif
171
Bram Moolenaar5c736222010-01-06 20:54:52 +0100172let &cpo = s:cpo_save
173unlet s:cpo_save
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000174
175" removed this, because 'cpoptions' is a global option.
176" setlocal cpo+=M " makes \%( match \)
Konfekt094494b2025-02-23 16:03:30 +0100177"
178" vim: sw=2 et