RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / toolchains / hndtools-arm-linux-2.6.36-uclibc-4.5.3 / share / automake-1.11 / Automake / Wrap.pm
blob66213d1ee4e16d3d6de9bacedb275d5e4a5ca1c5
1 # Copyright (C) 2003, 2006 Free Software Foundation, Inc.
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.
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.
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/>.
16 package Automake::Wrap;
18 use strict;
20 require Exporter;
21 use vars '@ISA', '@EXPORT_OK';
22 @ISA = qw/Exporter/;
23 @EXPORT_OK = qw/wrap makefile_wrap/;
25 =head1 NAME
27 Automake::Wrap - a paragraph formatter
29 =head1 SYNOPSIS
31 use Automake::Wrap 'wrap', 'makefile_wrap';
33 print wrap ($first_ident, $next_ident, $end_of_line, $max_length,
34 @values);
36 print makefile_wrap ("VARIABLE = ", " ", @values);
38 =head1 DESCRIPTION
40 This modules provide facility to format list of strings. It is
41 comparable to Perl's L<Text::Wrap>, however we can't use L<Text::Wrap>
42 because some versions will abort when some word to print exceeds the
43 maximum length allowed. (Ticket #17141, fixed in Perl 5.8.0.)
45 =head2 Functions
47 =over 4
49 =cut
51 # tab_length ($TXT)
52 # -----------------
53 # Compute the length of TXT, counting tab characters as 8 characters.
54 sub tab_length($)
56 my ($txt) = @_;
57 my $len = length ($txt);
58 $len += 7 * ($txt =~ tr/\t/\t/);
59 return $len;
62 =item C<wrap ($head, $fill, $eol, $max_len, @values)>
64 Format C<@values> as a block of text that starts with C<$head>,
65 followed by the strings in C<@values> separated by spaces or by
66 C<"$eol\n$fill"> so that the length of each line never exceeds
67 C<$max_len>.
69 The C<$max_len> constraint is ignored for C<@values> items which
70 are too big to fit alone one a line.
72 The constructed paragraph is C<"\n">-terminated.
74 =cut
76 sub wrap($$$$@)
78 my ($head, $fill, $eol, $max_len, @values) = @_;
80 my $result = $head;
81 my $column = tab_length ($head);
83 my $fill_len = tab_length ($fill);
84 my $eol_len = tab_length ($eol);
86 my $not_first_word = 0;
88 foreach (@values)
90 my $len = tab_length ($_);
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 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))
99 # Start a new line.
100 $result .= "$eol\n" . $fill;
101 $column = $fill_len;
103 elsif ($not_first_word)
105 # Add a space only if result does not already end
106 # with a space.
107 $_ = " $_" if $result =~ /\S\z/;
108 ++$len;
110 $result .= $_;
111 $column += $len;
112 $not_first_word = 1;
115 $result .= "\n";
116 return $result;
120 =item C<makefile_wrap ($head, $fill, @values)>
122 Format C<@values> in a way which is suitable for F<Makefile>s.
123 This is comparable to C<wrap>, except C<$eol> is known to
124 be C<" \\">, and the maximum length has been hardcoded to C<72>.
126 A space is appended to C<$head> when this is not already
127 the case.
129 This can be used to format variable definitions or dependency lines.
131 makefile_wrap ('VARIABLE =', "\t", @values);
132 makefile_wrap ('rule:', "\t", @dependencies);
134 =cut
136 sub makefile_wrap ($$@)
138 my ($head, $fill, @values) = @_;
139 if (@values)
141 $head .= ' ' if $head =~ /\S\z/;
142 return wrap $head, $fill, " \\", 72, @values;
144 return "$head\n";
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: