(direct-print-region-function): Renamed from
[bpt/emacs.git] / lisp / lpr.el
CommitLineData
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
29;; variables include `lpr-switches' and `lpr-command'.
30
e5167999 31;;; Code:
d6c7e99a 32
c5292bc8
RS
33(defgroup lpr nil
34 "Print Emacs buffer on line printer"
35 :group 'wp)
36
37
6503cec3 38;;;###autoload
c5292bc8 39(defcustom lpr-switches nil
22466c09 40 "*List of strings to pass as extra options for the printer program.
c5292bc8
RS
41See `lpr-command'."
42 :type '(repeat (string :tag "Argument"))
43 :group 'lpr)
3a621cfe 44
c5292bc8 45(defcustom lpr-add-switches (eq system-type 'berkeley-unix)
22466c09
KH
46 "*Non-nil means construct -T and -J options for the printer program.
47These are made assuming that the program is `lpr';
48if you are using some other incompatible printer program,
c5292bc8
RS
49this variable should be nil."
50 :type 'boolean
51 :group 'lpr)
d6c7e99a 52
910476ef 53;;;###autoload
c5292bc8 54(defcustom lpr-command
ffc74f20 55 (if (memq system-type '(usg-unix-v dgux hpux irix))
9f0a6471 56 "lp" "lpr")
c5292bc8
RS
57 "*Name of program for printing a file."
58 :type 'string
59 :group 'lpr)
d6c7e99a 60
7173ec77
RS
61;; Default is nil, because that enables us to use pr -f
62;; which is more reliable than pr with no args, which is what lpr -p does.
c5292bc8 63(defcustom lpr-headers-switches nil
5526cfde 64 "*List of strings of options to request page headings in the printer program.
7173ec77 65If nil, we run `lpr-page-header-program' to make page headings
c5292bc8
RS
66and print the result."
67 :type '(repeat (string :tag "Argument"))
68 :group 'lpr)
3a621cfe 69
c5292bc8 70(defcustom print-region-function nil
d6c7e99a 71 "Function to call to print the region on a printer.
c5292bc8
RS
72See definition of `print-region-1' for calling conventions."
73 :type 'function
74 :group 'lpr)
d6c7e99a 75
c5292bc8
RS
76(defcustom lpr-page-header-program "pr"
77 "*Name of program for adding page headers to a file."
78 :type 'string
79 :group 'lpr)
c820123b 80
bafa1a69
RS
81;; Berkeley systems support -F, and GNU pr supports both -f and -F,
82;; So it looks like -F is a better default.
63c7b220 83(defcustom lpr-page-header-switches '("-F")
5526cfde 84 "*List of strings to use as options for the page-header-generating program.
c5292bc8
RS
85The variable `lpr-page-header-program' specifies the program to use."
86 :type '(repeat string)
87 :group 'lpr)
c820123b 88
7229064d 89;;;###autoload
d6c7e99a
RS
90(defun lpr-buffer ()
91 "Print buffer contents as with Unix command `lpr'.
92`lpr-switches' is a list of extra switches (strings) to pass to lpr."
93 (interactive)
94 (print-region-1 (point-min) (point-max) lpr-switches nil))
95
7229064d 96;;;###autoload
d6c7e99a
RS
97(defun print-buffer ()
98 "Print buffer contents as with Unix command `lpr -p'.
99`lpr-switches' is a list of extra switches (strings) to pass to lpr."
100 (interactive)
101 (print-region-1 (point-min) (point-max) lpr-switches t))
102
7229064d 103;;;###autoload
d6c7e99a
RS
104(defun lpr-region (start end)
105 "Print region contents as with Unix command `lpr'.
106`lpr-switches' is a list of extra switches (strings) to pass to lpr."
107 (interactive "r")
108 (print-region-1 start end lpr-switches nil))
109
7229064d 110;;;###autoload
d6c7e99a
RS
111(defun print-region (start end)
112 "Print region contents as with Unix command `lpr -p'.
113`lpr-switches' is a list of extra switches (strings) to pass to lpr."
114 (interactive "r")
115 (print-region-1 start end lpr-switches t))
116
117(defun print-region-1 (start end switches page-headers)
904d7624
RS
118 ;; On some MIPS system, having a space in the job name
119 ;; crashes the printer demon. But using dashes looks ugly
120 ;; and it seems to annoying to do for that MIPS system.
121 (let ((name (concat (buffer-name) " Emacs buffer"))
a8037455 122 (title (concat (buffer-name) " Emacs buffer"))
0b45d07b
EZ
123 ;; Make pipes use the same coding system as
124 ;; writing the buffer to a file would.
125 (coding-system-for-write
126 (or coding-system-for-write buffer-file-coding-system))
127 (coding-system-for-read
128 (or coding-system-for-read buffer-file-coding-system))
5bbf28a8
RS
129 (width tab-width)
130 switch-string)
d6c7e99a 131 (save-excursion
5bbf28a8
RS
132 (if page-headers
133 (if lpr-headers-switches
134 ;; It is possible to use an lpr option
135 ;; to get page headers.
136 (setq switches (append (if (stringp lpr-headers-switches)
137 (list lpr-headers-switches)
138 lpr-headers-switches)
139 switches))))
140 (setq switch-string
141 (if switches (concat " with options "
142 (mapconcat 'identity switches " "))
143 ""))
144 (message "Spooling%s..." switch-string)
d6c7e99a 145 (if (/= tab-width 8)
8fa64b34
RS
146 (let ((new-coords (print-region-new-buffer start end)))
147 (setq start (car new-coords) end (cdr new-coords))
d6c7e99a 148 (setq tab-width width)
2adf4f61
RS
149 (save-excursion
150 (goto-char end)
151 (setq end (point-marker)))
d6c7e99a
RS
152 (untabify (point-min) (point-max))))
153 (if page-headers
3a621cfe 154 (if lpr-headers-switches
5bbf28a8
RS
155 ;; We handled this above by modifying SWITCHES.
156 nil
7173ec77 157 ;; Run a separate program to get page headers.
8fa64b34
RS
158 (let ((new-coords (print-region-new-buffer start end)))
159 (setq start (car new-coords) end (cdr new-coords)))
a3324470
RS
160 (apply 'call-process-region start end lpr-page-header-program
161 t t nil
887bbf18 162 (nconc (list "-h" title)
a3324470 163 lpr-page-header-switches))
3a621cfe 164 (setq start (point-min) end (point-max))))
d6c7e99a
RS
165 (apply (or print-region-function 'call-process-region)
166 (nconc (list start end lpr-command
167 nil nil nil)
a3324470
RS
168 (nconc (and lpr-add-switches
169 (list "-J" name))
170 ;; These belong in pr if we are using that.
171 (and lpr-add-switches lpr-headers-switches
172 (list "-T" title))
d6c7e99a 173 switches)))
2adf4f61
RS
174 (if (markerp end)
175 (set-marker end nil))
5bbf28a8 176 (message "Spooling%s...done" switch-string))))
d6c7e99a
RS
177
178;; This function copies the text between start and end
8fa64b34
RS
179;; into a new buffer, makes that buffer current.
180;; It returns the new range to print from the new current buffer
181;; as (START . END).
182
3a621cfe 183(defun print-region-new-buffer (ostart oend)
8fa64b34
RS
184 (if (string= (buffer-name) " *spool temp*")
185 (cons ostart oend)
186 (let ((oldbuf (current-buffer)))
187 (set-buffer (get-buffer-create " *spool temp*"))
188 (widen) (erase-buffer)
189 (insert-buffer-substring oldbuf ostart oend)
190 (cons (point-min) (point-max)))))
6594deb0 191
1c2df063 192(defun printify-region (begin end)
8c44b2ad
RS
193 "Replace nonprinting characters in region with printable representations.
194The printable representations use ^ (for ASCII control characters) or hex.
195The characters tab, linefeed, space, return and formfeed are not affected."
1c2df063
ER
196 (interactive "r")
197 (save-excursion
198 (goto-char begin)
199 (let (c)
200 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
201 (setq c (preceding-char))
202 (delete-backward-char 1)
203 (insert
204 (if (< c ?\ )
205 (format "\\^%c" (+ c ?@))
206 (format "\\%02x" c)))))))
207
977b1278
KH
208(provide 'lpr)
209
6594deb0 210;;; lpr.el ends here