automake-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FYI: 1/ Automake::Wrap


From: Alexandre Duret-Lutz
Subject: FYI: 1/ Automake::Wrap
Date: Wed, 21 May 2003 22:27:52 +0200
User-agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.3 (gnu/linux)

I have a copy of Automake in which variables are stored as object.
(An object for each variable name, and an object for each
conditional definition.)  

The patch is rather big and still need some work, however I'll start
to commit independents chunks of it.

This first part moves &pretty_print_internal() in a separate
package: Automake::Wrap.  The rational is that this function is
used by &output_variable() which I'll move in the Automake::Variable
package later, so it's cleaner if Automake::Variable doesn't
have to call any Automake functions.

Another point is that I'd like to use Automake::Wrap in
Automake::Channels to format diagnostics, this is why I've added
a few tuning arguments that weren't in the original function.
(I won't work on this before I've finished with the variables,
in case someone wants to do it.)

Here is what I've installed on HEAD.

2003-05-21  Alexandre Duret-Lutz  <address@hidden>

        * lib/Automake/Wrap.pm, lib/Automake/tests/Wrap.pl: New files.
        * lib/Automake/Makefile.am (dist_perllib_DATA): Add Wrap.pm.
        * lib/Automake/tests/Makefile.am (TESTS): Add Wrap.pl.
        * automake.in (handle_texinfo_helper, pretty_print_rule)
        (variable_output): Adjust to use makefile_wrap instead of
        pretty_print_internal.
        (pretty_print_internal): Remove.  Renamed as Automake::Wrap::wrap
        and augmented to accept the $eol and $max_len arguments.

Index: automake.in
===================================================================
RCS file: /cvs/automake/automake/automake.in,v
retrieving revision 1.1456
diff -u -r1.1456 automake.in
--- automake.in 27 Apr 2003 18:14:34 -0000      1.1456
+++ automake.in 21 May 2003 20:12:20 -0000
@@ -129,6 +129,7 @@
 use Automake::Condition qw/TRUE FALSE/;
 use Automake::DisjConditions;
 use Automake::Version;
+use Automake::Wrap 'makefile_wrap';
 use File::Basename;
 use Tie::RefHash;
 use Carp;
@@ -3847,7 +3848,7 @@
     &define_variable ('am__TEXINFO_TEX_DIR', $texinfodir, INTERNAL);
 
     # The return value.
-    my $texiclean = &pretty_print_internal ("", "\t  ", @texi_cleans);
+    my $texiclean = makefile_wrap ("", "\t  ", @texi_cleans);
 
     push (@dist_targets, 'dist-info');
 
@@ -5900,43 +5901,10 @@
 
 ################################################################
 
-# Pretty-print something.  HEAD is what should be printed at the
-# beginning of the first line, FILL is what should be printed at the
-# beginning of every subsequent line.
-sub pretty_print_internal
-{
-    my ($head, $fill, @values) = @_;
-
-    my $column = length ($head);
-    my $result = $head;
-
-    # Fill length is number of characters.  However, each Tab
-    # character counts for eight.  So we count the number of Tabs and
-    # multiply by 7.
-    my $fill_length = length ($fill);
-    $fill_length += 7 * ($fill =~ tr/\t/\t/d);
-
-    foreach (@values)
-    {
-       # "71" because we also print a space.
-       if ($column + length ($_) > 71)
-       {
-           $result .= " \\\n" . $fill;
-           $column = $fill_length;
-       }
-       $result .= ' ' if $result =~ /\S\z/;
-       $result .= $_;
-       $column += length ($_) + 1;
-    }
-
-    $result .= "\n";
-    return $result;
-}
-
 # Pretty-print something and append to output_rules.
 sub pretty_print_rule
 {
-    $output_rules .= &pretty_print_internal (@_);
+    $output_rules .= &makefile_wrap (@_);
 }
 
 
