2014-09-28 17:09:14 +08:00
function __fish_man_page
2022-06-17 03:15:49 +08:00
# Get all commandline tokens not starting with "-", up to and including the cursor's
2024-01-22 14:42:45 +08:00
set -l args ( string match -rv '^-|^$' -- ( commandline -cpx && commandline -t ) )
2016-12-26 21:27:38 +08:00
# If commandline is empty, exit.
if not set -q args [ 1 ]
printf \a
return
end
2022-05-14 02:39:20 +08:00
# Skip leading commands and display the manpage of following command
2019-11-16 21:57:59 +08:00
while set -q args [ 2 ]
2022-01-08 20:50:31 +08:00
and string match -qr -- '^(and|begin|builtin|caffeinate|command|doas|entr|env|exec|if|mosh|nice|not|or|pipenv|prime-run|setsid|sudo|systemd-nspawn|time|watch|while|xargs|.*=.*)$' $args [ 1 ]
2019-12-20 06:41:26 +08:00
set -e args [ 1 ]
2019-09-19 04:35:33 +08:00
end
2016-12-26 21:27:38 +08:00
# If there are at least two tokens not starting with "-", the second one might be a subcommand.
# Try "man first-second" and fall back to "man first" if that doesn't work out.
2023-01-26 03:04:57 +08:00
set -l maincmd ( path basename $args [ 1 ] )
2022-05-14 02:39:20 +08:00
# HACK: If stderr is not attached to a terminal `less` (the default pager)
# wouldn't use the alternate screen.
# But since we don't know what pager it is, and because `man` is totally underspecified,
# the best we can do is to *try* the man page, and assume that `man` will return false if it fails.
# See #7863.
2016-12-26 21:27:38 +08:00
if set -q args [ 2 ]
2022-05-14 02:39:54 +08:00
and not string match -q -- '*/*' $args [ 2 ]
2022-05-14 02:39:20 +08:00
and man " $maincmd - $args [2] " & > /dev/null
man " $maincmd - $args [2] "
2016-12-26 21:27:38 +08:00
else
2021-03-27 16:14:49 +08:00
if man " $maincmd " & > /dev/null
man " $maincmd "
else
printf \a
end
2016-12-26 21:27:38 +08:00
end
commandline -f repaint
2014-09-28 17:09:14 +08:00
end