Wrap.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright (C) 2003-2017 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2, or (at your option)
  5. # any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Automake::Wrap;
  13. use 5.006;
  14. use strict;
  15. require Exporter;
  16. use vars '@ISA', '@EXPORT_OK';
  17. @ISA = qw/Exporter/;
  18. @EXPORT_OK = qw/wrap makefile_wrap/;
  19. =head1 NAME
  20. Automake::Wrap - a paragraph formatter
  21. =head1 SYNOPSIS
  22. use Automake::Wrap 'wrap', 'makefile_wrap';
  23. print wrap ($first_ident, $next_ident, $end_of_line, $max_length,
  24. @values);
  25. print makefile_wrap ("VARIABLE = ", " ", @values);
  26. =head1 DESCRIPTION
  27. This modules provide facility to format list of strings. It is
  28. comparable to Perl's L<Text::Wrap>, however we can't use L<Text::Wrap>
  29. because some versions will abort when some word to print exceeds the
  30. maximum length allowed. (Ticket #17141, fixed in Perl 5.8.0.)
  31. =head2 Functions
  32. =over 4
  33. =cut
  34. # _tab_length ($TXT)
  35. # ------------------
  36. # Compute the length of TXT, counting tab characters as 8 characters.
  37. sub _tab_length($)
  38. {
  39. my ($txt) = @_;
  40. my $len = length ($txt);
  41. $len += 7 * ($txt =~ tr/\t/\t/);
  42. return $len;
  43. }
  44. =item C<wrap ($head, $fill, $eol, $max_len, @values)>
  45. Format C<@values> as a block of text that starts with C<$head>,
  46. followed by the strings in C<@values> separated by spaces or by
  47. C<"$eol\n$fill"> so that the length of each line never exceeds
  48. C<$max_len>.
  49. The C<$max_len> constraint is ignored for C<@values> items which
  50. are too big to fit alone one a line.
  51. The constructed paragraph is C<"\n">-terminated.
  52. =cut
  53. sub wrap($$$$@)
  54. {
  55. my ($head, $fill, $eol, $max_len, @values) = @_;
  56. my $result = $head;
  57. my $column = _tab_length ($head);
  58. my $fill_len = _tab_length ($fill);
  59. my $eol_len = _tab_length ($eol);
  60. my $not_first_word = 0;
  61. foreach (@values)
  62. {
  63. my $len = _tab_length ($_);
  64. # See if the new variable fits on this line.
  65. # (The + 1 is for the space we add in front of the value.).
  66. if ($column + $len + $eol_len + 1 > $max_len
  67. # Do not break before the first word if it does not fit on
  68. # the next line anyway.
  69. && ($not_first_word || $fill_len + $len + $eol_len + 1 <= $max_len))
  70. {
  71. # Start a new line.
  72. $result .= "$eol\n" . $fill;
  73. $column = $fill_len;
  74. }
  75. elsif ($not_first_word)
  76. {
  77. # Add a space only if result does not already end
  78. # with a space.
  79. $_ = " $_" if $result =~ /\S\z/;
  80. ++$len;
  81. }
  82. $result .= $_;
  83. $column += $len;
  84. $not_first_word = 1;
  85. }
  86. $result .= "\n";
  87. return $result;
  88. }
  89. =item C<makefile_wrap ($head, $fill, @values)>
  90. Format C<@values> in a way which is suitable for F<Makefile>s.
  91. This is comparable to C<wrap>, except C<$eol> is known to
  92. be C<" \\">, and the maximum length has been hardcoded to C<72>.
  93. A space is appended to C<$head> when this is not already
  94. the case.
  95. This can be used to format variable definitions or dependency lines.
  96. makefile_wrap ('VARIABLE =', "\t", @values);
  97. makefile_wrap ('rule:', "\t", @dependencies);
  98. =cut
  99. sub makefile_wrap ($$@)
  100. {
  101. my ($head, $fill, @values) = @_;
  102. if (@values)
  103. {
  104. $head .= ' ' if $head =~ /\S\z/;
  105. return wrap $head, $fill, " \\", 72, @values;
  106. }
  107. return "$head\n";
  108. }
  109. 1;
  110. ### Setup "GNU" style for perl-mode and cperl-mode.
  111. ## Local Variables:
  112. ## perl-indent-level: 2
  113. ## perl-continued-statement-offset: 2
  114. ## perl-continued-brace-offset: 0
  115. ## perl-brace-offset: 0
  116. ## perl-brace-imaginary-offset: 0
  117. ## perl-label-offset: -2
  118. ## cperl-indent-level: 2
  119. ## cperl-brace-offset: 0
  120. ## cperl-continued-brace-offset: 0
  121. ## cperl-label-offset: -2
  122. ## cperl-extra-newline-before-brace: t
  123. ## cperl-merge-trailing-else: nil
  124. ## cperl-continued-statement-offset: 2
  125. ## End: