2020-09-19 17:39:51 +08:00
|
|
|
function __fish_print_help --description "Print help message for the specified fish function or builtin" --argument-names item error_message
|
2020-07-12 06:02:11 +08:00
|
|
|
switch $item
|
|
|
|
case .
|
|
|
|
set item source
|
|
|
|
case :
|
|
|
|
set item true
|
|
|
|
case '['
|
|
|
|
set item test
|
2016-11-28 11:05:37 +08:00
|
|
|
end
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-11-28 11:05:37 +08:00
|
|
|
# Do nothing if the file does not exist
|
2018-03-12 21:34:20 +08:00
|
|
|
if not test -e "$__fish_data_dir/man/man1/$item.1" -o -e "$__fish_data_dir/man/man1/$item.1.gz"
|
2021-03-16 05:59:37 +08:00
|
|
|
return 2
|
2016-11-28 11:05:37 +08:00
|
|
|
end
|
2007-09-23 06:38:28 +08:00
|
|
|
|
2016-11-28 11:05:37 +08:00
|
|
|
# Render help output, save output into the variable 'help'
|
|
|
|
set -l help
|
2019-01-06 12:00:03 +08:00
|
|
|
set -l format
|
|
|
|
set -l cols
|
|
|
|
if test -n "$COLUMNS"
|
|
|
|
set cols (math $COLUMNS - 4) # leave a bit of space on the right
|
2016-11-28 11:05:37 +08:00
|
|
|
end
|
2019-01-06 12:00:03 +08:00
|
|
|
|
|
|
|
# Pick which command we are using to render output or fail if none
|
2021-11-04 21:58:18 +08:00
|
|
|
# We prefer mandoc because that doesn't break with unicode input.
|
|
|
|
if command -qs mandoc
|
|
|
|
set format mandoc -c
|
|
|
|
if test -n "$cols"
|
|
|
|
set -a format -O width=$cols
|
|
|
|
end
|
|
|
|
else if command -qs nroff
|
2019-01-06 12:00:03 +08:00
|
|
|
set format nroff -c -man -t
|
|
|
|
if test -e $__fish_data_dir/groff/fish.tmac
|
|
|
|
set -a format -M$__fish_data_dir/groff -mfish
|
|
|
|
end
|
|
|
|
if test -n "$cols"
|
|
|
|
set -a format -rLL={$cols}n
|
|
|
|
end
|
|
|
|
else
|
|
|
|
echo fish: (_ "Cannot format help; no parser found")
|
|
|
|
return 1
|
2017-05-24 10:57:18 +08:00
|
|
|
end
|
2019-01-06 12:00:03 +08:00
|
|
|
|
2018-03-12 21:34:20 +08:00
|
|
|
if test -e "$__fish_data_dir/man/man1/$item.1"
|
2021-11-04 21:58:18 +08:00
|
|
|
# Some nroff versions screw up non-ascii characters.
|
|
|
|
# (even with the locale set correctly!)
|
|
|
|
# Work around that by running preconv first.
|
|
|
|
if command -sq preconv; and test "$format[1]" = nroff
|
|
|
|
set help (preconv -e UTF-8 "$__fish_data_dir/man/man1/$item.1" | $format 2>/dev/null)
|
|
|
|
else
|
|
|
|
set help ($format "$__fish_data_dir/man/man1/$item.1" 2>/dev/null)
|
|
|
|
end
|
2018-03-12 21:34:20 +08:00
|
|
|
else if test -e "$__fish_data_dir/man/man1/$item.1.gz"
|
2021-11-04 21:58:18 +08:00
|
|
|
if command -sq preconv; and test "$format[1]" = nroff
|
|
|
|
set help (gunzip -c "$__fish_data_dir/man/man1/$item.1.gz" 2>/dev/null | preconv -e UTF-8 | $format 2>/dev/null)
|
|
|
|
else
|
|
|
|
set help (gunzip -c "$__fish_data_dir/man/man1/$item.1.gz" 2>/dev/null | $format 2>/dev/null)
|
|
|
|
end
|
2016-06-10 20:13:15 +08:00
|
|
|
end
|
2006-11-20 07:27:34 +08:00
|
|
|
|
2016-11-28 11:05:37 +08:00
|
|
|
# The original implementation trimmed off the top 5 lines and bottom 3 lines
|
|
|
|
# from the nroff output. Perhaps that's reliable, but the magic numbers make
|
|
|
|
# me extremely nervous. Instead, let's just strip out any lines that start
|
|
|
|
# in the first column. "normal" manpages put all section headers in the first
|
|
|
|
# column, but fish manpages only leave NAME like that, which we want to trim
|
|
|
|
# away anyway.
|
|
|
|
#
|
|
|
|
# While we're at it, let's compress sequences of blank lines down to a single
|
|
|
|
# blank line, to duplicate the default behavior of `man`, or more accurately,
|
|
|
|
# the `-s` flag to `less` that `man` passes.
|
|
|
|
set -l state blank
|
2019-04-11 00:20:25 +08:00
|
|
|
set -l have_name
|
2019-10-20 17:38:17 +08:00
|
|
|
begin
|
|
|
|
string join \n $error_message
|
2019-10-20 23:17:47 +08:00
|
|
|
for line in $help
|
|
|
|
# categorize the line
|
|
|
|
set -l line_type
|
|
|
|
switch $line
|
|
|
|
case ' *' \t\*
|
|
|
|
# starts with whitespace, check if it has non-whitespace
|
|
|
|
printf "%s\n" $line | read -l word __
|
|
|
|
if test -n $word
|
|
|
|
set line_type normal
|
|
|
|
else
|
|
|
|
# lines with just spaces probably shouldn't happen
|
|
|
|
# but let's consider them to be blank
|
|
|
|
set line_type blank
|
|
|
|
end
|
|
|
|
case ''
|
2016-11-28 11:05:37 +08:00
|
|
|
set line_type blank
|
2019-10-20 23:17:47 +08:00
|
|
|
case '*'
|
|
|
|
# Remove man's bolding
|
|
|
|
set -l name (string replace -ra '(.)'\b'.' '$1' -- $line)
|
|
|
|
# We start after we have the name
|
|
|
|
contains -- $name NAME; and set have_name 1; and continue
|
|
|
|
# We ignore the SYNOPSIS header
|
|
|
|
contains -- $name SYNOPSIS; and continue
|
|
|
|
# Everything after COPYRIGHT is useless
|
|
|
|
contains -- $name COPYRIGHT; and break
|
2019-04-11 00:20:25 +08:00
|
|
|
|
2019-10-20 23:17:47 +08:00
|
|
|
# not leading space, and not empty, so must contain a non-space
|
|
|
|
# in the first column. That makes it a header/footer.
|
|
|
|
set line_type meta
|
|
|
|
end
|
2006-11-20 07:27:34 +08:00
|
|
|
|
2019-10-20 23:17:47 +08:00
|
|
|
set -q have_name[1]; or continue
|
|
|
|
switch $state
|
|
|
|
case normal
|
|
|
|
switch $line_type
|
|
|
|
case normal meta
|
|
|
|
printf "%s\n" $line
|
|
|
|
case blank
|
|
|
|
set state blank
|
|
|
|
end
|
|
|
|
case blank
|
|
|
|
switch $line_type
|
|
|
|
case normal meta
|
|
|
|
echo # print the blank line
|
|
|
|
printf "%s\n" $line
|
|
|
|
set state normal
|
|
|
|
case blank meta
|
|
|
|
# skip it
|
|
|
|
end
|
|
|
|
end
|
2016-11-28 11:05:37 +08:00
|
|
|
end
|
2021-06-21 02:46:49 +08:00
|
|
|
end | string replace -ra '^ ' '' |
|
2020-08-17 17:43:02 +08:00
|
|
|
begin
|
|
|
|
set -l pager less
|
|
|
|
set -q PAGER
|
|
|
|
and echo $PAGER | read -at pager
|
|
|
|
not isatty stdout
|
|
|
|
and set pager cat # cannot use a builtin here
|
|
|
|
# similar to man, but add -F to quit paging when the help output is brief (#6227)
|
2021-09-17 07:30:33 +08:00
|
|
|
# Also set -X for less < v530, see #8157.
|
|
|
|
set -l lessopts isRF
|
2021-09-17 23:02:24 +08:00
|
|
|
if test (less --version | string match -r 'less (\d+)')[2] -lt 530 2>/dev/null
|
2021-09-17 07:30:33 +08:00
|
|
|
set lessopts "$lessopts"X
|
|
|
|
end
|
|
|
|
|
2021-05-15 04:18:01 +08:00
|
|
|
not set -qx LESS
|
2021-09-17 07:30:33 +08:00
|
|
|
and set -xl LESS $lessopts
|
|
|
|
|
2020-08-17 17:43:02 +08:00
|
|
|
# less options:
|
|
|
|
# -i (--ignore-case) search case-insensitively, like man
|
|
|
|
# -s (--squeeze-blank-lines) not strictly necessary since we already do that above
|
2021-05-15 04:18:01 +08:00
|
|
|
# -R (--RAW-CONTROL-CHARS) to display colors and such
|
2020-08-17 17:43:02 +08:00
|
|
|
# -F (--quit-if-one-screen) to maintain the non-paging behavior for small outputs
|
2021-09-17 07:30:33 +08:00
|
|
|
# -X (--no-init) do not clear the screen, necessary for less < v530 or else short output is dropped
|
2020-08-17 17:43:02 +08:00
|
|
|
$pager
|
|
|
|
end
|
2012-09-01 17:14:13 +08:00
|
|
|
end
|