Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 16: Interfacing with the System

Previous HourNext Hour

Sections in this Hour:

 

Printing

Another system interface Emacs has is the capability to print buffers. In general there are two ways to do this. The simplest way is with the lpr or print command. The commands M-x lpr-buffer.and M-x lpr-region.take the affected text and print them with the UNIX lpr command. The commands M-x print-buffer.and M-x print-region.print the affected text with lpr -p, meaning that plain-text headers will be added to the top of every page.

Both commands generate the text they want to print and execute a system command for printing, namely the lpr command. (lp on some systems). A side effect of this is that the variable lpr-switches can control additional command-line parameters to pass to the print command whenever you print a file. This command is a list; thus, if you want to specify a printer to send your output to, the command to add to your ~/.emacs file would look like this:


(setq lpr-switches '("-Potherprinter"))

If you wanted to add something else, such as -m to have the printer send your mail when the job is finished, the updated entry in your ~/.emacs file would look like this:


(setq lpr-switches '("-Potherprinter" "-m"))

With these commands you've covered the simple printing of plain text. Emacs also supports printing with PostScript. The PostScript printer has the advantage of enabling the printing of text attributes such as bold, italic, underline, and even color when available in hardware. The commands for printing with the PostScript driver are M-x ps-print-buffer, M-x ps-print-region. To enable the display of colors you can use the commands M-x ps-print-buffer-with-faces, and M-x ps-print-region-with-faces.

Each of these commands converts the buffer into PostScript, complete with page titles. If you don't want your files printed right away, or if you want to group them together, you can spool them, with commands such as M-x ps-spool-buffer, M-x ps-spool-region, M-x ps-spool-buffer-with-faces, and M-x ps-spool-region-with-faces. When you've spooled a few PostScript jobs together, you can print them with M-x ps-despool.

The PostScript print driver uses the lpr-switches and commands you learned earlier, so you don't have to do anything more to configure the printing of your file in PostScript. There are, however, many more configuration items available for the PostScript driver than for the plain text printing option. Some of these are as follows:

Some additional variables are useful in converting color displays, commonly found when using font lock in source code on color displays, into black and white. All the font-lock fonts are covered already, but you might want to add additional faces depending on your needs. The variables to customize for this are as follows:

A given face can appear in multiple lists adopting multiple features.

Although the defaults when using the PostScript printing code are great for source code, it is not always desirable to print a header all the time. For example, you can use enriched mode if you want to print your text with the faces you've added, but without the header on every page. You can write your own commands to print files with the tweaks you want. In the preceding scenario, you might put something like this in your ~/.emacs file.


(defun my-print () 
  "Postscript print the current buffer with faces, but without a header." 
  (interactive) 
  (let ((ps-print-header nil)) 
        (ps-print-buffer-with-faces))) 

This creates the command my-print in your ~/.emacs file which you can execute with the keystroke M-x my-print Enter. The way this program works is by overloading the value of ps-print-header, which controls the display of headers on pages with nil. While its value is nil, it prints the buffer with faces, but without page headers. When Emacs is finished printing, it reverts the value back to its original, so your future printouts contain the headers. You can do this with any of the customizable values if you want to have special print commands that have different types of outputs.

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 16: Interfacing with the System

Previous HourNext Hour

Sections in this Hour: