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 `printer-name', `lpr-switches' and `lpr-command'.
34 "Print Emacs buffer on line printer"
38 (defcustom printer-name
39 (if (memq system-type '(ms-dos windows-nt)) "PRN")
40 "*The name of a local printer to which data is sent for printing.
41 \(Note that PostScript files are sent to `ps-printer-name', which see.\)
43 On Unix-like systems, a string value should be a name understood by
46 On MS-DOS and MS-Windows systems, it is the name of a printer device or
47 port. Typical non-default settings would be \"LPT1\" to \"LPT3\" for
48 parallel printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial
49 printers, or \"//hostname/printer\" for a shared network printer. You
50 can also set it to a name of a file, in which case the output gets
51 appended to that file. If you want to discard the printed output, set
53 :type 'file ; could use string but then we lose completion for files.
57 (defcustom lpr-switches nil
58 "*List of strings to pass as extra options for the printer program.
59 It is recommended to set `printer-name' instead of including an explicit
62 :type '(repeat (string :tag "Argument"))
65 (defcustom lpr-add-switches (eq system-type 'berkeley-unix)
66 "*Non-nil means construct -T and -J options for the printer program.
67 These are made assuming that the program is `lpr';
68 if you are using some other incompatible printer program,
69 this variable should be nil."
74 (defcustom lpr-command
75 (if (memq system-type '(usg-unix-v dgux hpux irix))
77 "*Name of program for printing a file."
81 ;; Default is nil, because that enables us to use pr -f
82 ;; which is more reliable than pr with no args, which is what lpr -p does.
83 (defcustom lpr-headers-switches nil
84 "*List of strings of options to request page headings in the printer program.
85 If nil, we run `lpr-page-header-program' to make page headings
86 and print the result."
87 :type '(repeat (string :tag "Argument"))
90 (defcustom print-region-function nil
91 "Function to call to print the region on a printer.
92 See definition of `print-region-1' for calling conventions."
96 (defcustom lpr-page-header-program "pr"
97 "*Name of program for adding page headers to a file."
101 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
102 ;; So it looks like -F is a better default.
103 (defcustom lpr-page-header-switches '("-F")
104 "*List of strings to use as options for the page-header-generating program.
105 The variable `lpr-page-header-program' specifies the program to use."
106 :type '(repeat string)
111 "Print buffer contents as with Unix command `lpr'.
112 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
114 (print-region-1 (point-min) (point-max) lpr-switches nil))
117 (defun print-buffer ()
118 "Print buffer contents as with Unix command `lpr -p'.
119 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
121 (print-region-1 (point-min) (point-max) lpr-switches t))
124 (defun lpr-region (start end)
125 "Print region contents as with Unix command `lpr'.
126 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
128 (print-region-1 start end lpr-switches nil))
131 (defun print-region (start end)
132 "Print region contents as with Unix command `lpr -p'.
133 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
135 (print-region-1 start end lpr-switches t))
137 (defun print-region-1 (start end switches page-headers)
138 ;; On some MIPS system, having a space in the job name
139 ;; crashes the printer demon. But using dashes looks ugly
140 ;; and it seems to annoying to do for that MIPS system.
141 (let ((name (concat (buffer-name) " Emacs buffer"))
142 (title (concat (buffer-name) " Emacs buffer"))
143 ;; Make pipes use the same coding system as
144 ;; writing the buffer to a file would.
145 (coding-system-for-write
146 (or coding-system-for-write buffer-file-coding-system))
147 (coding-system-for-read
148 (or coding-system-for-read buffer-file-coding-system))
153 (if lpr-headers-switches
154 ;; It is possible to use an lpr option
155 ;; to get page headers.
156 (setq switches (append (if (stringp lpr-headers-switches)
157 (list lpr-headers-switches)
158 lpr-headers-switches)
161 (if switches (concat " with options "
162 (mapconcat 'identity switches " "))
164 (message "Spooling%s..." switch-string)
166 (let ((new-coords (print-region-new-buffer start end)))
167 (setq start (car new-coords) end (cdr new-coords))
168 (setq tab-width width)
171 (setq end (point-marker)))
172 (untabify (point-min) (point-max))))
174 (if lpr-headers-switches
175 ;; We handled this above by modifying SWITCHES.
177 ;; Run a separate program to get page headers.
178 (let ((new-coords (print-region-new-buffer start end)))
179 (setq start (car new-coords) end (cdr new-coords)))
180 (apply 'call-process-region start end lpr-page-header-program
182 (nconc (list "-h" title)
183 lpr-page-header-switches))
184 (setq start (point-min) end (point-max))))
185 (apply (or print-region-function 'call-process-region)
186 (nconc (list start end lpr-command
188 (nconc (and lpr-add-switches
190 ;; These belong in pr if we are using that.
191 (and lpr-add-switches lpr-headers-switches
193 (and (stringp printer-name)
194 (list (concat "-P" printer-name)))
197 (set-marker end nil))
198 (message "Spooling%s...done" switch-string))))
200 ;; This function copies the text between start and end
201 ;; into a new buffer, makes that buffer current.
202 ;; It returns the new range to print from the new current buffer
205 (defun print-region-new-buffer (ostart oend)
206 (if (string= (buffer-name) " *spool temp*")
208 (let ((oldbuf (current-buffer)))
209 (set-buffer (get-buffer-create " *spool temp*"))
210 (widen) (erase-buffer)
211 (insert-buffer-substring oldbuf ostart oend)
212 (cons (point-min) (point-max)))))
214 (defun printify-region (begin end)
215 "Replace nonprinting characters in region with printable representations.
216 The printable representations use ^ (for ASCII control characters) or hex.
217 The characters tab, linefeed, space, return and formfeed are not affected."
222 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
223 (setq c (preceding-char))
224 (delete-backward-char 1)
227 (format "\\^%c" (+ c ?@))
228 (format "\\%02x" c)))))))