1 # Copyright (C) 2003, 2006, 2009 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;
21 use vars '@ISA', '@EXPORT_OK';
23 @EXPORT_OK = qw/wrap makefile_wrap/;
27 Automake::Wrap - a paragraph formatter
31 use Automake::Wrap 'wrap', 'makefile_wrap';
33 print wrap ($first_ident, $next_ident, $end_of_line, $max_length,
36 print makefile_wrap ("VARIABLE = ", " ", @values);
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.)
53 # Compute the length of TXT, counting tab characters as 8 characters.
57 my $len = length ($txt);
58 $len += 7 * ($txt =~ tr/\t/\t/);
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
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.
78 my ($head, $fill, $eol, $max_len, @values) = @_;
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;
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))
100 $result .= "$eol\n" . $fill;
103 elsif ($not_first_word)
105 # Add a space only if result does not already end
107 $_ = " $_" if $result =~ /\S\z/;
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
129 This can be used to format variable definitions or dependency lines.
131 makefile_wrap ('VARIABLE =', "\t", @values);
132 makefile_wrap ('rule:', "\t", @dependencies);
136 sub makefile_wrap ($$@)
138 my ($head, $fill, @values) = @_;
141 $head .= ' ' if $head =~ /\S\z/;
142 return wrap $head, $fill, " \\", 72, @values;
150 ### Setup "GNU" style for perl-mode and cperl-mode.
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