home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / question / 14815 < prev    next >
Encoding:
Text File  |  1992-12-21  |  2.1 KB  |  81 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news.cerf.net!netlabs!lwall
  3. From: lwall@netlabs.com (Larry Wall)
  4. Subject: Re: What do *you* use to format mail?
  5. Message-ID: <1992Dec17.235350.24904@netlabs.com>
  6. Sender: news@netlabs.com
  7. Nntp-Posting-Host: scalpel.netlabs.com
  8. Organization: NetLabs, Inc.
  9. References: <1992Dec13.091156.19462@nwnexus.WA.COM> <nrp.724506949@reading> <1992Dec16.170902.21889@nwnexus.WA.COM>
  10. Date: Thu, 17 Dec 1992 23:53:50 GMT
  11. Lines: 68
  12.  
  13. In article <1992Dec16.170902.21889@nwnexus.WA.COM> osiris@halcyon.halcyon.com (David Ruggiero) writes:
  14. : If fmt(1) could do all that I wanted, I wouldn't have posted (see the original
  15. : for a statement of the problem). Besides, on my system [Ultrix], fmt(1) is so
  16. : brain dead that it doesn't even accept a line-length option, so it's useless
  17. : for the task I have laid out. What I want to do, I expect, takes a short
  18. : perl or awk script, and I figure *someone* out there must have one.
  19.  
  20. #!/usr/bin/perl
  21.  
  22. # Usage: reform [-lNUM] [-rNUM] [-iNUM] [-sNUM] [files]
  23.  
  24. # Set default values for left margin, right margin, indent
  25. # and paragraph spacing.
  26.  
  27. $l = 0;
  28. $r = 0;
  29. $i = 0;
  30. $s = 1;
  31.  
  32. # Process any switches.
  33.  
  34. while ($ARGV[0] =~ /^-/) {
  35.     $_ = shift;
  36.     /^-(l|r|i|s)(\d+)/ && (eval "\$$1 = \$2", next);
  37.     die "Unrecognized switch: $_\n";
  38. }
  39.  
  40. # Calculate format strings.
  41.  
  42. $r = $l + 65 unless $r;
  43. $r -= $l;                       # make $r relative to $l
  44. die "Margins too close\n" if $l + $i >= $r;
  45.  
  46. $LEFT = ' ' x $l;
  47. $INDENT = ' ' x $i;
  48. $RIGHT1 = '^' . '<' x ($r - 1 - $i);
  49. $RIGHT2 = '^' . '<' x ($r - 1);
  50. $SPACING = "\n" x $s;
  51.  
  52. # Define a format at run time.
  53.  
  54. $form = <<"End of Format Definition";
  55. format STDOUT =
  56. $LEFT$INDENT$RIGHT1
  57. \$_
  58. $LEFT$RIGHT2~~
  59. \$_
  60. $SPACING.
  61. End of Format Definition
  62.  
  63. print $form if $debugging;
  64. eval $form;
  65.  
  66. # Set paragraph mode on input.
  67.  
  68. $/ = '';
  69.  
  70. # For each paragraph...
  71.  
  72. while (<>) {
  73.     s/\s+/ /g;                      # Canonicalize white space.
  74.     s/ $//;                         # Trim final space.
  75.     s/([Ia-z0-9][)'"]*[.!?][)'"]*) /$1  /g; # Fix sentence ends.
  76.     write;                          # Spit out new paragraph.
  77. }
  78.  
  79. Larry Wall
  80. lwall@netlabs.com
  81.