4 # Pretty-print diff outputs with GNU enscript.
5 # Copyright (c) 1996-1998 Markku Rossi
7 # Author: Markku Rossi <mtr@iki.fi>
11 # This file is part of GNU Enscript.
13 # Enscript is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
18 # Enscript is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with Enscript. If not, see <http://www.gnu.org/licenses/>.
28 # Original idea by Trent Fisher <trent@informix.com>
30 # - Tero Kivinen <kivinen@iki.fi> for the first prototype
31 # - Tait Cyrus <tait.cyrus@mci.com> for fixes and cleanups
39 $program =~ s/.*\///g;
42 warn "Usage: $program ORIGINAL_FILE < DIFF\n\n";
43 warn "Program reads a diff file from its standard input and annotates
44 ORIGINAL_FILE to show the changes made to the file. The easiest way to use
45 this program is to use it as an input filter for GNU enscript:
47 \$ enscript -G2re --filter='rcsdiff %s | diffpp %s' *.c *.h
48 \$ enscript -G2re --filter='diff %s~ %s | diffpp %s' *.c *.h
53 if (!defined($file) || defined($ARGV[0])) {
58 if ($file eq "--help") {
63 if ($file eq "--version") {
68 # Read in original file into internal array.
69 open(FP, $file) || die "$program: couldn't open file `$file' for input: $!\n";
74 $orig_num_lines = @orig_file;
76 # Now read in file of diffs into internal array.
79 $diff_num_lines = @diffs;
81 while ($diff_line_num <= $diff_num_lines) {
82 $_ = $diffs[$diff_line_num];
92 while ($orig_line_num <= $orig_num_lines) {
93 print $orig_file[$orig_line_num++];
96 # Handle new/added lines
103 if ($line =~ /(\d+)a(\d+),(\d+)/) {
104 $insert_at_line = $1;
105 $num_new_lines = $3 - $2 + 1;
106 } elsif ($line =~ /(\d+)a(\d+)/) {
107 $insert_at_line = $1;
110 &skip_to_line($insert_at_line);
111 printf("\000shade{$add_gray}");
112 &mark_to_line($num_new_lines, "+");
113 printf("\000shade{1.0}");
116 # Handle deleted/removed lines
123 if ($line =~ /(\d+),(\d+)d(\d+)/) {
124 $num_del_lines = $2 - $1 + 1;
125 } elsif ($line =~ /(\d+)d(\d+)/) {
129 &skip_to_line($del_from - 1);
130 printf("\000shade{$del_gray}");
131 &mark_to_line($num_del_lines, "-");
132 printf("\000shade{1.0}");
133 $orig_line_num += $num_del_lines;
136 # Handle changed/modified lines
145 if ($line =~ /(\d+),(\d+)c(\d+),(\d+)/) {
146 $num_old_lines = $2 - $1 + 1;
147 $num_new_lines = $4 - $3 + 1;
148 $change_at_line = $1;
149 } elsif ($line =~ /(\d+),(\d+)c(\d+)/) {
150 $num_old_lines = $2 - $1 + 1;
152 $change_at_line = $1;
153 } elsif ($line =~ /(\d+)c(\d+),(\d+)/) {
155 $num_new_lines = $3 - $2 + 1;
156 $change_at_line = $1;
157 } elsif ($line =~ /(\d+)c(\d+)/) {
160 $change_at_line = $1;
163 &skip_to_line($change_at_line - 1);
164 $orig_line_num += $num_old_lines; # skip over changed lines
165 printf("\000shade{$del_gray}");
166 &mark_to_line($num_old_lines, "-");
167 printf("\000shade{1.0}");
169 printf("\000shade{$add_gray}");
170 &mark_to_line($num_new_lines, "+");
171 printf("\000shade{1.0}");
177 while ($orig_line_num <= $line) {
178 print $orig_file[$orig_line_num];
184 ($num_lines, $marker) = @_;
186 $diff_line_num++; # skip over diff command
187 while ($num_lines > 0) {
188 $diff_line = substr($diffs[$diff_line_num++],3);
189 print "\000ps{gsave -5 0 rmoveto ($marker) show grestore}";