2012-12-28 06:04:11 +08:00
|
|
|
function help --description 'Show help for the fish shell'
|
2020-03-10 02:36:12 +08:00
|
|
|
set -l options h/help
|
2020-03-21 22:40:14 +08:00
|
|
|
argparse -n help $options -- $argv
|
2017-07-14 04:49:15 +08:00
|
|
|
or return
|
2016-11-28 13:27:22 +08:00
|
|
|
|
2017-07-14 04:49:15 +08:00
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help help
|
|
|
|
return 0
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
|
|
|
|
2017-07-14 04:49:15 +08:00
|
|
|
set -l fish_help_item $argv[1]
|
2020-03-21 22:40:14 +08:00
|
|
|
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"
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-22 19:40:39 +08:00
|
|
|
# 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
|
|
|
|
set -a graphical_browsers konqueror epiphany opera netscape rekonq google-chrome chromium-browser
|
2016-11-28 13:27:22 +08:00
|
|
|
|
2018-01-13 07:02:13 +08:00
|
|
|
# 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
|
|
|
|
|
2019-03-22 19:40:39 +08:00
|
|
|
if not set -q fish_browser[1]
|
2016-11-28 13:27:22 +08:00
|
|
|
if set -q BROWSER
|
|
|
|
# User has manually set a preferred browser, so we respect that
|
2019-12-03 19:18:39 +08:00
|
|
|
echo $BROWSER | read -at fish_browser
|
2016-11-28 13:27:22 +08:00
|
|
|
else
|
2019-03-22 19:40:39 +08:00
|
|
|
# No browser set up, inferring.
|
|
|
|
# We check a bunch and use the last we find.
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# Check for a text-based browser.
|
2019-03-22 19:40:39 +08:00
|
|
|
for i in htmlview www-browser links elinks lynx w3m
|
2016-11-28 13:27:22 +08:00
|
|
|
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.
|
2019-03-22 19:40:39 +08:00
|
|
|
if test -n "$DISPLAY" -a \( "$XAUTHORITY" = "$HOME/.Xauthority" -o "$XAUTHORITY" = "" \)
|
2016-11-28 13:27:22 +08:00
|
|
|
for i in $graphical_browsers
|
|
|
|
if type -q -f $i
|
|
|
|
set fish_browser $i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 16:03:17 +08:00
|
|
|
# If we have an open _command_ we use it - otherwise it's our function,
|
|
|
|
# which might not have a backend to use.
|
|
|
|
# Note that we prefer xdg-open, because this open might also be a symlink to "openvt"
|
|
|
|
# like it is on Debian.
|
|
|
|
if 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
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# If the OS appears to be Windows (graphical), try to use cygstart
|
|
|
|
if type -q cygstart
|
|
|
|
set fish_browser cygstart
|
2020-01-14 03:34:22 +08:00
|
|
|
# If xdg-open is available, just use that
|
2020-03-13 00:27:37 +08:00
|
|
|
else if type -q xdg-open
|
2016-11-28 13:27:22 +08:00
|
|
|
set fish_browser xdg-open
|
|
|
|
end
|
2019-03-23 00:24:10 +08:00
|
|
|
|
|
|
|
# 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.
|
2020-03-24 03:57:23 +08:00
|
|
|
if test -f /proc/version
|
2020-06-25 00:36:37 +08:00
|
|
|
and string match -riq 'Microsoft|WSL|MSYS|MINGW' </proc/version
|
2020-01-14 03:34:22 +08:00
|
|
|
and set -l cmd (command -s cmd.exe /mnt/c/Windows/System32/cmd.exe)
|
2019-03-23 00:24:10 +08:00
|
|
|
# Use the first of these.
|
|
|
|
set fish_browser $cmd[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
if type -q wsl-open
|
|
|
|
set fish_browser wsl-open
|
|
|
|
end
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-19 04:48:41 +08:00
|
|
|
if not set -q fish_browser[1]
|
2016-11-28 13:27:22 +08:00
|
|
|
printf (_ '%s: Could not find a web browser.\n') help
|
2021-03-11 14:15:05 +08:00
|
|
|
printf (_ 'Please try `BROWSER=some_browser help`, `man fish-doc`, or `man fish-tutorial`.\n\n')
|
2016-11-28 13:27:22 +08:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2019-11-29 13:09:58 +08:00
|
|
|
# In Cygwin, start the user-specified browser using cygstart,
|
|
|
|
# only if a Windows browser is to be used.
|
2016-11-28 13:27:22 +08:00
|
|
|
if type -q cygstart
|
2020-03-10 02:36:12 +08:00
|
|
|
if test $fish_browser != cygstart
|
2020-01-14 03:34:22 +08:00
|
|
|
and not command -sq $fish_browser[1]
|
2016-11-28 13:27:22 +08:00
|
|
|
# Escaped quotes are necessary to work with spaces in the path
|
|
|
|
# when the command is finally eval'd.
|
2019-12-03 19:18:39 +08:00
|
|
|
set fish_browser cygstart $fish_browser
|
2019-11-27 11:01:47 +08:00
|
|
|
else
|
|
|
|
set need_trampoline 1
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-16 17:19:45 +08:00
|
|
|
set -l fish_help_page
|
2016-11-28 13:27:22 +08:00
|
|
|
switch "$fish_help_item"
|
|
|
|
case "."
|
2019-03-27 19:46:51 +08:00
|
|
|
set fish_help_page "cmds/source.html"
|
2016-11-28 13:27:22 +08:00
|
|
|
case globbing
|
2019-11-27 11:01:47 +08:00
|
|
|
set fish_help_page "index.html#expand"
|
2021-02-06 00:19:07 +08:00
|
|
|
case (builtin -n) (__fish_print_commands)
|
|
|
|
# If the docs aren't installed, __fish_print_commands won't print anything
|
|
|
|
# Since we document all our builtins, check those at least.
|
|
|
|
# The alternative is to create this list at build time.
|
2019-03-27 19:46:51 +08:00
|
|
|
set fish_help_page "cmds/$fish_help_item.html"
|
2020-05-16 17:19:45 +08:00
|
|
|
case 'completion-*'
|
|
|
|
set fish_help_page "completions.html#$fish_help_item"
|
2020-08-01 03:06:40 +08:00
|
|
|
case 'tut-*'
|
2020-05-16 16:34:32 +08:00
|
|
|
set fish_help_page "tutorial.html#"(string sub -s 5 -- $fish_help_item | string replace -a -- _ -)
|
2017-06-14 19:52:24 +08:00
|
|
|
case tutorial
|
|
|
|
set fish_help_page "tutorial.html"
|
2021-02-25 22:46:24 +08:00
|
|
|
case releasenotes
|
|
|
|
set fish_help_page relnotes.html
|
2020-08-05 23:51:47 +08:00
|
|
|
case completions
|
|
|
|
set fish_help_page completions.html
|
|
|
|
case faq
|
|
|
|
set fish_help_page faq.html
|
|
|
|
case fish-for-bash-users
|
|
|
|
set fish_help_page fish_for_bash_users.html
|
2020-05-16 16:34:32 +08:00
|
|
|
case ''
|
2016-11-28 13:27:22 +08:00
|
|
|
set fish_help_page "index.html"
|
2020-05-16 16:34:32 +08:00
|
|
|
case "*"
|
|
|
|
set fish_help_page "index.html#$fish_help_item"
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 14:39:11 +08:00
|
|
|
# In Crostini Chrome OS Linux, the default browser opens URLs in Chrome running outside the
|
|
|
|
# linux VM. This browser does not have access to the Linux filesystem. This uses Garcon, see e.g.
|
|
|
|
# https://chromium.googlesource.com/chromiumos/platform2/+/master/vm_tools/garcon/#opening-urls
|
|
|
|
# https://source.chromium.org/search?q=garcon-url-handler
|
|
|
|
string match -q '*garcon-url-handler*' $fish_browser[1]
|
|
|
|
and set -l chromeos_linux_garcon
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
set -l page_url
|
2021-03-02 14:39:11 +08:00
|
|
|
if test -f $__fish_help_dir/index.html; and not set -lq chromeos_linux_garcon
|
2016-11-28 13:27:22 +08:00
|
|
|
# Help is installed, use it
|
|
|
|
set page_url file://$__fish_help_dir/$fish_help_page
|
|
|
|
|
2021-03-02 14:39:11 +08:00
|
|
|
# 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
|
2019-11-29 13:09:58 +08:00
|
|
|
# but only if a Windows browser is being used
|
2016-11-28 13:27:22 +08:00
|
|
|
if type -q cygpath
|
2020-06-25 00:36:37 +08:00
|
|
|
and string match -qr '(cygstart|\.exe)(\s+|$)' $fish_browser[1]
|
2016-11-28 13:27:22 +08:00
|
|
|
set page_url file://(cygpath -m $__fish_help_dir)/$fish_help_page
|
2019-11-27 11:01:47 +08:00
|
|
|
else if type -q wslpath
|
2020-06-25 00:36:37 +08:00
|
|
|
and string match -qr '\.exe(\s+|$)' $fish_browser[1]
|
2019-11-27 11:01:47 +08:00
|
|
|
set page_url file://(wslpath -w $__fish_help_dir)/$fish_help_page
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
# Go to the web. Only include one dot in the version string
|
2020-03-25 12:02:32 +08:00
|
|
|
set -l version_string (string split . -f 1,2 -- $version | string join .)
|
2017-02-17 21:30:43 +08:00
|
|
|
set page_url https://fishshell.com/docs/$version_string/$fish_help_page
|
2018-01-13 07:02:13 +08:00
|
|
|
# We don't need a trampoline for a remote URL.
|
|
|
|
set need_trampoline
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
|
|
|
|
2018-01-13 07:02:13 +08:00
|
|
|
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)
|
2019-11-27 11:01:47 +08:00
|
|
|
if set -l clean_url (string match -re '#' $page_url)
|
2018-01-13 07:02:13 +08:00
|
|
|
# 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)
|
2020-11-13 23:58:45 +08:00
|
|
|
or return 1
|
2018-01-13 07:02:13 +08:00
|
|
|
set -l tmpname $tmpdir/help.html
|
2019-05-05 18:09:25 +08:00
|
|
|
echo '<meta http-equiv="refresh" content="0;URL=\''$clean_url'\'" />' >$tmpname
|
2018-01-13 07:02:13 +08:00
|
|
|
set page_url file://$tmpname
|
2019-11-27 11:01:47 +08:00
|
|
|
|
2020-06-25 00:36:37 +08:00
|
|
|
# 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
|
2019-11-30 19:15:53 +08:00
|
|
|
# but only if a Windows browser is being used
|
2019-11-27 11:01:47 +08:00
|
|
|
if type -q cygpath
|
2020-06-25 00:36:37 +08:00
|
|
|
and string match -qr '(cygstart|\.exe)(\s+|$)' $fish_browser[1]
|
2019-11-27 11:01:47 +08:00
|
|
|
set page_url file://(cygpath -m $tmpname)
|
|
|
|
else if type -q wslpath
|
2020-06-25 00:36:37 +08:00
|
|
|
and string match -qr '\.exe(\s+|$)' $fish_browser[1]
|
2019-11-27 11:01:47 +08:00
|
|
|
set page_url file://(wslpath -w $tmpname)
|
|
|
|
end
|
2018-01-13 07:02:13 +08:00
|
|
|
end
|
2018-01-13 06:58:28 +08:00
|
|
|
end
|
|
|
|
|
2019-03-23 00:24:10 +08:00
|
|
|
# cmd.exe needs more coaxing.
|
2020-06-25 00:36:37 +08:00
|
|
|
if string match -qr '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
|
2020-06-25 02:43:56 +08:00
|
|
|
# If browser is known to be graphical, put into background
|
2018-04-03 07:36:11 +08:00
|
|
|
else if contains -- $fish_browser[1] $graphical_browsers
|
2016-11-28 13:27:22 +08:00
|
|
|
switch $fish_browser[1]
|
2020-03-10 02:36:12 +08:00
|
|
|
case htmlview x-www-browser
|
2016-11-28 13:27:22 +08:00
|
|
|
printf (_ 'help: Help is being displayed in your default browser.\n')
|
|
|
|
case '*'
|
|
|
|
printf (_ 'help: Help is being displayed in %s.\n') $fish_browser[1]
|
|
|
|
end
|
2020-01-14 03:34:22 +08:00
|
|
|
$fish_browser $page_url &
|
2021-01-09 20:44:48 +08:00
|
|
|
disown $last_pid >/dev/null 2>&1
|
2016-11-28 13:27:22 +08:00
|
|
|
else
|
2017-09-27 02:34:23 +08:00
|
|
|
# Work around lynx bug where <div class="contents"> always has the same formatting as links (unreadable)
|
|
|
|
# by using a custom style sheet. See https://github.com/fish-shell/fish-shell/issues/4170
|
2020-06-25 00:36:37 +08:00
|
|
|
if string match -qr '^lynx' -- $fish_browser
|
2018-03-12 21:34:20 +08:00
|
|
|
set fish_browser $fish_browser -lss={$__fish_data_dir}/lynx.lss
|
2017-09-27 02:34:23 +08:00
|
|
|
end
|
2019-12-03 19:18:39 +08:00
|
|
|
$fish_browser $page_url
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2006-02-08 17:20:05 +08:00
|
|
|
end
|