(print-region-new-buffer): Return new start and end.
[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-switches (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 ;; 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.
49 If nil, we run `lpr-page-header-program' to make page headings
50 and print the result.")
51
52 (defvar print-region-function nil
53 "Function to call to print the region on a printer.
54 See definition of `print-region-1' for calling conventions.")
55
56 (defvar lpr-page-header-program "pr"
57 "*Name of program for adding page headers to a file.")
58
59 (defvar lpr-page-header-switches '("-f")
60 "*List of strings to use as options for `lpr-page-header-program'.")
61
62 ;;;###autoload
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
69 ;;;###autoload
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
76 ;;;###autoload
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
83 ;;;###autoload
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)
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"))
95 (title (concat (buffer-name) " Emacs buffer"))
96 (width tab-width))
97 (save-excursion
98 (message "Spooling...")
99 (if (/= tab-width 8)
100 (let ((new-coords (print-region-new-buffer start end)))
101 (setq start (car new-coords) end (cdr new-coords))
102 (setq tab-width width)
103 (save-excursion
104 (goto-char end)
105 (setq end (point-marker)))
106 (untabify (point-min) (point-max))))
107 (if page-headers
108 (if lpr-headers-switches
109 ;; It is possible to use an lpr option
110 ;; to get page headers.
111 (setq switches (append (if (stringp lpr-headers-switches)
112 (list lpr-headers-switches)
113 lpr-headers-switches)
114 switches))
115 ;; Run a separate program to get page headers.
116 (let ((new-coords (print-region-new-buffer start end)))
117 (setq start (car new-coords) end (cdr new-coords)))
118 (apply 'call-process-region start end lpr-page-header-program
119 t t nil
120 (nconc (and lpr-add-switches
121 (list "-h" title))
122 lpr-page-header-switches))
123 (setq start (point-min) end (point-max))))
124 (apply (or print-region-function 'call-process-region)
125 (nconc (list start end lpr-command
126 nil nil nil)
127 (nconc (and lpr-add-switches
128 (list "-J" name))
129 ;; These belong in pr if we are using that.
130 (and lpr-add-switches lpr-headers-switches
131 (list "-T" title))
132 switches)))
133 (if (markerp end)
134 (set-marker end nil))
135 (message "Spooling...done"))))
136
137 ;; This function copies the text between start and end
138 ;; into a new buffer, makes that buffer current.
139 ;; It returns the new range to print from the new current buffer
140 ;; as (START . END).
141
142 (defun print-region-new-buffer (ostart oend)
143 (if (string= (buffer-name) " *spool temp*")
144 (cons ostart oend)
145 (let ((oldbuf (current-buffer)))
146 (set-buffer (get-buffer-create " *spool temp*"))
147 (widen) (erase-buffer)
148 (insert-buffer-substring oldbuf ostart oend)
149 (cons (point-min) (point-max)))))
150
151 (defun printify-region (begin end)
152 "Turn nonprinting characters (other than TAB, LF, SPC, RET, and FF)
153 in the current buffer into printable representations as control or
154 hexadecimal escapes."
155 (interactive "r")
156 (save-excursion
157 (goto-char begin)
158 (let (c)
159 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
160 (setq c (preceding-char))
161 (delete-backward-char 1)
162 (insert
163 (if (< c ?\ )
164 (format "\\^%c" (+ c ?@))
165 (format "\\%02x" c)))))))
166
167 ;;; lpr.el ends here