Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; lpr.el --- print Emacs buffer on line printer |
6594deb0 | 2 | |
ab422c4d PE |
3 | ;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2013 Free Software |
4 | ;; 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 | ||
54 | On Unix-like systems, a string value should be a name understood by | |
8bc62130 AI |
55 | lpr's -P option; otherwise the value should be nil. |
56 | ||
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\"." | |
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 |
74 | It is recommended to set `printer-name' instead of including an explicit |
75 | switch on this list. | |
c5292bc8 RS |
76 | See `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 |
82 | These are made assuming that the program is `lpr'; |
83 | if you are using some other incompatible printer program, | |
c5292bc8 RS |
84 | this 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 |
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) | |
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 | |
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." | |
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 | 126 | If nil, we run `lpr-page-header-program' to make page headings |
c5292bc8 | 127 | and print the result." |
99fb2756 GM |
128 | :type '(choice (const nil) |
129 | (string :tag "Single argument") | |
130 | (repeat :tag "Multiple arguments" (string :tag "Argument"))) | |
c5292bc8 | 131 | :group 'lpr) |
3a621cfe | 132 | |
2cdeb903 SM |
133 | (defcustom print-region-function |
134 | (if (memq system-type '(ms-dos windows-nt)) | |
135 | #'direct-print-region-function | |
136 | #'call-process-region) | |
d6c7e99a | 137 | "Function to call to print the region on a printer. |
c5292bc8 | 138 | See definition of `print-region-1' for calling conventions." |
2cdeb903 | 139 | :type 'function |
c5292bc8 | 140 | :group 'lpr) |
d6c7e99a | 141 | |
c5292bc8 | 142 | (defcustom lpr-page-header-program "pr" |
9201cc28 | 143 | "Name of program for adding page headers to a file." |
c5292bc8 RS |
144 | :type 'string |
145 | :group 'lpr) | |
c820123b | 146 | |
bafa1a69 RS |
147 | ;; Berkeley systems support -F, and GNU pr supports both -f and -F, |
148 | ;; So it looks like -F is a better default. | |
be62e9d5 | 149 | (defcustom lpr-page-header-switches '("-h" "%s" "-F") |
9201cc28 | 150 | "List of strings to use as options for the page-header-generating program. |
be62e9d5 GM |
151 | If `%s' appears in any of the strings, it is substituted by the page title. |
152 | Note that for correct quoting, `%s' should normally be a separate element. | |
c5292bc8 RS |
153 | The variable `lpr-page-header-program' specifies the program to use." |
154 | :type '(repeat string) | |
155 | :group 'lpr) | |
c820123b | 156 | |
7229064d | 157 | ;;;###autoload |
d6c7e99a | 158 | (defun lpr-buffer () |
d7e110e4 RS |
159 | "Print buffer contents without pagination or page headers. |
160 | See the variables `lpr-switches' and `lpr-command' | |
161 | for customization of the printer command." | |
76dabd37 CY |
162 | (interactive |
163 | (unless (y-or-n-p "Send current buffer to default printer? ") | |
164 | (error "Cancelled"))) | |
d6c7e99a RS |
165 | (print-region-1 (point-min) (point-max) lpr-switches nil)) |
166 | ||
7229064d | 167 | ;;;###autoload |
d6c7e99a | 168 | (defun print-buffer () |
e20192f4 | 169 | "Paginate and print buffer contents. |
e20192f4 | 170 | |
d7e110e4 RS |
171 | The variable `lpr-headers-switches' controls how to paginate. |
172 | If it is nil (the default), we run the `pr' program (or whatever program | |
173 | `lpr-page-header-program' specifies) to paginate. | |
174 | `lpr-page-header-switches' specifies the switches for that program. | |
175 | ||
176 | Otherwise, the switches in `lpr-headers-switches' are used | |
177 | in the print command itself; we expect them to request pagination. | |
f1180544 | 178 | |
d7e110e4 RS |
179 | See the variables `lpr-switches' and `lpr-command' |
180 | for further customization of the printer command." | |
76dabd37 CY |
181 | (interactive |
182 | (unless (y-or-n-p "Send current buffer to default printer? ") | |
183 | (error "Cancelled"))) | |
d6c7e99a RS |
184 | (print-region-1 (point-min) (point-max) lpr-switches t)) |
185 | ||
7229064d | 186 | ;;;###autoload |
d6c7e99a | 187 | (defun lpr-region (start end) |
d7e110e4 RS |
188 | "Print region contents without pagination or page headers. |
189 | See the variables `lpr-switches' and `lpr-command' | |
190 | for customization of the printer command." | |
76dabd37 CY |
191 | (interactive |
192 | (if (y-or-n-p "Send selected text to default printer? ") | |
193 | (list (region-beginning) (region-end)) | |
194 | (error "Cancelled"))) | |
d6c7e99a RS |
195 | (print-region-1 start end lpr-switches nil)) |
196 | ||
7229064d | 197 | ;;;###autoload |
d6c7e99a | 198 | (defun print-region (start end) |
d7e110e4 RS |
199 | "Paginate and print the region contents. |
200 | ||
201 | The variable `lpr-headers-switches' controls how to paginate. | |
202 | If it is nil (the default), we run the `pr' program (or whatever program | |
203 | `lpr-page-header-program' specifies) to paginate. | |
204 | `lpr-page-header-switches' specifies the switches for that program. | |
205 | ||
206 | Otherwise, the switches in `lpr-headers-switches' are used | |
207 | in the print command itself; we expect them to request pagination. | |
f1180544 | 208 | |
d7e110e4 RS |
209 | See the variables `lpr-switches' and `lpr-command' |
210 | for further customization of the printer command." | |
76dabd37 CY |
211 | (interactive |
212 | (if (y-or-n-p "Send selected text to default printer? ") | |
213 | (list (region-beginning) (region-end)) | |
214 | (error "Cancelled"))) | |
d6c7e99a RS |
215 | (print-region-1 start end lpr-switches t)) |
216 | ||
217 | (defun print-region-1 (start end switches page-headers) | |
2cdeb903 SM |
218 | (and page-headers lpr-headers-switches |
219 | ;; It's possible to use an lpr option to get page headers. | |
220 | (setq switches (append (if (stringp lpr-headers-switches) | |
221 | (list lpr-headers-switches) | |
222 | lpr-headers-switches) | |
223 | switches))) | |
904d7624 RS |
224 | ;; On some MIPS system, having a space in the job name |
225 | ;; crashes the printer demon. But using dashes looks ugly | |
226 | ;; and it seems to annoying to do for that MIPS system. | |
2cdeb903 SM |
227 | (save-excursion |
228 | (let ((name (concat (buffer-name) " Emacs buffer")) | |
229 | ;; Make pipes use the same coding system as | |
230 | ;; writing the buffer to a file would. | |
231 | (coding-system-for-write (or coding-system-for-write | |
232 | buffer-file-coding-system)) | |
233 | (coding-system-for-read (or coding-system-for-read | |
234 | buffer-file-coding-system)) | |
235 | (width tab-width)) | |
d6c7e99a | 236 | (if (/= tab-width 8) |
8fa64b34 | 237 | (let ((new-coords (print-region-new-buffer start end))) |
4ad25e43 EZ |
238 | (setq start (car new-coords) |
239 | end (cdr new-coords) | |
240 | tab-width width) | |
2adf4f61 RS |
241 | (save-excursion |
242 | (goto-char end) | |
243 | (setq end (point-marker))) | |
d6c7e99a RS |
244 | (untabify (point-min) (point-max)))) |
245 | (if page-headers | |
3a621cfe | 246 | (if lpr-headers-switches |
5bbf28a8 RS |
247 | ;; We handled this above by modifying SWITCHES. |
248 | nil | |
7173ec77 | 249 | ;; Run a separate program to get page headers. |
8fa64b34 | 250 | (let ((new-coords (print-region-new-buffer start end))) |
4ad25e43 EZ |
251 | (apply 'call-process-region (car new-coords) (cdr new-coords) |
252 | lpr-page-header-program t t nil | |
2cdeb903 | 253 | (mapcar (lambda (e) (format e name)) |
c98557ae | 254 | lpr-page-header-switches))) |
4ad25e43 EZ |
255 | (setq start (point-min) |
256 | end (point-max)))) | |
2cdeb903 SM |
257 | (lpr-print-region start end switches name)))) |
258 | ||
259 | (defun lpr-print-region (start end switches name) | |
260 | (let ((buf (current-buffer)) | |
261 | (nswitches (lpr-flatten-list | |
262 | (mapcar #'lpr-eval-switch ; Dynamic evaluation | |
263 | switches))) | |
264 | (switch-string (if switches | |
265 | (concat " with options " | |
266 | (mapconcat #'identity switches " ")) | |
267 | ""))) | |
268 | (message "Spooling%s..." switch-string) | |
269 | (with-temp-buffer | |
270 | (let ((retval | |
271 | (let ((tempbuf (current-buffer))) | |
272 | (with-current-buffer buf | |
273 | (apply (or print-region-function 'call-process-region) | |
274 | start end lpr-command | |
275 | nil tempbuf nil | |
276 | (nconc (and name lpr-add-switches | |
277 | (list "-J" name)) | |
278 | ;; These belong in pr if we are using that. | |
279 | (and name lpr-add-switches lpr-headers-switches | |
280 | (list "-T" name)) | |
281 | (and (stringp printer-name) | |
282 | (string< "" printer-name) | |
283 | (list (concat lpr-printer-switch | |
284 | printer-name))) | |
285 | nswitches)))))) | |
286 | (if (markerp end) | |
287 | (set-marker end nil)) | |
288 | (funcall (if (memq retval '(nil 0)) #'message #'user-error) | |
289 | "Spooling%s...done%s%s" switch-string | |
290 | (pcase (count-lines (point-min) (point-max)) | |
291 | (0 "") | |
292 | (1 ": ") | |
293 | (_ ":\n")) | |
294 | (buffer-string)))))) | |
d6c7e99a RS |
295 | |
296 | ;; This function copies the text between start and end | |
8fa64b34 RS |
297 | ;; into a new buffer, makes that buffer current. |
298 | ;; It returns the new range to print from the new current buffer | |
299 | ;; as (START . END). | |
300 | ||
3a621cfe | 301 | (defun print-region-new-buffer (ostart oend) |
8fa64b34 RS |
302 | (if (string= (buffer-name) " *spool temp*") |
303 | (cons ostart oend) | |
304 | (let ((oldbuf (current-buffer))) | |
305 | (set-buffer (get-buffer-create " *spool temp*")) | |
4ad25e43 EZ |
306 | (widen) |
307 | (erase-buffer) | |
8fa64b34 RS |
308 | (insert-buffer-substring oldbuf ostart oend) |
309 | (cons (point-min) (point-max))))) | |
6594deb0 | 310 | |
1c2df063 | 311 | (defun printify-region (begin end) |
8c44b2ad RS |
312 | "Replace nonprinting characters in region with printable representations. |
313 | The printable representations use ^ (for ASCII control characters) or hex. | |
314 | The characters tab, linefeed, space, return and formfeed are not affected." | |
1c2df063 ER |
315 | (interactive "r") |
316 | (save-excursion | |
5a1173cb VJL |
317 | (save-restriction |
318 | (narrow-to-region begin end) | |
319 | (goto-char (point-min)) | |
320 | (let (c) | |
321 | (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t) | |
322 | (setq c (preceding-char)) | |
d355a0b7 | 323 | (delete-char -1) |
f93e905f | 324 | (insert (if (< c ?\s) |
5a1173cb VJL |
325 | (format "\\^%c" (+ c ?@)) |
326 | (format "\\%02x" c)))))))) | |
4ad25e43 EZ |
327 | |
328 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
329 | ;; Functions hacked from `ps-print' package. | |
330 | ||
331 | ;; Dynamic evaluation | |
332 | (defun lpr-eval-switch (arg) | |
333 | (cond ((stringp arg) arg) | |
2cdeb903 | 334 | ((functionp arg) (funcall arg)) |
4ad25e43 EZ |
335 | ((symbolp arg) (symbol-value arg)) |
336 | ((consp arg) (apply (car arg) (cdr arg))) | |
337 | (t nil))) | |
338 | ||
339 | ;; `lpr-flatten-list' is defined here (copied from "message.el" and | |
340 | ;; enhanced to handle dotted pairs as well) until we can get some | |
341 | ;; sensible autoloads, or `flatten-list' gets put somewhere decent. | |
342 | ||
343 | ;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j)) | |
344 | ;; => (a b c d e f g h i j) | |
345 | ||
346 | (defun lpr-flatten-list (&rest list) | |
347 | (lpr-flatten-list-1 list)) | |
348 | ||
349 | (defun lpr-flatten-list-1 (list) | |
350 | (cond | |
2cdeb903 | 351 | ((null list) nil) |
4ad25e43 EZ |
352 | ((consp list) |
353 | (append (lpr-flatten-list-1 (car list)) | |
354 | (lpr-flatten-list-1 (cdr list)))) | |
355 | (t (list list)))) | |
1c2df063 | 356 | |
977b1278 KH |
357 | (provide 'lpr) |
358 | ||
6594deb0 | 359 | ;;; lpr.el ends here |