runtime(netrw): simplify gx file handling

It did not work very well, at least on Debian 12, and I am not sure Git
Bash and WSL, for example, were taken care of as maintenance stalled.

The whole logic was somewhat convoluted with some parts repeatedly invoking
failed commands.

The file handling was outdated, for example, nowadays Netscape is rarely
used, and also opinionated, for example mainly Microsoft Paint and Gimp for
Image files.

Instead, let's use (xdg-)open and similar commands on other systems
which respects the user's preferences.

closes: #15721

Co-authored-by: Luca Saccarola <96259932+saccarosium@users.noreply.github.com>
Signed-off-by: Konfekt <Konfekt@users.noreply.github.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Konfekt
2024-10-27 22:16:49 +01:00
committed by Christian Brabandt
parent a04003a929
commit 3d7e567ea7
4 changed files with 200 additions and 127 deletions

View File

@ -1,9 +1,10 @@
" netrwPlugin.vim: Handles file transfer and remote directory listing across a network
" PLUGIN SECTION
" Maintainer: This runtime file is looking for a new maintainer.
" Date: Feb 09, 2021
" Date: Sep 09, 2021
" Last Change:
" 2024 May 08 by Vim Project: cleanup legacy Win9X checks
" 2024 Oct 27 by Vim Project: cleanup gx mapping
" Former Maintainer: Charles E Campbell
" GetLatestVimScripts: 1075 1 :AutoInstall: netrw.vim
" Copyright: Copyright (C) 1999-2021 Charles E. Campbell {{{1
@ -31,6 +32,87 @@ set cpo&vim
" ---------------------------------------------------------------------
" Public Interface: {{{1
" Commands Launch/URL {{{2
" surpress output of command; use bang for GUI applications
" set up redirection (avoids browser messages)
" by default, g:netrw_suppress_gx_mesg is true
if get(g:, ':netrw_suppress_gx_mesg', 1)
if &srr =~# "%s"
let s:redir = printf(&srr, has("win32") ? "nul" : "/dev/null")
else
let s:redir= &srr .. (has("win32") ? "nul" : "/dev/null")
endif
else
let s:redir= ""
endif
if has('unix')
if has('win32unix')
" If cygstart provided, then assume Cygwin and use cygstart --hide; see man cygstart.
if executable('cygstart')
command -complete=shellcmd -nargs=1 -bang Launch
\ exe 'silent ! cygstart --hide' trim(<q-args>) s:redir | redraw!
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/
command -complete=shellcmd -nargs=1 -bang Launch
\ exe 'silent !start "" //b' trim(<q-args>) s:redir | redraw!
else
" imitate /usr/bin/start script for other environments and hope for the best
command -complete=shellcmd -nargs=1 -bang Launch
\ exe 'silent !cmd //c start "" //b' trim(<q-args>) s:redir | redraw!
endif
elseif exists('$WSL_DISTRO_NAME') " use cmd.exe to start GUI apps in WSL
command -complete=shellcmd -nargs=1 -bang Launch execute ':silent !'..
\ ((<q-args> =~? '\v<\f+\.(exe|com|bat|cmd)>') ?
\ 'cmd.exe /c start "" /b' trim(<q-args>) :
\ 'nohup ' trim(<q-args>) s:redir '&')
\ | redraw!
else
command -complete=shellcmd -nargs=1 -bang Launch
\ exe ':silent ! nohup' trim(<q-args>) s:redir '&' | redraw!
endif
elseif has('win32')
command -complete=shellcmd -nargs=1 -bang Launch
\ exe 'silent !'.. (&shell =~? '\<cmd\.exe\>' ? '' : 'cmd.exe /c')
\ 'start /b ' trim(<q-args>) s:redir | redraw!
endif
if exists(':Launch') == 2
" Git Bash
if has('win32unix')
" start suffices
let s:cmd = ''
" Windows / WSL
elseif executable('explorer.exe')
let s:cmd = 'explorer.exe'
" Linux / BSD
elseif executable('xdg-open')
let s:cmd = 'xdg-open'
" MacOS
elseif executable('open')
let s:cmd = 'open'
else
s:cmd = ''
endif
function s:Open(cmd, file)
if empty(a:cmd) && !exists('g:netrw_browsex_viewer')
echoerr "No program to open this path found. See :help Open for more information."
else
Launch cmd shellescape(a:file, 1)
endif
endfunction
command -complete=file -nargs=1 Open call s:Open(s:cmd, <q-args>)
endif
if !exists('g:netrw_regex_url')
let g:netrw_regex_url = '\%(\%(http\|ftp\|irc\)s\?\|file\)://\S\{-}'
endif
" " }}}
" Local Browsing Autocmds: {{{2
augroup FileExplorer
au!