This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Build Text::Tabs before running mktables, as it needs it
[perl5.git] / ext / Text-Tabs / lib / Text / Tabs.pm
CommitLineData
cb1a09d0 1
75f92628 2package Text::Tabs;
a0d0e21e
LW
3
4require Exporter;
5
58389ed2 6@ISA = (Exporter);
a0d0e21e
LW
7@EXPORT = qw(expand unexpand $tabstop);
8
8bc713e8 9use vars qw($VERSION $tabstop $debug);
69e34dac 10$VERSION = 2009.0305;
8bc713e8 11
12use strict;
13
14BEGIN {
15 $tabstop = 8;
16 $debug = 0;
17}
a0d0e21e 18
8dfcc161
RGS
19sub expand {
20 my @l;
21 my $pad;
22 for ( @_ ) {
23 my $s = '';
24 for (split(/^/m, $_, -1)) {
25 my $offs = 0;
26 s{\t}{
27 $pad = $tabstop - (pos() + $offs) % $tabstop;
28 $offs += $pad - 1;
29 " " x $pad;
30 }eg;
31 $s .= $_;
32 }
33 push(@l, $s);
a0d0e21e 34 }
4633a7c4 35 return @l if wantarray;
4fc6b8d8 36 return $l[0];
a0d0e21e
LW
37}
38
39sub unexpand
40{
e8739726 41 my (@l) = @_;
a0d0e21e 42 my @e;
8bc713e8 43 my $x;
44 my $line;
45 my @lines;
46 my $lastbit;
95925ace 47 my $ts_as_space = " "x$tabstop;
4633a7c4 48 for $x (@l) {
8bc713e8 49 @lines = split("\n", $x, -1);
50 for $line (@lines) {
51 $line = expand($line);
52 @e = split(/(.{$tabstop})/,$line,-1);
53 $lastbit = pop(@e);
95925ace
SP
54 $lastbit = ''
55 unless defined $lastbit;
8bc713e8 56 $lastbit = "\t"
95925ace 57 if $lastbit eq $ts_as_space;
8bc713e8 58 for $_ (@e) {
59 if ($debug) {
60 my $x = $_;
61 $x =~ s/\t/^I\t/gs;
62 print "sub on '$x'\n";
63 }
64 s/ +$/\t/;
65 }
66 $line = join('',@e, $lastbit);
a0d0e21e 67 }
8bc713e8 68 $x = join("\n", @lines);
a0d0e21e 69 }
4633a7c4 70 return @l if wantarray;
4fc6b8d8 71 return $l[0];
a0d0e21e
LW
72}
73
741;
8bc713e8 75__END__
76
8dfcc161
RGS
77sub expand
78{
79 my (@l) = @_;
80 for $_ (@l) {
81 1 while s/(^|\n)([^\t\n]*)(\t+)/
82 $1. $2 . (" " x
83 ($tabstop * length($3)
84 - (length($2) % $tabstop)))
85 /sex;
86 }
87 return @l if wantarray;
88 return $l[0];
89}
90
8bc713e8 91
92=head1 NAME
93
4fc6b8d8 94Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1)
8bc713e8 95
96=head1 SYNOPSIS
97
6b1c26d6 98 use Text::Tabs;
8bc713e8 99
95925ace 100 $tabstop = 4; # default = 8
6b1c26d6
JF
101 @lines_without_tabs = expand(@lines_with_tabs);
102 @lines_with_tabs = unexpand(@lines_without_tabs);
8bc713e8 103
104=head1 DESCRIPTION
105
95925ace 106Text::Tabs does about what the unix utilities expand(1) and unexpand(1)
8bc713e8 107do. Given a line with tabs in it, expand will replace the tabs with
108the appropriate number of spaces. Given a line with or without tabs in
95925ace
SP
109it, unexpand will add tabs when it can save bytes by doing so (just
110like C<unexpand -a>). Invisible compression with plain ASCII!
111
112=head1 EXAMPLE
113
114 #!perl
115 # unexpand -a
116 use Text::Tabs;
117
118 while (<>) {
119 print unexpand $_;
120 }
121
122Instead of the C<expand> comand, use:
123
124 perl -MText::Tabs -n -e 'print expand $_'
125
126Instead of the C<unexpand -a> command, use:
127
128 perl -MText::Tabs -n -e 'print unexpand $_'
8bc713e8 129
8dfcc161
RGS
130=head1 LICENSE
131
95925ace 132Copyright (C) 1996-2002,2005,2006 David Muir Sharnoff.
8dfcc161
RGS
133Copyright (C) 2005 Aristotle Pagaltzis
134This module may be modified, used, copied, and redistributed at your own risk.
135Publicly redistributed modified versions must use a different name.
8bc713e8 136