File Coverage

File:/usr/local/share/automake-1.11/Automake/Wrap.pm
Coverage:17.5%

linestmtbrancondsubpodtimecode
1# Copyright (C) 2003, 2006 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2, or (at your option)
6# any later version.
7
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16package Automake::Wrap;
17
18
1
1
1
5
1
5
use strict;
19
20require Exporter;
21
1
1
1
4
1
2
use vars '@ISA', '@EXPORT_OK';
22@ISA = qw/Exporter/;
23@EXPORT_OK = qw/wrap makefile_wrap/;
24
25 - 49
=head1 NAME

Automake::Wrap - a paragraph formatter

=head1 SYNOPSIS

  use Automake::Wrap 'wrap', 'makefile_wrap';

  print wrap ($first_ident, $next_ident, $end_of_line, $max_length,
              @values);

  print makefile_wrap ("VARIABLE = ", "    ", @values);

=head1 DESCRIPTION

This modules provide facility to format list of strings.  It is
comparable to Perl's L<Text::Wrap>, however we can't use L<Text::Wrap>
because some versions will abort when some word to print exceeds the
maximum length allowed.  (Ticket #17141, fixed in Perl 5.8.0.)

=head2 Functions

=over 4

=cut
50
51# tab_length ($TXT)
52# -----------------
53# Compute the length of TXT, counting tab characters as 8 characters.
54sub tab_length($)
55{
56
0
0
  my ($txt) = @_;
57
0
  my $len = length ($txt);
58
0
  $len += 7 * ($txt =~ tr/\t/\t/);
59
0
  return $len;
60}
61
62 - 74
=item C<wrap ($head, $fill, $eol, $max_len, @values)>

Format C<@values> as a block of text that starts with C<$head>,
followed by the strings in C<@values> separated by spaces or by
C<"$eol\n$fill"> so that the length of each line never exceeds
C<$max_len>.

The C<$max_len> constraint is ignored for C<@values> items which
are too big to fit alone one a line.

The constructed paragraph is C<"\n">-terminated.

=cut
75
76sub wrap($$$$@)
77{
78
0
1
  my ($head, $fill, $eol, $max_len, @values) = @_;
79
80
0
  my $result = $head;
81
0
  my $column = tab_length ($head);
82
83
0
  my $fill_len = tab_length ($fill);
84
0
  my $eol_len = tab_length ($eol);
85
86
0
  my $not_first_word = 0;
87
88
0
  foreach (@values)
89    {
90
0
      my $len = tab_length ($_);
91
92      # See if the new variable fits on this line.
93      # (The + 1 is for the space we add in front of the value.).
94
0
      if ($column + $len + $eol_len + 1 > $max_len
95          # Do not break before the first word if it does not fit on
96          # the next line anyway.
97          && ($not_first_word || $fill_len + $len + $eol_len + 1 <= $max_len))
98        {
99          # Start a new line.
100
0
          $result .= "$eol\n" . $fill;
101
0
          $column = $fill_len;
102        }
103      elsif ($not_first_word)
104        {
105          # Add a space only if result does not already end
106          # with a space.
107
0
          $_ = " $_" if $result =~ /\S\z/;
108
0
          ++$len;
109        }
110
0
      $result .= $_;
111
0
      $column += $len;
112
0
      $not_first_word = 1;
113    }
114
115
0
  $result .= "\n";
116
0
  return $result;
117}
118
119
120 - 134
=item C<makefile_wrap ($head, $fill, @values)>

Format C<@values> in a way which is suitable for F<Makefile>s.
This is comparable to C<wrap>, except C<$eol> is known to
be C<" \\">, and the maximum length has been hardcoded to C<72>.

A space is appended to C<$head> when this is not already
the case.

This can be used to format variable definitions or dependency lines.

  makefile_wrap ('VARIABLE =', "\t", @values);
  makefile_wrap ('rule:', "\t", @dependencies);

=cut
135
136sub makefile_wrap ($$@)
137{
138
0
1
  my ($head, $fill, @values) = @_;
139
0
  if (@values)
140    {
141
0
      $head .= ' ' if $head =~ /\S\z/;
142
0
      return wrap $head, $fill, " \\", 72, @values;
143    }
144
0
  return "$head\n";
145}
146
147
1481;
149
150### Setup "GNU" style for perl-mode and cperl-mode.
151## Local Variables:
152## perl-indent-level: 2
153## perl-continued-statement-offset: 2
154## perl-continued-brace-offset: 0
155## perl-brace-offset: 0
156## perl-brace-imaginary-offset: 0
157## perl-label-offset: -2
158## cperl-indent-level: 2
159## cperl-brace-offset: 0
160## cperl-continued-brace-offset: 0
161## cperl-label-offset: -2
162## cperl-extra-newline-before-brace: t
163## cperl-merge-trailing-else: nil
164## cperl-continued-statement-offset: 2
165## End: