home/rc/tools/man.kak
Steven Chan 64fd0c72ce Enable man command to show stderr message to the user
1. If there is no man page found (error 16), then echo stderr text.
2. For other kinds of errors, fail with stderr text so that command can propagate through a try/catch.
2019-04-24 14:49:56 -07:00

77 lines
2.8 KiB
Plaintext

declare-option -docstring "name of the client in which documentation is to be displayed" \
str docsclient
declare-option -hidden str-list manpage
hook -group man-highlight global WinSetOption filetype=man %{
add-highlighter window/man-highlight group
# Sections
add-highlighter window/man-highlight/ regex ^\S.*?$ 0:title
# Subsections
add-highlighter window/man-highlight/ regex '^ {3}\S.*?$' 0:default+b
# Command line options
add-highlighter window/man-highlight/ regex '^ {7}-[^\s,]+(,\s+-[^\s,]+)*' 0:list
# References to other manpages
add-highlighter window/man-highlight/ regex [-a-zA-Z0-9_.]+\([a-z0-9]+\) 0:header
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/man-highlight }
}
hook global WinSetOption filetype=man %{
hook -group man-hooks window WinResize .* %{ man-impl %opt{manpage} }
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window man-hooks }
}
define-command -hidden -params 2..3 man-impl %{ evaluate-commands %sh{
buffer_name="$1"
shift
manout=$(mktemp "${TMPDIR:-/tmp}"/kak-man-XXXXXX)
manerr=$(mktemp "${TMPDIR:-/tmp}"/kak-man-XXXXXX)
colout=$(mktemp "${TMPDIR:-/tmp}"/kak-man-XXXXXX)
MANWIDTH=${kak_window_width} man "$@" > "$manout" 2> "$manerr"
retval=$?
col -b -x > ${colout} < ${manout}
rm ${manout}
if [ "${retval}" -eq 0 ]; then
printf %s\\n "
edit -scratch %{*$buffer_name ${*}*}
execute-keys '%|cat<space>${colout}<ret>gk'
nop %sh{ rm ${colout}; rm ${manerr} }
set-option buffer filetype man
set-option window manpage $buffer_name $*
"
elif [ "${retval}" -eq 16 ]; then
printf %s\\n "
echo -markup %{{Error}$(cat $manerr)}
nop %sh{ rm ${colout}; rm ${manerr} }
"
else
printf %s\\n "
fail $(cat $manerr)
nop %sh{ rm ${colout}; rm ${manerr} }
"
fi
} }
define-command -params ..1 \
-shell-script-candidates %{
find /usr/share/man/ -name '*.[1-8]*' | sed 's,^.*/\(.*\)\.\([1-8][a-zA-Z]*\).*$,\1(\2),'
} \
-docstring %{man [<page>]: manpage viewer wrapper
If no argument is passed to the command, the selection will be used as page
The page can be a word, or a word directly followed by a section number between parenthesis, e.g. kak(1)} \
man %{ evaluate-commands %sh{
subject=${1-$kak_selection}
## The completion suggestions display the page number, strip them if present
case "${subject}" in
*\([1-8]*\))
pagenum="${subject##*(}"
pagenum="${pagenum%)}"
subject="${subject%%(*}"
;;
esac
printf %s\\n "evaluate-commands -try-client %opt{docsclient} man-impl man $pagenum $subject"
} }