1 ;;; lpr.el --- print Emacs buffer on line printer.
3 ;; Copyright (C) 1985, 1988, 1992 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 switch args to `lpr' when it is invoked.")
36 (defvar lpr-add-options
(eq system-type
'berkeley-unix
)
37 "*Non-nil means construct -T and -J options for the `lpr'.")
41 (if (memq system-type
'(usg-unix-v dgux hpux irix
))
43 "*Shell command for printing a file")
45 (defvar lpr-headers-switches
46 (if (memq system-type
'(usg-unix-v hpux
)) nil
"-p")
47 "*List of strings to use as options for `lpr' to request page headings.")
49 (defvar print-region-function nil
50 "Function to call to print the region on a printer.
51 See definition of `print-region-1' for calling conventions.")
55 "Print buffer contents as with Unix command `lpr'.
56 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
58 (print-region-1 (point-min) (point-max) lpr-switches nil
))
61 (defun print-buffer ()
62 "Print buffer contents as with Unix command `lpr -p'.
63 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
65 (print-region-1 (point-min) (point-max) lpr-switches t
))
68 (defun lpr-region (start end
)
69 "Print region contents as with Unix command `lpr'.
70 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
72 (print-region-1 start end lpr-switches nil
))
75 (defun print-region (start end
)
76 "Print region 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 start end lpr-switches t
))
81 (defun print-region-1 (start end switches page-headers
)
82 (let ((name (concat (buffer-name) " Emacs buffer"))
85 (message "Spooling...")
88 (print-region-new-buffer start end
)
89 (setq tab-width width
)
92 (setq end
(point-marker)))
93 (untabify (point-min) (point-max))))
95 (if lpr-headers-switches
96 ;; On BSD, use an option to get page headers.
97 (setq switches
(append (if (stringp lpr-headers-switches
)
98 (list lpr-headers-switches
)
101 (print-region-new-buffer start end
)
102 (call-process-region start end
"pr" t t nil
)
103 (setq start
(point-min) end
(point-max))))
104 (apply (or print-region-function
'call-process-region
)
105 (nconc (list start end lpr-command
107 (nconc (and lpr-add-options
108 (list "-J" name
"-T" name
))
111 (set-marker end nil
))
112 (message "Spooling...done"))))
114 ;; This function copies the text between start and end
115 ;; into a new buffer, makes that buffer current,
116 ;; and sets start and end to the buffer bounds.
117 ;; start and end are used free.
118 (defun print-region-new-buffer (ostart oend
)
119 (or (string= (buffer-name) " *spool temp*")
120 (let ((oldbuf (current-buffer)))
121 (set-buffer (get-buffer-create " *spool temp*"))
122 (widen) (erase-buffer)
123 (insert-buffer-substring oldbuf ostart oend
)
124 (setq start
(point-min) end
(point-max)))))
126 (defun printify-region (begin end
)
127 "Turn nonprinting characters (other than TAB, LF, SPC, RET, and FF)
128 in the current buffer into printable representations as control or
129 hexadecimal escapes."
134 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t
)
135 (setq c
(preceding-char))
136 (delete-backward-char 1)
139 (format "\\^%c" (+ c ?
@))
140 (format "\\%02x" c
)))))))