Skip to content

Commit 094494b

Browse files
KonfektAndrewRadevbenknobleGary Johnsontpope
committed
runtime(vim): improve &keywordprg in ftplugin
- let keywordprg in vim filetype handle context-sensitive help calls by detecting the syntax group of the word under the cursor - reformat whitespace - add modeline related: #16677 closes: #16680 Co-authored-by: Andrew Radev <andrey.radev@gmail.com> Co-authored-by: "D. Ben Knoble" <ben.knoble+github@gmail.com> Co-authored-by: Gary Johnson <garyjohn@spocom.com> Co-authored-by: Tim Pope <code@tpope.net> Co-authored-by: Doug Kearns <dougkearns@gmail.com> Co-authored-by: Christian Brabandt <cb@256bit.org> Signed-off-by: Konfekt <Konfekt@users.noreply.github.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 025dc48 commit 094494b

1 file changed

Lines changed: 76 additions & 29 deletions

File tree

runtime/ftplugin/vim.vim

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
" Vim filetype plugin
2-
" Language: Vim
3-
" Maintainer: Doug Kearns <dougkearns@gmail.com>
4-
" Last Change: 2025 Jan 06
5-
" Former Maintainer: Bram Moolenaar <Bram@vim.org>
6-
" Contributors: Riley Bruins <ribru17@gmail.com> ('commentstring')
2+
" 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())
79

810
" Only do this when not done yet for this buffer
911
if exists("b:did_ftplugin")
@@ -18,20 +20,21 @@ set cpo&vim
1820

