Commit | Line | Data |
cb1a09d0 |
1 | |
75f92628 |
2 | package Text::Tabs; |
a0d0e21e |
3 | |
4 | require Exporter; |
5 | |
abc0156b |
6 | @ISA = qw(Exporter); |
a0d0e21e |
7 | @EXPORT = qw(expand unexpand $tabstop); |
8 | |
8bc713e8 |
9 | use vars qw($VERSION $tabstop $debug); |
8dfcc161 |
10 | $VERSION = 2005.0824; |
8bc713e8 |
11 | |
12 | use strict; |
13 | |
14 | BEGIN { |
15 | $tabstop = 8; |
16 | $debug = 0; |
17 | } |
a0d0e21e |
18 | |
8dfcc161 |
19 | sub 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 |
37 | } |
38 | |
39 | sub unexpand |
40 | { |
e8739726 |
41 | my (@l) = @_; |
a0d0e21e |
42 | my @e; |
8bc713e8 |
43 | my $x; |
44 | my $line; |
45 | my @lines; |
46 | my $lastbit; |
4633a7c4 |
47 | for $x (@l) { |
8bc713e8 |
48 | @lines = split("\n", $x, -1); |
49 | for $line (@lines) { |
50 | $line = expand($line); |
51 | @e = split(/(.{$tabstop})/,$line,-1); |
52 | $lastbit = pop(@e); |
53 | $lastbit = '' unless defined $lastbit; |
54 | $lastbit = "\t" |
55 | if $lastbit eq " "x$tabstop; |
56 | for $_ (@e) { |
57 | if ($debug) { |
58 | my $x = $_; |
59 | $x =~ s/\t/^I\t/gs; |
60 | print "sub on '$x'\n"; |
61 | } |
62 | s/ +$/\t/; |
63 | } |
64 | $line = join('',@e, $lastbit); |
a0d0e21e |
65 | } |
8bc713e8 |
66 | $x = join("\n", @lines); |
a0d0e21e |
67 | } |
4633a7c4 |
68 | return @l if wantarray; |
4fc6b8d8 |
69 | return $l[0]; |
a0d0e21e |
70 | } |
71 | |
72 | 1; |
8bc713e8 |
73 | __END__ |
74 | |
8dfcc161 |
75 | sub expand |
76 | { |
77 | my (@l) = @_; |
78 | for $_ (@l) { |
79 | 1 while s/(^|\n)([^\t\n]*)(\t+)/ |
80 | $1. $2 . (" " x |
81 | ($tabstop * length($3) |
82 | - (length($2) % $tabstop))) |
83 | /sex; |
84 | } |
85 | return @l if wantarray; |
86 | return $l[0]; |
87 | } |
88 | |
8bc713e8 |
89 | |
90 | =head1 NAME |
91 | |
4fc6b8d8 |
92 | Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1) |
8bc713e8 |
93 | |
94 | =head1 SYNOPSIS |
95 | |
6b1c26d6 |
96 | use Text::Tabs; |
8bc713e8 |
97 | |
6b1c26d6 |
98 | $tabstop = 4; |
99 | @lines_without_tabs = expand(@lines_with_tabs); |
100 | @lines_with_tabs = unexpand(@lines_without_tabs); |
8bc713e8 |
101 | |
102 | =head1 DESCRIPTION |
103 | |
104 | Text::Tabs does about what the unix utilities expand(1) and unexpand(1) |
105 | do. Given a line with tabs in it, expand will replace the tabs with |
106 | the appropriate number of spaces. Given a line with or without tabs in |
107 | it, unexpand will add tabs when it can save bytes by doing so. Invisible |
108 | compression with plain ascii! |
109 | |
110 | =head1 BUGS |
111 | |
112 | expand doesn't handle newlines very quickly -- do not feed it an |
113 | entire document in one string. Instead feed it an array of lines. |
114 | |
8dfcc161 |
115 | =head1 LICENSE |
116 | |
117 | Copyright (C) 1996-2002,2005 David Muir Sharnoff. |
118 | Copyright (C) 2005 Aristotle Pagaltzis |
119 | This module may be modified, used, copied, and redistributed at your own risk. |
120 | Publicly redistributed modified versions must use a different name. |
8bc713e8 |
121 | |