Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / lpr.el
CommitLineData
55535639 1;;; lpr.el --- print Emacs buffer on line printer
6594deb0 2
0d30b337 3;; Copyright (C) 1985, 1988, 1992, 1994, 2001, 2002, 2003,
409cc4a3 4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3a801d0c 5
55535639
PJ
6;; Maintainer: FSF
7;; Keywords: unix
e5167999 8
d6c7e99a
RS
9;; This file is part of GNU Emacs.
10
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
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
d6c7e99a
RS
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
d6c7e99a 25
e41b2db1
ER
26;;; Commentary:
27
bdc9238e 28;; Commands to send the region or a buffer to your printer. Entry points
75477452 29;; are `lpr-buffer', `print-buffer', `lpr-region', or `print-region'; option
8748e3eb 30;; variables include `printer-name', `lpr-switches' and `lpr-command'.
e41b2db1 31
e5167999 32;;; Code:
d6c7e99a 33
bdc9238e 34;;;###autoload
4ad25e43
EZ
35(defvar lpr-windows-system
36 (memq system-type '(emx win32 w32 mswindows ms-dos windows-nt)))
37
bdc9238e 38;;;###autoload
4ad25e43 39(defvar lpr-lp-system
f3041af1 40 (memq system-type '(usg-unix-v hpux irix)))
4ad25e43
EZ
41
42
c5292bc8 43(defgroup lpr nil
f93e905f 44 "Print Emacs buffer on line printer."
c5292bc8
RS
45 :group 'wp)
46
4ad25e43 47
8748e3eb
RS
48;;;###autoload
49(defcustom printer-name
93e45e63 50 (and (memq system-type '(emx ms-dos)) "PRN")
8748e3eb
RS
51 "*The name of a local printer to which data is sent for printing.
52\(Note that PostScript files are sent to `ps-printer-name', which see.\)
53
54On Unix-like systems, a string value should be a name understood by
8bc62130
AI
55lpr's -P option; otherwise the value should be nil.
56
57On MS-DOS and MS-Windows systems, a string value is taken as the name of
58a printer device or port, provided `lpr-command' is set to \"\".
59Typical non-default settings would be \"LPT1\" to \"LPT3\" for parallel
60printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial printers, or
61\"//hostname/printer\" for a shared network printer. You can also set
62it to the name of a file, in which case the output gets appended to that
63file. If you want to discard the printed output, set this to \"NUL\"."
4ad25e43
EZ
64 :type '(choice :menu-tag "Printer Name"
65 :tag "Printer Name"
66 (const :tag "Default" nil)
67 ;; could use string but then we lose completion for files.
68 (file :tag "Name"))
8748e3eb 69 :group 'lpr)
c5292bc8 70
6503cec3 71;;;###autoload
4ad25e43 72(defcustom lpr-switches nil
22466c09 73 "*List of strings to pass as extra options for the printer program.
8748e3eb
RS
74It is recommended to set `printer-name' instead of including an explicit
75switch on this list.
c5292bc8
RS
76See `lpr-command'."
77 :type '(repeat (string :tag "Argument"))
78 :group 'lpr)
3a621cfe 79
7298f3f7
RS
80(defcustom lpr-add-switches (memq system-type '(berkeley-unix gnu/linux))
81 "*Non-nil means construct `-T' and `-J' options for the printer program.
22466c09
KH
82These are made assuming that the program is `lpr';
83if you are using some other incompatible printer program,
c5292bc8
RS
84this variable should be nil."
85 :type 'boolean
86 :group 'lpr)
d6c7e99a 87
4ad25e43
EZ
88(defcustom lpr-printer-switch
89 (if lpr-lp-system
90 "-d "
91 "-P")
92 "*Printer switch, that is, something like \"-P\", \"-d \", \"/D:\", etc.
93This switch is used in conjunction with `printer-name'."
94 :type '(choice :menu-tag "Printer Name Switch"
95 :tag "Printer Name Switch"
96 (const :tag "None" nil)
97 (string :tag "Printer Switch"))
98 :group 'lpr)
99
910476ef 100;;;###autoload
c5292bc8 101(defcustom lpr-command
8bc62130 102 (cond
4ad25e43 103 (lpr-windows-system
8bc62130 104 "")
4ad25e43 105 (lpr-lp-system
8bc62130
AI
106 "lp")
107 (t
108 "lpr"))
109 "*Name of program for printing a file.
110
111On MS-DOS and MS-Windows systems, if the value is an empty string then
112Emacs will write directly to the printer port named by `printer-name'.
113The programs `print' and `nprint' (the standard print programs on
114Windows NT and Novell Netware respectively) are handled specially, using
115`printer-name' as the destination for output; any other program is
116treated like `lpr' except that an explicit filename is given as the last
117argument."
c5292bc8
RS
118 :type 'string
119 :group 'lpr)
d6c7e99a 120
7173ec77
RS
121;; Default is nil, because that enables us to use pr -f
122;; which is more reliable than pr with no args, which is what lpr -p does.
c5292bc8 123(defcustom lpr-headers-switches nil
5526cfde 124 "*List of strings of options to request page headings in the printer program.
7173ec77 125If nil, we run `lpr-page-header-program' to make page headings
c5292bc8
RS
126and print the result."
127 :type '(repeat (string :tag "Argument"))
128 :group 'lpr)
3a621cfe 129
c5292bc8 130(defcustom print-region-function nil
d6c7e99a 131 "Function to call to print the region on a printer.
c5292bc8 132See definition of `print-region-1' for calling conventions."
53c9f0fa 133 :type '(choice (const nil) function)
c5292bc8 134 :group 'lpr)
d6c7e99a 135
c5292bc8
RS
136(defcustom lpr-page-header-program "pr"
137 "*Name of program for adding page headers to a file."
138 :type 'string
139 :group 'lpr)
c820123b 140
bafa1a69
RS
141;; Berkeley systems support -F, and GNU pr supports both -f and -F,
142;; So it looks like -F is a better default.
be62e9d5 143(defcustom lpr-page-header-switches '("-h" "%s" "-F")
502ca00a 144 "*List of strings to use as options for the page-header-generating program.
be62e9d5
GM
145If `%s' appears in any of the strings, it is substituted by the page title.
146Note that for correct quoting, `%s' should normally be a separate element.
c5292bc8
RS
147The variable `lpr-page-header-program' specifies the program to use."
148 :type '(repeat string)
149 :group 'lpr)
c820123b 150
7229064d 151;;;###autoload
d6c7e99a 152(defun lpr-buffer ()
d7e110e4
RS
153 "Print buffer contents without pagination or page headers.
154See the variables `lpr-switches' and `lpr-command'
155for customization of the printer command."
d6c7e99a
RS
156 (interactive)
157 (print-region-1 (point-min) (point-max) lpr-switches nil))
158
7229064d 159;;;###autoload
d6c7e99a 160(defun print-buffer ()
e20192f4 161 "Paginate and print buffer contents.
e20192f4 162
d7e110e4
RS
163The variable `lpr-headers-switches' controls how to paginate.
164If it is nil (the default), we run the `pr' program (or whatever program
165`lpr-page-header-program' specifies) to paginate.
166`lpr-page-header-switches' specifies the switches for that program.
167
168Otherwise, the switches in `lpr-headers-switches' are used
169in the print command itself; we expect them to request pagination.
f1180544 170
d7e110e4
RS
171See the variables `lpr-switches' and `lpr-command'
172for further customization of the printer command."
d6c7e99a
RS
173 (interactive)
174 (print-region-1 (point-min) (point-max) lpr-switches t))
175
7229064d 176;;;###autoload
d6c7e99a 177(defun lpr-region (start end)
d7e110e4
RS
178 "Print region contents without pagination or page headers.
179See the variables `lpr-switches' and `lpr-command'
180for customization of the printer command."
d6c7e99a
RS
181 (interactive "r")
182 (print-region-1 start end lpr-switches nil))
183
7229064d 184;;;###autoload
d6c7e99a 185(defun print-region (start end)
d7e110e4
RS
186 "Paginate and print the region contents.
187
188The variable `lpr-headers-switches' controls how to paginate.
189If it is nil (the default), we run the `pr' program (or whatever program
190`lpr-page-header-program' specifies) to paginate.
191`lpr-page-header-switches' specifies the switches for that program.
192
193Otherwise, the switches in `lpr-headers-switches' are used
194in the print command itself; we expect them to request pagination.
f1180544 195
d7e110e4
RS
196See the variables `lpr-switches' and `lpr-command'
197for further customization of the printer command."
d6c7e99a
RS
198 (interactive "r")
199 (print-region-1 start end lpr-switches t))
200
201(defun print-region-1 (start end switches page-headers)
904d7624
RS
202 ;; On some MIPS system, having a space in the job name
203 ;; crashes the printer demon. But using dashes looks ugly
204 ;; and it seems to annoying to do for that MIPS system.
4ad25e43 205 (let ((name (concat (buffer-name) " Emacs buffer"))
a8037455 206 (title (concat (buffer-name) " Emacs buffer"))
0b45d07b
EZ
207 ;; Make pipes use the same coding system as
208 ;; writing the buffer to a file would.
4ad25e43
EZ
209 (coding-system-for-write (or coding-system-for-write
210 buffer-file-coding-system))
211 (coding-system-for-read (or coding-system-for-read
212 buffer-file-coding-system))
5bbf28a8 213 (width tab-width)
4ad25e43 214 nswitches
5bbf28a8 215 switch-string)
d6c7e99a 216 (save-excursion
4ad25e43
EZ
217 (and page-headers lpr-headers-switches
218 ;; It's possible to use an lpr option to get page headers.
219 (setq switches (append (if (stringp lpr-headers-switches)
220 (list lpr-headers-switches)
221 lpr-headers-switches)
222 switches)))
223 (setq nswitches (lpr-flatten-list
224 (mapcar 'lpr-eval-switch ; Dynamic evaluation
225 switches))
226 switch-string (if switches
227 (concat " with options "
228 (mapconcat 'identity switches " "))
229 ""))
5bbf28a8 230 (message "Spooling%s..." switch-string)
d6c7e99a 231 (if (/= tab-width 8)
8fa64b34 232 (let ((new-coords (print-region-new-buffer start end)))
4ad25e43
EZ
233 (setq start (car new-coords)
234 end (cdr new-coords)
235 tab-width width)
2adf4f61
RS
236 (save-excursion
237 (goto-char end)
238 (setq end (point-marker)))
d6c7e99a
RS
239 (untabify (point-min) (point-max))))
240 (if page-headers
3a621cfe 241 (if lpr-headers-switches
5bbf28a8
RS
242 ;; We handled this above by modifying SWITCHES.
243 nil
7173ec77 244 ;; Run a separate program to get page headers.
8fa64b34 245 (let ((new-coords (print-region-new-buffer start end)))
4ad25e43
EZ
246 (apply 'call-process-region (car new-coords) (cdr new-coords)
247 lpr-page-header-program t t nil
c98557ae
EZ
248 (mapcar (lambda (e) (format e title))
249 lpr-page-header-switches)))
4ad25e43
EZ
250 (setq start (point-min)
251 end (point-max))))
252 (apply (or print-region-function 'call-process-region)
253 (nconc (list start end lpr-command
254 nil nil nil)
255 (and lpr-add-switches
256 (list "-J" name))
257 ;; These belong in pr if we are using that.
258 (and lpr-add-switches lpr-headers-switches
259 (list "-T" title))
260 (and (stringp printer-name)
261 (list (concat lpr-printer-switch
262 printer-name)))
263 nswitches))
2adf4f61
RS
264 (if (markerp end)
265 (set-marker end nil))
5bbf28a8 266 (message "Spooling%s...done" switch-string))))
d6c7e99a
RS
267
268;; This function copies the text between start and end
8fa64b34
RS
269;; into a new buffer, makes that buffer current.
270;; It returns the new range to print from the new current buffer
271;; as (START . END).
272
3a621cfe 273(defun print-region-new-buffer (ostart oend)
8fa64b34
RS
274 (if (string= (buffer-name) " *spool temp*")
275 (cons ostart oend)
276 (let ((oldbuf (current-buffer)))
277 (set-buffer (get-buffer-create " *spool temp*"))
4ad25e43
EZ
278 (widen)
279 (erase-buffer)
8fa64b34
RS
280 (insert-buffer-substring oldbuf ostart oend)
281 (cons (point-min) (point-max)))))
6594deb0 282
1c2df063 283(defun printify-region (begin end)
8c44b2ad
RS
284 "Replace nonprinting characters in region with printable representations.
285The printable representations use ^ (for ASCII control characters) or hex.
286The characters tab, linefeed, space, return and formfeed are not affected."
1c2df063
ER
287 (interactive "r")
288 (save-excursion
5a1173cb
VJL
289 (save-restriction
290 (narrow-to-region begin end)
291 (goto-char (point-min))
292 (let (c)
293 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t)
294 (setq c (preceding-char))
295 (delete-backward-char 1)
f93e905f 296 (insert (if (< c ?\s)
5a1173cb
VJL
297 (format "\\^%c" (+ c ?@))
298 (format "\\%02x" c))))))))
4ad25e43
EZ
299
300;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301;; Functions hacked from `ps-print' package.
302
303;; Dynamic evaluation
304(defun lpr-eval-switch (arg)
305 (cond ((stringp arg) arg)
306 ((functionp arg) (apply arg nil))
307 ((symbolp arg) (symbol-value arg))
308 ((consp arg) (apply (car arg) (cdr arg)))
309 (t nil)))
310
311;; `lpr-flatten-list' is defined here (copied from "message.el" and
312;; enhanced to handle dotted pairs as well) until we can get some
313;; sensible autoloads, or `flatten-list' gets put somewhere decent.
314
315;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j))
316;; => (a b c d e f g h i j)
317
318(defun lpr-flatten-list (&rest list)
319 (lpr-flatten-list-1 list))
320
321(defun lpr-flatten-list-1 (list)
322 (cond
323 ((null list) (list))
324 ((consp list)
325 (append (lpr-flatten-list-1 (car list))
326 (lpr-flatten-list-1 (cdr list))))
327 (t (list list))))
1c2df063 328
977b1278
KH
329(provide 'lpr)
330
cbee283d 331;; arch-tag: 21c3f821-ebec-4ca9-ac67-a81e4b75c62a
6594deb0 332;;; lpr.el ends here