XEmacs compatibility and doc fixes from Vinicius Jose Latorre
[bpt/emacs.git] / lisp / lpr.el
1 ;;; lpr.el --- print Emacs buffer on line printer.
2
3 ;; Copyright (C) 1985, 1988, 1992, 1994, 2001 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 2, or (at your option)
13 ;; 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; 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.
24
25 ;;; Commentary:
26
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'.
30
31 ;;; Code:
32
33 (defvar lpr-windows-system
34 (memq system-type '(emx win32 w32 mswindows ms-dos windows-nt)))
35
36 (defvar lpr-lp-system
37 (memq system-type '(usg-unix-v dgux 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 lpr-windows-system "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 (eq system-type 'berkeley-unix)
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 (cond
100 (lpr-windows-system
101 "")
102 (lpr-lp-system
103 "lp")
104 (t
105 "lpr"))
106 "*Name of program for printing a file.
107
108 On MS-DOS and MS-Windows systems, if the value is an empty string then
109 Emacs will write directly to the printer port named by `printer-name'.
110 The programs `print' and `nprint' (the standard print programs on
111 Windows NT and Novell Netware respectively) are handled specially, using
112 `printer-name' as the destination for output; any other program is
113 treated like `lpr' except that an explicit filename is given as the last
114 argument."
115 :type 'string
116 :group 'lpr)
117
118 ;; Default is nil, because that enables us to use pr -f
119 ;; which is more reliable than pr with no args, which is what lpr -p does.
120 (defcustom lpr-headers-switches nil
121 "*List of strings of options to request page headings in the printer program.
122 If nil, we run `lpr-page-header-program' to make page headings
123 and print the result."
124 :type '(repeat (string :tag "Argument"))
125 :group 'lpr)
126
127 (defcustom print-region-function nil
128 "Function to call to print the region on a printer.
129 See definition of `print-region-1' for calling conventions."
130 :type 'function
131 :group 'lpr)
132
133 (defcustom lpr-page-header-program "pr"
134 "*Name of program for adding page headers to a file."
135 :type 'string
136 :group 'lpr)
137
138 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
139 ;; So it looks like -F is a better default.
140 (defcustom lpr-page-header-switches '("-h" "-F")
141 "*List of strings to use as options for the page-header-generating program.
142 The variable `lpr-page-header-program' specifies the program to use."
143 :type '(repeat string)
144 :group 'lpr)
145
146 ;;;###autoload
147 (defun lpr-buffer ()
148 "Print buffer contents without pagination or page headers.
149 See the variables `lpr-switches' and `lpr-command'
150 for customization of the printer command."
151 (interactive)
152 (print-region-1 (point-min) (point-max) lpr-switches nil))
153
154 ;;;###autoload
155 (defun print-buffer ()
156 "Paginate and print buffer contents.
157
158 The variable `lpr-headers-switches' controls how to paginate.
159 If it is nil (the default), we run the `pr' program (or whatever program
160 `lpr-page-header-program' specifies) to paginate.
161 `lpr-page-header-switches' specifies the switches for that program.
162
163 Otherwise, the switches in `lpr-headers-switches' are used
164 in the print command itself; we expect them to request pagination.
165
166 See the variables `lpr-switches' and `lpr-command'
167 for further customization of the printer command."
168 (interactive)
169 (print-region-1 (point-min) (point-max) lpr-switches t))
170
171 ;;;###autoload
172 (defun lpr-region (start end)
173 "Print region contents without pagination or page headers.
174 See the variables `lpr-switches' and `lpr-command'
175 for customization of the printer command."
176 (interactive "r")
177 (print-region-1 start end lpr-switches nil))
178
179 ;;;###autoload
180 (defun print-region (start end)
181 "Paginate and print the region contents.
182
183 The variable `lpr-headers-switches' controls how to paginate.
184 If it is nil (the default), we run the `pr' program (or whatever program
185 `lpr-page-header-program' specifies) to paginate.
186 `lpr-page-header-switches' specifies the switches for that program.
187
188 Otherwise, the switches in `lpr-headers-switches' are used
189 in the print command itself; we expect them to request pagination.
190
191 See the variables `lpr-switches' and `lpr-command'
192 for further customization of the printer command."
193 (interactive "r")
194 (print-region-1 start end lpr-switches t))
195
196 (defun print-region-1 (start end switches page-headers)
197 ;; On some MIPS system, having a space in the job name
198 ;; crashes the printer demon. But using dashes looks ugly
199 ;; and it seems to annoying to do for that MIPS system.
200 (let ((name (concat (buffer-name) " Emacs buffer"))
201 (title (concat (buffer-name) " Emacs buffer"))
202 ;; Make pipes use the same coding system as
203 ;; writing the buffer to a file would.
204 (coding-system-for-write (or coding-system-for-write
205 buffer-file-coding-system))
206 (coding-system-for-read (or coding-system-for-read
207 buffer-file-coding-system))
208 (width tab-width)
209 nswitches
210 switch-string)
211 (save-excursion
212 (and page-headers lpr-headers-switches
213 ;; It's possible to use an lpr option to get page headers.
214 (setq switches (append (if (stringp lpr-headers-switches)
215 (list lpr-headers-switches)
216 lpr-headers-switches)
217 switches)))
218 (setq nswitches (lpr-flatten-list
219 (mapcar 'lpr-eval-switch ; Dynamic evaluation
220 switches))
221 switch-string (if switches
222 (concat " with options "
223 (mapconcat 'identity switches " "))
224 ""))
225 (message "Spooling%s..." switch-string)
226 (if (/= tab-width 8)
227 (let ((new-coords (print-region-new-buffer start end)))
228 (setq start (car new-coords)
229 end (cdr new-coords)
230 tab-width width)
231 (save-excursion
232 (goto-char end)
233 (setq end (point-marker)))
234 (untabify (point-min) (point-max))))
235 (if page-headers
236 (if lpr-headers-switches
237 ;; We handled this above by modifying SWITCHES.
238 nil
239 ;; Run a separate program to get page headers.
240 (let ((new-coords (print-region-new-buffer start end)))
241 (apply 'call-process-region (car new-coords) (cdr new-coords)
242 lpr-page-header-program t t nil
243 lpr-page-header-switches))
244 (setq start (point-min)
245 end (point-max))))
246 (apply (or print-region-function 'call-process-region)
247 (nconc (list start end lpr-command
248 nil nil nil)
249 (and lpr-add-switches
250 (list "-J" name))
251 ;; These belong in pr if we are using that.
252 (and lpr-add-switches lpr-headers-switches
253 (list "-T" title))
254 (and (stringp printer-name)
255 (list (concat lpr-printer-switch
256 printer-name)))
257 nswitches))
258 (if (markerp end)
259 (set-marker end nil))
260 (message "Spooling%s...done" switch-string))))
261
262 ;; This function copies the text between start and end
263 ;; into a new buffer, makes that buffer current.
264 ;; It returns the new range to print from the new current buffer
265 ;; as (START . END).
266
267 (defun print-region-new-buffer (ostart oend)
268 (if (string= (buffer-name) " *spool temp*")
269 (cons ostart oend)
270 (let ((oldbuf (current-buffer)))
271 (set-buffer (get-buffer-create " *spool temp*"))
272 (widen)
273 (erase-buffer)
274 (insert-buffer-substring oldbuf ostart oend)
275 (cons (point-min) (point-max)))))
276
277 (defun printify-region (begin end)
278 "Replace nonprinting characters in region with printable representations.
279 The printable representations use ^ (for ASCII control characters) or hex.
280 The characters tab, linefeed, space, return and formfeed are not affected."
281 (interactive "r")
282 (save-excursion
283 (goto-char begin)
284 (let (c)
285 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
286 (setq c (preceding-char))
287 (delete-backward-char 1)
288 (insert (if (< c ?\ )
289 (format "\\^%c" (+ c ?@))
290 (format "\\%02x" c)))))))
291
292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
293 ;; Functions hacked from `ps-print' package.
294
295 ;; Dynamic evaluation
296 (defun lpr-eval-switch (arg)
297 (cond ((stringp arg) arg)
298 ((functionp arg) (apply arg nil))
299 ((symbolp arg) (symbol-value arg))
300 ((consp arg) (apply (car arg) (cdr arg)))
301 (t nil)))
302
303 ;; `lpr-flatten-list' is defined here (copied from "message.el" and
304 ;; enhanced to handle dotted pairs as well) until we can get some
305 ;; sensible autoloads, or `flatten-list' gets put somewhere decent.
306
307 ;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j))
308 ;; => (a b c d e f g h i j)
309
310 (defun lpr-flatten-list (&rest list)
311 (lpr-flatten-list-1 list))
312
313 (defun lpr-flatten-list-1 (list)
314 (cond
315 ((null list) (list))
316 ((consp list)
317 (append (lpr-flatten-list-1 (car list))
318 (lpr-flatten-list-1 (cdr list))))
319 (t (list list))))
320
321 (provide 'lpr)
322
323 ;;; lpr.el ends here