1 ;;; lpr.el --- print Emacs buffer on line printer.
3 ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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'.
33 (defvar lpr-switches nil
34 "*List of strings to pass as extra options for the printer program.
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.")
45 (if (memq system-type
'(usg-unix-v dgux hpux irix
))
47 "*Name of program for printing a file.")
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 to use as options for `lpr' to request page headings.
53 If nil, we run `lpr-page-header-program' to make page headings
54 and print the result.")
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.")
60 (defvar lpr-page-header-program
"pr"
61 "*Name of program for adding page headers to a file.")
63 (defvar lpr-page-header-switches
'("-f")
64 "*List of strings to use as options for `lpr-page-header-program'.")
68 "Print buffer contents as with Unix command `lpr'.
69 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
71 (print-region-1 (point-min) (point-max) lpr-switches nil
))
74 (defun print-buffer ()
75 "Print buffer contents as with Unix command `lpr -p'.
76 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
78 (print-region-1 (point-min) (point-max) lpr-switches t
))
81 (defun lpr-region (start end
)
82 "Print region contents as with Unix command `lpr'.
83 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
85 (print-region-1 start end lpr-switches nil
))
88 (defun print-region (start end
)
89 "Print region contents as with Unix command `lpr -p'.
90 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
92 (print-region-1 start end lpr-switches t
))
94 (defun print-region-1 (start end switches page-headers
)
95 ;; On some MIPS system, having a space in the job name
96 ;; crashes the printer demon. But using dashes looks ugly
97 ;; and it seems to annoying to do for that MIPS system.
98 (let ((name (concat (buffer-name) " Emacs buffer"))
99 (title (concat (buffer-name) " Emacs buffer"))
104 (if lpr-headers-switches
105 ;; It is possible to use an lpr option
106 ;; to get page headers.
107 (setq switches
(append (if (stringp lpr-headers-switches
)
108 (list lpr-headers-switches
)
109 lpr-headers-switches
)
112 (if switches
(concat " with options "
113 (mapconcat 'identity switches
" "))
115 (message "Spooling%s..." switch-string
)
117 (let ((new-coords (print-region-new-buffer start end
)))
118 (setq start
(car new-coords
) end
(cdr new-coords
))
119 (setq tab-width width
)
122 (setq end
(point-marker)))
123 (untabify (point-min) (point-max))))
125 (if lpr-headers-switches
126 ;; We handled this above by modifying SWITCHES.
128 ;; Run a separate program to get page headers.
129 (let ((new-coords (print-region-new-buffer start end
)))
130 (setq start
(car new-coords
) end
(cdr new-coords
)))
131 (apply 'call-process-region start end lpr-page-header-program
133 (nconc (and lpr-add-switches
135 lpr-page-header-switches
))
136 (setq start
(point-min) end
(point-max))))
137 (apply (or print-region-function
'call-process-region
)
138 (nconc (list start end lpr-command
140 (nconc (and lpr-add-switches
142 ;; These belong in pr if we are using that.
143 (and lpr-add-switches lpr-headers-switches
147 (set-marker end nil
))
148 (message "Spooling%s...done" switch-string
))))
150 ;; This function copies the text between start and end
151 ;; into a new buffer, makes that buffer current.
152 ;; It returns the new range to print from the new current buffer
155 (defun print-region-new-buffer (ostart oend
)
156 (if (string= (buffer-name) " *spool temp*")
158 (let ((oldbuf (current-buffer)))
159 (set-buffer (get-buffer-create " *spool temp*"))
160 (widen) (erase-buffer)
161 (insert-buffer-substring oldbuf ostart oend
)
162 (cons (point-min) (point-max)))))
164 (defun printify-region (begin end
)
165 "Turn nonprinting characters (other than TAB, LF, SPC, RET, and FF)
166 in the current buffer into printable representations as control or
167 hexadecimal escapes."
172 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t
)
173 (setq c
(preceding-char))
174 (delete-backward-char 1)
177 (format "\\^%c" (+ c ?
@))
178 (format "\\%02x" c
)))))))