home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5649 < prev    next >
Encoding:
Text File  |  1992-08-31  |  1.9 KB  |  62 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!koonda.acci.com.au!ggr
  3. From: ggr@acci.com.au (Greg Rose)
  4. Subject: source: program to adjust tabs
  5. Message-ID: <9224517.6903@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Reply-To: ggr@acci.com.au (Greg Rose)
  8. Organization: Australian Computing and Communications Institute
  9. Date: Tue, 1 Sep 1992 07:29:08 GMT
  10. Lines: 50
  11.  
  12. Well, after reading the thread about preferred indentation, and then
  13. running up against a program that used 8-column tabs to a depth of
  14. ridiculousness, which I basically needed to rewrite anyway, I wrote
  15. the following script to alter the indentation of anything that uses
  16. lots of tabs to have leading four-space indentation.
  17.  
  18. The hardest part of this job is the fact that if you take a program
  19. that uses tabs *within* the line, say to line up declarations or
  20. comments, and you just screw with the leading tabs, these things won't
  21. line up any more. This script takes care of them, too. 
  22.  
  23. You run it by saying something like "retab *.c".
  24.  
  25. Share and Enjoy,
  26. Greg.
  27.  
  28. #!/usr/local/bin/perl -pi.orig
  29.  
  30. # retab programs from 8 character indents to 4 character indents,
  31. # but preserving column alignment in declarations. Output should
  32. # produce 8 character tabs as much as possible, and assumes 8 character
  33. # tab stops in the input.
  34.  
  35. # make leading spaces into tabs first
  36. undef while s/^(\t*)        /$1\t/;
  37.  
  38. # check for nothing to do
  39. if (/^\t/) {
  40.     # split off leading tabs for separate handling
  41.     s/^(\t*)//;
  42.     $tabs = $1;
  43.  
  44.     # put out leading tabs
  45.     print "\t" while $tabs =~ s/\t\t//;
  46.  
  47.     # put out four spaces
  48.     if ($tabs) {
  49.     print "    ";
  50.     # might have to change alignment of rest of line
  51.     if (/^([^\t]*)\t/) {
  52.         # fix the internal tabs
  53.         s/\t/\t\t/ if (length($1) % 8) < 4;
  54.     }
  55.     }
  56. }
  57. # end of retab
  58.  
  59. --
  60. Greg Rose                 Australian Computing and Communications Institute
  61. ggr@acci.com.au                                              +61 3 282 2532
  62.