(Fcall_interactively): For `N' and `n',
[emacs.git] / lisp / lpr.el
blob349561936427eee449bea88b473c86e6bca633b9
1 ;;; lpr.el --- print Emacs buffer on line printer.
3 ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: unix
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)
13 ;; any later version.
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; Commands to send the region or a buffer your printer. Entry points
28 ;; are `lpr-buffer', `print-buffer', lpr-region', or `print-region'; option
29 ;; variables include `lpr-switches' and `lpr-command'.
31 ;;; Code:
33 ;;;###autoload
34 (defvar lpr-switches nil
35 "*List of strings to pass as extra options for the printer program.
36 See `lpr-command'.")
38 (defvar lpr-add-switches (eq system-type 'berkeley-unix)
39 "*Non-nil means construct -T and -J options for the printer program.
40 These are made assuming that the program is `lpr';
41 if you are using some other incompatible printer program,
42 this variable should be nil.")
44 ;;;###autoload
45 (defvar lpr-command
46 (if (memq system-type '(usg-unix-v dgux hpux irix))
47 "lp" "lpr")
48 "*Name of program for printing a file.")
50 ;; Default is nil, because that enables us to use pr -f
51 ;; which is more reliable than pr with no args, which is what lpr -p does.
52 (defvar lpr-headers-switches nil
53 "*List of strings of options to request page headings in the printer program.
54 If nil, we run `lpr-page-header-program' to make page headings
55 and print the result.")
57 (defvar print-region-function nil
58 "Function to call to print the region on a printer.
59 See definition of `print-region-1' for calling conventions.")
61 (defvar lpr-page-header-program "pr"
62 "*Name of program for adding page headers to a file.")
64 (defvar lpr-page-header-switches '("-f")
65 "*List of strings to use as options for the page-header-generating program.
66 The variable `lpr-page-header-program' specifies the program to use.")
68 ;;;###autoload
69 (defun lpr-buffer ()
70 "Print buffer contents as with Unix command `lpr'.
71 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
72 (interactive)
73 (print-region-1 (point-min) (point-max) lpr-switches nil))
75 ;;;###autoload
76 (defun print-buffer ()
77 "Print buffer contents as with Unix command `lpr -p'.
78 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
79 (interactive)
80 (print-region-1 (point-min) (point-max) lpr-switches t))
82 ;;;###autoload
83 (defun lpr-region (start end)
84 "Print region contents as with Unix command `lpr'.
85 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
86 (interactive "r")
87 (print-region-1 start end lpr-switches nil))
89 ;;;###autoload
90 (defun print-region (start end)
91 "Print region contents as with Unix command `lpr -p'.
92 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
93 (interactive "r")
94 (print-region-1 start end lpr-switches t))
96 (defun print-region-1 (start end switches page-headers)
97 ;; On some MIPS system, having a space in the job name
98 ;; crashes the printer demon. But using dashes looks ugly
99 ;; and it seems to annoying to do for that MIPS system.
100 (let ((name (concat (buffer-name) " Emacs buffer"))
101 (title (concat (buffer-name) " Emacs buffer"))
102 ;; On MS-DOS systems, make pipes use binary mode if the
103 ;; original file is binary.
104 (binary-process-input buffer-file-type)
105 (binary-process-output buffer-file-type)
106 (width tab-width)
107 switch-string)
108 (save-excursion
109 (if page-headers
110 (if lpr-headers-switches
111 ;; It is possible to use an lpr option
112 ;; to get page headers.
113 (setq switches (append (if (stringp lpr-headers-switches)
114 (list lpr-headers-switches)
115 lpr-headers-switches)
116 switches))))
117 (setq switch-string
118 (if switches (concat " with options "
119 (mapconcat 'identity switches " "))
120 ""))
121 (message "Spooling%s..." switch-string)
122 (if (/= tab-width 8)
123 (let ((new-coords (print-region-new-buffer start end)))
124 (setq start (car new-coords) end (cdr new-coords))
125 (setq tab-width width)
126 (save-excursion
127 (goto-char end)
128 (setq end (point-marker)))
129 (untabify (point-min) (point-max))))
130 (if page-headers
131 (if lpr-headers-switches
132 ;; We handled this above by modifying SWITCHES.
134 ;; Run a separate program to get page headers.
135 (let ((new-coords (print-region-new-buffer start end)))
136 (setq start (car new-coords) end (cdr new-coords)))
137 (apply 'call-process-region start end lpr-page-header-program
138 t t nil
139 (nconc (and lpr-add-switches
140 (list "-h" title))
141 lpr-page-header-switches))
142 (setq start (point-min) end (point-max))))
143 (apply (or print-region-function 'call-process-region)
144 (nconc (list start end lpr-command
145 nil nil nil)
146 (nconc (and lpr-add-switches
147 (list "-J" name))
148 ;; These belong in pr if we are using that.
149 (and lpr-add-switches lpr-headers-switches
150 (list "-T" title))
151 switches)))
152 (if (markerp end)
153 (set-marker end nil))
154 (message "Spooling%s...done" switch-string))))
156 ;; This function copies the text between start and end
157 ;; into a new buffer, makes that buffer current.
158 ;; It returns the new range to print from the new current buffer
159 ;; as (START . END).
161 (defun print-region-new-buffer (ostart oend)
162 (if (string= (buffer-name) " *spool temp*")
163 (cons ostart oend)
164 (let ((oldbuf (current-buffer)))
165 (set-buffer (get-buffer-create " *spool temp*"))
166 (widen) (erase-buffer)
167 (insert-buffer-substring oldbuf ostart oend)
168 (cons (point-min) (point-max)))))
170 (defun printify-region (begin end)
171 "Turn nonprinting characters (other than TAB, LF, SPC, RET, and FF)
172 in the current buffer into printable representations as control or
173 hexadecimal escapes."
174 (interactive "r")
175 (save-excursion
176 (goto-char begin)
177 (let (c)
178 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
179 (setq c (preceding-char))
180 (delete-backward-char 1)
181 (insert
182 (if (< c ?\ )
183 (format "\\^%c" (+ c ?@))
184 (format "\\%02x" c)))))))
186 (provide 'lpr)
188 ;;; lpr.el ends here