Hi,

Happy New Year everyone !

I've attached a (slightly) updated version of the script I sent sometime
back. I've added a wordwrap subroutine, which works alright for now, but
I'm quite sure there are better ways of doing this.

Do send in feedback, as I would like to know how 'real programmers'
would go about doing this :-)

<code>
#!/usr/bin/perl -w
# File: display_filter.pl
# Last Update: Sun, 30 Dec 2001 12:00:39 +0530
# Modified By: Prahlad Vaidyanathan <[EMAIL PROTECTED]>

# Usage :
#   set $display_filter in muttrc to this script, and make script executable

# THANKS
#   * thomas hurst's quotefix.rb

# TODO
#   * use g. johnson's mail-to-filter.pl to compress large to/cc headers
#   * better algorithm for blank-line consolidation, and line wrapping

use strict ;

my $flag = 0 ; # Flag for blank-line removal
my $line ;

# Change the following to suit your needs
my $quote_re = "^(%\ |-->|[\ \t]*[A-Za-z]+>|[\ \t]*[\|>:!])\ ?" ;
my $nonquote_re = "^(grub>|[:;=][-+]?[\)DPdb])" ;
my $bad_sig_re = "^(-{2,3}|_{30,})\$" ; # match hotmail/yahoo ads and ML footers
my $line_length = 72 ; # Anything longer is wrapped
# Flags to play with :-)
my $strip_empty_quotes      = 0 ; # ">>> \n" => "\n"
my $strip_trailing_spaces   = 1 ; # ">>> blah   \n" => ">>> blah\n"

# Headers (use muttrc to play with these)
while ( ( $line = <> ) !~ /^$/ ) { print $line ; }
print "\n" ;

# Strip ! ;-)
while ($line = <>) {
    # Emacs users ... bah !
    $line =~ s/>>>>> // ;
    # Strip if $quote_re matches, and $nonquote_re doesn't
    my $quote_depth = 0;
    while ( $line =~ m/$quote_re/o && $line !~ m/$nonquote_re/o ) {
        $quote_depth++; # Increment $quote_depth
        $line = $' ; # $line is set to everything _after_ the matched regex
    }
    # Strip trailing white-spaces
    $line =~ s/[\s]+$/\n/ if ($strip_trailing_spaces) ;
    # Fix bad sig-dashes
    $line =~ s/$bad_sig_re/-- /o;
    # Remove dos line-breaks
    $line =~ s/
// ;
    # Makes block of blank lines into one
    if ( $line =~ m/^[\s]*$/ ) {
        $flag += 1 ;
        if ( $flag eq 1 ) {
            # Re-quotes if $strip_empty_quotes is not set
            if ( ! $strip_empty_quotes && $quote_depth ) {
                $line = ('>' x $quote_depth) . "\n" ;
            }
            # Displays only the 1st blank line
            print $line ;
            next ;
        }
        elsif ( $flag gt 1 ) {
            # 2nd line onwards are ignored
            next ;
        }
    }
    $flag = 0 ;
    # Word wrapping, re-quoting, and displaying the line 
    # Don't wrap the pgp/mailcap output lines
    if ( length($line) >= $line_length && $line !~ /\[\-\-/ ) {
        # Preserves leading spaces
        my ($leading_spaces) = ( $line =~ m/^([\s]*)\w/ ) ;
        foreach $line ( wordwrap($line) ) {
            $line = $leading_spaces . $line if ($leading_spaces) ;
            $line = ('>' x $quote_depth ) . ' ' . $line if ($quote_depth) ;
            print "$line\n" ;
        }
    } 
    else {
        $line = ('>' x $quote_depth) . ' ' . $line if ($quote_depth) ;
        print $line ;
    }
}

# Input: single line longer than $line_length
# Output: Array of lines shorter than $line_length
sub wordwrap {
    my @words = split(' ', shift ) ;
    $line = $words[0] ; # o/w concatenation in loop barfs
    my @return ;
    # $word_flag = 1 if $line contains 2 or more words, else it is 0
#     my $word_flag = 0 ;
    for ( my $i = 1; $i <= ($#words+1) ; $i++) {
        if ( $words[$i] ) { # In case $words[$i] is a space, ignore
            if ( length($line) < $line_length) {
                $line .= ' ' . $words[$i] ;
#                 $word_flag = 1 ; # as $line contains 2 or more wds
            }
            else {
                push(@return,$line) ;
                $line = $words[$i] ;
#                 $word_flag = 0 ; # as $line contains only 1 wd
            }
        }
    }
    # push iff $line has 2 or more wds
    push(@return,$line) if (($line =~ m/\s/) || length($line) < $line_length) ;
#     wantarray()? @return : $return[0] ;
    return(@return) ;
}
</code>

pv.

-- 
Prahlad Vaidyanathan <[EMAIL PROTECTED]>

Why is it that we rejoice at a birth and grieve at a funeral?  It is because we
are not the person involved.
                -- Mark Twain, "Pudd'nhead Wilson's Calendar"

Attachment: msg15094/pgp00000.pgp
Description: PGP signature

Reply via email to