(makefile-cleanup-continuations, makefile-warn-suspicious-lines):
[emacs.git] / lisp / lpr.el
blob04b77a30a0e05c5259edcbcb3f34c9976cf0bbf1
1 ;;; lpr.el --- print Emacs buffer on line printer
3 ;; Copyright (C) 1985, 1988, 1992, 1994, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: unix
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/>.
24 ;;; Commentary:
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'.
30 ;;; Code:
32 ;;;###autoload
33 (defvar lpr-windows-system
34 (memq system-type '(emx win32 w32 mswindows ms-dos windows-nt)))
36 ;;;###autoload
37 (defvar lpr-lp-system
38 (memq system-type '(usg-unix-v hpux irix)))
41 (defgroup lpr nil
42 "Print Emacs buffer on line printer."
43 :group 'wp)
46 ;;;###autoload
47 (defcustom printer-name
48 (and (memq system-type '(emx ms-dos)) "PRN")
49 "*The name of a local printer to which data is sent for printing.
50 \(Note that PostScript files are sent to `ps-printer-name', which see.\)
52 On Unix-like systems, a string value should be a name understood by
53 lpr's -P option; otherwise the value should be nil.
55 On MS-DOS and MS-Windows systems, a string value is taken as the name of
56 a printer device or port, provided `lpr-command' is set to \"\".
57 Typical non-default settings would be \"LPT1\" to \"LPT3\" for parallel
58 printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial printers, or
59 \"//hostname/printer\" for a shared network printer. You can also set
60 it to the name of a file, in which case the output gets appended to that
61 file. If you want to discard the printed output, set this to \"NUL\"."
62 :type '(choice :menu-tag "Printer Name"
63 :tag "Printer Name"
64 (const :tag "Default" nil)
65 ;; could use string but then we lose completion for files.
66 (file :tag "Name"))
67 :group 'lpr)
69 ;;;###autoload
70 (defcustom lpr-switches nil
71 "*List of strings to pass as extra options for the printer program.
72 It is recommended to set `printer-name' instead of including an explicit
73 switch on this list.
74 See `lpr-command'."
75 :type '(repeat (string :tag "Argument"))
76 :group 'lpr)
78 (defcustom lpr-add-switches (memq system-type '(berkeley-unix gnu/linux))
79 "*Non-nil means construct `-T' and `-J' options for the printer program.
80 These are made assuming that the program is `lpr';
81 if you are using some other incompatible printer program,
82 this variable should be nil."
83 :type 'boolean
84 :group 'lpr)
86 (defcustom lpr-printer-switch
87 (if lpr-lp-system
88 "-d "
89 "-P")
90 "*Printer switch, that is, something like \"-P\", \"-d \", \"/D:\", etc.
91 This switch is used in conjunction with `printer-name'."
92 :type '(choice :menu-tag "Printer Name Switch"
93 :tag "Printer Name Switch"
94 (const :tag "None" nil)
95 (string :tag "Printer Switch"))
96 :group 'lpr)
98 ;;;###autoload
99 (defcustom lpr-command
100 (cond
101 (lpr-windows-system
103 (lpr-lp-system
104 "lp")
106 "lpr"))
107 "*Name of program for printing a file.
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)
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)
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)
134 (defcustom lpr-page-header-program "pr"
135 "*Name of program for adding page headers to a file."
136 :type 'string
137 :group 'lpr)
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)
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 (print-region-1 (point-min) (point-max) lpr-switches nil))
157 ;;;###autoload
158 (defun print-buffer ()
159 "Paginate and print buffer contents.
161 The variable `lpr-headers-switches' controls how to paginate.
162 If it is nil (the default), we run the `pr' program (or whatever program
163 `lpr-page-header-program' specifies) to paginate.
164 `lpr-page-header-switches' specifies the switches for that program.
166 Otherwise, the switches in `lpr-headers-switches' are used
167 in the print command itself; we expect them to request pagination.
169 See the variables `lpr-switches' and `lpr-command'
170 for further customization of the printer command."
171 (interactive)
172 (print-region-1 (point-min) (point-max) lpr-switches t))
174 ;;;###autoload
175 (defun lpr-region (start end)
176 "Print region contents without pagination or page headers.
177 See the variables `lpr-switches' and `lpr-command'
178 for customization of the printer command."
179 (interactive "r")
180 (print-region-1 start end lpr-switches nil))
182 ;;;###autoload
183 (defun print-region (start end)
184 "Paginate and print the region contents.
186 The variable `lpr-headers-switches' controls how to paginate.
187 If it is nil (the default), we run the `pr' program (or whatever program
188 `lpr-page-header-program' specifies) to paginate.
189 `lpr-page-header-switches' specifies the switches for that program.
191 Otherwise, the switches in `lpr-headers-switches' are used
192 in the print command itself; we expect them to request pagination.
194 See the variables `lpr-switches' and `lpr-command'
195 for further customization of the printer command."
196 (interactive "r")
197 (print-region-1 start end lpr-switches t))
199 (defun print-region-1 (start end switches page-headers)
200 ;; On some MIPS system, having a space in the job name
201 ;; crashes the printer demon. But using dashes looks ugly
202 ;; and it seems to annoying to do for that MIPS system.
203 (let ((name (concat (buffer-name) " Emacs buffer"))
204 (title (concat (buffer-name) " Emacs buffer"))
205 ;; Make pipes use the same coding system as
206 ;; writing the buffer to a file would.
207 (coding-system-for-write (or coding-system-for-write
208 buffer-file-coding-system))
209 (coding-system-for-read (or coding-system-for-read
210 buffer-file-coding-system))
211 (width tab-width)
212 nswitches
213 switch-string)
214 (save-excursion
215 (and page-headers lpr-headers-switches
216 ;; It's possible to use an lpr option to get page headers.
217 (setq switches (append (if (stringp lpr-headers-switches)
218 (list lpr-headers-switches)
219 lpr-headers-switches)
220 switches)))
221 (setq nswitches (lpr-flatten-list
222 (mapcar 'lpr-eval-switch ; Dynamic evaluation
223 switches))
224 switch-string (if switches
225 (concat " with options "
226 (mapconcat 'identity switches " "))
227 ""))
228 (message "Spooling%s..." switch-string)
229 (if (/= tab-width 8)
230 (let ((new-coords (print-region-new-buffer start end)))
231 (setq start (car new-coords)
232 end (cdr new-coords)
233 tab-width width)
234 (save-excursion
235 (goto-char end)
236 (setq end (point-marker)))
237 (untabify (point-min) (point-max))))
238 (if page-headers
239 (if lpr-headers-switches
240 ;; We handled this above by modifying SWITCHES.
242 ;; Run a separate program to get page headers.
243 (let ((new-coords (print-region-new-buffer start end)))
244 (apply 'call-process-region (car new-coords) (cdr new-coords)
245 lpr-page-header-program t t nil
246 (mapcar (lambda (e) (format e title))
247 lpr-page-header-switches)))
248 (setq start (point-min)
249 end (point-max))))
250 (apply (or print-region-function 'call-process-region)
251 (nconc (list start end lpr-command
252 nil nil nil)
253 (and lpr-add-switches
254 (list "-J" name))
255 ;; These belong in pr if we are using that.
256 (and lpr-add-switches lpr-headers-switches
257 (list "-T" title))
258 (and (stringp printer-name)
259 (list (concat lpr-printer-switch
260 printer-name)))
261 nswitches))
262 (if (markerp end)
263 (set-marker end nil))
264 (message "Spooling%s...done" switch-string))))
266 ;; This function copies the text between start and end
267 ;; into a new buffer, makes that buffer current.
268 ;; It returns the new range to print from the new current buffer
269 ;; as (START . END).
271 (defun print-region-new-buffer (ostart oend)
272 (if (string= (buffer-name) " *spool temp*")
273 (cons ostart oend)
274 (let ((oldbuf (current-buffer)))
275 (set-buffer (get-buffer-create " *spool temp*"))
276 (widen)
277 (erase-buffer)
278 (insert-buffer-substring oldbuf ostart oend)
279 (cons (point-min) (point-max)))))
281 (defun printify-region (begin end)
282 "Replace nonprinting characters in region with printable representations.
283 The printable representations use ^ (for ASCII control characters) or hex.
284 The characters tab, linefeed, space, return and formfeed are not affected."
285 (interactive "r")
286 (save-excursion
287 (save-restriction
288 (narrow-to-region begin end)
289 (goto-char (point-min))
290 (let (c)
291 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t)
292 (setq c (preceding-char))
293 (delete-backward-char 1)
294 (insert (if (< c ?\s)
295 (format "\\^%c" (+ c ?@))
296 (format "\\%02x" c))))))))
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 ;; Functions hacked from `ps-print' package.
301 ;; Dynamic evaluation
302 (defun lpr-eval-switch (arg)
303 (cond ((stringp arg) arg)
304 ((functionp arg) (apply arg nil))
305 ((symbolp arg) (symbol-value arg))
306 ((consp arg) (apply (car arg) (cdr arg)))
307 (t nil)))
309 ;; `lpr-flatten-list' is defined here (copied from "message.el" and
310 ;; enhanced to handle dotted pairs as well) until we can get some
311 ;; sensible autoloads, or `flatten-list' gets put somewhere decent.
313 ;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j))
314 ;; => (a b c d e f g h i j)
316 (defun lpr-flatten-list (&rest list)
317 (lpr-flatten-list-1 list))
319 (defun lpr-flatten-list-1 (list)
320 (cond
321 ((null list) (list))
322 ((consp list)
323 (append (lpr-flatten-list-1 (car list))
324 (lpr-flatten-list-1 (cdr list))))
325 (t (list list))))
327 (provide 'lpr)
329 ;; arch-tag: 21c3f821-ebec-4ca9-ac67-a81e4b75c62a
330 ;;; lpr.el ends here