1 ;;; lpr.el --- print Emacs buffer on line printer.
3 ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Commands to send the region or a buffer your printer. Entry points
28 ;; are `lpr-buffer', `print-buffer', lpr-region', or `print-region'; option
29 ;; variables include `lpr-switches' and `lpr-command'.
34 (defvar lpr-switches nil
35 "*List of strings to pass as extra options for the printer program.
38 (defvar lpr-add-switches (eq system-type 'berkeley-unix)
39 "*Non-nil means construct -T and -J options for the printer program.
40 These are made assuming that the program is `lpr';
41 if you are using some other incompatible printer program,
42 this variable should be nil.")
46 (if (memq system-type '(usg-unix-v dgux hpux irix))
48 "*Name of program for printing a file.")
50 ;; Default is nil, because that enables us to use pr -f
51 ;; which is more reliable than pr with no args, which is what lpr -p does.
52 (defvar lpr-headers-switches nil
53 "*List of strings of options to request page headings in the printer program.
54 If nil, we run `lpr-page-header-program' to make page headings
55 and print the result.")
57 (defvar print-region-function nil
58 "Function to call to print the region on a printer.
59 See definition of `print-region-1' for calling conventions.")
61 (defvar lpr-page-header-program "pr"
62 "*Name of program for adding page headers to a file.")
64 (defvar lpr-page-header-switches '("-f")
65 "*List of strings to use as options for the page-header-generating program.
66 The variable `lpr-page-header-program' specifies the program to use.")
70 "Print buffer contents as with Unix command `lpr'.
71 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
73 (print-region-1 (point-min) (point-max) lpr-switches nil))
76 (defun print-buffer ()
77 "Print buffer contents as with Unix command `lpr -p'.
78 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
80 (print-region-1 (point-min) (point-max) lpr-switches t))
83 (defun lpr-region (start end)
84 "Print region contents as with Unix command `lpr'.
85 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
87 (print-region-1 start end lpr-switches nil))
90 (defun print-region (start end)
91 "Print region contents as with Unix command `lpr -p'.
92 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
94 (print-region-1 start end lpr-switches t))
96 (defun print-region-1 (start end switches page-headers)
97 ;; On some MIPS system, having a space in the job name
98 ;; crashes the printer demon. But using dashes looks ugly
99 ;; and it seems to annoying to do for that MIPS system.
100 (let ((name (concat (buffer-name) " Emacs buffer"))
101 (title (concat (buffer-name) " Emacs buffer"))
102 ;; On MS-DOS systems, make pipes use binary mode if the
103 ;; original file is binary.
104 (binary-process-input buffer-file-type)
105 (binary-process-output buffer-file-type)
110 (if lpr-headers-switches
111 ;; It is possible to use an lpr option
112 ;; to get page headers.
113 (setq switches (append (if (stringp lpr-headers-switches)
114 (list lpr-headers-switches)
115 lpr-headers-switches)
118 (if switches (concat " with options "
119 (mapconcat 'identity switches " "))
121 (message "Spooling%s..." switch-string)
123 (let ((new-coords (print-region-new-buffer start end)))
124 (setq start (car new-coords) end (cdr new-coords))
125 (setq tab-width width)
128 (setq end (point-marker)))
129 (untabify (point-min) (point-max))))
131 (if lpr-headers-switches
132 ;; We handled this above by modifying SWITCHES.
134 ;; Run a separate program to get page headers.
135 (let ((new-coords (print-region-new-buffer start end)))
136 (setq start (car new-coords) end (cdr new-coords)))
137 (apply 'call-process-region start end lpr-page-header-program
139 (nconc (and lpr-add-switches
141 lpr-page-header-switches))
142 (setq start (point-min) end (point-max))))
143 (apply (or print-region-function 'call-process-region)
144 (nconc (list start end lpr-command
146 (nconc (and lpr-add-switches
148 ;; These belong in pr if we are using that.
149 (and lpr-add-switches lpr-headers-switches
153 (set-marker end nil))
154 (message "Spooling%s...done" switch-string))))
156 ;; This function copies the text between start and end
157 ;; into a new buffer, makes that buffer current.
158 ;; It returns the new range to print from the new current buffer
161 (defun print-region-new-buffer (ostart oend)
162 (if (string= (buffer-name) " *spool temp*")
164 (let ((oldbuf (current-buffer)))
165 (set-buffer (get-buffer-create " *spool temp*"))
166 (widen) (erase-buffer)
167 (insert-buffer-substring oldbuf ostart oend)
168 (cons (point-min) (point-max)))))
170 (defun printify-region (begin end)
171 "Turn nonprinting characters (other than TAB, LF, SPC, RET, and FF)
172 in the current buffer into printable representations as control or
173 hexadecimal escapes."
178 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
179 (setq c (preceding-char))
180 (delete-backward-char 1)
183 (format "\\^%c" (+ c ?@))
184 (format "\\%02x" c)))))))