1921
if !exists('*VimFtpluginUndo')
2022
func VimFtpluginUndo()
21-
setl fo< isk< com< tw< commentstring< include< define<
23+
setl fo< isk< com< tw< commentstring< include< define< keywordprg<
24+
sil! delc -buffer VimKeywordPrg
2225
if exists('b:did_add_maps')
2326
silent! nunmap <buffer> [[
24-
silent! vunmap <buffer> [[
27+
silent! xunmap <buffer> [[
2528
silent! nunmap <buffer> ]]
26-
silent! vunmap <buffer> ]]
29+
silent! xunmap <buffer> ]]
2730
silent! nunmap <buffer> []
28-
silent! vunmap <buffer> []
31+
silent! xunmap <buffer> []
2932
silent! nunmap <buffer> ][
30-
silent! vunmap <buffer> ][
33+
silent! xunmap <buffer> ][
3134
silent! nunmap <buffer> ]"
32-
silent! vunmap <buffer> ]"
35+
silent! xunmap <buffer> ]"
3336
silent! nunmap <buffer> ["
34-
silent! vunmap <buffer> ["
37+
silent! xunmap <buffer> ["
3538
endif
3639
unlet! b:match_ignorecase b:match_words b:match_skip b:did_add_maps
3740
endfunc
@@ -48,7 +51,49 @@ setlocal fo-=t fo+=croql
4851
setlocal isk+=#
4952

5053
" Use :help to lookup the keyword under the cursor with K.
51-
setlocal keywordprg=:help
54+
" Distinguish between commands, options and functions.
55+
if !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
94+
endif
95+
command! -buffer -nargs=1 VimKeywordPrg :exe 'help' s:Help(<q-args>)
96+
setlocal keywordprg=:VimKeywordPrg
5297

5398
" Comments starts with # in Vim9 script. We have to guess which one to use.
5499
if "\n" .. getline(1, 32)->join("\n") =~# '\n\s*vim9\%[script]\>'
@@ -77,19 +122,19 @@ if !exists("no_plugin_maps") && !exists("no_vim_maps")
77122

78123
" Move around functions.
79124
nnoremap <silent><buffer> [[ m':call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
80-
vnoremap <silent><buffer> [[ m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
125+
xnoremap <silent><buffer> [[ m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
81126
nnoremap <silent><buffer> ]] m':call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
82-
vnoremap <silent><buffer> ]] m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
127+
xnoremap <silent><buffer> ]] m':<C-U>exe "normal! gv"<Bar>call search('^\s*\(fu\%[nction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
83128
nnoremap <silent><buffer> [] m':call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
84-
vnoremap <silent><buffer> [] m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
129+
xnoremap <silent><buffer> [] m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "bW")<CR>
85130
nnoremap <silent><buffer> ][ m':call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
86-
vnoremap <silent><buffer> ][ m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
131+
xnoremap <silent><buffer> ][ m':<C-U>exe "normal! gv"<Bar>call search('^\s*end\(f\%[unction]\\|\(export\s\+\)\?def\)\>', "W")<CR>
87132
88133
" Move around comments
89134
nnoremap <silent><buffer> ]" :call search('\%(^\s*".*\n\)\@<!\%(^\s*"\)', "W")<CR>
90-
vnoremap <silent><buffer> ]" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\@<!\%(^\s*"\)', "W")<CR>
135+
xnoremap <silent><buffer> ]" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\@<!\%(^\s*"\)', "W")<CR>
91136
nnoremap <silent><buffer> [" :call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
92-
vnoremap <silent><buffer> [" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
137+
xnoremap <silent><buffer> [" :<C-U>exe "normal! gv"<Bar>call search('\%(^\s*".*\n\)\%(^\s*"\)\@!', "bW")<CR>
93138
endif
94139

95140
" Let the matchit plugin know what items can be matched.
@@ -101,27 +146,29 @@ if exists("loaded_matchit")
101146
" func name
102147
" require a parenthesis following, then there can be an "endfunc".
103148
let b:match_words =
104-
\ '\<\%(fu\%[nction]\|def\)!\=\s\+\S\+\s*(:\%(\%(^\||\)\s*\)\@<=\<retu\%[rn]\>:\%(\%(^\||\)\s*\)\@<=\<\%(endf\%[unction]\|enddef\)\>,' ..
105-
\ '\<\%(wh\%[ile]\|for\)\>:\%(\%(^\||\)\s*\)\@<=\<brea\%[k]\>:\%(\%(^\||\)\s*\)\@<=\<con\%[tinue]\>:\%(\%(^\||\)\s*\)\@<=\<end\%(w\%[hile]\|fo\%[r]\)\>,' ..
106-
\ '\<if\>:\%(\%(^\||\)\s*\)\@<=\<el\%[seif]\>:\%(\%(^\||\)\s*\)\@<=\<en\%[dif]\>,' ..
107-
\ '{:},' ..
108-
\ '\<try\>:\%(\%(^\||\)\s*\)\@<=\<cat\%[ch]\>:\%(\%(^\||\)\s*\)\@<=\<fina\%[lly]\>:\%(\%(^\||\)\s*\)\@<=\<endt\%[ry]\>,' ..
109-
\ '\<aug\%[roup]\s\+\%(END\>\)\@!\S:\<aug\%[roup]\s\+END\>,' ..
110-
\ '\<class\>:\<endclass\>,' ..
111-
\ '\<interface\>:\<endinterface\>,' ..
112-
\ '\<enum\>:\<endenum\>'
149+
\ '\<\%(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\>'
113158

114159
" Ignore syntax region commands and settings, any 'en*' would clobber
115160
" if-endif.
116161
" - set spl=de,en
117162
" - au! FileType javascript syntax region foldBraces start=/{/ end=/}/ …
118163
" Also ignore here-doc and dictionary keys (vimVar).
119164
let b:match_skip = 'synIDattr(synID(line("."), col("."), 1), "name")
120-
\ =~? "comment\\|string\\|vimSynReg\\|vimSet\\|vimLetHereDoc\\|vimVar"'
165+
\ =~? "comment\\|string\\|vimSynReg\\|vimSet\\|vimLetHereDoc\\|vimVar"'
121166
endif
122167

123168
let &cpo = s:cpo_save
124169
unlet s:cpo_save
125170

126171
" removed this, because 'cpoptions' is a global option.
127172
" setlocal cpo+=M " makes \%( match \)
173+
"
174+
" vim: sw=2 et

0 commit comments

Comments
 (0)