Start anew
[git/jnareb-git.git] / lib / perl5 / 5.6.1 / Text / Tabs.pm
blobc431019908e04dbe4697df06ab564967eba89b7a
2 package Text::Tabs;
4 require Exporter;
6 @ISA = (Exporter);
7 @EXPORT = qw(expand unexpand $tabstop);
9 use vars qw($VERSION $tabstop $debug);
10 $VERSION = 98.112801;
12 use strict;
14 BEGIN {
15 $tabstop = 8;
16 $debug = 0;
19 sub expand
21 my (@l) = @_;
22 for $_ (@l) {
23 1 while s/(^|\n)([^\t\n]*)(\t+)/
24 $1. $2 . (" " x
25 ($tabstop * length($3)
26 - (length($2) % $tabstop)))
27 /sex;
29 return @l if wantarray;
30 return $l[0];
33 sub unexpand
35 my (@l) = @_;
36 my @e;
37 my $x;
38 my $line;
39 my @lines;
40 my $lastbit;
41 for $x (@l) {
42 @lines = split("\n", $x, -1);
43 for $line (@lines) {
44 $line = expand($line);
45 @e = split(/(.{$tabstop})/,$line,-1);
46 $lastbit = pop(@e);
47 $lastbit = '' unless defined $lastbit;
48 $lastbit = "\t"
49 if $lastbit eq " "x$tabstop;
50 for $_ (@e) {
51 if ($debug) {
52 my $x = $_;
53 $x =~ s/\t/^I\t/gs;
54 print "sub on '$x'\n";
56 s/ +$/\t/;
58 $line = join('',@e, $lastbit);
60 $x = join("\n", @lines);
62 return @l if wantarray;
63 return $l[0];
67 __END__
70 =head1 NAME
72 Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1)
74 =head1 SYNOPSIS
76 use Text::Tabs;
78 $tabstop = 4;
79 @lines_without_tabs = expand(@lines_with_tabs);
80 @lines_with_tabs = unexpand(@lines_without_tabs);
82 =head1 DESCRIPTION
84 Text::Tabs does about what the unix utilities expand(1) and unexpand(1)
85 do. Given a line with tabs in it, expand will replace the tabs with
86 the appropriate number of spaces. Given a line with or without tabs in
87 it, unexpand will add tabs when it can save bytes by doing so. Invisible
88 compression with plain ascii!
90 =head1 BUGS
92 expand doesn't handle newlines very quickly -- do not feed it an
93 entire document in one string. Instead feed it an array of lines.
95 =head1 AUTHOR
97 David Muir Sharnoff <muir@idiom.com>