(compilation-parse-errors): Skip 2 lines only for Compilation major mode.
[emacs.git] / lisp / lpr.el
blob3b8e2d882558f732167ddf8258da208b8f4856b8
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 (defgroup lpr nil
34 "Print Emacs buffer on line printer"
35 :group 'wp)
38 ;;;###autoload
39 (defcustom lpr-switches nil
40 "*List of strings to pass as extra options for the printer program.
41 See `lpr-command'."
42 :type '(repeat (string :tag "Argument"))
43 :group 'lpr)
45 (defcustom lpr-add-switches (eq system-type 'berkeley-unix)
46 "*Non-nil means construct -T and -J options for the printer program.
47 These are made assuming that the program is `lpr';
48 if you are using some other incompatible printer program,
49 this variable should be nil."
50 :type 'boolean
51 :group 'lpr)
53 ;;;###autoload
54 (defcustom lpr-command
55 (if (memq system-type '(usg-unix-v dgux hpux irix))
56 "lp" "lpr")
57 "*Name of program for printing a file."
58 :type 'string
59 :group 'lpr)
61 ;; Default is nil, because that enables us to use pr -f
62 ;; which is more reliable than pr with no args, which is what lpr -p does.
63 (defcustom lpr-headers-switches nil
64 "*List of strings of options to request page headings in the printer program.
65 If nil, we run `lpr-page-header-program' to make page headings
66 and print the result."
67 :type '(repeat (string :tag "Argument"))
68 :group 'lpr)
70 (defcustom print-region-function nil
71 "Function to call to print the region on a printer.
72 See definition of `print-region-1' for calling conventions."
73 :type 'function
74 :group 'lpr)
76 (defcustom lpr-page-header-program "pr"
77 "*Name of program for adding page headers to a file."
78 :type 'string
79 :group 'lpr)
81 ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
82 ;; So it looks like -F is a better default.
83 (defcustom lpr-page-header-switches '("-F")
84 "*List of strings to use as options for the page-header-generating program.
85 The variable `lpr-page-header-program' specifies the program to use."
86 :type '(repeat string)
87 :group 'lpr)
89 ;;;###autoload
90 (defun lpr-buffer ()
91 "Print buffer contents as with Unix command `lpr'.
92 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
93 (interactive)
94 (print-region-1 (point-min) (point-max) lpr-switches nil))
96 ;;;###autoload
97 (defun print-buffer ()
98 "Print buffer contents as with Unix command `lpr -p'.
99 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
100 (interactive)
101 (print-region-1 (point-min) (point-max) lpr-switches t))
103 ;;;###autoload
104 (defun lpr-region (start end)
105 "Print region contents as with Unix command `lpr'.
106 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
107 (interactive "r")
108 (print-region-1 start end lpr-switches nil))
110 ;;;###autoload
111 (defun print-region (start end)
112 "Print region contents as with Unix command `lpr -p'.
113 `lpr-switches' is a list of extra switches (strings) to pass to lpr."
114 (interactive "r")
115 (print-region-1 start end lpr-switches t))
117 (defun print-region-1 (start end switches page-headers)
118 ;; On some MIPS system, having a space in the job name
119 ;; crashes the printer demon. But using dashes looks ugly
120 ;; and it seems to annoying to do for that MIPS system.
121 (let ((name (concat (buffer-name) " Emacs buffer"))
122 (title (concat (buffer-name) " Emacs buffer"))
123 ;; On MS-DOS systems, make pipes use binary mode if the
124 ;; original file is binary.
125 (binary-process-input buffer-file-type)
126 (binary-process-output buffer-file-type)
127 (width tab-width)
128 switch-string)
129 (save-excursion
130 (if page-headers
131 (if lpr-headers-switches
132 ;; It is possible to use an lpr option
133 ;; to get page headers.
134 (setq switches (append (if (stringp lpr-headers-switches)
135 (list lpr-headers-switches)
136 lpr-headers-switches)
137 switches))))
138 (setq switch-string
139 (if switches (concat " with options "
140 (mapconcat 'identity switches " "))
141 ""))
142 (message "Spooling%s..." switch-string)
143 (if (/= tab-width 8)
144 (let ((new-coords (print-region-new-buffer start end)))
145 (setq start (car new-coords) end (cdr new-coords))
146 (setq tab-width width)
147 (save-excursion
148 (goto-char end)
149 (setq end (point-marker)))
150 (untabify (point-min) (point-max))))
151 (if page-headers
152 (if lpr-headers-switches
153 ;; We handled this above by modifying SWITCHES.
155 ;; Run a separate program to get page headers.
156 (let ((new-coords (print-region-new-buffer start end)))
157 (setq start (car new-coords) end (cdr new-coords)))
158 (apply 'call-process-region start end lpr-page-header-program
159 t t nil
160 (nconc (and lpr-add-switches
161 (list "-h" title))
162 lpr-page-header-switches))
163 (setq start (point-min) end (point-max))))
164 (apply (or print-region-function 'call-process-region)
165 (nconc (list start end lpr-command
166 nil nil nil)
167 (nconc (and lpr-add-switches
168 (list "-J" name))
169 ;; These belong in pr if we are using that.
170 (and lpr-add-switches lpr-headers-switches
171 (list "-T" title))
172 switches)))
173 (if (markerp end)
174 (set-marker end nil))
175 (message "Spooling%s...done" switch-string))))
177 ;; This function copies the text between start and end
178 ;; into a new buffer, makes that buffer current.
179 ;; It returns the new range to print from the new current buffer
180 ;; as (START . END).
182 (defun print-region-new-buffer (ostart oend)
183 (if (string= (buffer-name) " *spool temp*")
184 (cons ostart oend)
185 (let ((oldbuf (current-buffer)))
186 (set-buffer (get-buffer-create " *spool temp*"))
187 (widen) (erase-buffer)
188 (insert-buffer-substring oldbuf ostart oend)
189 (cons (point-min) (point-max)))))
191 (defun printify-region (begin end)
192 "Replace nonprinting characters in region with printable representations.
193 The printable representations use ^ (for ASCII control characters) or hex.
194 The characters tab, linefeed, space, return and formfeed are not affected."
195 (interactive "r")
196 (save-excursion
197 (goto-char begin)
198 (let (c)
199 (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" end t)
200 (setq c (preceding-char))
201 (delete-backward-char 1)
202 (insert
203 (if (< c ?\ )
204 (format "\\^%c" (+ c ?@))
205 (format "\\%02x" c)))))))
207 (provide 'lpr)
209 ;;; lpr.el ends here