From: Kevin_McCarthy@BayNetworks.COM (Kevin McCarthy) Date: Thu, 26 Feb 1998 16:14:27 -0800 To: public@home-of.tj Subject: cor.pl modifications Hi Thomas, I used your cor.pl program as a base for my own version of it. I'm attaching my changed version. Maybe someone else will find it useful. Basically, I added three "box" styles i -> /* * */ c -> /******* * *******/ o -> /******* * * *******/ which you set using the -b option. I just put the following entries in my .vimrc: autocmd BufNewFile,BufRead *.c,*.h,*.cc vmap ,i ! $VIM/cor.pl -l c -b i autocmd BufNewFile,BufRead *.c,*.h,*.cc vmap ,c ! $VIM/cor.pl -l c -b c autocmd BufNewFile,BufRead *.c,*.h,*.cc vmap ,o ! $VIM/cor.pl -l c -b o autocmd BufNewFile,BufRead *.c,*.h,*.cc vmap ,r ! $VIM/cor.pl -l c -r autocmd BufNewFile,BufRead *.java vmap ,i ! $VIM/cor.pl -l j -b i autocmd BufNewFile,BufRead *.java vmap ,c ! $VIM/cor.pl -l j -b c autocmd BufNewFile,BufRead *.java vmap ,o ! $VIM/cor.pl -l j -b o autocmd BufNewFile,BufRead *.java vmap ,r ! $VIM/cor.pl -l j -r autocmd BufNewFile,BufRead *.pl vmap ,i ! $VIM/cor.pl -l p -b i autocmd BufNewFile,BufRead *.pl vmap ,c ! $VIM/cor.pl -l p -b c autocmd BufNewFile,BufRead *.pl vmap ,o ! $VIM/cor.pl -l p -b o autocmd BufNewFile,BufRead *.pl vmap ,r ! $VIM/cor.pl -l p -r Thanks, -Kevin McCarthy kmccarth@baynetworks.com ------------------------------- CUT HERE ------------------------------------ #!/usr/local/bin/perl -w # [Adjust the above line to where your perl5 lives.] # # cor - comment out region, activate region # Works with C/C++, Perl/Shell, vimrc, and HTML files. # # Thomas Jensen, tsjensen@cip.informatik.uni-erlangen.de # Start of Development: Sun Feb 9 16:01:53 MET 1997 # Last updated: Wed Feb 12 22:37:14 MET 1997 # # This is a filter intended for (though not limited to) use with vim. # Please refer to http://home.pages.de/~jensen/junk/cor.pl # for the latest version of this filter. # # Oh, by the way, the code quality of this thing here is terrible. This # is totally unreadable. In addition to that, I think it's quite buggy, # too. I still use it because I have not yet found anything else that # does the job. It works in all the cases that I use it for, but I know # it failed some tests. If cor.pl should suddenly become popular or I # have more time, I'll do it right. Until then, good luck! # # Distribution policy: Freeware. Use it, but don't charge for it. # If you decide to modify the program, e-mail me a copy. Make sure # its header mentions me as the original author. This software comes # with ABSOLUTELY NO WARRANTY. If it destroys your text, BAD LUCK! # # History: Kevin J. McCarthy [02/25/98] hacked up for my own purposes use Getopt::Std; use Text::Tabs; # # Definition of comments # $COMMENT_START =3D 0; # upper left corner of comment $COMMENT_END =3D 1; # lower right corner of comment $COMMENT_BOX_LEFT =3D 2; # left side of box $COMMENT_BOX_MID =3D 3; # top and bottom of box $COMMENT_BOX_RIGHT =3D 4; # right side of box # # box style i: box style c: box style o: # 0 03333333 0333333 # 2 2 2 4 # 2 2 2 4 # 1 33333331 3333331 # %cmts =3D=20 ( # C Style comments "c" =3D> ['/*', '*/', '*', '*', '*' ], # Perl Style comments "p" =3D> ['#', '#', '#', '#', '#' ], # Java Style comments "j" =3D> ['//', '//', '//', '/', '//' ], ); # # Processing of Command Line Options # $opt_r=3D0; $opt_l=3D"C"; $opt_b =3D "o"; $opt_t =3D "8"; if (!getopts ('rl:b:t:')) { &usage(-1); } if (($opt_l !~ /^[cpjCPJ]/)) {=20 usage(-1); } if (($opt_b !~ /^[ico]/)) {=20 usage(-1); } $tabstop =3D $opt_t; $opt_l =3D~ tr/CHPSV/chpsv/; # downcase significant characters $art =3D substr ($opt_l, 0, 1); # now an index to %cmts # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $cnt =3D 0; # input line counter $left =3D 9999; # assume high indentation $maxlen =3D 0; # assume short lines :-) # # ADD COMMENTS TO LINES # if (!$opt_r) { # # Read entire input into array, determine longest line and closest # distance to left side of paper for comment tag alignment. # $curlen =3D 0; $curleft =3D 0; $isvoid =3D 1; # Entire input void? ############### # Scan the input for statistics ############### while () { chomp; # remove trailing whitespaces s/(\s*)$//; # Expand all tabs $_ =3D expand ($_); $curlen =3D length; if ($curlen) { $isvoid =3D 0; # # Record longest line length # if ( $maxlen < $curlen ) { $maxlen =3D $curlen; } # # Record the leftmost indentation # /^(\s*)(.*)$/; $curleft =3D length $1; if ($curleft < $left) { $left =3D $curleft; } } # # remember line for output # $eing[$cnt] =3D $_; ++$cnt; } if ($isvoid) { $left =3D 0; $maxlen =3D 0; } ########## # add the comments ########## print &add_top(); foreach (@eing) { print &add_mid($_); } print &add_bottom(); } else { # # Remove comments # foreach () { $eing[$cnt] =3D $_; ++$cnt; }=20 if ( $#eing < 0 ) { return; } $eing[0] =3D &rm_top($eing[0]); $eing[$#eing] =3D &rm_bottom($eing[$#eing]); foreach ( @eing ) { $_ =3D &rm_mid($_); } if ( $eing[0] eq "\n" ) { shift(@eing); } if ( $eing[$#eing] eq "\n" ) { pop(@eing); } foreach (@eing) { print; } } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - sub add_top { if ( $opt_b eq 'i' ) { return " " x $left . $cmts{$art}[$COMMENT_START] . "\n"; } elsif ( $opt_b eq 'c' ) { $padsize =3D $maxlen - $left; $padsize +=3D length($cmts{$art}[$COMMENT_BOX_LEFT]) + 1; $padsize -=3D length($cmts{$art}[$COMMENT_START]); $padsize /=3D length($cmts{$art}[$COMMENT_BOX_MID]); return " " x $left . $cmts{$art}[$COMMENT_START] . $cmts{$art}[$COMMENT_BOX_MID] x $padsize . "\n"; } elsif ( $opt_b eq 'o' ) { $padsize =3D $maxlen - $left; $padsize +=3D length($cmts{$art}[$COMMENT_BOX_LEFT]) + 1 + length($cmts{$art}[$COMMENT_BOX_RIGHT]) + 1; $padsize -=3D length($cmts{$art}[$COMMENT_START]); $padsize /=3D length($cmts{$art}[$COMMENT_BOX_MID]); return " " x $left . $cmts{$art}[$COMMENT_START] . $cmts{$art}[$COMMENT_BOX_MID] x $padsize . "\n"; } } sub add_mid { my $line =3D $_[0]; my $len =3D length $line; if ( $len ) { $addspc =3D $maxlen - $len; } else { $addspc =3D $maxlen - $left; } # # Strip out the left padding # and divied up the line # $line =3D~ s/^(\s{$left})(.*)$/$2/; # # Put the line back together with the padding # if ( $opt_b eq 'i' ) { $line =3D " " x $left . " " x (length($cmts{$art}[$COMMENT_START]) - length($cmts{$art}[$COMMENT_BOX_LEFT])) . $cmts{$art}[$COMMENT_BOX_LEFT] . " " . $line . "\n"; } elsif ( $opt_b eq 'c' ) { $line =3D " " x $left . $cmts{$art}[$COMMENT_BOX_LEFT] . " " . $line . "\n"; } elsif ( $opt_b eq 'o' ) { $line =3D " " x $left . $cmts{$art}[$COMMENT_BOX_LEFT] . " " . $line . (" " x $addspc) . " " . $cmts{$art}[$COMMENT_BOX_RIGHT] . "\n"; } return $line; } sub add_bottom { if ( $opt_b eq 'i' ) { return " " x $left . " " x (length($cmts{$art}[$COMMENT_START]) - length($cmts{$art}[$COMMENT_BOX_LEFT])) . $cmts{$art}[$COMMENT_END] . "\n"; } elsif ( $opt_b eq 'c' ) { $padsize =3D $maxlen - $left; $padsize +=3D length($cmts{$art}[$COMMENT_BOX_LEFT]) + 1; $padsize -=3D length($cmts{$art}[$COMMENT_END]); $padsize /=3D length($cmts{$art}[$COMMENT_BOX_MID]); return " " x $left . $cmts{$art}[$COMMENT_BOX_MID] x $padsize . $cmts{$art}[$COMMENT_END] . "\n"; } elsif ( $opt_b eq 'o' ) { $padsize =3D $maxlen - $left; $padsize +=3D length($cmts{$art}[$COMMENT_BOX_LEFT]) + 1 + length($cmts{$art}[$COMMENT_BOX_RIGHT]) + 1; $padsize -=3D length($cmts{$art}[$COMMENT_END]); $padsize /=3D length($cmts{$art}[$COMMENT_BOX_MID]); return " " x $left . $cmts{$art}[$COMMENT_BOX_MID] x $padsize . $cmts{$art}[$COMMENT_END] . "\n"; } } sub rm_top { my $line =3D $_[0]; chomp($line); $line =3D~ s|^ (\s*) (\Q$cmts{$art}[$COMMENT_START]\E) (\Q$cmts{$art}[$COMMENT_BOX_MID]\E)* (\s*) $ ||x; return $line . "\n"; } sub rm_mid { my $line =3D $_[0]; chomp($line); $line =3D~ s|^ (\s*) (\Q$cmts{$art}[$COMMENT_BOX_LEFT]\E\s?) (.*?) (\s*\Q$cmts{$art}[$COMMENT_BOX_RIGHT]\E) $ |$1$3|x; $line =3D~ s|^ (\s*) (\Q$cmts{$art}[$COMMENT_BOX_LEFT]\E\s?) (.*) $ |$1$3|x; return $line . "\n"; } sub rm_bottom { my $line =3D $_[0]; chomp($line); $line =3D~ s|^ (\s*) (\Q$cmts{$art}[$COMMENT_BOX_MID]\E)* (\Q$cmts{$art}[$COMMENT_END]\E) (\s*) $ ||x; return $line . "\n"; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - sub usage { my $exs =3D @_; print STDERR ("Usage: cor -r -b type -l type\n"); exit $exs; } #EOF