Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; lpr.el --- print Emacs buffer on line printer. |
2 | ||
8f1204db | 3 | ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc. |
3a801d0c | 4 | |
e5167999 | 5 | ;; Maintainer: FSF |
fd7fa35a | 6 | ;; Keywords: unix |
e5167999 | 7 | |
d6c7e99a RS |
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 | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
d6c7e99a RS |
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 | |
b578f267 EN |
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. | |
d6c7e99a | 24 | |
e41b2db1 ER |
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 | |
8748e3eb | 29 | ;; variables include `printer-name', `lpr-switches' and `lpr-command'. |
e41b2db1 | 30 | |
e5167999 | 31 | ;;; Code: |
d6c7e99a | 32 | |
c5292bc8 RS |
33 | (defgroup lpr nil |
34 | "Print Emacs buffer on line printer" | |
35 | :group 'wp) | |
36 | ||
8748e3eb RS |
37 | ;;;###autoload |
38 | (defcustom printer-name | |
39 | (if (memq system-type '(ms-dos windows-nt)) "PRN") | |
40 | "*The name of a local printer to which data is sent for printing. | |
41 | \(Note that PostScript files are sent to `ps-printer-name', which see.\) | |
42 | ||
43 | On Unix-like systems, a string value should be a name understood by | |
8bc62130 AI |
44 | lpr's -P option; otherwise the value should be nil. |
45 | ||
46 | On MS-DOS and MS-Windows systems, a string value is taken as the name of | |
47 | a printer device or port, provided `lpr-command' is set to \"\". | |
48 | Typical non-default settings would be \"LPT1\" to \"LPT3\" for parallel | |
49 | printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial printers, or | |
50 | \"//hostname/printer\" for a shared network printer. You can also set | |
51 | it to the name of a file, in which case the output gets appended to that | |
52 | file. If you want to discard the printed output, set this to \"NUL\"." | |
3917863b AS |
53 | :type '(choice ; could use string but then we lose completion for files. |
54 | (file :tag "Name") | |
55 | (const :tag "Default" nil)) | |
8748e3eb | 56 | :group 'lpr) |
c5292bc8 | 57 | |
6503cec3 | 58 | ;;;###autoload |
c5292bc8 | 59 | (defcustom lpr-switches nil |
22466c09 | 60 | "*List of strings to pass as extra options for the printer program. |
8748e3eb RS |
61 | It is recommended to set `printer-name' instead of including an explicit |
62 | switch on this list. | |
c5292bc8 RS |
63 | See `lpr-command'." |
64 | :type '(repeat (string :tag "Argument")) | |
65 | :group 'lpr) | |
3a621cfe | 66 | |
c5292bc8 | 67 | (defcustom lpr-add-switches (eq system-type 'berkeley-unix) |
22466c09 KH |
68 | "*Non-nil means construct -T and -J options for the printer program. |
69 | These are made assuming that the program is `lpr'; | |
70 | if you are using some other incompatible printer program, | |
c5292bc8 RS |
71 | this variable should be nil." |
72 | :type 'boolean | |
73 | :group 'lpr) | |
d6c7e99a | 74 | |
910476ef | 75 | ;;;###autoload |
c5292bc8 | 76 | (defcustom lpr-command |
8bc62130 AI |
77 | (cond |
78 | ((memq system-type '(ms-dos windows-nt)) | |
79 | "") | |
80 | ((memq system-type '(usg-unix-v dgux hpux irix)) | |
81 | "lp") | |
82 | (t | |
83 | "lpr")) | |
84 | "*Name of program for printing a file. | |
85 | ||
86 | On MS-DOS and MS-Windows systems, if the value is an empty string then | |
87 | Emacs will write directly to the printer port named by `printer-name'. | |
88 | The programs `print' and `nprint' (the standard print programs on | |
89 | Windows NT and Novell Netware respectively) are handled specially, using | |
90 | `printer-name' as the destination for output; any other program is | |
91 | treated like `lpr' except that an explicit filename is given as the last | |
92 | argument." | |
c5292bc8 RS |
93 | :type 'string |
94 | :group 'lpr) | |
d6c7e99a | 95 | |
7173ec77 RS |
96 | ;; Default is nil, because that enables us to use pr -f |
97 | ;; which is more reliable than pr with no args, which is what lpr -p does. | |
c5292bc8 | 98 | (defcustom lpr-headers-switches nil |
5526cfde | 99 | "*List of strings of options to request page headings in the printer program. |
7173ec77 | 100 | If nil, we run `lpr-page-header-program' to make page headings |
c5292bc8 RS |
101 | and print the result." |
102 | :type '(repeat (string :tag "Argument")) | |
103 | :group 'lpr) | |
3a621cfe | 104 | |
c5292bc8 | 105 | (defcustom print-region-function nil |
d6c7e99a | 106 | "Function to call to print the region on a printer. |
c5292bc8 RS |
107 | See definition of `print-region-1' for calling conventions." |
108 | :type 'function | |
109 | :group 'lpr) | |
d6c7e99a | 110 | |
c5292bc8 RS |
111 | (defcustom lpr-page-header-program "pr" |
112 | "*Name of program for adding page headers to a file." | |
113 | :type 'string | |
114 | :group 'lpr) | |
c820123b | 115 | |
bafa1a69 RS |
116 | ;; Berkeley systems support -F, and GNU pr supports both -f and -F, |
117 | ;; So it looks like -F is a better default. | |
53e7c664 | 118 | (defcustom lpr-page-header-switches '("-h" "-F") |
5526cfde | 119 | "*List of strings to use as options for the page-header-generating program. |
c5292bc8 RS |
120 | The variable `lpr-page-header-program' specifies the program to use." |
121 | :type '(repeat string) | |
122 | :group 'lpr) | |
c820123b | 123 | |
7229064d | 124 | ;;;###autoload |
d6c7e99a | 125 | (defun lpr-buffer () |
d7e110e4 RS |
126 | "Print buffer contents without pagination or page headers. |
127 | See the variables `lpr-switches' and `lpr-command' | |
128 | for customization of the printer command." | |
d6c7e99a RS |
129 | (interactive) |
130 | (print-region-1 (point-min) (point-max) lpr-switches nil)) | |
131 | ||
7229064d | 132 | ;;;###autoload |
d6c7e99a | 133 | (defun print-buffer () |
e20192f4 | 134 | "Paginate and print buffer contents. |
e20192f4 | 135 | |
d7e110e4 RS |
136 | The variable `lpr-headers-switches' controls how to paginate. |
137 | If it is nil (the default), we run the `pr' program (or whatever program | |
138 | `lpr-page-header-program' specifies) to paginate. | |
139 | `lpr-page-header-switches' specifies the switches for that program. | |
140 | ||
141 | Otherwise, the switches in `lpr-headers-switches' are used | |
142 | in the print command itself; we expect them to request pagination. | |
143 | ||
144 | See the variables `lpr-switches' and `lpr-command' | |
145 | for further customization of the printer command." | |
d6c7e99a RS |
146 | (interactive) |
147 | (print-region-1 (point-min) (point-max) lpr-switches t)) | |
148 | ||
7229064d | 149 | ;;;###autoload |
d6c7e99a | 150 | (defun lpr-region (start end) |
d7e110e4 RS |
151 | "Print region contents without pagination or page headers. |
152 | See the variables `lpr-switches' and `lpr-command' | |
153 | for customization of the printer command." | |
d6c7e99a RS |
154 | (interactive "r") |
155 | (print-region-1 start end lpr-switches nil)) | |
156 | ||
7229064d | 157 | ;;;###autoload |
d6c7e99a | 158 | (defun print-region (start end) |
d7e110e4 RS |
159 | "Paginate and print the region contents. |
160 | ||
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. | |
165 | ||
166 | Otherwise, the switches in `lpr-headers-switches' are used | |
167 | in the print command itself; we expect them to request pagination. | |
168 | ||
169 | See the variables `lpr-switches' and `lpr-command' | |
170 | for further customization of the printer command." | |
d6c7e99a RS |
171 | (interactive "r") |
172 | (print-region-1 start end lpr-switches t)) | |
173 | ||
174 | (defun print-region-1 (start end switches page-headers) | |
904d7624 RS |
175 | ;; On some MIPS system, having a space in the job name |
176 | ;; crashes the printer demon. But using dashes looks ugly | |
177 | ;; and it seems to annoying to do for that MIPS system. | |
178 | (let ((name (concat (buffer-name) " Emacs buffer")) | |
a8037455 | 179 | (title (concat (buffer-name) " Emacs buffer")) |
0b45d07b EZ |
180 | ;; Make pipes use the same coding system as |
181 | ;; writing the buffer to a file would. | |
182 | (coding-system-for-write | |
183 | (or coding-system-for-write buffer-file-coding-system)) | |
184 | (coding-system-for-read | |
185 | (or coding-system-for-read buffer-file-coding-system)) | |
5bbf28a8 RS |
186 | (width tab-width) |
187 | switch-string) | |
d6c7e99a | 188 | (save-excursion |
5bbf28a8 RS |
189 | (if page-headers |
190 | (if lpr-headers-switches | |
191 | ;; It is possible to use an lpr option | |
192 | ;; to get page headers. | |
193 | (setq switches (append (if (stringp lpr-headers-switches) | |
194 | (list lpr-headers-switches) | |
195 | lpr-headers-switches) | |
196 | switches)))) | |
197 | (setq switch-string | |
198 | (if switches (concat " with options " | |
199 | (mapconcat 'identity switches " ")) | |
200 | "")) | |
201 | (message "Spooling%s..." switch-string) | |
d6c7e99a | 202 | (if (/= tab-width 8) |
8fa64b34 RS |
203 | (let ((new-coords (print-region-new-buffer start end))) |
204 | (setq start (car new-coords) end (cdr new-coords)) | |
d6c7e99a | 205 | (setq tab-width width) |
2adf4f61 RS |
206 | (save-excursion |
207 | (goto-char end) | |
208 | (setq end (point-marker))) | |
d6c7e99a RS |
209 | (untabify (point-min) (point-max)))) |
210 | (if page-headers | |
3a621cfe | 211 | (if lpr-headers-switches |
5bbf28a8 RS |
212 | ;; We handled this above by modifying SWITCHES. |
213 | nil | |
7173ec77 | 214 | ;; Run a separate program to get page headers. |
8fa64b34 RS |
215 | (let ((new-coords (print-region-new-buffer start end))) |
216 | (setq start (car new-coords) end (cdr new-coords))) | |
a3324470 RS |
217 | (apply 'call-process-region start end lpr-page-header-program |
218 | t t nil | |
53e7c664 | 219 | lpr-page-header-switches) |
3a621cfe | 220 | (setq start (point-min) end (point-max)))) |
ba74d3e3 GM |
221 | (let ((printer-name-switch (if (memq system-type |
222 | '(usg-unix-v dgux hpux irix)) | |
223 | "-d" "-P"))) | |
224 | (apply (or print-region-function 'call-process-region) | |
225 | (nconc (list start end lpr-command | |
226 | nil nil nil) | |
227 | (nconc (and lpr-add-switches | |
228 | (list "-J" name)) | |
229 | ;; These belong in pr if we are using that. | |
230 | (and lpr-add-switches lpr-headers-switches | |
231 | (list "-T" title)) | |
232 | (and (stringp printer-name) | |
233 | (list (concat printer-name-switch | |
234 | printer-name))) | |
235 | switches)))) | |
2adf4f61 RS |
236 | (if (markerp end) |
237 | (set-marker end nil)) | |
5bbf28a8 | 238 | (message "Spooling%s...done" switch-string)))) |
d6c7e99a RS |
239 | |
240 | ;; This function copies the text between start and end | |
8fa64b34 RS |
241 | ;; into a new buffer, makes that buffer current. |
242 | ;; It returns the new range to print from the new current buffer | |
243 | ;; as (START . END). | |
244 | ||
3a621cfe | 245 | (defun print-region-new-buffer (ostart oend) |
8fa64b34 RS |
246 | (if (string= (buffer-name) " *spool temp*") |
247 | (cons ostart oend) | |
248 | (let ((oldbuf (current-buffer))) | |
249 | (set-buffer (get-buffer-create " *spool temp*")) | |
250 | (widen) (erase-buffer) | |
251 | (insert-buffer-substring oldbuf ostart oend) | |
252 | (cons (point-min) (point-max))))) | |
6594deb0 | 253 | |
1c2df063 | 254 | (defun printify-region (begin end) |
8c44b2ad RS |
255 | "Replace nonprinting characters in region with printable representations. |
256 | The printable representations use ^ (for ASCII control characters) or hex. | |
257 | The characters tab, linefeed, space, return and formfeed are not affected." | |
1c2df063 ER |
258 | (interactive "r") |
259 | (save-excursion | |
260 | (goto-char begin) | |
261 | (let (c) | |
262 | (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t) | |
263 | (setq c (preceding-char)) | |
264 | (delete-backward-char 1) | |
265 | (insert | |
266 | (if (< c ?\ ) | |
267 | (format "\\^%c" (+ c ?@)) | |
268 | (format "\\%02x" c))))))) | |
269 | ||
977b1278 KH |
270 | (provide 'lpr) |
271 | ||
6594deb0 | 272 | ;;; lpr.el ends here |