Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; lpr.el --- print Emacs buffer on line printer |
6594deb0 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1985, 1988, 1992, 1994, 2001, 2002, 2003, |
5df4f04c | 4 | ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software 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 |
a3374680 | 34 | (memq system-type '(ms-dos windows-nt))) |
4ad25e43 | 35 | |
bdc9238e | 36 | ;;;###autoload |
4ad25e43 | 37 | (defvar lpr-lp-system |
f3041af1 | 38 | (memq system-type '(usg-unix-v hpux irix))) |
4ad25e43 EZ |
39 | |
40 | ||
c5292bc8 | 41 | (defgroup lpr nil |
f93e905f | 42 | "Print Emacs buffer on line printer." |
c5292bc8 RS |
43 | :group 'wp) |
44 | ||
4ad25e43 | 45 | |
8748e3eb RS |
46 | ;;;###autoload |
47 | (defcustom printer-name | |
a3374680 | 48 | (and (eq system-type 'ms-dos) "PRN") |
9201cc28 | 49 | "The name of a local printer to which data is sent for printing. |
8748e3eb RS |
50 | \(Note that PostScript files are sent to `ps-printer-name', which see.\) |
51 | ||
52 | On Unix-like systems, a string value should be a name understood by | |
8bc62130 AI |
53 | lpr's -P option; otherwise the value should be nil. |
54 | ||
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\"." | |
4ad25e43 EZ |
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")) | |
8748e3eb | 67 | :group 'lpr) |
c5292bc8 | 68 | |
6503cec3 | 69 | ;;;###autoload |
4ad25e43 | 70 | (defcustom lpr-switches nil |
9201cc28 | 71 | "List of strings to pass as extra options for the printer program. |
8748e3eb RS |
72 | It is recommended to set `printer-name' instead of including an explicit |
73 | switch on this list. | |
c5292bc8 RS |
74 | See `lpr-command'." |
75 | :type '(repeat (string :tag "Argument")) | |
76 | :group 'lpr) | |
3a621cfe | 77 | |
7298f3f7 | 78 | (defcustom lpr-add-switches (memq system-type '(berkeley-unix gnu/linux)) |
9201cc28 | 79 | "Non-nil means construct `-T' and `-J' options for the printer program. |
22466c09 KH |
80 | These are made assuming that the program is `lpr'; |
81 | if you are using some other incompatible printer program, | |
c5292bc8 RS |
82 | this variable should be nil." |
83 | :type 'boolean | |
84 | :group 'lpr) | |
d6c7e99a | 85 | |
4ad25e43 EZ |
86 | (defcustom lpr-printer-switch |
87 | (if lpr-lp-system | |
88 | "-d " | |
89 | "-P") | |
9201cc28 | 90 | "Printer switch, that is, something like \"-P\", \"-d \", \"/D:\", etc. |
4ad25e43 EZ |
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) | |
97 | ||
910476ef | 98 | ;;;###autoload |
c5292bc8 | 99 | (defcustom lpr-command |
6bdad9ae | 100 | (purecopy |
8bc62130 | 101 | (cond |
4ad25e43 | 102 | (lpr-windows-system |
8bc62130 | 103 | "") |
4ad25e43 | 104 | (lpr-lp-system |
8bc62130 AI |
105 | "lp") |
106 | (t | |
6bdad9ae | 107 | "lpr"))) |
9201cc28 | 108 | "Name of program for printing a file. |
8bc62130 AI |
109 | |
110 | On MS-DOS and MS-Windows systems, if the value is an empty string then | |
111 | Emacs will write directly to the printer port named by `printer-name'. | |
112 | The programs `print' and `nprint' (the standard print programs on | |
113 | Windows NT and Novell Netware respectively) are handled specially, using | |
114 | `printer-name' as the destination for output; any other program is | |
115 | treated like `lpr' except that an explicit filename is given as the last | |
116 | argument." | |
c5292bc8 RS |
117 | :type 'string |
118 | :group 'lpr) | |
d6c7e99a | 119 | |
7173ec77 RS |
120 | ;; Default is nil, because that enables us to use pr -f |
121 | ;; which is more reliable than pr with no args, which is what lpr -p does. | |
c5292bc8 | 122 | (defcustom lpr-headers-switches nil |
9201cc28 | 123 | "List of strings of options to request page headings in the printer program. |
7173ec77 | 124 | If nil, we run `lpr-page-header-program' to make page headings |
c5292bc8 RS |
125 | and print the result." |
126 | :type '(repeat (string :tag "Argument")) | |
127 | :group 'lpr) | |
3a621cfe | 128 | |
c5292bc8 | 129 | (defcustom print-region-function nil |
d6c7e99a | 130 | "Function to call to print the region on a printer. |
c5292bc8 | 131 | See definition of `print-region-1' for calling conventions." |
53c9f0fa | 132 | :type '(choice (const nil) function) |
c5292bc8 | 133 | :group 'lpr) |
d6c7e99a | 134 | |
c5292bc8 | 135 | (defcustom lpr-page-header-program "pr" |
9201cc28 | 136 | "Name of program for adding page headers to a file." |
c5292bc8 RS |
137 | :type 'string |
138 | :group 'lpr) | |
c820123b | 139 | |
bafa1a69 RS |
140 | ;; Berkeley systems support -F, and GNU pr supports both -f and -F, |
141 | ;; So it looks like -F is a better default. | |
be62e9d5 | 142 | (defcustom lpr-page-header-switches '("-h" "%s" "-F") |
9201cc28 | 143 | "List of strings to use as options for the page-header-generating program. |
be62e9d5 GM |
144 | If `%s' appears in any of the strings, it is substituted by the page title. |
145 | Note that for correct quoting, `%s' should normally be a separate element. | |
c5292bc8 RS |
146 | The variable `lpr-page-header-program' specifies the program to use." |
147 | :type '(repeat string) | |
148 | :group 'lpr) | |
c820123b | 149 | |
7229064d | 150 | ;;;###autoload |
d6c7e99a | 151 | (defun lpr-buffer () |
d7e110e4 RS |
152 | "Print buffer contents without pagination or page headers. |
153 | See the variables `lpr-switches' and `lpr-command' | |
154 | for customization of the printer command." | |
76dabd37 CY |
155 | (interactive |
156 | (unless (y-or-n-p "Send current buffer to default printer? ") | |
157 | (error "Cancelled"))) | |
d6c7e99a RS |
158 | (print-region-1 (point-min) (point-max) lpr-switches nil)) |
159 | ||
7229064d | 160 | ;;;###autoload |
d6c7e99a | 161 | (defun print-buffer () |
e20192f4 | 162 | "Paginate and print buffer contents. |
e20192f4 | 163 | |
d7e110e4 RS |
164 | The variable `lpr-headers-switches' controls how to paginate. |
165 | If it is nil (the default), we run the `pr' program (or whatever program | |
166 | `lpr-page-header-program' specifies) to paginate. | |
167 | `lpr-page-header-switches' specifies the switches for that program. | |
168 | ||
169 | Otherwise, the switches in `lpr-headers-switches' are used | |
170 | in the print command itself; we expect them to request pagination. | |
f1180544 | 171 | |
d7e110e4 RS |
172 | See the variables `lpr-switches' and `lpr-command' |
173 | for further customization of the printer command." | |
76dabd37 CY |
174 | (interactive |
175 | (unless (y-or-n-p "Send current buffer to default printer? ") | |
176 | (error "Cancelled"))) | |
d6c7e99a RS |
177 | (print-region-1 (point-min) (point-max) lpr-switches t)) |
178 | ||
7229064d | 179 | ;;;###autoload |
d6c7e99a | 180 | (defun lpr-region (start end) |
d7e110e4 RS |
181 | "Print region contents without pagination or page headers. |
182 | See the variables `lpr-switches' and `lpr-command' | |
183 | for customization of the printer command." | |
76dabd37 CY |
184 | (interactive |
185 | (if (y-or-n-p "Send selected text to default printer? ") | |
186 | (list (region-beginning) (region-end)) | |
187 | (error "Cancelled"))) | |
d6c7e99a RS |
188 | (print-region-1 start end lpr-switches nil)) |
189 | ||
7229064d | 190 | ;;;###autoload |
d6c7e99a | 191 | (defun print-region (start end) |
d7e110e4 RS |
192 | "Paginate and print the region contents. |
193 | ||
194 | The variable `lpr-headers-switches' controls how to paginate. | |
195 | If it is nil (the default), we run the `pr' program (or whatever program | |
196 | `lpr-page-header-program' specifies) to paginate. | |
197 | `lpr-page-header-switches' specifies the switches for that program. | |
198 | ||
199 | Otherwise, the switches in `lpr-headers-switches' are used | |
200 | in the print command itself; we expect them to request pagination. | |
f1180544 | 201 | |
d7e110e4 RS |
202 | See the variables `lpr-switches' and `lpr-command' |
203 | for further customization of the printer command." | |
76dabd37 CY |
204 | (interactive |
205 | (if (y-or-n-p "Send selected text to default printer? ") | |
206 | (list (region-beginning) (region-end)) | |
207 | (error "Cancelled"))) | |
d6c7e99a RS |
208 | (print-region-1 start end lpr-switches t)) |
209 | ||
210 | (defun print-region-1 (start end switches page-headers) | |
904d7624 RS |
211 | ;; On some MIPS system, having a space in the job name |
212 | ;; crashes the printer demon. But using dashes looks ugly | |
213 | ;; and it seems to annoying to do for that MIPS system. | |
4ad25e43 | 214 | (let ((name (concat (buffer-name) " Emacs buffer")) |
a8037455 | 215 | (title (concat (buffer-name) " Emacs buffer")) |
0b45d07b EZ |
216 | ;; Make pipes use the same coding system as |
217 | ;; writing the buffer to a file would. | |
4ad25e43 EZ |
218 | (coding-system-for-write (or coding-system-for-write |
219 | buffer-file-coding-system)) | |
220 | (coding-system-for-read (or coding-system-for-read | |
221 | buffer-file-coding-system)) | |
5bbf28a8 | 222 | (width tab-width) |
4ad25e43 | 223 | nswitches |
5bbf28a8 | 224 | switch-string) |
d6c7e99a | 225 | (save-excursion |
4ad25e43 EZ |
226 | (and page-headers lpr-headers-switches |
227 | ;; It's possible to use an lpr option to get page headers. | |
228 | (setq switches (append (if (stringp lpr-headers-switches) | |
229 | (list lpr-headers-switches) | |
230 | lpr-headers-switches) | |
231 | switches))) | |
232 | (setq nswitches (lpr-flatten-list | |
233 | (mapcar 'lpr-eval-switch ; Dynamic evaluation | |
234 | switches)) | |
235 | switch-string (if switches | |
236 | (concat " with options " | |
237 | (mapconcat 'identity switches " ")) | |
238 | "")) | |
5bbf28a8 | 239 | (message "Spooling%s..." switch-string) |
d6c7e99a | 240 | (if (/= tab-width 8) |
8fa64b34 | 241 | (let ((new-coords (print-region-new-buffer start end))) |
4ad25e43 EZ |
242 | (setq start (car new-coords) |
243 | end (cdr new-coords) | |
244 | tab-width width) | |
2adf4f61 RS |
245 | (save-excursion |
246 | (goto-char end) | |
247 | (setq end (point-marker))) | |
d6c7e99a RS |
248 | (untabify (point-min) (point-max)))) |
249 | (if page-headers | |
3a621cfe | 250 | (if lpr-headers-switches |
5bbf28a8 RS |
251 | ;; We handled this above by modifying SWITCHES. |
252 | nil | |
7173ec77 | 253 | ;; Run a separate program to get page headers. |
8fa64b34 | 254 | (let ((new-coords (print-region-new-buffer start end))) |
4ad25e43 EZ |
255 | (apply 'call-process-region (car new-coords) (cdr new-coords) |
256 | lpr-page-header-program t t nil | |
c98557ae EZ |
257 | (mapcar (lambda (e) (format e title)) |
258 | lpr-page-header-switches))) | |
4ad25e43 EZ |
259 | (setq start (point-min) |
260 | end (point-max)))) | |
261 | (apply (or print-region-function 'call-process-region) | |
262 | (nconc (list start end lpr-command | |
263 | nil nil nil) | |
264 | (and lpr-add-switches | |
265 | (list "-J" name)) | |
266 | ;; These belong in pr if we are using that. | |
267 | (and lpr-add-switches lpr-headers-switches | |
268 | (list "-T" title)) | |
269 | (and (stringp printer-name) | |
270 | (list (concat lpr-printer-switch | |
271 | printer-name))) | |
272 | nswitches)) | |
2adf4f61 RS |
273 | (if (markerp end) |
274 | (set-marker end nil)) | |
5bbf28a8 | 275 | (message "Spooling%s...done" switch-string)))) |
d6c7e99a RS |
276 | |
277 | ;; This function copies the text between start and end | |
8fa64b34 RS |
278 | ;; into a new buffer, makes that buffer current. |
279 | ;; It returns the new range to print from the new current buffer | |
280 | ;; as (START . END). | |
281 | ||
3a621cfe | 282 | (defun print-region-new-buffer (ostart oend) |
8fa64b34 RS |
283 | (if (string= (buffer-name) " *spool temp*") |
284 | (cons ostart oend) | |
285 | (let ((oldbuf (current-buffer))) | |
286 | (set-buffer (get-buffer-create " *spool temp*")) | |
4ad25e43 EZ |
287 | (widen) |
288 | (erase-buffer) | |
8fa64b34 RS |
289 | (insert-buffer-substring oldbuf ostart oend) |
290 | (cons (point-min) (point-max))))) | |
6594deb0 | 291 | |
1c2df063 | 292 | (defun printify-region (begin end) |
8c44b2ad RS |
293 | "Replace nonprinting characters in region with printable representations. |
294 | The printable representations use ^ (for ASCII control characters) or hex. | |
295 | The characters tab, linefeed, space, return and formfeed are not affected." | |
1c2df063 ER |
296 | (interactive "r") |
297 | (save-excursion | |
5a1173cb VJL |
298 | (save-restriction |
299 | (narrow-to-region begin end) | |
300 | (goto-char (point-min)) | |
301 | (let (c) | |
302 | (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t) | |
303 | (setq c (preceding-char)) | |
d355a0b7 | 304 | (delete-char -1) |
f93e905f | 305 | (insert (if (< c ?\s) |
5a1173cb VJL |
306 | (format "\\^%c" (+ c ?@)) |
307 | (format "\\%02x" c)))))))) | |
4ad25e43 EZ |
308 | |
309 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
310 | ;; Functions hacked from `ps-print' package. | |
311 | ||
312 | ;; Dynamic evaluation | |
313 | (defun lpr-eval-switch (arg) | |
314 | (cond ((stringp arg) arg) | |
315 | ((functionp arg) (apply arg nil)) | |
316 | ((symbolp arg) (symbol-value arg)) | |
317 | ((consp arg) (apply (car arg) (cdr arg))) | |
318 | (t nil))) | |
319 | ||
320 | ;; `lpr-flatten-list' is defined here (copied from "message.el" and | |
321 | ;; enhanced to handle dotted pairs as well) until we can get some | |
322 | ;; sensible autoloads, or `flatten-list' gets put somewhere decent. | |
323 | ||
324 | ;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j)) | |
325 | ;; => (a b c d e f g h i j) | |
326 | ||
327 | (defun lpr-flatten-list (&rest list) | |
328 | (lpr-flatten-list-1 list)) | |
329 | ||
330 | (defun lpr-flatten-list-1 (list) | |
331 | (cond | |
332 | ((null list) (list)) | |
333 | ((consp list) | |
334 | (append (lpr-flatten-list-1 (car list)) | |
335 | (lpr-flatten-list-1 (cdr list)))) | |
336 | (t (list list)))) | |
1c2df063 | 337 | |
977b1278 KH |
338 | (provide 'lpr) |
339 | ||
6594deb0 | 340 | ;;; lpr.el ends here |