1 ;;; lpr.el --- print Emacs buffer on line printer
3 ;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2012
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Commands to send the region or a buffer to your printer. Entry points
27 ;; are `lpr-buffer', `print-buffer', `lpr-region', or `print-region'; option
28 ;; variables include `printer-name', `lpr-switches' and `lpr-command'.
32 (eval-when-compile (require 'cl
))
35 (defvar lpr-windows-system
36 (memq system-type
'(ms-dos windows-nt
))
37 "Non-nil if running on MS-DOS or MS Windows.")
41 (memq system-type
'(usg-unix-v hpux irix
))
42 "Non-nil if running on a system type that uses the \"lp\" command.")
46 "Print Emacs buffer on line printer."
51 (defcustom printer-name
52 (and (eq system-type
'ms-dos
) "PRN")
53 "The name of a local printer to which data is sent for printing.
54 \(Note that PostScript files are sent to `ps-printer-name', which see.\)
56 On Unix-like systems, a string value should be a name understood by
57 lpr's -P option; otherwise the value should be nil.
59 On MS-DOS and MS-Windows systems, a string value is taken as the name of
60 a printer device or port, provided `lpr-command' is set to \"\".
61 Typical non-default settings would be \"LPT1\" to \"LPT3\" for parallel
62 printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial printers, or
63 \"//hostname/printer\" for a shared network printer. You can also set
64 it to the name of a file, in which case the output gets appended to that
65 file. If you want to discard the printed output, set this to \"NUL\"."
66 :type
'(choice :menu-tag
"Printer Name"
68 (const :tag
"Default" nil
)
69 ;; could use string but then we lose completion for files.
74 (defcustom lpr-switches nil
75 "List of strings to pass as extra options for the printer program.
76 It is recommended to set `printer-name' instead of including an explicit
79 :type
'(repeat (string :tag
"Argument"))
82 (defcustom lpr-add-switches
(memq system-type
'(berkeley-unix gnu
/linux
))
83 "Non-nil means construct `-T' and `-J' options for the printer program.
84 These are made assuming that the program is `lpr';
85 if you are using some other incompatible printer program,
86 this variable should be nil."
90 (defcustom lpr-printer-switch
94 "Printer switch, that is, something like \"-P\", \"-d \", \"/D:\", etc.
95 This switch is used in conjunction with `printer-name'."
96 :type
'(choice :menu-tag
"Printer Name Switch"
97 :tag
"Printer Name Switch"
98 (const :tag
"None" nil
)
99 (string :tag
"Printer Switch"))
103 (defcustom lpr-command
112 "Name of program for printing a file.
114 On MS-DOS and MS-Windows systems, if the value is an empty string then
115 Emacs will write directly to the printer port named by `printer-name'.
116 The programs `print' and `nprint' (the standard print programs on
117 Windows NT and Novell Netware respectively) are handled specially, using
118 `printer-name' as the destination for output; any other program is
119 treated like `lpr' except that an explicit filename is given as the last
124 ;; Default is nil, because that enables us to use pr -f
125 ;; which is more reliable than pr with no args, which is what lpr -p does.
126 (defcustom lpr-headers-switches nil
127 "List of strings of options to request page headings in the printer program.
128 If nil, we run `lpr-page-header-program' to make page headings
129 and print the result."
130 :type
'(repeat (string :tag
"Argument"))
133 (defcustom print-region-function nil
134 "Function to call to print the region on a printer.
135 See definition of `print-region-1' for calling conventions."
136 :type
'(choice (const nil
) function
)
139 (defcustom lpr-page-header-program
"pr"
140 "Name of program for adding page headers to a file."
144 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
145 ;; So it looks like -F is a better default.
146 (defcustom lpr-page-header-switches
'("-h" "%s" "-F")
147 "List of strings to use as options for the page-header-generating program.
148 If `%s' appears in any of the strings, it is substituted by the page title.
149 Note that for correct quoting, `%s' should normally be a separate element.
150 The variable `lpr-page-header-program' specifies the program to use."
151 :type
'(repeat string
)
156 "Print buffer contents without pagination or page headers.
157 See the variables `lpr-switches' and `lpr-command'
158 for customization of the printer command."
160 (unless (y-or-n-p "Send current buffer to default printer? ")
161 (error "Cancelled")))
162 (print-region-1 (point-min) (point-max) lpr-switches nil
))
165 (defun print-buffer ()
166 "Paginate and print buffer contents.
168 The variable `lpr-headers-switches' controls how to paginate.
169 If it is nil (the default), we run the `pr' program (or whatever program
170 `lpr-page-header-program' specifies) to paginate.
171 `lpr-page-header-switches' specifies the switches for that program.
173 Otherwise, the switches in `lpr-headers-switches' are used
174 in the print command itself; we expect them to request pagination.
176 See the variables `lpr-switches' and `lpr-command'
177 for further customization of the printer command."
179 (unless (y-or-n-p "Send current buffer to default printer? ")
180 (error "Cancelled")))
181 (print-region-1 (point-min) (point-max) lpr-switches t
))
184 (defun lpr-region (start end
)
185 "Print region contents without pagination or page headers.
186 See the variables `lpr-switches' and `lpr-command'
187 for customization of the printer command."
189 (if (y-or-n-p "Send selected text to default printer? ")
190 (list (region-beginning) (region-end))
191 (error "Cancelled")))
192 (print-region-1 start end lpr-switches nil
))
195 (defun print-region (start end
)
196 "Paginate and print the region contents.
198 The variable `lpr-headers-switches' controls how to paginate.
199 If it is nil (the default), we run the `pr' program (or whatever program
200 `lpr-page-header-program' specifies) to paginate.
201 `lpr-page-header-switches' specifies the switches for that program.
203 Otherwise, the switches in `lpr-headers-switches' are used
204 in the print command itself; we expect them to request pagination.
206 See the variables `lpr-switches' and `lpr-command'
207 for further customization of the printer command."
209 (if (y-or-n-p "Send selected text to default printer? ")
210 (list (region-beginning) (region-end))
211 (error "Cancelled")))
212 (print-region-1 start end lpr-switches t
))
214 (defun print-region-1 (start end switches page-headers
)
215 ;; On some MIPS system, having a space in the job name
216 ;; crashes the printer demon. But using dashes looks ugly
217 ;; and it seems to annoying to do for that MIPS system.
218 (let ((name (concat (buffer-name) " Emacs buffer"))
219 (title (concat (buffer-name) " Emacs buffer"))
220 ;; Make pipes use the same coding system as
221 ;; writing the buffer to a file would.
222 (coding-system-for-write (or coding-system-for-write
223 buffer-file-coding-system
))
224 (coding-system-for-read (or coding-system-for-read
225 buffer-file-coding-system
))
230 (and page-headers lpr-headers-switches
231 ;; It's possible to use an lpr option to get page headers.
232 (setq switches
(append (if (stringp lpr-headers-switches
)
233 (list lpr-headers-switches
)
234 lpr-headers-switches
)
236 (setq nswitches
(lpr-flatten-list
237 (mapcar 'lpr-eval-switch
; Dynamic evaluation
239 switch-string
(if switches
240 (concat " with options "
241 (mapconcat 'identity switches
" "))
243 (message "Spooling%s..." switch-string
)
245 (let ((new-coords (print-region-new-buffer start end
)))
246 (setq start
(car new-coords
)
251 (setq end
(point-marker)))
252 (untabify (point-min) (point-max))))
254 (if lpr-headers-switches
255 ;; We handled this above by modifying SWITCHES.
257 ;; Run a separate program to get page headers.
258 (let ((new-coords (print-region-new-buffer start end
)))
259 (apply 'call-process-region
(car new-coords
) (cdr new-coords
)
260 lpr-page-header-program t t nil
261 (mapcar (lambda (e) (format e title
))
262 lpr-page-header-switches
)))
263 (setq start
(point-min)
265 (let ((buf (current-buffer)))
267 (let ((tempbuf (current-buffer)))
268 (with-current-buffer buf
269 (apply (or print-region-function
'call-process-region
)
270 (nconc (list start end lpr-command
272 (and lpr-add-switches
274 ;; These belong in pr if we are using that.
275 (and lpr-add-switches lpr-headers-switches
277 (and (stringp printer-name
)
278 (list (concat lpr-printer-switch
282 (set-marker end nil
))
283 (message "Spooling%s...done%s%s" switch-string
284 (case (count-lines (point-min) (point-max))
288 (buffer-string)))))))
290 ;; This function copies the text between start and end
291 ;; into a new buffer, makes that buffer current.
292 ;; It returns the new range to print from the new current buffer
295 (defun print-region-new-buffer (ostart oend
)
296 (if (string= (buffer-name) " *spool temp*")
298 (let ((oldbuf (current-buffer)))
299 (set-buffer (get-buffer-create " *spool temp*"))
302 (insert-buffer-substring oldbuf ostart oend
)
303 (cons (point-min) (point-max)))))
305 (defun printify-region (begin end
)
306 "Replace nonprinting characters in region with printable representations.
307 The printable representations use ^ (for ASCII control characters) or hex.
308 The characters tab, linefeed, space, return and formfeed are not affected."
312 (narrow-to-region begin end
)
313 (goto-char (point-min))
315 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t
)
316 (setq c
(preceding-char))
318 (insert (if (< c ?\s
)
319 (format "\\^%c" (+ c ?
@))
320 (format "\\%02x" c
))))))))
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;; Functions hacked from `ps-print' package.
325 ;; Dynamic evaluation
326 (defun lpr-eval-switch (arg)
327 (cond ((stringp arg
) arg
)
328 ((functionp arg
) (apply arg nil
))
329 ((symbolp arg
) (symbol-value arg
))
330 ((consp arg
) (apply (car arg
) (cdr arg
)))
333 ;; `lpr-flatten-list' is defined here (copied from "message.el" and
334 ;; enhanced to handle dotted pairs as well) until we can get some
335 ;; sensible autoloads, or `flatten-list' gets put somewhere decent.
337 ;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j))
338 ;; => (a b c d e f g h i j)
340 (defun lpr-flatten-list (&rest list
)
341 (lpr-flatten-list-1 list
))
343 (defun lpr-flatten-list-1 (list)
347 (append (lpr-flatten-list-1 (car list
))
348 (lpr-flatten-list-1 (cdr list
))))