blob: 274f663aa4ea3756682a603d940a2f05b4d29d84 (
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# Autocompletion for man(1)
_man() {
# Don't interfere with a user typing a path
case $2 in
*/*) return 1 ;;
esac
# If previous word started with a number, we'll assume that's a section to
# search
case $3 in
[0-9]*) sec=$3 ;;
esac
# Cut completion short if we have neither section nor word; there will
# probably be too many results
[[ -n $sec ]] || [[ -n $2 ]] || return
# Read completion results from a subshell and add them to the COMPREPLY
# array individually
local ci comp
while IFS= read -d '' -r comp ; do
COMPREPLY[ci++]=$comp
done < <(
# Do not return dotfiles, give us extended globbing, and expand empty
# globs to just nothing
shopt -u dotglob
shopt -s nullglob
# Make globbing case-insensitive if appropriate
while read -r _ setting ; do
case $setting in
('completion-ignore-case on')
shopt -s nocaseglob
break
;;
esac
done < <(bind -v)
# Figure out the manual paths to search
if hash amanpath 2>/dev/null ; then
# manpath(1) exists, run it to find what to search
IFS=: read -a manpaths -r \
< <(manpath 2>/dev/null)
else
# Fall back on some typical paths
manpaths=( \
"$HOME"/.local/man \
"$HOME"/.local/share/man \
/usr/man \
/usr/share/man \
/usr/local/man \
/usr/local/share/man \
)
fi
# Add pages from each manual directory
local pages pi
for mp in "${manpaths[@]}" ; do
[[ -n $mp ]] || continue
# Which pattern? Depends on section specification
if [[ -n $sec ]] ; then
# Section requested; quoted value in glob
for page in "$mp"/man"${sec%%[!0-9]*}"/"$2"*."$sec"* ; do
pages[pi++]=${page##*/}
done
else
# No section;
for page in "$mp"/man[0-9]*/"$2"*.[0-9]* ; do
pages[pi++]=${page##*/}
done
fi
done
# Bail if there are no pages
((pi)) || exit
# Strip section suffixes
pages=("${pages[@]%.[0-9]*}")
# Print entries, null-delimited
printf '%s\0' "${pages[@]}"
)
}
complete -F _man -o bashdefault -o default man
|