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