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