]> code.delx.au - gnu-emacs/blob - lisp/lpr.el
iswitchb-default-keybindings: no-need for read-kbd-macro.
[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; 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\"."
53 :type '(choice ; could use string but then we lose completion for files.
54 (file :tag "Name")
55 (const :tag "Default" nil))
56 :group 'lpr)
57
58 ;;;###autoload
59 (defcustom lpr-switches nil
60 "*List of strings to pass as extra options for the printer program.
61 It is recommended to set `printer-name' instead of including an explicit
62 switch on this list.
63 See `lpr-command'."
64 :type '(repeat (string :tag "Argument"))
65 :group 'lpr)
66
67 (defcustom lpr-add-switches (eq system-type 'berkeley-unix)
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,
71 this variable should be nil."
72 :type 'boolean
73 :group 'lpr)
74
75 ;;;###autoload
76 (defcustom lpr-command
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."
93 :type 'string
94 :group 'lpr)
95
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.
98 (defcustom lpr-headers-switches nil
99 "*List of strings of options to request page headings in the printer program.
100 If nil, we run `lpr-page-header-program' to make page headings
101 and print the result."
102 :type '(repeat (string :tag "Argument"))
103 :group 'lpr)
104
105 (defcustom print-region-function nil
106 "Function to call to print the region on a printer.
107 See definition of `print-region-1' for calling conventions."
108 :type 'function
109 :group 'lpr)
110
111 (defcustom lpr-page-header-program "pr"
112 "*Name of program for adding page headers to a file."
113 :type 'string
114 :group 'lpr)
115
116 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
117 ;; So it looks like -F is a better default.
118 (defcustom lpr-page-header-switches '("-F")
119 "*List of strings to use as options for the page-header-generating program.
120 The variable `lpr-page-header-program' specifies the program to use."
121 :type '(repeat string)
122 :group 'lpr)
123
124 ;;;###autoload
125 (defun lpr-buffer ()
126 "Print buffer contents as with Unix command `lpr'.
127 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
128 (interactive)
129 (print-region-1 (point-min) (point-max) lpr-switches nil))
130
131 ;;;###autoload
132 (defun print-buffer ()
133 "Print buffer contents as with Unix command `lpr -p'.
134 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
135 (interactive)
136 (print-region-1 (point-min) (point-max) lpr-switches t))
137
138 ;;;###autoload
139 (defun lpr-region (start end)
140 "Print region contents as with Unix command `lpr'.
141 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
142 (interactive "r")
143 (print-region-1 start end lpr-switches nil))
144
145 ;;;###autoload
146 (defun print-region (start end)
147 "Print region contents as with Unix command `lpr -p'.
148 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
149 (interactive "r")
150 (print-region-1 start end lpr-switches t))
151
152 (defun print-region-1 (start end switches page-headers)
153 ;; On some MIPS system, having a space in the job name
154 ;; crashes the printer demon. But using dashes looks ugly
155 ;; and it seems to annoying to do for that MIPS system.
156 (let ((name (concat (buffer-name) " Emacs buffer"))
157 (title (concat (buffer-name) " Emacs buffer"))
158 ;; Make pipes use the same coding system as
159 ;; writing the buffer to a file would.
160 (coding-system-for-write
161 (or coding-system-for-write buffer-file-coding-system))
162 (coding-system-for-read
163 (or coding-system-for-read buffer-file-coding-system))
164 (width tab-width)
165 switch-string)
166 (save-excursion
167 (if page-headers
168 (if lpr-headers-switches
169 ;; It is possible to use an lpr option
170 ;; to get page headers.
171 (setq switches (append (if (stringp lpr-headers-switches)
172 (list lpr-headers-switches)
173 lpr-headers-switches)
174 switches))))
175 (setq switch-string
176 (if switches (concat " with options "
177 (mapconcat 'identity switches " "))
178 ""))
179 (message "Spooling%s..." switch-string)
180 (if (/= tab-width 8)
181 (let ((new-coords (print-region-new-buffer start end)))
182 (setq start (car new-coords) end (cdr new-coords))
183 (setq tab-width width)
184 (save-excursion
185 (goto-char end)
186 (setq end (point-marker)))
187 (untabify (point-min) (point-max))))
188 (if page-headers
189 (if lpr-headers-switches
190 ;; We handled this above by modifying SWITCHES.
191 nil
192 ;; Run a separate program to get page headers.
193 (let ((new-coords (print-region-new-buffer start end)))
194 (setq start (car new-coords) end (cdr new-coords)))
195 (apply 'call-process-region start end lpr-page-header-program
196 t t nil
197 (nconc (list "-h" title)
198 lpr-page-header-switches))
199 (setq start (point-min) end (point-max))))
200 (apply (or print-region-function 'call-process-region)
201 (nconc (list start end lpr-command
202 nil nil nil)
203 (nconc (and lpr-add-switches
204 (list "-J" name))
205 ;; These belong in pr if we are using that.
206 (and lpr-add-switches lpr-headers-switches
207 (list "-T" title))
208 (and (stringp printer-name)
209 (list (concat "-P" printer-name)))
210 switches)))
211 (if (markerp end)
212 (set-marker end nil))
213 (message "Spooling%s...done" switch-string))))
214
215 ;; This function copies the text between start and end
216 ;; into a new buffer, makes that buffer current.
217 ;; It returns the new range to print from the new current buffer
218 ;; as (START . END).
219
220 (defun print-region-new-buffer (ostart oend)
221 (if (string= (buffer-name) " *spool temp*")
222 (cons ostart oend)
223 (let ((oldbuf (current-buffer)))
224 (set-buffer (get-buffer-create " *spool temp*"))
225 (widen) (erase-buffer)
226 (insert-buffer-substring oldbuf ostart oend)
227 (cons (point-min) (point-max)))))
228
229 (defun printify-region (begin end)
230 "Replace nonprinting characters in region with printable representations.
231 The printable representations use ^ (for ASCII control characters) or hex.
232 The characters tab, linefeed, space, return and formfeed are not affected."
233 (interactive "r")
234 (save-excursion
235 (goto-char begin)
236 (let (c)
237 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
238 (setq c (preceding-char))
239 (delete-backward-char 1)
240 (insert
241 (if (< c ?\ )
242 (format "\\^%c" (+ c ?@))
243 (format "\\%02x" c)))))))
244
245 (provide 'lpr)
246
247 ;;; lpr.el ends here