* json.el: Bump version to 1.3; (json-alist-p, json-plist-p): Rewrite to avoid recursion.
[emacs.git] / lisp / lpr.el
blob76c69f3308c2796769ebe3bee971521c1d90e836
1 ;;; lpr.el --- print Emacs buffer on line printer
3 ;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2011
4 ;; 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 (eval-when-compile (require 'cl))
34 ;;;###autoload
35 (defvar lpr-windows-system
36 (memq system-type '(ms-dos windows-nt)))
38 ;;;###autoload
39 (defvar lpr-lp-system
40 (memq system-type '(usg-unix-v hpux irix)))
43 (defgroup lpr nil
44 "Print Emacs buffer on line printer."
45 :group 'wp)
48 ;;;###autoload
49 (defcustom printer-name
50 (and (eq system-type 'ms-dos) "PRN")
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.\)
54 On Unix-like systems, a string value should be a name understood by
55 lpr's -P option; otherwise the value should be nil.
57 On MS-DOS and MS-Windows systems, a string value is taken as the name of
58 a printer device or port, provided `lpr-command' is set to \"\".
59 Typical non-default settings would be \"LPT1\" to \"LPT3\" for parallel
60 printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial printers, or
61 \"//hostname/printer\" for a shared network printer. You can also set
62 it to the name of a file, in which case the output gets appended to that
63 file. If you want to discard the printed output, set this to \"NUL\"."
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"))
69 :group 'lpr)
71 ;;;###autoload
72 (defcustom lpr-switches nil
73 "List of strings to pass as extra options for the printer program.
74 It is recommended to set `printer-name' instead of including an explicit
75 switch on this list.
76 See `lpr-command'."
77 :type '(repeat (string :tag "Argument"))
78 :group 'lpr)
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.
82 These are made assuming that the program is `lpr';
83 if you are using some other incompatible printer program,
84 this variable should be nil."
85 :type 'boolean
86 :group 'lpr)
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.
93 This 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)
100 ;;;###autoload
101 (defcustom lpr-command
102 (purecopy
103 (cond
104 (lpr-windows-system
106 (lpr-lp-system
107 "lp")
109 "lpr")))
110 "Name of program for printing a file.
112 On MS-DOS and MS-Windows systems, if the value is an empty string then
113 Emacs will write directly to the printer port named by `printer-name'.
114 The programs `print' and `nprint' (the standard print programs on
115 Windows NT and Novell Netware respectively) are handled specially, using
116 `printer-name' as the destination for output; any other program is
117 treated like `lpr' except that an explicit filename is given as the last
118 argument."
119 :type 'string
120 :group 'lpr)
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.
124 (defcustom lpr-headers-switches nil
125 "List of strings of options to request page headings in the printer program.
126 If nil, we run `lpr-page-header-program' to make page headings
127 and print the result."
128 :type '(repeat (string :tag "Argument"))
129 :group 'lpr)
131 (defcustom print-region-function nil
132 "Function to call to print the region on a printer.
133 See definition of `print-region-1' for calling conventions."
134 :type '(choice (const nil) function)
135 :group 'lpr)
137 (defcustom lpr-page-header-program "pr"
138 "Name of program for adding page headers to a file."
139 :type 'string
140 :group 'lpr)
142 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
143 ;; So it looks like -F is a better default.
144 (defcustom lpr-page-header-switches '("-h" "%s" "-F")
145 "List of strings to use as options for the page-header-generating program.
146 If `%s' appears in any of the strings, it is substituted by the page title.
147 Note that for correct quoting, `%s' should normally be a separate element.
148 The variable `lpr-page-header-program' specifies the program to use."
149 :type '(repeat string)
150 :group 'lpr)
152 ;;;###autoload
153 (defun lpr-buffer ()
154 "Print buffer contents without pagination or page headers.
155 See the variables `lpr-switches' and `lpr-command'
156 for customization of the printer command."
157 (interactive
158 (unless (y-or-n-p "Send current buffer to default printer? ")
159 (error "Cancelled")))
160 (print-region-1 (point-min) (point-max) lpr-switches nil))
162 ;;;###autoload
163 (defun print-buffer ()
164 "Paginate and print buffer contents.
166 The variable `lpr-headers-switches' controls how to paginate.
167 If 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.
171 Otherwise, the switches in `lpr-headers-switches' are used
172 in the print command itself; we expect them to request pagination.
174 See the variables `lpr-switches' and `lpr-command'
175 for further customization of the printer command."
176 (interactive
177 (unless (y-or-n-p "Send current buffer to default printer? ")
178 (error "Cancelled")))
179 (print-region-1 (point-min) (point-max) lpr-switches t))
181 ;;;###autoload
182 (defun lpr-region (start end)
183 "Print region contents without pagination or page headers.
184 See the variables `lpr-switches' and `lpr-command'
185 for customization of the printer command."
186 (interactive
187 (if (y-or-n-p "Send selected text to default printer? ")
188 (list (region-beginning) (region-end))
189 (error "Cancelled")))
190 (print-region-1 start end lpr-switches nil))
192 ;;;###autoload
193 (defun print-region (start end)
194 "Paginate and print the region contents.
196 The variable `lpr-headers-switches' controls how to paginate.
197 If 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.
201 Otherwise, the switches in `lpr-headers-switches' are used
202 in the print command itself; we expect them to request pagination.
204 See the variables `lpr-switches' and `lpr-command'
205 for further customization of the printer command."
206 (interactive
207 (if (y-or-n-p "Send selected text to default printer? ")
208 (list (region-beginning) (region-end))
209 (error "Cancelled")))
210 (print-region-1 start end lpr-switches t))
212 (defun print-region-1 (start end switches page-headers)
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.
216 (let ((name (concat (buffer-name) " Emacs buffer"))
217 (title (concat (buffer-name) " Emacs buffer"))
218 ;; Make pipes use the same coding system as
219 ;; writing the buffer to a file would.
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))
224 (width tab-width)
225 nswitches
226 switch-string)
227 (save-excursion
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 ""))
241 (message "Spooling%s..." switch-string)
242 (if (/= tab-width 8)
243 (let ((new-coords (print-region-new-buffer start end)))
244 (setq start (car new-coords)
245 end (cdr new-coords)
246 tab-width width)
247 (save-excursion
248 (goto-char end)
249 (setq end (point-marker)))
250 (untabify (point-min) (point-max))))
251 (if page-headers
252 (if lpr-headers-switches
253 ;; We handled this above by modifying SWITCHES.
255 ;; Run a separate program to get page headers.
256 (let ((new-coords (print-region-new-buffer start end)))
257 (apply 'call-process-region (car new-coords) (cdr new-coords)
258 lpr-page-header-program t t nil
259 (mapcar (lambda (e) (format e title))
260 lpr-page-header-switches)))
261 (setq start (point-min)
262 end (point-max))))
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
282 (case (count-lines (point-min) (point-max))
283 (0 "")
284 (1 ": ")
285 (t ":\n"))
286 (buffer-string)))))))
288 ;; This function copies the text between start and end
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).
293 (defun print-region-new-buffer (ostart oend)
294 (if (string= (buffer-name) " *spool temp*")
295 (cons ostart oend)
296 (let ((oldbuf (current-buffer)))
297 (set-buffer (get-buffer-create " *spool temp*"))
298 (widen)
299 (erase-buffer)
300 (insert-buffer-substring oldbuf ostart oend)
301 (cons (point-min) (point-max)))))
303 (defun printify-region (begin end)
304 "Replace nonprinting characters in region with printable representations.
305 The printable representations use ^ (for ASCII control characters) or hex.
306 The characters tab, linefeed, space, return and formfeed are not affected."
307 (interactive "r")
308 (save-excursion
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))
315 (delete-char -1)
316 (insert (if (< c ?\s)
317 (format "\\^%c" (+ c ?@))
318 (format "\\%02x" c))))))))
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321 ;; Functions hacked from `ps-print' package.
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)))
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.
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)
338 (defun lpr-flatten-list (&rest list)
339 (lpr-flatten-list-1 list))
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))))
349 (provide 'lpr)
351 ;;; lpr.el ends here