c90d1aa672e538e0c3279bd335297c1081f4f380
[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
11 package Text::Tabs;
12
13 require Exporter;
14
15 @ISA = (Exporter);
16 @EXPORT = qw(expand unexpand $tabstop);
17
18 $tabstop = 8;
19
20 sub expand
21 {
22         my @l = @_;
23         for $_ (@l) {
24                 1 while s/^([^\t]*)(\t+)/
25                         $1 . (" " x 
26                                 ($tabstop * length($2)
27                                 - (length($1) % $tabstop)))
28                         /e;
29         }
30         return @l;
31 }
32
33 sub unexpand
34 {
35         my @l = &expand(@_);
36         my @e;
37         for $x (@l) {
38                 @e = split(/(.{$tabstop})/,$x);
39                 for $_ (@e) {
40                         s/  +$/\t/;
41                 }
42                 $x = join('',@e);
43         }
44         return @l;
45 }
46
47 1;