1 /* Output routines for ed-script format.
2 Copyright (C) 1988, 89, 91, 92, 93, 1998 Free Software Foundation, Inc.
4 This file is part of GNU DIFF.
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
20 static void print_ed_hunk PARAMS((struct change *));
21 static void print_rcs_hunk PARAMS((struct change *));
22 static void pr_forward_ed_hunk PARAMS((struct change *));
24 /* Print our script as ed commands. */
27 print_ed_script (script)
28 struct change *script;
30 print_script (script, find_reverse_change, print_ed_hunk);
33 /* Print a hunk of an ed diff */
43 hunk = flip_script (hunk);
49 /* Determine range of line numbers involved in each file. */
50 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
51 if (!deletes && !inserts)
56 /* Print out the line number header for this hunk */
57 print_number_range (',', &files[0], f0, l0);
58 printf_output ("%c\n", change_letter (inserts, deletes));
60 /* Print new/changed lines from second file, if needed */
65 for (i = f1; i <= l1; i++)
67 /* Resume the insert, if we stopped. */
69 printf_output ("%da\n",
70 i - f1 + translate_line_number (&files[0], f0) - 1);
73 /* If the file's line is just a dot, it would confuse `ed'.
74 So output it with a double dot, and set the flag LEADING_DOT
75 so that we will output another ed-command later
76 to change the double dot into a single dot. */
78 if (files[1].linbuf[i][0] == '.'
79 && files[1].linbuf[i][1] == '\n')
81 printf_output ("..\n");
82 printf_output (".\n");
83 /* Now change that double dot to the desired single dot. */
84 printf_output ("%ds/^\\.\\././\n",
85 i - f1 + translate_line_number (&files[0], f0));
89 /* Line is not `.', so output it unmodified. */
90 print_1_line ("", &files[1].linbuf[i]);
93 /* End insert mode, if we are still in it. */
95 printf_output (".\n");
99 /* Print change script in the style of ed commands,
100 but print the changes in the order they appear in the input files,
101 which means that the commands are not truly useful with ed. */
104 pr_forward_ed_script (script)
105 struct change *script;
107 print_script (script, find_change, pr_forward_ed_hunk);
111 pr_forward_ed_hunk (hunk)
116 int deletes, inserts;
118 /* Determine range of line numbers involved in each file. */
119 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
120 if (!deletes && !inserts)
125 printf_output ("%c", change_letter (inserts, deletes));
126 print_number_range (' ', files, f0, l0);
127 printf_output ("\n");
129 /* If deletion only, print just the number range. */
134 /* For insertion (with or without deletion), print the number range
135 and the lines from file 2. */
137 for (i = f1; i <= l1; i++)
138 print_1_line ("", &files[1].linbuf[i]);
140 printf_output (".\n");
143 /* Print in a format somewhat like ed commands
144 except that each insert command states the number of lines it inserts.
145 This format is used for RCS. */
148 print_rcs_script (script)
149 struct change *script;
151 print_script (script, find_change, print_rcs_hunk);
154 /* Print a hunk of an RCS diff */
157 print_rcs_hunk (hunk)
162 int deletes, inserts;
163 int tf0, tl0, tf1, tl1;
165 /* Determine range of line numbers involved in each file. */
166 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
167 if (!deletes && !inserts)
172 translate_range (&files[0], f0, l0, &tf0, &tl0);
177 /* For deletion, print just the starting line number from file 0
178 and the number of lines deleted. */
179 printf_output ("%d %d\n",
181 (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));
188 /* Take last-line-number from file 0 and # lines from file 1. */
189 translate_range (&files[1], f1, l1, &tf1, &tl1);
190 printf_output ("%d %d\n",
192 (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));
194 /* Print the inserted lines. */
195 for (i = f1; i <= l1; i++)
196 print_1_line ("", &files[1].linbuf[i]);