1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
vim9script
# Language: Vim9 script
# Contributers: @lacygoill
# Shane-XB-Qian
# Andrew Radev
# thinca
# Last Change: 2026 Feb 10
#
# Vim script to handle jumping to the targets of several types of Vim commands
# (:import, :packadd, :runtime, :colorscheme), and to autoloaded functions of
# the style <path>#<function_name>.
#
# see runtime/ftplugin/vim.vim
# Interface {{{1
export def Find(editcmd: string) #{{{2
var curline: string = getline('.')
if curline =~ '^\s*\%(:\s*\)\=\%(sil\%[ent]!\=\s\+\)\=packadd!\=\s'
HandlePackaddLine(editcmd, curline)
return
endif
if curline =~ '^\s*\%(:\s*\)\=\%(sil\%[ent]!\=\s\+\)\=ru\%[ntime]!\='
HandleRuntimeLine(editcmd, curline, expand('<cfile>'))
return
endif
if curline =~ '^\s*\%(:\s*\)\=\%(sil\%[ent]!\=\s\+\)\=colo\%[rscheme]\s'
HandleColoLine(editcmd, curline)
return
endif
if curline =~ '^\s*\%(:\s*\)\=import\s'
HandleImportLine(editcmd, curline)
return
endif
var curfunc = FindCurfunc()
if stridx(curfunc, '#') >= 0
var parts = split(curfunc, '#')
var path = $"autoload/{join(parts[0 : -2], '/')}.vim"
var resolved_path = globpath(&runtimepath, path, 1, 1)
if !resolved_path->empty()
var function_pattern: string = $'^\s*\%(:\s*\)\=fun\%[ction]!\=\s\+\zs{curfunc}('
resolved_path->Open(editcmd, function_pattern)
endif
return
endif
try
execute 'normal! ' .. editcmd
catch
Error(v:exception)
endtry
enddef
#}}}1
# Core {{{1
def HandlePackaddLine(editcmd: string, curline: string) #{{{2
var pat: string = '\s*\%(:\s*\)\=packadd!\=\s\+\zs\S\+\>\ze'
var plugin: string = curline
->matchstr(pat)
->substitute('^vim-\|\.vim$', '', 'g')
if plugin == ''
Fallback(editcmd)
else
var files: list<string> = getcompletion($'plugin/{plugin}', 'runtime')
->map((_, fname: string) => fname->findfile(&rtp)->fnamemodify(':p'))
->filter((_, path: string): bool => filereadable(path))
if empty(files)
echo 'Could not find any plugin file for ' .. string(plugin)
return
endif
files->Open(editcmd)
endif
enddef
def HandleRuntimeLine(editcmd: string, curline: string, cfile: string) #{{{2
var fname: string
var where_pat: string = '\%(START\|OPT\|PACK\|ALL\)'
if cfile == 'runtime' || cfile =~# $'^{where_pat}$'
# then the cursor was not on one of the filenames, jump to the first file:
var fname_pat: string = $'\s*\%(:\s*\)\=ru\%[ntime]\%(!\s*\|\s\+\)\%({where_pat}\s\+\)\=\zs\S\+\>\ze'
fname = curline->matchstr(fname_pat)
else
fname = cfile
endif
if fname == ''
Fallback(editcmd)
else
var file: string = fname
->findfile(&rtp)
->fnamemodify(':p')
if file == '' || !filereadable(file)
echo 'Could not be found in the runtimepath: ' .. string(fname)
return
endif
file->Open(editcmd)
endif
enddef
def HandleColoLine(editcmd: string, curline: string) #{{{2
var pat: string = '\s*\%(:\s*\)\=colo\%[rscheme]\s\+\zs\S\+\>\ze'
var colo: string = curline->matchstr(pat)
if colo == ''
Fallback(editcmd)
else
var files: list<string> = getcompletion($'colors/{colo}', 'runtime')
->map((_, fname: string) => fname->findfile(&rtp)->fnamemodify(':p'))
->filter((_, path: string): bool => filereadable(path))
if empty(files)
echo 'Could not find any colorscheme file for ' .. string(colo)
return
endif
files->Open(editcmd)
endif
enddef
def HandleImportLine(editcmd: string, curline: string) #{{{2
var fname: string
var import_cmd: string = '^\s*\%(:\s*\)\=import\s\+\%(autoload\s\+\)\='
var import_alias: string = '\%(\s\+as\s\+\w\+\)\=$'
var import_string: string = import_cmd .. '\([''"]\)\zs.*\ze\1' .. import_alias
var import_expr: string = import_cmd .. '\zs.*\ze' .. import_alias
# the script is referred to by its name in a quoted string
if curline =~ import_string
fname = curline->matchstr(import_string)
# the script is referred to by an expression
elseif curline =~ import_expr
try
sandbox fname = curline
->matchstr(import_expr)
->eval()
catch
Error(v:exception)
return
endtry
endif
var filepath: string
if fname->isabsolutepath()
filepath = fname
elseif fname[0] == '.'
filepath = (expand('%:h') .. '/' .. fname)->simplify()
else
var subdir: string = curline =~ '^\s*import\s\+autoload\>' ? 'autoload' : 'import'
# Matching patterns in `'wildignore'` can be slow.
# Let's set `{nosuf}` to `true` to avoid `globpath()` to be slow.
filepath = globpath(&runtimepath, subdir .. '/' .. fname, true, true)
->get(0, '')
endif
if !filepath->filereadable()
printf('E447: Can''t find file "%s" in path', fname)
->Error()
return
endif
var how_to_split: string = {
gF: 'edit',
"\<C-W>F": 'split',
"\<C-W>gF": 'tab split',
}[editcmd]
execute how_to_split .. ' ' .. filepath
enddef
def Open(target: any, editcmd: string, search_pattern: string = '') #{{{2
var split: string = editcmd[0] == 'g' ? 'edit' : editcmd[1] == 'g' ? 'tabedit' : 'split'
var fname: string
var cmd: string
if target->typename() == 'list<string>'
if target->empty()
return
endif
fname = target[0]
else
if target->typename() != 'string'
return
endif
fname = target
endif
if search_pattern != ''
var escaped_pattern = escape(search_pattern, '\#'' ')
cmd = $'+silent\ call\ search(''{escaped_pattern}'')'
endif
execute $'{split} {cmd} {fname}'
# If there are several files to open, put them into an arglist.
if target->typename() == 'list<string>'
&& target->len() > 1
var arglist: list<string> = target
->copy()
->map((_, f: string) => f->fnameescape())
execute $'arglocal {arglist->join()}'
endif
enddef
#}}}1
# Util {{{1
def Error(msg: string) #{{{2
echohl ErrorMsg
echomsg msg
echohl NONE
enddef
def Fallback(editcmd: string) #{{{2
try
execute 'normal! ' .. editcmd .. 'zv'
catch
Error(v:exception)
endtry
enddef
def FindCurfunc(): string #{{{2
var curfunc = ''
var saved_iskeyword = &iskeyword
try
set iskeyword+=#
curfunc = expand('<cword>')
finally
&iskeyword = saved_iskeyword
endtry
return curfunc
enddef
# vim: sw=4 et
|