1 # Copyright (C) 2003-2012 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)
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;
22 use vars '@ISA', '@EXPORT_OK';
24 @EXPORT_OK = qw/wrap makefile_wrap/;
28 Automake::Wrap - a paragraph formatter
32 use Automake::Wrap 'wrap', 'makefile_wrap';
34 print wrap ($first_ident, $next_ident, $end_of_line, $max_length,
37 print makefile_wrap ("VARIABLE = ", " ", @values);
41 This modules provide facility to format list of strings. It is
42 comparable to Perl's L<Text::Wrap>, however we can't use L<Text::Wrap>
43 because some versions will abort when some word to print exceeds the
44 maximum length allowed. (Ticket #17141, fixed in Perl 5.8.0.)
54 # Compute the length of TXT, counting tab characters as 8 characters.
58 my $len = length ($txt);
59 $len += 7 * ($txt =~ tr/\t/\t/);
63 =item C<wrap ($head, $fill, $eol, $max_len, @values)>
65 Format C<@values> as a block of text that starts with C<$head>,
66 followed by the strings in C<@values> separated by spaces or by
67 C<"$eol\n$fill"> so that the length of each line never exceeds
70 The C<$max_len> constraint is ignored for C<@values> items which
71 are too big to fit alone one a line.
73 The constructed paragraph is C<"\n">-terminated.
79 my ($head, $fill, $eol, $max_len, @values) = @_;
82 my $column = _tab_length ($head);
84 my $fill_len = _tab_length ($fill);
85 my $eol_len = _tab_length ($eol);
87 my $not_first_word = 0;
91 my $len = _tab_length ($_);
93 # See if the new variable fits on this line.
94 # (The + 1 is for the space we add in front of the value.).
95 if ($column + $len + $eol_len + 1 > $max_len
96 # Do not break before the first word if it does not fit on
97 # the next line anyway.
98 && ($not_first_word || $fill_len + $len + $eol_len + 1 <= $max_len))
101 $result .= "$eol\n" . $fill;
104 elsif ($not_first_word)
106 # Add a space only if result does not already end
108 $_ = " $_" if $result =~ /\S\z/;
121 =item C<makefile_wrap ($head, $fill, @values)>
123 Format C<@values> in a way which is suitable for F<Makefile>s.
124 This is comparable to C<wrap>, except C<$eol> is known to
125 be C<" \\">, and the maximum length has been hardcoded to C<72>.
127 A space is appended to C<$head> when this is not already
130 This can be used to format variable definitions or dependency lines.
132 makefile_wrap ('VARIABLE =', "\t", @values);
133 makefile_wrap ('rule:', "\t", @dependencies);
137 sub makefile_wrap ($$@)
139 my ($head, $fill, @values) = @_;
142 $head .= ' ' if $head =~ /\S\z/;
143 return wrap $head, $fill, " \\", 72, @values;
151 ### Setup "GNU" style for perl-mode and cperl-mode.
153 ## perl-indent-level: 2
154 ## perl-continued-statement-offset: 2
155 ## perl-continued-brace-offset: 0
156 ## perl-brace-offset: 0
157 ## perl-brace-imaginary-offset: 0
158 ## perl-label-offset: -2
159 ## cperl-indent-level: 2
160 ## cperl-brace-offset: 0
161 ## cperl-continued-brace-offset: 0
162 ## cperl-label-offset: -2
163 ## cperl-extra-newline-before-brace: t
164 ## cperl-merge-trailing-else: nil
165 ## cperl-continued-statement-offset: 2