File: bash_completion_nmh-gen

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (84 lines) | stat: -rwxr-xr-x 2,714 bytes parent folder | download | duplicates (3)
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
#! /bin/sh
#
# Generates bash_completion_nmh from man/mh-chart.man.
#
# This code is Copyright (c) 2016, by the authors of nmh.
# See the COPYRIGHT file in the root directory of the nmh
# distribution for complete copyright information.

mhchart=man/mh-chart.man

#### Extract switches for nmh program.
prog_switches() {
    program=$1

    #### Extract lines from just after .B program-name to just before
    #### blank line, and then only those that start with .RB, e.g.,
    #### .RB [ \-list " | " \-nolist ]
    #### .RB " | " \-autoheaderencoding ]
    #### .RB [ \-\|\-component
    #### paste(1) needs the explicit - file on some platforms (Solaris)
    awk '/^.B.*(\/| )'"$program"'/,/^$/ {
        if (/^\.RB /) print
    }' "$mhchart" | \
    sed -e 's/\.RB //g' -e 's/\[//g' -e 's/\]//g' \
        -e 's/\(\\\)*//g' -e 's/-|-/-/g' -e 's/" *| "//g' \
        -e 's/^ *//' -e 's/ *$//' -e 's/  */ /g' | \
    paste -s -d ' ' -
}

cat <<'EOF'
# bash completions for nmh commands                        -*- shell-script -*-
# This file was generated by etc/bash_completion_nmh-gen.
#
# This code is Copyright (c) 2016, by the authors of nmh.
# See the COPYRIGHT file in the root directory of the nmh
# distribution for complete copyright information.
#
# To use:  source at a bash prompt or in an initialization file.

_nmh() {
  local -a switches

  COMPREPLY=()
  #### Complete filenames.
  compopt -o default

  case ${COMP_WORDS[COMP_CWORD]} in
  -*) case $1 in
EOF

#### Extract program names from lines such as:
#### .B %nmhlibexecdir%/install\-mh
#### .B new \-mode fnext
programs=`grep '\.B [^\-]' "$mhchart" | \
          sed -e 's/^\.B //' -e 's/ .*//' -e 's|%[^%]*%/||' -e 's/\\-/-/'`
for program in $programs; do
    switches=`prog_switches "$program"`
    # Support mime as alias for mhbuild.
    [ "$program" = mhbuild ]  &&  program='mhbuild|mime'
    printf '      %s) switches=(%s) ;;\n' "$program" "$switches"

done
programs="$programs mime"

cat <<'EOF'
      esac ;;

  +*) switches=($(folder -all -fast -recurse | sed 's/^/+/')) ;;

  #### 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.
  [flc.np]*) [ ${COMP_CWORD:-0} -lt 2  -o  ${COMP_WORDS[$(($COMP_CWORD - 1))]} != -file ]  &&
                 switches=(first last cur . next prev) ;;
  esac

  #### Special case:  add "new" to mhpath's completions.
  [ $1 = mhpath ]  &&  switches=("${switches[*]}" + new)

  COMPREPLY=($(compgen -W "${switches[*]}" -- ${COMP_WORDS[COMP_CWORD]}))
}

EOF

#### There better not be an nmh program name with whitespace in it.
echo complete -F _nmh $programs