Update FSF's address.
[bpt/emacs.git] / lisp / ps-print.el
1 ;;; ps-print.el --- Jim's Pretty-Good PostScript Generator for Emacs 19.
2
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Jim Thompson <thompson@wg2.waii.com>
6 ;; Keywords: print, PostScript
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;; LCD Archive Entry:
26 ;; ps-print|James C. Thompson|thompson@wg2.waii.com|
27 ;; Jim's Pretty-Good PostScript Generator for Emacs 19 (ps-print)|
28 ;; 26-Feb-1994|2.8|~/packages/ps-print.el|
29
30 ;; Baseline-version: 2.8. (Jim's last change version -- this
31 ;; file may have been edited as part of Emacs without changes to the
32 ;; version number. When reporting bugs, please also report the
33 ;; version of Emacs, if any, that ps-print was distributed with.)
34
35 ;;; Commentary:
36
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;;
39 ;; About ps-print
40 ;; --------------
41 ;; This package provides printing of Emacs buffers on PostScript
42 ;; printers; the buffer's bold and italic text attributes are
43 ;; preserved in the printer output. Ps-print is intended for use with
44 ;; Emacs 19 or Lucid Emacs, together with a fontifying package such as
45 ;; font-lock or hilit.
46 ;;
47 ;; Using ps-print
48 ;; --------------
49 ;;
50 ;; The Commands
51 ;;
52 ;; Ps-print provides eight commands for generating PostScript images
53 ;; of Emacs buffers:
54 ;;
55 ;; ps-print-buffer
56 ;; ps-print-buffer-with-faces
57 ;; ps-print-region
58 ;; ps-print-region-with-faces
59 ;; ps-spool-buffer
60 ;; ps-spool-buffer-with-faces
61 ;; ps-spool-region
62 ;; ps-spool-region-with-faces
63 ;;
64 ;; These commands all perform essentially the same function: they
65 ;; generate PostScript images suitable for printing on a PostScript
66 ;; printer or displaying with GhostScript. These commands are
67 ;; collectively referred to as "ps-print- commands".
68 ;;
69 ;; The word "print" or "spool" in the command name determines when the
70 ;; PostScript image is sent to the printer:
71 ;;
72 ;; print - The PostScript image is immediately sent to the
73 ;; printer;
74 ;;
75 ;; spool - The PostScript image is saved temporarily in an
76 ;; Emacs buffer. Many images may be spooled locally
77 ;; before printing them. To send the spooled images
78 ;; to the printer, use the command ps-despool.
79 ;;
80 ;; The spooling mechanism was designed for printing lots of small
81 ;; files (mail messages or netnews articles) to save paper that would
82 ;; otherwise be wasted on banner pages, and to make it easier to find
83 ;; your output at the printer (it's easier to pick up one 50-page
84 ;; printout than to find 50 single-page printouts).
85 ;;
86 ;; Ps-print has a hook in the kill-emacs-hooks so that you won't
87 ;; accidentally quit from Emacs while you have unprinted PostScript
88 ;; waiting in the spool buffer. If you do attempt to exit with
89 ;; spooled PostScript, you'll be asked if you want to print it, and if
90 ;; you decline, you'll be asked to confirm the exit; this is modeled
91 ;; on the confirmation that Emacs uses for modified buffers.
92 ;;
93 ;; The word "buffer" or "region" in the command name determines how
94 ;; much of the buffer is printed:
95 ;;
96 ;; buffer - Print the entire buffer.
97 ;;
98 ;; region - Print just the current region.
99 ;;
100 ;; The -with-faces suffix on the command name means that the command
101 ;; will include font, color, and underline information in the
102 ;; PostScript image, so the printed image can look as pretty as the
103 ;; buffer. The ps-print- commands without the -with-faces suffix
104 ;; don't include font, color, or underline information; images printed
105 ;; with these commands aren't as pretty, but are faster to generate.
106 ;;
107 ;; Two ps-print- command examples:
108 ;;
109 ;; ps-print-buffer - print the entire buffer,
110 ;; without font, color, or
111 ;; underline information, and
112 ;; send it immediately to the
113 ;; printer.
114 ;;
115 ;; ps-spool-region-with-faces - print just the current region;
116 ;; include font, color, and
117 ;; underline information, and
118 ;; spool the image in Emacs to
119 ;; send to the printer later.
120 ;;
121 ;;
122 ;; Invoking Ps-Print
123 ;;
124 ;; To print your buffer, type
125 ;;
126 ;; M-x ps-print-buffer
127 ;;
128 ;; or substitute one of the other seven ps-print- commands. The
129 ;; command will generate the PostScript image and print or spool it as
130 ;; specified. By giving the command a prefix argument
131 ;;
132 ;; C-u M-x ps-print-buffer
133 ;;
134 ;; it will save the PostScript image to a file instead of sending it
135 ;; to the printer; you will be prompted for the name of the file to
136 ;; save the image to. The prefix argument is ignored by the commands
137 ;; that spool their images, but you may save the spooled images to a
138 ;; file by giving a prefix argument to ps-despool:
139 ;;
140 ;; C-u M-x ps-despool
141 ;;
142 ;; When invoked this way, ps-despool will prompt you for the name of
143 ;; the file to save to.
144 ;;
145 ;; Any of the ps-print- commands can be bound to keys; I recommend
146 ;; binding ps-spool-buffer-with-faces, ps-spool-region-with-faces, and
147 ;; ps-despool. Here are the bindings I use on my Sun 4 keyboard:
148 ;;
149 ;; (global-set-key 'f22 'ps-spool-buffer-with-faces) ;f22 is prsc
150 ;; (global-set-key '(shift f22) 'ps-spool-region-with-faces)
151 ;; (global-set-key '(control f22) 'ps-despool)
152 ;;
153 ;;
154 ;; The Printer Interface
155 ;;
156 ;; The variables ps-lpr-command and ps-lpr-switches determine what
157 ;; command is used to send the PostScript images to the printer, and
158 ;; what arguments to give the command. These are analogous to lpr-
159 ;; command and lpr-switches.
160 ;;
161 ;; NOTE: ps-lpr-command and ps-lpr-switches take their initial values
162 ;; from the variables lpr-command and lpr-switches. If you have
163 ;; lpr-command set to invoke a pretty-printer such as enscript,
164 ;; then ps-print won't work properly. ps-lpr-command must name
165 ;; a program that does not format the files it prints.
166 ;;
167 ;;
168 ;; How Ps-Print Deals With Fonts
169 ;;
170 ;; The ps-print-*-with-faces commands attempt to determine which faces
171 ;; should be printed in bold or italic, but their guesses aren't
172 ;; always right. For example, you might want to map colors into faces
173 ;; so that blue faces print in bold, and red faces in italic.
174 ;;
175 ;; It is possible to force ps-print to consider specific faces bold or
176 ;; italic, no matter what font they are displayed in, by setting the
177 ;; variables ps-bold-faces and ps-italic-faces. These variables
178 ;; contain lists of faces that ps-print should consider bold or
179 ;; italic; to set them, put code like the following into your .emacs
180 ;; file:
181 ;;
182 ;; (setq ps-bold-faces '(my-blue-face))
183 ;; (setq ps-italic-faces '(my-red-face))
184 ;;
185 ;; Faces like bold-italic that are both bold and italic should go in
186 ;; *both* lists.
187 ;;
188 ;; Ps-print does not attempt to guess the sizes of fonts; all text is
189 ;; rendered using the Courier font family, in 10 point size. To
190 ;; change the font family, change the variables ps-font, ps-font-bold,
191 ;; ps-font-italic, and ps-font-bold-italic; fixed-pitch fonts work
192 ;; best, but are not required. To change the font size, change the
193 ;; variable ps-font-size.
194 ;;
195 ;; If you change the font family or size, you MUST also change the
196 ;; variables ps-line-height, ps-avg-char-width, and ps-space-width, or
197 ;; ps-print cannot correctly place line and page breaks.
198 ;;
199 ;; Ps-print keeps internal lists of which fonts are bold and which are
200 ;; italic; these lists are built the first time you invoke ps-print.
201 ;; For the sake of efficiency, the lists are built only once; the same
202 ;; lists are referred in later invocations of ps-print.
203 ;;
204 ;; Because these lists are built only once, it's possible for them to
205 ;; get out of sync, if a face changes, or if new faces are added. To
206 ;; get the lists back in sync, you can set the variable
207 ;; ps-build-face-reference to t, and the lists will be rebuilt the
208 ;; next time ps-print is invoked.
209 ;;
210 ;;
211 ;; How Ps-Print Deals With Color
212 ;;
213 ;; Ps-print detects faces with foreground and background colors
214 ;; defined and embeds color information in the PostScript image. The
215 ;; default foreground and background colors are defined by the
216 ;; variables ps-default-fg and ps-default-bg. On black-and-white
217 ;; printers, colors are displayed in grayscale. To turn off color
218 ;; output, set ps-print-color-p to nil.
219 ;;
220 ;;
221 ;; Headers
222 ;;
223 ;; Ps-print can print headers at the top of each page; the default
224 ;; headers contain the following four items: on the left, the name of
225 ;; the buffer and, if the buffer is visiting a file, the file's
226 ;; directory; on the right, the page number and date of printing. The
227 ;; default headers look something like this:
228 ;;
229 ;; ps-print.el 1/21
230 ;; /home/jct/emacs-lisp/ps/new 94/12/31
231 ;;
232 ;; When printing on duplex printers, left and right are reversed so
233 ;; that the page numbers are toward the outside.
234 ;;
235 ;; Headers are configurable. To turn them off completely, set
236 ;; ps-print-header to nil. To turn off the header's gaudy framing
237 ;; box, set ps-print-header-frame to nil. Page numbers are printed in
238 ;; "n/m" format, indicating page n of m pages; to omit the total page
239 ;; count and just print the page number, set ps-show-n-of-n to nil.
240 ;;
241 ;; The amount of information in the header can be changed by changing
242 ;; the number of lines. To show less, set ps-header-lines to 1, and
243 ;; the header will show only the buffer name and page number. To show
244 ;; more, set ps-header-lines to 3, and the header will show the time of
245 ;; printing below the date.
246 ;;
247 ;; To change the content of the headers, change the variables
248 ;; ps-left-header and ps-right-header. These variables are lists,
249 ;; specifying top-to-bottom the text to display on the left or right
250 ;; side of the header. Each element of the list should be a string or
251 ;; a symbol. Strings are inserted directly into the PostScript
252 ;; arrays, and should contain the PostScript string delimiters '(' and
253 ;; ')'.
254 ;;
255 ;; Symbols in the header format lists can either represent functions
256 ;; or variables. Functions are called, and should return a string to
257 ;; show in the header. Variables should contain strings to display in
258 ;; the header. In either case, function or variable, the PostScript
259 ;; string delimiters are added by ps-print, and should not be part of
260 ;; the returned value.
261 ;;
262 ;; Here's an example: say we want the left header to display the text
263 ;;
264 ;; Moe
265 ;; Larry
266 ;; Curly
267 ;;
268 ;; where we have a function to return "Moe"
269 ;;
270 ;; (defun moe-func ()
271 ;; "Moe")
272 ;;
273 ;; a variable specifying "Larry"
274 ;;
275 ;; (setq larry-var "Larry")
276 ;;
277 ;; and a literal for "Curly". Here's how ps-left-header should be
278 ;; set:
279 ;;
280 ;; (setq ps-left-header (list 'moe-func 'larry-var "(Curly)"))
281 ;;
282 ;; Note that Curly has the PostScript string delimiters inside his
283 ;; quotes -- those aren't misplaced lisp delimiters! Without them,
284 ;; PostScript would attempt to call the undefined function Curly,
285 ;; which would result in a PostScript error. Since most printers
286 ;; don't report PostScript errors except by aborting the print job,
287 ;; this kind of error can be hard to track down. Consider yourself
288 ;; warned.
289 ;;
290 ;;
291 ;; Duplex Printers
292 ;;
293 ;; If you have a duplex-capable printer (one that prints both sides of
294 ;; the paper), set ps-spool-duplex to t. Ps-print will insert blank
295 ;; pages to make sure each buffer starts on the correct side of the
296 ;; paper. Don't forget to set ps-lpr-switches to select duplex
297 ;; printing for your printer.
298 ;;
299 ;;
300 ;; Paper Size
301 ;;
302 ;; The variable ps-paper-type determines the size of paper ps-print
303 ;; formats for; it should contain one of the symbols ps-letter,
304 ;; ps-legal, or ps-a4. The default is ps-letter.
305 ;;
306 ;;
307 ;; Make sure that the variables ps-lpr-command and ps-lpr-switches
308 ;; contain appropriate values for your system; see the usage notes
309 ;; below and the documentation of these variables.
310 ;;
311 ;;
312 ;; New since version 1.5
313 ;; ---------------------
314 ;; Color output capability.
315 ;;
316 ;; Automatic detection of font attributes (bold, italic).
317 ;;
318 ;; Configurable headers with page numbers.
319 ;;
320 ;; Slightly faster.
321 ;;
322 ;; Support for different paper sizes.
323 ;;
324 ;; Better conformance to PostScript Document Structure Conventions.
325 ;;
326 ;;
327 ;; Known bugs and limitations of ps-print:
328 ;; --------------------------------------
329 ;; Although color printing will work in XEmacs 19.12, it doesn't work
330 ;; well; in particular, bold or italic fonts don't print in the right
331 ;; background color.
332 ;;
333 ;; Invisible properties aren't correctly ignored in XEmacs 19.12.
334 ;;
335 ;; Automatic font-attribute detection doesn't work well, especially
336 ;; with hilit19 and older versions of get-create-face. Users having
337 ;; problems with auto-font detection should use the lists ps-italic-
338 ;; faces and ps-bold-faces and/or turn off automatic detection by
339 ;; setting ps-auto-font-detect to nil.
340 ;;
341 ;; Automatic font-attribute detection doesn't work with XEmacs 19.12
342 ;; in tty mode; use the lists ps-italic-faces and ps-bold-faces
343 ;; instead.
344 ;;
345 ;; Still too slow; could use some hand-optimization.
346 ;;
347 ;; ASCII Control characters other than tab, linefeed and pagefeed are
348 ;; not handled.
349 ;;
350 ;; Default background color isn't working.
351 ;;
352 ;; Faces are always treated as opaque.
353 ;;
354 ;; Epoch and Emacs 18 not supported. At all.
355 ;;
356 ;;
357 ;; Features to add:
358 ;; ---------------
359 ;; 2-up and 4-up capability.
360 ;;
361 ;; Line numbers.
362 ;;
363 ;; Wide-print (landscape) capability.
364 ;;
365 ;;
366 ;; Acknowledgements
367 ;; ----------------
368 ;; Thanks to Kevin Rodgers <kevinr@ihs.com> for adding support for
369 ;; color and the invisible property.
370 ;;
371 ;; Thanks to Avishai Yacobi, avishaiy@mcil.comm.mot.com, for writing
372 ;; the initial port to Emacs 19. His code is no longer part of
373 ;; ps-print, but his work is still appreciated.
374 ;;
375 ;; Thanks to Remi Houdaille and Michel Train, michel@metasoft.fdn.org,
376 ;; for adding underline support. Their code also is no longer part of
377 ;; ps-print, but their efforts are not forgotten.
378 ;;
379 ;; Thanks also to all of you who mailed code to add features to
380 ;; ps-print; although I didn't use your code, I still appreciate your
381 ;; sharing it with me.
382 ;;
383 ;; Thanks to all who mailed comments, encouragement, and criticism.
384 ;; Thanks also to all who responded to my survey; I had too many
385 ;; responses to reply to them all, but I greatly appreciate your
386 ;; interest.
387 ;;
388 ;; Jim
389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
390
391 ;;; Code:
392
393 (defconst ps-print-version "2.8"
394 "ps-print.el,v 2.8 1995/05/04 12:06:10 jct Exp
395
396 Jim's last change version -- this file may have been edited as part of
397 Emacs without changes to the version number. When reporting bugs,
398 please also report the version of Emacs, if any, that ps-print was
399 distributed with.
400
401 Please send all bug fixes and enhancements to
402 Jim Thompson <thompson@wg2.waii.com>.")
403
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 ;; User Variables:
406
407 (defvar ps-lpr-command lpr-command
408 "*The shell command for printing a PostScript file.")
409
410 (defvar ps-lpr-switches lpr-switches
411 "*A list of extra switches to pass to `ps-lpr-command'.")
412
413 (defvar ps-spool-duplex nil ; Not many people have duplex
414 ; printers, so default to nil.
415 "*Non-nil indicates spooling is for a two-sided printer.
416 For a duplex printer, the `ps-spool-*' commands will insert blank pages
417 as needed between print jobs so that the next buffer printed will
418 start on the right page. Also, if headers are turned on, the headers
419 will be reversed on duplex printers so that the page numbers fall to
420 the left on even-numbered pages.")
421
422 (defvar ps-paper-type 'ps-letter
423 "*Specifies the size of paper to format for. Should be one of
424 `ps-letter', `ps-legal', or `ps-a4'.")
425
426 (defvar ps-print-header t
427 "*Non-nil means print a header at the top of each page.
428 By default, the header displays the buffer name, page number, and, if
429 the buffer is visiting a file, the file's directory. Headers are
430 customizable by changing variables `ps-header-left' and
431 `ps-header-right'.")
432
433 (defvar ps-print-header-frame t
434 "*Non-nil means draw a gaudy frame around the header.")
435
436 (defvar ps-show-n-of-n t
437 "*Non-nil means show page numbers as N/M, meaning page N of M.
438 Note: page numbers are displayed as part of headers, see variable
439 `ps-print-headers'.")
440
441 (defvar ps-print-color-p (and (or (fboundp 'x-color-values) ; Emacs
442 (fboundp 'pixel-components)) ; XEmacs
443 (fboundp 'float))
444 ; Printing color requires both floating point and x-color-values.
445 "*If non-nil, print the buffer's text in color.")
446
447 (defvar ps-default-fg '(0.0 0.0 0.0)
448 "*RGB values of the default foreground color. Defaults to black.")
449
450 (defvar ps-default-bg '(1.0 1.0 1.0)
451 "*RGB values of the default background color. Defaults to white.")
452
453 (defvar ps-font-size 10
454 "*Font size, in points, for generating Postscript.")
455
456 (defvar ps-font "Courier"
457 "*Font family name for ordinary text, when generating Postscript.")
458
459 (defvar ps-font-bold "Courier-Bold"
460 "*Font family name for bold text, when generating Postscript.")
461
462 (defvar ps-font-italic "Courier-Oblique"
463 "*Font family name for italic text, when generating Postscript.")
464
465 (defvar ps-font-bold-italic "Courier-BoldOblique"
466 "*Font family name for bold italic text, when generating Postscript.")
467
468 (defvar ps-avg-char-width (if (fboundp 'float) 5.6 6)
469 "*The average width, in points, of a character, for generating Postscript.
470 This is the value that ps-print uses to determine the length,
471 x-dimension, of the text it has printed, and thus affects the point at
472 which long lines wrap around. If you change the font or
473 font size, you will probably have to adjust this value to match.")
474
475 (defvar ps-space-width (if (fboundp 'float) 5.6 6)
476 "*The width of a space character, for generating Postscript.
477 This value is used in expanding tab characters.")
478
479 (defvar ps-line-height (if (fboundp 'float) 11.29 11)
480 "*The height of a line, for generating Postscript.
481 This is the value that ps-print uses to determine the height,
482 y-dimension, of the lines of text it has printed, and thus affects the
483 point at which page-breaks are placed. If you change the font or font
484 size, you will probably have to adjust this value to match. The
485 line-height is *not* the same as the point size of the font.")
486
487 (defvar ps-auto-font-detect t
488 "*Non-nil means automatically detect bold/italic face attributes.
489 nil means rely solely on the lists `ps-bold-faces', `ps-italic-faces',
490 and `ps-underlined-faces'.")
491
492 (defvar ps-bold-faces '()
493 "*A list of the \(non-bold\) faces that should be printed in bold font.
494 This applies to generating Postscript.")
495
496 (defvar ps-italic-faces '()
497 "*A list of the \(non-italic\) faces that should be printed in italic font.
498 This applies to generating Postscript.")
499
500 (defvar ps-underlined-faces '()
501 "*A list of the \(non-underlined\) faces that should be printed underlined.
502 This applies to generating Postscript.")
503
504 (defvar ps-header-lines 2
505 "*Number of lines to display in page header, when generating Postscript.")
506 (make-variable-buffer-local 'ps-header-lines)
507
508 (defvar ps-left-header
509 (list 'ps-get-buffer-name 'ps-header-dirpart)
510 "*The items to display on the right part of the page header.
511 This applies to generating Postscript.
512
513 The value should be a list of strings and symbols, each representing an
514 entry in the PostScript array HeaderLinesLeft.
515
516 Strings are inserted unchanged into the array; those representing
517 PostScript string literals should be delimited with PostScript string
518 delimiters '(' and ')'.
519
520 For symbols with bound functions, the function is called and should
521 return a string to be inserted into the array. For symbols with bound
522 values, the value should be a string to be inserted into the array.
523 In either case, function or variable, the string value has PostScript
524 string delimiters added to it.")
525 (make-variable-buffer-local 'ps-left-header)
526
527 (defvar ps-right-header
528 (list "/pagenumberstring load" 'time-stamp-yy/mm/dd 'time-stamp-hh:mm:ss)
529 "*The items to display on the left part of the page header.
530 This applies to generating Postscript.
531
532 See the variable `ps-left-header' for a description of the format of
533 this variable.")
534 (make-variable-buffer-local 'ps-right-header)
535
536 (defvar ps-razzle-dazzle t
537 "*Non-nil means report progress while formatting buffer.")
538
539 (defvar ps-adobe-tag "%!PS-Adobe-1.0\n"
540 "*Contains the header line identifying the output as PostScript.
541 By default, `ps-adobe-tag' contains the standard identifier. Some
542 printers require slightly different versions of this line.")
543
544 (defvar ps-build-face-reference t
545 "*Non-nil means build the reference face lists.
546
547 Ps-print sets this value to nil after it builds its internal reference
548 lists of bold and italic faces. By settings its value back to t, you
549 can force ps-print to rebuild the lists the next time you invoke one
550 of the ...-with-faces commands.
551
552 You should set this value back to t after you change the attributes of
553 any face, or create new faces. Most users shouldn't have to worry
554 about its setting, though.")
555
556 (defvar ps-always-build-face-reference nil
557 "*Non-nil means always rebuild the reference face lists.
558
559 If this variable is non-nil, ps-print will rebuild its internal
560 reference lists of bold and italic faces *every* time one of the
561 -with-faces commands is called. Most users shouldn't need to set this
562 variable.")
563
564 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
565 ;; User commands
566
567 ;;;###autoload
568 (defun ps-print-buffer (&optional filename)
569 "Generate and print a PostScript image of the buffer.
570
571 When called with a numeric prefix argument (C-u), prompts the user for
572 the name of a file to save the PostScript image in, instead of sending
573 it to the printer.
574
575 More specifically, the FILENAME argument is treated as follows: if it
576 is nil, send the image to the printer. If FILENAME is a string, save
577 the PostScript image in a file with that name. If FILENAME is a
578 number, prompt the user for the name of the file to save in."
579
580 (interactive (list (ps-print-preprint current-prefix-arg)))
581 (ps-generate (current-buffer) (point-min) (point-max)
582 'ps-generate-postscript)
583 (ps-do-despool filename))
584
585
586 ;;;###autoload
587 (defun ps-print-buffer-with-faces (&optional filename)
588 "Generate and print a PostScript image of the buffer.
589 Like `ps-print-buffer', but includes font, color, and underline
590 information in the generated image. This command works only if you
591 are using a window system, so it has a way to determine color values."
592 (interactive (list (ps-print-preprint current-prefix-arg)))
593 (ps-generate (current-buffer) (point-min) (point-max)
594 'ps-generate-postscript-with-faces)
595 (ps-do-despool filename))
596
597
598 ;;;###autoload
599 (defun ps-print-region (from to &optional filename)
600 "Generate and print a PostScript image of the region.
601 Like `ps-print-buffer', but prints just the current region."
602
603 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
604 (ps-generate (current-buffer) from to
605 'ps-generate-postscript)
606 (ps-do-despool filename))
607
608
609 ;;;###autoload
610 (defun ps-print-region-with-faces (from to &optional filename)
611 "Generate and print a PostScript image of the region.
612 Like `ps-print-region', but includes font, color, and underline
613 information in the generated image. This command works only if you
614 are using a window system, so it has a way to determine color values."
615
616 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
617 (ps-generate (current-buffer) from to
618 'ps-generate-postscript-with-faces)
619 (ps-do-despool filename))
620
621
622 ;;;###autoload
623 (defun ps-spool-buffer ()
624 "Generate and spool a PostScript image of the buffer.
625 Like `ps-print-buffer' except that the PostScript image is saved in a
626 local buffer to be sent to the printer later.
627
628 Use the command `ps-despool' to send the spooled images to the printer."
629 (interactive)
630 (ps-generate (current-buffer) (point-min) (point-max)
631 'ps-generate-postscript))
632
633
634 ;;;###autoload
635 (defun ps-spool-buffer-with-faces ()
636 "Generate and spool a PostScript image of the buffer.
637 Like `ps-spool-buffer', but includes font, color, and underline
638 information in the generated image. This command works only if you
639 are using a window system, so it has a way to determine color values.
640
641 Use the command `ps-despool' to send the spooled images to the printer."
642
643 (interactive)
644 (ps-generate (current-buffer) (point-min) (point-max)
645 'ps-generate-postscript-with-faces))
646
647
648 ;;;###autoload
649 (defun ps-spool-region (from to)
650 "Generate a PostScript image of the region and spool locally.
651 Like `ps-spool-buffer', but spools just the current region.
652
653 Use the command `ps-despool' to send the spooled images to the printer."
654 (interactive "r")
655 (ps-generate (current-buffer) from to
656 'ps-generate-postscript))
657
658
659 ;;;###autoload
660 (defun ps-spool-region-with-faces (from to)
661 "Generate a PostScript image of the region and spool locally.
662 Like `ps-spool-region', but includes font, color, and underline
663 information in the generated image. This command works only if you
664 are using a window system, so it has a way to determine color values.
665
666 Use the command `ps-despool' to send the spooled images to the printer."
667 (interactive "r")
668 (ps-generate (current-buffer) from to
669 'ps-generate-postscript-with-faces))
670
671 ;;;###autoload
672 (defun ps-despool (&optional filename)
673 "Send the spooled PostScript to the printer.
674
675 When called with a numeric prefix argument (C-u), prompt the user for
676 the name of a file to save the spooled PostScript in, instead of sending
677 it to the printer.
678
679 More specifically, the FILENAME argument is treated as follows: if it
680 is nil, send the image to the printer. If FILENAME is a string, save
681 the PostScript image in a file with that name. If FILENAME is a
682 number, prompt the user for the name of the file to save in."
683 (interactive (list (ps-print-preprint current-prefix-arg)))
684 (ps-do-despool filename))
685
686 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
687 ;; Utility functions and variables:
688
689 (defvar ps-print-emacs-type
690 (cond ((string-match "XEmacs" emacs-version) 'xemacs)
691 ((string-match "Lucid" emacs-version) 'lucid)
692 ((string-match "Epoch" emacs-version) 'epoch)
693 (t 'emacs)))
694
695 (if (or (eq ps-print-emacs-type 'lucid)
696 (eq ps-print-emacs-type 'xemacs))
697 (if (< emacs-minor-version 12)
698 (setq ps-print-color-p nil))
699 (require 'faces)) ; face-font, face-underline-p,
700 ; x-font-regexp
701
702 (require 'time-stamp)
703
704 (defvar ps-print-prologue "% ISOLatin1Encoding stolen from ps_init.ps in GhostScript 2.6.1.4:
705 % If the ISOLatin1Encoding vector isn't known, define it.
706 /ISOLatin1Encoding where { pop } {
707 % Define the ISO Latin-1 encoding vector.
708 % The first half is the same as the standard encoding,
709 % except for minus instead of hyphen at code 055.
710 /ISOLatin1Encoding
711 StandardEncoding 0 45 getinterval aload pop
712 /minus
713 StandardEncoding 46 82 getinterval aload pop
714 %*** NOTE: the following are missing in the Adobe documentation,
715 %*** but appear in the displayed table:
716 %*** macron at 0225, dieresis at 0230, cedilla at 0233, space at 0240.
717 % \20x
718 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
719 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
720 /dotlessi /grave /acute /circumflex /tilde /macron /breve /dotaccent
721 /dieresis /.notdef /ring /cedilla /.notdef /hungarumlaut /ogonek /caron
722 % \24x
723 /space /exclamdown /cent /sterling
724 /currency /yen /brokenbar /section
725 /dieresis /copyright /ordfeminine /guillemotleft
726 /logicalnot /hyphen /registered /macron
727 /degree /plusminus /twosuperior /threesuperior
728 /acute /mu /paragraph /periodcentered
729 /cedilla /onesuperior /ordmasculine /guillemotright
730 /onequarter /onehalf /threequarters /questiondown
731 % \30x
732 /Agrave /Aacute /Acircumflex /Atilde
733 /Adieresis /Aring /AE /Ccedilla
734 /Egrave /Eacute /Ecircumflex /Edieresis
735 /Igrave /Iacute /Icircumflex /Idieresis
736 /Eth /Ntilde /Ograve /Oacute
737 /Ocircumflex /Otilde /Odieresis /multiply
738 /Oslash /Ugrave /Uacute /Ucircumflex
739 /Udieresis /Yacute /Thorn /germandbls
740 % \34x
741 /agrave /aacute /acircumflex /atilde
742 /adieresis /aring /ae /ccedilla
743 /egrave /eacute /ecircumflex /edieresis
744 /igrave /iacute /icircumflex /idieresis
745 /eth /ntilde /ograve /oacute
746 /ocircumflex /otilde /odieresis /divide
747 /oslash /ugrave /uacute /ucircumflex
748 /udieresis /yacute /thorn /ydieresis
749 256 packedarray def
750 } ifelse
751
752 /reencodeFontISO { %def
753 dup
754 length 5 add dict % Make a new font (a new dict
755 % the same size as the old
756 % one) with room for our new
757 % symbols.
758
759 begin % Make the new font the
760 % current dictionary.
761
762
763 { 1 index /FID ne
764 { def } { pop pop } ifelse
765 } forall % Copy each of the symbols
766 % from the old dictionary to
767 % the new except for the font
768 % ID.
769
770 /Encoding ISOLatin1Encoding def % Override the encoding with
771 % the ISOLatin1 encoding.
772
773 % Use the font's bounding box to determine the ascent, descent,
774 % and overall height; don't forget that these values have to be
775 % transformed using the font's matrix.
776 FontBBox
777 FontMatrix transform /Ascent exch def pop
778 FontMatrix transform /Descent exch def pop
779 /FontHeight Ascent Descent sub def
780
781 % Define these in case they're not in the FontInfo (also, here
782 % they're easier to get to.
783 /UnderlinePosition 1 def
784 /UnderlineThickness 1 def
785
786 % Get the underline position and thickness if they're defined.
787 currentdict /FontInfo known {
788 FontInfo
789
790 dup /UnderlinePosition known {
791 dup /UnderlinePosition get
792 0 exch FontMatrix transform exch pop
793 /UnderlinePosition exch def
794 } if
795
796 dup /UnderlineThickness known {
797 /UnderlineThickness get
798 0 exch FontMatrix transform exch pop
799 /UnderlineThickness exch def
800 } if
801
802 } if
803
804 currentdict % Leave the new font on the
805 % stack
806
807 end % Stop using the font as the
808 % current dictionary.
809
810 definefont % Put the font into the font
811 % dictionary
812
813 pop % Discard the returned font.
814 } bind def
815
816 /Font {
817 findfont exch scalefont reencodeFontISO
818 } def
819
820 /F { % Font select
821 findfont
822 dup /Ascent get /Ascent exch def
823 dup /Descent get /Descent exch def
824 dup /FontHeight get /FontHeight exch def
825 dup /UnderlinePosition get /UnderlinePosition exch def
826 dup /UnderlineThickness get /UnderlineThickness exch def
827 setfont
828 } def
829
830 /FG /setrgbcolor load def
831
832 /bg false def
833 /BG {
834 dup /bg exch def
835 { mark 4 1 roll ] /bgcolor exch def } if
836 } def
837
838 /dobackground { % width --
839 currentpoint
840 gsave
841 newpath
842 moveto
843 0 Ascent rmoveto
844 dup 0 rlineto
845 0 Descent Ascent sub rlineto
846 neg 0 rlineto
847 closepath
848 bgcolor aload pop setrgbcolor
849 fill
850 grestore
851 } def
852
853 /dobackgroundstring { % string --
854 stringwidth pop
855 dobackground
856 } def
857
858 /dounderline { % fromx fromy --
859 currentpoint
860 gsave
861 UnderlineThickness setlinewidth
862 4 2 roll
863 UnderlinePosition add moveto
864 UnderlinePosition add lineto
865 stroke
866 grestore
867 } def
868
869 /eolbg {
870 currentpoint pop
871 PrintWidth LeftMargin add exch sub dobackground
872 } def
873
874 /eolul {
875 currentpoint exch pop
876 PrintWidth LeftMargin add exch dounderline
877 } def
878
879 /SL { % Soft Linefeed
880 bg { eolbg } if
881 ul { eolul } if
882 currentpoint LineHeight sub LeftMargin exch moveto pop
883 } def
884
885 /HL /SL load def % Hard Linefeed
886
887 /sp1 { currentpoint 3 -1 roll } def
888
889 % Some debug
890 /dcp { currentpoint exch 40 string cvs print (, ) print = } def
891 /dp { print 2 copy
892 exch 40 string cvs print (, ) print = } def
893
894 /S {
895 bg { dup dobackgroundstring } if
896 ul { sp1 } if
897 show
898 ul { dounderline } if
899 } def
900
901 /W {
902 ul { sp1 } if
903 ( ) stringwidth % Get the width of a space
904 pop % Discard the Y component
905 mul % Multiply the width of a
906 % space by the number of
907 % spaces to plot
908 bg { dup dobackground } if
909 0 rmoveto
910 ul { dounderline } if
911 } def
912
913 /BeginDSCPage {
914 /vmstate save def
915 } def
916
917 /BeginPage {
918 PrintHeader {
919 PrintHeaderFrame { HeaderFrame } if
920 HeaderText
921 } if
922 LeftMargin
923 BottomMargin PrintHeight add
924 moveto % move to where printing will
925 % start.
926 } def
927
928 /EndPage {
929 bg { eolbg } if
930 ul { eolul } if
931 showpage % Spit out a page
932 } def
933
934 /EndDSCPage {
935 vmstate restore
936 } def
937
938 /ul false def
939
940 /UL { /ul exch def } def
941
942 /h0 14 /Helvetica-Bold Font
943 /h1 12 /Helvetica Font
944
945 /h1 F
946
947 /HeaderLineHeight FontHeight def
948 /HeaderDescent Descent def
949 /HeaderPad 2 def
950
951 /SetHeaderLines {
952 /HeaderOffset TopMargin 2 div def
953 /HeaderLines exch def
954 /HeaderHeight HeaderLines HeaderLineHeight mul HeaderPad 2 mul add def
955 /PrintHeight PrintHeight HeaderHeight sub def
956 } def
957
958 /HeaderFrameStart {
959 LeftMargin BottomMargin PrintHeight add HeaderOffset add
960 } def
961
962 /HeaderFramePath {
963 PrintWidth 0 rlineto
964 0 HeaderHeight rlineto
965 PrintWidth neg 0 rlineto
966 0 HeaderHeight neg rlineto
967 } def
968
969 /HeaderFrame {
970 gsave
971 0.4 setlinewidth
972 HeaderFrameStart moveto
973 1 -1 rmoveto
974 HeaderFramePath
975 0 setgray fill
976 HeaderFrameStart moveto
977 HeaderFramePath
978 gsave 0.9 setgray fill grestore
979 gsave 0 setgray stroke grestore
980 grestore
981 } def
982
983 /HeaderStart {
984 HeaderFrameStart
985 exch HeaderPad add exch
986 HeaderLineHeight HeaderLines 1 sub mul add HeaderDescent sub HeaderPad add
987 } def
988
989 /strcat {
990 dup length 3 -1 roll dup length dup 4 -1 roll add string dup
991 0 5 -1 roll putinterval
992 dup 4 2 roll exch putinterval
993 } def
994
995 /pagenumberstring {
996 PageNumber 32 string cvs
997 ShowNofN {
998 (/) strcat
999 PageCount 32 string cvs strcat
1000 } if
1001 } def
1002
1003 /HeaderText {
1004 HeaderStart moveto
1005
1006 HeaderLinesRight HeaderLinesLeft
1007 Duplex PageNumber 1 and 0 eq and { exch } if
1008
1009 {
1010 aload pop
1011 exch F
1012 gsave
1013 dup xcheck { exec } if
1014 show
1015 grestore
1016 0 HeaderLineHeight neg rmoveto
1017 } forall
1018
1019 HeaderStart moveto
1020
1021 {
1022 aload pop
1023 exch F
1024 gsave
1025 dup xcheck { exec } if
1026 dup stringwidth pop
1027 PrintWidth exch sub HeaderPad 2 mul sub 0 rmoveto
1028 show
1029 grestore
1030 0 HeaderLineHeight neg rmoveto
1031 } forall
1032 } def
1033
1034 /ReportFontInfo {
1035 2 copy
1036 /t0 3 1 roll Font
1037 /t0 F
1038 /lh FontHeight def
1039 /sw ( ) stringwidth pop def
1040 /aw (01234567890abcdefghijklmnopqrstuvwxyz) dup length exch
1041 stringwidth pop exch div def
1042 /t1 12 /Helvetica-Oblique Font
1043 /t1 F
1044 72 72 moveto
1045 gsave
1046 (For ) show
1047 128 string cvs show
1048 ( ) show
1049 32 string cvs show
1050 ( point, the line height is ) show
1051 lh 32 string cvs show
1052 (, the space width is ) show
1053 sw 32 string cvs show
1054 (,) show
1055 grestore
1056 0 FontHeight neg rmoveto
1057 (and a crude estimate of average character width is ) show
1058 aw 32 string cvs show
1059 (.) show
1060 showpage
1061 } def
1062
1063 % 10 /Courier ReportFontInfo
1064 ")
1065
1066 ;; Start Editing Here:
1067
1068 (defvar ps-source-buffer nil)
1069 (defvar ps-spool-buffer-name "*PostScript*")
1070 (defvar ps-spool-buffer nil)
1071
1072 (defvar ps-output-head nil)
1073 (defvar ps-output-tail nil)
1074
1075 (defvar ps-page-count 0)
1076 (defvar ps-showpage-count 0)
1077
1078 (defvar ps-current-font 0)
1079 (defvar ps-current-underline-p nil)
1080 (defvar ps-default-color (if ps-print-color-p ps-default-fg)) ; black
1081 (defvar ps-current-color ps-default-color)
1082 (defvar ps-current-bg nil)
1083
1084 (defvar ps-razchunk 0)
1085
1086 (defvar ps-color-format (if (eq ps-print-emacs-type 'emacs)
1087
1088 ;;Emacs understands the %f format; we'll
1089 ;;use it to limit color RGB values to
1090 ;;three decimals to cut down some on the
1091 ;;size of the PostScript output.
1092 "%0.3f %0.3f %0.3f"
1093
1094 ;; Lucid emacsen will have to make do with
1095 ;; %s (princ) for floats.
1096 "%s %s %s"))
1097
1098 ;; These values determine how much print-height to deduct when headers
1099 ;; are turned on. This is a pretty clumsy way of handling it, but
1100 ;; it'll do for now.
1101 (defvar ps-header-title-line-height (if (fboundp 'float) 16.0 16));Helvetica 14
1102 (defvar ps-header-line-height (if (fboundp 'float) 13.7 14));Helvetica 12
1103 (defvar ps-header-pad 2)
1104
1105 ;; LetterSmall 7.68 inch 10.16 inch
1106 ;; Tabloid 11.0 inch 17.0 inch
1107 ;; Ledger 17.0 inch 11.0 inch
1108 ;; Statement 5.5 inch 8.5 inch
1109 ;; Executive 7.5 inch 10.0 inch
1110 ;; A3 11.69 inch 16.5 inch
1111 ;; A4Small 7.47 inch 10.85 inch
1112 ;; B4 10.125 inch 14.33 inch
1113 ;; B5 7.16 inch 10.125 inch
1114
1115 ;; All page dimensions are in PostScript points.
1116
1117 (defvar ps-left-margin 72) ; 1 inch
1118 (defvar ps-right-margin 72) ; 1 inch
1119 (defvar ps-bottom-margin 36) ; 1/2 inch
1120 (defvar ps-top-margin 72) ; 1 inch
1121
1122 ;; Letter 8.5 inch x 11.0 inch
1123 (defvar ps-letter-page-height 792) ; 11 inches
1124 (defvar ps-letter-page-width 612) ; 8.5 inches
1125
1126 ;; Legal 8.5 inch x 14.0 inch
1127 (defvar ps-legal-page-height 1008) ; 14.0 inches
1128 (defvar ps-legal-page-width 612) ; 8.5 inches
1129
1130 ;; A4 8.26 inch x 11.69 inch
1131 (defvar ps-a4-page-height 842) ; 11.69 inches
1132 (defvar ps-a4-page-width 595) ; 8.26 inches
1133
1134 (defvar ps-pages-alist
1135 (list (list 'ps-letter ps-letter-page-width ps-letter-page-height)
1136 (list 'ps-legal ps-legal-page-width ps-legal-page-height)
1137 (list 'ps-a4 ps-a4-page-width ps-a4-page-height)))
1138
1139 ;; Define some constants to index into the page lists.
1140 (defvar ps-page-width-i 1)
1141 (defvar ps-page-height-i 2)
1142
1143 (defvar ps-page-dimensions nil)
1144 (defvar ps-print-width nil)
1145 (defvar ps-print-height nil)
1146
1147 (defvar ps-height-remaining)
1148 (defvar ps-width-remaining)
1149
1150 (defvar ps-ref-bold-faces nil)
1151 (defvar ps-ref-italic-faces nil)
1152 (defvar ps-ref-underlined-faces nil)
1153
1154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1155 ;; Internal functions
1156
1157 (defun ps-get-page-dimensions ()
1158 (setq ps-page-dimensions (assq ps-paper-type ps-pages-alist))
1159 (let ((ps-page-width (nth ps-page-width-i ps-page-dimensions))
1160 (ps-page-height (nth ps-page-height-i ps-page-dimensions)))
1161 (setq ps-print-height (- ps-page-height ps-top-margin ps-bottom-margin))
1162 (setq ps-print-width (- ps-page-width ps-left-margin ps-right-margin))))
1163
1164 (defun ps-print-preprint (&optional filename)
1165 (if (and filename
1166 (or (numberp filename)
1167 (listp filename)))
1168 (let* ((name (concat (buffer-name) ".ps"))
1169 (prompt (format "Save PostScript to file: (default %s) "
1170 name)))
1171 (read-file-name prompt default-directory
1172 name nil))))
1173
1174 ;; The following functions implement a simple list-buffering scheme so
1175 ;; that ps-print doesn't have to repeatedly switch between buffers
1176 ;; while spooling. The functions ps-output and ps-output-string build
1177 ;; up the lists; the function ps-flush-output takes the lists and
1178 ;; insert its contents into the spool buffer (*PostScript*).
1179
1180 (defun ps-output-string-prim (string)
1181 (insert "(") ;insert start-string delimiter
1182 (save-excursion ;insert string
1183 (insert string))
1184
1185 ;; Find and quote special characters as necessary for PS
1186 (while (re-search-forward "[()\\]" nil t)
1187 (save-excursion
1188 (forward-char -1)
1189 (insert "\\")))
1190
1191 (goto-char (point-max))
1192 (insert ")")) ;insert end-string delimiter
1193
1194 (defun ps-init-output-queue ()
1195 (setq ps-output-head (list ""))
1196 (setq ps-output-tail ps-output-head))
1197
1198 (defun ps-output (&rest args)
1199 (setcdr ps-output-tail args)
1200 (while (cdr ps-output-tail)
1201 (setq ps-output-tail (cdr ps-output-tail))))
1202
1203 (defun ps-output-string (string)
1204 (ps-output t string))
1205
1206 (defun ps-flush-output ()
1207 (save-excursion
1208 (set-buffer ps-spool-buffer)
1209 (goto-char (point-max))
1210 (while ps-output-head
1211 (let ((it (car ps-output-head)))
1212 (if (not (eq t it))
1213 (insert it)
1214 (setq ps-output-head (cdr ps-output-head))
1215 (ps-output-string-prim (car ps-output-head))))
1216 (setq ps-output-head (cdr ps-output-head))))
1217 (ps-init-output-queue))
1218
1219 (defun ps-insert-file (fname)
1220 (ps-flush-output)
1221
1222 ;; Check to see that the file exists and is readable; if not, throw
1223 ;; and error.
1224 (if (not (file-readable-p fname))
1225 (error "Could not read file `%s'" fname))
1226
1227 (save-excursion
1228 (set-buffer ps-spool-buffer)
1229 (goto-char (point-max))
1230 (insert-file fname)))
1231
1232 ;; These functions insert the arrays that define the contents of the
1233 ;; headers.
1234
1235 (defun ps-generate-header-line (fonttag &optional content)
1236 (ps-output " [ " fonttag " ")
1237 (cond
1238 ;; Literal strings should be output as is -- the string must
1239 ;; contain its own PS string delimiters, '(' and ')', if necessary.
1240 ((stringp content)
1241 (ps-output content))
1242
1243 ;; Functions are called -- they should return strings; they will be
1244 ;; inserted as strings and the PS string delimiters added.
1245 ((and (symbolp content) (fboundp content))
1246 (ps-output-string (funcall content)))
1247
1248 ;; Variables will have their contents inserted. They should
1249 ;; contain strings, and will be inserted as strings.
1250 ((and (symbolp content) (boundp content))
1251 (ps-output-string (symbol-value content)))
1252
1253 ;; Anything else will get turned into an empty string.
1254 (t
1255 (ps-output-string "")))
1256 (ps-output " ]\n"))
1257
1258 (defun ps-generate-header (name contents)
1259 (ps-output "/" name " [\n")
1260 (if (> ps-header-lines 0)
1261 (let ((count 1))
1262 (ps-generate-header-line "/h0" (car contents))
1263 (while (and (< count ps-header-lines)
1264 (setq contents (cdr contents)))
1265 (ps-generate-header-line "/h1" (car contents))
1266 (setq count (+ count 1)))
1267 (ps-output "] def\n"))))
1268
1269 (defun ps-output-boolean (name bool)
1270 (ps-output (format "/%s %s def\n" name (if bool "true" "false"))))
1271
1272 (defun ps-begin-file ()
1273 (setq ps-showpage-count 0)
1274
1275 (ps-output ps-adobe-tag)
1276 (ps-output "%%Title: " (buffer-name) "\n") ;Take job name from name of
1277 ;first buffer printed
1278 (ps-output "%%Creator: " (user-full-name) "\n")
1279 (ps-output "%%CreationDate: "
1280 (time-stamp-hh:mm:ss) " " (time-stamp-mon-dd-yyyy) "\n")
1281 (ps-output "%% DocumentFonts: Helvetica Helvetica-Bold "
1282 ps-font " " ps-font-bold " " ps-font-italic " "
1283 ps-font-bold-italic "\n")
1284 (ps-output "%%Pages: (atend)\n")
1285 (ps-output "%%EndComments\n\n")
1286
1287 (ps-output-boolean "Duplex" ps-spool-duplex)
1288 (ps-output-boolean "PrintHeader" ps-print-header)
1289 (ps-output-boolean "PrintHeaderFrame" ps-print-header-frame)
1290 (ps-output-boolean "ShowNofN" ps-show-n-of-n)
1291
1292 (ps-output (format "/LeftMargin %d def\n" ps-left-margin))
1293 (ps-output (format "/RightMargin %d def\n" ps-right-margin))
1294 (ps-output (format "/BottomMargin %d def\n" ps-bottom-margin))
1295 (ps-output (format "/TopMargin %d def\n" ps-top-margin))
1296
1297 (ps-get-page-dimensions)
1298 (ps-output (format "/PrintWidth %d def\n" ps-print-width))
1299 (ps-output (format "/PrintHeight %d def\n" ps-print-height))
1300
1301 (ps-output (format "/LineHeight %s def\n" ps-line-height))
1302
1303 (ps-output ps-print-prologue)
1304
1305 (ps-output (format "/f0 %d /%s Font\n" ps-font-size ps-font))
1306 (ps-output (format "/f1 %d /%s Font\n" ps-font-size ps-font-bold))
1307 (ps-output (format "/f2 %d /%s Font\n" ps-font-size ps-font-italic))
1308 (ps-output (format "/f3 %d /%s Font\n" ps-font-size
1309 ps-font-bold-italic))
1310
1311 (ps-output "%%EndPrologue\n"))
1312
1313 (defun ps-header-dirpart ()
1314 (let ((fname (buffer-file-name)))
1315 (if fname
1316 (if (string-equal (buffer-name) (file-name-nondirectory fname))
1317 (file-name-directory fname)
1318 fname)
1319 "")))
1320
1321 (defun ps-get-buffer-name ()
1322 ;; Indulge me this little easter egg:
1323 (if (string= (buffer-name) "ps-print.el")
1324 "Hey, Cool! It's ps-print.el!!!"
1325 (buffer-name)))
1326
1327 (defun ps-begin-job ()
1328 (setq ps-page-count 0))
1329
1330 (defun ps-end-file ()
1331 (ps-output "%%Trailer\n")
1332 (ps-output "%%Pages: " (format "%d\n" ps-showpage-count)))
1333
1334 (defun ps-next-page ()
1335 (ps-end-page)
1336 (ps-flush-output)
1337 (ps-begin-page))
1338
1339 (defun ps-begin-page (&optional dummypage)
1340 (ps-get-page-dimensions)
1341 (setq ps-width-remaining ps-print-width)
1342 (setq ps-height-remaining ps-print-height)
1343
1344 ;; If headers are turned on, deduct the height of the header from
1345 ;; the print height remaining. Clumsy clumsy clumsy.
1346 (if ps-print-header
1347 (setq ps-height-remaining
1348 (- ps-height-remaining
1349 ps-header-title-line-height
1350 (* ps-header-line-height (- ps-header-lines 1))
1351 (* 2 ps-header-pad))))
1352
1353 (setq ps-page-count (+ ps-page-count 1))
1354
1355 (ps-output "\n%%Page: "
1356 (format "%d %d\n" ps-page-count (+ 1 ps-showpage-count)))
1357 (ps-output "BeginDSCPage\n")
1358 (ps-output (format "/PageNumber %d def\n" ps-page-count))
1359 (ps-output "/PageCount 0 def\n")
1360
1361 (if ps-print-header
1362 (progn
1363 (ps-generate-header "HeaderLinesLeft" ps-left-header)
1364 (ps-generate-header "HeaderLinesRight" ps-right-header)
1365 (ps-output (format "%d SetHeaderLines\n" ps-header-lines))))
1366
1367 (ps-output "BeginPage\n")
1368 (ps-set-font ps-current-font)
1369 (ps-set-bg ps-current-bg)
1370 (ps-set-color ps-current-color)
1371 (ps-set-underline ps-current-underline-p))
1372
1373 (defun ps-end-page ()
1374 (setq ps-showpage-count (+ 1 ps-showpage-count))
1375 (ps-output "EndPage\n")
1376 (ps-output "EndDSCPage\n"))
1377
1378 (defun ps-dummy-page ()
1379 (setq ps-showpage-count (+ 1 ps-showpage-count))
1380 (ps-output "%%Page: " (format "- %d\n" ps-showpage-count)
1381 "BeginDSCPage
1382 /PrintHeader false def
1383 BeginPage
1384 EndPage
1385 EndDSCPage\n"))
1386
1387 (defun ps-next-line ()
1388 (if (< ps-height-remaining ps-line-height)
1389 (ps-next-page)
1390 (setq ps-width-remaining ps-print-width)
1391 (setq ps-height-remaining (- ps-height-remaining ps-line-height))
1392 (ps-hard-lf)))
1393
1394 (defun ps-continue-line ()
1395 (if (< ps-height-remaining ps-line-height)
1396 (ps-next-page)
1397 (setq ps-width-remaining ps-print-width)
1398 (setq ps-height-remaining (- ps-height-remaining ps-line-height))
1399 (ps-soft-lf)))
1400
1401 (defun ps-hard-lf ()
1402 (ps-output "HL\n"))
1403
1404 (defun ps-soft-lf ()
1405 (ps-output "SL\n"))
1406
1407 (defun ps-find-wrappoint (from to char-width)
1408 (let ((avail (truncate (/ ps-width-remaining char-width)))
1409 (todo (- to from)))
1410 (if (< todo avail)
1411 (cons to (* todo char-width))
1412 (cons (+ from avail) ps-width-remaining))))
1413
1414 (defun ps-basic-plot-string (from to &optional bg-color)
1415 (let* ((wrappoint (ps-find-wrappoint from to ps-avg-char-width))
1416 (to (car wrappoint))
1417 (string (buffer-substring from to)))
1418 (ps-output-string string)
1419 (ps-output " S\n") ;
1420 wrappoint))
1421
1422 (defun ps-basic-plot-whitespace (from to &optional bg-color)
1423 (let* ((wrappoint (ps-find-wrappoint from to ps-space-width))
1424 (to (car wrappoint)))
1425
1426 (ps-output (format "%d W\n" (- to from)))
1427 wrappoint))
1428
1429 (defun ps-plot (plotfunc from to &optional bg-color)
1430 (while (< from to)
1431 (let* ((wrappoint (funcall plotfunc from to bg-color))
1432 (plotted-to (car wrappoint))
1433 (plotted-width (cdr wrappoint)))
1434 (setq from plotted-to)
1435 (setq ps-width-remaining (- ps-width-remaining plotted-width))
1436 (if (< from to)
1437 (ps-continue-line))))
1438 (if ps-razzle-dazzle
1439 (let* ((q-todo (- (point-max) (point-min)))
1440 (q-done (- (point) (point-min)))
1441 (chunkfrac (/ q-todo 8))
1442 (chunksize (if (> chunkfrac 1000) 1000 chunkfrac)))
1443 (if (> (- q-done ps-razchunk) chunksize)
1444 (let (foo)
1445 (setq ps-razchunk q-done)
1446 (setq foo
1447 (if (< q-todo 100)
1448 (/ (* 100 q-done) q-todo)
1449 (/ q-done (/ q-todo 100))))
1450 (message "Formatting...%d%%" foo))))))
1451
1452 (defun ps-set-font (font)
1453 (setq ps-current-font font)
1454 (ps-output (format "/f%d F\n" ps-current-font)))
1455
1456 (defvar ps-print-color-scale nil)
1457
1458 (defun ps-set-bg (color)
1459 (if (setq ps-current-bg color)
1460 (ps-output (format ps-color-format (nth 0 color) (nth 1 color)
1461 (nth 2 color))
1462 " true BG\n")
1463 (ps-output "false BG\n")))
1464
1465 (defun ps-set-color (color)
1466 (if (setq ps-current-color color)
1467 nil
1468 (setq ps-current-color ps-default-fg))
1469 (ps-output (format ps-color-format (nth 0 ps-current-color)
1470 (nth 1 ps-current-color) (nth 2 ps-current-color))
1471 " FG\n"))
1472
1473 (defun ps-set-underline (underline-p)
1474 (ps-output (if underline-p "true" "false") " UL\n")
1475 (setq ps-current-underline-p underline-p))
1476
1477 (defun ps-plot-region (from to font fg-color &optional bg-color underline-p)
1478
1479 (if (not (equal font ps-current-font))
1480 (ps-set-font font))
1481
1482 ;; Specify a foreground color only if one's specified and it's
1483 ;; different than the current.
1484 (if (not (equal fg-color ps-current-color))
1485 (ps-set-color fg-color))
1486
1487 (if (not (equal bg-color ps-current-bg))
1488 (ps-set-bg bg-color))
1489
1490 ;; Toggle underlining if different.
1491 (if (not (equal underline-p ps-current-underline-p))
1492 (ps-set-underline underline-p))
1493
1494 ;; Starting at the beginning of the specified region...
1495 (save-excursion
1496 (goto-char from)
1497
1498 ;; ...break the region up into chunks separated by tabs, linefeeds,
1499 ;; and pagefeeds, and plot each chunk.
1500 (while (< from to)
1501 (if (re-search-forward "[\t\n\f]" to t)
1502 (let ((match (char-after (match-beginning 0))))
1503 (cond
1504 ((= match ?\t)
1505 (let ((linestart
1506 (save-excursion (beginning-of-line) (point))))
1507 (ps-plot 'ps-basic-plot-string from (- (point) 1)
1508 bg-color)
1509 (forward-char -1)
1510 (setq from (+ linestart (current-column)))
1511 (if (re-search-forward "[ \t]+" to t)
1512 (ps-plot 'ps-basic-plot-whitespace
1513 from (+ linestart (current-column))
1514 bg-color))))
1515
1516 ((= match ?\n)
1517 (ps-plot 'ps-basic-plot-string from (- (point) 1)
1518 bg-color)
1519 (ps-next-line)
1520 )
1521
1522 ((= match ?\f)
1523 (ps-plot 'ps-basic-plot-string from (- (point) 1)
1524 bg-color)
1525 (ps-next-page)))
1526 (setq from (point)))
1527 (ps-plot 'ps-basic-plot-string from to bg-color)
1528 (setq from to)))))
1529
1530 (defun ps-color-value (x-color-value)
1531 ;; Scale 16-bit X-COLOR-VALUE to PostScript color value in [0, 1] interval.
1532 (/ x-color-value ps-print-color-scale))
1533
1534 (defun ps-color-values (x-color)
1535 (cond ((fboundp 'x-color-values)
1536 (x-color-values x-color))
1537 ((fboundp 'pixel-components)
1538 (pixel-components x-color))
1539 (t (error "No available function to determine X color values."))))
1540
1541 (defun ps-face-attributes (face)
1542 (let ((differs (face-differs-from-default-p face)))
1543 (list (memq face ps-ref-bold-faces)
1544 (memq face ps-ref-italic-faces)
1545 (memq face ps-ref-underlined-faces)
1546 (and differs (face-foreground face))
1547 (and differs (face-background face)))))
1548
1549 (defun ps-face-attribute-list (face-or-list)
1550 (if (listp face-or-list)
1551 (let (bold-p italic-p underline-p foreground background face-attr face)
1552 (while face-or-list
1553 (setq face (car face-or-list))
1554 (setq face-attr (ps-face-attributes face))
1555 (setq bold-p (or bold-p (nth 0 face-attr)))
1556 (setq italic-p (or italic-p (nth 1 face-attr)))
1557 (setq underline-p (or underline-p (nth 2 face-attr)))
1558 (if foreground
1559 nil
1560 (setq foreground (nth 3 face-attr)))
1561 (if background
1562 nil
1563 (setq background (nth 4 face-attr)))
1564 (setq face-or-list (cdr face-or-list)))
1565 (list bold-p italic-p underline-p foreground background))
1566
1567 (ps-face-attributes face-or-list)))
1568
1569 (defun ps-plot-with-face (from to face)
1570 (if face
1571 (let* ((face-attr (ps-face-attribute-list face))
1572 (bold-p (nth 0 face-attr))
1573 (italic-p (nth 1 face-attr))
1574 (underline-p (nth 2 face-attr))
1575 (foreground (nth 3 face-attr))
1576 (background (nth 4 face-attr))
1577 (fg-color (if (and ps-print-color-p foreground)
1578 (mapcar 'ps-color-value
1579 (ps-color-values foreground))
1580 ps-default-color))
1581 (bg-color (if (and ps-print-color-p background)
1582 (mapcar 'ps-color-value
1583 (ps-color-values background)))))
1584 (ps-plot-region from to
1585 (cond ((and bold-p italic-p) 3)
1586 (italic-p 2)
1587 (bold-p 1)
1588 (t 0))
1589 ; (or fg-color '(0.0 0.0 0.0))
1590 fg-color
1591 bg-color underline-p))
1592 (goto-char to)))
1593
1594
1595 (defun ps-emacs-face-kind-p (face kind kind-regex kind-list)
1596 (let ((frame-font (face-font face))
1597 (face-defaults (face-font face t)))
1598 (or
1599 ;; Check FACE defaults:
1600 (and (listp face-defaults)
1601 (memq kind face-defaults))
1602
1603 ;; Check the user's preferences
1604 (memq face kind-list))))
1605
1606 (defun ps-xemacs-face-kind-p (face kind kind-regex kind-list)
1607 (let* ((frame-font (or (face-font face) (face-font 'default)))
1608 (kind-cons (assq kind (x-font-properties frame-font)))
1609 (kind-spec (cdr-safe kind-cons))
1610 (case-fold-search t))
1611
1612 (or (and kind-spec (string-match kind-regex kind-spec))
1613 ;; Kludge-compatible:
1614 (memq face kind-list))))
1615
1616 (defun ps-face-bold-p (face)
1617 (if (eq ps-print-emacs-type 'emacs)
1618 (ps-emacs-face-kind-p face 'bold "-\\(bold\\|demibold\\)-"
1619 ps-bold-faces)
1620 (ps-xemacs-face-kind-p face 'WEIGHT_NAME "bold\\|demibold"
1621 ps-bold-faces)))
1622
1623 (defun ps-face-italic-p (face)
1624 (if (eq ps-print-emacs-type 'emacs)
1625 (ps-emacs-face-kind-p face 'italic "-[io]-" ps-italic-faces)
1626 (or
1627 (ps-xemacs-face-kind-p face 'ANGLE_NAME "i\\|o" ps-italic-faces)
1628 (ps-xemacs-face-kind-p face 'SLANT "i\\|o" ps-italic-faces))))
1629
1630 (defun ps-face-underlined-p (face)
1631 (or (face-underline-p face)
1632 (memq face ps-underlined-faces)))
1633
1634 ;; Ensure that face-list is fbound.
1635 (or (fboundp 'face-list) (defalias 'face-list 'list-faces))
1636
1637 (defun ps-build-reference-face-lists ()
1638 (if ps-auto-font-detect
1639 (let ((faces (face-list))
1640 the-face)
1641 (setq ps-ref-bold-faces nil
1642 ps-ref-italic-faces nil
1643 ps-ref-underlined-faces nil)
1644 (while faces
1645 (setq the-face (car faces))
1646 (if (ps-face-italic-p the-face)
1647 (setq ps-ref-italic-faces
1648 (cons the-face ps-ref-italic-faces)))
1649 (if (ps-face-bold-p the-face)
1650 (setq ps-ref-bold-faces
1651 (cons the-face ps-ref-bold-faces)))
1652 (if (ps-face-underlined-p the-face)
1653 (setq ps-ref-underlined-faces
1654 (cons the-face ps-ref-underlined-faces)))
1655 (setq faces (cdr faces))))
1656 (setq ps-ref-bold-faces ps-bold-faces)
1657 (setq ps-ref-italic-faces ps-italic-faces)
1658 (setq ps-ref-underlined-faces ps-underlined-faces))
1659 (setq ps-build-face-reference nil))
1660
1661 (defun ps-mapper (extent list)
1662 (nconc list (list (list (extent-start-position extent) 'push extent)
1663 (list (extent-end-position extent) 'pull extent)))
1664 nil)
1665
1666 (defun ps-sorter (a b)
1667 (< (car a) (car b)))
1668
1669 (defun ps-extent-sorter (a b)
1670 (< (extent-priority a) (extent-priority b)))
1671
1672 (defun ps-print-ensure-fontified (start end)
1673 (if (and (boundp 'lazy-lock-mode) lazy-lock-mode)
1674 (if (fboundp 'lazy-lock-fontify-region)
1675 (lazy-lock-fontify-region start end)
1676 (lazy-lock-fontify-buffer))))
1677
1678 (defun ps-generate-postscript-with-faces (from to)
1679 ;; Build the reference lists of faces if necessary.
1680 (if (or ps-always-build-face-reference
1681 ps-build-face-reference)
1682 (progn
1683 (message "Collecting face information...")
1684 (ps-build-reference-face-lists)))
1685 ;; Set the color scale. We do it here instead of in the defvar so
1686 ;; that ps-print can be dumped into emacs. This expression can't be
1687 ;; evaluated at dump-time because X isn't initialized.
1688 (setq ps-print-color-scale
1689 (if ps-print-color-p
1690 (float (car (ps-color-values "white")))
1691 1.0))
1692 ;; Generate some PostScript.
1693 (save-restriction
1694 (narrow-to-region from to)
1695 (let ((face 'default)
1696 (position to))
1697 (ps-print-ensure-fontified from to)
1698 (cond ((or (eq ps-print-emacs-type 'lucid) (eq ps-print-emacs-type 'xemacs))
1699 ;; Build the list of extents...
1700 (let ((a (cons 'dummy nil))
1701 record type extent extent-list)
1702 (map-extents 'ps-mapper nil from to a)
1703 (setq a (cdr a))
1704 (setq a (sort a 'ps-sorter))
1705
1706 (setq extent-list nil)
1707
1708 ;; Loop through the extents...
1709 (while a
1710 (setq record (car a))
1711
1712 (setq position (car record))
1713 (setq record (cdr record))
1714
1715 (setq type (car record))
1716 (setq record (cdr record))
1717
1718 (setq extent (car record))
1719
1720 ;; Plot up to this record.
1721 ;; XEmacs 19.12: for some reason, we're getting into a
1722 ;; situation in which some of the records have
1723 ;; positions less than 'from'. Since we've narrowed
1724 ;; the buffer, this'll generate errors. This is a
1725 ;; hack, but don't call ps-plot-with-face unless from >
1726 ;; point-min.
1727 (if (and (>= from (point-min))
1728 (<= position (point-max)))
1729 (ps-plot-with-face from position face))
1730
1731 (cond
1732 ((eq type 'push)
1733 (if (extent-face extent)
1734 (setq extent-list (sort (cons extent extent-list)
1735 'ps-extent-sorter))))
1736
1737 ((eq type 'pull)
1738 (setq extent-list (sort (delq extent extent-list)
1739 'ps-extent-sorter))))
1740
1741 (setq face
1742 (if extent-list
1743 (extent-face (car extent-list))
1744 'default))
1745
1746 (setq from position)
1747 (setq a (cdr a)))))
1748
1749 ((eq ps-print-emacs-type 'emacs)
1750 (let ((property-change from)
1751 (overlay-change from))
1752 (while (< from to)
1753 (if (< property-change to) ; Don't search for property change
1754 ; unless previous search succeeded.
1755 (setq property-change
1756 (next-property-change from nil to)))
1757 (if (< overlay-change to) ; Don't search for overlay change
1758 ; unless previous search succeeded.
1759 (setq overlay-change
1760 (min (next-overlay-change from) to)))
1761 (setq position
1762 (min property-change overlay-change))
1763 (setq face
1764 (cond ((get-text-property from 'invisible) nil)
1765 ((get-text-property from 'face))
1766 (t 'default)))
1767 (let ((overlays (overlays-at from))
1768 (face-priority -1)) ; text-property
1769 (while overlays
1770 (let* ((overlay (car overlays))
1771 (overlay-face (overlay-get overlay 'face))
1772 (overlay-invisible (overlay-get overlay 'invisible))
1773 (overlay-priority (or (overlay-get overlay
1774 'priority)
1775 0)))
1776 (if (and (or overlay-invisible overlay-face)
1777 (> overlay-priority face-priority))
1778 (setq face (cond (overlay-invisible nil)
1779 ((and face overlay-face)))
1780 face-priority overlay-priority)))
1781 (setq overlays (cdr overlays))))
1782 ;; Plot up to this record.
1783 (ps-plot-with-face from position face)
1784 (setq from position)))))
1785 (ps-plot-with-face from to face))))
1786
1787 (defun ps-generate-postscript (from to)
1788 (ps-plot-region from to 0 nil))
1789
1790 (defun ps-generate (buffer from to genfunc)
1791 (let ((from (min to from))
1792 (to (max to from)))
1793 (save-restriction
1794 (narrow-to-region from to)
1795 (if ps-razzle-dazzle
1796 (message "Formatting...%d%%" (setq ps-razchunk 0)))
1797 (set-buffer buffer)
1798 (setq ps-source-buffer buffer)
1799 (setq ps-spool-buffer (get-buffer-create ps-spool-buffer-name))
1800 (ps-init-output-queue)
1801 (let (safe-marker completed-safely needs-begin-file)
1802 (unwind-protect
1803 (progn
1804 (set-buffer ps-spool-buffer)
1805
1806 ;; Get a marker and make it point to the current end of the
1807 ;; buffer, If an error occurs, we'll delete everything from
1808 ;; the end of this marker onwards.
1809 (setq safe-marker (make-marker))
1810 (set-marker safe-marker (point-max))
1811
1812 (goto-char (point-min))
1813 (if (looking-at (regexp-quote "%!PS-Adobe-1.0"))
1814 nil
1815 (setq needs-begin-file t))
1816 (save-excursion
1817 (set-buffer ps-source-buffer)
1818 (if needs-begin-file (ps-begin-file))
1819 (ps-begin-job)
1820 (ps-begin-page))
1821 (set-buffer ps-source-buffer)
1822 (funcall genfunc from to)
1823 (ps-end-page)
1824
1825 (if (and ps-spool-duplex
1826 (= (mod ps-page-count 2) 1))
1827 (ps-dummy-page))
1828 (ps-flush-output)
1829
1830 ;; Back to the PS output buffer to set the page count
1831 (set-buffer ps-spool-buffer)
1832 (goto-char (point-max))
1833 (while (re-search-backward "^/PageCount 0 def$" nil t)
1834 (replace-match (format "/PageCount %d def" ps-page-count) t))
1835
1836 ;; Setting this variable tells the unwind form that the
1837 ;; the postscript was generated without error.
1838 (setq completed-safely t))
1839
1840 ;; Unwind form: If some bad mojo occurred while generating
1841 ;; postscript, delete all the postscript that was generated.
1842 ;; This protects the previously spooled files from getting
1843 ;; corrupted.
1844 (if (and (markerp safe-marker) (not completed-safely))
1845 (progn
1846 (set-buffer ps-spool-buffer)
1847 (delete-region (marker-position safe-marker) (point-max))))))
1848
1849 (if ps-razzle-dazzle
1850 (message "Formatting...done")))))
1851
1852 (defun ps-do-despool (filename)
1853 (if (or (not (boundp 'ps-spool-buffer))
1854 (not ps-spool-buffer))
1855 (message "No spooled PostScript to print")
1856 (ps-end-file)
1857 (ps-flush-output)
1858 (if filename
1859 (save-excursion
1860 (if ps-razzle-dazzle
1861 (message "Saving..."))
1862 (set-buffer ps-spool-buffer)
1863 (setq filename (expand-file-name filename))
1864 (write-region (point-min) (point-max) filename)
1865 (if ps-razzle-dazzle
1866 (message "Wrote %s" filename)))
1867 ;; Else, spool to the printer
1868 (if ps-razzle-dazzle
1869 (message "Printing..."))
1870 (save-excursion
1871 (set-buffer ps-spool-buffer)
1872 (if (eq system-type 'ms-dos)
1873 (write-region (point-min) (point-max) "PRN")
1874 (apply 'call-process-region
1875 (point-min) (point-max) ps-lpr-command nil 0 nil
1876 ps-lpr-switches)))
1877 (if ps-razzle-dazzle
1878 (message "Printing...done")))
1879 (kill-buffer ps-spool-buffer)))
1880
1881 (defun ps-kill-emacs-check ()
1882 (let (ps-buffer)
1883 (if (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
1884 (buffer-modified-p ps-buffer))
1885 (if (y-or-n-p "Unprinted PostScript waiting; print now? ")
1886 (ps-despool)))
1887 (if (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
1888 (buffer-modified-p ps-buffer))
1889 (if (yes-or-no-p "Unprinted PostScript waiting; exit anyway? ")
1890 nil
1891 (error "Unprinted PostScript")))))
1892
1893 (if (fboundp 'add-hook)
1894 (add-hook 'kill-emacs-hook 'ps-kill-emacs-check)
1895 (if kill-emacs-hook
1896 (message "Won't override existing kill-emacs-hook")
1897 (setq kill-emacs-hook 'ps-kill-emacs-check)))
1898
1899 ;;; Sample Setup Code:
1900
1901 ;; This stuff is for anybody that's brave enough to look this far,
1902 ;; and able to figure out how to use it. It isn't really part of ps-
1903 ;; print, but I'll leave it here in hopes it might be useful:
1904
1905 ;; WARNING!!! The following code is *sample* code only. Don't use it
1906 ;; unless you understand what it does!
1907
1908 (defmacro ps-prsc () (list 'if (list 'eq 'ps-print-emacs-type ''emacs)
1909 [f22] ''f22))
1910 (defmacro ps-c-prsc () (list 'if (list 'eq 'ps-print-emacs-type ''emacs)
1911 [C-f22]
1912 ''(control f22)))
1913 (defmacro ps-s-prsc () (list 'if (list 'eq 'ps-print-emacs-type ''emacs)
1914 [S-f22]
1915 ''(shift f22)))
1916
1917 ;; Look in an article or mail message for the Subject: line. To be
1918 ;; placed in ps-left-headers.
1919 (defun ps-article-subject ()
1920 (save-excursion
1921 (goto-char (point-min))
1922 (if (re-search-forward "^Subject:[ \t]+\\(.*\\)$")
1923 (buffer-substring (match-beginning 1) (match-end 1))
1924 "Subject ???")))
1925
1926 ;; Look in an article or mail message for the From: line. Sorta-kinda
1927 ;; understands RFC-822 addresses and can pull the real name out where
1928 ;; it's provided. To be placed in ps-left-headers.
1929 (defun ps-article-author ()
1930 (save-excursion
1931 (goto-char (point-min))
1932 (if (re-search-forward "^From:[ \t]+\\(.*\\)$")
1933 (let ((fromstring (buffer-substring (match-beginning 1) (match-end 1))))
1934 (cond
1935
1936 ;; Try first to match addresses that look like
1937 ;; thompson@wg2.waii.com (Jim Thompson)
1938 ((string-match ".*[ \t]+(\\(.*\\))" fromstring)
1939 (substring fromstring (match-beginning 1) (match-end 1)))
1940
1941 ;; Next try to match addresses that look like
1942 ;; Jim Thompson <thompson@wg2.waii.com>
1943 ((string-match "\\(.*\\)[ \t]+<.*>" fromstring)
1944 (substring fromstring (match-beginning 1) (match-end 1)))
1945
1946 ;; Couldn't find a real name -- show the address instead.
1947 (t fromstring)))
1948 "From ???")))
1949
1950 ;; A hook to bind to gnus-Article-prepare-hook. This will set the ps-
1951 ;; left-headers specially for gnus articles. Unfortunately, gnus-
1952 ;; article-mode-hook is called only once, the first time the *Article*
1953 ;; buffer enters that mode, so it would only work for the first time
1954 ;; we ran gnus. The second time, this hook wouldn't get set up. The
1955 ;; only alternative is gnus-article-prepare-hook.
1956 (defun ps-gnus-article-prepare-hook ()
1957 (setq ps-header-lines 3)
1958 (setq ps-left-header
1959 ;; The left headers will display the article's subject, its
1960 ;; author, and the newsgroup it was in.
1961 (list 'ps-article-subject 'ps-article-author 'gnus-newsgroup-name)))
1962
1963 ;; A hook to bind to vm-mode-hook to locally bind prsc and set the ps-
1964 ;; left-headers specially for mail messages. This header setup would
1965 ;; also work, I think, for RMAIL.
1966 (defun ps-vm-mode-hook ()
1967 (local-set-key (ps-prsc) 'ps-vm-print-message-from-summary)
1968 (setq ps-header-lines 3)
1969 (setq ps-left-header
1970 ;; The left headers will display the message's subject, its
1971 ;; author, and the name of the folder it was in.
1972 (list 'ps-article-subject 'ps-article-author 'buffer-name)))
1973
1974 ;; Every now and then I forget to switch from the *Summary* buffer to
1975 ;; the *Article* before hitting prsc, and a nicely formatted list of
1976 ;; article subjects shows up at the printer. This function, bound to
1977 ;; prsc for the gnus *Summary* buffer means I don't have to switch
1978 ;; buffers first.
1979 (defun ps-gnus-print-article-from-summary ()
1980 (interactive)
1981 (if (get-buffer "*Article*")
1982 (save-excursion
1983 (set-buffer "*Article*")
1984 (ps-spool-buffer-with-faces))))
1985
1986 ;; See ps-gnus-print-article-from-summary. This function does the
1987 ;; same thing for vm.
1988 (defun ps-vm-print-message-from-summary ()
1989 (interactive)
1990 (if vm-mail-buffer
1991 (save-excursion
1992 (set-buffer vm-mail-buffer)
1993 (ps-spool-buffer-with-faces))))
1994
1995 ;; A hook to bind to bind to gnus-summary-setup-buffer to locally bind
1996 ;; prsc.
1997 (defun ps-gnus-summary-setup ()
1998 (local-set-key (ps-prsc) 'ps-gnus-print-article-from-summary))
1999
2000 ;; Look in an article or mail message for the Subject: line. To be
2001 ;; placed in ps-left-headers.
2002 (defun ps-info-file ()
2003 (save-excursion
2004 (goto-char (point-min))
2005 (if (re-search-forward "File:[ \t]+\\([^, \t\n]*\\)")
2006 (buffer-substring (match-beginning 1) (match-end 1))
2007 "File ???")))
2008
2009 ;; Look in an article or mail message for the Subject: line. To be
2010 ;; placed in ps-left-headers.
2011 (defun ps-info-node ()
2012 (save-excursion
2013 (goto-char (point-min))
2014 (if (re-search-forward "Node:[ \t]+\\([^,\t\n]*\\)")
2015 (buffer-substring (match-beginning 1) (match-end 1))
2016 "Node ???")))
2017
2018 (defun ps-info-mode-hook ()
2019 (setq ps-left-header
2020 ;; The left headers will display the node name and file name.
2021 (list 'ps-info-node 'ps-info-file)))
2022
2023 ;; WARNING! The following function is a *sample* only, and is *not*
2024 ;; meant to be used as a whole unless you understand what the effects
2025 ;; will be! (In fact, this is a copy if my setup for ps-print -- I'd
2026 ;; be very surprised if it was useful to *anybody*, without
2027 ;; modification.)
2028
2029 (defun ps-jts-ps-setup ()
2030 (global-set-key (ps-prsc) 'ps-spool-buffer-with-faces) ;f22 is prsc
2031 (global-set-key (ps-s-prsc) 'ps-spool-region-with-faces)
2032 (global-set-key (ps-c-prsc) 'ps-despool)
2033 (add-hook 'gnus-article-prepare-hook 'ps-gnus-article-prepare-hook)
2034 (add-hook 'gnus-summary-mode-hook 'ps-gnus-summary-setup)
2035 (add-hook 'vm-mode-hook 'ps-vm-mode-hook)
2036 (add-hook 'vm-mode-hooks 'ps-vm-mode-hook)
2037 (add-hook 'Info-mode-hook 'ps-info-mode-hook)
2038 (setq ps-spool-duplex t)
2039 (setq ps-print-color-p nil)
2040 (setq ps-lpr-command "lpr")
2041 (setq ps-lpr-switches '("-Jjct,duplex_long")))
2042
2043 (provide 'ps-print)
2044
2045 ;;; ps-print.el ends here