38412
|
1 ;;; lpr.el --- print Emacs buffer on line printer
|
659
|
2
|
35724
|
3 ;; Copyright (C) 1985, 1988, 1992, 1994, 2001 Free Software Foundation, Inc.
|
846
|
4
|
38412
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: unix
|
807
|
7
|
155
|
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
|
807
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
155
|
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
|
14169
|
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.
|
155
|
24
|
2307
|
25 ;;; Commentary:
|
|
26
|
35743
|
27 ;; Commands to send the region or a buffer to your printer. Entry points
|
2307
|
28 ;; are `lpr-buffer', `print-buffer', lpr-region', or `print-region'; option
|
22679
|
29 ;; variables include `printer-name', `lpr-switches' and `lpr-command'.
|
2307
|
30
|
807
|
31 ;;; Code:
|
155
|
32
|
35743
|
33 ;;;###autoload
|
35724
|
34 (defvar lpr-windows-system
|
|
35 (memq system-type '(emx win32 w32 mswindows ms-dos windows-nt)))
|
|
36
|
35743
|
37 ;;;###autoload
|
35724
|
38 (defvar lpr-lp-system
|
|
39 (memq system-type '(usg-unix-v dgux hpux irix)))
|
|
40
|
|
41
|
17414
|
42 (defgroup lpr nil
|
|
43 "Print Emacs buffer on line printer"
|
|
44 :group 'wp)
|
|
45
|
35724
|
46
|
22679
|
47 ;;;###autoload
|
|
48 (defcustom printer-name
|
35724
|
49 (and lpr-windows-system "PRN")
|
22679
|
50 "*The name of a local printer to which data is sent for printing.
|
|
51 \(Note that PostScript files are sent to `ps-printer-name', which see.\)
|
|
52
|
|
53 On Unix-like systems, a string value should be a name understood by
|
24091
|
54 lpr's -P option; otherwise the value should be nil.
|
22679
|
55
|
24091
|
56 On MS-DOS and MS-Windows systems, a string value is taken as the name of
|
|
57 a printer device or port, provided `lpr-command' is set to \"\".
|
|
58 Typical non-default settings would be \"LPT1\" to \"LPT3\" for parallel
|
|
59 printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial printers, or
|
|
60 \"//hostname/printer\" for a shared network printer. You can also set
|
|
61 it to the name of a file, in which case the output gets appended to that
|
|
62 file. If you want to discard the printed output, set this to \"NUL\"."
|
35724
|
63 :type '(choice :menu-tag "Printer Name"
|
|
64 :tag "Printer Name"
|
|
65 (const :tag "Default" nil)
|
|
66 ;; could use string but then we lose completion for files.
|
|
67 (file :tag "Name"))
|
22679
|
68 :group 'lpr)
|
17414
|
69
|
269
|
70 ;;;###autoload
|
35724
|
71 (defcustom lpr-switches nil
|
13803
|
72 "*List of strings to pass as extra options for the printer program.
|
22679
|
73 It is recommended to set `printer-name' instead of including an explicit
|
|
74 switch on this list.
|
17414
|
75 See `lpr-command'."
|
|
76 :type '(repeat (string :tag "Argument"))
|
|
77 :group 'lpr)
|
5267
|
78
|
36420
|
79 (defcustom lpr-add-switches (memq system-type '(berkeley-unix gnu/linux))
|
|
80 "*Non-nil means construct `-T' and `-J' options for the printer program.
|
13803
|
81 These are made assuming that the program is `lpr';
|
|
82 if you are using some other incompatible printer program,
|
17414
|
83 this variable should be nil."
|
|
84 :type 'boolean
|
|
85 :group 'lpr)
|
155
|
86
|
35724
|
87 (defcustom lpr-printer-switch
|
|
88 (if lpr-lp-system
|
|
89 "-d "
|
|
90 "-P")
|
|
91 "*Printer switch, that is, something like \"-P\", \"-d \", \"/D:\", etc.
|
|
92 This switch is used in conjunction with `printer-name'."
|
|
93 :type '(choice :menu-tag "Printer Name Switch"
|
|
94 :tag "Printer Name Switch"
|
|
95 (const :tag "None" nil)
|
|
96 (string :tag "Printer Switch"))
|
|
97 :group 'lpr)
|
|
98
|
1068
|
99 ;;;###autoload
|
17414
|
100 (defcustom lpr-command
|
24091
|
101 (cond
|
35724
|
102 (lpr-windows-system
|
24091
|
103 "")
|
35724
|
104 (lpr-lp-system
|
24091
|
105 "lp")
|
|
106 (t
|
|
107 "lpr"))
|
|
108 "*Name of program for printing a file.
|
|
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."
|
17414
|
117 :type 'string
|
|
118 :group 'lpr)
|
155
|
119
|
10637
|
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.
|
17414
|
122 (defcustom lpr-headers-switches nil
|
13894
|
123 "*List of strings of options to request page headings in the printer program.
|
10637
|
124 If nil, we run `lpr-page-header-program' to make page headings
|
17414
|
125 and print the result."
|
|
126 :type '(repeat (string :tag "Argument"))
|
|
127 :group 'lpr)
|
5267
|
128
|
17414
|
129 (defcustom print-region-function nil
|
155
|
130 "Function to call to print the region on a printer.
|
17414
|
131 See definition of `print-region-1' for calling conventions."
|
35969
|
132 :type '(choice (const nil) function)
|
17414
|
133 :group 'lpr)
|
155
|
134
|
17414
|
135 (defcustom lpr-page-header-program "pr"
|
|
136 "*Name of program for adding page headers to a file."
|
|
137 :type 'string
|
|
138 :group 'lpr)
|
9187
|
139
|
17813
|
140 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
|
|
141 ;; So it looks like -F is a better default.
|
36419
50998d3dd0bb
(lpr-page-header-switches, print-region-1): Undo 2000-07-06 change.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
142 (defcustom lpr-page-header-switches '("-F")
|
13894
|
143 "*List of strings to use as options for the page-header-generating program.
|
17414
|
144 The variable `lpr-page-header-program' specifies the program to use."
|
|
145 :type '(repeat string)
|
|
146 :group 'lpr)
|
9187
|
147
|
256
|
148 ;;;###autoload
|
155
|
149 (defun lpr-buffer ()
|
27007
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
150 "Print buffer contents without pagination or page headers.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
151 See the variables `lpr-switches' and `lpr-command'
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
152 for customization of the printer command."
|
155
|
153 (interactive)
|
|
154 (print-region-1 (point-min) (point-max) lpr-switches nil))
|
|
155
|
256
|
156 ;;;###autoload
|
155
|
157 (defun print-buffer ()
|
26219
|
158 "Paginate and print buffer contents.
|
27007
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
159
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
160 The variable `lpr-headers-switches' controls how to paginate.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
161 If it is nil (the default), we run the `pr' program (or whatever program
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
162 `lpr-page-header-program' specifies) to paginate.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
163 `lpr-page-header-switches' specifies the switches for that program.
|
26219
|
164
|
27007
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
165 Otherwise, the switches in `lpr-headers-switches' are used
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
166 in the print command itself; we expect them to request pagination.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
167
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
168 See the variables `lpr-switches' and `lpr-command'
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
169 for further customization of the printer command."
|
155
|
170 (interactive)
|
|
171 (print-region-1 (point-min) (point-max) lpr-switches t))
|
|
172
|
256
|
173 ;;;###autoload
|
155
|
174 (defun lpr-region (start end)
|
27007
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
175 "Print region contents without pagination or page headers.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
176 See the variables `lpr-switches' and `lpr-command'
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
177 for customization of the printer command."
|
155
|
178 (interactive "r")
|
|
179 (print-region-1 start end lpr-switches nil))
|
|
180
|
256
|
181 ;;;###autoload
|
155
|
182 (defun print-region (start end)
|
27007
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
183 "Paginate and print the region contents.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
184
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
185 The variable `lpr-headers-switches' controls how to paginate.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
186 If it is nil (the default), we run the `pr' program (or whatever program
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
187 `lpr-page-header-program' specifies) to paginate.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
188 `lpr-page-header-switches' specifies the switches for that program.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
189
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
190 Otherwise, the switches in `lpr-headers-switches' are used
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
191 in the print command itself; we expect them to request pagination.
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
192
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
193 See the variables `lpr-switches' and `lpr-command'
|
2d337fafb392
(lpr-buffer, print-buffer, lpr-region, print-region): Doc fixes.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
194 for further customization of the printer command."
|
155
|
195 (interactive "r")
|
|
196 (print-region-1 start end lpr-switches t))
|
|
197
|
|
198 (defun print-region-1 (start end switches page-headers)
|
8113
|
199 ;; On some MIPS system, having a space in the job name
|
|
200 ;; crashes the printer demon. But using dashes looks ugly
|
|
201 ;; and it seems to annoying to do for that MIPS system.
|
35724
|
202 (let ((name (concat (buffer-name) " Emacs buffer"))
|
8020
|
203 (title (concat (buffer-name) " Emacs buffer"))
|
21569
|
204 ;; Make pipes use the same coding system as
|
|
205 ;; writing the buffer to a file would.
|
35724
|
206 (coding-system-for-write (or coding-system-for-write
|
|
207 buffer-file-coding-system))
|
|
208 (coding-system-for-read (or coding-system-for-read
|
|
209 buffer-file-coding-system))
|
10708
|
210 (width tab-width)
|
35724
|
211 nswitches
|
10708
|
212 switch-string)
|
155
|
213 (save-excursion
|
35724
|
214 (and page-headers lpr-headers-switches
|
|
215 ;; It's possible to use an lpr option to get page headers.
|
|
216 (setq switches (append (if (stringp lpr-headers-switches)
|
|
217 (list lpr-headers-switches)
|
|
218 lpr-headers-switches)
|
|
219 switches)))
|
|
220 (setq nswitches (lpr-flatten-list
|
|
221 (mapcar 'lpr-eval-switch ; Dynamic evaluation
|
|
222 switches))
|
|
223 switch-string (if switches
|
|
224 (concat " with options "
|
|
225 (mapconcat 'identity switches " "))
|
|
226 ""))
|
10708
|
227 (message "Spooling%s..." switch-string)
|
155
|
228 (if (/= tab-width 8)
|
10700
|
229 (let ((new-coords (print-region-new-buffer start end)))
|
35724
|
230 (setq start (car new-coords)
|
|
231 end (cdr new-coords)
|
|
232 tab-width width)
|
4446
|
233 (save-excursion
|
|
234 (goto-char end)
|
|
235 (setq end (point-marker)))
|
155
|
236 (untabify (point-min) (point-max))))
|
|
237 (if page-headers
|
5267
|
238 (if lpr-headers-switches
|
10708
|
239 ;; We handled this above by modifying SWITCHES.
|
|
240 nil
|
10637
|
241 ;; Run a separate program to get page headers.
|
10700
|
242 (let ((new-coords (print-region-new-buffer start end)))
|
35724
|
243 (apply 'call-process-region (car new-coords) (cdr new-coords)
|
|
244 lpr-page-header-program t t nil
|
36419
50998d3dd0bb
(lpr-page-header-switches, print-region-1): Undo 2000-07-06 change.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
245 (nconc (list "-h" title)
|
50998d3dd0bb
(lpr-page-header-switches, print-region-1): Undo 2000-07-06 change.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
246 lpr-page-header-switches)))
|
35724
|
247 (setq start (point-min)
|
|
248 end (point-max))))
|
|
249 (apply (or print-region-function 'call-process-region)
|
|
250 (nconc (list start end lpr-command
|
|
251 nil nil nil)
|
|
252 (and lpr-add-switches
|
|
253 (list "-J" name))
|
|
254 ;; These belong in pr if we are using that.
|
|
255 (and lpr-add-switches lpr-headers-switches
|
|
256 (list "-T" title))
|
|
257 (and (stringp printer-name)
|
|
258 (list (concat lpr-printer-switch
|
|
259 printer-name)))
|
|
260 nswitches))
|
4446
|
261 (if (markerp end)
|
|
262 (set-marker end nil))
|
10708
|
263 (message "Spooling%s...done" switch-string))))
|
155
|
264
|
|
265 ;; This function copies the text between start and end
|
10700
|
266 ;; into a new buffer, makes that buffer current.
|
|
267 ;; It returns the new range to print from the new current buffer
|
|
268 ;; as (START . END).
|
|
269
|
5267
|
270 (defun print-region-new-buffer (ostart oend)
|
10700
|
271 (if (string= (buffer-name) " *spool temp*")
|
|
272 (cons ostart oend)
|
|
273 (let ((oldbuf (current-buffer)))
|
|
274 (set-buffer (get-buffer-create " *spool temp*"))
|
35724
|
275 (widen)
|
|
276 (erase-buffer)
|
10700
|
277 (insert-buffer-substring oldbuf ostart oend)
|
|
278 (cons (point-min) (point-max)))))
|
659
|
279
|
2382
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
280 (defun printify-region (begin end)
|
17695
|
281 "Replace nonprinting characters in region with printable representations.
|
|
282 The printable representations use ^ (for ASCII control characters) or hex.
|
|
283 The characters tab, linefeed, space, return and formfeed are not affected."
|
2382
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
284 (interactive "r")
|
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
285 (save-excursion
|
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
286 (goto-char begin)
|
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
287 (let (c)
|
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
288 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
|
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
289 (setq c (preceding-char))
|
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
290 (delete-backward-char 1)
|
35724
|
291 (insert (if (< c ?\ )
|
|
292 (format "\\^%c" (+ c ?@))
|
|
293 (format "\\%02x" c)))))))
|
|
294
|
|
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
296 ;; Functions hacked from `ps-print' package.
|
|
297
|
|
298 ;; Dynamic evaluation
|
|
299 (defun lpr-eval-switch (arg)
|
|
300 (cond ((stringp arg) arg)
|
|
301 ((functionp arg) (apply arg nil))
|
|
302 ((symbolp arg) (symbol-value arg))
|
|
303 ((consp arg) (apply (car arg) (cdr arg)))
|
|
304 (t nil)))
|
|
305
|
|
306 ;; `lpr-flatten-list' is defined here (copied from "message.el" and
|
|
307 ;; enhanced to handle dotted pairs as well) until we can get some
|
|
308 ;; sensible autoloads, or `flatten-list' gets put somewhere decent.
|
|
309
|
|
310 ;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j))
|
|
311 ;; => (a b c d e f g h i j)
|
|
312
|
|
313 (defun lpr-flatten-list (&rest list)
|
|
314 (lpr-flatten-list-1 list))
|
|
315
|
|
316 (defun lpr-flatten-list-1 (list)
|
|
317 (cond
|
|
318 ((null list) (list))
|
|
319 ((consp list)
|
|
320 (append (lpr-flatten-list-1 (car list))
|
|
321 (lpr-flatten-list-1 (cdr list))))
|
|
322 (t (list list))))
|
2382
030b76db285a
(printify-buffer) Added, debugged from Roland McGrath's printify-buffer code
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
323
|
11831
|
324 (provide 'lpr)
|
|
325
|
659
|
326 ;;; lpr.el ends here
|