summaryrefslogtreecommitdiff
path: root/lib/Text
diff options
context:
space:
mode:
authorLarry Wall <lwall@scalpel.netlabs.com>1995-11-21 10:01:00 +1200
committerLarry <lwall@scalpel.netlabs.com>1995-11-21 10:01:00 +1200
commit4633a7c4bad06b471d9310620b7fe8ddd158cccd (patch)
tree37ebeb26a64f123784fd8fac6243b124767243b0 /lib/Text
parent8e07c86ebc651fe92eb7e3b25f801f57cfb8dd6f (diff)
downloadperl-4633a7c4bad06b471d9310620b7fe8ddd158cccd.tar.gz
5.002 beta 1
If you're adventurous, have a look at ftp://ftp.sems.com/pub/outgoing/perl5.0/perl5.002beta1.tar.gz Many thanks to Andy for doing the integration. Obviously, if you consult the bugs database, you'll note there are still plenty of buglets that need fixing, and several enhancements that I've intended to put in still haven't made it in (Hi, Tim and Ilya). But I think it'll be pretty stable. And you can start to fiddle around with prototypes (which are, of course, still totally undocumented). Packrats, don't worry too much about readvertising this widely. Nowadays we're on a T1 here, so our bandwidth is okay. Have the appropriate amount of jollity. Larry
Diffstat (limited to 'lib/Text')
-rw-r--r--lib/Text/Tabs.pm54
-rw-r--r--lib/Text/Wrap.pm68
2 files changed, 89 insertions, 33 deletions
diff --git a/lib/Text/Tabs.pm b/lib/Text/Tabs.pm
index fa866988cf..7cfb478b75 100644
--- a/lib/Text/Tabs.pm
+++ b/lib/Text/Tabs.pm
@@ -2,11 +2,13 @@
# expand and unexpand tabs as per the unix expand and
# unexpand programs.
#
-# expand and unexpand operate on arrays of lines.
+# expand and unexpand operate on arrays of lines. Do not
+# feed strings that contain newlines to them.
#
# David Muir Sharnoff <muir@idiom.com>
-# Version: 4/19/95
#
+# Version: 9/21/95
+#
package Text::Tabs;
@@ -19,45 +21,31 @@ $tabstop = 8;
sub expand
{
- my (@l) = @_;
- my $l, @k;
- my $nl;
- for $l (@l) {
- $nl = $/ if chomp($l);
- @k = split($/,$l);
- for $_ (@k) {
- 1 while s/^([^\t]*)(\t+)/
- $1 . (" " x
- ($tabstop * length($2)
- - (length($1) % $tabstop)))
- /e;
- }
- $l = join("\n",@k).$nl;
+ my @l = @_;
+ for $_ (@l) {
+ 1 while s/^([^\t]*)(\t+)/
+ $1 . (" " x
+ ($tabstop * length($2)
+ - (length($1) % $tabstop)))
+ /e;
}
- return @l if $#l > 0;
- return $l[0];
+ return @l if wantarray;
+ return @l[0];
}
sub unexpand
{
- my (@l) = &expand(@_);
+ my @l = &expand(@_);
my @e;
- my $k, @k;
- my $nl;
- for $k (@l) {
- $nl = $/ if chomp($k);
- @k = split($/,$k);
- for $x (@k) {
- @e = split(/(.{$tabstop})/,$x);
- for $_ (@e) {
- s/ +$/\t/;
- }
- $x = join('',@e);
+ for $x (@l) {
+ @e = split(/(.{$tabstop})/,$x);
+ for $_ (@e) {
+ s/ +$/\t/;
}
- $k = join("\n",@k).$nl;
+ $x = join('',@e);
}
- return @l if $#l > 0;
- return $l[0];
+ return @l if wantarray;
+ return @l[0];
}
1;
diff --git a/lib/Text/Wrap.pm b/lib/Text/Wrap.pm
new file mode 100644
index 0000000000..9b1d054704
--- /dev/null
+++ b/lib/Text/Wrap.pm
@@ -0,0 +1,68 @@
+
+package Text::Wrap;
+
+#
+# This is a very simple paragraph formatter. It formats one
+# paragraph at a time by wrapping and indenting text.
+#
+# Usage:
+#
+# use Text::Wrap;
+#
+# print wrap($initial_tab,$subsequent_tab,@text);
+#
+# You can also set the number of columns to wrap before:
+#
+# $Text::Wrap::columns = 135; # <= width of screen
+#
+# use Text::Wrap qw(wrap $columns);
+# $columns = 70;
+#
+#
+# The first line will be printed with $initial_tab prepended. All
+# following lines will have $subsequent_tab prepended.
+#
+# Example:
+#
+# print wrap("\t","","This is a bit of text that ...");
+#
+# David Muir Sharnoff <muir@idiom.com>
+# Version: 9/21/95
+#
+
+require Exporter;
+
+@ISA = (Exporter);
+@EXPORT = qw(wrap);
+@EXPORT_OK = qw($columns);
+
+BEGIN {
+ $Text::Wrap::columns = 76; # <= screen width
+}
+
+use Text::Tabs;
+use strict;
+
+sub wrap
+{
+ my ($ip, $xp, @t) = @_;
+
+ my $r;
+ my $t = expand(join(" ",@t));
+ my $lead = $ip;
+ my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
+ if ($t =~ s/^([^\n]{0,$ll})\s//) {
+ $r .= unexpand($lead . $1 . "\n");
+ $lead = $xp;
+ my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
+ while ($t =~ s/^([^\n]{0,$ll})\s//) {
+ $r .= unexpand($lead . $1 . "\n");
+ }
+ }
+ die "couldn't wrap '$t'"
+ if length($t) > $ll;
+ $r .= $t;
+ return $r;
+}
+
+1;