]> git.decadent.org.uk Git - ion3.git/blob - utils/ion-completeman.in
[svn-upgrade] Tagging new upstream version, ion3 (20070927)
[ion3.git] / utils / ion-completeman.in
1 #!/bin/sh
2
3 prog=$0
4 tocompl=""
5 section=""
6 beg=""
7 action=""
8 usercache=""
9 syscache=""
10 linebeg="^"
11 icase=""
12
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'
19 }
20
21
22 while test $# -ge 1; do
23     case "$1" in
24         -mid)
25             linebeg=""
26             ;;
27         -icase)
28             icase="-i"
29             ;;
30         -complete)
31             read section tocompl << EOF
32 $2
33 EOF
34             if test "$tocompl" = ""; then
35                 tocompl="$section"
36                 section=""
37             else
38                 beg="$section "
39             fi
40             tocompl=`echo "$tocompl" | translate_grepsafe`
41             action="complete"
42             break
43             ;;
44         -mkusercache)
45             action="mkusercache"
46             break
47             ;;
48         -mksyscache)
49             action="mksyscache"
50             break
51             ;;
52         *)
53             break
54             ;;
55     esac
56     shift
57 done
58
59 if test "x$action" = x; then
60     echo 2>&1 "Usage: $prog [-icase] [-mid] (-complete what|-mkusercache|-mksyscache)"
61     exit 1
62 fi
63         
64
65 filterpath() {
66     sed 's:^.*/\([^/]*\.[0-9].*\)$:\1:p; d'
67 }
68
69 filtersect() {
70     sed 's:^\(.*\)\.[0-9].*$:\1:p; d'
71 }
72
73 grepper() {
74     if test "$tocompl" = "" -a "$section" = ""; then
75         cat
76     else
77         if test "$section" = ""; then
78             section="[0-9]"
79         fi
80         grep $icase "$linebeg$tocompl.*\.$section"
81     fi
82 }
83
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
94         fi
95         mpath=`$mpprog`
96     fi
97     
98     for p in `echo "$mpath"|tr : '\n'`; do
99         find  "$p" -type f -o -type l | filterpath
100     done 
101 }
102
103
104 cachefile=""
105
106 if test "x$HOME" != x; then
107     usercache="$HOME/.ion3/mancache"
108 fi
109
110 syscache="@VARDIR@/mancache"
111
112 case "$action" in
113     complete)
114         if test "x$usercache" != x -a -f "$usercache"; then
115             cachefile="$usercache"
116         fi
117         
118         if test -f "$syscache"; then
119             cachefile="$syscache"
120         fi
121         
122         # Empty "common part" of completions.
123         echo "$beg"
124         
125         if test "x$cachefile" != x; then
126             grepper < "$cachefile" | filtersect
127         else
128             scan | grepper | filtersect
129         fi
130         ;;
131     mkusercache)
132         if test "x$usercache" != x; then
133             scan > "$usercache"
134         else
135             echo >&2 "\$HOME not set."
136         fi
137         ;;
138     mksyscache)
139         mkdir -p "@VARDIR@"
140         scan > "$syscache"
141         ;;
142 esac