fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
d19c92776
master
1# localization: tier1 2function help --description'Show help for the fish shell' 3set -l options h/help 4argparse -n help$options --$argv 5or return 6 7if set -q _flag_help 8__fish_print_help help 9return 10end 11 12set -l fish_help_item$argv [ 1 ] 13if test ( count $argv ) -gt 1 14if test $argv [ 1 ] = string 15set fish_help_item( string join --'-' $argv [ 1 ] $argv [ 2 ]) 16else 17echo "help: Expected at most 1 args, got 2" >&2 18return 1 19end 20end 21 22# Find a suitable browser for viewing the help pages. 23# The first thing we try is $fish_help_browser. 24set -l fish_browser$fish_help_browser 25 26# A list of graphical browsers we know about. 27set -l graphical_browsers( printf %s"\ 28htmlview 29x-www-browser 30firefox 31galeon 32mozilla 33xdg-open 34konqueror 35epiphany 36opera 37netscape 38rekonq 39google-chrome 40chromium-browser 41" ) 42 43# On mac we may have to write a temporary file that redirects to the desired 44# help page, since `open` will drop fragments from file URIs (issue #4480). 45set -l need_trampoline false 46 47if not set -q fish_browser[ 1 ] 48if set -q BROWSER 49# User has manually set a preferred browser, so we respect that 50echo $BROWSER | read -at fish_browser 51if not type -q$fish_browser [ 1 ] 52printf ( _ 'help: %s is not a valid command: %s\n' ) '$fish_browser' " $fish_browser " 53return 2 54end 55else 56# No browser set up, inferring. 57# We check a bunch and use the last we find. 58 59# Check for a text-based browser. 60for iin htmlview www-browser links elinks lynx w3m 61if type -q -f$i 62set fish_browser$i 63break 64end 65end 66 67# If we are in a graphical environment, check if there is a graphical 68# browser to use instead. 69set -l is_graphical false 70if test -n " $DISPLAY " && 71{ test -z" $XAUTHORITY " || 72test " $XAUTHORITY " = " $HOME /.Xauthority" 73} 74set is_graphical true 75for i in $graphical_browsers 76if type -q -f $i 77set fish_browser $i 78break 79end 80end 81end 82 83# If we're SSH'd into a desktop installation, don't use a regular browser unless X is being forwarded 84if not set -q SSH_CLIENT || $is_graphical 85# We use the macOS open, but not otherwise. 86# On Debian, there is an open command that's a symlink to openvt. 87if uname | string match -q Darwin && command -sq open 88set fish_browser open 89# The open command needs a trampoline because the macOS version can't handle #-fragments. 90set need_trampoline true 91end 92 93# If the OS appears to be Windows (graphical), try to use cygstart 94if type -q cygstart 95set fish_browser cygstart 96else if type -q xdg-open 97# If xdg-open is available, just use that 98set fish_browser xdg-open 99end 100 101# Try to find cmd.exe via $PATH or one of the paths that it's often at. 102# 103# We use this instead of xdg-open because that's useless without a backend 104# like wsl-open which we'll check in a minute. 105if test -f /proc/version 106and string match -riq 'Microsoft|WSL|MSYS|MINGW' < /proc/version 107and set -l cmd ( command -s powershell.exe cmd.exe /mnt/c/Windows/System32/cmd.exe) 108# Use the first of these. 109set fish_browser $cmd [ 1 ] 110end 111 112if type -q wsl-open 113set fish_browser wsl-open 114end 115end 116end 117else 118if not type -q $fish_browser [ 1 ] 119printf ( _ 'help: %s is not a valid command: %s\n' ) '$fish_help_browser' " $fish_browser " 120return 2 121end 122end 123 124# In Cygwin, start the user-specified browser using cygstart, 125# only if a Windows browser is to be used. 126if type -q cygstart 127if test " $fish_browser " != cygstart 128and set -q fish_browser[ 1 ] 129and not command -sq $fish_browser [ 1 ] 130# Escaped quotes are necessary to work with spaces in the path 131# when the command is finally eval'd. 132set fish_browser cygstart $fish_browser 133else 134set need_trampoline true 135end 136end 137 138set fish_help_item ( string replace -r -- '\b#$' '' $fish_help_item ) 139set -l fish_help_page 140switch " $fish_help_item " 141case '' 142set fish_help_page index.html 143case ( status get-file help_sections | string replace -r "^index(#|\$)" introduction \$ 1 ) 144set fish_help_page ( 145printf %s $fish_help_item | 146string replace -r '^introduction(#|$)' 'index$1' | 147string replace -r -- '(#|$)' '.html$1' 148) 149 150case {cmds/,} "!" 151set fish_help_page cmds/not.html 152case {cmds/,} "." 153set fish_help_page cmds/source.html 154case {cmds/,} ":" 155set fish_help_page cmds/true.html 156case {cmds/,} "[" 157set fish_help_page cmds/test.html 158case {cmds/,} "{" 159set fish_help_page cmds/begin.html 160case ( __fish_print_commands ) 161set fish_help_page cmds/ $fish_help_item .html 162 163case "*" 164printf ( _ "%s: no fish help topic '%s', try 'man %s'\n" ) help $fish_help_item $fish_help_item 165return 1 166end 167 168# In Crostini Chrome OS Linux, the default browser opens URLs in Chrome running outside the 169# linux VM. This browser does not have access to the Linux filesystem. This uses Garcon, see e.g. 170# https://chromium.googlesource.com/chromiumos/platform2/+/master/vm_tools/garcon/#opening-urls 171# https://source.chromium.org/search?q=garcon-url-handler 172string match -q '*garcon-url-handler*' $fish_browser [ 1 ] 173and set -l chromeos_linux_garcon 174 175# Generate the online URL, with one dot in the version string (major.minor) 176set -l version_string ( string match -rg '(\d+\.\d+(?:b\d+)?).*' -- $version ) 177set -l ext_url https://fishshell.com/docs/ $version_string / $fish_help_page 178set -l page_url 179if set -q __fish_help_dir[ 1 ]; and test -f $__fish_help_dir /index.html; and not set -lq chromeos_linux_garcon 180# Help is installed, use it 181set page_url file:// $__fish_help_dir / $fish_help_page 182 183# For Windows (Cygwin, msys2 and WSL), we need to convert the base 184# help dir to a Windows path before converting it to a file URL 185# but only if a Windows browser is being used 186if type -q cygpath 187and string match -qr '(cygstart|\.exe)(\s+|$)' $fish_browser [ 1 ] 188set page_url file://( cygpath -m $__fish_help_dir )/ $fish_help_page 189else if type -q wslpath 190and string match -qr '\.exe(\s+|$)' $fish_browser [ 1 ] 191set page_url file://( wslpath -w $__fish_help_dir )/ $fish_help_page 192end 193else 194set page_url $ext_url 195# We don't need a trampoline for a remote URL. 196set need_trampoline false 197end 198 199if not set -q fish_browser[ 1 ] 200printf ( _ '%s: Could not find a web browser.\n' ) help >&2 201printf ( _ 'Please try `BROWSER=some_browser help`, `man fish-doc`, or `man fish-tutorial`.\n\n' ) >&2 202printf ( _ 'Or open %s in your browser of choice.\n' ) $ext_url >&2 203return 1 204end 205 206if $need_trampoline && string match -rq '#' $page_url 207# Write a temporary file that will redirect where we want. 208set -l tmpdir ( __fish_mktemp_relative -d fish-help) 209or return 1 210set -l tmpname $tmpdir /help.html 211echo '<meta http-equiv="refresh" content="0;URL=\'' $page_url '\'" />' > $tmpname 212set page_url file:// $tmpname 213 214# For Windows (Cygwin, msys2 and WSL), we need to convert the base help dir to a Windows path 215# before converting it to a file URL, but only if a Windows browser is being used 216if type -q cygpath 217and string match -qr '(cygstart|\.exe)(\s+|$)' $fish_browser [ 1 ] 218set page_url file://( cygpath -m $tmpname ) 219else if type -q wslpath 220and string match -qr '\.exe(\s+|$)' $fish_browser [ 1 ] 221set page_url file://( wslpath -w $tmpname ) 222end 223end 224 225printf ( _ 'help: Help is being displayed in %s.\n' ) $fish_browser [ 1 ] 226# cmd.exe and powershell needs more coaxing. 227if string match -qr 'powershell\.exe$|cmd\.exe$' -- $fish_browser [ 1 ] 228# The space before the /c is to prevent msys2 from expanding it to a path 229$fish_browser " /c" start $page_url 230else if contains -- $fish_browser [ 1 ] $graphical_browsers 231set -l sh ( __fish_posix_shell ) 232$sh -c '( "$@" ) &' -- $fish_browser $page_url 233else 234$fish_browser $page_url 235end 236 237# Show the online URL anyway in case we did not manage to open something successfully. 238# (we can't check because we need to background it) 239printf ( _ 'help: If no help could be displayed, go to %s to view the documentation online.\n' ) $ext_url 240end