7cfb478b759c9e4e96ea37e79e79c6704c01eebb
[p5sagit/p5-mst-13.2.git] / lib / Text / Tabs.pm
1 #
2 # expand and unexpand tabs as per the unix expand and 
3 # unexpand programs.
4 #
5 # expand and unexpand operate on arrays of lines.  Do not
6 # feed strings that contain newlines to them.
7 #
8 # David Muir Sharnoff <muir@idiom.com>
9
10 # Version: 9/21/95
11 #
12
13 package Text::Tabs;
14
15 require Exporter;
16
17 @ISA = (Exporter);
18 @EXPORT = qw(expand unexpand $tabstop);
19
20 $tabstop = 8;
21
22 sub expand
23 {
24         my @l = @_;
25         for $_ (@l) {
26                 1 while s/^([^\t]*)(\t+)/
27                         $1 . (" " x 
28                                 ($tabstop * length($2)
29                                 - (length($1) % $tabstop)))
30                         /e;
31         }
32         return @l if wantarray;
33         return @l[0];
34 }
35
36 sub unexpand
37 {
38         my @l = &expand(@_);
39         my @e;
40         for $x (@l) {
41                 @e = split(/(.{$tabstop})/,$x);
42                 for $_ (@e) {
43                         s/  +$/\t/;
44                 }
45                 $x = join('',@e);
46         }
47         return @l if wantarray;
48         return @l[0];
49 }
50
51 1;