]> code.delx.au - gnu-emacs/blob - lisp/ps-print.el
Some comment and doc fixes.
[gnu-emacs] / lisp / ps-print.el
1 ;;; ps-print.el --- Print text from the buffer as PostScript
2
3 ;; Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Jim Thompson (was <thompson@wg2.waii.com>)
6 ;; Author: Jacques Duthen <duthen@cegelec-red.fr>
7 ;; Maintainer: Vinicius Jose Latorre <vinicius@cpqd.br>
8 ;; Keywords: print, PostScript
9 ;; Time-stamp: <97/08/27 13:00:37 vinicius>
10 ;; Version: 3.05.1
11
12 (defconst ps-print-version "3.05.1"
13 "ps-print.el, v 3.05.1 <97/08/24 vinicius>
14
15 Vinicius's last change version -- this file may have been edited as part of
16 Emacs without changes to the version number. When reporting bugs,
17 please also report the version of Emacs, if any, that ps-print was
18 distributed with.
19
20 Please send all bug fixes and enhancements to
21 Jacques Duthen <duthen@cegelec-red.fr>.
22 ")
23
24 ;; This file is part of GNU Emacs.
25
26 ;; GNU Emacs is free software; you can redistribute it and/or modify
27 ;; it under the terms of the GNU General Public License as published by
28 ;; the Free Software Foundation; either version 2, or (at your option)
29 ;; any later version.
30
31 ;; GNU Emacs is distributed in the hope that it will be useful,
32 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
33 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 ;; GNU General Public License for more details.
35
36 ;; You should have received a copy of the GNU General Public License
37 ;; along with GNU Emacs; see the file COPYING. If not, write to the
38 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
39 ;; Boston, MA 02111-1307, USA.
40
41 ;;; Commentary:
42
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;;
45 ;; About ps-print
46 ;; --------------
47 ;;
48 ;; This package provides printing of Emacs buffers on PostScript
49 ;; printers; the buffer's bold and italic text attributes are
50 ;; preserved in the printer output. Ps-print is intended for use with
51 ;; Emacs 19 or Lucid Emacs, together with a fontifying package such as
52 ;; font-lock or hilit.
53 ;;
54 ;; ps-print uses the same face attributes defined through font-lock or hilit
55 ;; to print a PostScript file, but some faces are better seeing on the screen
56 ;; than on paper, specially when you have a black/white PostScript printer.
57 ;;
58 ;; ps-print allows a remap of face to another one that it is better to print,
59 ;; for example, the face font-lock-comment-face (if you are using font-lock)
60 ;; could have bold or italic attribute when printing, besides foreground color.
61 ;; This remap improves printing look (see How Ps-Print Maps Faces).
62 ;;
63 ;;
64 ;; Using ps-print
65 ;; --------------
66 ;;
67 ;; The Commands
68 ;;
69 ;; Ps-print provides eight commands for generating PostScript images
70 ;; of Emacs buffers:
71 ;;
72 ;; ps-print-buffer
73 ;; ps-print-buffer-with-faces
74 ;; ps-print-region
75 ;; ps-print-region-with-faces
76 ;; ps-spool-buffer
77 ;; ps-spool-buffer-with-faces
78 ;; ps-spool-region
79 ;; ps-spool-region-with-faces
80 ;;
81 ;; These commands all perform essentially the same function: they
82 ;; generate PostScript images suitable for printing on a PostScript
83 ;; printer or displaying with GhostScript. These commands are
84 ;; collectively referred to as "ps-print- commands".
85 ;;
86 ;; The word "print" or "spool" in the command name determines when the
87 ;; PostScript image is sent to the printer:
88 ;;
89 ;; print - The PostScript image is immediately sent to the
90 ;; printer;
91 ;;
92 ;; spool - The PostScript image is saved temporarily in an
93 ;; Emacs buffer. Many images may be spooled locally
94 ;; before printing them. To send the spooled images
95 ;; to the printer, use the command `ps-despool'.
96 ;;
97 ;; The spooling mechanism was designed for printing lots of small
98 ;; files (mail messages or netnews articles) to save paper that would
99 ;; otherwise be wasted on banner pages, and to make it easier to find
100 ;; your output at the printer (it's easier to pick up one 50-page
101 ;; printout than to find 50 single-page printouts).
102 ;;
103 ;; Ps-print has a hook in the `kill-emacs-hooks' so that you won't
104 ;; accidentally quit from Emacs while you have unprinted PostScript
105 ;; waiting in the spool buffer. If you do attempt to exit with
106 ;; spooled PostScript, you'll be asked if you want to print it, and if
107 ;; you decline, you'll be asked to confirm the exit; this is modeled
108 ;; on the confirmation that Emacs uses for modified buffers.
109 ;;
110 ;; The word "buffer" or "region" in the command name determines how
111 ;; much of the buffer is printed:
112 ;;
113 ;; buffer - Print the entire buffer.
114 ;;
115 ;; region - Print just the current region.
116 ;;
117 ;; The -with-faces suffix on the command name means that the command
118 ;; will include font, color, and underline information in the
119 ;; PostScript image, so the printed image can look as pretty as the
120 ;; buffer. The ps-print- commands without the -with-faces suffix
121 ;; don't include font, color, or underline information; images printed
122 ;; with these commands aren't as pretty, but are faster to generate.
123 ;;
124 ;; Two ps-print- command examples:
125 ;;
126 ;; ps-print-buffer - print the entire buffer,
127 ;; without font, color, or
128 ;; underline information, and
129 ;; send it immediately to the
130 ;; printer.
131 ;;
132 ;; ps-spool-region-with-faces - print just the current region;
133 ;; include font, color, and
134 ;; underline information, and
135 ;; spool the image in Emacs to
136 ;; send to the printer later.
137 ;;
138 ;;
139 ;; Invoking Ps-Print
140 ;; -----------------
141 ;;
142 ;; To print your buffer, type
143 ;;
144 ;; M-x ps-print-buffer
145 ;;
146 ;; or substitute one of the other seven ps-print- commands. The
147 ;; command will generate the PostScript image and print or spool it as
148 ;; specified. By giving the command a prefix argument
149 ;;
150 ;; C-u M-x ps-print-buffer
151 ;;
152 ;; it will save the PostScript image to a file instead of sending it
153 ;; to the printer; you will be prompted for the name of the file to
154 ;; save the image to. The prefix argument is ignored by the commands
155 ;; that spool their images, but you may save the spooled images to a
156 ;; file by giving a prefix argument to `ps-despool':
157 ;;
158 ;; C-u M-x ps-despool
159 ;;
160 ;; When invoked this way, `ps-despool' will prompt you for the name of
161 ;; the file to save to.
162 ;;
163 ;; Any of the `ps-print-' commands can be bound to keys; I recommend
164 ;; binding `ps-spool-buffer-with-faces', `ps-spool-region-with-faces',
165 ;; and `ps-despool'. Here are the bindings I use on my Sun 4 keyboard:
166 ;;
167 ;; (global-set-key 'f22 'ps-spool-buffer-with-faces) ;f22 is prsc
168 ;; (global-set-key '(shift f22) 'ps-spool-region-with-faces)
169 ;; (global-set-key '(control f22) 'ps-despool)
170 ;;
171 ;;
172 ;; The Printer Interface
173 ;; ---------------------
174 ;;
175 ;; The variables `ps-lpr-command' and `ps-lpr-switches' determine what
176 ;; command is used to send the PostScript images to the printer, and
177 ;; what arguments to give the command. These are analogous to
178 ;; `lpr-command' and `lpr-switches'.
179 ;;
180 ;; Make sure that they contain appropriate values for your system;
181 ;; see the usage notes below and the documentation of these variables.
182 ;;
183 ;; NOTE: `ps-lpr-command' and `ps-lpr-switches' take their initial values
184 ;; from the variables `lpr-command' and `lpr-switches'. If you have
185 ;; `lpr-command' set to invoke a pretty-printer such as `enscript',
186 ;; then ps-print won't work properly. `ps-lpr-command' must name
187 ;; a program that does not format the files it prints.
188 ;;
189 ;;
190 ;; The Page Layout
191 ;; ---------------
192 ;;
193 ;; All dimensions are floats in PostScript points.
194 ;; 1 inch == 2.54 cm == 72 points
195 ;; 1 cm == (/ 1 2.54) inch == (/ 72 2.54) points
196 ;;
197 ;; The variable `ps-paper-type' determines the size of paper ps-print
198 ;; formats for; it should contain one of the symbols:
199 ;; `a4' `a3' `letter' `legal' `letter-small' `tabloid'
200 ;; `ledger' `statement' `executive' `a4small' `b4' `b5'
201 ;;
202 ;; The variable `ps-landscape-mode' determines the orientation
203 ;; of the printing on the page:
204 ;; nil means `portrait' mode, non-nil means `landscape' mode.
205 ;; There is no oblique mode yet, though this is easy to do in ps.
206 ;;
207 ;; In landscape mode, the text is NOT scaled: you may print 70 lines
208 ;; in portrait mode and only 50 lignes in landscape mode.
209 ;; The margins represent margins in the printed paper:
210 ;; the top margin is the margin between the top of the page
211 ;; and the printed header, whatever the orientation is.
212 ;;
213 ;; The variable `ps-number-of-columns' determines the number of columns
214 ;; both in landscape and portrait mode.
215 ;; You can use:
216 ;; - (the standard) one column portrait mode
217 ;; - (my favorite) two columns landscape mode (which spares trees)
218 ;; but also
219 ;; - one column landscape mode for files with very long lines.
220 ;; - multi-column portrait or landscape mode
221 ;;
222 ;;
223 ;; Horizontal layout
224 ;; -----------------
225 ;;
226 ;; The horizontal layout is determined by the variables
227 ;; `ps-left-margin' `ps-inter-column' `ps-right-margin'
228 ;; as follows:
229 ;;
230 ;; ------------------------------------------
231 ;; | | | | | | | |
232 ;; | lm | text | ic | text | ic | text | rm |
233 ;; | | | | | | | |
234 ;; ------------------------------------------
235 ;;
236 ;; If `ps-number-of-columns' is 1, `ps-inter-column' is not relevant.
237 ;; Usually, lm = rm > 0 and ic = lm
238 ;; If (ic < 0), the text of adjacent columns can overlap.
239 ;;
240 ;;
241 ;; Vertical layout
242 ;; ---------------
243 ;;
244 ;; The vertical layout is determined by the variables
245 ;; `ps-bottom-margin' `ps-top-margin' `ps-header-offset'
246 ;; as follows:
247 ;;
248 ;; |--------| |--------|
249 ;; | tm | | tm |
250 ;; |--------| |--------|
251 ;; | header | | |
252 ;; |--------| | |
253 ;; | ho | | |
254 ;; |--------| or | text |
255 ;; | | | |
256 ;; | text | | |
257 ;; | | | |
258 ;; |--------| |--------|
259 ;; | bm | | bm |
260 ;; |--------| |--------|
261 ;;
262 ;; If `ps-print-header' is nil, `ps-header-offset' is not relevant.
263 ;; The margins represent margins in the printed paper:
264 ;; the top margin is the margin between the top of the page
265 ;; and the printed header, whatever the orientation is.
266 ;;
267 ;;
268 ;; Headers
269 ;; -------
270 ;;
271 ;; Ps-print can print headers at the top of each column; the default
272 ;; headers contain the following four items: on the left, the name of
273 ;; the buffer and, if the buffer is visiting a file, the file's
274 ;; directory; on the right, the page number and date of printing.
275 ;; The default headers look something like this:
276 ;;
277 ;; ps-print.el 1/21
278 ;; /home/jct/emacs-lisp/ps/new 94/12/31
279 ;;
280 ;; When printing on duplex printers, left and right are reversed so
281 ;; that the page numbers are toward the outside (cf. `ps-spool-duplex').
282 ;;
283 ;; Headers are configurable:
284 ;; To turn them off completely, set `ps-print-header' to nil.
285 ;; To turn off the header's gaudy framing box,
286 ;; set `ps-print-header-frame' to nil.
287 ;;
288 ;; The font family and size of text in the header are determined
289 ;; by the variables `ps-header-font-family', `ps-header-font-size' and
290 ;; `ps-header-title-font-size' (see below).
291 ;;
292 ;; The variable `ps-header-line-pad' determines the portion of a header
293 ;; title line height to insert between the header frame and the text
294 ;; it contains, both in the vertical and horizontal directions:
295 ;; .5 means half a line.
296
297 ;; Page numbers are printed in `n/m' format, indicating page n of m pages;
298 ;; to omit the total page count and just print the page number,
299 ;; set `ps-show-n-of-n' to nil.
300 ;;
301 ;; The amount of information in the header can be changed by changing
302 ;; the number of lines. To show less, set `ps-header-lines' to 1, and
303 ;; the header will show only the buffer name and page number. To show
304 ;; more, set `ps-header-lines' to 3, and the header will show the time of
305 ;; printing below the date.
306 ;;
307 ;; To change the content of the headers, change the variables
308 ;; `ps-left-header' and `ps-right-header'.
309 ;; These variables are lists, specifying top-to-bottom the text
310 ;; to display on the left or right side of the header.
311 ;; Each element of the list should be a string or a symbol.
312 ;; Strings are inserted directly into the PostScript arrays,
313 ;; and should contain the PostScript string delimiters '(' and ')'.
314 ;;
315 ;; Symbols in the header format lists can either represent functions
316 ;; or variables. Functions are called, and should return a string to
317 ;; show in the header. Variables should contain strings to display in
318 ;; the header. In either case, function or variable, the PostScript
319 ;; string delimiters are added by ps-print, and should not be part of
320 ;; the returned value.
321 ;;
322 ;; Here's an example: say we want the left header to display the text
323 ;;
324 ;; Moe
325 ;; Larry
326 ;; Curly
327 ;;
328 ;; where we have a function to return "Moe"
329 ;;
330 ;; (defun moe-func ()
331 ;; "Moe")
332 ;;
333 ;; a variable specifying "Larry"
334 ;;
335 ;; (setq larry-var "Larry")
336 ;;
337 ;; and a literal for "Curly". Here's how `ps-left-header' should be
338 ;; set:
339 ;;
340 ;; (setq ps-left-header (list 'moe-func 'larry-var "(Curly)"))
341 ;;
342 ;; Note that Curly has the PostScript string delimiters inside his
343 ;; quotes -- those aren't misplaced lisp delimiters!
344 ;;
345 ;; Without them, PostScript would attempt to call the undefined
346 ;; function Curly, which would result in a PostScript error.
347 ;;
348 ;; Since most printers don't report PostScript errors except by
349 ;; aborting the print job, this kind of error can be hard to track down.
350 ;;
351 ;; Consider yourself warned!
352 ;;
353 ;;
354 ;; Duplex Printers
355 ;; ---------------
356 ;;
357 ;; If you have a duplex-capable printer (one that prints both sides of
358 ;; the paper), set `ps-spool-duplex' to t.
359 ;; Ps-print will insert blank pages to make sure each buffer starts
360 ;; on the correct side of the paper.
361 ;; Don't forget to set `ps-lpr-switches' to select duplex printing
362 ;; for your printer.
363 ;;
364 ;;
365 ;; Line Number
366 ;; -----------
367 ;;
368 ;; The variable `ps-line-number' specifies whether to number each line;
369 ;; non-nil means do so. The default is nil (don't number each line).
370 ;;
371 ;;
372 ;; Zebra Stripes
373 ;; -------------
374 ;;
375 ;; Zebra stripes are a kind of background that appear "underneath" the text
376 ;; and can make the text easier to read. They look like this:
377 ;;
378 ;; XXXXXXXXXXXXXXXXXXXXXXXX
379 ;; XXXXXXXXXXXXXXXXXXXXXXXX
380 ;; XXXXXXXXXXXXXXXXXXXXXXXX
381 ;;
382 ;;
383 ;;
384 ;; XXXXXXXXXXXXXXXXXXXXXXXX
385 ;; XXXXXXXXXXXXXXXXXXXXXXXX
386 ;; XXXXXXXXXXXXXXXXXXXXXXXX
387 ;;
388 ;; The X's here represent rectangles filled with a light gray color.
389 ;; Each rectangle extends all the way across the page.
390 ;;
391 ;; The height, in lines, of each rectangle is controlled by
392 ;; the variable `ps-zebra-stripe-height', which is 3 by default.
393 ;; The distance between stripes equals the height of a stripe.
394 ;;
395 ;; The variable `ps-zebra-stripe' controls whether to print zebra stripes.
396 ;; Non-nil means yes, nil means no. The default is nil.
397 ;;
398 ;; See also section How Ps-Print Has A Text And/Or Image On Background.
399 ;;
400 ;;
401 ;; Font managing
402 ;; -------------
403 ;;
404 ;; Ps-print now knows rather precisely some fonts:
405 ;; the variable `ps-font-info-database' contains information
406 ;; for a list of font families (currently mainly `Courier' `Helvetica'
407 ;; `Times' `Palatino' `Helvetica-Narrow' `NewCenturySchlbk').
408 ;; Each font family contains the font names for standard, bold, italic
409 ;; and bold-italic characters, a reference size (usually 10) and the
410 ;; corresponding line height, width of a space and average character width.
411 ;;
412 ;; The variable `ps-font-family' determines which font family
413 ;; is to be used for ordinary text.
414 ;; If its value does not correspond to a known font family,
415 ;; an error message is printed into the `*Messages*' buffer,
416 ;; which lists the currently available font families.
417 ;;
418 ;; The variable `ps-font-size' determines the size (in points)
419 ;; of the font for ordinary text, when generating Postscript.
420 ;; Its value is a float.
421 ;;
422 ;; Similarly, the variable `ps-header-font-family' determines
423 ;; which font family is to be used for text in the header.
424 ;; The variable `ps-header-font-size' determines the font size,
425 ;; in points, for text in the header.
426 ;; The variable `ps-header-title-font-size' determines the font size,
427 ;; in points, for the top line of text in the header.
428 ;;
429 ;;
430 ;; Adding a new font family
431 ;; ------------------------
432 ;;
433 ;; To use a new font family, you MUST first teach ps-print
434 ;; this font, i.e., add its information to `ps-font-info-database',
435 ;; otherwise ps-print cannot correctly place line and page breaks.
436 ;;
437 ;; For example, assuming `Helvetica' is unknown,
438 ;; you first need to do the following ONLY ONCE:
439 ;;
440 ;; - create a new buffer
441 ;; - generate the PostScript image to a file (C-u M-x ps-print-buffer)
442 ;; - open this file and find the line:
443 ;; `% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage'
444 ;; - delete the leading `%' (which is the PostScript comment character)
445 ;; - replace in this line `Courier' by the new font (say `Helvetica')
446 ;; to get the line:
447 ;; `3 cm 20 cm moveto 10 /Helvetica ReportFontInfo showpage'
448 ;; - send this file to the printer (or to ghostscript).
449 ;; You should read the following on the output page:
450 ;;
451 ;; For Helvetica 10 point, the line height is 11.56, the space width is 2.78
452 ;; and a crude estimate of average character width is 5.09243
453 ;;
454 ;; - Add these values to the `ps-font-info-database':
455 ;; (setq ps-font-info-database
456 ;; (append
457 ;; '((Helvetica ; the family name
458 ;; "Helvetica" "Helvetica-Bold" "Helvetica-Oblique" "Helvetica-BoldOblique"
459 ;; 10.0 11.56 2.78 5.09243))
460 ;; ps-font-info-database))
461 ;; - Now you can use this font family with any size:
462 ;; (setq ps-font-family 'Helvetica)
463 ;; - if you want to use this family in another emacs session, you must
464 ;; put into your `~/.emacs':
465 ;; (require 'ps-print)
466 ;; (setq ps-font-info-database (append ...)))
467 ;; if you don't want to load ps-print, you have to copy the whole value:
468 ;; (setq ps-font-info-database '(<your stuff> <the standard stuff>))
469 ;; or, if you can wait until the `ps-print-hook' is implemented, do:
470 ;; (add-hook 'ps-print-hook '(setq ps-font-info-database (append ...)))
471 ;; This does not work yet, since there is no `ps-print-hook' yet.
472 ;;
473 ;; You can create new `mixed' font families like:
474 ;; (my-mixed-family
475 ;; "Courier-Bold" "Helvetica"
476 ;; "Zapf-Chancery-MediumItalic" "NewCenturySchlbk-BoldItalic"
477 ;; 10.0 10.55 6.0 6.0)
478 ;; Now you can use your new font family with any size:
479 ;; (setq ps-font-family 'my-mixed-family)
480 ;;
481 ;; You can get information on all the fonts resident in YOUR printer
482 ;; by uncommenting the line:
483 ;; % 3 cm 20 cm moveto ReportAllFontInfo showpage
484 ;;
485 ;; The PostScript file should be sent to YOUR PostScript printer.
486 ;; If you send it to ghostscript or to another PostScript printer,
487 ;; you may get slightly different results.
488 ;; Anyway, as ghostscript fonts are autoload, you won't get
489 ;; much font info.
490 ;;
491 ;;
492 ;; How Ps-Print Deals With Faces
493 ;; -----------------------------
494 ;;
495 ;; The ps-print-*-with-faces commands attempt to determine which faces
496 ;; should be printed in bold or italic, but their guesses aren't
497 ;; always right. For example, you might want to map colors into faces
498 ;; so that blue faces print in bold, and red faces in italic.
499 ;;
500 ;; It is possible to force ps-print to consider specific faces bold or
501 ;; italic, no matter what font they are displayed in, by setting the
502 ;; variables `ps-bold-faces' and `ps-italic-faces'. These variables
503 ;; contain lists of faces that ps-print should consider bold or
504 ;; italic; to set them, put code like the following into your .emacs
505 ;; file:
506 ;;
507 ;; (setq ps-bold-faces '(my-blue-face))
508 ;; (setq ps-italic-faces '(my-red-face))
509 ;;
510 ;; Faces like bold-italic that are both bold and italic should go in
511 ;; *both* lists.
512 ;;
513 ;; Ps-print keeps internal lists of which fonts are bold and which are
514 ;; italic; these lists are built the first time you invoke ps-print.
515 ;; For the sake of efficiency, the lists are built only once; the same
516 ;; lists are referred in later invocations of ps-print.
517 ;;
518 ;; Because these lists are built only once, it's possible for them to
519 ;; get out of sync, if a face changes, or if new faces are added. To
520 ;; get the lists back in sync, you can set the variable
521 ;; `ps-build-face-reference' to t, and the lists will be rebuilt the
522 ;; next time ps-print is invoked.
523 ;;
524 ;;
525 ;; How Ps-Print Deals With Color
526 ;; -----------------------------
527 ;;
528 ;; Ps-print detects faces with foreground and background colors
529 ;; defined and embeds color information in the PostScript image.
530 ;; The default foreground and background colors are defined by the
531 ;; variables `ps-default-fg' and `ps-default-bg'.
532 ;; On black-and-white printers, colors are displayed in grayscale.
533 ;; To turn off color output, set `ps-print-color-p' to nil.
534 ;;
535 ;;
536 ;; How Ps-Print Maps Faces
537 ;; -----------------------
538 ;;
539 ;; As ps-print uses PostScript to print buffers, it is possible to have
540 ;; other attributes associated with faces. So the new attributes used
541 ;; by ps-print are:
542 ;;
543 ;; strikeout - like underline, but the line is in middle of text.
544 ;; overline - like underline, but the line is over the text.
545 ;; shadow - text will have a shadow.
546 ;; box - text will be surrounded by a box.
547 ;; outline - print characters as hollow outlines.
548 ;;
549 ;; See the documentation for `ps-extend-face' and `ps-extend-face-list'.
550 ;;
551 ;; Let's, for example, remap font-lock-keyword-face to another foreground color
552 ;; and bold attribute:
553 ;;
554 ;; (ps-extend-face '(font-lock-keyword-face "RoyalBlue" nil bold) 'MERGE)
555 ;;
556 ;; If you want to use a new face, define it first with `defface',
557 ;; and then call `ps-extend-face' to specify how to print it.
558 ;;
559 ;; NOTE: the only face attributes that have an effect are bold, italic and
560 ;; underline. All other attributes are ignored.
561 ;;
562 ;;
563 ;; How Ps-Print Has A Text And/Or Image On Background
564 ;; --------------------------------------------------
565 ;;
566 ;; Ps-print can print texts and/or EPS PostScript images on background; it is
567 ;; possible to define the following text attributes: font name, font size,
568 ;; initial position, angle, gray scale and pages to print.
569 ;;
570 ;; It has the following EPS PostScript images attributes: file name containing
571 ;; the image, initial position, X and Y scales, angle and pages to print.
572 ;;
573 ;; See documentation for `ps-print-background-text' and
574 ;; `ps-print-background-image'.
575 ;;
576 ;; For example, if we wish to print text "preliminary" on all pages and text
577 ;; "special" on page 5 and from page 11 to page 17, we could specify:
578 ;;
579 ;; (setq ps-print-background-text
580 ;; '(("preliminary")
581 ;; ("special"
582 ;; "LeftMargin" "BottomMargin PrintHeight add" ; X and Y position
583 ;; ; (upper left corner)
584 ;; nil nil nil
585 ;; "PrintHeight neg PrintWidth atan" ; angle
586 ;; 5 (11 . 17)) ; page list
587 ;; ))
588 ;;
589 ;; Similarly, we could print image "~/images/EPS-image1.ps" on all pages and
590 ;; image "~/images/EPS-image2.ps" on page 5 and from page 11 to page 17, we
591 ;; specify:
592 ;;
593 ;; (setq ps-print-background-image
594 ;; '(("~/images/EPS-image1.ps"
595 ;; "LeftMargin" "BottomMargin") ; X and Y position (lower left corner)
596 ;; ("~/images/EPS-image2.ps"
597 ;; "LeftMargin" "BottomMargin PrintHeight 2 div add" ; X and Y position
598 ;; ; (upper left corner)
599 ;; nil nil nil
600 ;; 5 (11 . 17)) ; page list
601 ;; ))
602 ;;
603 ;; If it is not possible to read (or does not exist) an image file, that file
604 ;; is ignored.
605 ;;
606 ;; The printing order is:
607 ;;
608 ;; 1. Print zebra stripes
609 ;; 2. Print background texts that it should be on all pages
610 ;; 3. Print background images that it should be on all pages
611 ;; 4. Print background texts only for current page (if any)
612 ;; 5. Print background images only for current page (if any)
613 ;; 6. Print header
614 ;; 7. Print buffer text (with faces, if specified) and line number
615 ;;
616 ;;
617 ;; Utilities
618 ;; ---------
619 ;;
620 ;; Some tools are provided to help you customize your font setup.
621 ;;
622 ;; `ps-setup' returns (some part of) the current setup.
623 ;;
624 ;; To avoid wrapping too many lines, you may want to adjust the
625 ;; left and right margins and the font size. On UN*X systems, do:
626 ;; pr -t file | awk '{printf "%3d %s\n", length($0), $0}' | sort -r | head
627 ;; to determine the longest lines of your file.
628 ;; Then, the command `ps-line-lengths' will give you the correspondence
629 ;; between a line length (number of characters) and the maximum font
630 ;; size which doesn't wrap such a line with the current ps-print setup.
631 ;;
632 ;; The commands `ps-nb-pages-buffer' and `ps-nb-pages-region' display
633 ;; the correspondence between a number of pages and the maximum font
634 ;; size which allow the number of lines of the current buffer or of
635 ;; its current region to fit in this number of pages.
636 ;;
637 ;; NOTE: line folding is not taken into account in this process and could
638 ;; change the results.
639 ;;
640 ;;
641 ;; New since version 1.5
642 ;; ---------------------
643 ;;
644 ;; Color output capability.
645 ;; Automatic detection of font attributes (bold, italic).
646 ;; Configurable headers with page numbers.
647 ;; Slightly faster.
648 ;; Support for different paper sizes.
649 ;; Better conformance to PostScript Document Structure Conventions.
650 ;;
651 ;;
652 ;; New since version 2.8
653 ;; ---------------------
654 ;;
655 ;; [vinicius] 970809 Vinicius Jose Latorre <vinicius@cpqd.br>
656 ;;
657 ;; Handle control characters.
658 ;; Face remapping.
659 ;; New face attributes.
660 ;; Line number.
661 ;; Zebra stripes.
662 ;; Text and/or image on background.
663 ;;
664 ;; [jack] 960517 Jacques Duthen <duthen@cegelec-red.fr>
665 ;;
666 ;; Font family and float size for text and header.
667 ;; Landscape mode.
668 ;; Multiple columns.
669 ;; Tools for page setup.
670 ;;
671 ;;
672 ;; Known bugs and limitations of ps-print:
673 ;; --------------------------------------
674 ;;
675 ;; Although color printing will work in XEmacs 19.12, it doesn't work
676 ;; well; in particular, bold or italic fonts don't print in the right
677 ;; background color.
678 ;;
679 ;; Invisible properties aren't correctly ignored in XEmacs 19.12.
680 ;;
681 ;; Automatic font-attribute detection doesn't work well, especially
682 ;; with hilit19 and older versions of get-create-face. Users having
683 ;; problems with auto-font detection should use the lists
684 ;; `ps-italic-faces' and `ps-bold-faces' and/or turn off automatic
685 ;; detection by setting `ps-auto-font-detect' to nil.
686 ;;
687 ;; Automatic font-attribute detection doesn't work with XEmacs 19.12
688 ;; in tty mode; use the lists `ps-italic-faces' and `ps-bold-faces'
689 ;; instead.
690 ;;
691 ;; Still too slow; could use some hand-optimization.
692 ;;
693 ;; Default background color isn't working.
694 ;;
695 ;; Faces are always treated as opaque.
696 ;;
697 ;; Epoch and Emacs 18 not supported. At all.
698 ;;
699 ;; Fixed-pitch fonts work better for line folding, but are not required.
700 ;;
701 ;; `ps-nb-pages-buffer' and `ps-nb-pages-region' don't take care
702 ;; of folding lines.
703 ;;
704 ;;
705 ;; Things to change:
706 ;; ----------------
707 ;;
708 ;; Add `ps-print-hook' (I don't know how to do that (yet!)).
709 ;; Add 4-up capability (really needed?).
710 ;; Add `ps-non-bold-faces' and `ps-non-italic-faces' (should be easy).
711 ;; Put one header per page over the columns (easy but needed?).
712 ;; Improve the memory management for big files (hard?).
713 ;; `ps-nb-pages-buffer' and `ps-nb-pages-region' should take care
714 ;; of folding lines.
715 ;;
716 ;;
717 ;; Acknowledgements
718 ;; ----------------
719 ;; Thanks to Jim Thompson <?@?> for the 2.8 version I started from.
720 ;; [jack]
721 ;;
722 ;; Thanks to Kevin Rodgers <kevinr@ihs.com> for adding support for
723 ;; color and the invisible property.
724 ;;
725 ;; Thanks to Avishai Yacobi, avishaiy@mcil.comm.mot.com, for writing
726 ;; the initial port to Emacs 19. His code is no longer part of
727 ;; ps-print, but his work is still appreciated.
728 ;;
729 ;; Thanks to Remi Houdaille and Michel Train, michel@metasoft.fdn.org,
730 ;; for adding underline support. Their code also is no longer part of
731 ;; ps-print, but their efforts are not forgotten.
732 ;;
733 ;; Thanks also to all of you who mailed code to add features to
734 ;; ps-print; although I didn't use your code, I still appreciate your
735 ;; sharing it with me.
736 ;;
737 ;; Thanks to all who mailed comments, encouragement, and criticism.
738 ;; Thanks also to all who responded to my survey; I had too many
739 ;; responses to reply to them all, but I greatly appreciate your
740 ;; interest.
741 ;;
742 ;; Jim
743 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
744
745 ;;; Code:
746
747 (eval-when-compile
748 (require 'cl))
749
750 (unless (featurep 'lisp-float-type)
751 (error "`ps-print' requires floating point support"))
752
753 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
754 ;; User Variables:
755
756 ;;; Interface to the command system
757
758 (defgroup ps-print nil
759 "Postscript generator for Emacs 19"
760 :prefix "ps-"
761 :group 'wp)
762
763 (defgroup ps-print-horizontal nil
764 "Horizontal page layout"
765 :prefix "ps-"
766 :tag "Horizontal"
767 :group 'ps-print)
768
769 (defgroup ps-print-vertical nil
770 "Vertical page layout"
771 :prefix "ps-"
772 :tag "Vertical"
773 :group 'ps-print)
774
775 (defgroup ps-print-header nil
776 "Headers layout"
777 :prefix "ps-"
778 :tag "Header"
779 :group 'ps-print)
780
781 (defgroup ps-print-font nil
782 "Fonts customization"
783 :prefix "ps-"
784 :tag "Font"
785 :group 'ps-print)
786
787 (defgroup ps-print-color nil
788 "Color customization"
789 :prefix "ps-"
790 :tag "Color"
791 :group 'ps-print)
792
793 (defgroup ps-print-face nil
794 "Faces customization"
795 :prefix "ps-"
796 :tag "PS Faces"
797 :group 'ps-print
798 :group 'faces)
799
800
801 (defcustom ps-lpr-command lpr-command
802 "*The shell command for printing a PostScript file."
803 :type 'string
804 :group 'ps-print)
805
806 (defcustom ps-lpr-switches lpr-switches
807 "*A list of extra switches to pass to `ps-lpr-command'."
808 :type '(repeat string)
809 :group 'ps-print)
810
811 ;;; Page layout
812
813 ;; All page dimensions are in PostScript points.
814 ;; 1 inch == 2.54 cm == 72 points
815 ;; 1 cm == (/ 1 2.54) inch == (/ 72 2.54) points
816
817 ;; Letter 8.5 inch x 11.0 inch
818 ;; Legal 8.5 inch x 14.0 inch
819 ;; A4 8.26 inch x 11.69 inch = 21.0 cm x 29.7 cm
820
821 ;; LetterSmall 7.68 inch x 10.16 inch
822 ;; Tabloid 11.0 inch x 17.0 inch
823 ;; Ledger 17.0 inch x 11.0 inch
824 ;; Statement 5.5 inch x 8.5 inch
825 ;; Executive 7.5 inch x 10.0 inch
826 ;; A3 11.69 inch x 16.5 inch = 29.7 cm x 42.0 cm
827 ;; A4Small 7.47 inch x 10.85 inch
828 ;; B4 10.125 inch x 14.33 inch
829 ;; B5 7.16 inch x 10.125 inch
830
831 (defcustom ps-page-dimensions-database
832 (list (list 'a4 (/ (* 72 21.0) 2.54) (/ (* 72 29.7) 2.54))
833 (list 'a3 (/ (* 72 29.7) 2.54) (/ (* 72 42.0) 2.54))
834 (list 'letter (* 72 8.5) (* 72 11.0))
835 (list 'legal (* 72 8.5) (* 72 14.0))
836 (list 'letter-small (* 72 7.68) (* 72 10.16))
837 (list 'tabloid (* 72 11.0) (* 72 17.0))
838 (list 'ledger (* 72 17.0) (* 72 11.0))
839 (list 'statement (* 72 5.5) (* 72 8.5))
840 (list 'executive (* 72 7.5) (* 72 10.0))
841 (list 'a4small (* 72 7.47) (* 72 10.85))
842 (list 'b4 (* 72 10.125) (* 72 14.33))
843 (list 'b5 (* 72 7.16) (* 72 10.125)))
844 "*List associating a symbolic paper type to its width and height.
845 see `ps-paper-type'."
846 :type '(repeat (list :tag "Paper Type"
847 (symbol :tag "Name")
848 (number :tag "Width")
849 (number :tag "Height")))
850 :group 'ps-print)
851
852 (defcustom ps-paper-type 'letter
853 "*Specifies the size of paper to format for.
854 Should be one of the paper types defined in `ps-page-dimensions-database', for
855 example `letter', `legal' or `a4'."
856 :type '(symbol :validate (lambda (wid)
857 (if (assq (widget-value wid)
858 ps-page-dimensions-database)
859 nil
860 (widget-put wid :error "Unknown paper size")
861 wid)))
862 :group 'ps-print)
863
864 (defcustom ps-landscape-mode nil
865 "*Non-nil means print in landscape mode."
866 :type 'boolean
867 :group 'ps-print)
868
869 (defcustom ps-number-of-columns (if ps-landscape-mode 2 1)
870 "*Specifies the number of columns"
871 :type 'number
872 :group 'ps-print)
873
874 (defcustom ps-zebra-stripes nil
875 "*Non-nil means print zebra stripes.
876 See also documentation for `ps-print-n-zebra'."
877 :type 'boolean
878 :group 'ps-print)
879
880 (defcustom ps-zebra-stripe-height 3
881 "*Number of zebra stripe lines.
882 See also documentation for `ps-print-zebra'."
883 :type 'number
884 :group 'ps-print)
885
886 (defcustom ps-line-number nil
887 "*Non-nil means print line number."
888 :type 'boolean
889 :group 'ps-print)
890
891 (defcustom ps-print-background-image nil
892 "*EPS image list to be printed on background.
893
894 The elements are:
895
896 (FILENAME X Y XSCALE YSCALE ROTATION PAGES...)
897
898 FILENAME is a file name which contains an EPS image or some PostScript
899 programming like EPS.
900 FILENAME is ignored, if it doesn't exist or is read protected.
901
902 X and Y are relative positions on paper to put the image.
903 If X and Y are nil, the image is centralized on paper.
904
905 XSCALE and YSCALE are scale factor to be applied to image before printing.
906 If XSCALE and YSCALE are nil, the original size is used.
907
908 ROTATION is the image rotation angle; if nil, the default is 0.
909
910 PAGES designates the page to print background image.
911 PAGES may be a number or a cons cell (FROM . TO) designating FROM page
912 to TO page.
913 If PAGES is nil, print background image on all pages.
914
915 X, Y, XSCALE, YSCALE and ROTATION may be a floating point number,
916 an integer number or a string. If it is a string, the string should contain
917 PostScript programming that returns a float or integer value.
918
919 For example, if you wish to print an EPS image on all pages do:
920
921 '((\"~/images/EPS-image.ps\"))"
922 :type 'list
923 :group 'ps-print)
924
925 (defcustom ps-print-background-text nil
926 "*Text list to be printed on background.
927
928 The elements are:
929
930 (STRING X Y FONT FONTSIZE GRAY ROTATION PAGES...)
931
932 STRING is the text to be printed on background.
933
934 X and Y are positions on paper to put the text.
935 If X and Y are nil, the text is positioned at lower left corner.
936
937 FONT is a font name to be used on printing the text.
938 If nil, \"Times-Roman\" is used.
939
940 FONTSIZE is font size to be used, if nil, 200 is used.
941
942 GRAY is the text gray factor (should be very light like 0.8).
943 If nil, the default is 0.85.
944
945 ROTATION is the text rotation angle; if nil, the angle is given by
946 the diagonal from lower left corner to upper right corner.
947
948 PAGES designates the page to print background text.
949 PAGES may be a number or a cons cell (FROM . TO) designating FROM page
950 to TO page.
951 If PAGES is nil, print background text on all pages.
952
953 X, Y, FONTSIZE, GRAY and ROTATION may be a floating point number,
954 an integer number or a string. If it is a string, the string should contain
955 PostScript programming that returns a float or integer value.
956
957 For example, if you wish to print text \"Preliminary\" on all pages do:
958
959 '((\"Preliminary\"))"
960 :type 'list
961 :group 'ps-print)
962
963 ;;; Horizontal layout
964
965 ;; ------------------------------------------
966 ;; | | | | | | | |
967 ;; | lm | text | ic | text | ic | text | rm |
968 ;; | | | | | | | |
969 ;; ------------------------------------------
970
971 (defcustom ps-left-margin (/ (* 72 2.0) 2.54) ; 2 cm
972 "*Left margin in points (1/72 inch)."
973 :type 'number
974 :group 'ps-print-horizontal)
975
976 (defcustom ps-right-margin (/ (* 72 2.0) 2.54) ; 2 cm
977 "*Right margin in points (1/72 inch)."
978 :type 'number
979 :group 'ps-print-horizontal)
980
981 (defcustom ps-inter-column (/ (* 72 2.0) 2.54) ; 2 cm
982 "*Horizontal space between columns in points (1/72 inch)."
983 :type 'number
984 :group 'ps-print-horizontal)
985
986 ;;; Vertical layout
987
988 ;; |--------|
989 ;; | tm |
990 ;; |--------|
991 ;; | header |
992 ;; |--------|
993 ;; | ho |
994 ;; |--------|
995 ;; | text |
996 ;; |--------|
997 ;; | bm |
998 ;; |--------|
999
1000 (defcustom ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
1001 "*Bottom margin in points (1/72 inch)."
1002 :type 'number
1003 :group 'ps-print-vertical)
1004
1005 (defcustom ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
1006 "*Top margin in points (1/72 inch)."
1007 :type 'number
1008 :group 'ps-print-vertical)
1009
1010 (defcustom ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
1011 "*Vertical space in points (1/72 inch) between the main text and the header."
1012 :type 'number
1013 :group 'ps-print-vertical)
1014
1015 (defcustom ps-header-line-pad 0.15
1016 "*Portion of a header title line height to insert between the header frame
1017 and the text it contains, both in the vertical and horizontal directions."
1018 :type 'number
1019 :group 'ps-print-vertical)
1020
1021 ;;; Header setup
1022
1023 (defcustom ps-print-header t
1024 "*Non-nil means print a header at the top of each page.
1025 By default, the header displays the buffer name, page number, and, if
1026 the buffer is visiting a file, the file's directory. Headers are
1027 customizable by changing variables `ps-left-header' and
1028 `ps-right-header'."
1029 :type 'boolean
1030 :group 'ps-print-header)
1031
1032 (defcustom ps-print-header-frame t
1033 "*Non-nil means draw a gaudy frame around the header."
1034 :type 'boolean
1035 :group 'ps-print-header)
1036
1037 (defcustom ps-header-lines 2
1038 "*Number of lines to display in page header, when generating Postscript."
1039 :type 'integer
1040 :group 'ps-print-header)
1041 (make-variable-buffer-local 'ps-header-lines)
1042
1043 (defcustom ps-show-n-of-n t
1044 "*Non-nil means show page numbers as N/M, meaning page N of M.
1045 Note: page numbers are displayed as part of headers, see variable
1046 `ps-print-headers'."
1047 :type 'boolean
1048 :group 'ps-print-header)
1049
1050 (defcustom ps-spool-duplex nil ; Not many people have duplex
1051 ; printers, so default to nil.
1052 "*Non-nil indicates spooling is for a two-sided printer.
1053 For a duplex printer, the `ps-spool-*' commands will insert blank pages
1054 as needed between print jobs so that the next buffer printed will
1055 start on the right page. Also, if headers are turned on, the headers
1056 will be reversed on duplex printers so that the page numbers fall to
1057 the left on even-numbered pages."
1058 :type 'boolean
1059 :group 'ps-print-header)
1060
1061 ;;; Fonts
1062
1063 (defcustom ps-font-info-database
1064 '((Courier ; the family key
1065 "Courier" "Courier-Bold" "Courier-Oblique" "Courier-BoldOblique"
1066 10.0 10.55 6.0 6.0)
1067 (Helvetica ; the family key
1068 "Helvetica" "Helvetica-Bold" "Helvetica-Oblique" "Helvetica-BoldOblique"
1069 10.0 11.56 2.78 5.09243)
1070 (Times
1071 "Times-Roman" "Times-Bold" "Times-Italic" "Times-BoldItalic"
1072 10.0 11.0 2.5 4.71432)
1073 (Palatino
1074 "Palatino-Roman" "Palatino-Bold" "Palatino-Italic" "Palatino-BoldItalic"
1075 10.0 12.1 2.5 5.08676)
1076 (Helvetica-Narrow
1077 "Helvetica-Narrow" "Helvetica-Narrow-Bold"
1078 "Helvetica-Narrow-Oblique" "Helvetica-Narrow-BoldOblique"
1079 10.0 11.56 2.2796 4.17579)
1080 (NewCenturySchlbk
1081 "NewCenturySchlbk-Roman" "NewCenturySchlbk-Bold"
1082 "NewCenturySchlbk-Italic" "NewCenturySchlbk-BoldItalic"
1083 10.0 12.15 2.78 5.31162)
1084 ;; got no bold for the next ones
1085 (AvantGarde-Book
1086 "AvantGarde-Book" "AvantGarde-Book"
1087 "AvantGarde-BookOblique" "AvantGarde-BookOblique"
1088 10.0 11.77 2.77 5.45189)
1089 (AvantGarde-Demi
1090 "AvantGarde-Demi" "AvantGarde-Demi"
1091 "AvantGarde-DemiOblique" "AvantGarde-DemiOblique"
1092 10.0 12.72 2.8 5.51351)
1093 (Bookman-Demi
1094 "Bookman-Demi" "Bookman-Demi"
1095 "Bookman-DemiItalic" "Bookman-DemiItalic"
1096 10.0 11.77 3.4 6.05946)
1097 (Bookman-Light
1098 "Bookman-Light" "Bookman-Light"
1099 "Bookman-LightItalic" "Bookman-LightItalic"
1100 10.0 11.79 3.2 5.67027)
1101 ;; got no bold and no italic for the next ones
1102 (Symbol
1103 "Symbol" "Symbol" "Symbol" "Symbol"
1104 10.0 13.03 2.5 3.24324)
1105 (Zapf-Dingbats
1106 "Zapf-Dingbats" "Zapf-Dingbats" "Zapf-Dingbats" "Zapf-Dingbats"
1107 10.0 9.63 2.78 2.78)
1108 (Zapf-Chancery-MediumItalic
1109 "Zapf-Chancery-MediumItalic" "Zapf-Chancery-MediumItalic"
1110 "Zapf-Chancery-MediumItalic" "Zapf-Chancery-MediumItalic"
1111 10.0 11.45 2.2 4.10811)
1112 )
1113 "*Font info database: font family (the key), name, bold, italic, bold-italic,
1114 reference size, line height, space width, average character width.
1115 To get the info for another specific font (say Helvetica), do the following:
1116 - create a new buffer
1117 - generate the PostScript image to a file (C-u M-x ps-print-buffer)
1118 - open this file and delete the leading `%' (which is the Postscript
1119 comment character) from the line
1120 `% 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage'
1121 to get the line
1122 `3 cm 20 cm moveto 10 /Helvetica ReportFontInfo showpage'
1123 - add the values to `ps-font-info-database'.
1124 You can get all the fonts of YOUR printer using `ReportAllFontInfo'."
1125 :type '(repeat (list :tag "Font Definition"
1126 (symbol :tag "Font")
1127 (string :tag "Name")
1128 (string :tag "Bold")
1129 (string :tag "Italic")
1130 (string :tag "Bold-Italic")
1131 (number :tag "Reference Size")
1132 (number :tag "Line Height")
1133 (number :tag "Space Width")
1134 (number :tag "Average Character Width")))
1135 :group 'ps-print-font)
1136
1137 (defcustom ps-font-family 'Courier
1138 "Font family name for ordinary text, when generating Postscript."
1139 :type 'symbol
1140 :group 'ps-print-font)
1141
1142 (defcustom ps-font-size (if ps-landscape-mode 7 8.5)
1143 "Font size, in points, for ordinary text, when generating Postscript."
1144 :type 'number
1145 :group 'ps-print-font)
1146
1147 (defcustom ps-header-font-family 'Helvetica
1148 "Font family name for text in the header, when generating Postscript."
1149 :type 'symbol
1150 :group 'ps-print-font)
1151
1152 (defcustom ps-header-font-size (if ps-landscape-mode 10 12)
1153 "Font size, in points, for text in the header, when generating Postscript."
1154 :type 'number
1155 :group 'ps-print-font)
1156
1157 (defcustom ps-header-title-font-size (if ps-landscape-mode 12 14)
1158 "Font size, in points, for the top line of text in the header,
1159 when generating Postscript."
1160 :type 'number
1161 :group 'ps-print-font)
1162
1163 ;;; Colors
1164
1165 ;; Printing color requires x-color-values.
1166 (defcustom ps-print-color-p (or (fboundp 'x-color-values) ; Emacs
1167 (fboundp 'pixel-components)) ; XEmacs
1168 "*If non-nil, print the buffer's text in color."
1169 :type 'boolean
1170 :group 'ps-print-color)
1171
1172 (defcustom ps-default-fg '(0.0 0.0 0.0)
1173 "*RGB values of the default foreground color. Defaults to black."
1174 :type '(list (number :tag "Red") (number :tag "Green") (number :tag "Blue"))
1175 :group 'ps-print-color)
1176
1177 (defcustom ps-default-bg '(1.0 1.0 1.0)
1178 "*RGB values of the default background color. Defaults to white."
1179 :type '(list (number :tag "Red") (number :tag "Green") (number :tag "Blue"))
1180 :group 'ps-print-color)
1181
1182 (defcustom ps-auto-font-detect t
1183 "*Non-nil means automatically detect bold/italic face attributes.
1184 nil means rely solely on the lists `ps-bold-faces', `ps-italic-faces',
1185 and `ps-underlined-faces'."
1186 :type 'boolean
1187 :group 'ps-print-font)
1188
1189 (defcustom ps-bold-faces
1190 (unless ps-print-color-p
1191 '(font-lock-function-name-face
1192 font-lock-builtin-face
1193 font-lock-variable-name-face
1194 font-lock-keyword-face
1195 font-lock-warning-face))
1196 "*A list of the \(non-bold\) faces that should be printed in bold font.
1197 This applies to generating Postscript."
1198 :type '(repeat face)
1199 :group 'ps-print-face)
1200
1201 (defcustom ps-italic-faces
1202 (unless ps-print-color-p
1203 '(font-lock-variable-name-face
1204 font-lock-string-face
1205 font-lock-comment-face
1206 font-lock-warning-face))
1207 "*A list of the \(non-italic\) faces that should be printed in italic font.
1208 This applies to generating Postscript."
1209 :type '(repeat face)
1210 :group 'ps-print-face)
1211
1212 (defcustom ps-underlined-faces
1213 (unless ps-print-color-p
1214 '(font-lock-function-name-face
1215 font-lock-type-face
1216 font-lock-reference-face
1217 font-lock-warning-face))
1218 "*A list of the \(non-underlined\) faces that should be printed underlined.
1219 This applies to generating Postscript."
1220 :type '(repeat face)
1221 :group 'ps-print-face)
1222
1223 (defcustom ps-left-header
1224 (list 'ps-get-buffer-name 'ps-header-dirpart)
1225 "*The items to display (each on a line) on the left part of the page header.
1226 This applies to generating Postscript.
1227
1228 The value should be a list of strings and symbols, each representing an
1229 entry in the PostScript array HeaderLinesLeft.
1230
1231 Strings are inserted unchanged into the array; those representing
1232 PostScript string literals should be delimited with PostScript string
1233 delimiters '(' and ')'.
1234
1235 For symbols with bound functions, the function is called and should
1236 return a string to be inserted into the array. For symbols with bound
1237 values, the value should be a string to be inserted into the array.
1238 In either case, function or variable, the string value has PostScript
1239 string delimiters added to it."
1240 :type '(repeat (choice string symbol))
1241 :group 'ps-print-header)
1242 (make-variable-buffer-local 'ps-left-header)
1243
1244 (defcustom ps-right-header
1245 (list "/pagenumberstring load" 'time-stamp-mon-dd-yyyy 'time-stamp-hh:mm:ss)
1246 "*The items to display (each on a line) on the right part of the page header.
1247 This applies to generating Postscript.
1248
1249 See the variable `ps-left-header' for a description of the format of
1250 this variable."
1251 :type '(repeat (choice string symbol))
1252 :group 'ps-print-header)
1253 (make-variable-buffer-local 'ps-right-header)
1254
1255 (defcustom ps-razzle-dazzle t
1256 "*Non-nil means report progress while formatting buffer."
1257 :type 'boolean
1258 :group 'ps-print)
1259
1260 (defcustom ps-adobe-tag "%!PS-Adobe-3.0\n"
1261 "*Contains the header line identifying the output as PostScript.
1262 By default, `ps-adobe-tag' contains the standard identifier. Some
1263 printers require slightly different versions of this line."
1264 :type 'string
1265 :group 'ps-print)
1266
1267 (defcustom ps-build-face-reference t
1268 "*Non-nil means build the reference face lists.
1269
1270 Ps-print sets this value to nil after it builds its internal reference
1271 lists of bold and italic faces. By settings its value back to t, you
1272 can force ps-print to rebuild the lists the next time you invoke one
1273 of the ...-with-faces commands.
1274
1275 You should set this value back to t after you change the attributes of
1276 any face, or create new faces. Most users shouldn't have to worry
1277 about its setting, though."
1278 :type 'boolean
1279 :group 'ps-print-face)
1280
1281 (defcustom ps-always-build-face-reference nil
1282 "*Non-nil means always rebuild the reference face lists.
1283
1284 If this variable is non-nil, ps-print will rebuild its internal
1285 reference lists of bold and italic faces *every* time one of the
1286 -with-faces commands is called. Most users shouldn't need to set this
1287 variable."
1288 :type 'boolean
1289 :group 'ps-print-face)
1290
1291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1292 ;; User commands
1293
1294 ;;;###autoload
1295 (defun ps-print-buffer (&optional filename)
1296 "Generate and print a PostScript image of the buffer.
1297
1298 When called with a numeric prefix argument (C-u), prompts the user for
1299 the name of a file to save the PostScript image in, instead of sending
1300 it to the printer.
1301
1302 More specifically, the FILENAME argument is treated as follows: if it
1303 is nil, send the image to the printer. If FILENAME is a string, save
1304 the PostScript image in a file with that name. If FILENAME is a
1305 number, prompt the user for the name of the file to save in."
1306 (interactive (list (ps-print-preprint current-prefix-arg)))
1307 (ps-print-without-faces (point-min) (point-max) filename))
1308
1309
1310 ;;;###autoload
1311 (defun ps-print-buffer-with-faces (&optional filename)
1312 "Generate and print a PostScript image of the buffer.
1313 Like `ps-print-buffer', but includes font, color, and underline
1314 information in the generated image. This command works only if you
1315 are using a window system, so it has a way to determine color values."
1316 (interactive (list (ps-print-preprint current-prefix-arg)))
1317 (ps-print-with-faces (point-min) (point-max) filename))
1318
1319
1320 ;;;###autoload
1321 (defun ps-print-region (from to &optional filename)
1322 "Generate and print a PostScript image of the region.
1323 Like `ps-print-buffer', but prints just the current region."
1324 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
1325 (ps-print-without-faces from to filename t))
1326
1327
1328 ;;;###autoload
1329 (defun ps-print-region-with-faces (from to &optional filename)
1330 "Generate and print a PostScript image of the region.
1331 Like `ps-print-region', but includes font, color, and underline
1332 information in the generated image. This command works only if you
1333 are using a window system, so it has a way to determine color values."
1334 (interactive (list (point) (mark) (ps-print-preprint current-prefix-arg)))
1335 (ps-print-with-faces from to filename t))
1336
1337
1338 ;;;###autoload
1339 (defun ps-spool-buffer ()
1340 "Generate and spool a PostScript image of the buffer.
1341 Like `ps-print-buffer' except that the PostScript image is saved in a
1342 local buffer to be sent to the printer later.
1343
1344 Use the command `ps-despool' to send the spooled images to the printer."
1345 (interactive)
1346 (ps-spool-without-faces (point-min) (point-max)))
1347
1348
1349 ;;;###autoload
1350 (defun ps-spool-buffer-with-faces ()
1351 "Generate and spool a PostScript image of the buffer.
1352 Like `ps-spool-buffer', but includes font, color, and underline
1353 information in the generated image. This command works only if you
1354 are using a window system, so it has a way to determine color values.
1355
1356 Use the command `ps-despool' to send the spooled images to the printer."
1357 (interactive)
1358 (ps-spool-with-faces (point-min) (point-max)))
1359
1360
1361 ;;;###autoload
1362 (defun ps-spool-region (from to)
1363 "Generate a PostScript image of the region and spool locally.
1364 Like `ps-spool-buffer', but spools just the current region.
1365
1366 Use the command `ps-despool' to send the spooled images to the printer."
1367 (interactive "r")
1368 (ps-spool-without-faces from to t))
1369
1370
1371 ;;;###autoload
1372 (defun ps-spool-region-with-faces (from to)
1373 "Generate a PostScript image of the region and spool locally.
1374 Like `ps-spool-region', but includes font, color, and underline
1375 information in the generated image. This command works only if you
1376 are using a window system, so it has a way to determine color values.
1377
1378 Use the command `ps-despool' to send the spooled images to the printer."
1379 (interactive "r")
1380 (ps-spool-with-faces from to t))
1381
1382 ;;;###autoload
1383 (defun ps-despool (&optional filename)
1384 "Send the spooled PostScript to the printer.
1385
1386 When called with a numeric prefix argument (C-u), prompt the user for
1387 the name of a file to save the spooled PostScript in, instead of sending
1388 it to the printer.
1389
1390 More specifically, the FILENAME argument is treated as follows: if it
1391 is nil, send the image to the printer. If FILENAME is a string, save
1392 the PostScript image in a file with that name. If FILENAME is a
1393 number, prompt the user for the name of the file to save in."
1394 (interactive (list (ps-print-preprint current-prefix-arg)))
1395 (ps-do-despool filename))
1396
1397 ;;;###autoload
1398 (defun ps-line-lengths ()
1399 "*Display the correspondence between a line length and a font size,
1400 using the current ps-print setup.
1401 Try: pr -t file | awk '{printf \"%3d %s\n\", length($0), $0}' | sort -r | head"
1402 (interactive)
1403 (ps-line-lengths-internal))
1404
1405 ;;;###autoload
1406 (defun ps-nb-pages-buffer (nb-lines)
1407 "*Display an approximate correspondence between a font size and the number
1408 of pages the current buffer would require to print
1409 using the current ps-print setup."
1410 (interactive (list (count-lines (point-min) (point-max))))
1411 (ps-nb-pages nb-lines))
1412
1413 ;;;###autoload
1414 (defun ps-nb-pages-region (nb-lines)
1415 "*Display an approximate correspondence between a font size and the number
1416 of pages the current region would require to print
1417 using the current ps-print setup."
1418 (interactive (list (count-lines (mark) (point))))
1419 (ps-nb-pages nb-lines))
1420
1421 ;;;###autoload
1422 (defun ps-setup ()
1423 "*Return the current setup"
1424 (format
1425 "
1426 \(setq ps-print-color-p %s
1427 ps-lpr-command \"%s\"
1428 ps-lpr-switches %s
1429
1430 ps-paper-type '%s
1431 ps-landscape-mode %s
1432 ps-number-of-columns %s
1433
1434 ps-zebra-stripe %s
1435 ps-number-of-zebra %s
1436 ps-line-number %s
1437
1438 ps-print-background-image %s
1439
1440 ps-print-background-text %s
1441
1442 ps-left-margin %s
1443 ps-right-margin %s
1444 ps-inter-column %s
1445 ps-bottom-margin %s
1446 ps-top-margin %s
1447 ps-header-offset %s
1448 ps-header-line-pad %s
1449 ps-print-header %s
1450 ps-print-header-frame %s
1451 ps-header-lines %s
1452 ps-show-n-of-n %s
1453 ps-spool-duplex %s
1454
1455 ps-font-family '%s
1456 ps-font-size %s
1457 ps-header-font-family '%s
1458 ps-header-font-size %s
1459 ps-header-title-font-size %s)
1460 "
1461 ps-print-color-p
1462 ps-lpr-command
1463 ps-lpr-switches
1464 ps-paper-type
1465 ps-landscape-mode
1466 ps-number-of-columns
1467 ps-zebra-stripe
1468 ps-number-of-zebra
1469 ps-line-number
1470 ps-print-background-image
1471 ps-print-background-text
1472 ps-left-margin
1473 ps-right-margin
1474 ps-inter-column
1475 ps-bottom-margin
1476 ps-top-margin
1477 ps-header-offset
1478 ps-header-line-pad
1479 ps-print-header
1480 ps-print-header-frame
1481 ps-header-lines
1482 ps-show-n-of-n
1483 ps-spool-duplex
1484 ps-font-family
1485 ps-font-size
1486 ps-header-font-family
1487 ps-header-font-size
1488 ps-header-title-font-size))
1489
1490 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1491 ;; Utility functions and variables:
1492
1493 (defvar ps-print-emacs-type
1494 (cond ((string-match "XEmacs" emacs-version) 'xemacs)
1495 ((string-match "Lucid" emacs-version) 'lucid)
1496 ((string-match "Epoch" emacs-version) 'epoch)
1497 (t 'emacs)))
1498
1499 (if (or (eq ps-print-emacs-type 'lucid)
1500 (eq ps-print-emacs-type 'xemacs))
1501 (if (< emacs-minor-version 12)
1502 (setq ps-print-color-p nil))
1503 (require 'faces)) ; face-font, face-underline-p,
1504 ; x-font-regexp
1505
1506 (require 'time-stamp)
1507
1508 (defvar ps-font nil
1509 "Font family name for ordinary text, when generating Postscript.")
1510
1511 (defvar ps-font-bold nil
1512 "Font family name for bold text, when generating Postscript.")
1513
1514 (defvar ps-font-italic nil
1515 "Font family name for italic text, when generating Postscript.")
1516
1517 (defvar ps-font-bold-italic nil
1518 "Font family name for bold italic text, when generating Postscript.")
1519
1520 (defvar ps-avg-char-width nil
1521 "The average width, in points, of a character, for generating Postscript.
1522 This is the value that ps-print uses to determine the length,
1523 x-dimension, of the text it has printed, and thus affects the point at
1524 which long lines wrap around.")
1525
1526 (defvar ps-space-width nil
1527 "The width of a space character, for generating Postscript.
1528 This value is used in expanding tab characters.")
1529
1530 (defvar ps-line-height nil
1531 "The height of a line, for generating Postscript.
1532 This is the value that ps-print uses to determine the height,
1533 y-dimension, of the lines of text it has printed, and thus affects the
1534 point at which page-breaks are placed.
1535 The line-height is *not* the same as the point size of the font.")
1536
1537 (defvar ps-print-prologue-1
1538 "% ISOLatin1Encoding stolen from ps_init.ps in GhostScript 2.6.1.4:
1539 /ISOLatin1Encoding where { pop } {
1540 % -- The ISO Latin-1 encoding vector isn't known, so define it.
1541 % -- The first half is the same as the standard encoding,
1542 % -- except for minus instead of hyphen at code 055.
1543 /ISOLatin1Encoding
1544 StandardEncoding 0 45 getinterval aload pop
1545 /minus
1546 StandardEncoding 46 82 getinterval aload pop
1547 %*** NOTE: the following are missing in the Adobe documentation,
1548 %*** but appear in the displayed table:
1549 %*** macron at 0225, dieresis at 0230, cedilla at 0233, space at 0240.
1550 % 0200 (128)
1551 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
1552 /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
1553 /dotlessi /grave /acute /circumflex /tilde /macron /breve /dotaccent
1554 /dieresis /.notdef /ring /cedilla /.notdef /hungarumlaut /ogonek /caron
1555 % 0240 (160)
1556 /space /exclamdown /cent /sterling
1557 /currency /yen /brokenbar /section
1558 /dieresis /copyright /ordfeminine /guillemotleft
1559 /logicalnot /hyphen /registered /macron
1560 /degree /plusminus /twosuperior /threesuperior
1561 /acute /mu /paragraph /periodcentered
1562 /cedilla /onesuperior /ordmasculine /guillemotright
1563 /onequarter /onehalf /threequarters /questiondown
1564 % 0300 (192)
1565 /Agrave /Aacute /Acircumflex /Atilde
1566 /Adieresis /Aring /AE /Ccedilla
1567 /Egrave /Eacute /Ecircumflex /Edieresis
1568 /Igrave /Iacute /Icircumflex /Idieresis
1569 /Eth /Ntilde /Ograve /Oacute
1570 /Ocircumflex /Otilde /Odieresis /multiply
1571 /Oslash /Ugrave /Uacute /Ucircumflex
1572 /Udieresis /Yacute /Thorn /germandbls
1573 % 0340 (224)
1574 /agrave /aacute /acircumflex /atilde
1575 /adieresis /aring /ae /ccedilla
1576 /egrave /eacute /ecircumflex /edieresis
1577 /igrave /iacute /icircumflex /idieresis
1578 /eth /ntilde /ograve /oacute
1579 /ocircumflex /otilde /odieresis /divide
1580 /oslash /ugrave /uacute /ucircumflex
1581 /udieresis /yacute /thorn /ydieresis
1582 256 packedarray def
1583 } ifelse
1584
1585 /reencodeFontISO { %def
1586 dup
1587 length 12 add dict % Make a new font (a new dict the same size
1588 % as the old one) with room for our new symbols.
1589
1590 begin % Make the new font the current dictionary.
1591
1592
1593 { 1 index /FID ne
1594 { def } { pop pop } ifelse
1595 } forall % Copy each of the symbols from the old dictionary
1596 % to the new one except for the font ID.
1597
1598 /Encoding ISOLatin1Encoding def % Override the encoding with
1599 % the ISOLatin1 encoding.
1600
1601 % Use the font's bounding box to determine the ascent, descent,
1602 % and overall height; don't forget that these values have to be
1603 % transformed using the font's matrix.
1604
1605 % ^ (x2 y2)
1606 % | |
1607 % | v
1608 % | +----+ - -
1609 % | | | ^
1610 % | | | | Ascent (usually > 0)
1611 % | | | |
1612 % (0 0) -> +--+----+-------->
1613 % | | |
1614 % | | v Descent (usually < 0)
1615 % (x1 y1) --> +----+ - -
1616
1617 FontBBox % -- x1 y1 x2 y2
1618 FontMatrix transform /Ascent exch def pop
1619 FontMatrix transform /Descent exch def pop
1620 /FontHeight Ascent Descent sub def % use `sub' because descent < 0
1621
1622 % Define these in case they're not in the FontInfo
1623 % (also, here they're easier to get to).
1624 /UnderlinePosition Descent 0.70 mul def
1625 /OverlinePosition Descent UnderlinePosition sub Ascent add def
1626 /StrikeoutPosition Ascent 0.30 mul def
1627 /LineThickness 0 50 FontMatrix transform exch pop def
1628 /Xshadow 0 80 FontMatrix transform exch pop def
1629 /Yshadow 0 -90 FontMatrix transform exch pop def
1630 /SpaceBackground Descent neg UnderlinePosition add def
1631 /XBox Descent neg def
1632 /YBox LineThickness 0.7 mul def
1633
1634 currentdict % Leave the new font on the stack
1635 end % Stop using the font as the current dictionary.
1636 definefont % Put the font into the font dictionary
1637 pop % Discard the returned font.
1638 } bind def
1639
1640 /DefFont { % Font definition
1641 findfont exch scalefont reencodeFontISO
1642 } def
1643
1644 /F { % Font selection
1645 findfont
1646 dup /Ascent get /Ascent exch def
1647 dup /Descent get /Descent exch def
1648 dup /FontHeight get /FontHeight exch def
1649 dup /UnderlinePosition get /UnderlinePosition exch def
1650 dup /OverlinePosition get /OverlinePosition exch def
1651 dup /StrikeoutPosition get /StrikeoutPosition exch def
1652 dup /LineThickness get /LineThickness exch def
1653 dup /Xshadow get /Xshadow exch def
1654 dup /Yshadow get /Yshadow exch def
1655 dup /SpaceBackground get /SpaceBackground exch def
1656 dup /XBox get /XBox exch def
1657 dup /YBox get /YBox exch def
1658 setfont
1659 } def
1660
1661 /FG /setrgbcolor load def
1662
1663 /bg false def
1664 /BG {
1665 dup /bg exch def
1666 {mark 4 1 roll ]}
1667 {[ 1.0 1.0 1.0 ]}
1668 ifelse
1669 /bgcolor exch def
1670 } def
1671
1672 % B width C
1673 % +-----------+
1674 % | Ascent (usually > 0)
1675 % A + +
1676 % | Descent (usually < 0)
1677 % +-----------+
1678 % E width D
1679
1680 /dobackground { % width --
1681 currentpoint % -- width x y
1682 gsave
1683 newpath
1684 moveto % A (x y)
1685 0 Ascent rmoveto % B
1686 dup 0 rlineto % C
1687 0 Descent Ascent sub rlineto % D
1688 neg 0 rlineto % E
1689 closepath
1690 bgcolor aload pop setrgbcolor
1691 fill
1692 grestore
1693 } def
1694
1695 /eolbg { % dobackground until right margin
1696 PrintWidth % -- x-eol
1697 currentpoint pop % -- cur-x
1698 sub % -- width until eol
1699 dobackground
1700 } def
1701
1702 /PLN {PrintLineNumber {doLineNumber}if} def
1703
1704 /SL { % Soft Linefeed
1705 bg { eolbg } if
1706 0 currentpoint exch pop LineHeight sub moveto
1707 } def
1708
1709 /HL {SL PLN} def % Hard Linefeed
1710
1711 % Some debug
1712 /dcp { currentpoint exch 40 string cvs print (, ) print = } def
1713 /dp { print 2 copy exch 40 string cvs print (, ) print = } def
1714
1715 /W {
1716 ( ) stringwidth % Get the width of a space in the current font.
1717 pop % Discard the Y component.
1718 mul % Multiply the width of a space
1719 % by the number of spaces to plot
1720 bg { dup dobackground } if
1721 0 rmoveto
1722 } def
1723
1724 /Effect 0 def
1725 /EF {/Effect exch def} def
1726
1727 % stack: string |- --
1728 % effect: 1 - underline 2 - strikeout 4 - overline
1729 % 8 - shadow 16 - box 32 - outline
1730 /S {
1731 /xx currentpoint dup Descent add /yy exch def
1732 Ascent add /YY exch def def
1733 dup stringwidth pop xx add /XX exch def
1734 Effect 8 and 0 ne {
1735 /yy yy Yshadow add def
1736 /XX XX Xshadow add def
1737 } if
1738 bg {
1739 true
1740 Effect 16 and 0 ne
1741 {SpaceBackground doBox}
1742 {xx yy XX YY doRect}
1743 ifelse
1744 } if % background
1745 Effect 16 and 0 ne {false 0 doBox}if % box
1746 Effect 8 and 0 ne {dup doShadow}if % shadow
1747 Effect 32 and 0 ne
1748 {true doOutline} % outline
1749 {show} % normal text
1750 ifelse
1751 Effect 1 and 0 ne {UnderlinePosition Hline}if % underline
1752 Effect 2 and 0 ne {StrikeoutPosition Hline}if % strikeout
1753 Effect 4 and 0 ne {OverlinePosition Hline}if % overline
1754 } bind def
1755
1756 % stack: position |- --
1757 /Hline {
1758 currentpoint exch pop add dup
1759 gsave
1760 newpath
1761 xx exch moveto
1762 XX exch lineto
1763 closepath
1764 LineThickness setlinewidth stroke
1765 grestore
1766 } bind def
1767
1768 % stack: fill-or-not delta |- --
1769 /doBox {
1770 /dd exch def
1771 xx XBox sub dd sub yy YBox sub dd sub
1772 XX XBox add dd add YY YBox add dd add
1773 doRect
1774 } bind def
1775
1776 % stack: fill-or-not lower-x lower-y upper-x upper-y |- --
1777 /doRect {
1778 /rYY exch def
1779 /rXX exch def
1780 /ryy exch def
1781 /rxx exch def
1782 gsave
1783 newpath
1784 rXX rYY moveto
1785 rxx rYY lineto
1786 rxx ryy lineto
1787 rXX ryy lineto
1788 closepath
1789 % top of stack: fill-or-not
1790 {FillBgColor}
1791 {LineThickness setlinewidth stroke}
1792 ifelse
1793 grestore
1794 } bind def
1795
1796 % stack: string |- --
1797 /doShadow {
1798 gsave
1799 Xshadow Yshadow rmoveto
1800 false doOutline
1801 grestore
1802 } bind def
1803
1804 /st 1 string def
1805
1806 % stack: string fill-or-not |- --
1807 /doOutline {
1808 /-fillp- exch def
1809 /-ox- currentpoint /-oy- exch def def
1810 gsave
1811 LineThickness setlinewidth
1812 {
1813 st 0 3 -1 roll put
1814 st dup true charpath
1815 -fillp- {gsave FillBgColor grestore}if
1816 stroke stringwidth
1817 -oy- add /-oy- exch def
1818 -ox- add /-ox- exch def
1819 -ox- -oy- moveto
1820 } forall
1821 grestore
1822 -ox- -oy- moveto
1823 } bind def
1824
1825 % stack: --
1826 /FillBgColor {bgcolor aload pop setrgbcolor fill} bind def
1827
1828 /L0 6 /Times-Italic DefFont
1829
1830 % stack: --
1831 /doLineNumber {
1832 currentfont
1833 gsave
1834 0.0 0.0 0.0 setrgbcolor
1835 /L0 findfont setfont
1836 LineNumber Lines ge
1837 {(end )}
1838 {LineNumber 6 string cvs ( ) strcat}
1839 ifelse
1840 dup stringwidth pop neg 0 rmoveto
1841 show
1842 grestore
1843 setfont
1844 /LineNumber LineNumber 1 add def
1845 } def
1846
1847 % stack: --
1848 /printZebra {
1849 gsave
1850 0.985 setgray
1851 /double-zebra NumberOfZebra NumberOfZebra add def
1852 /yiter double-zebra LineHeight mul neg def
1853 /xiter PrintWidth InterColumn add def
1854 NumberOfColumns {LinesPerColumn doColumnZebra xiter 0 rmoveto}repeat
1855 grestore
1856 } def
1857
1858 % stack: lines-per-column |- --
1859 /doColumnZebra {
1860 gsave
1861 dup double-zebra idiv {NumberOfZebra doZebra 0 yiter rmoveto}repeat
1862 double-zebra mod
1863 dup 0 le {pop}{dup NumberOfZebra gt {pop NumberOfZebra}if doZebra}ifelse
1864 grestore
1865 } def
1866
1867 % stack: zebra-height (in lines) |- --
1868 /doZebra {
1869 /zh exch 0.05 sub LineHeight mul def
1870 gsave
1871 0 LineHeight 0.65 mul rmoveto
1872 PrintWidth 0 rlineto
1873 0 zh neg rlineto
1874 PrintWidth neg 0 rlineto
1875 0 zh rlineto
1876 fill
1877 grestore
1878 } def
1879
1880 % tx ty rotation xscale yscale xpos ypos BeginBackImage
1881 /BeginBackImage {
1882 /-save-image- save def
1883 /showpage {}def
1884 translate
1885 scale
1886 rotate
1887 translate
1888 } def
1889
1890 /EndBackImage {
1891 -save-image- restore
1892 } def
1893
1894 % string fontsize fontname rotation gray xpos ypos ShowBackText
1895 /ShowBackText {
1896 gsave
1897 translate
1898 setgray
1899 rotate
1900 findfont exch dup /-offset- exch -0.25 mul def scalefont setfont
1901 0 -offset- moveto
1902 /-saveLineThickness- LineThickness def
1903 /LineThickness 1 def
1904 false doOutline
1905 /LineThickness -saveLineThickness- def
1906 grestore
1907 } def
1908
1909 /BeginDoc {
1910 % ---- save the state of the document (useful for ghostscript!)
1911 /docState save def
1912 % ---- [jack] Kludge: my ghostscript window is 21x27.7 instead of 21x29.7
1913 /JackGhostscript where {
1914 pop 1 27.7 29.7 div scale
1915 } if
1916 LandscapeMode {
1917 % ---- translate to bottom-right corner of Portrait page
1918 LandscapePageHeight 0 translate
1919 90 rotate
1920 } if
1921 /ColumnWidth PrintWidth InterColumn add def
1922 % ---- translate to lower left corner of TEXT
1923 LeftMargin BottomMargin translate
1924 % ---- define where printing will start
1925 /f0 F % this installs Ascent
1926 /PrintStartY PrintHeight Ascent sub def
1927 /ColumnIndex 1 def
1928 } def
1929
1930 /EndDoc {
1931 % ---- on last page but not last column, spit out the page
1932 ColumnIndex 1 eq not { showpage } if
1933 % ---- restore the state of the document (useful for ghostscript!)
1934 docState restore
1935 } def
1936
1937 /BeginDSCPage {
1938 % ---- when 1st column, save the state of the page
1939 ColumnIndex 1 eq { /pageState save def } if
1940 % ---- save the state of the column
1941 /columnState save def
1942 } def
1943
1944 /BeginPage {
1945 % ---- when 1st column, print all background effects
1946 ColumnIndex 1 eq {
1947 0 PrintStartY moveto % move to where printing will start
1948 Zebra {printZebra}if
1949 printGlobalBackground
1950 printLocalBackground
1951 } if
1952 PrintHeader {
1953 PrintHeaderFrame { HeaderFrame } if
1954 HeaderText
1955 } if
1956 0 PrintStartY moveto % move to where printing will start
1957 PLN
1958 } def
1959
1960 /EndPage {
1961 bg { eolbg } if
1962 } def
1963
1964 /EndDSCPage {
1965 ColumnIndex NumberOfColumns eq {
1966 % ---- on last column, spit out the page
1967 showpage
1968 % ---- restore the state of the page
1969 pageState restore
1970 /ColumnIndex 1 def
1971 } { % else
1972 % ---- restore the state of the current column
1973 columnState restore
1974 % ---- and translate to the next column
1975 ColumnWidth 0 translate
1976 /ColumnIndex ColumnIndex 1 add def
1977 } ifelse
1978 } def
1979
1980 /SetHeaderLines { % nb-lines --
1981 /HeaderLines exch def
1982 % ---- bottom up
1983 HeaderPad
1984 HeaderLines 1 sub HeaderLineHeight mul add
1985 HeaderTitleLineHeight add
1986 HeaderPad add
1987 /HeaderHeight exch def
1988 } def
1989
1990 % |---------|
1991 % | tm |
1992 % |---------|
1993 % | header |
1994 % |-+-------| <-- (x y)
1995 % | ho |
1996 % |---------|
1997 % | text |
1998 % |-+-------| <-- (0 0)
1999 % | bm |
2000 % |---------|
2001
2002 /HeaderFrameStart { % -- x y
2003 0 PrintHeight HeaderOffset add
2004 } def
2005
2006 /HeaderFramePath {
2007 PrintWidth 0 rlineto
2008 0 HeaderHeight rlineto
2009 PrintWidth neg 0 rlineto
2010 0 HeaderHeight neg rlineto
2011 } def
2012
2013 /HeaderFrame {
2014 gsave
2015 0.4 setlinewidth
2016 % ---- fill a black rectangle (the shadow of the next one)
2017 HeaderFrameStart moveto
2018 1 -1 rmoveto
2019 HeaderFramePath
2020 0 setgray fill
2021 % ---- do the next rectangle ...
2022 HeaderFrameStart moveto
2023 HeaderFramePath
2024 gsave 0.9 setgray fill grestore % filled with grey
2025 gsave 0 setgray stroke grestore % drawn with black
2026 grestore
2027 } def
2028
2029 /HeaderStart {
2030 HeaderFrameStart
2031 exch HeaderPad add exch % horizontal pad
2032 % ---- bottom up
2033 HeaderPad add % vertical pad
2034 HeaderDescent sub
2035 HeaderLineHeight HeaderLines 1 sub mul add
2036 } def
2037
2038 /strcat {
2039 dup length 3 -1 roll dup length dup 4 -1 roll add string dup
2040 0 5 -1 roll putinterval
2041 dup 4 2 roll exch putinterval
2042 } def
2043
2044 /pagenumberstring {
2045 PageNumber 32 string cvs
2046 ShowNofN {
2047 (/) strcat
2048 PageCount 32 string cvs strcat
2049 } if
2050 } def
2051
2052 /HeaderText {
2053 HeaderStart moveto
2054
2055 HeaderLinesRight HeaderLinesLeft % -- rightLines leftLines
2056
2057 % ---- hack: `PN 1 and' == `PN 2 modulo'
2058
2059 % ---- if duplex and even page number, then exchange left and right
2060 Duplex PageNumber 1 and 0 eq and { exch } if
2061
2062 { % ---- process the left lines
2063 aload pop
2064 exch F
2065 gsave
2066 dup xcheck { exec } if
2067 show
2068 grestore
2069 0 HeaderLineHeight neg rmoveto
2070 } forall
2071
2072 HeaderStart moveto
2073
2074 { % ---- process the right lines
2075 aload pop
2076 exch F
2077 gsave
2078 dup xcheck { exec } if
2079 dup stringwidth pop
2080 PrintWidth exch sub HeaderPad 2 mul sub 0 rmoveto
2081 show
2082 grestore
2083 0 HeaderLineHeight neg rmoveto
2084 } forall
2085 } def
2086
2087 /ReportFontInfo {
2088 2 copy
2089 /t0 3 1 roll DefFont
2090 /t0 F
2091 /lh FontHeight def
2092 /sw ( ) stringwidth pop def
2093 /aw (01234567890abcdefghijklmnopqrstuvwxyz) dup length exch
2094 stringwidth pop exch div def
2095 /t1 12 /Helvetica-Oblique DefFont
2096 /t1 F
2097 gsave
2098 (For ) show
2099 128 string cvs show
2100 ( ) show
2101 32 string cvs show
2102 ( point, the line height is ) show
2103 lh 32 string cvs show
2104 (, the space width is ) show
2105 sw 32 string cvs show
2106 (,) show
2107 grestore
2108 0 FontHeight neg rmoveto
2109 gsave
2110 (and a crude estimate of average character width is ) show
2111 aw 32 string cvs show
2112 (.) show
2113 grestore
2114 0 FontHeight neg rmoveto
2115 } def
2116
2117 /cm { % cm to point
2118 72 mul 2.54 div
2119 } def
2120
2121 /ReportAllFontInfo {
2122 FontDirectory
2123 { % key = font name value = font dictionary
2124 pop 10 exch ReportFontInfo
2125 } forall
2126 } def
2127
2128 % 3 cm 20 cm moveto 10 /Courier ReportFontInfo showpage
2129 % 3 cm 20 cm moveto ReportAllFontInfo showpage
2130
2131 ")
2132
2133 (defvar ps-print-prologue-2
2134 "
2135 % ---- These lines must be kept together because...
2136
2137 /h0 F
2138 /HeaderTitleLineHeight FontHeight def
2139
2140 /h1 F
2141 /HeaderLineHeight FontHeight def
2142 /HeaderDescent Descent def
2143
2144 % ---- ...because `F' has a side-effect on `FontHeight' and `Descent'
2145
2146 ")
2147
2148 ;; Start Editing Here:
2149
2150 (defvar ps-source-buffer nil)
2151 (defvar ps-spool-buffer-name "*PostScript*")
2152 (defvar ps-spool-buffer nil)
2153
2154 (defvar ps-output-head nil)
2155 (defvar ps-output-tail nil)
2156
2157 (defvar ps-page-count 0)
2158 (defvar ps-showline-count 1)
2159
2160 (defvar ps-background-pages nil)
2161 (defvar ps-background-all-pages nil)
2162 (defvar ps-background-text-count 0)
2163 (defvar ps-background-image-count 0)
2164
2165 (defvar ps-current-font 0)
2166 (defvar ps-default-color (if ps-print-color-p ps-default-fg)) ; black
2167 (defvar ps-current-color ps-default-color)
2168 (defvar ps-current-bg nil)
2169
2170 (defvar ps-razchunk 0)
2171
2172 (defvar ps-color-format
2173 (if (eq ps-print-emacs-type 'emacs)
2174
2175 ;;Emacs understands the %f format; we'll
2176 ;;use it to limit color RGB values to
2177 ;;three decimals to cut down some on the
2178 ;;size of the PostScript output.
2179 "%0.3f %0.3f %0.3f"
2180
2181 ;; Lucid emacsen will have to make do with
2182 ;; %s (princ) for floats.
2183 "%s %s %s"))
2184
2185 ;; These values determine how much print-height to deduct when headers
2186 ;; are turned on. This is a pretty clumsy way of handling it, but
2187 ;; it'll do for now.
2188
2189 (defvar ps-header-font nil)
2190 (defvar ps-header-title-font nil)
2191
2192 (defvar ps-header-line-height nil)
2193 (defvar ps-header-title-line-height nil)
2194 (defvar ps-header-pad 0
2195 "Vertical and horizontal space in points (1/72 inch) between the header frame
2196 and the text it contains.")
2197
2198 ;; Define accessors to the dimensions list.
2199
2200 (defmacro ps-page-dimensions-get-width (dims) `(nth 0 ,dims))
2201 (defmacro ps-page-dimensions-get-height (dims) `(nth 1 ,dims))
2202
2203 (defvar ps-landscape-page-height nil)
2204
2205 (defvar ps-print-width nil)
2206 (defvar ps-print-height nil)
2207
2208 (defvar ps-height-remaining)
2209 (defvar ps-width-remaining)
2210
2211 (defvar ps-print-color-scale nil)
2212
2213 \f
2214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2215 ;; Internal Variables
2216
2217
2218 (defvar ps-print-face-extension-alist nil
2219 "Alist of symbolic faces *WITH* extension features (box, outline, etc).
2220 An element of this list has the following form:
2221
2222 (FACE . [BITS FG BG])
2223
2224 FACE is a symbol denoting a face name
2225 BITS is a bit vector, where each bit correspond
2226 to a feature (bold, underline, etc)
2227 (see documentation for `ps-print-face-map-alist')
2228 FG foreground color (string or nil)
2229 BG background color (string or nil)
2230
2231 Don't change this list directly; instead,
2232 use `ps-extend-face' and `ps-extend-face-list'.
2233 See documentation for `ps-extend-face' for valid extension symbol.")
2234
2235
2236 (defvar ps-print-face-alist nil
2237 "Alist of symbolic faces *WITHOUT* extension features (box, outline, etc).
2238
2239 An element of this list has the same form as an element of
2240 `ps-print-face-extension-alist'.
2241
2242 Don't change this list directly; this list is used by `ps-face-attributes',
2243 `ps-map-face' and `ps-build-reference-face-lists'.")
2244
2245
2246 (defconst ps-print-face-map-alist
2247 '((bold . 1)
2248 (italic . 2)
2249 (underline . 4)
2250 (strikeout . 8)
2251 (overline . 16)
2252 (shadow . 32)
2253 (box . 64)
2254 (outline . 128))
2255 "Alist of all features and the corresponding bit mask.
2256 Each symbol correspond to one bit in a bit vector.")
2257
2258 \f
2259 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2260 ;; Remapping Faces
2261
2262
2263 ;;;###autoload
2264 (defun ps-extend-face-list (face-extension-list &optional merge-p)
2265 "Extend face in `ps-print-face-extension-alist'.
2266
2267 If optional MERGE-P is non-nil, extensions in FACE-EXTENSION-LIST are merged
2268 with face extension in `ps-print-face-extension-alist'; otherwise, overrides.
2269
2270 The elements in FACE-EXTENSION-LIST is like those for `ps-extend-face'.
2271
2272 See `ps-extend-face' for documentation."
2273 (while face-extension-list
2274 (ps-extend-face (car face-extension-list) merge-p)
2275 (setq face-extension-list (cdr face-extension-list))))
2276
2277
2278 ;;;###autoload
2279 (defun ps-extend-face (face-extension &optional merge-p)
2280 "Extend face in `ps-print-face-extension-alist'.
2281
2282 If optional MERGE-P is non-nil, extensions in FACE-EXTENSION-LIST are merged
2283 with face extensions in `ps-print-face-extension-alist'; otherwise, overrides.
2284
2285 The elements of FACE-EXTENSION list have the form:
2286
2287 (FACE-NAME FOREGROUND BACKGROUND EXTENSION...)
2288
2289 FACE-NAME is a face name symbol.
2290
2291 FOREGROUND and BACKGROUND may be nil or a string that denotes the
2292 foreground and background colors respectively.
2293
2294 EXTENSION is one of the following symbols:
2295 bold - use bold font.
2296 italic - use italic font.
2297 underline - put a line under text.
2298 strikeout - like underline, but the line is in middle of text.
2299 overline - like underline, but the line is over the text.
2300 shadow - text will have a shadow.
2301 box - text will be surrounded by a box.
2302 outline - print characters as hollow outlines.
2303
2304 If EXTENSION is any other symbol, it is ignored."
2305 (let* ((face-name (nth 0 face-extension))
2306 (foreground (nth 1 face-extension))
2307 (background (nth 2 face-extension))
2308 (ps-face (cdr (assq face-name ps-print-face-extension-alist)))
2309 (face-vector (or ps-face (vector 0 nil nil)))
2310 (face-bit (ps-extension-bit face-extension)))
2311 ;; extend face
2312 (aset face-vector 0 (if merge-p
2313 (logior (aref face-vector 0) face-bit)
2314 face-bit))
2315 (and foreground (stringp foreground) (aset face-vector 1 foreground))
2316 (and background (stringp background) (aset face-vector 2 background))
2317 ;; if face does not exist, insert it
2318 (or ps-face
2319 (setq ps-print-face-extension-alist
2320 (cons (cons face-name face-vector)
2321 ps-print-face-extension-alist)))))
2322
2323
2324 (defun ps-extension-bit (face-extension)
2325 (let ((face-bit 0))
2326 ;; map valid symbol extension to bit vector
2327 (setq face-extension (cdr (cdr face-extension)))
2328 (while (setq face-extension (cdr face-extension))
2329 (setq face-bit (logior face-bit
2330 (or (cdr (assq (car face-extension)
2331 ps-print-face-map-alist))
2332 0))))
2333 face-bit))
2334
2335 \f
2336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2337 ;; Internal functions and variables
2338
2339
2340 (defun ps-print-without-faces (from to &optional filename region-p)
2341 (ps-printing-region region-p)
2342 (ps-generate (current-buffer) from to 'ps-generate-postscript)
2343 (ps-do-despool filename))
2344
2345
2346 (defun ps-spool-without-faces (from to &optional region-p)
2347 (ps-printing-region region-p)
2348 (ps-generate (current-buffer) from to 'ps-generate-postscript))
2349
2350
2351 (defun ps-print-with-faces (from to &optional filename region-p)
2352 (ps-printing-region region-p)
2353 (ps-generate (current-buffer) from to 'ps-generate-postscript-with-faces)
2354 (ps-do-despool filename))
2355
2356
2357 (defun ps-spool-with-faces (from to &optional region-p)
2358 (ps-printing-region region-p)
2359 (ps-generate (current-buffer) from to 'ps-generate-postscript-with-faces))
2360
2361
2362 (defsubst ps-count-lines (from to)
2363 (+ (count-lines from to)
2364 (save-excursion (goto-char to)
2365 (if (= (current-column) 0) 1 0))))
2366
2367
2368 (defvar ps-printing-region nil
2369 "Variable used to indicate if it is printing a region.
2370 If non-nil, it is a cons, the car of which is the line number
2371 where the region begins, and its cdr is the total number of lines
2372 in the buffer. Formatting functions can use this information
2373 to print the original line number (and not the number of lines printed),
2374 and to indicate in the header that the printout is of a partial file.")
2375
2376
2377 (defun ps-printing-region (region-p)
2378 (setq ps-printing-region
2379 (and region-p
2380 (cons (ps-count-lines (point-min) (region-beginning))
2381 (ps-count-lines (point-min) (point-max))))))
2382
2383 \f
2384 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2385 ;; Internal functions
2386
2387 (defun ps-line-lengths-internal ()
2388 "Display the correspondence between a line length and a font size,
2389 using the current ps-print setup.
2390 Try: pr -t file | awk '{printf \"%3d %s\n\", length($0), $0}' | sort -r | head"
2391 (let ((buf (get-buffer-create "*Line-lengths*"))
2392 (ifs ps-font-size) ; initial font size
2393 (icw ps-avg-char-width) ; initial character width
2394 (print-width (progn (ps-get-page-dimensions)
2395 ps-print-width))
2396 (ps-setup (ps-setup)) ; setup for the current buffer
2397 (fs-min 5) ; minimum font size
2398 cw-min ; minimum character width
2399 nb-cpl-max ; maximum nb of characters per line
2400 (fs-max 14) ; maximum font size
2401 cw-max ; maximum character width
2402 nb-cpl-min ; minimum nb of characters per line
2403 fs ; current font size
2404 cw ; current character width
2405 nb-cpl ; current nb of characters per line
2406 )
2407 (setq cw-min (/ (* icw fs-min) ifs)
2408 nb-cpl-max (floor (/ print-width cw-min))
2409 cw-max (/ (* icw fs-max) ifs)
2410 nb-cpl-min (floor (/ print-width cw-max)))
2411 (setq nb-cpl nb-cpl-min)
2412 (set-buffer buf)
2413 (goto-char (point-max))
2414 (if (not (bolp)) (insert "\n"))
2415 (insert ps-setup)
2416 (insert "nb char per line / font size\n")
2417 (while (<= nb-cpl nb-cpl-max)
2418 (setq cw (/ print-width (float nb-cpl))
2419 fs (/ (* ifs cw) icw))
2420 (insert (format "%3s %s\n" nb-cpl fs))
2421 (setq nb-cpl (1+ nb-cpl)))
2422 (insert "\n")
2423 (display-buffer buf 'not-this-window)))
2424
2425 (defun ps-nb-pages (nb-lines)
2426 "Display an approximate correspondence between a font size and the number
2427 of pages the number of lines would require to print
2428 using the current ps-print setup."
2429 (let ((buf (get-buffer-create "*Nb-Pages*"))
2430 (ifs ps-font-size) ; initial font size
2431 (ilh ps-line-height) ; initial line height
2432 (page-height (progn (ps-get-page-dimensions)
2433 ps-print-height))
2434 (ps-setup (ps-setup)) ; setup for the current buffer
2435 (fs-min 4) ; minimum font size
2436 lh-min ; minimum line height
2437 nb-lpp-max ; maximum nb of lines per page
2438 nb-page-min ; minimum nb of pages
2439 (fs-max 14) ; maximum font size
2440 lh-max ; maximum line height
2441 nb-lpp-min ; minimum nb of lines per page
2442 nb-page-max ; maximum nb of pages
2443 fs ; current font size
2444 lh ; current line height
2445 nb-lpp ; current nb of lines per page
2446 nb-page ; current nb of pages
2447 )
2448 (setq lh-min (/ (* ilh fs-min) ifs)
2449 nb-lpp-max (floor (/ page-height lh-min))
2450 nb-page-min (ceiling (/ (float nb-lines) nb-lpp-max))
2451 lh-max (/ (* ilh fs-max) ifs)
2452 nb-lpp-min (floor (/ page-height lh-max))
2453 nb-page-max (ceiling (/ (float nb-lines) nb-lpp-min)))
2454 (setq nb-page nb-page-min)
2455 (set-buffer buf)
2456 (goto-char (point-max))
2457 (if (not (bolp)) (insert "\n"))
2458 (insert ps-setup)
2459 (insert (format "%d lines\n" nb-lines))
2460 (insert "nb page / font size\n")
2461 (while (<= nb-page nb-page-max)
2462 (setq nb-lpp (ceiling (/ nb-lines (float nb-page)))
2463 lh (/ page-height nb-lpp)
2464 fs (/ (* ifs lh) ilh))
2465 (insert (format "%s %s\n" nb-page fs))
2466 (setq nb-page (1+ nb-page)))
2467 (insert "\n")
2468 (display-buffer buf 'not-this-window)))
2469
2470 (defun ps-select-font ()
2471 "Choose the font name and size (scaling data)."
2472 (let ((assoc (assq ps-font-family ps-font-info-database))
2473 l fn fb fi bi sz lh sw aw)
2474 (if (null assoc)
2475 (error "Don't have data to scale font %s. Known fonts families are %s"
2476 ps-font-family
2477 (mapcar 'car ps-font-info-database)))
2478 (setq l (cdr assoc)
2479 fn (prog1 (car l) (setq l (cdr l))) ; need `pop'
2480 fb (prog1 (car l) (setq l (cdr l)))
2481 fi (prog1 (car l) (setq l (cdr l)))
2482 bi (prog1 (car l) (setq l (cdr l)))
2483 sz (prog1 (car l) (setq l (cdr l)))
2484 lh (prog1 (car l) (setq l (cdr l)))
2485 sw (prog1 (car l) (setq l (cdr l)))
2486 aw (prog1 (car l) (setq l (cdr l))))
2487
2488 (setq ps-font fn)
2489 (setq ps-font-bold fb)
2490 (setq ps-font-italic fi)
2491 (setq ps-font-bold-italic bi)
2492 ;; These data just need to be rescaled:
2493 (setq ps-line-height (/ (* lh ps-font-size) sz))
2494 (setq ps-space-width (/ (* sw ps-font-size) sz))
2495 (setq ps-avg-char-width (/ (* aw ps-font-size) sz))
2496 ps-font-family))
2497
2498 (defun ps-select-header-font ()
2499 "Choose the font name and size (scaling data) for the header."
2500 (let ((assoc (assq ps-header-font-family ps-font-info-database))
2501 l fn fb fi bi sz lh sw aw)
2502 (if (null assoc)
2503 (error "Don't have data to scale font %s. Known fonts families are %s"
2504 ps-font-family
2505 (mapcar 'car ps-font-info-database)))
2506 (setq l (cdr assoc)
2507 fn (prog1 (car l) (setq l (cdr l))) ; need `pop'
2508 fb (prog1 (car l) (setq l (cdr l)))
2509 fi (prog1 (car l) (setq l (cdr l)))
2510 bi (prog1 (car l) (setq l (cdr l)))
2511 sz (prog1 (car l) (setq l (cdr l)))
2512 lh (prog1 (car l) (setq l (cdr l)))
2513 sw (prog1 (car l) (setq l (cdr l)))
2514 aw (prog1 (car l) (setq l (cdr l))))
2515
2516 ;; Font name
2517 (setq ps-header-font fn)
2518 (setq ps-header-title-font fb)
2519 ;; Line height: These data just need to be rescaled:
2520 (setq ps-header-title-line-height (/ (* lh ps-header-title-font-size) sz))
2521 (setq ps-header-line-height (/ (* lh ps-header-font-size) sz))
2522 ps-header-font-family))
2523
2524 (defun ps-get-page-dimensions ()
2525 (let ((page-dimensions (cdr (assq ps-paper-type ps-page-dimensions-database)))
2526 page-width page-height)
2527 (cond
2528 ((null page-dimensions)
2529 (error "`ps-paper-type' must be one of:\n%s"
2530 (mapcar 'car ps-page-dimensions-database)))
2531 ((< ps-number-of-columns 1)
2532 (error "The number of columns %d should not be negative" ps-number-of-columns)))
2533
2534 (ps-select-font)
2535 (ps-select-header-font)
2536
2537 (setq page-width (ps-page-dimensions-get-width page-dimensions)
2538 page-height (ps-page-dimensions-get-height page-dimensions))
2539
2540 ;; Landscape mode
2541 (if ps-landscape-mode
2542 ;; exchange width and height
2543 (setq page-width (prog1 page-height (setq page-height page-width))))
2544
2545 ;; It is used to get the lower right corner (only in landscape mode)
2546 (setq ps-landscape-page-height page-height)
2547
2548 ;; | lm | text | ic | text | ic | text | rm |
2549 ;; page-width == lm + n * pw + (n - 1) * ic + rm
2550 ;; => pw == (page-width - lm -rm - (n - 1) * ic) / n
2551 (setq ps-print-width
2552 (/ (- page-width
2553 ps-left-margin ps-right-margin
2554 (* (1- ps-number-of-columns) ps-inter-column))
2555 ps-number-of-columns))
2556 (if (<= ps-print-width 0)
2557 (error "Bad horizontal layout:
2558 page-width == %s
2559 ps-left-margin == %s
2560 ps-right-margin == %s
2561 ps-inter-column == %s
2562 ps-number-of-columns == %s
2563 | lm | text | ic | text | ic | text | rm |
2564 page-width == lm + n * print-width + (n - 1) * ic + rm
2565 => print-width == %d !"
2566 page-width
2567 ps-left-margin
2568 ps-right-margin
2569 ps-inter-column
2570 ps-number-of-columns
2571 ps-print-width))
2572
2573 (setq ps-print-height
2574 (- page-height ps-bottom-margin ps-top-margin))
2575 (if (<= ps-print-height 0)
2576 (error "Bad vertical layout:
2577 ps-top-margin == %s
2578 ps-bottom-margin == %s
2579 page-height == bm + print-height + tm
2580 => print-height == %d !"
2581 ps-top-margin
2582 ps-bottom-margin
2583 ps-print-height))
2584 ;; If headers are turned on, deduct the height of the header from
2585 ;; the print height.
2586 (cond
2587 (ps-print-header
2588 (setq ps-header-pad
2589 (* ps-header-line-pad ps-header-title-line-height))
2590 (setq ps-print-height
2591 (- ps-print-height
2592 ps-header-offset
2593 ps-header-pad
2594 ps-header-title-line-height
2595 (* ps-header-line-height (- ps-header-lines 1))
2596 ps-header-pad))))
2597 (if (<= ps-print-height 0)
2598 (error "Bad vertical layout:
2599 ps-top-margin == %s
2600 ps-bottom-margin == %s
2601 ps-header-offset == %s
2602 ps-header-pad == %s
2603 header-height == %s
2604 page-height == bm + print-height + tm - ho - hh
2605 => print-height == %d !"
2606 ps-top-margin
2607 ps-bottom-margin
2608 ps-header-offset
2609 ps-header-pad
2610 (+ ps-header-pad
2611 ps-header-title-line-height
2612 (* ps-header-line-height (- ps-header-lines 1))
2613 ps-header-pad)
2614 ps-print-height))))
2615
2616 (defun ps-print-preprint (&optional filename)
2617 (if (and filename
2618 (or (numberp filename)
2619 (listp filename)))
2620 (let* ((name (concat (buffer-name) ".ps"))
2621 (prompt (format "Save PostScript to file: (default %s) "
2622 name))
2623 (res (read-file-name prompt default-directory name nil)))
2624 (if (file-directory-p res)
2625 (expand-file-name name (file-name-as-directory res))
2626 res))))
2627
2628 ;; The following functions implement a simple list-buffering scheme so
2629 ;; that ps-print doesn't have to repeatedly switch between buffers
2630 ;; while spooling. The functions ps-output and ps-output-string build
2631 ;; up the lists; the function ps-flush-output takes the lists and
2632 ;; insert its contents into the spool buffer (*PostScript*).
2633
2634 (defun ps-output-string-prim (string)
2635 (insert "(") ;insert start-string delimiter
2636 (save-excursion ;insert string
2637 (insert string))
2638
2639 ;; Find and quote special characters as necessary for PS
2640 (while (re-search-forward "[()\\]" nil t)
2641 (save-excursion
2642 (forward-char -1)
2643 (insert "\\")))
2644
2645 (goto-char (point-max))
2646 (insert ")")) ;insert end-string delimiter
2647
2648 (defun ps-init-output-queue ()
2649 (setq ps-output-head (list ""))
2650 (setq ps-output-tail ps-output-head))
2651
2652 (defun ps-output (&rest args)
2653 (setcdr ps-output-tail args)
2654 (while (cdr ps-output-tail)
2655 (setq ps-output-tail (cdr ps-output-tail))))
2656
2657 (defun ps-output-string (string)
2658 (ps-output t string))
2659
2660 (defun ps-output-list (the-list)
2661 (mapcar 'ps-output the-list))
2662
2663 (defun ps-flush-output ()
2664 (save-excursion
2665 (set-buffer ps-spool-buffer)
2666 (goto-char (point-max))
2667 (while ps-output-head
2668 (let ((it (car ps-output-head)))
2669 (if (not (eq t it))
2670 (insert it)
2671 (setq ps-output-head (cdr ps-output-head))
2672 (ps-output-string-prim (car ps-output-head))))
2673 (setq ps-output-head (cdr ps-output-head))))
2674 (ps-init-output-queue))
2675
2676 (defun ps-insert-file (fname)
2677 (ps-flush-output)
2678 ;; Check to see that the file exists and is readable; if not, throw
2679 ;; an error.
2680 (or (file-readable-p fname)
2681 (error "Could not read file `%s'" fname))
2682 (save-excursion
2683 (set-buffer ps-spool-buffer)
2684 (goto-char (point-max))
2685 (insert-file fname)))
2686
2687 ;; These functions insert the arrays that define the contents of the
2688 ;; headers.
2689
2690 (defun ps-generate-header-line (fonttag &optional content)
2691 (ps-output " [ " fonttag " ")
2692 (cond
2693 ;; Literal strings should be output as is -- the string must
2694 ;; contain its own PS string delimiters, '(' and ')', if necessary.
2695 ((stringp content)
2696 (ps-output content))
2697
2698 ;; Functions are called -- they should return strings; they will be
2699 ;; inserted as strings and the PS string delimiters added.
2700 ((and (symbolp content) (fboundp content))
2701 (ps-output-string (funcall content)))
2702
2703 ;; Variables will have their contents inserted. They should
2704 ;; contain strings, and will be inserted as strings.
2705 ((and (symbolp content) (boundp content))
2706 (ps-output-string (symbol-value content)))
2707
2708 ;; Anything else will get turned into an empty string.
2709 (t
2710 (ps-output-string "")))
2711 (ps-output " ]\n"))
2712
2713 (defun ps-generate-header (name contents)
2714 (ps-output "/" name " [\n")
2715 (if (> ps-header-lines 0)
2716 (let ((count 1))
2717 (ps-generate-header-line "/h0" (car contents))
2718 (while (and (< count ps-header-lines)
2719 (setq contents (cdr contents)))
2720 (ps-generate-header-line "/h1" (car contents))
2721 (setq count (+ count 1)))
2722 (ps-output "] def\n"))))
2723
2724 (defun ps-output-boolean (name bool)
2725 (ps-output (format "/%s %s def\n" name (if bool "true" "false"))))
2726
2727 (defun ps-background-pages (page-list func)
2728 (if page-list
2729 (mapcar
2730 '(lambda (pages)
2731 (let ((start (if (consp pages) (car pages) pages))
2732 (end (if (consp pages) (cdr pages) pages)))
2733 (and (integerp start) (integerp end) (<= start end)
2734 (add-to-list 'ps-background-pages (vector start end func)))))
2735 page-list)
2736 (setq ps-background-all-pages (cons func ps-background-all-pages))))
2737
2738
2739 (defun ps-get-boundingbox ()
2740 (save-excursion
2741 (set-buffer ps-spool-buffer)
2742 (save-excursion
2743 (if (re-search-forward
2744 "^%%BoundingBox:\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)\\s-+\\([0-9.]+\\)"
2745 nil t)
2746 (vector (string-to-number ; lower x
2747 (buffer-substring (match-beginning 1) (match-end 1)))
2748 (string-to-number ; lower y
2749 (buffer-substring (match-beginning 2) (match-end 2)))
2750 (string-to-number ; upper x
2751 (buffer-substring (match-beginning 3) (match-end 3)))
2752 (string-to-number ; upper y
2753 (buffer-substring (match-beginning 4) (match-end 4))))
2754 (vector 0 0 0 0)))))
2755
2756
2757 ;; Emacs understands the %f format; we'll use it to limit color RGB values
2758 ;; to three decimals to cut down some on the size of the PostScript output.
2759 ;; Lucid emacsen will have to make do with %s (princ) for floats.
2760
2761 (defvar ps-float-format (if (eq ps-print-emacs-type 'emacs)
2762 "%0.3f " ; emacs
2763 "%s ")) ; Lucid emacsen
2764
2765
2766 (defun ps-float-format (value &optional default)
2767 (let ((literal (or value default)))
2768 (if literal
2769 (format (if (numberp literal)
2770 ps-float-format
2771 "%s ")
2772 literal)
2773 " ")))
2774
2775
2776 (defun ps-background-text ()
2777 (mapcar
2778 '(lambda (text)
2779 (setq ps-background-text-count (1+ ps-background-text-count))
2780 (ps-output (format "/ShowBackText-%d {\n" ps-background-text-count))
2781 (ps-output-string (nth 0 text)) ; text
2782 (ps-output
2783 "\n"
2784 (ps-float-format (nth 4 text) 200.0) ; font size
2785 (format "/%s " (or (nth 3 text) "Times-Roman")) ; font name
2786 (ps-float-format (nth 6 text)
2787 "PrintHeight PrintPageWidth atan") ; rotation
2788 (ps-float-format (nth 5 text) 0.85) ; gray
2789 (ps-float-format (nth 1 text) "0") ; x position
2790 (ps-float-format (nth 2 text) "BottomMargin") ; y position
2791 "\nShowBackText} def\n")
2792 (ps-background-pages (nthcdr 7 text) ; page list
2793 (format "ShowBackText-%d\n"
2794 ps-background-text-count)))
2795 ps-print-background-text))
2796
2797
2798 (defun ps-background-image ()
2799 (mapcar
2800 '(lambda (image)
2801 (let ((image-file (expand-file-name (nth 0 image))))
2802 (if (file-readable-p image-file)
2803 (progn
2804 (setq ps-background-image-count (1+ ps-background-image-count))
2805 (ps-output
2806 (format "/ShowBackImage-%d {\n--back-- " ps-background-image-count)
2807 (ps-float-format (nth 5 image) 0.0) ; rotation
2808 (ps-float-format (nth 3 image) 1.0) ; x scale
2809 (ps-float-format (nth 4 image) 1.0) ; y scale
2810 (ps-float-format (nth 1 image) ; x position
2811 "PrintPageWidth 2 div")
2812 (ps-float-format (nth 2 image) ; y position
2813 "PrintHeight 2 div BottomMargin add")
2814 "\nBeginBackImage\n")
2815 (ps-insert-file image-file)
2816 ;; coordinate adjustment to centralize image
2817 ;; around x and y position
2818 (let ((box (ps-get-boundingbox)))
2819 (save-excursion
2820 (set-buffer ps-spool-buffer)
2821 (save-excursion
2822 (if (re-search-backward "^--back--" nil t)
2823 (replace-match
2824 (format "%s %s"
2825 (ps-float-format
2826 (- (+ (/ (- (aref box 2) (aref box 0)) 2.0)
2827 (aref box 0))))
2828 (ps-float-format
2829 (- (+ (/ (- (aref box 3) (aref box 1)) 2.0)
2830 (aref box 1)))))
2831 t)))))
2832 (ps-output "\nEndBackImage} def\n")
2833 (ps-background-pages (nthcdr 6 image) ; page list
2834 (format "ShowBackImage-%d\n"
2835 ps-background-image-count))))))
2836 ps-print-background-image))
2837
2838
2839 (defun ps-background (page-number)
2840 (let (has-local-background)
2841 (mapcar '(lambda (range)
2842 (and (<= (aref range 0) page-number)
2843 (<= page-number (aref range 1))
2844 (if has-local-background
2845 (ps-output (aref range 2))
2846 (setq has-local-background t)
2847 (ps-output "/printLocalBackground {\n"
2848 (aref range 2)))))
2849 ps-background-pages)
2850 (and has-local-background (ps-output "} def\n"))))
2851
2852
2853 (defun ps-begin-file ()
2854 (ps-get-page-dimensions)
2855 (setq ps-showline-count (if ps-printing-region (car ps-printing-region) 1)
2856 ps-background-text-count 0
2857 ps-background-image-count 0
2858 ps-background-pages nil
2859 ps-background-all-pages nil)
2860
2861 (ps-output ps-adobe-tag)
2862 (ps-output "%%Title: " (buffer-name)) ;Take job name from name of
2863 ;first buffer printed
2864 (ps-output "\n%%Creator: " (user-full-name))
2865 (ps-output "\n%%CreationDate: "
2866 (time-stamp-hh:mm:ss) " " (time-stamp-mon-dd-yyyy)
2867 "\n%%Orientation: "
2868 (if ps-landscape-mode "Landscape" "Portrait"))
2869 (ps-output "\n%% DocumentFonts: Times-Roman Times-Italic "
2870 ps-font " " ps-font-bold " " ps-font-italic " "
2871 ps-font-bold-italic " "
2872 ps-header-font " " ps-header-title-font)
2873 (ps-output "\n%%Pages: (atend)\n")
2874 (ps-output "%%EndComments\n\n")
2875
2876 (ps-output-boolean "LandscapeMode" ps-landscape-mode)
2877 (ps-output (format "/NumberOfColumns %d def\n" ps-number-of-columns))
2878
2879 (ps-output (format "/LandscapePageHeight %s def\n" ps-landscape-page-height))
2880 (ps-output (format "/PrintPageWidth %s def\n"
2881 (- (* (+ ps-print-width ps-inter-column)
2882 ps-number-of-columns)
2883 ps-inter-column)))
2884 (ps-output (format "/PrintWidth %s def\n" ps-print-width))
2885 (ps-output (format "/PrintHeight %s def\n" ps-print-height))
2886
2887 (ps-output (format "/LeftMargin %s def\n" ps-left-margin))
2888 (ps-output (format "/RightMargin %s def\n" ps-right-margin)) ; not used
2889 (ps-output (format "/InterColumn %s def\n" ps-inter-column))
2890
2891 (ps-output (format "/BottomMargin %s def\n" ps-bottom-margin))
2892 (ps-output (format "/TopMargin %s def\n" ps-top-margin)) ; not used
2893 (ps-output (format "/HeaderOffset %s def\n" ps-header-offset))
2894 (ps-output (format "/HeaderPad %s def\n" ps-header-pad))
2895
2896 (ps-output-boolean "PrintHeader" ps-print-header)
2897 (ps-output-boolean "PrintHeaderFrame" ps-print-header-frame)
2898 (ps-output-boolean "ShowNofN" ps-show-n-of-n)
2899 (ps-output-boolean "Duplex" ps-spool-duplex)
2900
2901 (ps-output (format "/LineHeight %s def\n" ps-line-height)
2902 (format "/LinesPerColumn %d def\n"
2903 (round (/ (+ ps-print-height
2904 (* ps-line-height 0.45))
2905 ps-line-height))))
2906
2907 (ps-output-boolean "Zebra" ps-zebra-stripes)
2908 (ps-output (format "/NumberOfZebra %d def\n" ps-zebra-stripe-height))
2909
2910 (ps-output-boolean "PrintLineNumber" ps-line-number)
2911 (ps-output (format "/Lines %d def\n"
2912 (if ps-printing-region
2913 (cdr ps-printing-region)
2914 (ps-count-lines (point-min) (point-max)))))
2915
2916 (ps-background-text)
2917 (ps-background-image)
2918 (setq ps-background-all-pages (nreverse ps-background-all-pages)
2919 ps-background-pages (nreverse ps-background-pages))
2920
2921 (ps-output ps-print-prologue-1)
2922
2923 (ps-output "/printGlobalBackground {\n")
2924 (ps-output-list ps-background-all-pages)
2925 (ps-output "} def\n/printLocalBackground {\n} def\n")
2926
2927 ;; Header fonts
2928 (ps-output ; /h0 14 /Helvetica-Bold Font
2929 (format "/h0 %s /%s DefFont\n" ps-header-title-font-size ps-header-title-font))
2930 (ps-output ; /h1 12 /Helvetica Font
2931 (format "/h1 %s /%s DefFont\n" ps-header-font-size ps-header-font))
2932
2933 (ps-output ps-print-prologue-2)
2934
2935 ;; Text fonts
2936 (ps-output (format "/f0 %s /%s DefFont\n" ps-font-size ps-font))
2937 (ps-output (format "/f1 %s /%s DefFont\n" ps-font-size ps-font-bold))
2938 (ps-output (format "/f2 %s /%s DefFont\n" ps-font-size ps-font-italic))
2939 (ps-output (format "/f3 %s /%s DefFont\n" ps-font-size ps-font-bold-italic))
2940
2941 (ps-output "\nBeginDoc\n\n")
2942 (ps-output "%%EndPrologue\n"))
2943
2944 (defun ps-header-dirpart ()
2945 (let ((fname (buffer-file-name)))
2946 (if fname
2947 (if (string-equal (buffer-name) (file-name-nondirectory fname))
2948 (file-name-directory fname)
2949 fname)
2950 "")))
2951
2952 (defun ps-get-buffer-name ()
2953 (cond
2954 ;; Indulge Jim this little easter egg:
2955 ((string= (buffer-name) "ps-print.el")
2956 "Hey, Cool! It's ps-print.el!!!")
2957 ;; Indulge Jack this other little easter egg:
2958 ((string= (buffer-name) "sokoban.el")
2959 "Super! C'est sokoban.el!")
2960 (t (concat
2961 (and ps-printing-region "Subset of: ")
2962 (buffer-name)
2963 (and (buffer-modified-p) " (unsaved)")))))
2964
2965 (defun ps-begin-job ()
2966 (setq ps-page-count 0))
2967
2968 (defun ps-end-file ()
2969 (ps-output "\n%%Trailer\n")
2970 (ps-output (format "%%%%Pages: %d\n" (1+ (/ (1- ps-page-count)
2971 ps-number-of-columns))))
2972 (ps-output "\nEndDoc\n\n%%EOF\n"))
2973
2974
2975 (defun ps-next-page ()
2976 (ps-end-page)
2977 (ps-flush-output)
2978 (ps-begin-page))
2979
2980 (defun ps-header-page (&optional inc-p)
2981 (if (zerop (mod ps-page-count ps-number-of-columns))
2982 ;; Print only when a new real page begins.
2983 (let ((page-number (1+ (/ ps-page-count ps-number-of-columns))))
2984 (ps-output (format "\n%%%%Page: %d %d\n" page-number page-number))
2985 (ps-output "BeginDSCPage\n")
2986 (ps-background page-number)
2987 (and inc-p (incf ps-page-count)))
2988 ;; Print when any other page begins.
2989 (ps-output "BeginDSCPage\n")))
2990
2991 (defun ps-begin-page (&optional dummypage)
2992 (ps-get-page-dimensions)
2993 (setq ps-width-remaining ps-print-width)
2994 (setq ps-height-remaining ps-print-height)
2995
2996 (ps-header-page)
2997
2998 (ps-output (format "/LineNumber %d def\n" ps-showline-count)
2999 (format "/PageNumber %d def\n" (incf ps-page-count)))
3000 (ps-output "/PageCount 0 def\n")
3001
3002 (when ps-print-header
3003 (ps-generate-header "HeaderLinesLeft" ps-left-header)
3004 (ps-generate-header "HeaderLinesRight" ps-right-header)
3005 (ps-output (format "%d SetHeaderLines\n" ps-header-lines)))
3006
3007 (ps-output "BeginPage\n")
3008 (ps-set-font ps-current-font)
3009 (ps-set-bg ps-current-bg)
3010 (ps-set-color ps-current-color))
3011
3012 (defun ps-end-page ()
3013 (ps-output "EndPage\nEndDSCPage\n"))
3014
3015 (defun ps-dummy-page ()
3016 (ps-header-page t)
3017 (ps-output "/PrintHeader false def
3018 BeginPage
3019 EndPage
3020 EndDSCPage\n"))
3021
3022 (defun ps-next-line ()
3023 (setq ps-showline-count (1+ ps-showline-count))
3024 (if (< ps-height-remaining ps-line-height)
3025 (ps-next-page)
3026 (setq ps-width-remaining ps-print-width)
3027 (setq ps-height-remaining (- ps-height-remaining ps-line-height))
3028 (ps-hard-lf)))
3029
3030 (defun ps-continue-line ()
3031 (if (< ps-height-remaining ps-line-height)
3032 (ps-next-page)
3033 (setq ps-width-remaining ps-print-width)
3034 (setq ps-height-remaining (- ps-height-remaining ps-line-height))
3035 (ps-soft-lf)))
3036
3037 ;; [jack] Why hard and soft ?
3038
3039 (defun ps-hard-lf ()
3040 (ps-output "HL\n"))
3041
3042 (defun ps-soft-lf ()
3043 (ps-output "SL\n"))
3044
3045 (defun ps-find-wrappoint (from to char-width)
3046 (let ((avail (truncate (/ ps-width-remaining char-width)))
3047 (todo (- to from)))
3048 (if (< todo avail)
3049 (cons to (* todo char-width))
3050 (cons (+ from avail) ps-width-remaining))))
3051
3052 (defun ps-basic-plot-string (from to &optional bg-color)
3053 (let* ((wrappoint (ps-find-wrappoint from to ps-avg-char-width))
3054 (to (car wrappoint))
3055 (string (buffer-substring-no-properties from to)))
3056 (ps-output-string string)
3057 (ps-output " S\n")
3058 wrappoint))
3059
3060 (defun ps-basic-plot-whitespace (from to &optional bg-color)
3061 (let* ((wrappoint (ps-find-wrappoint from to ps-space-width))
3062 (to (car wrappoint)))
3063 (ps-output (format "%d W\n" (- to from)))
3064 wrappoint))
3065
3066 (defun ps-plot (plotfunc from to &optional bg-color)
3067 (while (< from to)
3068 (let* ((wrappoint (funcall plotfunc from to bg-color))
3069 (plotted-to (car wrappoint))
3070 (plotted-width (cdr wrappoint)))
3071 (setq from plotted-to)
3072 (setq ps-width-remaining (- ps-width-remaining plotted-width))
3073 (if (< from to)
3074 (ps-continue-line))))
3075 (if ps-razzle-dazzle
3076 (let* ((q-todo (- (point-max) (point-min)))
3077 (q-done (- (point) (point-min)))
3078 (chunkfrac (/ q-todo 8))
3079 (chunksize (if (> chunkfrac 1000) 1000 chunkfrac)))
3080 (if (> (- q-done ps-razchunk) chunksize)
3081 (let (foo)
3082 (setq ps-razchunk q-done)
3083 (setq foo
3084 (if (< q-todo 100)
3085 (/ (* 100 q-done) q-todo)
3086 (/ q-done (/ q-todo 100))))
3087 (message "Formatting...%3d%%" foo))))))
3088
3089 (defun ps-set-font (font)
3090 (setq ps-current-font font)
3091 (ps-output (format "/f%d F\n" ps-current-font)))
3092
3093 (defun ps-set-bg (color)
3094 (if (setq ps-current-bg color)
3095 (ps-output (format ps-color-format (nth 0 color) (nth 1 color)
3096 (nth 2 color))
3097 " true BG\n")
3098 (ps-output "false BG\n")))
3099
3100 (defun ps-set-color (color)
3101 (setq ps-current-color (or color ps-default-fg))
3102 (ps-output (format ps-color-format (nth 0 ps-current-color)
3103 (nth 1 ps-current-color) (nth 2 ps-current-color))
3104 " FG\n"))
3105
3106
3107 (defvar ps-current-effect 0)
3108
3109
3110 (defun ps-plot-region (from to font &optional fg-color bg-color effects)
3111 (if (not (equal font ps-current-font))
3112 (ps-set-font font))
3113
3114 ;; Specify a foreground color only if one's specified and it's
3115 ;; different than the current.
3116 (if (not (equal fg-color ps-current-color))
3117 (ps-set-color fg-color))
3118
3119 (if (not (equal bg-color ps-current-bg))
3120 (ps-set-bg bg-color))
3121
3122 ;; Specify effects (underline, overline, box, etc)
3123 (cond
3124 ((not (integerp effects))
3125 (ps-output "0 EF\n")
3126 (setq ps-current-effect 0))
3127 ((/= effects ps-current-effect)
3128 (ps-output (number-to-string effects) " EF\n")
3129 (setq ps-current-effect effects)))
3130
3131 ;; Starting at the beginning of the specified region...
3132 (save-excursion
3133 (goto-char from)
3134
3135 ;; ...break the region up into chunks separated by tabs, linefeeds,
3136 ;; pagefeeds, control characters, and plot each chunk.
3137 (while (< from to)
3138 (if (re-search-forward "[\000-\037\177-\377]" to t)
3139 ;; region with some control characters
3140 (let ((match (char-after (match-beginning 0))))
3141 (if (= match ?\t) ; tab
3142 (let ((linestart
3143 (save-excursion (beginning-of-line) (point))))
3144 (ps-plot 'ps-basic-plot-string from (- (point) 1)
3145 bg-color)
3146 (forward-char -1)
3147 (setq from (+ linestart (current-column)))
3148 (if (re-search-forward "[ \t]+" to t)
3149 (ps-plot 'ps-basic-plot-whitespace
3150 from (+ linestart (current-column))
3151 bg-color)))
3152 ;; any other control character except tab
3153 (ps-plot 'ps-basic-plot-string from (- (point) 1) bg-color)
3154 (cond
3155 ((= match ?\n) ; newline
3156 (ps-next-line))
3157
3158 ((= match ?\f) ; form feed
3159 (ps-next-page))
3160
3161 ((<= match ?\037) ; characters from ^@ to ^_
3162 (ps-control-character (format "^%c" (+ match ?@))))
3163
3164 ((= match ?\177) ; del (127) is printed ^?
3165 (ps-control-character "^?"))
3166
3167 (t ; characters from 128 to 255
3168 (ps-control-character (format "\\%o" match)))))
3169 (setq from (point)))
3170 ;; region without control characters
3171 (ps-plot 'ps-basic-plot-string from to bg-color)
3172 (setq from to)))))
3173
3174 (defun ps-control-character (str)
3175 (let* ((from (1- (point)))
3176 (len (length str))
3177 (to (+ from len))
3178 (wrappoint (ps-find-wrappoint from to ps-avg-char-width)))
3179 (if (< (car wrappoint) to)
3180 (ps-continue-line))
3181 (setq ps-width-remaining (- ps-width-remaining (* len ps-avg-char-width)))
3182 (ps-output-string str)
3183 (ps-output " S\n")))
3184
3185 (defun ps-color-value (x-color-value)
3186 ;; Scale 16-bit X-COLOR-VALUE to PostScript color value in [0, 1] interval.
3187 (/ x-color-value ps-print-color-scale))
3188
3189 (defun ps-color-values (x-color)
3190 (cond ((fboundp 'x-color-values)
3191 (x-color-values x-color))
3192 ((fboundp 'pixel-components)
3193 (pixel-components x-color))
3194 (t (error "No available function to determine X color values."))))
3195
3196
3197 (defun ps-face-attributes (face)
3198 "Return face attribute vector.
3199
3200 If FACE is not in `ps-print-face-extension-alist' or in
3201 `ps-print-face-alist', insert it on `ps-print-face-alist' and
3202 return the attribute vector.
3203
3204 If FACE is not a valid face name, it is used default face."
3205 (cdr (or (assq face ps-print-face-extension-alist)
3206 (assq face ps-print-face-alist)
3207 (let* ((the-face (if (facep face) face 'default))
3208 (new-face (ps-screen-to-bit-face the-face)))
3209 (or (and (eq the-face 'default)
3210 (assq the-face ps-print-face-alist))
3211 (setq ps-print-face-alist (cons new-face ps-print-face-alist)))
3212 new-face))))
3213
3214
3215 (defun ps-face-attribute-list (face-or-list)
3216 (if (listp face-or-list)
3217 ;; list of faces
3218 (let ((effects 0) foreground background face-attr face)
3219 (while face-or-list
3220 (setq face (car face-or-list)
3221 face-attr (ps-face-attributes face)
3222 effects (logior effects (aref face-attr 0)))
3223 (or foreground (setq foreground (aref face-attr 1)))
3224 (or background (setq background (aref face-attr 2)))
3225 (setq face-or-list (cdr face-or-list)))
3226 (vector effects foreground background))
3227 ;; simple face
3228 (ps-face-attributes face-or-list)))
3229
3230
3231 (defun ps-plot-with-face (from to face)
3232 (if face
3233 (let* ((face-bit (ps-face-attribute-list face))
3234 (effect (aref face-bit 0))
3235 (foreground (aref face-bit 1))
3236 (background (aref face-bit 2))
3237 (fg-color (if (and ps-print-color-p foreground)
3238 (mapcar 'ps-color-value
3239 (ps-color-values foreground))
3240 ps-default-color))
3241 (bg-color (if (and ps-print-color-p background)
3242 (mapcar 'ps-color-value
3243 (ps-color-values background)))))
3244 (ps-plot-region from to (logand effect 3)
3245 fg-color bg-color (lsh effect -2)))
3246 (ps-plot-region from to 0))
3247 (goto-char to))
3248
3249
3250 (defun ps-emacs-face-kind-p (face kind kind-regex kind-list)
3251 (let ((frame-font (face-font face))
3252 (face-defaults (face-font face t)))
3253 (or
3254 ;; Check FACE defaults:
3255 (and (listp face-defaults)
3256 (memq kind face-defaults))
3257 ;; Check the user's preferences
3258 (memq face kind-list))))
3259
3260 (defun ps-xemacs-face-kind-p (face kind kind-regex kind-list)
3261 (let* ((frame-font (or (face-font face) (face-font 'default)))
3262 (kind-cons (assq kind (x-font-properties frame-font)))
3263 (kind-spec (cdr-safe kind-cons))
3264 (case-fold-search t))
3265
3266 (or (and kind-spec (string-match kind-regex kind-spec))
3267 ;; Kludge-compatible:
3268 (memq face kind-list))))
3269
3270 (defun ps-face-bold-p (face)
3271 (if (eq ps-print-emacs-type 'emacs)
3272 (ps-emacs-face-kind-p face 'bold "-\\(bold\\|demibold\\)-"
3273 ps-bold-faces)
3274 (ps-xemacs-face-kind-p face 'WEIGHT_NAME "bold\\|demibold"
3275 ps-bold-faces)))
3276
3277 (defun ps-face-italic-p (face)
3278 (if (eq ps-print-emacs-type 'emacs)
3279 (ps-emacs-face-kind-p face 'italic "-[io]-" ps-italic-faces)
3280 (or
3281 (ps-xemacs-face-kind-p face 'ANGLE_NAME "i\\|o" ps-italic-faces)
3282 (ps-xemacs-face-kind-p face 'SLANT "i\\|o" ps-italic-faces))))
3283
3284 (defun ps-face-underlined-p (face)
3285 (or (face-underline-p face)
3286 (memq face ps-underlined-faces)))
3287
3288
3289 ;; Ensure that face-list is fbound.
3290 (or (fboundp 'face-list) (defalias 'face-list 'list-faces))
3291
3292
3293 (defun ps-build-reference-face-lists ()
3294 (setq ps-print-face-alist nil)
3295 (if ps-auto-font-detect
3296 (mapcar 'ps-map-face (face-list))
3297 (mapcar 'ps-set-face-bold ps-bold-faces)
3298 (mapcar 'ps-set-face-italic ps-italic-faces)
3299 (mapcar 'ps-set-face-underline ps-underlined-faces))
3300 (setq ps-build-face-reference nil))
3301
3302
3303 (defun ps-set-face-bold (face)
3304 (ps-set-face-attribute face 1))
3305
3306 (defun ps-set-face-italic (face)
3307 (ps-set-face-attribute face 2))
3308
3309 (defun ps-set-face-underline (face)
3310 (ps-set-face-attribute face 4))
3311
3312
3313 (defun ps-set-face-attribute (face effect)
3314 (let ((face-bit (cdr (ps-map-face face))))
3315 (aset face-bit 0 (logior (aref face-bit 0) effect))))
3316
3317
3318 (defun ps-map-face (face)
3319 (let* ((face-map (ps-screen-to-bit-face face))
3320 (ps-face-bit (cdr (assq (car face-map) ps-print-face-alist))))
3321 (if ps-face-bit
3322 ;; if face exists, merge both
3323 (let ((face-bit (cdr face-map)))
3324 (aset ps-face-bit 0 (logior (aref ps-face-bit 0) (aref face-bit 0)))
3325 (or (aref ps-face-bit 1) (aset ps-face-bit 1 (aref face-bit 1)))
3326 (or (aref ps-face-bit 2) (aset ps-face-bit 2 (aref face-bit 2))))
3327 ;; if face does not exist, insert it
3328 (setq ps-print-face-alist (cons face-map ps-print-face-alist)))
3329 face-map))
3330
3331
3332 (defun ps-screen-to-bit-face (face)
3333 (cons face
3334 (vector (logior (if (ps-face-bold-p face) 1 0) ; bold
3335 (if (ps-face-italic-p face) 2 0) ; italic
3336 (if (ps-face-underlined-p face) 4 0)) ; underline
3337 (face-foreground face)
3338 (face-background face))))
3339
3340
3341 (defun ps-mapper (extent list)
3342 (nconc list (list (list (extent-start-position extent) 'push extent)
3343 (list (extent-end-position extent) 'pull extent)))
3344 nil)
3345
3346 (defun ps-extent-sorter (a b)
3347 (< (extent-priority a) (extent-priority b)))
3348
3349 (defun ps-print-ensure-fontified (start end)
3350 (if (and (boundp 'lazy-lock-mode) lazy-lock-mode)
3351 (if (fboundp 'lazy-lock-fontify-region)
3352 (lazy-lock-fontify-region start end) ; the new
3353 (lazy-lock-fontify-buffer)))) ; the old
3354
3355 (defun ps-generate-postscript-with-faces (from to)
3356 ;; Some initialization...
3357 (setq ps-current-effect 0)
3358
3359 ;; Build the reference lists of faces if necessary.
3360 (if (or ps-always-build-face-reference
3361 ps-build-face-reference)
3362 (progn
3363 (message "Collecting face information...")
3364 (ps-build-reference-face-lists)))
3365 ;; Set the color scale. We do it here instead of in the defvar so
3366 ;; that ps-print can be dumped into emacs. This expression can't be
3367 ;; evaluated at dump-time because X isn't initialized.
3368 (setq ps-print-color-scale
3369 (if ps-print-color-p
3370 (float (car (ps-color-values "white")))
3371 1.0))
3372 ;; Generate some PostScript.
3373 (save-restriction
3374 (narrow-to-region from to)
3375 (let ((face 'default)
3376 (position to))
3377 (ps-print-ensure-fontified from to)
3378 (cond
3379 ((or (eq ps-print-emacs-type 'lucid)
3380 (eq ps-print-emacs-type 'xemacs))
3381 ;; Build the list of extents...
3382 (let ((a (cons 'dummy nil))
3383 record type extent extent-list)
3384 (map-extents 'ps-mapper nil from to a)
3385 (setq a (sort (cdr a) 'car-less-than-car))
3386
3387 (setq extent-list nil)
3388
3389 ;; Loop through the extents...
3390 (while a
3391 (setq record (car a))
3392
3393 (setq position (car record))
3394 (setq record (cdr record))
3395
3396 (setq type (car record))
3397 (setq record (cdr record))
3398
3399 (setq extent (car record))
3400
3401 ;; Plot up to this record.
3402 ;; XEmacs 19.12: for some reason, we're getting into a
3403 ;; situation in which some of the records have
3404 ;; positions less than 'from'. Since we've narrowed
3405 ;; the buffer, this'll generate errors. This is a
3406 ;; hack, but don't call ps-plot-with-face unless from >
3407 ;; point-min.
3408 (if (and (>= from (point-min))
3409 (<= position (point-max)))
3410 (ps-plot-with-face from position face))
3411
3412 (cond
3413 ((eq type 'push)
3414 (if (extent-face extent)
3415 (setq extent-list (sort (cons extent extent-list)
3416 'ps-extent-sorter))))
3417
3418 ((eq type 'pull)
3419 (setq extent-list (sort (delq extent extent-list)
3420 'ps-extent-sorter))))
3421
3422 (setq face
3423 (if extent-list
3424 (extent-face (car extent-list))
3425 'default))
3426
3427 (setq from position)
3428 (setq a (cdr a)))))
3429
3430 ((eq ps-print-emacs-type 'emacs)
3431 (let ((property-change from)
3432 (overlay-change from))
3433 (while (< from to)
3434 (if (< property-change to) ; Don't search for property change
3435 ; unless previous search succeeded.
3436 (setq property-change
3437 (next-property-change from nil to)))
3438 (if (< overlay-change to) ; Don't search for overlay change
3439 ; unless previous search succeeded.
3440 (setq overlay-change
3441 (min (next-overlay-change from) to)))
3442 (setq position
3443 (min property-change overlay-change))
3444 ;; The code below is not quite correct,
3445 ;; because a non-nil overlay invisible property
3446 ;; which is inactive according to the current value
3447 ;; of buffer-invisibility-spec nonetheless overrides
3448 ;; a face text property.
3449 (setq face
3450 (cond ((let ((prop (get-text-property from 'invisible)))
3451 ;; Decide whether this invisible property
3452 ;; really makes the text invisible.
3453 (if (eq buffer-invisibility-spec t)
3454 (not (null prop))
3455 (or (memq prop buffer-invisibility-spec)
3456 (assq prop buffer-invisibility-spec))))
3457 nil)
3458 ((get-text-property from 'face))
3459 (t 'default)))
3460 (let ((overlays (overlays-at from))
3461 (face-priority -1)) ; text-property
3462 (while overlays
3463 (let* ((overlay (car overlays))
3464 (overlay-face (overlay-get overlay 'face))
3465 (overlay-invisible (overlay-get overlay 'invisible))
3466 (overlay-priority (or (overlay-get overlay
3467 'priority)
3468 0)))
3469 (if (and (or overlay-invisible overlay-face)
3470 (> overlay-priority face-priority))
3471 (setq face (cond ((if (eq buffer-invisibility-spec t)
3472 (not (null overlay-invisible))
3473 (or (memq overlay-invisible
3474 buffer-invisibility-spec)
3475 (assq overlay-invisible
3476 buffer-invisibility-spec)))
3477 nil)
3478 ((and face overlay-face)))
3479 face-priority overlay-priority)))
3480 (setq overlays (cdr overlays))))
3481 ;; Plot up to this record.
3482 (ps-plot-with-face from position face)
3483 (setq from position)))))
3484 (ps-plot-with-face from to face))))
3485
3486 (defun ps-generate-postscript (from to)
3487 (ps-plot-region from to 0 nil))
3488
3489 (defun ps-generate (buffer from to genfunc)
3490 (save-excursion
3491 (let ((from (min to from))
3492 (to (max to from))
3493 ;; This avoids trouble if chars with read-only properties
3494 ;; are copied into ps-spool-buffer.
3495 (inhibit-read-only t))
3496 (save-restriction
3497 (narrow-to-region from to)
3498 (if ps-razzle-dazzle
3499 (message "Formatting...%3d%%" (setq ps-razchunk 0)))
3500 (set-buffer buffer)
3501 (setq ps-source-buffer buffer)
3502 (setq ps-spool-buffer (get-buffer-create ps-spool-buffer-name))
3503 (ps-init-output-queue)
3504 (let (safe-marker completed-safely needs-begin-file)
3505 (unwind-protect
3506 (progn
3507 (set-buffer ps-spool-buffer)
3508
3509 ;; Get a marker and make it point to the current end of the
3510 ;; buffer, If an error occurs, we'll delete everything from
3511 ;; the end of this marker onwards.
3512 (setq safe-marker (make-marker))
3513 (set-marker safe-marker (point-max))
3514
3515 (goto-char (point-min))
3516 (if (looking-at (regexp-quote ps-adobe-tag))
3517 nil
3518 (setq needs-begin-file t))
3519 (save-excursion
3520 (set-buffer ps-source-buffer)
3521 (if needs-begin-file (ps-begin-file))
3522 (ps-begin-job)
3523 (ps-begin-page))
3524 (set-buffer ps-source-buffer)
3525 (funcall genfunc from to)
3526 (ps-end-page)
3527
3528 (if (and ps-spool-duplex
3529 (= (mod ps-page-count 2) 1))
3530 (ps-dummy-page))
3531 (ps-flush-output)
3532
3533 ;; Back to the PS output buffer to set the page count
3534 (set-buffer ps-spool-buffer)
3535 (goto-char (point-max))
3536 (while (re-search-backward "^/PageCount 0 def$" nil t)
3537 (replace-match (format "/PageCount %d def" ps-page-count) t))
3538
3539 ;; Setting this variable tells the unwind form that the
3540 ;; the postscript was generated without error.
3541 (setq completed-safely t))
3542
3543 ;; Unwind form: If some bad mojo occurred while generating
3544 ;; postscript, delete all the postscript that was generated.
3545 ;; This protects the previously spooled files from getting
3546 ;; corrupted.
3547 (if (and (markerp safe-marker) (not completed-safely))
3548 (progn
3549 (set-buffer ps-spool-buffer)
3550 (delete-region (marker-position safe-marker) (point-max))))))
3551
3552 (if ps-razzle-dazzle
3553 (message "Formatting...done"))))))
3554
3555 (defun ps-do-despool (filename)
3556 (if (or (not (boundp 'ps-spool-buffer))
3557 (not (symbol-value 'ps-spool-buffer)))
3558 (message "No spooled PostScript to print")
3559 (ps-end-file)
3560 (ps-flush-output)
3561 (if filename
3562 (save-excursion
3563 (if ps-razzle-dazzle
3564 (message "Saving..."))
3565 (set-buffer ps-spool-buffer)
3566 (setq filename (expand-file-name filename))
3567 (write-region (point-min) (point-max) filename)
3568 (if ps-razzle-dazzle
3569 (message "Wrote %s" filename)))
3570 ;; Else, spool to the printer
3571 (if ps-razzle-dazzle
3572 (message "Printing..."))
3573 (save-excursion
3574 (set-buffer ps-spool-buffer)
3575 (if (and (eq system-type 'ms-dos)
3576 (stringp (symbol-value 'dos-ps-printer)))
3577 (write-region (point-min) (point-max)
3578 (symbol-value 'dos-ps-printer) t 0)
3579 (let ((binary-process-input t)) ; for MS-DOS
3580 (apply 'call-process-region
3581 (point-min) (point-max) ps-lpr-command nil
3582 (if (fboundp 'start-process) 0 nil)
3583 nil
3584 ps-lpr-switches))))
3585 (if ps-razzle-dazzle
3586 (message "Printing...done")))
3587 (kill-buffer ps-spool-buffer)))
3588
3589 (defun ps-kill-emacs-check ()
3590 (let (ps-buffer)
3591 (if (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
3592 (buffer-modified-p ps-buffer))
3593 (if (y-or-n-p "Unprinted PostScript waiting; print now? ")
3594 (ps-despool)))
3595 (if (and (setq ps-buffer (get-buffer ps-spool-buffer-name))
3596 (buffer-modified-p ps-buffer))
3597 (if (yes-or-no-p "Unprinted PostScript waiting; exit anyway? ")
3598 nil
3599 (error "Unprinted PostScript")))))
3600
3601 (if (fboundp 'add-hook)
3602 (funcall 'add-hook 'kill-emacs-hook 'ps-kill-emacs-check)
3603 (if kill-emacs-hook
3604 (message "Won't override existing kill-emacs-hook")
3605 (setq kill-emacs-hook 'ps-kill-emacs-check)))
3606
3607 ;;; Sample Setup Code:
3608
3609 ;; This stuff is for anybody that's brave enough to look this far,
3610 ;; and able to figure out how to use it. It isn't really part of
3611 ;; ps-print, but I'll leave it here in hopes it might be useful:
3612
3613 ;; WARNING!!! The following code is *sample* code only. Don't use it
3614 ;; unless you understand what it does!
3615
3616 (defmacro ps-prsc ()
3617 `(if (eq ps-print-emacs-type 'emacs) [f22] 'f22))
3618 (defmacro ps-c-prsc ()
3619 `(if (eq ps-print-emacs-type 'emacs) [C-f22] '(control f22)))
3620 (defmacro ps-s-prsc ()
3621 `(if (eq ps-print-emacs-type 'emacs) [S-f22] '(shift f22)))
3622
3623 ;; A hook to bind to `rmail-mode-hook' to locally bind prsc and set the
3624 ;; `ps-left-headers' specially for mail messages.
3625 (defun ps-rmail-mode-hook ()
3626 (local-set-key (ps-prsc) 'ps-rmail-print-message-from-summary)
3627 (setq ps-header-lines 3
3628 ps-left-header
3629 ;; The left headers will display the message's subject, its
3630 ;; author, and the name of the folder it was in.
3631 '(ps-article-subject ps-article-author buffer-name)))
3632
3633 ;; See `ps-gnus-print-article-from-summary'. This function does the
3634 ;; same thing for rmail.
3635 (defun ps-rmail-print-message-from-summary ()
3636 (interactive)
3637 (ps-print-message-from-summary 'rmail-summary-buffer "RMAIL"))
3638
3639 ;; Used in `ps-rmail-print-article-from-summary',
3640 ;; `ps-gnus-print-article-from-summary' and `ps-vm-print-message-from-summary'.
3641 (defun ps-print-message-from-summary (summary-buffer summary-default)
3642 (let ((ps-buf (or (and (boundp summary-buffer)
3643 (symbol-value summary-buffer))
3644 summary-default)))
3645 (and (get-buffer ps-buf)
3646 (save-excursion
3647 (set-buffer ps-buf)
3648 (ps-spool-buffer-with-faces)))))
3649
3650 ;; Look in an article or mail message for the Subject: line. To be
3651 ;; placed in `ps-left-headers'.
3652 (defun ps-article-subject ()
3653 (save-excursion
3654 (goto-char (point-min))
3655 (if (re-search-forward "^Subject:[ \t]+\\(.*\\)$" nil t)
3656 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
3657 "Subject ???")))
3658
3659 ;; Look in an article or mail message for the From: line. Sorta-kinda
3660 ;; understands RFC-822 addresses and can pull the real name out where
3661 ;; it's provided. To be placed in `ps-left-headers'.
3662 (defun ps-article-author ()
3663 (save-excursion
3664 (goto-char (point-min))
3665 (if (re-search-forward "^From:[ \t]+\\(.*\\)$" nil t)
3666 (let ((fromstring (buffer-substring-no-properties (match-beginning 1)
3667 (match-end 1))))
3668 (cond
3669
3670 ;; Try first to match addresses that look like
3671 ;; thompson@wg2.waii.com (Jim Thompson)
3672 ((string-match ".*[ \t]+(\\(.*\\))" fromstring)
3673 (substring fromstring (match-beginning 1) (match-end 1)))
3674
3675 ;; Next try to match addresses that look like
3676 ;; Jim Thompson <thompson@wg2.waii.com>
3677 ((string-match "\\(.*\\)[ \t]+<.*>" fromstring)
3678 (substring fromstring (match-beginning 1) (match-end 1)))
3679
3680 ;; Couldn't find a real name -- show the address instead.
3681 (t fromstring)))
3682 "From ???")))
3683
3684 ;; A hook to bind to `gnus-article-prepare-hook'. This will set the
3685 ;; `ps-left-headers' specially for gnus articles. Unfortunately,
3686 ;; `gnus-article-mode-hook' is called only once, the first time the *Article*
3687 ;; buffer enters that mode, so it would only work for the first time
3688 ;; we ran gnus. The second time, this hook wouldn't get set up. The
3689 ;; only alternative is `gnus-article-prepare-hook'.
3690 (defun ps-gnus-article-prepare-hook ()
3691 (setq ps-header-lines 3)
3692 (setq ps-left-header
3693 ;; The left headers will display the article's subject, its
3694 ;; author, and the newsgroup it was in.
3695 (list 'ps-article-subject 'ps-article-author 'gnus-newsgroup-name)))
3696
3697 ;; A hook to bind to `vm-mode-hook' to locally bind prsc and set the
3698 ;; `ps-left-headers' specially for mail messages.
3699 (defun ps-vm-mode-hook ()
3700 (local-set-key (ps-prsc) 'ps-vm-print-message-from-summary)
3701 (setq ps-header-lines 3)
3702 (setq ps-left-header
3703 ;; The left headers will display the message's subject, its
3704 ;; author, and the name of the folder it was in.
3705 (list 'ps-article-subject 'ps-article-author 'buffer-name)))
3706
3707 ;; Every now and then I forget to switch from the *Summary* buffer to
3708 ;; the *Article* before hitting prsc, and a nicely formatted list of
3709 ;; article subjects shows up at the printer. This function, bound to
3710 ;; prsc for the gnus *Summary* buffer means I don't have to switch
3711 ;; buffers first.
3712 ;; sb: Updated for Gnus 5.
3713 (defun ps-gnus-print-article-from-summary ()
3714 (interactive)
3715 (ps-print-message-from-summary 'gnus-article-buffer "*Article*"))
3716
3717 ;; See `ps-gnus-print-article-from-summary'. This function does the
3718 ;; same thing for vm.
3719 (defun ps-vm-print-message-from-summary ()
3720 (interactive)
3721 (ps-print-message-from-summary 'vm-mail-buffer ""))
3722
3723 ;; A hook to bind to bind to `gnus-summary-setup-buffer' to locally bind
3724 ;; prsc.
3725 (defun ps-gnus-summary-setup ()
3726 (local-set-key (ps-prsc) 'ps-gnus-print-article-from-summary))
3727
3728 ;; Look in an article or mail message for the Subject: line. To be
3729 ;; placed in `ps-left-headers'.
3730 (defun ps-info-file ()
3731 (save-excursion
3732 (goto-char (point-min))
3733 (if (re-search-forward "File:[ \t]+\\([^, \t\n]*\\)" nil t)
3734 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
3735 "File ???")))
3736
3737 ;; Look in an article or mail message for the Subject: line. To be
3738 ;; placed in `ps-left-headers'.
3739 (defun ps-info-node ()
3740 (save-excursion
3741 (goto-char (point-min))
3742 (if (re-search-forward "Node:[ \t]+\\([^,\t\n]*\\)" nil t)
3743 (buffer-substring-no-properties (match-beginning 1) (match-end 1))
3744 "Node ???")))
3745
3746 (defun ps-info-mode-hook ()
3747 (setq ps-left-header
3748 ;; The left headers will display the node name and file name.
3749 (list 'ps-info-node 'ps-info-file)))
3750
3751 ;; WARNING! The following function is a *sample* only, and is *not*
3752 ;; meant to be used as a whole unless you understand what the effects
3753 ;; will be! (In fact, this is a copy of Jim's setup for ps-print --
3754 ;; I'd be very surprised if it was useful to *anybody*, without
3755 ;; modification.)
3756
3757 (defun ps-jts-ps-setup ()
3758 (global-set-key (ps-prsc) 'ps-spool-buffer-with-faces) ;f22 is prsc
3759 (global-set-key (ps-s-prsc) 'ps-spool-region-with-faces)
3760 (global-set-key (ps-c-prsc) 'ps-despool)
3761 (add-hook 'gnus-article-prepare-hook 'ps-gnus-article-prepare-hook)
3762 (add-hook 'gnus-summary-mode-hook 'ps-gnus-summary-setup)
3763 (add-hook 'vm-mode-hook 'ps-vm-mode-hook)
3764 (add-hook 'vm-mode-hooks 'ps-vm-mode-hook)
3765 (add-hook 'Info-mode-hook 'ps-info-mode-hook)
3766 (setq ps-spool-duplex t)
3767 (setq ps-print-color-p nil)
3768 (setq ps-lpr-command "lpr")
3769 (setq ps-lpr-switches '("-Jjct,duplex_long"))
3770 'ps-jts-ps-setup)
3771
3772 ;; WARNING! The following function is a *sample* only, and is *not*
3773 ;; meant to be used as a whole unless it corresponds to your needs.
3774 ;; (In fact, this is a copy of Jack's setup for ps-print --
3775 ;; I would not be that surprised if it was useful to *anybody*,
3776 ;; without modification.)
3777
3778 (defun ps-jack-setup ()
3779 (setq ps-print-color-p nil
3780 ps-lpr-command "lpr"
3781 ps-lpr-switches (list)
3782
3783 ps-paper-type 'a4
3784 ps-landscape-mode t
3785 ps-number-of-columns 2
3786
3787 ps-left-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
3788 ps-right-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
3789 ps-inter-column (/ (* 72 1.0) 2.54) ; 1.0 cm
3790 ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
3791 ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
3792 ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
3793 ps-header-line-pad .15
3794 ps-print-header t
3795 ps-print-header-frame t
3796 ps-header-lines 2
3797 ps-show-n-of-n t
3798 ps-spool-duplex nil
3799
3800 ps-font-family 'Courier
3801 ps-font-size 5.5
3802 ps-header-font-family 'Helvetica
3803 ps-header-font-size 6
3804 ps-header-title-font-size 8)
3805 'ps-jack-setup)
3806
3807 (provide 'ps-print)
3808
3809 ;;; ps-print.el ends here