]> code.delx.au - gnu-emacs/blob - lisp/lpr.el
Customize.
[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 `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
38 ;;;###autoload
39 (defcustom lpr-switches nil
40 "*List of strings to pass as extra options for the printer program.
41 See `lpr-command'."
42 :type '(repeat (string :tag "Argument"))
43 :group 'lpr)
44
45 (defcustom lpr-add-switches (eq system-type 'berkeley-unix)
46 "*Non-nil means construct -T and -J options for the printer program.
47 These are made assuming that the program is `lpr';
48 if you are using some other incompatible printer program,
49 this variable should be nil."
50 :type 'boolean
51 :group 'lpr)
52
53 ;;;###autoload
54 (defcustom lpr-command
55 (if (memq system-type '(usg-unix-v dgux hpux irix))
56 "lp" "lpr")
57 "*Name of program for printing a file."
58 :type 'string
59 :group 'lpr)
60
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.
63 (defcustom lpr-headers-switches nil
64 "*List of strings of options to request page headings in the printer program.
65 If nil, we run `lpr-page-header-program' to make page headings
66 and print the result."
67 :type '(repeat (string :tag "Argument"))
68 :group 'lpr)
69
70 (defcustom print-region-function nil
71 "Function to call to print the region on a printer.
72 See definition of `print-region-1' for calling conventions."
73 :type 'function
74 :group 'lpr)
75
76 (defcustom lpr-page-header-program "pr"
77 "*Name of program for adding page headers to a file."
78 :type 'string
79 :group 'lpr)
80
81 (defcustom lpr-page-header-switches '("-f")
82 "*List of strings to use as options for the page-header-generating program.
83 The variable `lpr-page-header-program' specifies the program to use."
84 :type '(repeat string)
85 :group 'lpr)
86
87 ;;;###autoload
88 (defun lpr-buffer ()
89 "Print buffer contents as with Unix command `lpr'.
90 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
91 (interactive)
92 (print-region-1 (point-min) (point-max) lpr-switches nil))
93
94 ;;;###autoload
95 (defun print-buffer ()
96 "Print buffer contents as with Unix command `lpr -p'.
97 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
98 (interactive)
99 (print-region-1 (point-min) (point-max) lpr-switches t))
100
101 ;;;###autoload
102 (defun lpr-region (start end)
103 "Print region contents as with Unix command `lpr'.
104 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
105 (interactive "r")
106 (print-region-1 start end lpr-switches nil))
107
108 ;;;###autoload
109 (defun print-region (start end)
110 "Print region contents as with Unix command `lpr -p'.
111 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
112 (interactive "r")
113 (print-region-1 start end lpr-switches t))
114
115 (defun print-region-1 (start end switches page-headers)
116 ;; On some MIPS system, having a space in the job name
117 ;; crashes the printer demon. But using dashes looks ugly
118 ;; and it seems to annoying to do for that MIPS system.
119 (let ((name (concat (buffer-name) " Emacs buffer"))
120 (title (concat (buffer-name) " Emacs buffer"))
121 ;; On MS-DOS systems, make pipes use binary mode if the
122 ;; original file is binary.
123 (binary-process-input buffer-file-type)
124 (binary-process-output buffer-file-type)
125 (width tab-width)
126 switch-string)
127 (save-excursion
128 (if page-headers
129 (if lpr-headers-switches
130 ;; It is possible to use an lpr option
131 ;; to get page headers.
132 (setq switches (append (if (stringp lpr-headers-switches)
133 (list lpr-headers-switches)
134 lpr-headers-switches)
135 switches))))
136 (setq switch-string
137 (if switches (concat " with options "
138 (mapconcat 'identity switches " "))
139 ""))
140 (message "Spooling%s..." switch-string)
141 (if (/= tab-width 8)
142 (let ((new-coords (print-region-new-buffer start end)))
143 (setq start (car new-coords) end (cdr new-coords))
144 (setq tab-width width)
145 (save-excursion
146 (goto-char end)
147 (setq end (point-marker)))
148 (untabify (point-min) (point-max))))
149 (if page-headers
150 (if lpr-headers-switches
151 ;; We handled this above by modifying SWITCHES.
152 nil
153 ;; Run a separate program to get page headers.
154 (let ((new-coords (print-region-new-buffer start end)))
155 (setq start (car new-coords) end (cdr new-coords)))
156 (apply 'call-process-region start end lpr-page-header-program
157 t t nil
158 (nconc (and lpr-add-switches
159 (list "-h" title))
160 lpr-page-header-switches))
161 (setq start (point-min) end (point-max))))
162 (apply (or print-region-function 'call-process-region)
163 (nconc (list start end lpr-command
164 nil nil nil)
165 (nconc (and lpr-add-switches
166 (list "-J" name))
167 ;; These belong in pr if we are using that.
168 (and lpr-add-switches lpr-headers-switches
169 (list "-T" title))
170 switches)))
171 (if (markerp end)
172 (set-marker end nil))
173 (message "Spooling%s...done" switch-string))))
174
175 ;; This function copies the text between start and end
176 ;; into a new buffer, makes that buffer current.
177 ;; It returns the new range to print from the new current buffer
178 ;; as (START . END).
179
180 (defun print-region-new-buffer (ostart oend)
181 (if (string= (buffer-name) " *spool temp*")
182 (cons ostart oend)
183 (let ((oldbuf (current-buffer)))
184 (set-buffer (get-buffer-create " *spool temp*"))
185 (widen) (erase-buffer)
186 (insert-buffer-substring oldbuf ostart oend)
187 (cons (point-min) (point-max)))))
188
189 (defun printify-region (begin end)
190 "Replace nonprinting characters in region with printable representations.
191 The printable representations use ^ (for ASCII control characters) or hex.
192 The characters tab, linefeed, space, return and formfeed are not affected."
193 (interactive "r")
194 (save-excursion
195 (goto-char begin)
196 (let (c)
197 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
198 (setq c (preceding-char))
199 (delete-backward-char 1)
200 (insert
201 (if (< c ?\ )
202 (format "\\^%c" (+ c ?@))
203 (format "\\%02x" c)))))))
204
205 (provide 'lpr)
206
207 ;;; lpr.el ends here