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