blob: 8a6df3ea1c9f79138ae48128af04b2e37fc5a11d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# Autocompletion for man(1)
_man() {
# Don't even bother if we don't have manpath(1)
hash manpath || return 1
# Snarf the word
local word
word=${COMP_WORDS[COMP_CWORD]}
# If this is the second word, and the previous word was a number, we'll
# assume that's the section to search
local section
if ((COMP_CWORD > 1)) && [[ ${COMP_WORDS[COMP_CWORD-1]} != [^0-9] ]] ; then
section='man'${COMP_WORDS[COMP_CWORD-1]}
fi
# Read slash-separated output from a subshell into the COMPREPLY array; use
# read -a rather than adding each element individually, as it's much faster
IFS=/ read -a COMPREPLY -d '' -r < <(
# Do not return dotfiles, and expand empty globs to just nothing
shopt -u dotglob
shopt -s nullglob
# Start an array of pages
declare -a pages
# Break manpath(1) output into an array of paths
IFS=: read -a manpaths -r < <(manpath 2>/dev/null)
# Iterate through the manual page paths and add every manual page we find
for manpath in "${manpaths[@]}" ; do
[[ $manpath ]] || continue
pages=("${pages[@]}" "$manpath"/"$section"*/"$word"*)
done
# Strip paths, .gz suffixes, and finally .<section> suffixes
pages=("${pages[@]##*/}")
pages=("${pages[@]%.gz}")
pages=("${pages[@]%.*}")
# Bail out if we ended up with no pages somehow to prevent us from
# printing
((${#pages[@]})) || exit 1
# Print the pages array to stdout, slash-separated, null-terminated
(IFS=/ ; printf '%q\0' "${pages[*]}")
)
}
complete -F _man -o default man
|