File: | /tmp/automake/lib/Automake/Wrap.pm |
Coverage: | 100.0% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # Copyright (C) 2003, 2006, 2009 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 | |||||||
16 | package Automake::Wrap; | ||||||
17 | |||||||
18 | 1163 1163 1163 | 3249 1135 3162 | use strict; | ||||
19 | |||||||
20 | require Exporter; | ||||||
21 | 1163 1163 1163 | 4069 1232 3185 | 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. | ||||||
54 | sub _tab_length($) | ||||||
55 | { | ||||||
56 | 334278 | 433857 | my ($txt) = @_; | ||||
57 | 334278 | 348704 | my $len = length ($txt); | ||||
58 | 334278 | 478690 | $len += 7 * ($txt =~ tr/\t/\t/); | ||||
59 | 334278 | 460124 | 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 | |||||||
76 | sub wrap($$$$@) | ||||||
77 | { | ||||||
78 | 49795 | 1 | 133873 | my ($head, $fill, $eol, $max_len, @values) = @_; | |||
79 | |||||||
80 | 49795 | 51490 | my $result = $head; | ||||
81 | 49795 | 76570 | my $column = _tab_length ($head); | ||||
82 | |||||||
83 | 49795 | 73124 | my $fill_len = _tab_length ($fill); | ||||
84 | 49795 | 72175 | my $eol_len = _tab_length ($eol); | ||||
85 | |||||||
86 | 49795 | 50713 | my $not_first_word = 0; | ||||
87 | |||||||
88 | 49795 | 77448 | foreach (@values) | ||||
89 | { | ||||||
90 | 184893 | 266217 | 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 | 184893 | 740363 | 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 | 27887 | 45494 | $result .= "$eol\n" . $fill; | ||||
101 | 27887 | 31495 | $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 | 107221 | 352898 | $_ = " $_" if $result =~ /\S\z/; | ||||
108 | 107221 | 108990 | ++$len; | ||||
109 | } | ||||||
110 | 184893 | 202372 | $result .= $_; | ||||
111 | 184893 | 182250 | $column += $len; | ||||
112 | 184893 | 270972 | $not_first_word = 1; | ||||
113 | } | ||||||
114 | |||||||
115 | 49795 | 57860 | $result .= "\n"; | ||||
116 | 49795 | 223714 | 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 | |||||||
136 | sub makefile_wrap ($$@) | ||||||
137 | { | ||||||
138 | 69662 | 1 | 153164 | my ($head, $fill, @values) = @_; | |||
139 | 69662 | 125404 | if (@values) | ||||
140 | { | ||||||
141 | 49775 | 164087 | $head .= ' ' if $head =~ /\S\z/; | ||||
142 | 49775 | 103800 | return wrap $head, $fill, " \\", 72, @values; | ||||
143 | } | ||||||
144 | 19887 | 80517 | return "$head\n"; | ||||
145 | } | ||||||
146 | |||||||
147 | |||||||
148 | 1; | ||||||
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: |