#!/usr/local/bin/perl # Perl script called from within vi (elvis) to reformat the present paragraph. # Slow, but functional. It uses the 'reform' perl script from the ORA Camel # programming perl book. # Usage: virefm.pl # WARNING paragraph must be marked by BBBBBB and EEEEEE # example in ~/.exrc # map  mp{OBBBBBBmbjmo'p}oEEEEEEme:w! /tmp/virefm :!virefm.pl /tmp/virefm 'o"od'e'b:r /tmp/virefm 'bdd $MAXLINE = "72"; open(IN,"$ARGV[0]") || die; # file to be vi-reformed $out = "/tmp/virerm$$"; open(OUT,">$out") || die; # and if we die, what will poor little vi do? $switch = ''; while () { if ( /EEEEEE/ ) { $switch = ''; } # EEEEEE marks end of paragraph if ($switch) { print OUT $_; } if ( /BBBBBB/ ) { $switch = 'True'; } # BBBBBB marks begin of paragraph } close(IN); close(OUT); # OK, $out now has the single paragraph, now find the max length of line open(IN,"$out"); $max = 0; $got = ''; $first_blank = ''; while () { $len = length($_); if ( $len > $max) { $max = $len; } if (! $got) { # kludge to be used below if 1st line blank $got = 'True'; if ( /^\s*$/ ) { $first_blank = 'True'; } } } if ($max > $MAXLINE) { $max = $MAXLINE; } # call the camel book script: system "reform -r$max $out > $ARGV[0]"; # patch in "\n" if needed: if ($first_blank) { open(OUT,">$out"); print OUT "\n"; close(OUT); system "cat $ARGV[0] >> $out"; system "cp $out $ARGV[0]"; } system "rm -f $out";