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 # This program 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 2, or (at your option)
18 # This program 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 this program; see the file COPYING. If not, write to
25 # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26 # Boston, MA 02110-1301, USA.
30 # Original idea by Trent Fisher <trent@informix.com>
32 # - Tero Kivinen <kivinen@iki.fi> for the first prototype
33 # - Tait Cyrus <tait.cyrus@mci.com> for fixes and cleanups
41 $program =~ s/.*\///g;
44 warn "Usage: $program ORIGINAL_FILE < DIFF\n\n";
45 warn "Program reads a diff file from its standard input and annotates
46 ORIGINAL_FILE to show the changes made to the file. The easiest way to use
47 this program is to use it as an input filter for GNU enscript:
49 \$ enscript -G2re --filter='rcsdiff %s | diffpp %s' *.c *.h
50 \$ enscript -G2re --filter='diff %s~ %s | diffpp %s' *.c *.h
55 if (!defined($file) || defined($ARGV[0])) {
60 if ($file eq "--help") {
65 if ($file eq "--version") {
70 # Read in original file into internal array.
71 open(FP, $file) || die "$program: couldn't open file `$file' for input: $!\n";
76 $orig_num_lines = @orig_file;
78 # Now read in file of diffs into internal array.
81 $diff_num_lines = @diffs;
83 while ($diff_line_num <= $diff_num_lines) {
84 $_ = $diffs[$diff_line_num];
94 while ($orig_line_num <= $orig_num_lines) {
95 print $orig_file[$orig_line_num++];
98 # Handle new/added lines
105 if ($line =~ /(\d+)a(\d+),(\d+)/) {
106 $insert_at_line = $1;
107 $num_new_lines = $3 - $2 + 1;
108 } elsif ($line =~ /(\d+)a(\d+)/) {
109 $insert_at_line = $1;
112 &skip_to_line($insert_at_line);
113 printf("\000shade{$add_gray}");
114 &mark_to_line($num_new_lines, "+");
115 printf("\000shade{1.0}");
118 # Handle deleted/removed lines
125 if ($line =~ /(\d+),(\d+)d(\d+)/) {
126 $num_del_lines = $2 - $1 + 1;
127 } elsif ($line =~ /(\d+)d(\d+)/) {
131 &skip_to_line($del_from - 1);
132 printf("\000shade{$del_gray}");
133 &mark_to_line($num_del_lines, "-");
134 printf("\000shade{1.0}");
135 $orig_line_num += $num_del_lines;
138 # Handle changed/modified lines
147 if ($line =~ /(\d+),(\d+)c(\d+),(\d+)/) {
148 $num_old_lines = $2 - $1 + 1;
149 $num_new_lines = $4 - $3 + 1;
150 $change_at_line = $1;
151 } elsif ($line =~ /(\d+),(\d+)c(\d+)/) {
152 $num_old_lines = $2 - $1 + 1;
154 $change_at_line = $1;
155 } elsif ($line =~ /(\d+)c(\d+),(\d+)/) {
157 $num_new_lines = $3 - $2 + 1;
158 $change_at_line = $1;
159 } elsif ($line =~ /(\d+)c(\d+)/) {
162 $change_at_line = $1;
165 &skip_to_line($change_at_line - 1);
166 $orig_line_num += $num_old_lines; # skip over changed lines
167 printf("\000shade{$del_gray}");
168 &mark_to_line($num_old_lines, "-");
169 printf("\000shade{1.0}");
171 printf("\000shade{$add_gray}");
172 &mark_to_line($num_new_lines, "+");
173 printf("\000shade{1.0}");
179 while ($orig_line_num <= $line) {
180 print $orig_file[$orig_line_num];
186 ($num_lines, $marker) = @_;
188 $diff_line_num++; # skip over diff command
189 while ($num_lines > 0) {
190 $diff_line = substr($diffs[$diff_line_num++],3);
191 print "\000ps{gsave -5 0 rmoveto ($marker) show grestore}";