2007-09-24 05:07:30 +08:00
|
|
|
function __fish_print_help --description "Print help message for the specified fish function or builtin" --argument item
|
2016-11-28 11:05:37 +08:00
|
|
|
if test "$item" = '.'
|
|
|
|
set item source
|
|
|
|
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"
|
2016-11-28 11:05:37 +08:00
|
|
|
return
|
|
|
|
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
|
2018-10-23 21:05:15 +08:00
|
|
|
set -l cols $COLUMNS
|
2016-11-28 11:05:37 +08:00
|
|
|
set -l rLL
|
|
|
|
if test -n "$cols"
|
2017-09-10 14:35:47 +08:00
|
|
|
set cols (math $cols - 4) # leave a bit of space on the right
|
2016-11-28 11:05:37 +08:00
|
|
|
set rLL -rLL=$cols[1]n
|
|
|
|
end
|
2018-03-12 21:34:20 +08:00
|
|
|
set -lx GROFF_TMAC_PATH $__fish_data_dir/groff
|
2017-05-24 10:57:18 +08:00
|
|
|
set -l mfish
|
|
|
|
if test -e $GROFF_TMAC_PATH/fish.tmac
|
|
|
|
set mfish -mfish
|
|
|
|
end
|
2018-03-12 21:34:20 +08:00
|
|
|
if test -e "$__fish_data_dir/man/man1/$item.1"
|
2018-04-02 04:42:38 +08:00
|
|
|
set help (nroff -c -man $mfish -t $rLL "$__fish_data_dir/man/man1/$item.1" 2>/dev/null)
|
2018-03-12 21:34:20 +08:00
|
|
|
else if test -e "$__fish_data_dir/man/man1/$item.1.gz"
|
2018-04-02 04:42:38 +08:00
|
|
|
set help (gunzip -c "$__fish_data_dir/man/man1/$item.1.gz" 2>/dev/null | nroff -c -man $mfish -t $rLL 2>/dev/null)
|
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
|
|
|
|
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 ''
|
|
|
|
set line_type blank
|
|
|
|
case '*'
|
|
|
|
# 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
|
|
|
|
2016-11-28 11:05:37 +08:00
|
|
|
switch $state
|
|
|
|
case normal
|
|
|
|
switch $line_type
|
|
|
|
case normal
|
|
|
|
printf "%s\n" $line
|
|
|
|
case blank
|
|
|
|
set state blank
|
|
|
|
case meta
|
|
|
|
# skip it
|
|
|
|
end
|
|
|
|
case blank
|
|
|
|
switch $line_type
|
|
|
|
case normal
|
|
|
|
echo # print the blank line
|
|
|
|
printf "%s\n" $line
|
|
|
|
set state normal
|
|
|
|
case blank meta
|
|
|
|
# skip it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end | ul # post-process with `ul`, to interpret the old-style grotty escapes
|
|
|
|
echo # print a trailing blank line
|
2012-09-01 17:14:13 +08:00
|
|
|
end
|