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
|
vim9script
# Vim runtime support library
#
# Maintainer: The Vim Project <https://github.com/vim/vim>
# Last Change: 2025 Dec 21
export def IsSafeExecutable(filetype: string, executable: string): bool
if empty(exepath(executable))
return v:false
endif
var cwd = getcwd()
return get(g:, filetype .. '_exec', get(g:, 'plugin_exec', 0))
&& (fnamemodify(exepath(executable), ':p:h') !=# cwd
|| (split($PATH, has('win32') ? ';' : ':')->index(cwd) != -1
&& cwd != '.'))
enddef
def Redir(): string
if get(g:, 'netrw_suppress_gx_mesg', true)
if &srr =~# "%s"
return printf(&srr, has("win32") ? "nul" : "/dev/null")
elseif &srr =~# '>&\?$'
return &srr .. (has("win32") ? "nul" : "/dev/null")
else
return &srr .. (has("win32") ? "> nul" : "> /dev/null")
endif
endif
return ''
enddef
if has('unix')
if has('win32unix')
# Cygwin provides cygstart
if executable('cygstart')
export def Launch(args: string)
execute $'silent ! cygstart --hide {args} {Redir()}' | redraw!
enddef
elseif !empty($MSYSTEM) && executable('start')
# MSYS2/Git Bash comes by default without cygstart; see
# https://www.msys2.org/wiki/How-does-MSYS2-differ-from-Cygwin
# Instead it provides /usr/bin/start script running `cmd.exe //c start`
# Adding "" //b` sets void title, hides cmd window and blocks path conversion
# of /b to \b\ " by MSYS2; see https://www.msys2.org/docs/filesystem-paths/
export def Launch(args: string)
execute $'silent !start "" //b {args} {Redir()}' | redraw!
enddef
else
# imitate /usr/bin/start script for other environments and hope for the best
export def Launch(args: string)
execute $'silent !cmd /c start "" /b {args} {Redir()}' | redraw!
enddef
endif
elseif exists('$WSL_DISTRO_NAME') # use cmd.exe to start GUI apps in WSL
export def Launch(args: string)
const command = (args =~? '\v<\f+\.(exe|com|bat|cmd)>')
? $'cmd.exe /c start /b {args} {Redir()}'
: $'nohup {args} {Redir()} &'
execute $'silent ! {command}' | redraw!
enddef
else
export def Launch(args: string)
const fork = has('gui_running') ? '&' : ''
execute $':silent ! nohup {args} {Redir()} {fork}' | redraw!
enddef
endif
elseif has('win32')
export def Launch(args: string)
try
execute ':silent !start' args | redraw!
catch /^Vim(!):E371:/
echohl ErrorMsg
echom "dist#vim9#Launch(): can not start" args
echohl None
endtry
enddef
else
export def Launch(dummy: string)
echom 'No common launcher found'
enddef
endif
var os_viewer = null_string
# Git Bash
if has('win32unix')
# (cyg)start suffices
os_viewer = ''
# Windows
elseif has('win32')
os_viewer = '' # Use :!start
# WSL
elseif executable('explorer.exe')
os_viewer = 'explorer.exe'
# Linux / BSD
elseif executable('xdg-open')
os_viewer = 'xdg-open'
# MacOS
elseif executable('open')
os_viewer = 'open'
endif
def Viewer(): string
# g:Openprg could be a string of program + its arguments, test if first
# argument is executable
var user_viewer = get(g:, "Openprg", get(g:, "netrw_browsex_viewer", ""))
# Take care of an off-by-one check for "for" too
if executable(trim(user_viewer))
return user_viewer
endif
var args = split(user_viewer, '\s\+\zs')
var viewer = get(args, 0, '')
for arg in args[1 :]
if executable(trim(viewer))
return user_viewer
endif
viewer ..= arg
endfor
if os_viewer == null
echoerr "No program to open this path found. See :help Open for more information."
endif
return os_viewer
enddef
export def Open(file: string)
# disable shellslash for shellescape, required on Windows #17995
if exists('+shellslash') && &shellslash
&shellslash = false
defer setbufvar('%', '&shellslash', true)
endif
if &shell == 'pwsh' || &shell == 'powershell'
const shell = &shell
setlocal shell&
defer setbufvar('%', '&shell', shell)
endif
Launch($"{Viewer()} {shellescape(file, 1)}")
enddef
# Uncomment this line to check for compilation errors early
# defcompile
# vim: ts=8 sts=2 sw=2 et
|