(print-region-1): Use them instead of just pr.
[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 switch args to `lpr' when it is invoked.")
35
36 (defvar lpr-add-options (eq system-type 'berkeley-unix)
37 "*Non-nil means construct -T and -J options for the `lpr'.")
38
39 ;;;###autoload
40 (defvar lpr-command
41 (if (memq system-type '(usg-unix-v dgux hpux irix))
42 "lp" "lpr")
43 "*Name of program for printing a file.")
44
45 (defvar lpr-headers-switches
46 (if (memq system-type '(usg-unix-v dgux hpux irix)) nil "-p")
47 "*List of strings to use as options for `lpr' to request page headings.")
48
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.")
52
53 (defvar lpr-page-header-program "pr"
54 "*Name of program for adding page headers to a file.")
55
56 (defvar lpr-page-header-switches nil
57 "*List of strings to use as options for `lpr-page-header-program'.")
58
59 ;;;###autoload
60 (defun lpr-buffer ()
61 "Print buffer contents as with Unix command `lpr'.
62 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
63 (interactive)
64 (print-region-1 (point-min) (point-max) lpr-switches nil))
65
66 ;;;###autoload
67 (defun print-buffer ()
68 "Print buffer contents as with Unix command `lpr -p'.
69 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
70 (interactive)
71 (print-region-1 (point-min) (point-max) lpr-switches t))
72
73 ;;;###autoload
74 (defun lpr-region (start end)
75 "Print region contents as with Unix command `lpr'.
76 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
77 (interactive "r")
78 (print-region-1 start end lpr-switches nil))
79
80 ;;;###autoload
81 (defun print-region (start end)
82 "Print region contents as with Unix command `lpr -p'.
83 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
84 (interactive "r")
85 (print-region-1 start end lpr-switches t))
86
87 (defun print-region-1 (start end switches page-headers)
88 ;; On some MIPS system, having a space in the job name
89 ;; crashes the printer demon. But using dashes looks ugly
90 ;; and it seems to annoying to do for that MIPS system.
91 (let ((name (concat (buffer-name) " Emacs buffer"))
92 (title (concat (buffer-name) " Emacs buffer"))
93 (width tab-width))
94 (save-excursion
95 (message "Spooling...")
96 (if (/= tab-width 8)
97 (progn
98 (print-region-new-buffer start end)
99 (setq tab-width width)
100 (save-excursion
101 (goto-char end)
102 (setq end (point-marker)))
103 (untabify (point-min) (point-max))))
104 (if page-headers
105 (if lpr-headers-switches
106 ;; On BSD, use an option to get page headers.
107 (setq switches (append (if (stringp lpr-headers-switches)
108 (list lpr-headers-switches)
109 lpr-headers-switches)
110 switches))
111 (print-region-new-buffer start end)
112 (call-process-region start end lpr-page-header-program
113 t t lpr-page-header-options)
114 (setq start (point-min) end (point-max))))
115 (apply (or print-region-function 'call-process-region)
116 (nconc (list start end lpr-command
117 nil nil nil)
118 (nconc (and lpr-add-options
119 (list "-J" name "-T" title))
120 switches)))
121 (if (markerp end)
122 (set-marker end nil))
123 (message "Spooling...done"))))
124
125 ;; This function copies the text between start and end
126 ;; into a new buffer, makes that buffer current,
127 ;; and sets start and end to the buffer bounds.
128 ;; start and end are used free.
129 (defun print-region-new-buffer (ostart oend)
130 (or (string= (buffer-name) " *spool temp*")
131 (let ((oldbuf (current-buffer)))
132 (set-buffer (get-buffer-create " *spool temp*"))
133 (widen) (erase-buffer)
134 (insert-buffer-substring oldbuf ostart oend)
135 (setq start (point-min) end (point-max)))))
136
137 (defun printify-region (begin end)
138 "Turn nonprinting characters (other than TAB, LF, SPC, RET, and FF)
139 in the current buffer into printable representations as control or
140 hexadecimal escapes."
141 (interactive "r")
142 (save-excursion
143 (goto-char begin)
144 (let (c)
145 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
146 (setq c (preceding-char))
147 (delete-backward-char 1)
148 (insert
149 (if (< c ?\ )
150 (format "\\^%c" (+ c ?@))
151 (format "\\%02x" c)))))))
152
153 ;;; lpr.el ends here