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