Update FSF's address.
[bpt/emacs.git] / 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 ;;;###autoload
34 (defvar lpr-switches nil
35 "*List of strings to pass as extra options for the printer program.
36 See `lpr-command'.")
37
38 (defvar lpr-add-switches (eq system-type 'berkeley-unix)
39 "*Non-nil means construct -T and -J options for the printer program.
40 These are made assuming that the program is `lpr';
41 if you are using some other incompatible printer program,
42 this variable should be nil.")
43
44 ;;;###autoload
45 (defvar lpr-command
46 (if (memq system-type '(usg-unix-v dgux hpux irix))
47 "lp" "lpr")
48 "*Name of program for printing a file.")
49
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
53 "*List of strings of options to request page headings in the printer program.
54 If nil, we run `lpr-page-header-program' to make page headings
55 and print the result.")
56
57 (defvar print-region-function nil
58 "Function to call to print the region on a printer.
59 See definition of `print-region-1' for calling conventions.")
60
61 (defvar lpr-page-header-program "pr"
62 "*Name of program for adding page headers to a file.")
63
64 (defvar lpr-page-header-switches '("-f")
65 "*List of strings to use as options for the page-header-generating program.
66 The variable `lpr-page-header-program' specifies the program to use.")
67
68 ;;;###autoload
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
75 ;;;###autoload
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
82 ;;;###autoload
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
89 ;;;###autoload
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)
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"))
101 (title (concat (buffer-name) " Emacs buffer"))
102 (width tab-width)
103 switch-string)
104 (save-excursion
105 (if page-headers
106 (if lpr-headers-switches
107 ;; It is possible to use an lpr option
108 ;; to get page headers.
109 (setq switches (append (if (stringp lpr-headers-switches)
110 (list lpr-headers-switches)
111 lpr-headers-switches)
112 switches))))
113 (setq switch-string
114 (if switches (concat " with options "
115 (mapconcat 'identity switches " "))
116 ""))
117 (message "Spooling%s..." switch-string)
118 (if (/= tab-width 8)
119 (let ((new-coords (print-region-new-buffer start end)))
120 (setq start (car new-coords) end (cdr new-coords))
121 (setq tab-width width)
122 (save-excursion
123 (goto-char end)
124 (setq end (point-marker)))
125 (untabify (point-min) (point-max))))
126 (if page-headers
127 (if lpr-headers-switches
128 ;; We handled this above by modifying SWITCHES.
129 nil
130 ;; Run a separate program to get page headers.
131 (let ((new-coords (print-region-new-buffer start end)))
132 (setq start (car new-coords) end (cdr new-coords)))
133 (apply 'call-process-region start end lpr-page-header-program
134 t t nil
135 (nconc (and lpr-add-switches
136 (list "-h" title))
137 lpr-page-header-switches))
138 (setq start (point-min) end (point-max))))
139 (apply (or print-region-function 'call-process-region)
140 (nconc (list start end lpr-command
141 nil nil nil)
142 (nconc (and lpr-add-switches
143 (list "-J" name))
144 ;; These belong in pr if we are using that.
145 (and lpr-add-switches lpr-headers-switches
146 (list "-T" title))
147 switches)))
148 (if (markerp end)
149 (set-marker end nil))
150 (message "Spooling%s...done" switch-string))))
151
152 ;; This function copies the text between start and end
153 ;; into a new buffer, makes that buffer current.
154 ;; It returns the new range to print from the new current buffer
155 ;; as (START . END).
156
157 (defun print-region-new-buffer (ostart oend)
158 (if (string= (buffer-name) " *spool temp*")
159 (cons ostart oend)
160 (let ((oldbuf (current-buffer)))
161 (set-buffer (get-buffer-create " *spool temp*"))
162 (widen) (erase-buffer)
163 (insert-buffer-substring oldbuf ostart oend)
164 (cons (point-min) (point-max)))))
165
166 (defun printify-region (begin end)
167 "Turn nonprinting characters (other than TAB, LF, SPC, RET, and FF)
168 in the current buffer into printable representations as control or
169 hexadecimal escapes."
170 (interactive "r")
171 (save-excursion
172 (goto-char begin)
173 (let (c)
174 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
175 (setq c (preceding-char))
176 (delete-backward-char 1)
177 (insert
178 (if (< c ?\ )
179 (format "\\^%c" (+ c ?@))
180 (format "\\%02x" c)))))))
181
182 (provide 'lpr)
183
184 ;;; lpr.el ends here