@@ -6888,12 +6856,11 @@
 
       if ($var_pretty{$var}{$cond} == VAR_PRETTY)
        {
-         # Suppress escaped new lines.  &pretty_print_internal will
+         # Suppress escaped new lines.  &makefile_wrap will
          # add them back, maybe at other places.
          $val =~ s/\\$//mg;
-         $output_vars .= pretty_print_internal ("$str$var $equals",
-                                                "$str\t",
-                                                split (' ' , $val));
+         $output_vars .= makefile_wrap ("$str$var $equals",
+                                        "$str\t", split (' ' , $val));
        }
       else                     # VAR_ASIS
        {
Index: lib/Automake/Makefile.am
===================================================================
RCS file: /cvs/automake/automake/lib/Automake/Makefile.am,v
retrieving revision 1.11
diff -u -r1.11 Makefile.am
--- lib/Automake/Makefile.am    11 Apr 2003 22:11:43 -0000      1.11
+++ lib/Automake/Makefile.am    21 May 2003 20:12:25 -0000
@@ -29,4 +29,5 @@
   Location.pm \
   Struct.pm \
   Version.pm \
-  XFile.pm
+  XFile.pm \
+  Wrap.pm
Index: lib/Automake/Wrap.pm
===================================================================
RCS file: lib/Automake/Wrap.pm
diff -N lib/Automake/Wrap.pm
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/Automake/Wrap.pm        21 May 2003 20:12:25 -0000
@@ -0,0 +1,167 @@
+# Copyright (C) 2003  Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+# 02111-1307, USA.
+
+package Automake::Wrap;
+
+use strict;
+
+require Exporter;
+use vars '@ISA', '@EXPORT_OK';
address@hidden = qw/Exporter/;
address@hidden = qw/wrap makefile_wrap/;
+
+=head1 NAME
+
+Automake::Wrap - a paragraph formater
+
+=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 exceed the
+maximum length allowed.  (Ticket #17141, fixed in Perl 5.8.0.)
+
+=head2 Functions
+
+=over 4
+
+=cut
+
+# tab_length ($TXT)
+# -----------------
+# Compute the length of TXT, counting tab characters as 8 characters.
+sub tab_length($)
+{
+  my ($txt) = @_;
+  my $len = length ($txt);
+  $len += 7 * ($txt =~ tr/\t/\t/d);
+  return $len;
+}
+
+=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 lenght of each line never exceed
+C<$max_len>.
+
+The C<$max_len> contraint is ignored for C<@values> items which
+are too big to fit alone one a line.
+
+The constructed paragraph is C<"\n">-terminated.
+
+=cut
+
+sub wrap($$$$@)
+{
+  my ($head, $fill, $eol, $max_len, @values) = @_;
+
+  my $result = $head;
+  my $column = tab_length ($head);
+
+  my $fill_len = tab_length ($fill);
+  my $eol_len = tab_length ($eol);
+
+  my $not_first_word = 0;
+
+  foreach (@values)
+    {
+      my $len = tab_length ($_);
+
+      # See if the new variable fits on this line.
+      # (The + 1 is for the space we add in front of the value.).
+      if ($column + $len + $eol_len + 1 > $max_len
+         # Do not break before the first word if it does not fit on
+         # the next line anyway.
+         && ($not_first_word || $fill_len + $len + $eol_len + 1 <= $max_len))
+       {
+         # Start a new line.
+         $result .= "$eol\n" . $fill;
+         $column = $fill_len;
+       }
+      elsif ($not_first_word)
+       {
+         # Add a space only if result does not already end
+         # with a space.
+         $_ = " $_" if $result =~ /\S\z/;
+         ++$len;
+       }
+      $result .= $_;
+      $column += $len;
+      $not_first_word = 1;
+    }
+
+  $result .= "\n";
+  return $result;
+}
+
+
+=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
+
+sub makefile_wrap ($$@)
+{
+  my ($head, $fill, @values) = @_;
+  if (@values)
+    {
+      $head .= ' ' if $head =~ /\S\z/;
+      return wrap $head, $fill, " \\", 72, @values;
+    }
+  return "$head\n";
+}
+
+
+1;
+
+### Setup "GNU" style for perl-mode and cperl-mode.
+## Local Variables:
+## perl-indent-level: 2
+## perl-continued-statement-offset: 2
+## perl-continued-brace-offset: 0
+## perl-brace-offset: 0
+## perl-brace-imaginary-offset: 0
+## perl-label-offset: -2
+## cperl-indent-level: 2
+## cperl-brace-offset: 0
+## cperl-continued-brace-offset: 0
+## cperl-label-offset: -2
+## cperl-extra-newline-before-brace: t
+## cperl-merge-trailing-else: nil
+## cperl-continued-statement-offset: 2
+## End:
Index: lib/Automake/tests/Makefile.am
===================================================================
RCS file: /cvs/automake/automake/lib/Automake/tests/Makefile.am,v
retrieving revision 1.3
diff -u -r1.3 Makefile.am
--- lib/Automake/tests/Makefile.am      11 Apr 2003 22:11:43 -0000      1.3
+++ lib/Automake/tests/Makefile.am      21 May 2003 20:12:25 -0000
@@ -21,6 +21,7 @@
 TESTS = \
 Condition.pl \
 DisjConditions.pl \
-Version.pl
+Version.pl \
+Wrap.pl
 
 EXTRA_DIST = $(TESTS)
Index: lib/Automake/tests/Wrap.pl
===================================================================
RCS file: lib/Automake/tests/Wrap.pl
diff -N lib/Automake/tests/Wrap.pl
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/Automake/tests/Wrap.pl  21 May 2003 20:12:25 -0000
@@ -0,0 +1,82 @@
+# Copyright (C) 2003  Free Software Foundation, Inc.
+#
+# This file is part of GNU Automake.
+#
+# GNU Automake is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# GNU Automake is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Automake; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+use Automake::Wrap 'wrap';
+
+my $failed = 0;
+
+sub test_wrap
+{
+  my ($in, $exp_out) = @_;
+
+  my $out = &wrap (@$in);
+  if ($out ne $exp_out)
+    {
+      print STDERR "For: @$in\nGot:\n$out\nInstead of:\n$exp_out\n---\n";
+      ++$failed;
+    }
+}
+
+my @tests = (
+  [["HEAD:", "NEXT:", "CONT", 13, "v" ,"a", "l", "ue", "s", "values"],
+"HEAD:v aCONT
+NEXT:l ueCONT
+NEXT:sCONT
+NEXT:values
+"],
+  [["rule: ", "\t", " \\", 20, "dep1" ,"dep2", "dep3", "dep4", "dep5",
+    "dep06", "dep07", "dep08"],
+"rule: dep1 dep2 \\
+\tdep3 dep4 \\
+\tdep5 dep06 \\
+\tdep07 \\
+\tdep08
+"],
+  [["big header:", "big continuation:", " END", 5, "diag1", "diag2", "diag3"],
+"big header:diag1 END
+big continuation:diag2 END
+big continuation:diag3
+"],
+  [["big header:", "cont: ", " END", 16, "word1", "word2"],
+"big header: END
+cont: word1 END
+cont: word2
+"]);
+
+
+test_wrap (@{$_}) foreach @tests;
+
+exit $failed;
+
+### Setup "GNU" style for perl-mode and cperl-mode.
+## Local Variables:
+## perl-indent-level: 2
+## perl-continued-statement-offset: 2
+## perl-continued-brace-offset: 0
+## perl-brace-offset: 0
+## perl-brace-imaginary-offset: 0
+## perl-label-offset: -2
+## cperl-indent-level: 2
+## cperl-brace-offset: 0
+## cperl-continued-brace-offset: 0
+## cperl-label-offset: -2
+## cperl-extra-newline-before-brace: t
+## cperl-merge-trailing-else: nil
+## cperl-continued-statement-offset: 2
+## End:

-- 
Alexandre Duret-Lutz





reply via email to

[Prev in Thread] Current Thread [Next in Thread]