1#compdef lp lpr lpq lprm lpoptions lpstat lpinfo lpadmin
2
3_lp_get_printer()
4{
5  # No reason to call _lp_get_printer when service == lpstat or lpinfo. Others
6  # matched below.
7  case $service in
8    (lpr|lpq|lprm)
9      [[ "$words" == (#I)*-P* ]] && printer="${${words##*(#I)-P( |)}%% *}"
10      ;;
11    (lp)
12      [[ "$words" == (#I)*-d* ]] && printer="${${words##*(#I)-d( |)}%% *}"
13      ;;
14    (lpadmin)
15      [[ "$words" == (#I)*-p* ]] && printer="${${words##*(#I)-p( |)}%% *}"
16      ;;
17    (lpoptions)
18      [[ "$words" == (#I)*-(d|p)* ]] && \
19	printer="${${words##*(#I)-(d|p)( |)}%% *}"
20      ;;
21  esac
22}
23
24_lp_job_options()
25{
26  local expl printer
27  local -a lopts_with_args lopts_no_args
28
29  # Generic options (from lp manual page)
30  lopts_with_args=( media orientation-requested sides number-up scaling cpi lpi
31    page-{bottom,left,right,top} page-ranges )
32
33  lopts_no_args=(fitplot landscape)
34
35  if [[ $service == 'lpadmin' ]]; then
36    # Extra options from lpadmin man page.
37    lopts_with_args+=(cupsIPPSupplies cupsSNMPSupplies job-k-limit
38      job-page-limit job-quota-period job-sheets-default name name-default
39      port-monitor printer-error-policy printer-is-shared printer-op-policy)
40  fi
41
42  _lp_get_printer
43  [[ -n "$printer" ]] && printer=(-p $printer)
44
45  # The program specified by the style list-printer-options should list jobs in
46  # the same style as lpoptions -l.
47  if compset -P '*='; then
48    # List values for the option
49    case ${IPREFIX%=} in
50      (media)
51	compadd "$@" a4 letter legal
52	;;
53      (orientation-requested)
54	compadd "$@" 4
55	;;
56      (sides)
57	compadd "$@" one-sided two-sided-{long,short}-edge
58	;;
59      (number-up)
60	_description -V option-o-1 expl "pages per sheet"
61	compadd "$expl[@]" 2 4 6 9 16
62	;;
63      (scaling|cpi|lpi|page-(bottom|left|right|top))
64	return 0; # Don't complete anything
65	;;
66      (cupsIPPSupplies|cupsSNMPSupplies|printer-is-shared)
67	compadd "$@" true false
68	;;
69      (printer-error-policy)
70	compadd "$@" abort-job retry-job retry-current-job stop-printer
71	;;
72      (*)
73	compadd "$@" \
74	  $(_call_program list-printer-options lpoptions $printer -l | \
75	    grep "^${IPREFIX%=}" | cut -d: -f2 | tr -d \* )
76	;;
77    esac
78  else
79    # List options
80    local eq_suffix
81
82    # Don't add an '=' suffix when completing lpoptions -r
83    if [[ $service == lpoptions && $words[CURRENT-1] == "-r" ]]; then
84      eq_suffix=()
85    else
86      eq_suffix=(-S '=')
87    fi
88
89    _description lpopts expl "generic printer options"
90    compadd "$expl[@]" $eq_suffix $lopts_with_args
91    compadd "$expl[@]" $lopts_no_args
92
93    _description printeropts expl "printer specific options"
94    compadd "$expl[@]" $eq_suffix \
95      $(_call_program list-printer-options \
96	lpoptions $printer -l | cut -d/ -f1)
97  fi
98}
99
100_lp_list_jobs()
101{
102  local ret=1 printer shown
103  local -a list disp strs
104
105  _lp_get_printer
106  [[ -n "$printer" ]] && printer=(-P $printer)
107
108  list=( ${(M)"${(f@)$(_call_program jobs lpq $printer 2> /dev/null)}":#[0-9]*} )
109
110  if (( $#list )); then
111    _tags users jobs
112
113    while _tags; do
114      if _requested users; then
115	strs=( "${(@)${(@)list##[^ 	]##[ 	]##[^ 	]##[ 	]##}%%[ 	]*}" )
116	if [[ -z "$shown" ]] &&
117	   zstyle -T ":completion:${curcontext}:users" verbose;
118	then
119	  disp=(-ld list)
120	  shown=yes
121	else
122	  disp=()
123	fi
124	_all_labels users expl user compadd "$disp[@]" -a strs ||
125	    _users && ret=0
126      fi
127      if _requested jobs; then
128	strs=( "${(@)${(@)list##[^ 	]##[ 	]##[^ 	]##[ 	]##[^ 	]##[ 	]##}%%[ 	]*}" )
129	if [[ -z "$shown" ]] &&
130	   zstyle -T ":completion:${curcontext}:jobs" verbose; then
131	  disp=(-ld list)
132	shown=yes
133	else
134	disp=()
135	fi
136	_all_labels jobs expl job compadd "$disp[@]" -a strs && ret=0
137      fi
138      (( ret )) || return 0
139    done
140  else
141    _message 'no print jobs'
142  fi
143  return 1
144}
145
146
147_lp()
148{
149  case $service in
150    (lpq)
151      _arguments \
152	'-E[Force encryption]' \
153	'-U:username (for connection to server):_users' \
154	'-h:alternate server:_hosts' \
155	'(-a)-P+[Destination printer]:printers:_printers' \
156	'(-P)-a[All printers]' \
157	'-l[Long listing]' \
158	'*:poll interval (+seconds):'
159      ;;
160
161    (lprm)
162      _arguments \
163	'-E[Force encryption]' \
164	'-U:username (for connection to server):_users' \
165	'-h:alternate server:_hosts' \
166	'-P+[Destination printer]:printers:_printers' \
167	'*:job ids:_lp_list_jobs'
168      ;;
169
170    (lpoptions)
171      _arguments \
172	'-E[Force encryption]' \
173	'-U:username (for connection to server):_users' \
174	'-h:alternate server:_hosts' \
175	'(-p -l -r -x)-d+[Set default printer]:printers:_printers' \
176	'(-l -x)*-o:job options:_lp_job_options' \
177	'(-d -x)-p+[Destination printer for options]:printers:_printers' \
178	'(-d -o -r -x)-l[List options]' \
179	'(-d -l -x)*-r:remove option:_lp_job_options' \
180	'(-d -l -r -o)-x+[Remove all options]:printers:_printers'
181      ;;
182
183    (lpstat)
184      _arguments \
185	'-E[Force encryption]' \
186	'-R[Shows print job ranking]' \
187	'-U:username (for connection to server):_users' \
188	'-W:which jobs:(completed not-completed)' \
189	'-a+[Show accepting state]:printers:_printers' \
190	'-c:printer classes:' \
191	'-d[Show current default destination]' \
192	'-h:hostname (alternate server):_hosts' \
193	'-l[Long listing]' \
194	'-o+[Destinations]:printers:_printers' \
195	'-p+:printers:_printers' \
196	'-r[CUPS server running status]' \
197	'-s[Status summary]' \
198	'-t[All status info]' \
199	'-u[List jobs by users]:users:_users' \
200	'-v+[Show devices]:printers:_printers'
201      ;;
202
203    (lpr)
204      _arguments \
205	'-E[Force encryption]' \
206	'-H:hostname (alternate server):_hosts' \
207	'(-C -J -T)'-{C,J,T}':job name:' \
208	'-P+[Destination printer]:printers:_printers' \
209	'-U:username (for connection to server):_users' \
210	'-#[Copies]:copies (1--100):' \
211	'-h[Disables banner printing]' \
212	'-l[Raw file]' \
213	'-m[Send an email on job completion]' \
214	'*-o:print job options:_lp_job_options' \
215	'-p[Format with shaded header incl. date, time etc.]' \
216	'-q[Hold job for printing.]' \
217	'-r[Delete files after printing]' \
218	'*:PS/PDF files:_pspdf'
219      ;;
220
221    (lp)
222      _arguments \
223	'-E[Force encryption]' \
224	'-U[Username (for connection to server)]:username:_users' \
225	'-c[(OBSOLETE) copy to spool dir before printing]' \
226	'-d+[Destination printer]:printers:_printers' \
227	'-h:hostname (alternate server):_hosts' \
228	'-i[Job id to modify]:job id:' \
229	'-m[Send an email on job completion]' \
230	'-n[Copies]:copies (1--100):' \
231	'*-o:print job options:_lp_job_options' \
232	'-q[Job priority -- 1 (lowest) to 100 (highest)]:priority:' \
233	'-s[Dont report resulting job IDs]' \
234	'-t[Sets the job name]:job name:' \
235	'-u[Job submission username]:username:_users' \
236	'-H[Time to print]:print time (or enter hh\:mm):(hold immediate restart resume)' \
237	'-P:page range list:' \
238	'*:PS/PDF files:_pspdf'
239      ;;
240
241    (lpinfo)
242      _arguments \
243	'-E[Force encryption]' \
244	'-U[Username (for connection to server)]:username:_users' \
245	'-h:hostname (alternate server):_hosts' \
246	'-l[Shows a "long" listing of devices or drivers]' \
247	{--exclude-schemes,--include-schemes}'[Device/PPD schemes to filter from results]:scheme-list:' \
248	'(-v --timeout)--device-id[IEEE-1284 device ID to match]:device-id-string:' \
249	'(-v --timeout)--language:locale:' \
250	'(-v --timeout)--product[Product to match]:name:' \
251	'(-v --timeout)--make-and-model[Make and model to match]:name:' \
252	'(-v --timeout)-m[List available drivers]' \
253	'(-m --device-id --language --make-and-model --product)--timeout[Timeout when listing devices with -v]:timeout (seconds):' \
254	'(-m --device-id --language --make-and-model --product)-v[List available devices]'
255      ;;
256
257    (lpadmin)
258      _arguments \
259	'-E[Force encryption/Enable destination]' \
260	'-U[Username (for connection to server)]:username:_users' \
261	'-h:hostname (alternate server):_hosts' \
262	'(-p -R -x -o)-d+[Default printer]:printers:_printers' \
263	'(-d -x)-p+[Configure printer]:printers:_printers' \
264	'(-p -R -d -o)-x+[Delete printer]:printers:_printers' \
265	'(-x -d)-R[Name-default]:name-default:' \
266	'-c:printer classes:' \
267	'-m:model:' \
268	'(-x -d)*-o:options:_lp_job_options' \
269	'-r[Remove from class]:class:' \
270	'-u[Access policy]:access policy:' \
271	'-v[Device-uri of printer queue]:device-uri:' \
272	'-D[Text description of destination]:info:' \
273	'-L[Location of the printer]:location:' \
274	'-P[PPD file to use]:PPD file:_files "*.(#i)ppd(-.)"'
275  esac
276}
277
278_lp "$@"
279