This is a nice utility for printing, Dave. And some of the other suggestions
in this thread work very well, also. I think this one may potentially be 
more flexible because of your use of TeX.

I added some enhancements/bugs (you decide ;) ) to your original script:

1. Added '_' as a special character. The very first email I printed with
this script had an underline in one of the addresses so TeX interpreted the
next character(s) as a subscript.

2. Changed the pattern matching for the headers to match _all_ headers (if
my reg exp is correct) Of course, only the headers that are 'unignored' in the
.muttrc will actually be printed.

3. Modified the header section so that it will print headers that span more
than one line.

4. Added a PREVIEW variable so the user can send the output to their favorite 
viewer before printing and then print from the viewer. I used ghostview but 
the user could change this to any viewer (e.g., xdvi).

I have noticed that there are some emails that won't print. I haven't 
been able to figure this out. Haven't spent a lot of time to either.
But, the error message I see is,

dvips: ! DVI file can't be opened.

Any ideas? 

Hope you don't mind the changes.

Regards,

Mike


On Thu, Aug 16, 2001 at 04:13:16PM +0100, Dave Ewart wrote:
> Dear Mutt users,
> 
> Printing plain text email messages has always been a bit of a annoyance,
> since it looks so, well, "plain".
> 
> I wrote the attached quick-n-dirty Perl script to process a print job -
> it filters the email text, picks out the standard headers and writes a
> TeX file, which is then rendered and sent to the default printer.
> Modify to your taste!
> 
> I use this:
> 
> set print_command="/FULL/PATH/TO/PROGRAM/emailprint.pl"
> 
> in ~/.muttrc for it to work.
> 
> Hope it's useful - it works for me, YMMV.
> 
> Cheers,
> 
> Dave.
> 
> -- 
> Dave Ewart
> [EMAIL PROTECTED]
> Computing Manager
> ICRF Cancer Epidemiology Unit, Oxford UK




#!/usr/bin/perl -w
#
# $Id: emailprint.pl,v 1.2 2001/08/16 15:12:05 davee Exp $

# Set this to one if you don't actually want to print immediately
# on paper.  Uses xdvi to preview then the user can print from xdvi.
$PREVIEW = 1;

$open_char  = '<';
$close_char = '>';
$homedir    = $ENV{HOME};
$ul         = '_';

# This is 1 for headers, 2 for body and 3 for sig
$current_section = 1;

open(OUTPUT, ">tmpprint.tex");

print OUTPUT "\\documentclass[letter]{article}\n";
print OUTPUT "\\pagestyle{plain}\n";
print OUTPUT "\\usepackage{charter}\n";
print OUTPUT "\\begin{document}\n";
print OUTPUT "\\thispagestyle{empty}\n";
print OUTPUT "\\noindent\n";
print OUTPUT "\\begin{tabular}{ll}\n";
print OUTPUT "\\hline";
while (<STDIN>) {
    $rec = $_;
    chop($rec);
    if ($current_section == 1) {
        $rec =~ s/$open_char/(\\emph{/g;
        $rec =~ s/$close_char/})/g;
        $rec =~ s/$ul/\\_/g;
        # Handle any header: One or more alphanumeric characters plus a '-'
        # at the beginning of the line followed by a ': '.
        if ($rec =~ /^[A-Za-z0-9-]+: /) {
            print OUTPUT "{\\bf $&} & $'\\\\\n";
        }
        # For multi-line headers
        elsif ($rec =~ /^.+$/) {
            print OUTPUT "& $rec\\\\\n";
        } 
        elsif ($rec =~ /^$/) {
            $current_section = 2;
            print OUTPUT "\\hline\n\\end{tabular}\n";
            print OUTPUT "\\begin{verbatim}\n";
        }
    }
    elsif ($current_section == 2) {
        print OUTPUT "$rec\n";
        if ($rec =~ /^-- $/) {
            print OUTPUT "\\end{verbatim}\n";
            $current_section = 3;
            print OUTPUT "\\emph{";
        }

    }
    else {
        print OUTPUT "$rec\\\\\n";
    }
}
if ($current_section == 3) {
    print OUTPUT "}";
}
else {
    print OUTPUT "\\end{verbatim}\n";
}
print OUTPUT "\\end{document}\n";
close(OUTPUT);
system("latex -interaction=batchmode tmpprint.tex");
if ($PREVIEW == 1) {
    system("dvips tmpprint.dvi -o");
    system("ghostview tmpprint.ps");
}
else {
    system("dvips tmpprint.dvi");
}
system("rm $homedir/tmpprint.*");
system("rm $homedir/texput.log");

Reply via email to