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