]> code.delx.au - gnu-emacs/blob - lisp/lpr.el
Doc fixes.
[gnu-emacs] / lisp / lpr.el
1 ;;; lpr.el --- print Emacs buffer on line printer.
2
3 ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: unix
7
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
24
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
29 ;; variables include `printer-name', `lpr-switches' and `lpr-command'.
30
31 ;;; Code:
32
33 (defgroup lpr nil
34 "Print Emacs buffer on line printer"
35 :group 'wp)
36
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
44 lpr's -P option.
45
46 On MS-DOS and MS-Windows systems, it is the name of a printer device or
47 port. Typical non-default settings would be \"LPT1\" to \"LPT3\" for
48 parallel printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial
49 printers, or \"//hostname/printer\" for a shared network printer. You
50 can also set it to a name of a file, in which case the output gets
51 appended to that file. If you want to discard the printed output, set
52 this to \"NUL\"."
53 :type 'file ; could use string but then we lose completion for files.
54 :group 'lpr)
55
56 ;;;###autoload
57 (defcustom lpr-switches nil
58 "*List of strings to pass as extra options for the printer program.
59 It is recommended to set `printer-name' instead of including an explicit
60 switch on this list.
61 See `lpr-command'."
62 :type '(repeat (string :tag "Argument"))
63 :group 'lpr)
64
65 (defcustom lpr-add-switches (eq system-type 'berkeley-unix)
66 "*Non-nil means construct -T and -J options for the printer program.
67 These are made assuming that the program is `lpr';
68 if you are using some other incompatible printer program,
69 this variable should be nil."
70 :type 'boolean
71 :group 'lpr)
72
73 ;;;###autoload
74 (defcustom lpr-command
75 (if (memq system-type '(usg-unix-v dgux hpux irix))
76 "lp" "lpr")
77 "*Name of program for printing a file."
78 :type 'string
79 :group 'lpr)
80
81 ;; Default is nil, because that enables us to use pr -f
82 ;; which is more reliable than pr with no args, which is what lpr -p does.
83 (defcustom lpr-headers-switches nil
84 "*List of strings of options to request page headings in the printer program.
85 If nil, we run `lpr-page-header-program' to make page headings
86 and print the result."
87 :type '(repeat (string :tag "Argument"))
88 :group 'lpr)
89
90 (defcustom print-region-function nil
91 "Function to call to print the region on a printer.
92 See definition of `print-region-1' for calling conventions."
93 :type 'function
94 :group 'lpr)
95
96 (defcustom lpr-page-header-program "pr"
97 "*Name of program for adding page headers to a file."
98 :type 'string
99 :group 'lpr)
100
101 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
102 ;; So it looks like -F is a better default.
103 (defcustom lpr-page-header-switches '("-F")
104 "*List of strings to use as options for the page-header-generating program.
105 The variable `lpr-page-header-program' specifies the program to use."
106 :type '(repeat string)
107 :group 'lpr)
108
109 ;;;###autoload
110 (defun lpr-buffer ()
111 "Print buffer contents as with Unix command `lpr'.
112 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
113 (interactive)
114 (print-region-1 (point-min) (point-max) lpr-switches nil))
115
116 ;;;###autoload
117 (defun print-buffer ()
118 "Print buffer contents as with Unix command `lpr -p'.
119 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
120 (interactive)
121 (print-region-1 (point-min) (point-max) lpr-switches t))
122
123 ;;;###autoload
124 (defun lpr-region (start end)
125 "Print region contents as with Unix command `lpr'.
126 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
127 (interactive "r")
128 (print-region-1 start end lpr-switches nil))
129
130 ;;;###autoload
131 (defun print-region (start end)
132 "Print region contents as with Unix command `lpr -p'.
133 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
134 (interactive "r")
135 (print-region-1 start end lpr-switches t))
136
137 (defun print-region-1 (start end switches page-headers)
138 ;; On some MIPS system, having a space in the job name
139 ;; crashes the printer demon. But using dashes looks ugly
140 ;; and it seems to annoying to do for that MIPS system.
141 (let ((name (concat (buffer-name) " Emacs buffer"))
142 (title (concat (buffer-name) " Emacs buffer"))
143 ;; Make pipes use the same coding system as
144 ;; writing the buffer to a file would.
145 (coding-system-for-write
146 (or coding-system-for-write buffer-file-coding-system))
147 (coding-system-for-read
148 (or coding-system-for-read buffer-file-coding-system))
149 (width tab-width)
150 switch-string)
151 (save-excursion
152 (if page-headers
153 (if lpr-headers-switches
154 ;; It is possible to use an lpr option
155 ;; to get page headers.
156 (setq switches (append (if (stringp lpr-headers-switches)
157 (list lpr-headers-switches)
158 lpr-headers-switches)
159 switches))))
160 (setq switch-string
161 (if switches (concat " with options "
162 (mapconcat 'identity switches " "))
163 ""))
164 (message "Spooling%s..." switch-string)
165 (if (/= tab-width 8)
166 (let ((new-coords (print-region-new-buffer start end)))
167 (setq start (car new-coords) end (cdr new-coords))
168 (setq tab-width width)
169 (save-excursion
170 (goto-char end)
171 (setq end (point-marker)))
172 (untabify (point-min) (point-max))))
173 (if page-headers
174 (if lpr-headers-switches
175 ;; We handled this above by modifying SWITCHES.
176 nil
177 ;; Run a separate program to get page headers.
178 (let ((new-coords (print-region-new-buffer start end)))
179 (setq start (car new-coords) end (cdr new-coords)))
180 (apply 'call-process-region start end lpr-page-header-program
181 t t nil
182 (nconc (list "-h" title)
183 lpr-page-header-switches))
184 (setq start (point-min) end (point-max))))
185 (apply (or print-region-function 'call-process-region)
186 (nconc (list start end lpr-command
187 nil nil nil)
188 (nconc (and lpr-add-switches
189 (list "-J" name))
190 ;; These belong in pr if we are using that.
191 (and lpr-add-switches lpr-headers-switches
192 (list "-T" title))
193 (and (stringp printer-name)
194 (list (concat "-P" printer-name)))
195 switches)))
196 (if (markerp end)
197 (set-marker end nil))
198 (message "Spooling%s...done" switch-string))))
199
200 ;; This function copies the text between start and end
201 ;; into a new buffer, makes that buffer current.
202 ;; It returns the new range to print from the new current buffer
203 ;; as (START . END).
204
205 (defun print-region-new-buffer (ostart oend)
206 (if (string= (buffer-name) " *spool temp*")
207 (cons ostart oend)
208 (let ((oldbuf (current-buffer)))
209 (set-buffer (get-buffer-create " *spool temp*"))
210 (widen) (erase-buffer)
211 (insert-buffer-substring oldbuf ostart oend)
212 (cons (point-min) (point-max)))))
213
214 (defun printify-region (begin end)
215 "Replace nonprinting characters in region with printable representations.
216 The printable representations use ^ (for ASCII control characters) or hex.
217 The characters tab, linefeed, space, return and formfeed are not affected."
218 (interactive "r")
219 (save-excursion
220 (goto-char begin)
221 (let (c)
222 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
223 (setq c (preceding-char))
224 (delete-backward-char 1)
225 (insert
226 (if (< c ?\ )
227 (format "\\^%c" (+ c ?@))
228 (format "\\%02x" c)))))))
229
230 (provide 'lpr)
231
232 ;;; lpr.el ends here