Releasing version 3-2014010504
[notion/jeffpc.git] / utils / ion-completeman.in
blob135efaf248dc5855e06103b22c66c3df98670c71
1 #!/bin/sh
3 prog=$0
4 tocompl=""
5 section=""
6 beg=""
7 action=""
8 usercache=""
9 syscache=""
10 linebeg="^"
11 icase=""
13 translate_grepsafe() {
14 # The regexp below is supposed to be [\[\].*$^\\], but sed sucks
15 # and doesn't support simple and intuitive escaping and we have to
16 # do it the hard way with the collations [.[.] and [.].] standing
17 # for [ and ], respectively.
18 sed 's:^[ \t]*\(.*\)[ \t]*$:\1:; s:[[.[.][.].].*$^\\]:\\&:g'
22 while test $# -ge 1; do
23 case "$1" in
24 -mid)
25 linebeg=""
27 -icase)
28 icase="-i"
30 -complete)
31 read section tocompl << EOF
33 EOF
34 if test "$tocompl" = ""; then
35 tocompl="$section"
36 section=""
37 else
38 beg="$section "
40 tocompl=`echo "$tocompl" | translate_grepsafe`
41 action="complete"
42 break
44 -mkusercache)
45 action="mkusercache"
46 break
48 -mksyscache)
49 action="mksyscache"
50 break
53 break
55 esac
56 shift
57 done
59 if test "x$action" = x; then
60 echo 2>&1 "Usage: $prog [-icase] [-mid] (-complete what|-mkusercache|-mksyscache)"
61 exit 1
65 filterpath() {
66 sed 's:^.*/\([^/]*\.[0-9].*\)$:\1:p; d'
69 filtersect() {
70 sed 's:^\(.*\)\.[0-9].*$:\1:p; d'
73 grepper() {
74 if test "$tocompl" = "" -a "$section" = ""; then
75 cat
76 else
77 if test "$section" = ""; then
78 section="[0-9]"
80 grep $icase "$linebeg$tocompl.*\.$section"
84 scan() {
85 if test "x$ION_MANPATH" != "x"; then
86 mpath="$ION_MANPATH"
87 elif test "x$MANPATH" != "x"; then
88 mpath="$MANPATH"
89 else
90 mpprog=`which manpath`
91 if test "x$mpprog" = x; then
92 echo "Please set MANPATH, ION_MANPATH or put 'manpath' on PATH" > /dev/stderr
93 exit 1
95 mpath=`$mpprog`
98 for p in `echo "$mpath"|tr : '\n'`; do
99 find "$p" -type f -o -type l | filterpath
100 done
104 cachefile=""
106 if test "x$HOME" != x; then
107 usercache="$HOME/.ion3/mancache"
110 vardir=${ION_VAR_PATH-@VARDIR@}
111 syscache="$vardir/mancache"
114 case "$action" in
115 complete)
116 if test "x$usercache" != x -a -f "$usercache"; then
117 cachefile="$usercache"
118 elif test -f "$syscache"; then
119 cachefile="$syscache"
122 # Empty "common part" of completions.
123 echo "$beg"
125 if test "x$cachefile" != x; then
126 grepper < "$cachefile" | filtersect
127 else
128 scan | grepper | filtersect
131 mkusercache)
132 if test "x$usercache" != x; then
133 scan > "$usercache"
134 else
135 echo >&2 "\$HOME not set."
138 mksyscache)
139 mkdir -p "$vardir"
140 scan > "$syscache"
142 esac