summary refs log tree commit diff
path: root/Completion/User/_perl_modules
blob: f28fc3eccd205b106fcd1414881705cb460a331a (plain) (blame)
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
#compdef pmpath pmvers pmdesc pmload pmexp pmeth pmls pmcat pman pmfunc podgrep podtoc podpath
#
# _perl_modules - zsh completion function
#
# Adam Spiers <adam@spiers.net>
#
# Calculate all installed Perl modules.  The result is cached
# for future use.
#
# Available styles:
#
# * try-to-use-pminst
#
#   Set this if you have pminst and want to use it.  The zsh code
#   actually produces better results because pminst misses modules of
#   the form Foo/bar/Baz.pm through its clumsy -d && /^[A-Z]/ && prune
#   algorithm (the zsh code does almost the same, but only misses
#   modules which don't begin with an uppercase letter).

local opts
zparseopts -D -a opts S: q

if [[ ${+_perl_modules} -eq 0 ]]; then
  if zstyle -t ":completion:${curcontext}:modules" try-to-use-pminst \
     && (( ${+commands[pminst]} )); then
    _perl_modules=( $(pminst) )
  else
    local inc libdir new_pms
    if (( ${+commands[perl]} )); then
      inc=( $( perl -e 'print "@INC"' ) )
    else
      # If perl isn't there, one wonders why the user's trying to
      # complete Perl modules.  Maybe her $path is wrong?
      _message "Didn't find perl on \$PATH; guessing @INC ..."

      setopt localoptions extendedglob
      inc=( /usr/lib/perl5{,/{site_perl/,}<5->.([0-9]##)}(N) 
            ${(s.:.)PERL5LIB} )
    fi

    typeset -agU _perl_modules  # _perl_modules is global, no duplicates
    _perl_modules=( )

    for libdir in $inc; do
      # Ignore cwd - could be too expensive e.g. if we're near /
      if [[ $libdir == '.' ]]; then break; fi

      # Find all modules
      if [[ -d $libdir && -x $libdir ]]; then
      cd $libdir
      new_pms=( {[A-Z]*/***/,}*.pm~*blib*(N) )
      cd $OLDPWD
      fi

      # Convert to Perl nomenclature
      new_pms=( ${new_pms:r:fs#/#::#} )

      _perl_modules=( $new_pms $_perl_modules )
    done
  fi
fi

local expl

_wanted modules expl 'Perl modules' compadd "$opts[@]" - $_perl_modules