]> diplodocus.org Git - nmh/blob - etc/bash_completion_nmh-gen
getpass.c: Move interface to own file.
[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 etc/bash_completion_nmh-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 #### Complete filenames.
44 compopt -o default
45
46 case ${COMP_WORDS[COMP_CWORD]} in
47 -*) case $1 in
48 EOF
49
50 #### Extract program names from lines such as:
51 #### .B %nmhlibexecdir%/install\-mh
52 #### .B new \-mode fnext
53 programs=`egrep '\.B [^\-]' "$mhchart" | \
54 sed -e 's/^\.B //' -e 's/ .*//' -e 's|%[^%]*%/||' -e 's/\\-/-/'`
55 for program in $programs; do
56 switches=`prog_switches "$program"`
57 # Support mime as alias for mhbuild.
58 [ "$program" = mhbuild ] && program='mhbuild|mime'
59 printf ' %s) switches=(%s) ;;\n' "$program" "$switches"
60
61 done
62 programs="$programs mime"
63
64 cat <<'EOF'
65 esac ;;
66
67 +*) switches=($(folder -all -fast -recurse | sed 's/^/+/')) ;;
68
69 #### Complete special message names, except after -file. The compopt -o default above provides filename completions. Assume that -file was fully completed, so don't need to match -fil, etc.
70 [flc.np]*) [ ${COMP_CWORD:-0} -lt 2 -o ${COMP_WORDS[$(($COMP_CWORD - 1))]} != -file ] &&
71 switches=(first last cur . next prev) ;;
72 esac
73
74 #### Special case: add "new" to mhpath's completions.
75 [ $1 = mhpath ] && switches=("${switches[*]}" + new)
76
77 COMPREPLY=($(compgen -W "${switches[*]}" -- ${COMP_WORDS[COMP_CWORD]}))
78 }
79
80 EOF
81
82 #### There better not be an nmh program name with whitespace in it.
83 echo complete -F _nmh $programs