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