function help --description 'Show help for the fish shell' set -l options h/help argparse -n help $options -- $argv or return if set -q _flag_help __fish_print_help help return 0 end set -l fish_help_item $argv[1] if test (count $argv) -gt 1 if string match -q string $argv[1] set fish_help_item (string join '-' $argv[1] $argv[2]) else echo "help: Expected at most 1 args, got 2" >&2 return 1 end end # Find a suitable browser for viewing the help pages. # The first thing we try is $fish_help_browser. set -l fish_browser $fish_help_browser # A list of graphical browsers we know about. set -l graphical_browsers htmlview x-www-browser firefox galeon mozilla xdg-open set -a graphical_browsers konqueror epiphany opera netscape rekonq google-chrome chromium-browser # On mac we may have to write a temporary file that redirects to the desired # help page, since `open` will drop fragments from file URIs (issue #4480). set -l need_trampoline if not set -q fish_browser[1] if set -q BROWSER # User has manually set a preferred browser, so we respect that echo $BROWSER | read -at fish_browser else # No browser set up, inferring. # We check a bunch and use the last we find. # Check for a text-based browser. for i in htmlview www-browser links elinks lynx w3m if type -q -f $i set fish_browser $i break end end # If we are in a graphical environment, check if there is a graphical # browser to use instead. set -f is_graphical 0 if test -n "$DISPLAY" -a \( "$XAUTHORITY" = "$HOME/.Xauthority" -o -z "$XAUTHORITY" \) set is_graphical 1 for i in $graphical_browsers if type -q -f $i set fish_browser $i break end end end # If we're SSH'd into a desktop installation, don't use a regular browser unless X is being forwarded if not set -q SSH_CLIENT || test $is_graphical -eq 1 # We use the macOS open, but not otherwise. # On Debian, there is an open command that's a symlink to openvt. if uname | string match -q Darwin && command -sq open set fish_browser open # The open command needs a trampoline because the macOS version can't handle #-fragments. set need_trampoline 1 end # If the OS appears to be Windows (graphical), try to use cygstart if type -q cygstart set fish_browser cygstart else if type -q xdg-open # If xdg-open is available, just use that set fish_browser xdg-open end # Try to find cmd.exe via $PATH or one of the paths that it's often at. # # We use this instead of xdg-open because that's useless without a backend # like wsl-open which we'll check in a minute. if test -f /proc/version and string match -riq 'Microsoft|WSL|MSYS|MINGW' &2 printf (_ 'Please try `BROWSER=some_browser help`, `man fish-doc`, or `man fish-tutorial`.\n\n') >&2 printf (_ 'Or open %s in your browser of choice.\n') $ext_url >&2 return 1 end if set -q need_trampoline[1] # If string replace doesn't replace anything, we don't actually need a # trampoline (they're only needed if there's a fragment in the path) if set -l clean_url (string match -re '#' $page_url) # Write a temporary file that will redirect where we want. set -q TMPDIR or set -l TMPDIR /tmp set -l tmpdir (mktemp -d $TMPDIR/help.XXXXXX) or return 1 set -l tmpname $tmpdir/help.html echo '' >$tmpname set page_url file://$tmpname # For Windows (Cygwin, msys2 and WSL), we need to convert the base help dir to a Windows path # before converting it to a file URL, but only if a Windows browser is being used if type -q cygpath and string match -qr '(cygstart|\.exe)(\s+|$)' $fish_browser[1] set page_url file://(cygpath -m $tmpname) else if type -q wslpath and string match -qr '\.exe(\s+|$)' $fish_browser[1] set page_url file://(wslpath -w $tmpname) end end end printf (_ 'help: Help is being displayed in %s.\n') $fish_browser[1] # cmd.exe and powershell needs more coaxing. if string match -qr 'powershell\.exe$|cmd\.exe$' -- $fish_browser[1] # The space before the /c is to prevent msys2 from expanding it to a path $fish_browser " /c" start $page_url else if contains -- $fish_browser[1] $graphical_browsers $fish_browser $page_url & disown $last_pid >/dev/null 2>&1 else $fish_browser $page_url end # Show the online URL anyway in case we did not manage to open something successfully. # (we can't check because we need to background it) printf (_ 'help: If no help could be displayed, go to %s to view the documentation online.\n') $ext_url end