blob: 971ef6520de39c32bc0575b7bdbff9580b1cc1ee [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +000011 * diff.c: code for diff'ing two, three or four buffers.
Bram Moolenaare828b762018-09-10 17:51:58 +020012 *
13 * There are three ways to diff:
14 * - Shell out to an external diff program, using files.
15 * - Use the compiled-in xdiff library.
16 * - Let 'diffexpr' do the work, using files.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 */
18
19#include "vim.h"
Bram Moolenaare828b762018-09-10 17:51:58 +020020#include "xdiff/xdiff.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22#if defined(FEAT_DIFF) || defined(PROTO)
23
Bram Moolenaard2b58c02018-09-16 18:10:48 +020024static int diff_busy = FALSE; // using diff structs, don't change them
25static int diff_need_update = FALSE; // ex_diffupdate needs to be called
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010027// flags obtained from the 'diffopt' option
Bram Moolenaar785fc652018-09-15 19:17:38 +020028#define DIFF_FILLER 0x001 // display filler lines
29#define DIFF_IBLANK 0x002 // ignore empty lines
30#define DIFF_ICASE 0x004 // ignore case
31#define DIFF_IWHITE 0x008 // ignore change in white space
32#define DIFF_IWHITEALL 0x010 // ignore all white space changes
33#define DIFF_IWHITEEOL 0x020 // ignore change in white space at EOL
34#define DIFF_HORIZONTAL 0x040 // horizontal splits
35#define DIFF_VERTICAL 0x080 // vertical splits
36#define DIFF_HIDDEN_OFF 0x100 // diffoff when hidden
37#define DIFF_INTERNAL 0x200 // use internal xdiff algorithm
Bram Moolenaarc8234772019-11-10 21:00:27 +010038#define DIFF_CLOSE_OFF 0x400 // diffoff when closing window
Bram Moolenaar4223d432021-02-10 13:18:17 +010039#define DIFF_FOLLOWWRAP 0x800 // follow the wrap option
Jonathon7c7a4e62025-01-12 09:58:00 +010040#define DIFF_LINEMATCH 0x1000 // match most similar lines within diff
Bram Moolenaar785fc652018-09-15 19:17:38 +020041#define ALL_WHITE_DIFF (DIFF_IWHITE | DIFF_IWHITEALL | DIFF_IWHITEEOL)
Bram Moolenaarc8234772019-11-10 21:00:27 +010042static int diff_flags = DIFF_INTERNAL | DIFF_FILLER | DIFF_CLOSE_OFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
Bram Moolenaare828b762018-09-10 17:51:58 +020044static long diff_algorithm = 0;
45
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010046#define LBUFLEN 50 // length of line in diff file
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010048static int diff_a_works = MAYBE; // TRUE when "diff -a" works, FALSE when it
49 // doesn't work, MAYBE when not checked yet
Bram Moolenaar48e330a2016-02-23 14:53:34 +010050#if defined(MSWIN)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010051static int diff_bin_works = MAYBE; // TRUE when "diff --binary" works, FALSE
52 // when it doesn't work, MAYBE when not
53 // checked yet
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#endif
55
Bram Moolenaare828b762018-09-10 17:51:58 +020056// used for diff input
57typedef struct {
58 char_u *din_fname; // used for external diff
59 mmfile_t din_mmfile; // used for internal diff
60} diffin_T;
61
62// used for diff result
63typedef struct {
64 char_u *dout_fname; // used for external diff
65 garray_T dout_ga; // used for internal diff
66} diffout_T;
67
Lewis Russelld9da86e2021-12-28 13:54:41 +000068// used for recording hunks from xdiff
69typedef struct {
70 linenr_T lnum_orig;
71 long count_orig;
72 linenr_T lnum_new;
73 long count_new;
74} diffhunk_T;
75
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +010076typedef enum {
77 DIO_OUTPUT_INDICES = 0, // default
78 DIO_OUTPUT_UNIFIED = 1 // unified diff format
79} dio_outfmt_T;
80
Bram Moolenaare828b762018-09-10 17:51:58 +020081// two diff inputs and one result
82typedef struct {
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +010083 diffin_T dio_orig; // original file input
84 diffin_T dio_new; // new file input
85 diffout_T dio_diff; // diff result
86 int dio_internal; // using internal diff
87 dio_outfmt_T dio_outfmt; // internal diff output format
88 int dio_ctxlen; // unified diff context length
Bram Moolenaare828b762018-09-10 17:51:58 +020089} diffio_T;
90
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static int diff_buf_idx(buf_T *buf);
92static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp);
93static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after);
94static void diff_check_unchanged(tabpage_T *tp, diff_T *dp);
95static int diff_check_sanity(tabpage_T *tp, diff_T *dp);
Bram Moolenaare828b762018-09-10 17:51:58 +020096static int check_external_diff(diffio_T *diffio);
97static int diff_file(diffio_T *diffio);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010098static int diff_equal_entry(diff_T *dp, int idx1, int idx2);
99static int diff_cmp(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void diff_fold_update(diff_T *dp, int skip_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Lewis Russelld9da86e2021-12-28 13:54:41 +0000103static void diff_read(int idx_orig, int idx_new, diffio_T *dio);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100104static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new);
105static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp);
Lewis Russelld9da86e2021-12-28 13:54:41 +0000106static int parse_diff_ed(char_u *line, diffhunk_T *hunk);
107static int parse_diff_unified(char_u *line, diffhunk_T *hunk);
Yegappan Lakshmananfa378352024-02-01 22:05:27 +0100108static int xdiff_out_indices(long start_a, long count_a, long start_b, long count_b, void *priv);
109static int xdiff_out_unified(void *priv, mmbuffer_t *mb, int nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200111#define FOR_ALL_DIFFBLOCKS_IN_TAB(tp, dp) \
112 for ((dp) = (tp)->tp_first_diff; (dp) != NULL; (dp) = (dp)->df_next)
113
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 */
117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100118diff_buf_delete(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000121 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
Bram Moolenaar29323592016-07-24 22:04:11 +0200123 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000125 i = diff_buf_idx_tp(buf, tp);
126 if (i != DB_COUNT)
127 {
128 tp->tp_diffbuf[i] = NULL;
129 tp->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000130 if (tp == curtab)
Bram Moolenaarcd38bb42022-06-26 14:04:07 +0100131 {
132 // don't redraw right away, more might change or buffer state
133 // is invalid right now
134 need_diff_redraw = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100135 redraw_later(UPD_VALID);
Bram Moolenaarcd38bb42022-06-26 14:04:07 +0100136 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 }
139}
140
141/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000142 * Check if the current buffer should be added to or removed from the list of
143 * diff buffers.
144 */
145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100146diff_buf_adjust(win_T *win)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000147{
148 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000149 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000150
151 if (!win->w_p_diff)
152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100153 // When there is no window showing a diff for this buffer, remove
154 // it from the diffs.
Bram Moolenaar29323592016-07-24 22:04:11 +0200155 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000156 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
157 break;
158 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000159 {
160 i = diff_buf_idx(win->w_buffer);
161 if (i != DB_COUNT)
162 {
163 curtab->tp_diffbuf[i] = NULL;
164 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000165 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000166 }
167 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000168 }
169 else
170 diff_buf_add(win->w_buffer);
171}
172
173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175 * Call this when a new buffer is being edited in the current window where
176 * 'diff' is set.
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000177 * Marks the current buffer as being part of the diff and requiring updating.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000178 * This must be done before any autocmd, because a command may use info
179 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 */
181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100182diff_buf_add(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183{
184 int i;
185
186 if (diff_buf_idx(buf) != DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100187 return; // It's already there.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000190 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000192 curtab->tp_diffbuf[i] = buf;
193 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000194 diff_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 return;
196 }
197
Bram Moolenaare1242042021-12-16 20:56:57 +0000198 semsg(_(e_cannot_diff_more_than_nr_buffers), DB_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199}
200
201/*
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100202 * Remove all buffers to make diffs for.
203 */
204 static void
205diff_buf_clear(void)
206{
207 int i;
208
209 for (i = 0; i < DB_COUNT; ++i)
210 if (curtab->tp_diffbuf[i] != NULL)
211 {
212 curtab->tp_diffbuf[i] = NULL;
213 curtab->tp_diff_invalid = TRUE;
214 diff_redraw(TRUE);
215 }
216}
217
218/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000219 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 * Return its index or DB_COUNT if not found.
221 */
222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100223diff_buf_idx(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224{
225 int idx;
226
227 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000228 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 break;
230 return idx;
231}
232
233/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000234 * Find buffer "buf" in the list of diff buffers for tab page "tp".
235 * Return its index or DB_COUNT if not found.
236 */
237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100238diff_buf_idx_tp(buf_T *buf, tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000239{
240 int idx;
241
242 for (idx = 0; idx < DB_COUNT; ++idx)
243 if (tp->tp_diffbuf[idx] == buf)
244 break;
245 return idx;
246}
247
248/*
249 * Mark the diff info involving buffer "buf" as invalid, it will be updated
250 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100253diff_invalidate(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000255 tabpage_T *tp;
256 int i;
257
Bram Moolenaar29323592016-07-24 22:04:11 +0200258 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000260 i = diff_buf_idx_tp(buf, tp);
261 if (i != DB_COUNT)
262 {
263 tp->tp_diff_invalid = TRUE;
264 if (tp == curtab)
265 diff_redraw(TRUE);
266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 }
268}
269
270/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000271 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 */
273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100274diff_mark_adjust(
275 linenr_T line1,
276 linenr_T line2,
277 long amount,
278 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000280 int idx;
281 tabpage_T *tp;
282
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100283 // Handle all tab pages that use the current buffer in a diff.
Bram Moolenaar29323592016-07-24 22:04:11 +0200284 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000285 {
286 idx = diff_buf_idx_tp(curbuf, tp);
287 if (idx != DB_COUNT)
288 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
289 }
290}
291
292/*
293 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
294 * This attempts to update the changes as much as possible:
295 * When inserting/deleting lines outside of existing change blocks, create a
296 * new change block and update the line numbers in following blocks.
297 * When inserting/deleting lines in existing change blocks, update them.
298 */
299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100300diff_mark_adjust_tp(
301 tabpage_T *tp,
302 int idx,
303 linenr_T line1,
304 linenr_T line2,
305 long amount,
306 long amount_after)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000307{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 diff_T *dp;
309 diff_T *dprev;
310 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 int i;
312 int inserted, deleted;
313 int n, off;
314 linenr_T last;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100315 linenr_T lnum_deleted = line1; // lnum of remaining deletion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 int check_unchanged;
317
Bram Moolenaare3521d92018-09-16 14:10:31 +0200318 if (diff_internal())
319 {
Bram Moolenaar198fa062018-09-18 21:20:26 +0200320 // Will update diffs before redrawing. Set _invalid to update the
Bram Moolenaare3521d92018-09-16 14:10:31 +0200321 // diffs themselves, set _update to also update folds properly just
322 // before redrawing.
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +0200323 // Do update marks here, it is needed for :%diffput.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200324 tp->tp_diff_invalid = TRUE;
325 tp->tp_diff_update = TRUE;
Bram Moolenaare3521d92018-09-16 14:10:31 +0200326 }
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 if (line2 == MAXLNUM)
329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100330 // mark_adjust(99, MAXLNUM, 9, 0): insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 inserted = amount;
332 deleted = 0;
333 }
334 else if (amount_after > 0)
335 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100336 // mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 inserted = amount_after;
338 deleted = 0;
339 }
340 else
341 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100342 // mark_adjust(98, 99, MAXLNUM, -2): delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 inserted = 0;
344 deleted = -amount_after;
345 }
346
347 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000348 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 for (;;)
350 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100351 // If the change is after the previous diff block and before the next
352 // diff block, thus not touching an existing change, create a new diff
353 // block. Don't do this when ex_diffgetput() is busy.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
355 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
356 && (dprev == NULL
357 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
358 && !diff_busy)
359 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000360 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 if (dnext == NULL)
362 return;
363
364 dnext->df_lnum[idx] = line1;
365 dnext->df_count[idx] = inserted;
366 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000367 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 {
369 if (dprev == NULL)
370 dnext->df_lnum[i] = line1;
371 else
372 dnext->df_lnum[i] = line1
373 + (dprev->df_lnum[i] + dprev->df_count[i])
374 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
375 dnext->df_count[i] = deleted;
376 }
377 }
378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100379 // if at end of the list, quit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (dp == NULL)
381 break;
382
383 /*
384 * Check for these situations:
385 * 1 2 3
386 * 1 2 3
387 * line1 2 3 4 5
388 * 2 3 4 5
389 * 2 3 4 5
390 * line2 2 3 4 5
391 * 3 5 6
392 * 3 5 6
393 */
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100394 // compute last line of this change
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100397 // 1. change completely above line1: nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (last >= line1 - 1)
399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100400 // 6. change below line2: only adjust for amount_after; also when
401 // "deleted" became zero when deleted all lines between two diffs
Jonathon7c7a4e62025-01-12 09:58:00 +0100402 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2 -
403 (dp->is_linematched ? 1 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 {
405 if (amount_after == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100406 break; // nothing left to change
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 dp->df_lnum[idx] += amount_after;
408 }
409 else
410 {
411 check_unchanged = FALSE;
412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100413 // 2. 3. 4. 5.: inserted/deleted lines touching this diff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (deleted > 0)
415 {
Bram Moolenaarc101abf2022-06-26 16:53:34 +0100416 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 if (dp->df_lnum[idx] >= line1)
418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 if (last <= line2)
420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100421 // 4. delete all lines of diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 if (dp->df_next != NULL
423 && dp->df_next->df_lnum[idx] - 1 <= line2)
424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100425 // delete continues in next diff, only do
426 // lines until that one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 n = dp->df_next->df_lnum[idx] - lnum_deleted;
428 deleted -= n;
429 n -= dp->df_count[idx];
430 lnum_deleted = dp->df_next->df_lnum[idx];
431 }
432 else
433 n = deleted - dp->df_count[idx];
434 dp->df_count[idx] = 0;
435 }
436 else
437 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100438 // 5. delete lines at or just before top of diff
Bram Moolenaarc101abf2022-06-26 16:53:34 +0100439 off = dp->df_lnum[idx] - lnum_deleted;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 n = off;
441 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
442 check_unchanged = TRUE;
443 }
444 dp->df_lnum[idx] = line1;
445 }
446 else
447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (last < line2)
449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100450 // 2. delete at end of diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 dp->df_count[idx] -= last - lnum_deleted + 1;
452 if (dp->df_next != NULL
453 && dp->df_next->df_lnum[idx] - 1 <= line2)
454 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100455 // delete continues in next diff, only do
456 // lines until that one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 n = dp->df_next->df_lnum[idx] - 1 - last;
458 deleted -= dp->df_next->df_lnum[idx]
459 - lnum_deleted;
460 lnum_deleted = dp->df_next->df_lnum[idx];
461 }
462 else
463 n = line2 - last;
464 check_unchanged = TRUE;
465 }
466 else
467 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100468 // 3. delete lines inside the diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 n = 0;
470 dp->df_count[idx] -= deleted;
471 }
472 }
473
474 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000475 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {
Bram Moolenaar4e677b92022-07-28 18:44:27 +0100477 if (dp->df_lnum[i] > off)
478 dp->df_lnum[i] -= off;
479 else
480 dp->df_lnum[i] = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 dp->df_count[i] += n;
482 }
483 }
484 else
485 {
486 if (dp->df_lnum[idx] <= line1)
487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100488 // inserted lines somewhere in this diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 dp->df_count[idx] += inserted;
490 check_unchanged = TRUE;
491 }
492 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100493 // inserted lines somewhere above this diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 dp->df_lnum[idx] += inserted;
495 }
496
497 if (check_unchanged)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100498 // Check if inserted lines are equal, may reduce the
499 // size of the diff. TODO: also check for equal lines
500 // in the middle and perhaps split the block.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000501 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 }
504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100505 // check if this block touches the previous one, may merge them.
Jonathon7c7a4e62025-01-12 09:58:00 +0100506 if (dprev != NULL && !dp->is_linematched
507 && dprev->df_lnum[idx] + dprev->df_count[idx]
508 == dp->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {
510 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000511 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 dprev->df_count[i] += dp->df_count[i];
513 dprev->df_next = dp->df_next;
514 vim_free(dp);
515 dp = dprev->df_next;
516 }
517 else
518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100519 // Advance to next entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 dprev = dp;
521 dp = dp->df_next;
522 }
523 }
524
525 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000526 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 while (dp != NULL)
528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100529 // All counts are zero, remove this entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000531 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 break;
533 if (i == DB_COUNT)
534 {
535 dnext = dp->df_next;
536 vim_free(dp);
537 dp = dnext;
538 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000539 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 else
541 dprev->df_next = dnext;
542 }
543 else
544 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100545 // Advance to next entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 dprev = dp;
547 dp = dp->df_next;
548 }
549
550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000552 if (tp == curtab)
553 {
Bram Moolenaar4f57eef2019-08-24 20:54:19 +0200554 // Don't redraw right away, this updates the diffs, which can be slow.
555 need_diff_redraw = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100557 // Need to recompute the scroll binding, may remove or add filler
558 // lines (e.g., when adding lines above w_topline). But it's slow when
559 // making many changes, postpone until redrawing.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000560 diff_need_scrollbind = TRUE;
561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562}
563
564/*
565 * Allocate a new diff block and link it between "dprev" and "dp".
566 */
567 static diff_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100568diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570 diff_T *dnew;
571
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200572 dnew = ALLOC_ONE(diff_T);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000573 if (dnew == NULL)
574 return NULL;
575
Jonathon7c7a4e62025-01-12 09:58:00 +0100576 dnew->is_linematched = FALSE;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000577 dnew->df_next = dp;
578 if (dprev == NULL)
579 tp->tp_first_diff = dnew;
580 else
581 dprev->df_next = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 return dnew;
583}
584
585/*
586 * Check if the diff block "dp" can be made smaller for lines at the start and
587 * end that are equal. Called after inserting lines.
588 * This may result in a change where all buffers have zero lines, the caller
589 * must take care of removing it.
590 */
591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100592diff_check_unchanged(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 int i_org;
595 int i_new;
596 int off_org, off_new;
597 char_u *line_org;
598 int dir = FORWARD;
599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100600 // Find the first buffers, use it as the original, compare the other
601 // buffer lines against this one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000603 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100605 if (i_org == DB_COUNT) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 return;
607
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000608 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 return;
610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100611 // First check lines at the top, then at the bottom.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 off_org = 0;
613 off_new = 0;
614 for (;;)
615 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100616 // Repeat until a line is found which is different or the number of
617 // lines has become zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 while (dp->df_count[i_org] > 0)
619 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100620 // Copy the line, the next ml_get() will invalidate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 if (dir == BACKWARD)
622 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000623 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 dp->df_lnum[i_org] + off_org, FALSE));
625 if (line_org == NULL)
626 return;
627 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
628 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000629 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 continue;
631 if (dir == BACKWARD)
632 off_new = dp->df_count[i_new] - 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100633 // if other buffer doesn't have this line, it was inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (off_new < 0 || off_new >= dp->df_count[i_new])
635 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000636 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
638 break;
639 }
640 vim_free(line_org);
641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100642 // Stop when a line isn't equal in all diff buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 if (i_new != DB_COUNT)
644 break;
645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100646 // Line matched in all buffers, remove it from the diff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000648 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
650 if (dir == FORWARD)
651 ++dp->df_lnum[i_new];
652 --dp->df_count[i_new];
653 }
654 }
655 if (dir == BACKWARD)
656 break;
657 dir = BACKWARD;
658 }
659}
660
661/*
662 * Check if a diff block doesn't contain invalid line numbers.
663 * This can happen when the diff program returns invalid results.
664 */
665 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100666diff_check_sanity(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 int i;
669
670 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000671 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000673 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 return FAIL;
675 return OK;
676}
677
678/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000679 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 */
Bram Moolenaar4f57eef2019-08-24 20:54:19 +0200681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100682diff_redraw(
Bram Moolenaare3521d92018-09-16 14:10:31 +0200683 int dofold) // also recompute the folds
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 win_T *wp;
Bram Moolenaar04626c22021-09-01 16:02:07 +0200686 win_T *wp_other = NULL;
Bram Moolenaar841c2252021-10-22 20:56:55 +0100687 int used_max_fill_other = FALSE;
688 int used_max_fill_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 int n;
690
Bram Moolenaar4f57eef2019-08-24 20:54:19 +0200691 need_diff_redraw = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +0200692 FOR_ALL_WINDOWS(wp)
zeertzjq101d57b2022-07-31 18:34:32 +0100693 {
Bram Moolenaarcd38bb42022-06-26 14:04:07 +0100694 // when closing windows or wiping buffers skip invalid window
zeertzjq101d57b2022-07-31 18:34:32 +0100695 if (!wp->w_p_diff || !buf_valid(wp->w_buffer))
696 continue;
697
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100698 redraw_win_later(wp, UPD_SOME_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +0100699 if (wp != curwin)
700 wp_other = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701#ifdef FEAT_FOLDING
zeertzjq101d57b2022-07-31 18:34:32 +0100702 if (dofold && foldmethodIsDiff(wp))
703 foldUpdateAll(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
zeertzjq101d57b2022-07-31 18:34:32 +0100705 // A change may have made filler lines invalid, need to take care of
706 // that for other windows.
707 n = diff_check(wp, wp->w_topline);
708 if ((wp != curwin && wp->w_topfill > 0) || n > 0)
709 {
710 if (wp->w_topfill > n)
711 wp->w_topfill = (n < 0 ? 0 : n);
712 else if (n > 0 && n > wp->w_topfill)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
zeertzjq101d57b2022-07-31 18:34:32 +0100714 wp->w_topfill = n;
715 if (wp == curwin)
716 used_max_fill_curwin = TRUE;
717 else if (wp_other != NULL)
718 used_max_fill_other = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
zeertzjq101d57b2022-07-31 18:34:32 +0100720 check_topfill(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
zeertzjq101d57b2022-07-31 18:34:32 +0100722 }
Bram Moolenaar04626c22021-09-01 16:02:07 +0200723
Bram Moolenaar841c2252021-10-22 20:56:55 +0100724 if (wp_other != NULL && curwin->w_p_scb)
725 {
726 if (used_max_fill_curwin)
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000727 // The current window was set to use the maximum number of filler
Bram Moolenaar841c2252021-10-22 20:56:55 +0100728 // lines, may need to reduce them.
729 diff_set_topline(wp_other, curwin);
730 else if (used_max_fill_other)
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000731 // The other window was set to use the maximum number of filler
Bram Moolenaar841c2252021-10-22 20:56:55 +0100732 // lines, may need to reduce them.
733 diff_set_topline(curwin, wp_other);
734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735}
736
Bram Moolenaare828b762018-09-10 17:51:58 +0200737 static void
738clear_diffin(diffin_T *din)
739{
740 if (din->din_fname == NULL)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +0000741 VIM_CLEAR(din->din_mmfile.ptr);
Bram Moolenaare828b762018-09-10 17:51:58 +0200742 else
743 mch_remove(din->din_fname);
744}
745
746 static void
747clear_diffout(diffout_T *dout)
748{
749 if (dout->dout_fname == NULL)
750 ga_clear_strings(&dout->dout_ga);
751 else
752 mch_remove(dout->dout_fname);
753}
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200756 * Write buffer "buf" to a memory buffer.
757 * Return FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 */
759 static int
Jonathon7c7a4e62025-01-12 09:58:00 +0100760diff_write_buffer(buf_T *buf, diffin_T *din, linenr_T start, linenr_T end)
Bram Moolenaare828b762018-09-10 17:51:58 +0200761{
762 linenr_T lnum;
763 char_u *s;
764 long len = 0;
765 char_u *ptr;
766
Jonathon7c7a4e62025-01-12 09:58:00 +0100767 if (end < 0)
768 end = buf->b_ml.ml_line_count;
769
Yukihiro Nakadairaf1694b42024-09-22 11:26:13 +0200770 if (buf->b_ml.ml_flags & ML_EMPTY)
771 {
772 din->din_mmfile.ptr = NULL;
773 din->din_mmfile.size = 0;
774 return OK;
775 }
776
Bram Moolenaare828b762018-09-10 17:51:58 +0200777 // xdiff requires one big block of memory with all the text.
Jonathon7c7a4e62025-01-12 09:58:00 +0100778 for (lnum = start; lnum <= end; ++lnum)
zeertzjq94b7c322024-03-12 21:50:32 +0100779 len += ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200780 ptr = alloc(len);
Bram Moolenaare828b762018-09-10 17:51:58 +0200781 if (ptr == NULL)
782 {
783 // Allocating memory failed. This can happen, because we try to read
784 // the whole buffer text into memory. Set the failed flag, the diff
785 // will be retried with external diff. The flag is never reset.
786 buf->b_diff_failed = TRUE;
787 if (p_verbose > 0)
788 {
789 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100790 smsg(_("Not enough memory to use internal diff for buffer \"%s\""),
Bram Moolenaare828b762018-09-10 17:51:58 +0200791 buf->b_fname);
792 verbose_leave();
793 }
794 return FAIL;
795 }
796 din->din_mmfile.ptr = (char *)ptr;
797 din->din_mmfile.size = len;
798
799 len = 0;
Jonathon7c7a4e62025-01-12 09:58:00 +0100800 for (lnum = start; lnum <= end; ++lnum)
Bram Moolenaare828b762018-09-10 17:51:58 +0200801 {
802 for (s = ml_get_buf(buf, lnum, FALSE); *s != NUL; )
803 {
804 if (diff_flags & DIFF_ICASE)
805 {
806 int c;
Bram Moolenaare828b762018-09-10 17:51:58 +0200807 int orig_len;
808 char_u cbuf[MB_MAXBYTES + 1];
809
Bram Moolenaar06f60952021-12-28 18:30:05 +0000810 if (*s == NL)
811 c = NUL;
812 else
813 {
814 // xdiff doesn't support ignoring case, fold-case the text.
815 c = PTR2CHAR(s);
816 c = MB_CASEFOLD(c);
817 }
Bram Moolenaar1614a142019-10-06 22:00:13 +0200818 orig_len = mb_ptr2len(s);
Bram Moolenaare828b762018-09-10 17:51:58 +0200819 if (mb_char2bytes(c, cbuf) != orig_len)
820 // TODO: handle byte length difference
821 mch_memmove(ptr + len, s, orig_len);
822 else
823 mch_memmove(ptr + len, cbuf, orig_len);
824
825 s += orig_len;
826 len += orig_len;
Bram Moolenaare828b762018-09-10 17:51:58 +0200827 }
828 else
Bram Moolenaar06f60952021-12-28 18:30:05 +0000829 {
830 ptr[len++] = *s == NL ? NUL : *s;
831 s++;
832 }
Bram Moolenaare828b762018-09-10 17:51:58 +0200833 }
834 ptr[len++] = NL;
835 }
836 return OK;
837}
838
839/*
840 * Write buffer "buf" to file or memory buffer.
841 * Return FAIL for failure.
842 */
843 static int
844diff_write(buf_T *buf, diffin_T *din)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 int r;
847 char_u *save_ff;
Bram Moolenaare1004402020-10-24 20:49:43 +0200848 int save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaare828b762018-09-10 17:51:58 +0200850 if (din->din_fname == NULL)
Jonathon7c7a4e62025-01-12 09:58:00 +0100851 return diff_write_buffer(buf, din, 1, -1);
Bram Moolenaare828b762018-09-10 17:51:58 +0200852
853 // Always use 'fileformat' set to "unix".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 save_ff = buf->b_p_ff;
855 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
Bram Moolenaare1004402020-10-24 20:49:43 +0200856 save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100857 // Writing the buffer is an implementation detail of performing the diff,
858 // so it shouldn't update the '[ and '] marks.
Bram Moolenaare1004402020-10-24 20:49:43 +0200859 cmdmod.cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaare828b762018-09-10 17:51:58 +0200860 r = buf_write(buf, din->din_fname, NULL,
861 (linenr_T)1, buf->b_ml.ml_line_count,
862 NULL, FALSE, FALSE, FALSE, TRUE);
Bram Moolenaare1004402020-10-24 20:49:43 +0200863 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 free_string_option(buf->b_p_ff);
865 buf->b_p_ff = save_ff;
866 return r;
867}
868
869/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200870 * Update the diffs for all buffers involved.
871 */
872 static void
873diff_try_update(
874 diffio_T *dio,
875 int idx_orig,
876 exarg_T *eap) // "eap" can be NULL
877{
878 buf_T *buf;
879 int idx_new;
880
881 if (dio->dio_internal)
882 {
883 ga_init2(&dio->dio_diff.dout_ga, sizeof(char *), 1000);
884 }
885 else
886 {
887 // We need three temp file names.
888 dio->dio_orig.din_fname = vim_tempname('o', TRUE);
889 dio->dio_new.din_fname = vim_tempname('n', TRUE);
890 dio->dio_diff.dout_fname = vim_tempname('d', TRUE);
891 if (dio->dio_orig.din_fname == NULL
892 || dio->dio_new.din_fname == NULL
893 || dio->dio_diff.dout_fname == NULL)
894 goto theend;
895 }
896
897 // Check external diff is actually working.
898 if (!dio->dio_internal && check_external_diff(dio) == FAIL)
899 goto theend;
900
901 // :diffupdate!
902 if (eap != NULL && eap->forceit)
903 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
904 {
905 buf = curtab->tp_diffbuf[idx_new];
906 if (buf_valid(buf))
907 buf_check_timestamp(buf, FALSE);
908 }
909
910 // Write the first buffer to a tempfile or mmfile_t.
911 buf = curtab->tp_diffbuf[idx_orig];
912 if (diff_write(buf, &dio->dio_orig) == FAIL)
913 goto theend;
914
915 // Make a difference between the first buffer and every other.
916 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
917 {
918 buf = curtab->tp_diffbuf[idx_new];
919 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
920 continue; // skip buffer that isn't loaded
921
922 // Write the other buffer and diff with the first one.
923 if (diff_write(buf, &dio->dio_new) == FAIL)
924 continue;
925 if (diff_file(dio) == FAIL)
926 continue;
927
928 // Read the diff output and add each entry to the diff list.
Lewis Russelld9da86e2021-12-28 13:54:41 +0000929 diff_read(idx_orig, idx_new, dio);
Bram Moolenaare828b762018-09-10 17:51:58 +0200930
931 clear_diffin(&dio->dio_new);
932 clear_diffout(&dio->dio_diff);
933 }
934 clear_diffin(&dio->dio_orig);
935
936theend:
937 vim_free(dio->dio_orig.din_fname);
938 vim_free(dio->dio_new.din_fname);
939 vim_free(dio->dio_diff.dout_fname);
940}
941
942/*
943 * Return TRUE if the options are set to use the internal diff library.
944 * Note that if the internal diff failed for one of the buffers, the external
945 * diff will be used anyway.
946 */
Bram Moolenaare3521d92018-09-16 14:10:31 +0200947 int
Bram Moolenaare828b762018-09-10 17:51:58 +0200948diff_internal(void)
949{
Bram Moolenaar975880b2019-03-03 14:42:11 +0100950 return (diff_flags & DIFF_INTERNAL) != 0
951#ifdef FEAT_EVAL
952 && *p_dex == NUL
953#endif
954 ;
Bram Moolenaare828b762018-09-10 17:51:58 +0200955}
956
957/*
958 * Return TRUE if the internal diff failed for one of the diff buffers.
959 */
960 static int
961diff_internal_failed(void)
962{
963 int idx;
964
965 // Only need to do something when there is another buffer.
966 for (idx = 0; idx < DB_COUNT; ++idx)
967 if (curtab->tp_diffbuf[idx] != NULL
968 && curtab->tp_diffbuf[idx]->b_diff_failed)
969 return TRUE;
970 return FALSE;
971}
972
973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 * Completely update the diffs for the buffers involved.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200975 * When using the external "diff" command the buffers are written to a file,
976 * also for unmodified buffers (the file could have been produced by
977 * autocommands, e.g. the netrw plugin).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 void
Bram Moolenaare828b762018-09-10 17:51:58 +0200980ex_diffupdate(exarg_T *eap) // "eap" can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 int idx_orig;
983 int idx_new;
Bram Moolenaare828b762018-09-10 17:51:58 +0200984 diffio_T diffio;
Bram Moolenaar198fa062018-09-18 21:20:26 +0200985 int had_diffs = curtab->tp_first_diff != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaard2b58c02018-09-16 18:10:48 +0200987 if (diff_busy)
988 {
989 diff_need_update = TRUE;
990 return;
991 }
992
Bram Moolenaare828b762018-09-10 17:51:58 +0200993 // Delete all diffblocks.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000994 diff_clear(curtab);
995 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
Bram Moolenaare828b762018-09-10 17:51:58 +0200997 // Use the first buffer as the original text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000999 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001 if (idx_orig == DB_COUNT)
Bram Moolenaar198fa062018-09-18 21:20:26 +02001002 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaare828b762018-09-10 17:51:58 +02001004 // Only need to do something when there is another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001006 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 break;
1008 if (idx_new == DB_COUNT)
Bram Moolenaar198fa062018-09-18 21:20:26 +02001009 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaare828b762018-09-10 17:51:58 +02001011 // Only use the internal method if it did not fail for one of the buffers.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001012 CLEAR_FIELD(diffio);
Bram Moolenaare828b762018-09-10 17:51:58 +02001013 diffio.dio_internal = diff_internal() && !diff_internal_failed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaare828b762018-09-10 17:51:58 +02001015 diff_try_update(&diffio, idx_orig, eap);
1016 if (diffio.dio_internal && diff_internal_failed())
1017 {
1018 // Internal diff failed, use external diff instead.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001019 CLEAR_FIELD(diffio);
Bram Moolenaare828b762018-09-10 17:51:58 +02001020 diff_try_update(&diffio, idx_orig, eap);
1021 }
1022
1023 // force updating cursor position on screen
1024 curwin->w_valid_cursor.lnum = 0;
1025
Bram Moolenaar198fa062018-09-18 21:20:26 +02001026theend:
1027 // A redraw is needed if there were diffs and they were cleared, or there
1028 // are diffs now, which means they got updated.
1029 if (had_diffs || curtab->tp_first_diff != NULL)
1030 {
1031 diff_redraw(TRUE);
1032 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
1033 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001034}
1035
1036/*
1037 * Do a quick test if "diff" really works. Otherwise it looks like there
1038 * are no differences. Can't use the return value, it's non-zero when
1039 * there are differences.
1040 */
1041 static int
1042check_external_diff(diffio_T *diffio)
1043{
1044 FILE *fd;
1045 int ok;
1046 int io_error = FALSE;
1047
1048 // May try twice, first with "-a" and then without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 for (;;)
1050 {
1051 ok = FALSE;
Bram Moolenaare828b762018-09-10 17:51:58 +02001052 fd = mch_fopen((char *)diffio->dio_orig.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001053 if (fd == NULL)
1054 io_error = TRUE;
1055 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001057 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
1058 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +02001060 fd = mch_fopen((char *)diffio->dio_new.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001061 if (fd == NULL)
1062 io_error = TRUE;
1063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001065 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
1066 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +02001068 fd = NULL;
1069 if (diff_file(diffio) == OK)
1070 fd = mch_fopen((char *)diffio->dio_diff.dout_fname, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001071 if (fd == NULL)
1072 io_error = TRUE;
1073 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 char_u linebuf[LBUFLEN];
1076
1077 for (;;)
1078 {
glacambread5c1782021-05-24 14:20:53 +02001079 // For normal diff there must be a line that contains
1080 // "1c1". For unified diff "@@ -1 +1 @@".
Bram Moolenaar00590742019-02-15 21:06:09 +01001081 if (vim_fgets(linebuf, LBUFLEN, fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 break;
glacambread5c1782021-05-24 14:20:53 +02001083 if (STRNCMP(linebuf, "1c1", 3) == 0
1084 || STRNCMP(linebuf, "@@ -1 +1 @@", 11) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ok = TRUE;
1086 }
1087 fclose(fd);
1088 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001089 mch_remove(diffio->dio_diff.dout_fname);
1090 mch_remove(diffio->dio_new.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001092 mch_remove(diffio->dio_orig.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 }
1094
1095#ifdef FEAT_EVAL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001096 // When using 'diffexpr' break here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (*p_dex != NUL)
1098 break;
1099#endif
1100
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001101#if defined(MSWIN)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001102 // If the "-a" argument works, also check if "--binary" works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
1104 {
1105 diff_a_works = TRUE;
1106 diff_bin_works = TRUE;
1107 continue;
1108 }
1109 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
1110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001111 // Tried --binary, but it failed. "-a" works though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 diff_bin_works = FALSE;
1113 ok = TRUE;
1114 }
1115#endif
1116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 // If we checked if "-a" works already, break here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (diff_a_works != MAYBE)
1119 break;
1120 diff_a_works = ok;
1121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001122 // If "-a" works break here, otherwise retry without "-a".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (ok)
1124 break;
1125 }
1126 if (!ok)
1127 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001128 if (io_error)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001129 emsg(_(e_cannot_read_or_write_temp_files));
Bram Moolenaare1242042021-12-16 20:56:57 +00001130 emsg(_(e_cannot_create_diffs));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001132#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 diff_bin_works = MAYBE;
1134#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001137 return OK;
1138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
Bram Moolenaare828b762018-09-10 17:51:58 +02001140/*
1141 * Invoke the xdiff function.
1142 */
1143 static int
1144diff_file_internal(diffio_T *diffio)
1145{
1146 xpparam_t param;
1147 xdemitconf_t emit_cfg;
1148 xdemitcb_t emit_cb;
Bram Moolenaarbd1d5602012-05-18 18:47:17 +02001149
Bram Moolenaara80faa82020-04-12 19:37:17 +02001150 CLEAR_FIELD(param);
1151 CLEAR_FIELD(emit_cfg);
1152 CLEAR_FIELD(emit_cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
Bram Moolenaare828b762018-09-10 17:51:58 +02001154 param.flags = diff_algorithm;
1155
1156 if (diff_flags & DIFF_IWHITE)
1157 param.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001158 if (diff_flags & DIFF_IWHITEALL)
1159 param.flags |= XDF_IGNORE_WHITESPACE;
1160 if (diff_flags & DIFF_IWHITEEOL)
1161 param.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
1162 if (diff_flags & DIFF_IBLANK)
1163 param.flags |= XDF_IGNORE_BLANK_LINES;
Bram Moolenaare828b762018-09-10 17:51:58 +02001164
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01001165 emit_cfg.ctxlen = diffio->dio_ctxlen;
Bram Moolenaare828b762018-09-10 17:51:58 +02001166 emit_cb.priv = &diffio->dio_diff;
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01001167 if (diffio->dio_outfmt == DIO_OUTPUT_INDICES)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001168 emit_cfg.hunk_func = xdiff_out_indices;
1169 else
1170 emit_cb.out_line = xdiff_out_unified;
Bram Moolenaare828b762018-09-10 17:51:58 +02001171 if (xdl_diff(&diffio->dio_orig.din_mmfile,
1172 &diffio->dio_new.din_mmfile,
1173 &param, &emit_cfg, &emit_cb) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001175 emsg(_(e_problem_creating_internal_diff));
Bram Moolenaare828b762018-09-10 17:51:58 +02001176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001178 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
1180
1181/*
1182 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
Bram Moolenaare828b762018-09-10 17:51:58 +02001183 * return OK or FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 */
Bram Moolenaare828b762018-09-10 17:51:58 +02001185 static int
1186diff_file(diffio_T *dio)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001189 size_t len;
Bram Moolenaare828b762018-09-10 17:51:58 +02001190 char_u *tmp_orig = dio->dio_orig.din_fname;
1191 char_u *tmp_new = dio->dio_new.din_fname;
1192 char_u *tmp_diff = dio->dio_diff.dout_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
1194#ifdef FEAT_EVAL
1195 if (*p_dex != NUL)
Bram Moolenaare828b762018-09-10 17:51:58 +02001196 {
1197 // Use 'diffexpr' to generate the diff file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 eval_diff(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaare828b762018-09-10 17:51:58 +02001199 return OK;
1200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else
1202#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001203 // Use xdiff for generating the diff.
1204 if (dio->dio_internal)
Bram Moolenaare828b762018-09-10 17:51:58 +02001205 return diff_file_internal(dio);
Bram Moolenaar95fb60a2005-03-08 22:29:20 +00001206
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001207 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
1208 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
1209 cmd = alloc(len);
1210 if (cmd == NULL)
1211 return FAIL;
Bram Moolenaare828b762018-09-10 17:51:58 +02001212
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001213 // We don't want $DIFF_OPTIONS to get in the way.
1214 if (getenv("DIFF_OPTIONS"))
1215 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
1216
1217 // Build the diff command and execute it. Always use -a, binary
1218 // differences are of no use. Ignore errors, diff returns
1219 // non-zero when differences have been found.
1220 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s%s%s%s %s",
1221 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001222#if defined(MSWIN)
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001223 diff_bin_works == TRUE ? "--binary " : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224#else
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001225 "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226#endif
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001227 (diff_flags & DIFF_IWHITE) ? "-b " : "",
1228 (diff_flags & DIFF_IWHITEALL) ? "-w " : "",
1229 (diff_flags & DIFF_IWHITEEOL) ? "-Z " : "",
1230 (diff_flags & DIFF_IBLANK) ? "-B " : "",
1231 (diff_flags & DIFF_ICASE) ? "-i " : "",
1232 tmp_orig, tmp_new);
1233 append_redir(cmd, (int)len, p_srr, tmp_diff);
1234 block_autocmds(); // avoid ShellCmdPost stuff
1235 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
1236 unblock_autocmds();
1237 vim_free(cmd);
1238 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239}
1240
1241/*
1242 * Create a new version of a file from the current buffer and a diff file.
1243 * The buffer is written to a file, also for unmodified buffers (the file
1244 * could have been produced by autocommands, e.g. the netrw plugin).
1245 */
1246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001247ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001249 char_u *tmp_orig; // name of original temp file
1250 char_u *tmp_new; // name of patched temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001252 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 win_T *old_curwin = curwin;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 char_u *newname = NULL; // name of patched file buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255#ifdef UNIX
1256 char_u dirbuf[MAXPATHL];
1257 char_u *fullname = NULL;
1258#endif
1259#ifdef FEAT_BROWSE
1260 char_u *browseFile = NULL;
Bram Moolenaare1004402020-10-24 20:49:43 +02001261 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02001263 stat_T st;
Bram Moolenaara95ab322017-03-11 19:21:53 +01001264 char_u *esc_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02001267 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001269 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02001270 eap->arg, NULL, NULL,
1271 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (browseFile == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001273 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 eap->arg = browseFile;
Bram Moolenaare1004402020-10-24 20:49:43 +02001275 cmdmod.cmod_flags &= ~CMOD_BROWSE; // don't let do_ecmd() browse again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277#endif
1278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001279 // We need two temp file names.
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001280 tmp_orig = vim_tempname('o', FALSE);
1281 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (tmp_orig == NULL || tmp_new == NULL)
1283 goto theend;
1284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001285 // Write the current buffer to "tmp_orig".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (buf_write(curbuf, tmp_orig, NULL,
1287 (linenr_T)1, curbuf->b_ml.ml_line_count,
1288 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
1289 goto theend;
1290
1291#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001292 // Get the absolute path of the patchfile, changing directory below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 fullname = FullName_save(eap->arg, FALSE);
1294#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001295 esc_name = vim_strsave_shellescape(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296# ifdef UNIX
Bram Moolenaara95ab322017-03-11 19:21:53 +01001297 fullname != NULL ? fullname :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298# endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001299 eap->arg, TRUE, TRUE);
1300 if (esc_name == NULL)
1301 goto theend;
1302 buflen = STRLEN(tmp_orig) + STRLEN(esc_name) + STRLEN(tmp_new) + 16;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001303 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (buf == NULL)
1305 goto theend;
1306
1307#ifdef UNIX
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // Temporarily chdir to /tmp, to avoid patching files in the current
1309 // directory when the patch file contains more than one patch. When we
1310 // have our own temp dir use that instead, it will be cleaned up when we
1311 // exit (any .rej files created). Don't change directory if we can't
1312 // return to the current.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
1314 dirbuf[0] = NUL;
1315 else
1316 {
1317# ifdef TEMPDIRNAMES
1318 if (vim_tempdir != NULL)
Bram Moolenaar42335f52018-09-13 15:33:43 +02001319 vim_ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 else
1321# endif
Bram Moolenaar42335f52018-09-13 15:33:43 +02001322 vim_ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 shorten_fnames(TRUE);
1324 }
1325#endif
1326
1327#ifdef FEAT_EVAL
1328 if (*p_pex != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001329 // Use 'patchexpr' to generate the new file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 eval_patch(tmp_orig,
1331# ifdef UNIX
1332 fullname != NULL ? fullname :
1333# endif
1334 eap->arg, tmp_new);
1335 else
1336#endif
1337 {
Bram Moolenaar23a971d2023-04-04 22:04:53 +01001338 if (check_restricted())
1339 goto theend;
1340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001341 // Build the patch command and execute it. Ignore errors. Switch to
1342 // cooked mode to allow the user to respond to prompts.
Bram Moolenaara95ab322017-03-11 19:21:53 +01001343 vim_snprintf((char *)buf, buflen, "patch -o %s %s < %s",
1344 tmp_new, tmp_orig, esc_name);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001345 block_autocmds(); // Avoid ShellCmdPost stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001347 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349
1350#ifdef UNIX
1351 if (dirbuf[0] != NUL)
1352 {
1353 if (mch_chdir((char *)dirbuf) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001354 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 shorten_fnames(TRUE);
1356 }
1357#endif
1358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001359 // patch probably has written over the screen
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001360 redraw_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001362 // Delete any .orig or .rej file created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 STRCPY(buf, tmp_new);
1364 STRCAT(buf, ".orig");
1365 mch_remove(buf);
1366 STRCPY(buf, tmp_new);
1367 STRCAT(buf, ".rej");
1368 mch_remove(buf);
1369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001370 // Only continue if the output file was created.
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001371 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001372 emsg(_(e_cannot_read_patch_output));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001375 if (curbuf->b_fname != NULL)
1376 {
1377 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaardf44a272020-06-07 20:49:05 +02001378 STRLEN(curbuf->b_fname) + 4);
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001379 if (newname != NULL)
1380 STRCAT(newname, ".new");
1381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
1383#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001384 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001386 // don't use a new tab page, each tab page has its own diffs
Bram Moolenaare1004402020-10-24 20:49:43 +02001387 cmdmod.cmod_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001388
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001389 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001391 // Pretend it was a ":split fname" command
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001392 eap->cmdidx = CMD_split;
1393 eap->arg = tmp_new;
1394 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001396 // check that split worked and editing tmp_new
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001397 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001399 // Set 'diff', 'scrollbind' on and 'wrap' off.
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001400 diff_win_options(curwin, TRUE);
1401 diff_win_options(old_curwin, TRUE);
1402
1403 if (newname != NULL)
1404 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001405 // do a ":file filename.new" on the patched buffer
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001406 eap->arg = newname;
1407 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001409 // Do filetype detection with the new name.
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001410 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar23a971d2023-04-04 22:04:53 +01001411 do_cmdline_cmd(
1412 (char_u *)":doau filetypedetect BufRead");
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 }
1416 }
1417
1418theend:
1419 if (tmp_orig != NULL)
1420 mch_remove(tmp_orig);
1421 vim_free(tmp_orig);
1422 if (tmp_new != NULL)
1423 mch_remove(tmp_new);
1424 vim_free(tmp_new);
1425 vim_free(newname);
1426 vim_free(buf);
1427#ifdef UNIX
1428 vim_free(fullname);
1429#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001430 vim_free(esc_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431#ifdef FEAT_BROWSE
1432 vim_free(browseFile);
Bram Moolenaare1004402020-10-24 20:49:43 +02001433 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#endif
1435}
1436
1437/*
1438 * Split the window and edit another file, setting options to show the diffs.
1439 */
1440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001441ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001444 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001446 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447#ifdef FEAT_GUI
1448 need_mouse_correct = TRUE;
1449#endif
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001450 // Need to compute w_fraction when no redraw happened yet.
Bram Moolenaar46328f92016-08-28 15:39:57 +02001451 validate_cursor();
1452 set_fraction(curwin);
1453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001454 // don't use a new tab page, each tab page has its own diffs
Bram Moolenaare1004402020-10-24 20:49:43 +02001455 cmdmod.cmod_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001456
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001457 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) == FAIL)
1458 return;
1459
1460 // Pretend it was a ":split fname" command
1461 eap->cmdidx = CMD_split;
1462 curwin->w_p_diff = TRUE;
1463 do_exedit(eap, old_curwin);
1464
1465 if (curwin == old_curwin) // split didn't work
1466 return;
1467
1468 // Set 'diff', 'scrollbind' on and 'wrap' off.
1469 diff_win_options(curwin, TRUE);
1470 if (win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001472 diff_win_options(old_curwin, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001474 if (bufref_valid(&old_curbuf))
1475 // Move the cursor position to that of the old window.
1476 curwin->w_cursor.lnum = diff_get_corresponding_line(
1477 old_curbuf.br_buf, old_curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +00001479 // Now that lines are folded scroll to show the cursor at the same
1480 // relative position.
1481 scroll_to_fraction(curwin, curwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482}
1483
1484/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001485 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001488ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001490 // Set 'diff', 'scrollbind' on and 'wrap' off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 diff_win_options(curwin, TRUE);
1492}
1493
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001494 static void
1495set_diff_option(win_T *wp, int value)
1496{
1497 win_T *old_curwin = curwin;
1498
1499 curwin = wp;
1500 curbuf = curwin->w_buffer;
1501 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001502 set_option_value_give_err((char_u *)"diff", (long)value, NULL, OPT_LOCAL);
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001503 --curbuf_lock;
1504 curwin = old_curwin;
1505 curbuf = curwin->w_buffer;
1506}
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508/*
1509 * Set options in window "wp" for diff mode.
1510 */
1511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512diff_win_options(
1513 win_T *wp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001514 int addbuf) // Add buffer to diff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001516# ifdef FEAT_FOLDING
1517 win_T *old_curwin = curwin;
1518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001519 // close the manually opened folds
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001520 curwin = wp;
1521 newFoldLevel();
1522 curwin = old_curwin;
1523# endif
1524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001525 // Use 'scrollbind' and 'cursorbind' when available
Bram Moolenaar43929962015-07-03 15:06:56 +02001526 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001527 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001528 wp->w_p_scb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001529 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001530 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001531 wp->w_p_crb = TRUE;
Bram Moolenaar4223d432021-02-10 13:18:17 +01001532 if (!(diff_flags & DIFF_FOLLOWWRAP))
1533 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00001534 if (!wp->w_p_diff)
Bram Moolenaar4223d432021-02-10 13:18:17 +01001535 wp->w_p_wrap_save = wp->w_p_wrap;
Lewis Russelld9da86e2021-12-28 13:54:41 +00001536 wp->w_p_wrap = FALSE;
zeertzjq9e7f1fc2024-03-16 09:40:22 +01001537 wp->w_skipcol = 0;
Bram Moolenaar4223d432021-02-10 13:18:17 +01001538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539# ifdef FEAT_FOLDING
Bram Moolenaar43929962015-07-03 15:06:56 +02001540 if (!wp->w_p_diff)
1541 {
1542 if (wp->w_p_diff_saved)
1543 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001544 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001545 }
Bram Moolenaar20c023a2019-05-26 21:03:24 +02001546 set_string_option_direct_in_win(wp, (char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001547 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaar43929962015-07-03 15:06:56 +02001548 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001549 {
1550 wp->w_p_fdc_save = wp->w_p_fdc;
1551 wp->w_p_fen_save = wp->w_p_fen;
1552 wp->w_p_fdl_save = wp->w_p_fdl;
1553 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001554 wp->w_p_fdc = diff_foldcolumn;
1555 wp->w_p_fen = TRUE;
1556 wp->w_p_fdl = 0;
1557 foldUpdateAll(wp);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001558 // make sure topline is not halfway a fold
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001559 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (vim_strchr(p_sbo, 'h') == NULL)
1562 do_cmdline_cmd((char_u *)"set sbo+=hor");
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001563 // Save the current values, to be restored in ex_diffoff().
Bram Moolenaara87aa802013-07-03 15:47:03 +02001564 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001566 set_diff_option(wp, TRUE);
Bram Moolenaar43929962015-07-03 15:06:56 +02001567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (addbuf)
1569 diff_buf_add(wp->w_buffer);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001570 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571}
1572
1573/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001574 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001575 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001576 */
1577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001579{
1580 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001581 int diffwin = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001582
Bram Moolenaar29323592016-07-24 22:04:11 +02001583 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001584 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001585 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001586 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001587 // Set 'diff' off. If option values were saved in
1588 // diff_win_options(), restore the ones whose settings seem to have
1589 // been left over from diff mode.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001590 set_diff_option(wp, FALSE);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001591
Bram Moolenaara87aa802013-07-03 15:47:03 +02001592 if (wp->w_p_diff_saved)
1593 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001594
Bram Moolenaar43929962015-07-03 15:06:56 +02001595 if (wp->w_p_scb)
1596 wp->w_p_scb = wp->w_p_scb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001597 if (wp->w_p_crb)
1598 wp->w_p_crb = wp->w_p_crb_save;
Bram Moolenaar4223d432021-02-10 13:18:17 +01001599 if (!(diff_flags & DIFF_FOLLOWWRAP))
1600 {
zeertzjq9e7f1fc2024-03-16 09:40:22 +01001601 if (!wp->w_p_wrap && wp->w_p_wrap_save)
1602 {
1603 wp->w_p_wrap = TRUE;
1604 wp->w_leftcol = 0;
1605 }
Bram Moolenaar4223d432021-02-10 13:18:17 +01001606 }
Bram Moolenaar43929962015-07-03 15:06:56 +02001607#ifdef FEAT_FOLDING
1608 free_string_option(wp->w_p_fdm);
Bram Moolenaar79a213d2017-05-16 13:15:18 +02001609 wp->w_p_fdm = vim_strsave(
1610 *wp->w_p_fdm_save ? wp->w_p_fdm_save : (char_u*)"manual");
Bram Moolenaar43929962015-07-03 15:06:56 +02001611
1612 if (wp->w_p_fdc == diff_foldcolumn)
1613 wp->w_p_fdc = wp->w_p_fdc_save;
1614 if (wp->w_p_fdl == 0)
1615 wp->w_p_fdl = wp->w_p_fdl_save;
1616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 // Only restore 'foldenable' when 'foldmethod' is not
1618 // "manual", otherwise we continue to show the diff folds.
Bram Moolenaar43929962015-07-03 15:06:56 +02001619 if (wp->w_p_fen)
1620 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1621 : wp->w_p_fen_save;
1622
1623 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001624#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001625 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001626 // remove filler lines
Bram Moolenaare67d5462016-08-27 22:40:42 +02001627 wp->w_topfill = 0;
1628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 // make sure topline is not halfway a fold and cursor is
1630 // invalidated
Bram Moolenaare67d5462016-08-27 22:40:42 +02001631 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001633 // Note: 'sbo' is not restored, it's a global option.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001634 diff_buf_adjust(wp);
1635 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001636 diffwin |= wp->w_p_diff;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001637 }
1638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001639 // Also remove hidden buffers from the list.
Bram Moolenaar25ea0542017-02-03 23:16:28 +01001640 if (eap->forceit)
1641 diff_buf_clear();
1642
Bram Moolenaarc8234772019-11-10 21:00:27 +01001643 if (!diffwin)
1644 {
1645 diff_need_update = FALSE;
1646 curtab->tp_diff_invalid = FALSE;
1647 curtab->tp_diff_update = FALSE;
1648 diff_clear(curtab);
1649 }
1650
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001651 // Remove "hor" from 'scrollopt' if there are no diff windows left.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001652 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1653 do_cmdline_cmd((char_u *)"set sbo-=hor");
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001654}
1655
1656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 * Read the diff output and add each entry to the diff list.
1658 */
1659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660diff_read(
Bram Moolenaare828b762018-09-10 17:51:58 +02001661 int idx_orig, // idx of original file
1662 int idx_new, // idx of new file
Lewis Russelld9da86e2021-12-28 13:54:41 +00001663 diffio_T *dio) // diff output
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
Bram Moolenaare828b762018-09-10 17:51:58 +02001665 FILE *fd = NULL;
1666 int line_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001668 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 diff_T *dn, *dpl;
Lewis Russelld9da86e2021-12-28 13:54:41 +00001670 diffout_T *dout = &dio->dio_diff;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001671 char_u linebuf[LBUFLEN]; // only need to hold the diff line
Bram Moolenaare828b762018-09-10 17:51:58 +02001672 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 long off;
1674 int i;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 int notset = TRUE; // block "*dp" not set yet
Bram Moolenaar49166972021-12-30 10:51:45 +00001676 diffhunk_T *hunk = NULL; // init to avoid gcc warning
Lewis Russelld9da86e2021-12-28 13:54:41 +00001677
Bram Moolenaare828b762018-09-10 17:51:58 +02001678 enum {
1679 DIFF_ED,
1680 DIFF_UNIFIED,
1681 DIFF_NONE
1682 } diffstyle = DIFF_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaare828b762018-09-10 17:51:58 +02001684 if (dout->dout_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001686 diffstyle = DIFF_UNIFIED;
1687 }
1688 else
1689 {
1690 fd = mch_fopen((char *)dout->dout_fname, "r");
1691 if (fd == NULL)
1692 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001693 emsg(_(e_cannot_read_diff_output));
Bram Moolenaare828b762018-09-10 17:51:58 +02001694 return;
1695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
1697
Lewis Russelld9da86e2021-12-28 13:54:41 +00001698 if (!dio->dio_internal)
1699 {
1700 hunk = ALLOC_ONE(diffhunk_T);
1701 if (hunk == NULL)
Bram Moolenaar5d46dcf2022-03-25 14:46:47 +00001702 {
1703 if (fd != NULL)
1704 fclose(fd);
Lewis Russelld9da86e2021-12-28 13:54:41 +00001705 return;
Bram Moolenaar5d46dcf2022-03-25 14:46:47 +00001706 }
Lewis Russelld9da86e2021-12-28 13:54:41 +00001707 }
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 for (;;)
1710 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00001711 if (dio->dio_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001713 if (line_idx >= dout->dout_ga.ga_len)
Lewis Russelld9da86e2021-12-28 13:54:41 +00001714 break; // did last line
Lewis Russelld9da86e2021-12-28 13:54:41 +00001715 hunk = ((diffhunk_T **)dout->dout_ga.ga_data)[line_idx++];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717 else
1718 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00001719 if (fd == NULL)
1720 {
1721 if (line_idx >= dout->dout_ga.ga_len)
1722 break; // did last line
1723 line = ((char_u **)dout->dout_ga.ga_data)[line_idx++];
1724 }
Bram Moolenaar3b8defd2018-09-13 13:03:11 +02001725 else
Lewis Russelld9da86e2021-12-28 13:54:41 +00001726 {
1727 if (vim_fgets(linebuf, LBUFLEN, fd))
1728 break; // end of file
1729 line = linebuf;
1730 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001731
Lewis Russelld9da86e2021-12-28 13:54:41 +00001732 if (diffstyle == DIFF_NONE)
1733 {
1734 // Determine diff style.
1735 // ed like diff looks like this:
1736 // {first}[,{last}]c{first}[,{last}]
1737 // {first}a{first}[,{last}]
1738 // {first}[,{last}]d{first}
1739 //
1740 // unified diff looks like this:
1741 // --- file1 2018-03-20 13:23:35.783153140 +0100
1742 // +++ file2 2018-03-20 13:23:41.183156066 +0100
1743 // @@ -1,3 +1,5 @@
Keith Thompson184f71c2024-01-04 21:19:04 +01001744 if (SAFE_isdigit(*line))
Lewis Russelld9da86e2021-12-28 13:54:41 +00001745 diffstyle = DIFF_ED;
1746 else if ((STRNCMP(line, "@@ ", 3) == 0))
1747 diffstyle = DIFF_UNIFIED;
1748 else if ((STRNCMP(line, "--- ", 4) == 0)
1749 && (vim_fgets(linebuf, LBUFLEN, fd) == 0)
1750 && (STRNCMP(line, "+++ ", 4) == 0)
1751 && (vim_fgets(linebuf, LBUFLEN, fd) == 0)
1752 && (STRNCMP(line, "@@ ", 3) == 0))
1753 diffstyle = DIFF_UNIFIED;
1754 else
1755 // Format not recognized yet, skip over this line. Cygwin
1756 // diff may put a warning at the start of the file.
1757 continue;
1758 }
1759
1760 if (diffstyle == DIFF_ED)
1761 {
Keith Thompson184f71c2024-01-04 21:19:04 +01001762 if (!SAFE_isdigit(*line))
Lewis Russelld9da86e2021-12-28 13:54:41 +00001763 continue; // not the start of a diff block
1764 if (parse_diff_ed(line, hunk) == FAIL)
1765 continue;
1766 }
1767 else if (diffstyle == DIFF_UNIFIED)
1768 {
1769 if (STRNCMP(line, "@@ ", 3) != 0)
1770 continue; // not the start of a diff block
1771 if (parse_diff_unified(line, hunk) == FAIL)
1772 continue;
1773 }
1774 else
1775 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001776 emsg(_(e_invalid_diff_format));
Lewis Russelld9da86e2021-12-28 13:54:41 +00001777 break;
1778 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001779 }
1780
1781 // Go over blocks before the change, for which orig and new are equal.
1782 // Copy blocks from orig to new.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 while (dp != NULL
Lewis Russelld9da86e2021-12-28 13:54:41 +00001784 && hunk->lnum_orig > dp->df_lnum[idx_orig]
1785 + dp->df_count[idx_orig])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
1787 if (notset)
1788 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1789 dprev = dp;
1790 dp = dp->df_next;
1791 notset = TRUE;
1792 }
1793
1794 if (dp != NULL
Lewis Russelld9da86e2021-12-28 13:54:41 +00001795 && hunk->lnum_orig <= dp->df_lnum[idx_orig]
1796 + dp->df_count[idx_orig]
1797 && hunk->lnum_orig + hunk->count_orig >= dp->df_lnum[idx_orig])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001799 // New block overlaps with existing block(s).
1800 // First find last block that overlaps.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
Lewis Russelld9da86e2021-12-28 13:54:41 +00001802 if (hunk->lnum_orig + hunk->count_orig
1803 < dpl->df_next->df_lnum[idx_orig])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 break;
1805
Bram Moolenaare828b762018-09-10 17:51:58 +02001806 // If the newly found block starts before the old one, set the
1807 // start back a number of lines.
Lewis Russelld9da86e2021-12-28 13:54:41 +00001808 off = dp->df_lnum[idx_orig] - hunk->lnum_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (off > 0)
1810 {
1811 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001812 if (curtab->tp_diffbuf[i] != NULL)
Yukihiro Nakadaira06fe70c2024-09-26 16:19:42 +02001813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 dp->df_lnum[i] -= off;
Yukihiro Nakadaira06fe70c2024-09-26 16:19:42 +02001815 dp->df_count[i] += off;
1816 }
Lewis Russelld9da86e2021-12-28 13:54:41 +00001817 dp->df_lnum[idx_new] = hunk->lnum_new;
1818 dp->df_count[idx_new] = hunk->count_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 else if (notset)
1821 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001822 // new block inside existing one, adjust new block
Lewis Russelld9da86e2021-12-28 13:54:41 +00001823 dp->df_lnum[idx_new] = hunk->lnum_new + off;
1824 dp->df_count[idx_new] = hunk->count_new - off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826 else
Bram Moolenaare828b762018-09-10 17:51:58 +02001827 // second overlap of new block with existing block
Yukihiro Nakadaira06fe70c2024-09-26 16:19:42 +02001828 dp->df_count[idx_new] += hunk->count_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaare828b762018-09-10 17:51:58 +02001830 // Adjust the size of the block to include all the lines to the
1831 // end of the existing block or the new diff, whatever ends last.
Lewis Russelld9da86e2021-12-28 13:54:41 +00001832 off = (hunk->lnum_orig + hunk->count_orig)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1834 if (off < 0)
1835 {
Yukihiro Nakadaira06fe70c2024-09-26 16:19:42 +02001836 // new change ends in existing block, adjust the end
1837 dp->df_count[idx_new] += -off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 off = 0;
1839 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001840 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001841 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1843 - dp->df_lnum[i] + off;
1844
Bram Moolenaare828b762018-09-10 17:51:58 +02001845 // Delete the diff blocks that have been merged into one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 dn = dp->df_next;
1847 dp->df_next = dpl->df_next;
1848 while (dn != dp->df_next)
1849 {
1850 dpl = dn->df_next;
1851 vim_free(dn);
1852 dn = dpl;
1853 }
1854 }
1855 else
1856 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001857 // Allocate a new diffblock.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001858 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001860 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Lewis Russelld9da86e2021-12-28 13:54:41 +00001862 dp->df_lnum[idx_orig] = hunk->lnum_orig;
1863 dp->df_count[idx_orig] = hunk->count_orig;
1864 dp->df_lnum[idx_new] = hunk->lnum_new;
1865 dp->df_count[idx_new] = hunk->count_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
Bram Moolenaare828b762018-09-10 17:51:58 +02001867 // Set values for other buffers, these must be equal to the
1868 // original buffer, otherwise there would have been a change
1869 // already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001871 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 diff_copy_entry(dprev, dp, idx_orig, i);
1873 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001874 notset = FALSE; // "*dp" has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876
Bram Moolenaare828b762018-09-10 17:51:58 +02001877 // for remaining diff blocks orig and new are equal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 while (dp != NULL)
1879 {
1880 if (notset)
1881 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1882 dprev = dp;
1883 dp = dp->df_next;
1884 notset = TRUE;
1885 }
1886
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001887done:
Lewis Russelld9da86e2021-12-28 13:54:41 +00001888 if (!dio->dio_internal)
1889 vim_free(hunk);
1890
Bram Moolenaare828b762018-09-10 17:51:58 +02001891 if (fd != NULL)
1892 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893}
1894
1895/*
1896 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1897 */
1898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001899diff_copy_entry(
1900 diff_T *dprev,
1901 diff_T *dp,
1902 int idx_orig,
1903 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
1905 long off;
1906
1907 if (dprev == NULL)
1908 off = 0;
1909 else
1910 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1911 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1912 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1913 dp->df_count[idx_new] = dp->df_count[idx_orig];
1914}
1915
1916/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001917 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001920diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921{
1922 diff_T *p, *next_p;
1923
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001924 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 next_p = p->df_next;
1927 vim_free(p);
1928 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001929 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930}
1931
1932/*
Jonathon7c7a4e62025-01-12 09:58:00 +01001933 * return true if the options are set to use diff linematch
1934 */
1935 static int
1936diff_linematch(diff_T *dp)
1937{
1938 if (!(diff_flags & DIFF_LINEMATCH))
1939 return 0;
1940
1941 // are there more than three diff buffers?
1942 int tsize = 0;
1943 for (int i = 0; i < DB_COUNT; i++)
1944 {
1945 if (curtab->tp_diffbuf[i] != NULL)
1946 {
1947 // for the rare case (bug?) that the count of a diff block is
1948 // negative, do not run the algorithm because this will try to
1949 // allocate a negative amount of space and crash
1950 if (dp->df_count[i] < 0)
1951 return FALSE;
1952 tsize += dp->df_count[i];
1953 }
1954 }
1955
1956 // avoid allocating a huge array because it will lag
1957 return tsize <= linematch_lines;
1958}
1959
1960 static int
1961get_max_diff_length(const diff_T *dp)
1962{
1963 int maxlength = 0;
1964
1965 for (int k = 0; k < DB_COUNT; k++)
1966 {
1967 if (curtab->tp_diffbuf[k] != NULL)
1968 {
1969 if (dp->df_count[k] > maxlength)
1970 maxlength = dp->df_count[k];
1971 }
1972 }
1973 return maxlength;
1974}
1975
1976 static void
1977find_top_diff_block(
1978 diff_T **thistopdiff,
1979 diff_T **nextblockblock,
1980 int fromidx,
1981 int topline)
1982{
1983 diff_T *topdiff = NULL;
1984 diff_T *localtopdiff = NULL;
1985 int topdiffchange = 0;
1986
1987 for (topdiff = curtab->tp_first_diff; topdiff != NULL;
1988 topdiff = topdiff->df_next)
1989 {
1990 // set the top of the current overlapping diff block set as we
1991 // iterate through all of the sets of overlapping diff blocks
1992 if (!localtopdiff || topdiffchange)
1993 {
1994 localtopdiff = topdiff;
1995 topdiffchange = 0;
1996 }
1997
1998 // check if the fromwin topline is matched by the current diff. if so,
1999 // set it to the top of the diff block
2000 if (topline >= topdiff->df_lnum[fromidx] && topline <=
2001 (topdiff->df_lnum[fromidx] + topdiff->df_count[fromidx]))
2002 {
2003 // this line is inside the current diff block, so we will save the
2004 // top block of the set of blocks to refer to later
2005 if ((*thistopdiff) == NULL)
2006 (*thistopdiff) = localtopdiff;
2007 }
2008
2009 // check if the next set of overlapping diff blocks is next
2010 if (!(topdiff->df_next && (topdiff->df_next->df_lnum[fromidx] ==
2011 (topdiff->df_lnum[fromidx] +
2012 topdiff->df_count[fromidx]))))
2013 {
2014 // mark that the next diff block is belongs to a different set of
2015 // overlapping diff blocks
2016 topdiffchange = 1;
2017
2018 // if we already have found that the line number is inside a diff
2019 // block, set the marker of the next block and finish the iteration
2020 if (*thistopdiff)
2021 {
2022 (*nextblockblock) = topdiff->df_next;
2023 break;
2024 }
2025 }
2026 }
2027}
2028
2029 static void
2030count_filler_lines_and_topline(
2031 int *curlinenum_to,
2032 int *linesfiller,
2033 const diff_T *thistopdiff,
2034 const int toidx,
2035 int virtual_lines_passed)
2036{
2037 const diff_T *curdif = thistopdiff;
2038 int ch_virtual_lines = 0;
2039 int isfiller = FALSE;
2040
2041 while (virtual_lines_passed > 0)
2042 {
2043 if (ch_virtual_lines)
2044 {
2045 virtual_lines_passed--;
2046 ch_virtual_lines--;
2047 if (!isfiller)
2048 (*curlinenum_to)++;
2049 else
2050 (*linesfiller)++;
2051 }
2052 else
2053 {
2054 (*linesfiller) = 0;
2055 ch_virtual_lines = get_max_diff_length(curdif);
2056 isfiller = (curdif->df_count[toidx] ? FALSE : TRUE);
2057 if (isfiller)
2058 {
2059 while (curdif && curdif->df_next &&
2060 curdif->df_lnum[toidx] ==
2061 curdif->df_next->df_lnum[toidx] &&
2062 curdif->df_next->df_count[toidx] == 0)
2063 {
2064 curdif = curdif->df_next;
2065 ch_virtual_lines += get_max_diff_length(curdif);
2066 }
2067 }
2068 if (curdif)
2069 curdif = curdif->df_next;
2070 }
2071 }
2072}
2073
2074 static void
2075calculate_topfill_and_topline(
2076 const int fromidx,
2077 const int toidx,
2078 const int from_topline,
2079 const int from_topfill,
2080 int *topfill,
2081 linenr_T *topline)
2082{
2083 // 1. find the position from the top of the diff block, and the start
2084 // of the next diff block
2085 diff_T *thistopdiff = NULL;
2086 diff_T *nextblockblock = NULL;
2087 int virtual_lines_passed = 0;
2088
2089 find_top_diff_block(&thistopdiff, &nextblockblock, fromidx, from_topline);
2090
2091 // count the virtual lines that have been passed
2092 diff_T *curdif = thistopdiff;
2093 while (curdif && (curdif->df_lnum[fromidx] + curdif->df_count[fromidx])
2094 <= from_topline)
2095 {
2096 virtual_lines_passed += get_max_diff_length(curdif);
2097
2098 curdif = curdif->df_next;
2099 }
2100
2101 if (curdif != nextblockblock)
2102 virtual_lines_passed += from_topline - curdif->df_lnum[fromidx];
2103 virtual_lines_passed -= from_topfill;
2104
2105 // count the same amount of virtual lines in the toidx buffer
2106 int curlinenum_to = thistopdiff->df_lnum[toidx];
2107 int linesfiller = 0;
2108
2109 count_filler_lines_and_topline(&curlinenum_to, &linesfiller, thistopdiff,
2110 toidx, virtual_lines_passed);
2111
2112 // count the number of filler lines that would normally be above this line
2113 int maxfiller = 0;
2114 for (diff_T *dpfillertest = thistopdiff; dpfillertest != NULL;
2115 dpfillertest = dpfillertest->df_next)
2116 {
2117 if (dpfillertest->df_lnum[toidx] == curlinenum_to)
2118 {
2119 while (dpfillertest && dpfillertest->df_lnum[toidx] ==
2120 curlinenum_to)
2121 {
2122 maxfiller += dpfillertest->df_count[toidx] ? 0 :
2123 get_max_diff_length(dpfillertest);
2124 dpfillertest = dpfillertest->df_next;
2125 }
2126 break;
2127 }
2128 }
2129 (*topfill) = maxfiller - linesfiller;
2130 (*topline) = curlinenum_to;
2131}
2132
2133 static int
2134linematched_filler_lines(diff_T *dp, int idx, linenr_T lnum, int *linestatus)
2135{
2136 int filler_lines_d1 = 0;
2137
2138 while (dp && dp->df_next &&
2139 lnum == (dp->df_lnum[idx] + dp->df_count[idx]) &&
2140 dp->df_next->df_lnum[idx] == lnum)
2141 {
2142 if (dp->df_count[idx] == 0)
2143 filler_lines_d1 += get_max_diff_length(dp);
2144 dp = dp->df_next;
2145 }
2146
2147 if (dp->df_count[idx] == 0)
2148 filler_lines_d1 += get_max_diff_length(dp);
2149
2150 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
2151 {
2152 int j = 0;
2153
2154 for (int i = 0; i < DB_COUNT; i++)
2155 {
2156 if (curtab->tp_diffbuf[i] != NULL)
2157 {
2158 if (dp->df_count[i])
2159 j++;
2160 }
2161 // is this an added line or a changed line?
2162 if (linestatus)
2163 (*linestatus) = (j == 1) ? -2 : -1;
2164 }
2165 }
2166
2167 return filler_lines_d1;
2168}
2169
2170// Apply results from the linematch algorithm and apply to 'dp' by splitting it
2171// into multiple adjacent diff blocks.
2172 static void
2173apply_linematch_results(
2174 diff_T *dp,
2175 size_t decisions_length,
2176 const int *decisions)
2177{
2178 // get the start line number here in each diff buffer, and then increment
2179 int line_numbers[DB_COUNT];
2180 int outputmap[DB_COUNT];
2181 size_t ndiffs = 0;
2182
2183 for (int i = 0; i < DB_COUNT; i++)
2184 {
2185 if (curtab->tp_diffbuf[i] != NULL)
2186 {
2187 line_numbers[i] = dp->df_lnum[i];
2188 dp->df_count[i] = 0;
2189
2190 // Keep track of the index of the diff buffer we are using here.
2191 // We will use this to write the output of the algorithm to
2192 // diff_T structs at the correct indexes
2193 outputmap[ndiffs] = i;
2194 ndiffs++;
2195 }
2196 }
2197
2198 // write the diffs starting with the current diff block
2199 diff_T *dp_s = dp;
2200 for (size_t i = 0; i < decisions_length; i++)
2201 {
2202 // Don't allocate on first iter since we can reuse the initial
2203 // diffblock
2204 if (i != 0 && (decisions[i - 1] != decisions[i]))
2205 {
2206 // create new sub diff blocks to segment the original diff block
2207 // which we further divided by running the linematch algorithm
2208 dp_s = diff_alloc_new(curtab, dp_s, dp_s->df_next);
2209 dp_s->is_linematched = TRUE;
2210 for (int j = 0; j < DB_COUNT; j++)
2211 {
2212 if (curtab->tp_diffbuf[j] != NULL)
2213 {
2214 dp_s->df_lnum[j] = line_numbers[j];
2215 dp_s->df_count[j] = 0;
2216 }
2217 }
2218 }
2219 for (size_t j = 0; j < ndiffs; j++)
2220 {
2221 if (decisions[i] & (1 << j))
2222 {
2223 // will need to use the map here
2224 dp_s->df_count[outputmap[j]]++;
2225 line_numbers[outputmap[j]]++;
2226 }
2227 }
2228 }
2229 dp->is_linematched = TRUE;
2230}
2231
2232 static void
2233run_linematch_algorithm(diff_T *dp)
2234{
2235 // define buffers for diff algorithm
2236 diffin_T diffbufs_mm[DB_COUNT];
2237 const mmfile_t *diffbufs[DB_COUNT];
2238 int diff_length[DB_COUNT];
2239 size_t ndiffs = 0;
2240
2241 for (int i = 0; i < DB_COUNT; i++)
2242 {
2243 if (curtab->tp_diffbuf[i] != NULL)
2244 {
2245 // write the contents of the entire buffer to
2246 // diffbufs_mm[diffbuffers_count]
2247 if (dp->df_count[i] > 0)
2248 {
2249 diff_write_buffer(curtab->tp_diffbuf[i], &diffbufs_mm[ndiffs],
2250 dp->df_lnum[i], dp->df_lnum[i] + dp->df_count[i] - 1);
2251 }
2252 else
2253 {
2254 diffbufs_mm[ndiffs].din_mmfile.size = 0;
2255 diffbufs_mm[ndiffs].din_mmfile.ptr = NULL;
2256 }
2257
2258 diffbufs[ndiffs] = &diffbufs_mm[ndiffs].din_mmfile;
2259
2260 // keep track of the length of this diff block to pass it to the
2261 // linematch algorithm
2262 diff_length[ndiffs] = dp->df_count[i];
2263
2264 // increment the amount of diff buffers we are passing to the
2265 // algorithm
2266 ndiffs++;
2267 }
2268 }
2269
2270 // we will get the output of the linematch algorithm in the format of an
2271 // array of integers (*decisions) and the length of that array
2272 // (decisions_length)
2273 int *decisions = NULL;
2274 const int iwhite = (diff_flags & (DIFF_IWHITEALL | DIFF_IWHITE)) > 0 ? 1 : 0;
2275 size_t decisions_length =
2276 linematch_nbuffers(diffbufs, diff_length, ndiffs, &decisions, iwhite);
2277
2278 for (size_t i = 0; i < ndiffs; i++)
2279 free(diffbufs_mm[i].din_mmfile.ptr); // TODO should this be vim_free ?
2280
2281 apply_linematch_results(dp, decisions_length, decisions);
2282
2283 free(decisions);
2284}
2285
2286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 * Check diff status for line "lnum" in buffer "buf":
2288 * Returns 0 for nothing special
2289 * Returns -1 for a line that should be highlighted as changed.
2290 * Returns -2 for a line that should be highlighted as added/deleted.
2291 * Returns > 0 for inserting that many filler lines above it (never happens
2292 * when 'diffopt' doesn't contain "filler").
2293 * This should only be used for windows where 'diff' is set.
Jonathon7c7a4e62025-01-12 09:58:00 +01002294 * When diffopt contains linematch, a changed/added/deleted line
2295 * may also have filler lines above it. In such a case, the possibilities
2296 * are no longer mutually exclusive. The number of filler lines is
2297 * returned from diff_check, and the integer 'linestatus' passed by
2298 * pointer is set to -1 to indicate a changed line, and -2 to indicate an
2299 * added line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 */
2301 int
Jonathon7c7a4e62025-01-12 09:58:00 +01002302diff_check_with_linestatus(win_T *wp, linenr_T lnum, int *linestatus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002304 int idx; // index in tp_diffbuf[] for this buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 diff_T *dp;
2306 int maxcount;
2307 int i;
2308 buf_T *buf = wp->w_buffer;
2309 int cmp;
2310
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002311 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002312 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002314 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) // no diffs at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 return 0;
2316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 // safety check: "lnum" must be a buffer line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
2319 return 0;
2320
2321 idx = diff_buf_idx(buf);
2322 if (idx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002323 return 0; // no diffs for buffer "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002326 // A closed fold never has filler lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
2328 return 0;
2329#endif
2330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002331 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002332 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2334 break;
2335 if (dp == NULL || lnum < dp->df_lnum[idx])
2336 return 0;
2337
Jonathon7c7a4e62025-01-12 09:58:00 +01002338 // Don't run linematch when lnum is offscreen. Useful for scrollbind
2339 // calculations which need to count all the filler lines above the screen.
2340 if (lnum >= wp->w_topline && lnum < wp->w_botline
2341 && !dp->is_linematched && diff_linematch(dp))
2342 run_linematch_algorithm(dp);
2343
2344 if (dp->is_linematched)
2345 return linematched_filler_lines(dp, idx, lnum, linestatus);
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
2348 {
2349 int zero = FALSE;
2350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002351 // Changed or inserted line. If the other buffers have a count of
2352 // zero, the lines were inserted. If the other buffers have the same
2353 // count, check if the lines are identical.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 cmp = FALSE;
2355 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002356 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 if (dp->df_count[i] == 0)
2359 zero = TRUE;
2360 else
2361 {
2362 if (dp->df_count[i] != dp->df_count[idx])
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002363 return -1; // nr of lines changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 cmp = TRUE;
2365 }
2366 }
2367 if (cmp)
2368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002369 // Compare all lines. If they are equal the lines were inserted
2370 // in some buffers, deleted in others, but not changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002372 if (i != idx && curtab->tp_diffbuf[i] != NULL
2373 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (!diff_equal_entry(dp, idx, i))
2375 return -1;
2376 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002377 // If there is no buffer with zero lines then there is no difference
2378 // any longer. Happens when making a change (or undo) that removes
2379 // the difference. Can't remove the entry here, we might be halfway
2380 // updating the window. Just report the text as unchanged. Other
2381 // windows might still show the change though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (zero == FALSE)
2383 return 0;
2384 return -2;
2385 }
2386
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002387 // If 'diffopt' doesn't contain "filler", return 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (!(diff_flags & DIFF_FILLER))
2389 return 0;
2390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002391 // Insert filler lines above the line just below the change. Will return
2392 // 0 when this buf had the max count.
Jonathon7c7a4e62025-01-12 09:58:00 +01002393 maxcount = get_max_diff_length(dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 return maxcount - dp->df_count[idx];
2395}
2396
Jonathon7c7a4e62025-01-12 09:58:00 +01002397 int
2398diff_check(win_T *wp, linenr_T lnum)
2399{
2400 return diff_check_with_linestatus(wp, lnum, NULL);
2401}
2402
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403/*
2404 * Compare two entries in diff "*dp" and return TRUE if they are equal.
2405 */
2406 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002407diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 int i;
2410 char_u *line;
2411 int cmp;
2412
2413 if (dp->df_count[idx1] != dp->df_count[idx2])
2414 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002415 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 return FALSE;
2417 for (i = 0; i < dp->df_count[idx1]; ++i)
2418 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002419 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 dp->df_lnum[idx1] + i, FALSE));
2421 if (line == NULL)
2422 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002423 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 dp->df_lnum[idx2] + i, FALSE));
2425 vim_free(line);
2426 if (cmp != 0)
2427 return FALSE;
2428 }
2429 return TRUE;
2430}
2431
2432/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002433 * Compare the characters at "p1" and "p2". If they are equal (possibly
2434 * ignoring case) return TRUE and set "len" to the number of bytes.
2435 */
2436 static int
2437diff_equal_char(char_u *p1, char_u *p2, int *len)
2438{
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002439 int l = (*mb_ptr2len)(p1);
2440
2441 if (l != (*mb_ptr2len)(p2))
2442 return FALSE;
2443 if (l > 1)
2444 {
2445 if (STRNCMP(p1, p2, l) != 0
2446 && (!enc_utf8
2447 || !(diff_flags & DIFF_ICASE)
2448 || utf_fold(utf_ptr2char(p1))
2449 != utf_fold(utf_ptr2char(p2))))
2450 return FALSE;
2451 *len = l;
2452 }
2453 else
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002454 {
2455 if ((*p1 != *p2)
2456 && (!(diff_flags & DIFF_ICASE)
2457 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
2458 return FALSE;
2459 *len = 1;
2460 }
2461 return TRUE;
2462}
2463
2464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 * Compare strings "s1" and "s2" according to 'diffopt'.
2466 * Return non-zero when they are different.
2467 */
2468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002469diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
Bram Moolenaar785fc652018-09-15 19:17:38 +02002474 if ((diff_flags & DIFF_IBLANK)
2475 && (*skipwhite(s1) == NUL || *skipwhite(s2) == NUL))
2476 return 0;
2477
2478 if ((diff_flags & (DIFF_ICASE | ALL_WHITE_DIFF)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return STRCMP(s1, s2);
Bram Moolenaar785fc652018-09-15 19:17:38 +02002480 if ((diff_flags & DIFF_ICASE) && !(diff_flags & ALL_WHITE_DIFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 return MB_STRICMP(s1, s2);
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 p1 = s1;
2484 p2 = s2;
Bram Moolenaar785fc652018-09-15 19:17:38 +02002485
2486 // Ignore white space changes and possibly ignore case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 while (*p1 != NUL && *p2 != NUL)
2488 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002489 if (((diff_flags & DIFF_IWHITE)
2490 && VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
2491 || ((diff_flags & DIFF_IWHITEALL)
2492 && (VIM_ISWHITE(*p1) || VIM_ISWHITE(*p2))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 p1 = skipwhite(p1);
2495 p2 = skipwhite(p2);
2496 }
2497 else
2498 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002499 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002501 p1 += l;
2502 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504 }
2505
Bram Moolenaar785fc652018-09-15 19:17:38 +02002506 // Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 p1 = skipwhite(p1);
2508 p2 = skipwhite(p2);
2509 if (*p1 != NUL || *p2 != NUL)
2510 return 1;
2511 return 0;
2512}
2513
2514/*
2515 * Return the number of filler lines above "lnum".
2516 */
2517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002518diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520 int n;
2521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002522 // be quick when there are no filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (!(diff_flags & DIFF_FILLER))
2524 return 0;
2525 n = diff_check(wp, lnum);
2526 if (n <= 0)
2527 return 0;
2528 return n;
2529}
2530
2531/*
2532 * Set the topline of "towin" to match the position in "fromwin", so that they
2533 * show the same diff'ed lines.
2534 */
2535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002536diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002538 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002540 int fromidx;
2541 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002543 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 int i;
2545
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002546 fromidx = diff_buf_idx(frombuf);
2547 if (fromidx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002548 return; // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002550 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002551 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553 towin->w_topfill = 0;
2554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002555 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002556 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002557 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 break;
2559 if (dp == NULL)
2560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002561 // After last change, compute topline relative to end of file; no
2562 // filler lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002564 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566 else
2567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002568 // Find index for "towin".
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002569 toidx = diff_buf_idx(towin->w_buffer);
2570 if (toidx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002571 return; // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002573 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2574 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Jonathon7c7a4e62025-01-12 09:58:00 +01002576 if (dp->is_linematched)
2577 {
2578 calculate_topfill_and_topline(fromidx, toidx,
2579 fromwin->w_topline,
2580 fromwin->w_topfill,
2581 &towin->w_topfill,
2582 &towin->w_topline);
2583 }
2584 else
2585 {
2586 // Inside a change: compute filler lines. With three or more
2587 // buffers we need to know the largest count.
2588 max_count = 0;
2589 for (i = 0; i < DB_COUNT; ++i)
2590 if (curtab->tp_diffbuf[i] != NULL
2591 && max_count < dp->df_count[i])
2592 max_count = dp->df_count[i];
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002593
Jonathon7c7a4e62025-01-12 09:58:00 +01002594 if (dp->df_count[toidx] == dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Jonathon7c7a4e62025-01-12 09:58:00 +01002596 // same number of lines: use same filler count
2597 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002598 }
Jonathon7c7a4e62025-01-12 09:58:00 +01002599 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002600 {
2601 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Jonathon7c7a4e62025-01-12 09:58:00 +01002602 {
2603 // more lines in towin and fromwin doesn't show diff
2604 // lines, only filler lines
2605 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2606 {
2607 // towin also only shows filler lines
2608 towin->w_topline = dp->df_lnum[toidx]
2609 + dp->df_count[toidx];
2610 towin->w_topfill = fromwin->w_topfill;
2611 }
2612 else
2613 // towin still has some diff lines to show
2614 towin->w_topline = dp->df_lnum[toidx]
2615 + max_count - fromwin->w_topfill;
2616 }
2617 }
2618 else if (towin->w_topline >= dp->df_lnum[toidx]
2619 + dp->df_count[toidx])
2620 {
2621 // less lines in towin and no diff lines to show: compute
2622 // filler lines
2623 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2624 if (diff_flags & DIFF_FILLER)
2625 {
2626 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2627 // fromwin is also out of diff lines
2628 towin->w_topfill = fromwin->w_topfill;
2629 else
2630 // fromwin has some diff lines
2631 towin->w_topfill = dp->df_lnum[fromidx] +
2632 max_count - lnum;
2633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
2635 }
2636 }
2637 }
2638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002639 // safety check (if diff info gets outdated strange things may happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 towin->w_botfill = FALSE;
2641 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2642 {
2643 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2644 towin->w_botfill = TRUE;
2645 }
2646 if (towin->w_topline < 1)
2647 {
2648 towin->w_topline = 1;
2649 towin->w_topfill = 0;
2650 }
2651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 // When w_topline changes need to recompute w_botline and cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 invalidate_botline_win(towin);
2654 changed_line_abv_curs_win(towin);
2655
2656 check_topfill(towin, FALSE);
2657#ifdef FEAT_FOLDING
2658 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2659 NULL, TRUE, NULL);
2660#endif
2661}
2662
2663/*
2664 * This is called when 'diffopt' is changed.
2665 */
2666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 char_u *p;
2670 int diff_context_new = 6;
Jonathon7c7a4e62025-01-12 09:58:00 +01002671 int linematch_lines_new = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002673 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002674 long diff_algorithm_new = 0;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002675 long diff_indent_heuristic = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002676 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
2678 p = p_dip;
2679 while (*p != NUL)
2680 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002681 // Note: Keep this in sync with p_dip_values
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (STRNCMP(p, "filler", 6) == 0)
2683 {
2684 p += 6;
2685 diff_flags_new |= DIFF_FILLER;
2686 }
2687 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2688 {
2689 p += 8;
2690 diff_context_new = getdigits(&p);
2691 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002692 else if (STRNCMP(p, "iblank", 6) == 0)
2693 {
2694 p += 6;
2695 diff_flags_new |= DIFF_IBLANK;
2696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 else if (STRNCMP(p, "icase", 5) == 0)
2698 {
2699 p += 5;
2700 diff_flags_new |= DIFF_ICASE;
2701 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002702 else if (STRNCMP(p, "iwhiteall", 9) == 0)
2703 {
2704 p += 9;
2705 diff_flags_new |= DIFF_IWHITEALL;
2706 }
2707 else if (STRNCMP(p, "iwhiteeol", 9) == 0)
2708 {
2709 p += 9;
2710 diff_flags_new |= DIFF_IWHITEEOL;
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 else if (STRNCMP(p, "iwhite", 6) == 0)
2713 {
2714 p += 6;
2715 diff_flags_new |= DIFF_IWHITE;
2716 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002717 else if (STRNCMP(p, "horizontal", 10) == 0)
2718 {
2719 p += 10;
2720 diff_flags_new |= DIFF_HORIZONTAL;
2721 }
2722 else if (STRNCMP(p, "vertical", 8) == 0)
2723 {
2724 p += 8;
2725 diff_flags_new |= DIFF_VERTICAL;
2726 }
2727 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2728 {
2729 p += 11;
2730 diff_foldcolumn_new = getdigits(&p);
2731 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002732 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2733 {
2734 p += 9;
2735 diff_flags_new |= DIFF_HIDDEN_OFF;
2736 }
Bram Moolenaarc8234772019-11-10 21:00:27 +01002737 else if (STRNCMP(p, "closeoff", 8) == 0)
2738 {
2739 p += 8;
2740 diff_flags_new |= DIFF_CLOSE_OFF;
2741 }
Bram Moolenaar4223d432021-02-10 13:18:17 +01002742 else if (STRNCMP(p, "followwrap", 10) == 0)
2743 {
2744 p += 10;
2745 diff_flags_new |= DIFF_FOLLOWWRAP;
2746 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002747 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2748 {
2749 p += 16;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002750 diff_indent_heuristic = XDF_INDENT_HEURISTIC;
Bram Moolenaare828b762018-09-10 17:51:58 +02002751 }
2752 else if (STRNCMP(p, "internal", 8) == 0)
2753 {
2754 p += 8;
2755 diff_flags_new |= DIFF_INTERNAL;
2756 }
2757 else if (STRNCMP(p, "algorithm:", 10) == 0)
2758 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002759 // Note: Keep this in sync with p_dip_algorithm_values.
Bram Moolenaare828b762018-09-10 17:51:58 +02002760 p += 10;
2761 if (STRNCMP(p, "myers", 5) == 0)
2762 {
2763 p += 5;
2764 diff_algorithm_new = 0;
2765 }
2766 else if (STRNCMP(p, "minimal", 7) == 0)
2767 {
2768 p += 7;
2769 diff_algorithm_new = XDF_NEED_MINIMAL;
2770 }
2771 else if (STRNCMP(p, "patience", 8) == 0)
2772 {
2773 p += 8;
2774 diff_algorithm_new = XDF_PATIENCE_DIFF;
2775 }
2776 else if (STRNCMP(p, "histogram", 9) == 0)
2777 {
2778 p += 9;
2779 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2780 }
Bram Moolenaard0721052018-11-05 21:21:33 +01002781 else
2782 return FAIL;
Bram Moolenaare828b762018-09-10 17:51:58 +02002783 }
Jonathon7c7a4e62025-01-12 09:58:00 +01002784 else if (STRNCMP(p, "linematch:", 10) == 0 && VIM_ISDIGIT(p[11]))
2785 {
2786 p += 10;
2787 linematch_lines_new = getdigits(&p);
2788 diff_flags_new |= DIFF_LINEMATCH;
2789 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (*p != ',' && *p != NUL)
2792 return FAIL;
2793 if (*p == ',')
2794 ++p;
2795 }
2796
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002797 diff_algorithm_new |= diff_indent_heuristic;
2798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002799 // Can't have both "horizontal" and "vertical".
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002800 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2801 return FAIL;
2802
Bram Moolenaar198fa062018-09-18 21:20:26 +02002803 // If flags were added or removed, or the algorithm was changed, need to
2804 // update the diff.
Bram Moolenaare828b762018-09-10 17:51:58 +02002805 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002806 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002807 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
2809 diff_flags = diff_flags_new;
Bram Moolenaarb9ddda62019-02-19 23:00:50 +01002810 diff_context = diff_context_new == 0 ? 1 : diff_context_new;
Jonathon7c7a4e62025-01-12 09:58:00 +01002811 linematch_lines = linematch_lines_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002812 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002813 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 diff_redraw(TRUE);
2816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002817 // recompute the scroll binding with the new option value, may
2818 // remove or add filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 check_scrollbind((linenr_T)0, 0L);
2820
2821 return OK;
2822}
2823
2824/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002825 * Return TRUE if 'diffopt' contains "horizontal".
2826 */
2827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002828diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002829{
2830 return (diff_flags & DIFF_HORIZONTAL) != 0;
2831}
2832
2833/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002834 * Return TRUE if 'diffopt' contains "hiddenoff".
2835 */
2836 int
2837diffopt_hiddenoff(void)
2838{
2839 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2840}
2841
2842/*
Bram Moolenaarc8234772019-11-10 21:00:27 +01002843 * Return TRUE if 'diffopt' contains "closeoff".
2844 */
2845 int
2846diffopt_closeoff(void)
2847{
2848 return (diff_flags & DIFF_CLOSE_OFF) != 0;
2849}
2850
2851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * Find the difference within a changed line.
2853 * Returns TRUE if the line was added, no other buffer has it.
2854 */
2855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002856diff_find_change(
2857 win_T *wp,
2858 linenr_T lnum,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002859 int *startp, // first char of the change
2860 int *endp) // last char of the change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861{
2862 char_u *line_org;
2863 char_u *line_new;
2864 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002865 int si_org, si_new;
2866 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 diff_T *dp;
2868 int idx;
2869 int off;
2870 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002871 char_u *p1, *p2;
2872 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002874 // Make a copy of the line, the next ml_get() will invalidate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2876 if (line_org == NULL)
2877 return FALSE;
2878
2879 idx = diff_buf_idx(wp->w_buffer);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002880 if (idx == DB_COUNT) // cannot happen
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002881 {
2882 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002886 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002887 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2889 break;
Jonathon7c7a4e62025-01-12 09:58:00 +01002890 if (dp->is_linematched)
2891 {
2892 while (dp && dp->df_next
2893 && lnum == dp->df_count[idx] + dp->df_lnum[idx]
2894 && dp->df_next->df_lnum[idx] == lnum)
2895 dp = dp->df_next;
2896 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002897 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002898 {
2899 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 off = lnum - dp->df_lnum[idx];
2904
2905 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002906 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002908 // Skip lines that are not in the other change (filler lines).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 if (off >= dp->df_count[i])
2910 continue;
2911 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002912 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2913 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002915 // Search for start of difference
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002916 si_org = si_new = 0;
2917 while (line_org[si_org] != NUL)
2918 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002919 if (((diff_flags & DIFF_IWHITE)
2920 && VIM_ISWHITE(line_org[si_org])
2921 && VIM_ISWHITE(line_new[si_new]))
2922 || ((diff_flags & DIFF_IWHITEALL)
2923 && (VIM_ISWHITE(line_org[si_org])
2924 || VIM_ISWHITE(line_new[si_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002925 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002926 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2927 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002928 }
2929 else
2930 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002931 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2932 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002933 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002934 si_org += l;
2935 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002936 }
2937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (has_mbyte)
2939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002940 // Move back to first byte of character in both lines (may
2941 // have "nn^" in line_org and "n^ in line_new).
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002942 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2943 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002945 if (*startp > si_org)
2946 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002948 // Search for end of difference, if any.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002949 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
2951 ei_org = (int)STRLEN(line_org);
2952 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002953 while (ei_org >= *startp && ei_new >= si_new
2954 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002956 if (((diff_flags & DIFF_IWHITE)
2957 && VIM_ISWHITE(line_org[ei_org])
2958 && VIM_ISWHITE(line_new[ei_new]))
2959 || ((diff_flags & DIFF_IWHITEALL)
2960 && (VIM_ISWHITE(line_org[ei_org])
2961 || VIM_ISWHITE(line_new[ei_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002962 {
2963 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002964 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002965 --ei_org;
2966 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002967 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002968 --ei_new;
2969 }
2970 else
2971 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002972 p1 = line_org + ei_org;
2973 p2 = line_new + ei_new;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002974 p1 -= (*mb_head_off)(line_org, p1);
2975 p2 -= (*mb_head_off)(line_new, p2);
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002976 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002977 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002978 ei_org -= l;
2979 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 if (*endp < ei_org)
2983 *endp = ei_org;
2984 }
2985 }
2986
2987 vim_free(line_org);
2988 return added;
2989}
2990
2991#if defined(FEAT_FOLDING) || defined(PROTO)
2992/*
2993 * Return TRUE if line "lnum" is not close to a diff block, this line should
2994 * be in a fold.
2995 * Return FALSE if there are no diff blocks at all in this window.
2996 */
2997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002998diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 int i;
3001 int idx = -1;
3002 int other = FALSE;
3003 diff_T *dp;
3004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003005 // Return if 'diff' isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (!wp->w_p_diff)
3007 return FALSE;
3008
3009 for (i = 0; i < DB_COUNT; ++i)
3010 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003011 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003013 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 other = TRUE;
3015 }
3016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003017 // return here if there are no diffs in the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (idx == -1 || !other)
3019 return FALSE;
3020
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003021 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003022 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003024 // Return if there are no diff blocks. All lines will be folded.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003025 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 return TRUE;
3027
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003028 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003030 // If this change is below the line there can't be any further match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (dp->df_lnum[idx] - diff_context > lnum)
3032 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003033 // If this change ends before the line we have a match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
3035 return FALSE;
3036 }
3037 return TRUE;
3038}
3039#endif
3040
3041/*
3042 * "dp" and "do" commands.
3043 */
3044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003045nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01003048 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
Bram Moolenaarf2732452018-06-03 14:47:35 +02003050#ifdef FEAT_JOB_CHANNEL
3051 if (bt_prompt(curbuf))
3052 {
3053 vim_beep(BO_OPER);
3054 return;
3055 }
3056#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01003057 if (count == 0)
3058 ea.arg = (char_u *)"";
3059 else
3060 {
3061 vim_snprintf((char *)buf, 30, "%ld", count);
3062 ea.arg = buf;
3063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 if (put)
3065 ea.cmdidx = CMD_diffput;
3066 else
3067 ea.cmdidx = CMD_diffget;
3068 ea.addr_count = 0;
3069 ea.line1 = curwin->w_cursor.lnum;
3070 ea.line2 = curwin->w_cursor.lnum;
3071 ex_diffgetput(&ea);
3072}
3073
3074/*
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003075 * Return TRUE if "diff" appears in the list of diff blocks of the current tab.
3076 */
3077 static int
3078valid_diff(diff_T *diff)
3079{
3080 diff_T *dp;
3081
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003082 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003083 if (dp == diff)
3084 return TRUE;
3085 return FALSE;
3086}
3087
3088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * ":diffget"
3090 * ":diffput"
3091 */
3092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 linenr_T lnum;
3096 int count;
3097 linenr_T off = 0;
3098 diff_T *dp;
3099 diff_T *dprev;
3100 diff_T *dfree;
3101 int idx_cur;
3102 int idx_other;
3103 int idx_from;
3104 int idx_to;
3105 int i;
3106 int added;
3107 char_u *p;
3108 aco_save_T aco;
3109 buf_T *buf;
3110 int start_skip, end_skip;
3111 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003112 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00003113 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003115 // Find the current buffer in the list of diff buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 idx_cur = diff_buf_idx(curbuf);
3117 if (idx_cur == DB_COUNT)
3118 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003119 emsg(_(e_current_buffer_is_not_in_diff_mode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 return;
3121 }
3122
3123 if (*eap->arg == NUL)
3124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003125 // No argument: Find the other buffer in the list of diff buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003127 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00003128 && curtab->tp_diffbuf[idx_other] != NULL)
3129 {
3130 if (eap->cmdidx != CMD_diffput
3131 || curtab->tp_diffbuf[idx_other]->b_p_ma)
3132 break;
3133 found_not_ma = TRUE;
3134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (idx_other == DB_COUNT)
3136 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00003137 if (found_not_ma)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003138 emsg(_(e_no_other_buffer_in_diff_mode_is_modifiable));
Bram Moolenaar602eb742007-02-20 03:43:38 +00003139 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003140 emsg(_(e_no_other_buffer_in_diff_mode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 return;
3142 }
3143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003144 // Check that there isn't a third buffer in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003146 if (curtab->tp_diffbuf[i] != curbuf
3147 && curtab->tp_diffbuf[i] != NULL
3148 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003150 emsg(_(e_more_than_two_buffers_in_diff_mode_dont_know_which_one_to_use));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 return;
3152 }
3153 }
3154 else
3155 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003156 // Buffer number or pattern given. Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003158 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 --p;
3160 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
3161 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003162 if (eap->arg + i == p) // digits only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 i = atol((char *)eap->arg);
3164 else
3165 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01003166 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (i < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003168 return; // error message already given
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 buf = buflist_findnr(i);
3171 if (buf == NULL)
3172 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003173 semsg(_(e_cant_find_buffer_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 return;
3175 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00003176 if (buf == curbuf)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003177 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 idx_other = diff_buf_idx(buf);
3179 if (idx_other == DB_COUNT)
3180 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003181 semsg(_(e_buffer_str_is_not_in_diff_mode), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 return;
3183 }
3184 }
3185
3186 diff_busy = TRUE;
3187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003188 // When no range given include the line above or below the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (eap->addr_count == 0)
3190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003191 // Make it possible that ":diffget" on the last line gets line below
3192 // the cursor line when there is no difference above the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (eap->cmdidx == CMD_diffget
3194 && eap->line1 == curbuf->b_ml.ml_line_count
3195 && diff_check(curwin, eap->line1) == 0
3196 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
3197 ++eap->line2;
3198 else if (eap->line1 > 0)
3199 --eap->line1;
3200 }
3201
3202 if (eap->cmdidx == CMD_diffget)
3203 {
3204 idx_from = idx_other;
3205 idx_to = idx_cur;
3206 }
3207 else
3208 {
3209 idx_from = idx_cur;
3210 idx_to = idx_other;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003211 // Need to make the other buffer the current buffer to be able to make
3212 // changes in it.
Bram Moolenaare76062c2022-11-28 18:51:43 +00003213 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003214 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaare76062c2022-11-28 18:51:43 +00003215 if (curbuf != curtab->tp_diffbuf[idx_other])
3216 // Could not find a window for this buffer, the rest is likely to
3217 // fail.
3218 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
3220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003221 // May give the warning for a changed buffer here, which can trigger the
3222 // FileChangedRO autocommand, which may do nasty things and mess
3223 // everything up.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003224 if (!curbuf->b_changed)
3225 {
3226 change_warning(0);
3227 if (diff_buf_idx(curbuf) != idx_to)
3228 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003229 emsg(_(e_buffer_changed_unexpectedly));
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003230 goto theend;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003231 }
3232 }
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003235 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Jonathon7c7a4e62025-01-12 09:58:00 +01003237 if (!eap->addr_count)
3238 {
3239 // handle the case with adjacent diff blocks
3240 while (dp->is_linematched
3241 && dp->df_next
3242 && dp->df_next->df_lnum[idx_cur] == dp->df_lnum[idx_cur] +
3243 dp->df_count[idx_cur]
3244 && dp->df_next->df_lnum[idx_cur] == eap->line1 + off + 1)
3245 {
3246 dprev = dp;
3247 dp = dp->df_next;
3248 }
3249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (dp->df_lnum[idx_cur] > eap->line2 + off)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003251 break; // past the range that was specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253 dfree = NULL;
3254 lnum = dp->df_lnum[idx_to];
3255 count = dp->df_count[idx_to];
3256 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
3257 && u_save(lnum - 1, lnum + count) != FAIL)
3258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 // Inside the specified range and saving for undo worked.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 start_skip = 0;
3261 end_skip = 0;
3262 if (eap->addr_count > 0)
3263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003264 // A range was specified: check if lines need to be skipped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
3266 if (start_skip > 0)
3267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003268 // range starts below start of current diff block
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (start_skip > count)
3270 {
3271 lnum += count;
3272 count = 0;
3273 }
3274 else
3275 {
3276 count -= start_skip;
3277 lnum += start_skip;
3278 }
3279 }
3280 else
3281 start_skip = 0;
3282
3283 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
3284 - (eap->line2 + off);
3285 if (end_skip > 0)
3286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003287 // range ends above end of current/from diff block
3288 if (idx_cur == idx_from) // :diffput
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 i = dp->df_count[idx_cur] - start_skip - end_skip;
3291 if (count > i)
3292 count = i;
3293 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 else // :diffget
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
3296 count -= end_skip;
3297 end_skip = dp->df_count[idx_from] - start_skip - count;
3298 if (end_skip < 0)
3299 end_skip = 0;
3300 }
3301 }
3302 else
3303 end_skip = 0;
3304 }
3305
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003306 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 added = 0;
3308 for (i = 0; i < count; ++i)
3309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003310 // remember deleting the last line of the buffer
Bram Moolenaar280f1262006-01-30 00:14:18 +00003311 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar4e677b92022-07-28 18:44:27 +01003312 if (ml_delete(lnum) == OK)
3313 --added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
3316 {
3317 linenr_T nr;
3318
3319 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003320 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003322 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
3323 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (p != NULL)
3325 {
3326 ml_append(lnum + i - 1, p, 0, FALSE);
3327 vim_free(p);
3328 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003329 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
3330 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003331 // Added the first line into an empty buffer, need to
3332 // delete the dummy empty line.
Bram Moolenaar280f1262006-01-30 00:14:18 +00003333 buf_empty = FALSE;
Bram Moolenaarca70c072020-05-30 20:30:46 +02003334 ml_delete((linenr_T)2);
Bram Moolenaar280f1262006-01-30 00:14:18 +00003335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 }
3338 new_count = dp->df_count[idx_to] + added;
3339 dp->df_count[idx_to] = new_count;
3340
3341 if (start_skip == 0 && end_skip == 0)
3342 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003343 // Check if there are any other buffers and if the diff is
3344 // equal in them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003346 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
3347 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 && !diff_equal_entry(dp, idx_from, i))
3349 break;
3350 if (i == DB_COUNT)
3351 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 // delete the diff entry, the buffers are now equal here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 dfree = dp;
3354 dp = dp->df_next;
3355 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003356 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
3358 dprev->df_next = dp;
3359 }
3360 }
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (added != 0)
3363 {
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003364 // Adjust marks. This will change the following entries!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
3366 if (curwin->w_cursor.lnum >= lnum)
3367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003368 // Adjust the cursor position if it's in/after the changed
3369 // lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (curwin->w_cursor.lnum >= lnum + count)
3371 curwin->w_cursor.lnum += added;
3372 else if (added < 0)
3373 curwin->w_cursor.lnum = lnum;
3374 }
3375 }
3376 changed_lines(lnum, 0, lnum + count, (long)added);
3377
3378 if (dfree != NULL)
3379 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003380 // Diff is deleted, update folds in other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#ifdef FEAT_FOLDING
3382 diff_fold_update(dfree, idx_to);
3383#endif
3384 vim_free(dfree);
3385 }
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003386
3387 // mark_adjust() may have made "dp" invalid. We don't know where
3388 // to continue then, bail out.
3389 if (added != 0 && !valid_diff(dp))
3390 break;
3391
3392 if (dfree == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003393 // mark_adjust() may have changed the count in a wrong way
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 dp->df_count[idx_to] = new_count;
3395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003396 // When changing the current buffer, keep track of line numbers
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (idx_cur == idx_to)
3398 off += added;
3399 }
3400
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003401 // If before the range or not deleted, go to next diff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (dfree == NULL)
3403 {
3404 dprev = dp;
3405 dp = dp->df_next;
3406 }
3407 }
3408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 // restore curwin/curbuf and a few other things
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003410 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003412 // Syncing undo only works for the current buffer, but we change
3413 // another buffer. Sync undo if the command was typed. This isn't
3414 // 100% right when ":diffput" is used in a function or mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003416 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 aucmd_restbuf(&aco);
3418 }
3419
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003420theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 diff_busy = FALSE;
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003422 if (diff_need_update)
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003423 ex_diffupdate(NULL);
Bram Moolenaardf77cef2018-10-07 17:46:42 +02003424
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +02003425 // Check that the cursor is on a valid character and update its
Bram Moolenaardf77cef2018-10-07 17:46:42 +02003426 // position. When there were filler lines the topline has become
3427 // invalid.
3428 check_cursor();
3429 changed_line_abv_curs();
3430
3431 if (diff_need_update)
3432 // redraw already done by ex_diffupdate()
3433 diff_need_update = FALSE;
Bram Moolenaar198fa062018-09-18 21:20:26 +02003434 else
3435 {
Bram Moolenaar198fa062018-09-18 21:20:26 +02003436 // Also need to redraw the other buffers.
3437 diff_redraw(FALSE);
3438 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
3439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440}
3441
3442#ifdef FEAT_FOLDING
3443/*
3444 * Update folds for all diff buffers for entry "dp".
3445 * Skip buffer with index "skip_idx".
3446 * When there are no diffs, all folds are removed.
3447 */
3448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 int i;
3452 win_T *wp;
3453
Bram Moolenaar29323592016-07-24 22:04:11 +02003454 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003456 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 foldUpdate(wp, dp->df_lnum[i],
3458 dp->df_lnum[i] + dp->df_count[i]);
3459}
3460#endif
3461
3462/*
3463 * Return TRUE if buffer "buf" is in diff-mode.
3464 */
3465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003466diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003468 tabpage_T *tp;
3469
Bram Moolenaar29323592016-07-24 22:04:11 +02003470 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003471 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
3472 return TRUE;
3473 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474}
3475
3476/*
3477 * Move "count" times in direction "dir" to the next diff block.
3478 * Return FAIL if there isn't such a diff block.
3479 */
3480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003481diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483 int idx;
3484 linenr_T lnum = curwin->w_cursor.lnum;
3485 diff_T *dp;
3486
3487 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003488 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return FAIL;
3490
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003491 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003494 if (curtab->tp_first_diff == NULL) // no diffs today
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 return FAIL;
3496
3497 while (--count >= 0)
3498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003499 // Check if already before first diff.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003500 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 break;
3502
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003503 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 if (dp == NULL)
3506 break;
3507 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
3508 || (dir == BACKWARD
3509 && (dp->df_next == NULL
3510 || lnum <= dp->df_next->df_lnum[idx])))
3511 {
3512 lnum = dp->df_lnum[idx];
3513 break;
3514 }
3515 }
3516 }
3517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003518 // don't end up past the end of the file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (lnum > curbuf->b_ml.ml_line_count)
3520 lnum = curbuf->b_ml.ml_line_count;
3521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003522 // When the cursor didn't move at all we fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (lnum == curwin->w_cursor.lnum)
3524 return FAIL;
3525
3526 setpcmark();
3527 curwin->w_cursor.lnum = lnum;
3528 curwin->w_cursor.col = 0;
3529
3530 return OK;
3531}
3532
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003533/*
3534 * Return the line number in the current window that is closest to "lnum1" in
3535 * "buf1" in diff mode.
3536 */
3537 static linenr_T
3538diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003539 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003540 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003541{
3542 int idx1;
3543 int idx2;
3544 diff_T *dp;
3545 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003546
3547 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003548 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003549 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
3550 return lnum1;
3551
3552 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003553 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar860cae12010-06-05 23:22:07 +02003554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003555 if (curtab->tp_first_diff == NULL) // no diffs today
Bram Moolenaar860cae12010-06-05 23:22:07 +02003556 return lnum1;
3557
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003558 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003559 {
3560 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003561 return lnum1 - baseline;
3562 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003564 // Inside the diffblock
Bram Moolenaar860cae12010-06-05 23:22:07 +02003565 baseline = lnum1 - dp->df_lnum[idx1];
3566 if (baseline > dp->df_count[idx2])
3567 baseline = dp->df_count[idx2];
3568
3569 return dp->df_lnum[idx2] + baseline;
3570 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003571 if ( (dp->df_lnum[idx1] == lnum1)
3572 && (dp->df_count[idx1] == 0)
3573 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
3574 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
3575 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02003576 /*
3577 * Special case: if the cursor is just after a zero-count
3578 * block (i.e. all filler) and the target cursor is already
3579 * inside the corresponding block, leave the target cursor
3580 * unmoved. This makes repeated CTRL-W W operations work
3581 * as expected.
3582 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003583 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003584 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
3585 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
3586 }
3587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003588 // If we get here then the cursor is after the last diff
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003589 return lnum1 - baseline;
3590}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003592/*
3593 * Return the line number in the current window that is closest to "lnum1" in
3594 * "buf1" in diff mode. Checks the line number to be valid.
3595 */
3596 linenr_T
3597diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
3598{
3599 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
3600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 // don't end up past the end of the file
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003602 if (lnum > curbuf->b_ml.ml_line_count)
3603 return curbuf->b_ml.ml_line_count;
3604 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003605}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607/*
3608 * For line "lnum" in the current window find the equivalent lnum in window
3609 * "wp", compensating for inserted/deleted lines.
3610 */
3611 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
3614 diff_T *dp;
3615 int idx;
3616 int i;
3617 linenr_T n;
3618
3619 idx = diff_buf_idx(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003620 if (idx == DB_COUNT) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 return (linenr_T)0;
3622
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003623 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003624 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003626 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003627 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
3629 break;
3630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003631 // When after the last change, compute relative to the last line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 if (dp == NULL)
3633 return wp->w_buffer->b_ml.ml_line_count
3634 - (curbuf->b_ml.ml_line_count - lnum);
3635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003636 // Find index for "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 i = diff_buf_idx(wp->w_buffer);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003638 if (i == DB_COUNT) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return (linenr_T)0;
3640
3641 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
3642 if (n > dp->df_lnum[i] + dp->df_count[i])
3643 n = dp->df_lnum[i] + dp->df_count[i];
3644 return n;
3645}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
Bram Moolenaare828b762018-09-10 17:51:58 +02003647/*
3648 * Handle an ED style diff line.
3649 * Return FAIL if the line does not contain diff info.
3650 */
3651 static int
3652parse_diff_ed(
3653 char_u *line,
Lewis Russelld9da86e2021-12-28 13:54:41 +00003654 diffhunk_T *hunk)
Bram Moolenaare828b762018-09-10 17:51:58 +02003655{
3656 char_u *p;
3657 long f1, l1, f2, l2;
3658 int difftype;
3659
3660 // The line must be one of three formats:
3661 // change: {first}[,{last}]c{first}[,{last}]
3662 // append: {first}a{first}[,{last}]
3663 // delete: {first}[,{last}]d{first}
3664 p = line;
3665 f1 = getdigits(&p);
3666 if (*p == ',')
3667 {
3668 ++p;
3669 l1 = getdigits(&p);
3670 }
3671 else
3672 l1 = f1;
3673 if (*p != 'a' && *p != 'c' && *p != 'd')
3674 return FAIL; // invalid diff format
3675 difftype = *p++;
3676 f2 = getdigits(&p);
3677 if (*p == ',')
3678 {
3679 ++p;
3680 l2 = getdigits(&p);
3681 }
3682 else
3683 l2 = f2;
3684 if (l1 < f1 || l2 < f2)
3685 return FAIL;
3686
3687 if (difftype == 'a')
3688 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003689 hunk->lnum_orig = f1 + 1;
3690 hunk->count_orig = 0;
Bram Moolenaare828b762018-09-10 17:51:58 +02003691 }
3692 else
3693 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003694 hunk->lnum_orig = f1;
3695 hunk->count_orig = l1 - f1 + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +02003696 }
3697 if (difftype == 'd')
3698 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003699 hunk->lnum_new = f2 + 1;
3700 hunk->count_new = 0;
Bram Moolenaare828b762018-09-10 17:51:58 +02003701 }
3702 else
3703 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003704 hunk->lnum_new = f2;
3705 hunk->count_new = l2 - f2 + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +02003706 }
3707 return OK;
3708}
3709
3710/*
3711 * Parses unified diff with zero(!) context lines.
3712 * Return FAIL if there is no diff information in "line".
3713 */
3714 static int
3715parse_diff_unified(
3716 char_u *line,
Lewis Russelld9da86e2021-12-28 13:54:41 +00003717 diffhunk_T *hunk)
Bram Moolenaare828b762018-09-10 17:51:58 +02003718{
3719 char_u *p;
3720 long oldline, oldcount, newline, newcount;
3721
3722 // Parse unified diff hunk header:
3723 // @@ -oldline,oldcount +newline,newcount @@
3724 p = line;
3725 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3726 {
3727 oldline = getdigits(&p);
3728 if (*p == ',')
3729 {
3730 ++p;
3731 oldcount = getdigits(&p);
3732 }
3733 else
3734 oldcount = 1;
3735 if (*p++ == ' ' && *p++ == '+')
3736 {
3737 newline = getdigits(&p);
3738 if (*p == ',')
3739 {
3740 ++p;
3741 newcount = getdigits(&p);
3742 }
3743 else
3744 newcount = 1;
3745 }
3746 else
3747 return FAIL; // invalid diff format
3748
3749 if (oldcount == 0)
3750 oldline += 1;
3751 if (newcount == 0)
3752 newline += 1;
3753 if (newline == 0)
3754 newline = 1;
3755
Lewis Russelld9da86e2021-12-28 13:54:41 +00003756 hunk->lnum_orig = oldline;
3757 hunk->count_orig = oldcount;
3758 hunk->lnum_new = newline;
3759 hunk->count_new = newcount;
Bram Moolenaare828b762018-09-10 17:51:58 +02003760
3761 return OK;
3762 }
3763
3764 return FAIL;
3765}
3766
3767/*
3768 * Callback function for the xdl_diff() function.
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003769 * Stores the diff output (indices) in a grow array.
Bram Moolenaare828b762018-09-10 17:51:58 +02003770 */
3771 static int
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003772xdiff_out_indices(
Lewis Russelld9da86e2021-12-28 13:54:41 +00003773 long start_a,
3774 long count_a,
3775 long start_b,
3776 long count_b,
3777 void *priv)
Bram Moolenaare828b762018-09-10 17:51:58 +02003778{
3779 diffout_T *dout = (diffout_T *)priv;
Lewis Russelld9da86e2021-12-28 13:54:41 +00003780 diffhunk_T *p = ALLOC_ONE(diffhunk_T);
Bram Moolenaare828b762018-09-10 17:51:58 +02003781
Lewis Russelld9da86e2021-12-28 13:54:41 +00003782 if (p == NULL)
3783 return -1;
Bram Moolenaarf080d702018-10-31 22:57:26 +01003784
3785 if (ga_grow(&dout->dout_ga, 1) == FAIL)
Bram Moolenaarfebb78f2021-12-29 11:59:53 +00003786 {
3787 vim_free(p);
Bram Moolenaarf080d702018-10-31 22:57:26 +01003788 return -1;
Bram Moolenaarfebb78f2021-12-29 11:59:53 +00003789 }
Lewis Russelld9da86e2021-12-28 13:54:41 +00003790
3791 p->lnum_orig = start_a + 1;
3792 p->count_orig = count_a;
3793 p->lnum_new = start_b + 1;
3794 p->count_new = count_b;
3795 ((diffhunk_T **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
Bram Moolenaare828b762018-09-10 17:51:58 +02003796 return 0;
3797}
3798
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003799/*
3800 * Callback function for the xdl_diff() function.
3801 * Stores the unified diff output in a grow array.
3802 */
3803 static int
3804xdiff_out_unified(
3805 void *priv,
3806 mmbuffer_t *mb,
3807 int nbuf)
3808{
3809 diffout_T *dout = (diffout_T *)priv;
3810 int i;
3811
3812 for (i = 0; i < nbuf; i++)
3813 ga_concat_len(&dout->dout_ga, (char_u *)mb[i].ptr, mb[i].size);
3814
3815 return 0;
3816}
3817
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003818#endif // FEAT_DIFF
3819
3820#if defined(FEAT_EVAL) || defined(PROTO)
3821
3822/*
3823 * "diff_filler()" function
3824 */
3825 void
3826f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3827{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003828# ifdef FEAT_DIFF
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003829 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
3830 return;
3831
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003832 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003833# endif
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003834}
3835
3836/*
3837 * "diff_hlID()" function
3838 */
3839 void
3840f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3841{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003842# ifdef FEAT_DIFF
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003843 linenr_T lnum;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003844 static linenr_T prev_lnum = 0;
3845 static varnumber_T changedtick = 0;
3846 static int fnum = 0;
3847 static int change_start = 0;
3848 static int change_end = 0;
3849 static hlf_T hlID = (hlf_T)0;
3850 int filler_lines;
3851 int col;
3852
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003853 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02003854 && (check_for_lnum_arg(argvars,0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003855 || check_for_number_arg(argvars, 1) == FAIL))
3856 return;
3857
3858 lnum = tv_get_lnum(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003859 if (lnum < 0) // ignore type error in {lnum} arg
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003860 lnum = 0;
3861 if (lnum != prev_lnum
3862 || changedtick != CHANGEDTICK(curbuf)
3863 || fnum != curbuf->b_fnum)
3864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003865 // New line, buffer, change: need to get the values.
Jonathon7c7a4e62025-01-12 09:58:00 +01003866 int linestatus = 0;
3867 filler_lines = diff_check_with_linestatus(curwin, lnum, &linestatus);
3868 if (filler_lines < 0 || linestatus < 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003869 {
Jonathon7c7a4e62025-01-12 09:58:00 +01003870 if (filler_lines == -1 || linestatus == -1)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003871 {
3872 change_start = MAXCOL;
3873 change_end = -1;
3874 if (diff_find_change(curwin, lnum, &change_start, &change_end))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003875 hlID = HLF_ADD; // added line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003876 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003877 hlID = HLF_CHD; // changed line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003878 }
3879 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003880 hlID = HLF_ADD; // added line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003881 }
3882 else
3883 hlID = (hlf_T)0;
3884 prev_lnum = lnum;
3885 changedtick = CHANGEDTICK(curbuf);
3886 fnum = curbuf->b_fnum;
3887 }
3888
3889 if (hlID == HLF_CHD || hlID == HLF_TXD)
3890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003891 col = tv_get_number(&argvars[1]) - 1; // ignore type error in {col}
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003892 if (col >= change_start && col <= change_end)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003893 hlID = HLF_TXD; // changed text
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003894 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003895 hlID = HLF_CHD; // changed line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003896 }
3897 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003898# endif
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003899}
3900
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003901# ifdef FEAT_DIFF
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003902/*
3903 * Parse the diff options passed in "optarg" to the diff() function and return
3904 * the options in "diffopts" and the diff algorithm in "diffalgo".
3905 */
3906 static int
3907parse_diff_optarg(
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003908 typval_T *opts,
3909 int *diffopts,
3910 long *diffalgo,
3911 dio_outfmt_T *diff_output_fmt,
3912 int *diff_ctxlen)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003913{
3914 dict_T *d = opts->vval.v_dict;
3915
3916 char_u *algo = dict_get_string(d, "algorithm", FALSE);
3917 if (algo != NULL)
3918 {
3919 if (STRNCMP(algo, "myers", 5) == 0)
3920 *diffalgo = 0;
3921 else if (STRNCMP(algo, "minimal", 7) == 0)
3922 *diffalgo = XDF_NEED_MINIMAL;
3923 else if (STRNCMP(algo, "patience", 8) == 0)
3924 *diffalgo = XDF_PATIENCE_DIFF;
3925 else if (STRNCMP(algo, "histogram", 9) == 0)
3926 *diffalgo = XDF_HISTOGRAM_DIFF;
3927 }
3928
3929 char_u *output_fmt = dict_get_string(d, "output", FALSE);
3930 if (output_fmt != NULL)
3931 {
3932 if (STRNCMP(output_fmt, "unified", 7) == 0)
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003933 *diff_output_fmt = DIO_OUTPUT_UNIFIED;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003934 else if (STRNCMP(output_fmt, "indices", 7) == 0)
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003935 *diff_output_fmt = DIO_OUTPUT_INDICES;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003936 else
3937 {
3938 semsg(_(e_unsupported_diff_output_format_str), output_fmt);
3939 return FAIL;
3940 }
3941 }
3942
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01003943 *diff_ctxlen = dict_get_number_def(d, "context", 0);
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003944 if (*diff_ctxlen < 0)
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01003945 *diff_ctxlen = 0;
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003946
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003947 if (dict_get_bool(d, "iblank", FALSE))
3948 *diffopts |= DIFF_IBLANK;
3949 if (dict_get_bool(d, "icase", FALSE))
3950 *diffopts |= DIFF_ICASE;
3951 if (dict_get_bool(d, "iwhite", FALSE))
3952 *diffopts |= DIFF_IWHITE;
3953 if (dict_get_bool(d, "iwhiteall", FALSE))
3954 *diffopts |= DIFF_IWHITEALL;
3955 if (dict_get_bool(d, "iwhiteeol", FALSE))
3956 *diffopts |= DIFF_IWHITEEOL;
3957 if (dict_get_bool(d, "indent-heuristic", FALSE))
3958 *diffalgo |= XDF_INDENT_HEURISTIC;
3959
3960 return OK;
3961}
3962
3963/*
3964 * Concatenate the List of strings in "l" and store the result in
3965 * "din->din_mmfile.ptr" and the length in "din->din_mmfile.size".
3966 */
3967 static void
3968list_to_diffin(list_T *l, diffin_T *din, int icase)
3969{
3970 garray_T ga;
3971 listitem_T *li;
3972 char_u *str;
3973
3974 ga_init2(&ga, 512, 4);
3975
3976 FOR_ALL_LIST_ITEMS(l, li)
3977 {
3978 str = tv_get_string(&li->li_tv);
3979 if (icase)
3980 {
3981 str = strlow_save(str);
3982 if (str == NULL)
3983 continue;
3984 }
3985 ga_concat(&ga, str);
3986 ga_concat(&ga, (char_u *)NL_STR);
3987 if (icase)
3988 vim_free(str);
3989 }
3990 if (ga.ga_len > 0)
3991 ((char *)ga.ga_data)[ga.ga_len] = NUL;
3992
3993 din->din_mmfile.ptr = (char *)ga.ga_data;
3994 din->din_mmfile.size = ga.ga_len;
3995}
3996
3997/*
3998 * Get the start and end indices from the diff "hunk".
3999 */
4000 static dict_T *
4001get_diff_hunk_indices(diffhunk_T *hunk)
4002{
4003 dict_T *hunk_dict;
4004
4005 hunk_dict = dict_alloc();
4006 if (hunk_dict == NULL)
4007 return NULL;
4008
4009 dict_add_number(hunk_dict, "from_idx", hunk->lnum_orig - 1);
4010 dict_add_number(hunk_dict, "from_count", hunk->count_orig);
4011 dict_add_number(hunk_dict, "to_idx", hunk->lnum_new - 1);
4012 dict_add_number(hunk_dict, "to_count", hunk->count_new);
4013
4014 return hunk_dict;
4015}
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004016# endif
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004017
4018/*
4019 * "diff()" function
4020 */
4021 void
4022f_diff(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4023{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004024# ifdef FEAT_DIFF
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004025 diffio_T dio;
4026
4027 if (check_for_nonnull_list_arg(argvars, 0) == FAIL
4028 || check_for_nonnull_list_arg(argvars, 1) == FAIL
4029 || check_for_opt_nonnull_dict_arg(argvars, 2) == FAIL)
4030 return;
4031
4032 CLEAR_FIELD(dio);
4033 dio.dio_internal = TRUE;
4034 ga_init2(&dio.dio_diff.dout_ga, sizeof(char *), 1000);
4035
4036 list_T *orig_list = argvars[0].vval.v_list;
4037 list_T *new_list = argvars[1].vval.v_list;
4038
4039 // Save the 'diffopt' option value and restore it after getting the diff.
4040 int save_diff_flags = diff_flags;
4041 long save_diff_algorithm = diff_algorithm;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004042 diff_flags = DIFF_INTERNAL;
4043 diff_algorithm = 0;
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004044 dio.dio_outfmt = DIO_OUTPUT_UNIFIED;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004045 if (argvars[2].v_type != VAR_UNKNOWN)
4046 if (parse_diff_optarg(&argvars[2], &diff_flags, &diff_algorithm,
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004047 &dio.dio_outfmt, &dio.dio_ctxlen) == FAIL)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004048 return;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004049
4050 // Concatenate the List of strings into a single string using newline
4051 // separator. Internal diff library expects a single string.
4052 list_to_diffin(orig_list, &dio.dio_orig, diff_flags & DIFF_ICASE);
4053 list_to_diffin(new_list, &dio.dio_new, diff_flags & DIFF_ICASE);
4054
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004055 // If 'diffexpr' is set, then the internal diff is not used. Set
4056 // 'diffexpr' to an empty string temporarily.
4057 int restore_diffexpr = FALSE;
4058 char_u cc = *p_dex;
4059 if (*p_dex != NUL)
4060 {
4061 restore_diffexpr = TRUE;
4062 *p_dex = NUL;
4063 }
4064
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004065 // Compute the diff
4066 int diff_status = diff_file(&dio);
4067
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004068 // restore 'diffexpr'
4069 if (restore_diffexpr)
4070 *p_dex = cc;
4071
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004072 if (diff_status == FAIL)
4073 goto done;
4074
4075 int hunk_idx = 0;
4076 dict_T *hunk_dict;
4077
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004078 if (dio.dio_outfmt == DIO_OUTPUT_INDICES)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004079 {
4080 if (rettv_list_alloc(rettv) != OK)
4081 goto done;
4082 list_T *l = rettv->vval.v_list;
4083
4084 // Process each diff hunk
4085 diffhunk_T *hunk = NULL;
4086 while (hunk_idx < dio.dio_diff.dout_ga.ga_len)
4087 {
4088 hunk = ((diffhunk_T **)dio.dio_diff.dout_ga.ga_data)[hunk_idx++];
4089
4090 hunk_dict = get_diff_hunk_indices(hunk);
4091 if (hunk_dict == NULL)
4092 goto done;
4093
4094 list_append_dict(l, hunk_dict);
4095 }
4096 }
4097 else
4098 {
4099 ga_append(&dio.dio_diff.dout_ga, NUL);
4100 rettv->v_type = VAR_STRING;
4101 rettv->vval.v_string =
4102 vim_strsave((char_u *)dio.dio_diff.dout_ga.ga_data);
4103 }
4104
4105done:
4106 clear_diffin(&dio.dio_new);
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004107 if (dio.dio_outfmt == DIO_OUTPUT_INDICES)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004108 clear_diffout(&dio.dio_diff);
4109 else
4110 ga_clear(&dio.dio_diff.dout_ga);
4111 clear_diffin(&dio.dio_orig);
4112 // Restore the 'diffopt' option value.
4113 diff_flags = save_diff_flags;
4114 diff_algorithm = save_diff_algorithm;
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004115# endif
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004116}
4117
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004118#endif