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

PATCH: update ghostscript completion



This updates _gs. Any objections if I rename it to _ghostscript which is
more meaningful and more inline with our conventions?

My intention had just been to update the options but it seems that you
can invoke gs directly to query some matches using PostScript code. So I
had fun playing with that, for example you can get a list of supported
paper sizes with: 'statusdict /.pagetypenames get { = } forall'. I
suspect that a lot more along these lines is possible so if someone out
there is a dab hand with ghostscript/postscript it'd be good to expand
this further. It'd be particularly useful to complete options specific
to output drivers (pdfwrite, jpeg etc).

The redirection in _call_program seems to prevent gs from working so
this doesn't use _call_program.

Oliver

diff --git a/Completion/Unix/Command/_gs b/Completion/Unix/Command/_gs
index 98ba14912..2c6f40a35 100644
--- a/Completion/Unix/Command/_gs
+++ b/Completion/Unix/Command/_gs
@@ -1,12 +1,18 @@
 #compdef gs ghostscript
 
-if compset -N --; then
+local -a specs names device
+
+device=( ${${path[1,CURRENT-1]}[(R)-sDEVICE=*]} )
+
+if compset -N '-[-+@]'; then
   if [[ CURRENT -eq 1 ]]; then
     _pspdf
   else
     _message -e arguments 'userdict ARGUMENTS'
     return 1
   fi
+elif compset -P '@'; then
+  _files
 else
   local curcontext="$curcontext" state line expl ret=1
   typeset -A opt_args
@@ -15,20 +21,28 @@ else
     '-q[quiet startup]' \
     '-g-[set device size]:device size (<width>x<height>):' \
     '-r-[set resolution]:resolution (<val> or <x>x<y>):' \
-    '-I[add to library file search path]:directory:_dir_list' \
+    '-I+[add to library file search path]:directory:_dir_list' \
     \*-{d,D}'-[define name with definition]:def:->dname' \
     \*-{s,S}'-[define name with value]:def:->sname' \
+    '-u[undefine name, cancelling -d or -s]:name:->uname' \
+    '-P[look first in the current directory for library files]' \
+    '-c[specify postscript code]:*[-@]*:postscript (-f to end)' \
+    '-o+[specify OutputFile and enable -dPATCH and -dNOPAUSE options]: :->outfile' \
+    '(- *)-'{h,\?}'[display useful options and available devices]' \
+    '!-f-:PostScript or PDF file:_pspdf' \
     '*:PostScript or PDF file:_pspdf' && ret=0
 
+  [[ $state = sname ]] && compset -P '(stdout|OutputFile)=' && state=outfile
   case "$state" in
   dname)
     if [[ "$PREFIX" = *\=* ]]; then
       _message -e definition-values 'systemdict definition value'
     else
-      _wanted names expl 'systemdict definition name' \
-          compadd -M 'm:{a-z}={A-Z}' - \
-                  DISKFONTS NOCACHE NOBIND NODISPLAY NOPAUSE PLATFONTS SAFER \
-                  WRITESYSTEMDICT && ret=0
+      names=( ${${(f)"$($words[1] -q -o /dev/null -DNODISPLAY -c 'systemdict { type /booleantype eq { = } if } forall')"}:#*[a-z]*} )
+      _wanted boolean-definitions expl 'systemdict boolean definition' compadd -M 'm:{a-z}={A-Z}' -a names && ret=0
+      names=( FirstPage LastPage
+          $($words[1] -q -o /dev/null $device -DNODISPLAY -c 'currentpagedevice { type /integertype eq { = } if } forall') )
+      _wanted integer-definitions expl 'currentpagedevice integer definition' compadd -S = -M 'm:{a-z}={A-Z}' -a names && ret=0
     fi
     ;;
   sname)
@@ -36,21 +50,71 @@ else
       case "$IPREFIX" in
       *DEVICE\=)
         _wanted devices expl 'ghostscript device' \
-            compadd - "${(@)${=${$(_call_program devices gs -h)##* devices:}%%Search path:*}:#}" && ret=0
-        ;;
-      *OutputFile\=)
-        _description files expl 'output file'
-        _files "$expl[@]" && ret=0
+	    compadd - ${(0)"$($words[1] -q -o /dev/null -DNODISPLAY -c 'devicenames { dup length string cvs print (\0) print } forall')"} && ret=0
         ;;
+      *PAPERSIZE=)
+	names=( $($words[1] -q -o /dev/null -DNODISPLAY -c 'statusdict /.pagetypenames get { = } forall ') )
+	_description paper-sizes expl 'paper size'
+	compadd "$expl[@]" -a names && ret=0
+      ;;
+      *PDFSETTINGS=)
+	_description settings expl 'configuration'
+	compadd "$expl[@]" /screen /ebook /printer /prepress /default && ret=0
+      ;;
+      *AutoRotatePages=)
+	_description settings expl 'orientation selection'
+	compadd "$expl[@]" /None /All /PageByPage && ret=0
+      ;;
+      *CompatibilityLevel=)
+	_description pdf-levels expl 'PDF compatibility level'
+	compadd "$expl[@]" 1.{2,3,4} && ret=0
+      ;;
+      *FONT=) # e.g. SUBSTFONT
+	_wanted fonts expl 'font' compadd - \
+	    $($words[1] -q -o /dev/null -DNODISPLAY -c '(*) { = } 256 string /Font resourceforall') && ret=0
+      ;;
       *)
         _message -e values 'systemdict value'
         return 1
+      ;;
       esac
     else
-      _wanted names expl 'systemdict name' \
-          compadd -S\= -M 'm:{a-z}={A-Z}' - DEVICE OutputFile && ret=0
+      _wanted definition-names expl 'systemdict name' compadd -S\= -M 'm:{a-z}={A-Z}' - \
+	  DEVICE OutputFile PAPERSIZE PDFSETTINGS CompatibilityLevel stdout COLORSCREEN \
+	  DITHERPPI InterpolateControl TextAlphaBits GraphicsAlphaBits PageList SUBSTFONT \
+	  && ret=0
     fi
     ;;
+  uname)
+    _message -e definition-names 'systemdict definition name'
+  ;;
+  outfile)
+    if compset -P "*%"; then
+      specs=(
+	{d,i}':signed decimal number or with leading " numeric value of following character'
+	'o:unsigned octal number'
+	'u:unsigned decimal number'
+	{x,X}':unsigned hexadecimal number, letters capitalized as x'
+	'%:a literal percent sign'
+	'#:alternate form'
+	'0:zero pad to length'
+	'-:left adjust result'
+	'+:always place sign before number'
+	'.:precision'
+      )
+      if ! compset -P '[0-9#.+-]##'; then
+	specs+=(
+	  'stdout:redirect to standard output'
+	  'stderr:redirect to standard error'
+	  'pipe%:redirect to a pipe'
+	)
+      fi
+      _describe -t print-format-specifiers 'print format specifier' specs -S '' && ret=0
+    else
+      _description files expl 'output file'
+      _files "$expl[@]" && ret=0
+    fi
+  ;;
   esac
 
   return ret



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