3 # Copyright (C) 2000-2003 Simon Huggins
4 # merge merges the sig and the tag but also merges the sig and the new style
5 # plugin things (i.e. all those silly files in $cfg{'tmpdir'}
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 2 of the License, or (at your option)
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc., 59
19 # Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 $Text::Wrap::columns=defined $cfg{'maxlinelen'} ? $cfg{'maxlinelen'} : 72;
26 $cfg{'leader'} ||= "";
28 my $anal_merge_debug=0;
33 # Remove whitespace at the end of lines but not newlines themselves.
34 # And don't remove the space if it comes directly after a -- which is
35 # anchored at the beginning of a line.
37 $text =~ s/(?<!^--)[ ]*$//mg;
39 # Remove any newlines from the very end of the string.
52 my ($plugin,$len,$align,$wascr);
55 while ($sig =~ /@([A-Za-z]?)(\*|(?:[1-9][0-9]*))([RC]?)@/) {
61 } elsif (not defined $2) {
65 } elsif ($2 =~ /^[RC]$/) {
75 print STDERR "plugin,len,type = #$plugin#,#$len#,#$align#\n" if $anal_merge_debug;
77 $chunk = getplugin($plugin);
78 print STDERR "Got plugin $plugin and $chunk\n"
80 $len = quotemeta $len; # escape * if it is *
81 $sig =~ s/\@$plugin$len[RC]?@/$chunk/;
82 print STDERR "Sig is now:\n$sig" if $anal_merge_debug;
88 $chunk = substr $tag, 0, $len;
89 print STDERR "chunk,tag = #$chunk#,#$tag#".length($tag)." ".length($chunk)."\n"
91 if ($chunk =~ s/^([^\n]+)\n+(.*)$/$1/s) {
93 print STDERR "\$extra = [$extra]\n"
96 if (length($chunk) < $len) {
97 print STDERR "length(chunk) < $len\n"
99 $chunk=&chunksizealign($chunk,$len,$align);
100 print STDERR "chunk = #$chunk#\n"
101 if $anal_merge_debug;
103 if (length($tag) < $len + 1) {
104 $tag= $extra ? $extra : "";
105 print STDERR "length(tag) < $len + 1, tag now = #$tag#(extra = #$extra#)\n"
106 if $anal_merge_debug;
107 } elsif (substr $tag, 0, $len + 1 eq ' ') {
108 $tag=substr $tag, $len + 1;
109 $tag=$extra . $tag if defined $extra;
110 print STDERR "substr tag, 0, $len + 1 was a space. tag now = #$tag#\n"
111 if $anal_merge_debug;
113 $tag=substr $tag, $len;
114 ### Back up a word in $chunk
115 $tag=$extra . $tag if defined $extra;
116 print STDERR "didn't break at space. Backing up word. tag now = #$tag#\n"
117 if $anal_merge_debug;
118 if ($chunk =~ s/(.*) (.*)$/$1/) {
120 $chunk=&chunksizealign($chunk,$len,$align);
122 print STDERR "If space in chunk then change chunk and add word to tag.".
123 "Reformat chunk now = #$chunk# (tag = #$tag#)\n" if $anal_merge_debug;
129 $len = quotemeta $len; # escape * if it's *
130 $sig =~ s/\@$plugin$len[RC]?@/$chunk/;
133 $sig =~ s/@([0-9]+)[RC]?@/" " x $1/eg;
134 $sig =~ s/@\*[RC]?@//g;
135 $cfg{'notag'} = $notag;
136 if ($tag and not $notag) {
139 return &remove_space($sig);
148 $count = $plugins{$plugin} if defined $plugins{$plugin};
149 open(IN, "$cfg{'tmpdir'}/$plugin") or htagdie "$0: Could not open $cfg{'tmpdir'}/$plugin: $!\n";
155 $plugins{$plugin} = $count+1;
163 my ($tag,$sig,$newsig);
164 open(SIG, "<$cfg{'tmpsigfile'}") or htagdie "$0: Could not open $cfg{'tmpsigfile'}: $!\n";
170 if (grep { /\@NOTAG\@/ } $sig) {
173 $sig =~ s/\@NOTAG\@\n//;
175 open(TAG, "<$cfg{'tmptagfile'}") or htagdie "$1: Could not open $cfg{'tmptagfile'}: $!\n";
181 if (defined $sig and $sig =~ /@[A-Za-z]?\*|(?:[1-9][0-9]*)[RC]?@/) {
182 $sig = merge($tag,$sig);
184 my $formatted_tag = Text::Wrap::wrap($cfg{'first'},$cfg{'leader'},$tag);
185 $sig .= $formatted_tag;
186 $sig = &remove_space($sig);
190 open(SIG, ">$cfg{'tmpsigfile'}") or htagdie "$0: Could not open $cfg{'tmpsigfile'}: $!\n";
191 print SIG "\n" while $cfg{'newline'}--;