[comp.emacs] using a subprocess for printing

ron@mlfarm.UUCP (Ronald Florence) (10/07/89)

I would welcome suggestions on how to start a subprocess to run troff
on the contents of a GNU Emacs buffer.  Currently, I use the following
code

     (defun troff-buffer (&optional copies)
       "Typeset region contents after formatting with `troff -mm -rN2'.
     Optional prefix argument specifies number of copies."
       (interactive "P")
       (let ((buffer (get-buffer-create "*printer command output*")))
	 (save-excursion
	   (set-buffer buffer)
	   (erase-buffer))
	 (message "Spooling...")
	 (if (null copies) (setq copies "1"))
	 (call-process-region (point-min) (point-max) "emacstroff" 
			      nil buffer nil  
			      (concat "-n" copies))
	 (if (> (buffer-size) 0)
	     (progn
	       (set-buffer buffer)
	       (goto-char (point-min))
	       (end-of-line)
	       (message "%s" (buffer-substring 1 (point))))
	   (message "(Printer command failed)")))
       (kill-buffer "*printer command output*"))

The "emacstroff" is a shell script with printer and troff options

     :
     #! /bin/sh
     # emacstroff

     troff -t -mm -rN2 | lp -ot $1

It works, but I sit and twiddle my thumbs, staring at the
"Spooling..." message while troff formats.  Is there a way to use a
subprocess to do this?  We are running Emacs 18.50 with Xenix 2.3.2,
using the select() call.  There are plenty of free ptys for
subprocesses.

Thanks in advance for any suggestions.
-- 

Ronald Florence			{hsi,rayssd}!mlfarm!ron

tale@pawl.rpi.edu (David C Lawrence) (10/11/89)

[Fourth UUCP bounce today.  Blargh.]

In <337@mlfarm.UUCP> ron@mlfarm.UUCP (Ronald Florence) writes:
Ronald> I would welcome suggestions on how to start a subprocess to run troff
Ronald> on the contents of a GNU Emacs buffer.  Currently, I use the following
Ronald> code

Ronald> [Code deleted.]

The problem here is that you are using call-process-region, which is
designed to wait until the process returns.  GNU Emacs has much more
rich process handling ability as long as "subprocesses" was #defined
and accurate for your system when Emacs was compiled.  From what you
said later about your system, this is probably the case.

Based on the code you provided, the same results, but asynchronous
execution of the script, could be accomplished using the following
skeleton algorithm:

(defun your-entry-command ...
   (interactive ...)
   (start-process ...) ;; pick up process-object here
   (set-process-sentinel process-object 'your-proc-sentinel)
   (process-send-region process-object (point-min) (point-max)))

(defun your-proc-sentinel ...
   (if the process has exited ...
       ( ... do your clean-up stuff ...)))

For the start-process you can get-buffer-create and erase-buffer an
object based on " *emacstroff output*" or such.  The leading space
makes it a hidden buffer because the user shouldn't bother with the
fact that it exists.  The clean-up part is where you build your
minibuffer message and kill the temporary buffer.

For more information do an apropos (not command-apropos) on "process"
and see what it turns up.

Dave
-- 
 (setq mail '("tale@pawl.rpi.edu" "tale@itsgw.rpi.edu" "tale@rpitsmts.bitnet"))