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 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.")
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 the page-header-generating program.
65 The variable `lpr-page-header-program' specifies the program to use.")
69 "Print buffer contents as with Unix command `lpr'.
70 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
72 (print-region-1 (point-min) (point-max) lpr-switches nil
))
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."
79 (print-region-1 (point-min) (point-max) lpr-switches t
))
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."
86 (print-region-1 start end lpr-switches nil
))
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."
93 (print-region-1 start end lpr-switches t
))
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"))
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
)
113 (if switches
(concat " with options "
114 (mapconcat 'identity switches
" "))
116 (message "Spooling%s..." switch-string
)
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
)
123 (setq end
(point-marker)))
124 (untabify (point-min) (point-max))))
126 (if lpr-headers-switches
127 ;; We handled this above by modifying SWITCHES.
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
134 (nconc (and lpr-add-switches
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
141 (nconc (and lpr-add-switches
143 ;; These belong in pr if we are using that.
144 (and lpr-add-switches lpr-headers-switches
148 (set-marker end nil
))
149 (message "Spooling%s...done" switch-string
))))
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
156 (defun print-region-new-buffer (ostart oend
)
157 (if (string= (buffer-name) " *spool temp*")
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)))))
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."
173 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t
)
174 (setq c
(preceding-char))
175 (delete-backward-char 1)
178 (format "\\^%c" (+ c ?
@))
179 (format "\\%02x" c
)))))))