Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: better printer finding



This improves _printers in two ways:

- the cached output is supplemented by the output from lpstat -a,
if the command is available.  Typically the info is in printers.conf
but lpstat -a is more authoritative source.

- if there's a -h <server>[:<port>] option, and lpstat is available, use
that to find printers on the given server.  The result is not cached
(this would have to be done per server, but as lpstat is doing the
hard work I don't think it's worth it).

This is aimed at CUPS, but I think the options are chosen not to
conflict with traditional Berkeley and System V printing options.
I'm surely you'll let me know otherwise.

Index: Completion/Unix/Type/_printers
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Type/_printers,v
retrieving revision 1.6
diff -u -r1.6 _printers
--- Completion/Unix/Type/_printers	8 Jun 2005 12:45:36 -0000	1.6
+++ Completion/Unix/Type/_printers	14 May 2007 15:15:58 -0000
@@ -1,6 +1,7 @@
 #compdef -value-,PRINTER,-default- -value-,LPDEST,-default-
 
-local expl ret=1 list disp sep tmp
+local expl ret=1 list disp sep tmp servopt
+integer ind
 
 if (( $+commands[lsallq] )); then
   # Use AIX's command to list print queues
@@ -10,6 +11,31 @@
 
 zstyle -s ":completion:${curcontext}:printers" list-separator sep || sep=--
 
+# If we've been given a different print server on the command line,
+# list printers on that.  This information shouldn't be cached.
+# (I) searches backwards---this is good, since some commands, such
+# as lpoptions, can take multiple -h arguments and we want the last
+# one before the current argument.  For lpr, -h might mean something
+# else and the option is -H.
+if [[ $service = lpr ]]; then
+  servopt=-H
+else
+  servopt=-h
+fi
+if (( $+commands[lpstat] )); then
+  ind=${words[1,CURRENT][(I)${servopt}*]}
+  if (( ind > 0 )); then
+    if [[ $words[ind] = $servopt ]]; then
+      tmp=-h$words[ind+1]
+    else
+      tmp=-h${words[ind][3,-1]}
+    fi
+    _wanted printers expl printer compadd "$@" - \
+      ${${(f)"$(lpstat $tmp -a)"}%% *} && return
+  fi
+fi
+
+
 if (( ! $+_lp_cache )); then
   local file entry names i
 
@@ -40,6 +66,17 @@
     done < $file[1]
   fi
 
+  if (( $+commands[lpstat] )); then
+    # If lpstat it exists, it's possible there are some other
+    # printers there, so add them.
+    lpstat -a | while read entry; do
+      entry=${entry%%[[:blank:]]*}
+      if (( ${_lp_cache[(I)$entry:*]} == 0 )); then
+	_lp_cache+=( $entry )
+      fi
+    done
+  fi
+
   if [[ $OSTYPE = solaris* ]] && (( ${+commands[ypcat]} )) &&
       tmp=$(_call_program printers ypcat printers.conf.byname 2>/dev/null); then
     _lp_cache+=( ${${${(S)${(f)tmp}/(#b):*((#e)|description=([^:]#):)*/:${match[2]}|}%%|*}:#_default*} ) # If you use YP

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview



Messages sorted by: Reverse Date, Date, Thread, Author