Merge from trunk.
[bpt/emacs.git] / lisp / lpr.el
CommitLineData
55535639 1;;; lpr.el --- print Emacs buffer on line printer
6594deb0 2
acaf905b 3;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2012
95df8112 4;; 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
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
d6c7e99a 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
d6c7e99a
RS
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
d6c7e99a 23
e41b2db1
ER
24;;; Commentary:
25
bdc9238e 26;; Commands to send the region or a buffer to your printer. Entry points
75477452 27;; are `lpr-buffer', `print-buffer', `lpr-region', or `print-region'; option
8748e3eb 28;; variables include `printer-name', `lpr-switches' and `lpr-command'.
e41b2db1 29
e5167999 30;;; Code:
d6c7e99a 31
bdc9238e 32;;;###autoload
4ad25e43 33(defvar lpr-windows-system
3adbe224
GM
34 (memq system-type '(ms-dos windows-nt))
35 "Non-nil if running on MS-DOS or MS Windows.")
4ad25e43 36
bdc9238e 37;;;###autoload
4ad25e43 38(defvar lpr-lp-system
3adbe224
GM
39 (memq system-type '(usg-unix-v hpux irix))
40 "Non-nil if running on a system type that uses the \"lp\" command.")
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
a3374680 50 (and (eq system-type 'ms-dos) "PRN")
9201cc28 51 "The name of a local printer to which data is sent for printing.
8748e3eb
RS
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
9201cc28 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 80(defcustom lpr-add-switches (memq system-type '(berkeley-unix gnu/linux))
9201cc28 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")
9201cc28 92 "Printer switch, that is, something like \"-P\", \"-d \", \"/D:\", etc.
4ad25e43
EZ
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
6bdad9ae 102 (purecopy
8bc62130 103 (cond
4ad25e43 104 (lpr-windows-system
8bc62130 105 "")
4ad25e43 106 (lpr-lp-system
8bc62130
AI
107 "lp")
108 (t
6bdad9ae 109 "lpr")))
9201cc28 110 "Name of program for printing a file.
8bc62130
AI
111
112On MS-DOS and MS-Windows systems, if the value is an empty string then
113Emacs will write directly to the printer port named by `printer-name'.
114The programs `print' and `nprint' (the standard print programs on
115Windows NT and Novell Netware respectively) are handled specially, using
116`printer-name' as the destination for output; any other program is
117treated like `lpr' except that an explicit filename is given as the last
118argument."
c5292bc8
RS
119 :type 'string
120 :group 'lpr)
d6c7e99a 121
7173ec77
RS
122;; Default is nil, because that enables us to use pr -f
123;; which is more reliable than pr with no args, which is what lpr -p does.
c5292bc8 124(defcustom lpr-headers-switches nil
9201cc28 125 "List of strings of options to request page headings in the printer program.
7173ec77 126If nil, we run `lpr-page-header-program' to make page headings
c5292bc8
RS
127and print the result."
128 :type '(repeat (string :tag "Argument"))
129 :group 'lpr)
3a621cfe 130
c5292bc8 131(defcustom print-region-function nil
d6c7e99a 132 "Function to call to print the region on a printer.
c5292bc8 133See definition of `print-region-1' for calling conventions."
53c9f0fa 134 :type '(choice (const nil) function)
c5292bc8 135 :group 'lpr)
d6c7e99a 136
c5292bc8 137(defcustom lpr-page-header-program "pr"
9201cc28 138 "Name of program for adding page headers to a file."
c5292bc8
RS
139 :type 'string
140 :group 'lpr)
c820123b 141
bafa1a69
RS
142;; Berkeley systems support -F, and GNU pr supports both -f and -F,
143;; So it looks like -F is a better default.
be62e9d5 144(defcustom lpr-page-header-switches '("-h" "%s" "-F")
9201cc28 145 "List of strings to use as options for the page-header-generating program.
be62e9d5
GM
146If `%s' appears in any of the strings, it is substituted by the page title.
147Note that for correct quoting, `%s' should normally be a separate element.
c5292bc8
RS
148The variable `lpr-page-header-program' specifies the program to use."
149 :type '(repeat string)
150 :group 'lpr)
c820123b 151
7229064d 152;;;###autoload
d6c7e99a 153(defun lpr-buffer ()
d7e110e4
RS
154 "Print buffer contents without pagination or page headers.
155See the variables `lpr-switches' and `lpr-command'
156for customization of the printer command."
76dabd37
CY
157 (interactive
158 (unless (y-or-n-p "Send current buffer to default printer? ")
159 (error "Cancelled")))
d6c7e99a
RS
160 (print-region-1 (point-min) (point-max) lpr-switches nil))
161
7229064d 162;;;###autoload
d6c7e99a 163(defun print-buffer ()
e20192f4 164 "Paginate and print buffer contents.
e20192f4 165
d7e110e4
RS
166The variable `lpr-headers-switches' controls how to paginate.
167If it is nil (the default), we run the `pr' program (or whatever program
168`lpr-page-header-program' specifies) to paginate.
169`lpr-page-header-switches' specifies the switches for that program.
170
171Otherwise, the switches in `lpr-headers-switches' are used
172in the print command itself; we expect them to request pagination.
f1180544 173
d7e110e4
RS
174See the variables `lpr-switches' and `lpr-command'
175for further customization of the printer command."
76dabd37
CY
176 (interactive
177 (unless (y-or-n-p "Send current buffer to default printer? ")
178 (error "Cancelled")))
d6c7e99a
RS
179 (print-region-1 (point-min) (point-max) lpr-switches t))
180
7229064d 181;;;###autoload
d6c7e99a 182(defun lpr-region (start end)
d7e110e4
RS
183 "Print region contents without pagination or page headers.
184See the variables `lpr-switches' and `lpr-command'
185for customization of the printer command."
76dabd37
CY
186 (interactive
187 (if (y-or-n-p "Send selected text to default printer? ")
188 (list (region-beginning) (region-end))
189 (error "Cancelled")))
d6c7e99a
RS
190 (print-region-1 start end lpr-switches nil))
191
7229064d 192;;;###autoload
d6c7e99a 193(defun print-region (start end)
d7e110e4
RS
194 "Paginate and print the region contents.
195
196The variable `lpr-headers-switches' controls how to paginate.
197If it is nil (the default), we run the `pr' program (or whatever program
198`lpr-page-header-program' specifies) to paginate.
199`lpr-page-header-switches' specifies the switches for that program.
200
201Otherwise, the switches in `lpr-headers-switches' are used
202in the print command itself; we expect them to request pagination.
f1180544 203
d7e110e4
RS
204See the variables `lpr-switches' and `lpr-command'
205for further customization of the printer command."
76dabd37
CY
206 (interactive
207 (if (y-or-n-p "Send selected text to default printer? ")
208 (list (region-beginning) (region-end))
209 (error "Cancelled")))
d6c7e99a
RS
210 (print-region-1 start end lpr-switches t))
211
212(defun print-region-1 (start end switches page-headers)
904d7624
RS
213 ;; On some MIPS system, having a space in the job name
214 ;; crashes the printer demon. But using dashes looks ugly
215 ;; and it seems to annoying to do for that MIPS system.
4ad25e43 216 (let ((name (concat (buffer-name) " Emacs buffer"))
a8037455 217 (title (concat (buffer-name) " Emacs buffer"))
0b45d07b
EZ
218 ;; Make pipes use the same coding system as
219 ;; writing the buffer to a file would.
4ad25e43
EZ
220 (coding-system-for-write (or coding-system-for-write
221 buffer-file-coding-system))
222 (coding-system-for-read (or coding-system-for-read
223 buffer-file-coding-system))
5bbf28a8 224 (width tab-width)
4ad25e43 225 nswitches
5bbf28a8 226 switch-string)
d6c7e99a 227 (save-excursion
4ad25e43
EZ
228 (and page-headers lpr-headers-switches
229 ;; It's possible to use an lpr option to get page headers.
230 (setq switches (append (if (stringp lpr-headers-switches)
231 (list lpr-headers-switches)
232 lpr-headers-switches)
233 switches)))
234 (setq nswitches (lpr-flatten-list
235 (mapcar 'lpr-eval-switch ; Dynamic evaluation
236 switches))
237 switch-string (if switches
238 (concat " with options "
239 (mapconcat 'identity switches " "))
240 ""))
5bbf28a8 241 (message "Spooling%s..." switch-string)
d6c7e99a 242 (if (/= tab-width 8)
8fa64b34 243 (let ((new-coords (print-region-new-buffer start end)))
4ad25e43
EZ
244 (setq start (car new-coords)
245 end (cdr new-coords)
246 tab-width width)
2adf4f61
RS
247 (save-excursion
248 (goto-char end)
249 (setq end (point-marker)))
d6c7e99a
RS
250 (untabify (point-min) (point-max))))
251 (if page-headers
3a621cfe 252 (if lpr-headers-switches
5bbf28a8
RS
253 ;; We handled this above by modifying SWITCHES.
254 nil
7173ec77 255 ;; Run a separate program to get page headers.
8fa64b34 256 (let ((new-coords (print-region-new-buffer start end)))
4ad25e43
EZ
257 (apply 'call-process-region (car new-coords) (cdr new-coords)
258 lpr-page-header-program t t nil
c98557ae
EZ
259 (mapcar (lambda (e) (format e title))
260 lpr-page-header-switches)))
4ad25e43
EZ
261 (setq start (point-min)
262 end (point-max))))
6c19f744
SM
263 (let ((buf (current-buffer)))
264 (with-temp-buffer
265 (let ((tempbuf (current-buffer)))
266 (with-current-buffer buf
267 (apply (or print-region-function 'call-process-region)
268 (nconc (list start end lpr-command
269 nil tempbuf nil)
270 (and lpr-add-switches
271 (list "-J" name))
272 ;; These belong in pr if we are using that.
273 (and lpr-add-switches lpr-headers-switches
274 (list "-T" title))
275 (and (stringp printer-name)
276 (list (concat lpr-printer-switch
277 printer-name)))
278 nswitches))))
279 (if (markerp end)
280 (set-marker end nil))
281 (message "Spooling%s...done%s%s" switch-string
f58e0fd5 282 (pcase (count-lines (point-min) (point-max))
6c19f744
SM
283 (0 "")
284 (1 ": ")
f58e0fd5 285 (_ ":\n"))
6c19f744 286 (buffer-string)))))))
d6c7e99a
RS
287
288;; This function copies the text between start and end
8fa64b34
RS
289;; into a new buffer, makes that buffer current.
290;; It returns the new range to print from the new current buffer
291;; as (START . END).
292
3a621cfe 293(defun print-region-new-buffer (ostart oend)
8fa64b34
RS
294 (if (string= (buffer-name) " *spool temp*")
295 (cons ostart oend)
296 (let ((oldbuf (current-buffer)))
297 (set-buffer (get-buffer-create " *spool temp*"))
4ad25e43
EZ
298 (widen)
299 (erase-buffer)
8fa64b34
RS
300 (insert-buffer-substring oldbuf ostart oend)
301 (cons (point-min) (point-max)))))
6594deb0 302
1c2df063 303(defun printify-region (begin end)
8c44b2ad
RS
304 "Replace nonprinting characters in region with printable representations.
305The printable representations use ^ (for ASCII control characters) or hex.
306The characters tab, linefeed, space, return and formfeed are not affected."
1c2df063
ER
307 (interactive "r")
308 (save-excursion
5a1173cb
VJL
309 (save-restriction
310 (narrow-to-region begin end)
311 (goto-char (point-min))
312 (let (c)
313 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t)
314 (setq c (preceding-char))
d355a0b7 315 (delete-char -1)
f93e905f 316 (insert (if (< c ?\s)
5a1173cb
VJL
317 (format "\\^%c" (+ c ?@))
318 (format "\\%02x" c))))))))
4ad25e43
EZ
319
320;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321;; Functions hacked from `ps-print' package.
322
323;; Dynamic evaluation
324(defun lpr-eval-switch (arg)
325 (cond ((stringp arg) arg)
326 ((functionp arg) (apply arg nil))
327 ((symbolp arg) (symbol-value arg))
328 ((consp arg) (apply (car arg) (cdr arg)))
329 (t nil)))
330
331;; `lpr-flatten-list' is defined here (copied from "message.el" and
332;; enhanced to handle dotted pairs as well) until we can get some
333;; sensible autoloads, or `flatten-list' gets put somewhere decent.
334
335;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j))
336;; => (a b c d e f g h i j)
337
338(defun lpr-flatten-list (&rest list)
339 (lpr-flatten-list-1 list))
340
341(defun lpr-flatten-list-1 (list)
342 (cond
343 ((null list) (list))
344 ((consp list)
345 (append (lpr-flatten-list-1 (car list))
346 (lpr-flatten-list-1 (cdr list))))
347 (t (list list))))
1c2df063 348
977b1278
KH
349(provide 'lpr)
350
6594deb0 351;;; lpr.el ends here