Support more C++ file extensions for MSVC in the compile script.
[platform/upstream/automake.git] / lib / Automake / Wrap.pm
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
16 package Automake::Wrap;
17
18 use strict;
19
20 require Exporter;
21 use vars '@ISA', '@EXPORT_OK';
22 @ISA = qw/Exporter/;
23 @EXPORT_OK = qw/wrap makefile_wrap/;
24
25 =head1 NAME
26
27 Automake::Wrap - a paragraph formatter
28
29 =head1 SYNOPSIS
30
31   use Automake::Wrap 'wrap', 'makefile_wrap';
32
33   print wrap ($first_ident, $next_ident, $end_of_line, $max_length,
34               @values);
35
36   print makefile_wrap ("VARIABLE = ", "    ", @values);
37
38 =head1 DESCRIPTION
39
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.)
44
45 =head2 Functions
46
47 =over 4
48
49 =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   my ($txt) = @_;
57   my $len = length ($txt);
58   $len += 7 * ($txt =~ tr/\t/\t/);
59   return $len;
60 }
61
62 =item C<wrap ($head, $fill, $eol, $max_len, @values)>
63
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>.
68
69 The C<$max_len> constraint is ignored for C<@values> items which
70 are too big to fit alone one a line.
71
72 The constructed paragraph is C<"\n">-terminated.
73
74 =cut
75
76 sub wrap($$$$@)
77 {
78   my ($head, $fill, $eol, $max_len, @values) = @_;
79
80   my $result = $head;
81   my $column = tab_length ($head);
82
83   my $fill_len = tab_length ($fill);
84   my $eol_len = tab_length ($eol);
85
86   my $not_first_word = 0;
87
88   foreach (@values)
89     {
90       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       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           $result .= "$eol\n" . $fill;
101           $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           $_ = " $_" if $result =~ /\S\z/;
108           ++$len;
109         }
110       $result .= $_;
111       $column += $len;
112       $not_first_word = 1;
113     }
114
115   $result .= "\n";
116   return $result;
117 }
118
119
120 =item C<makefile_wrap ($head, $fill, @values)>
121
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>.
125
126 A space is appended to C<$head> when this is not already
127 the case.
128
129 This can be used to format variable definitions or dependency lines.
130
131   makefile_wrap ('VARIABLE =', "\t", @values);
132   makefile_wrap ('rule:', "\t", @dependencies);
133
134 =cut
135
136 sub makefile_wrap ($$@)
137 {
138   my ($head, $fill, @values) = @_;
139   if (@values)
140     {
141       $head .= ' ' if $head =~ /\S\z/;
142       return wrap $head, $fill, " \\", 72, @values;
143     }
144   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: