1#compdef -value-,PRINTER,-default- -value-,LPDEST,-default-
2
3local expl ret=1 list disp sep tmp servopt
4integer ind
5
6if (( $+commands[lsallq] )); then
7  # Use AIX's command to list print queues
8  _wanted printers expl printer compadd "$@" - $(lsallq)
9  return
10fi
11
12zstyle -s ":completion:${curcontext}:printers" list-separator sep || sep=--
13
14# If we've been given a different print server on the command line,
15# list printers on that.  This information shouldn't be cached.
16# (I) searches backwards---this is good, since some commands, such
17# as lpoptions, can take multiple -h arguments and we want the last
18# one before the current argument.  For lpr, -h might mean something
19# else and the option is -H.
20if [[ $service = lpr ]]; then
21  servopt=-H
22else
23  servopt=-h
24fi
25if (( $+commands[lpstat] )); then
26  ind=${words[1,CURRENT][(I)${servopt}*]}
27  if (( ind > 0 )); then
28    if [[ $words[ind] = $servopt ]]; then
29      tmp=-h$words[ind+1]
30    else
31      tmp=-h${words[ind][3,-1]}
32    fi
33    _wanted printers expl printer compadd "$@" - \
34      ${${(f)"$(_call_program printers lpstat $tmp -a 2>/dev/null)"}%% *} &&
35        return
36  fi
37fi
38
39
40if (( ! $+_lp_cache )); then
41  local file entry names i
42
43  file=( /etc/(printcap|printers.conf) )
44
45  _lp_cache=()
46  _lp_alias_cache=()
47
48  if (( $#file )); then
49    while read entry; do
50      if [[ "$entry" = [^[:blank:]\#\*_]*:* ]]; then
51        names=( "${(s:|:)entry%%:*}" )
52        if [[ "$entry" = *:description=* ]]; then
53          disp="${${entry##*:description=}%%:*}"
54        elif [[ $#names -gt 1 && "$names[-1]" = *\ * ]] ;then
55          disp="$names[-1]"
56        else
57          disp=''
58        fi
59        if [[ -n "$disp" ]]; then
60          _lp_cache+=( "${names[1]}:${disp}" )
61  	  _lp_alias_cache+=( "${(@)^names[2,-1]:#*\ *}:${disp}" )
62        else
63          _lp_cache+=( "${names[1]}" )
64  	  _lp_alias_cache+=( "${(@)names[2,-1]:#*\ *}" )
65        fi
66      fi
67    done < $file[1]
68  fi
69
70  if (( $+commands[lpstat] )); then
71    # If lpstat it exists, it's possible there are some other
72    # printers there, so add them.
73    _call_program printers lpstat -a 2>/dev/null | while read entry; do
74      entry=${entry%%[[:blank:]]*}
75      if (( ${_lp_cache[(I)$entry:*]} == 0 )); then
76	_lp_cache+=( $entry )
77      fi
78    done
79  fi
80
81  if [[ $OSTYPE = solaris* ]] && (( ${+commands[ypcat]} )) &&
82      tmp=$(_call_program printers ypcat printers.conf.byname 2>/dev/null); then
83    _lp_cache+=( ${${${(S)${(f)tmp}/(#b):*((#e)|description=([^:]#):)*/:${match[2]}|}%%|*}:#_default*} ) # If you use YP
84  fi
85
86  (( $#_lp_cache )) || _lp_cache=( 'lp0:guessed default printer' )
87  (( $#_lp_alias_cache )) || unset _lp_alias_cache
88fi
89
90if zstyle -T ":completion:${curcontext}:printers" verbose; then
91  zformat -a list " $sep " "$_lp_cache[@]"
92  disp=(-ld list)
93else
94  disp=()
95fi
96_wanted printers expl printer \
97    compadd "$@" "$disp[@]" - "${(@)_lp_cache%%:*}" && return 0
98
99(( $+_lp_alias_cache )) || return 1
100
101if zstyle -T ":completion:${curcontext}:printers" verbose; then
102  zformat -a list " $sep " "$_lp_alias_cache[@]"
103  disp=(-ld list)
104else
105  disp=()
106fi
107_wanted printers expl printer \
108    compadd "$@" "$disp[@]" - "${(@)_lp_alias_cache%%:*}" && return 0
109
110return 1
111