Subject: command line enscript - follow up

From: "George D. Hadden" <[email-masked]>
Date: Wed, 9 Oct 96 18:16:18 CDT

thanks to charles henkel for the enscript look-alike a2ps (available
at ftp://mack.rt66.com/users/csh/a2ps.zip).  here's an enscript-buffer
(which i for some reason call "my-print-buffer" ... oh well) that
might be generally useful.  since emacs on w95 only allows one
subprocess, the program checks to see if a shell is up or not before
executing the command.  with a prefix arg, the region is printed.

comments welcome.  have fun.

-geo
---
George D. Hadden, Senior Research Fellow
Honeywell Technology Center, 3660 Technology Drive, Minneapolis, MN 55418
[email-masked], (612)951-7769(voice), (612)951-7438(fax)

****************
;; enscript-buffer

(setq print-command "a2ps")			; "copy" would work here, too
;(setq print-switches "> lpt1:")		; ok, not really switches ...
(setq print-switches "> \\\\Casper\\big_john_hp5_2515")

(define-key global-map "\M-o" 'my-print-buffer)

(defun my-print-buffer (n)
  "send buffer to printer; with prefix arg, send region"
  (interactive "P")
  (save-window-excursion
    (let* ((temp-file
	    ;; this gets a guaranteed unused filename in the temp directory
	    ;; note, errors may cause this file not to be deleted, but judging
	    ;; from the junk ms and others leave in that directory, it 
	    ;; probably won't be a big problem.
	    (find-unused-filename (concat (getenv "TEMP") "\\") "eprnt"))
	   (command (concat print-command " " temp-file " " print-switches))
	   (del-command (concat "del " temp-file)))
      (write-region
       (if n (min (mark) (point)) (point-min))
       (if n (max (mark) (point)) (point-max))
       temp-file)
      ;; there's a choice here because in my nt-emacs on w95, i can have
      ;; at most one shell process.  this handles the two cases (shell buffer
      ;; or no shell buffer)
      (cond ((get-buffer-process (get-buffer "*shell*"))
	     (set-buffer "*shell*")
	     (comint-simple-send (get-buffer-process (current-buffer))
				 command)
	     ;; we don't use delete-file here so the file is still
	     ;; around by the time we get around to printing it
	     (comint-simple-send (get-buffer-process (current-buffer))
				 del-command))
	    (t (shell-command command)
	       (shell-command del-command))))
    (message "printing done")))

; find an unused temp file name
;  file-exists-p
(defun find-unused-filename (directory prefix)
  (let* ((n 0)
	 (path))
    (while (file-exists-p
	    (setq path (concat directory prefix (int-to-string n))))
      (setq n (1+ n)))
    path))

;;; this isn't used now, but here it is ...

(defun buffer-exists-p (name)
  (member name (mapcar (function buffer-name) (buffer-list))))