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