Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | # |
2 | # expand and unexpand tabs as per the unix expand and | |
3 | # unexpand programs. | |
4 | # | |
4633a7c4 | 5 | # expand and unexpand operate on arrays of lines. Do not |
6 | # feed strings that contain newlines to them. | |
a0d0e21e | 7 | # |
8 | # David Muir Sharnoff <muir@idiom.com> | |
9 | # | |
4633a7c4 | 10 | # Version: 9/21/95 |
11 | # | |
a0d0e21e | 12 | |
75f92628 | 13 | package Text::Tabs; |
a0d0e21e | 14 | |
15 | require Exporter; | |
16 | ||
17 | @ISA = (Exporter); | |
18 | @EXPORT = qw(expand unexpand $tabstop); | |
19 | ||
20 | $tabstop = 8; | |
21 | ||
22 | sub expand | |
23 | { | |
4633a7c4 | 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; | |
a0d0e21e | 31 | } |
4633a7c4 | 32 | return @l if wantarray; |
33 | return @l[0]; | |
a0d0e21e | 34 | } |
35 | ||
36 | sub unexpand | |
37 | { | |
4633a7c4 | 38 | my @l = &expand(@_); |
a0d0e21e | 39 | my @e; |
4633a7c4 | 40 | for $x (@l) { |
41 | @e = split(/(.{$tabstop})/,$x); | |
42 | for $_ (@e) { | |
43 | s/ +$/\t/; | |
a0d0e21e | 44 | } |
4633a7c4 | 45 | $x = join('',@e); |
a0d0e21e | 46 | } |
4633a7c4 | 47 | return @l if wantarray; |
48 | return @l[0]; | |
a0d0e21e | 49 | } |
50 | ||
51 | 1; |