home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!koonda.acci.com.au!ggr
- From: ggr@acci.com.au (Greg Rose)
- Subject: source: program to adjust tabs
- Message-ID: <9224517.6903@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Reply-To: ggr@acci.com.au (Greg Rose)
- Organization: Australian Computing and Communications Institute
- Date: Tue, 1 Sep 1992 07:29:08 GMT
- Lines: 50
-
- Well, after reading the thread about preferred indentation, and then
- running up against a program that used 8-column tabs to a depth of
- ridiculousness, which I basically needed to rewrite anyway, I wrote
- the following script to alter the indentation of anything that uses
- lots of tabs to have leading four-space indentation.
-
- The hardest part of this job is the fact that if you take a program
- that uses tabs *within* the line, say to line up declarations or
- comments, and you just screw with the leading tabs, these things won't
- line up any more. This script takes care of them, too.
-
- You run it by saying something like "retab *.c".
-
- Share and Enjoy,
- Greg.
-
- #!/usr/local/bin/perl -pi.orig
-
- # retab programs from 8 character indents to 4 character indents,
- # but preserving column alignment in declarations. Output should
- # produce 8 character tabs as much as possible, and assumes 8 character
- # tab stops in the input.
-
- # make leading spaces into tabs first
- undef while s/^(\t*) /$1\t/;
-
- # check for nothing to do
- if (/^\t/) {
- # split off leading tabs for separate handling
- s/^(\t*)//;
- $tabs = $1;
-
- # put out leading tabs
- print "\t" while $tabs =~ s/\t\t//;
-
- # put out four spaces
- if ($tabs) {
- print " ";
- # might have to change alignment of rest of line
- if (/^([^\t]*)\t/) {
- # fix the internal tabs
- s/\t/\t\t/ if (length($1) % 8) < 4;
- }
- }
- }
- # end of retab
-
- --
- Greg Rose Australian Computing and Communications Institute
- ggr@acci.com.au +61 3 282 2532
-