]> diplodocus.org Git - nmh/blob - etc/bash_completion_nmh-gen
Added application/ics entries, and "file was generated" comment.
[nmh] / etc / bash_completion_nmh-gen
1 #!/bin/sh
2 #
3 # Generates COMPLETION-BASH from man/mh-chart.man.
4 #
5 # This code is Copyright (c) 2016, by the authors of nmh.
6 # See the COPYRIGHT file in the root directory of the nmh
7 # distribution for complete copyright information.
8
9 mhchart=man/mh-chart.man
10
11 #### Extract switches for nmh program.
12 prog_switches() {
13 program=$1
14
15 #### Extract lines from just after .B program-name to just before
16 #### blank line, and then only those that start with .RB, e.g.,
17 #### .RB [ \-list " | " \-nolist ]
18 #### .RB " | " \-autoheaderencoding ]
19 #### .RB [ \-\|\-component
20 awk '/^.B.*(\/| )'"$program"'/,/^$/ {
21 if (/^\.RB /) print
22 }' "$mhchart" | \
23 tr -d '\n' | \
24 sed -e 's/\.RB //g' -e 's/\[//g' -e 's/\]//g' \
25 -e 's/\(\\\)*//g' -e 's/-|-/-/g' -e 's/" *| "//g' \
26 -e 's/^ *//' -e 's/ *$//' -e 's/ */ /g'
27 }
28
29 cat <<'EOF'
30 # bash completions for nmh commands -*- shell-script -*-
31 # This file was generated by COMPLETION-BASH-gen.
32 #
33 # This code is Copyright (c) 2016, by the authors of nmh.
34 # See the COPYRIGHT file in the root directory of the nmh
35 # distribution for complete copyright information.
36 #
37 # To use: source at a bash prompt or in an initialization file.
38
39 _nmh() {
40 local -a switches
41
42 COMPREPLY=()
43
44 case ${COMP_WORDS[COMP_CWORD]} in
45 -*) case $1 in
46 EOF
47
48 #### Extract program names from lines such as:
49 #### .B %nmhlibexecdir%/install\-mh
50 #### .B new \-mode fnext
51 programs=`egrep '\.B [^\-]' "$mhchart" | \
52 sed -e 's/^\.B //' -e 's/ .*//' -e 's|%[^%]*%/||' -e 's/\\-/-/'`
53 for program in $programs; do
54 switches=`prog_switches "$program"`
55 # Support mime as alias for mhbuild.
56 [ "$program" = mhbuild ] && program='mhbuild|mime'
57 printf ' %s) switches=(%s) ;;\n' "$program" "$switches"
58
59 done
60 programs="$programs mime"
61
62 cat <<'EOF'
63 esac ;;
64
65 +*) switches=($(folder -all -fast -recurse | sed 's/^/+/')) ;;
66
67 [flc.np]*) switches=(first last cur . next prev) ;;
68 esac
69
70 #### Special case.
71 [ $1 = mhpath ] && switches=("${switches[*]}" + new)
72
73 COMPREPLY=($(compgen -W "${switches[*]}" -- ${COMP_WORDS[COMP_CWORD]}))
74 }
75
76 EOF
77
78 #### There better not be an nmh program name with whitespace in it.
79 echo complete -F _nmh $programs