blob: c348894ec946f2478723ab3a2e4b4b8e6444fc47 [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
Yukihiro Nakadaira01f65092025-01-15 18:36:43 +01001827 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001828 // second overlap of new block with existing block
Yukihiro Nakadaira06fe70c2024-09-26 16:19:42 +02001829 dp->df_count[idx_new] += hunk->count_new;
Yukihiro Nakadaira01f65092025-01-15 18:36:43 +01001830 if ((dp->df_lnum[idx_new] + dp->df_count[idx_new] - 1)
1831 > curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count)
1832 dp->df_count[idx_new] = curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count
1833 - dp->df_lnum[idx_new] + 1;
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaare828b762018-09-10 17:51:58 +02001836 // Adjust the size of the block to include all the lines to the
1837 // end of the existing block or the new diff, whatever ends last.
Lewis Russelld9da86e2021-12-28 13:54:41 +00001838 off = (hunk->lnum_orig + hunk->count_orig)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1840 if (off < 0)
1841 {
Yukihiro Nakadaira06fe70c2024-09-26 16:19:42 +02001842 // new change ends in existing block, adjust the end
1843 dp->df_count[idx_new] += -off;
Yukihiro Nakadaira01f65092025-01-15 18:36:43 +01001844 if ((dp->df_lnum[idx_new] + dp->df_count[idx_new] - 1)
1845 > curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count)
1846 dp->df_count[idx_new] = curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count
1847 - dp->df_lnum[idx_new] + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 off = 0;
1849 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001850 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001851 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1853 - dp->df_lnum[i] + off;
1854
Bram Moolenaare828b762018-09-10 17:51:58 +02001855 // Delete the diff blocks that have been merged into one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 dn = dp->df_next;
1857 dp->df_next = dpl->df_next;
1858 while (dn != dp->df_next)
1859 {
1860 dpl = dn->df_next;
1861 vim_free(dn);
1862 dn = dpl;
1863 }
1864 }
1865 else
1866 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001867 // Allocate a new diffblock.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001868 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001870 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Lewis Russelld9da86e2021-12-28 13:54:41 +00001872 dp->df_lnum[idx_orig] = hunk->lnum_orig;
1873 dp->df_count[idx_orig] = hunk->count_orig;
1874 dp->df_lnum[idx_new] = hunk->lnum_new;
1875 dp->df_count[idx_new] = hunk->count_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaare828b762018-09-10 17:51:58 +02001877 // Set values for other buffers, these must be equal to the
1878 // original buffer, otherwise there would have been a change
1879 // already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001881 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 diff_copy_entry(dprev, dp, idx_orig, i);
1883 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001884 notset = FALSE; // "*dp" has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
1886
Bram Moolenaare828b762018-09-10 17:51:58 +02001887 // for remaining diff blocks orig and new are equal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 while (dp != NULL)
1889 {
1890 if (notset)
1891 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1892 dprev = dp;
1893 dp = dp->df_next;
1894 notset = TRUE;
1895 }
1896
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001897done:
Lewis Russelld9da86e2021-12-28 13:54:41 +00001898 if (!dio->dio_internal)
1899 vim_free(hunk);
1900
Bram Moolenaare828b762018-09-10 17:51:58 +02001901 if (fd != NULL)
1902 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903}
1904
1905/*
1906 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1907 */
1908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001909diff_copy_entry(
1910 diff_T *dprev,
1911 diff_T *dp,
1912 int idx_orig,
1913 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
1915 long off;
1916
1917 if (dprev == NULL)
1918 off = 0;
1919 else
1920 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1921 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1922 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1923 dp->df_count[idx_new] = dp->df_count[idx_orig];
1924}
1925
1926/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001927 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001930diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 diff_T *p, *next_p;
1933
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001934 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936 next_p = p->df_next;
1937 vim_free(p);
1938 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001939 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941
1942/*
Jonathon7c7a4e62025-01-12 09:58:00 +01001943 * return true if the options are set to use diff linematch
1944 */
1945 static int
1946diff_linematch(diff_T *dp)
1947{
1948 if (!(diff_flags & DIFF_LINEMATCH))
1949 return 0;
1950
1951 // are there more than three diff buffers?
1952 int tsize = 0;
1953 for (int i = 0; i < DB_COUNT; i++)
1954 {
1955 if (curtab->tp_diffbuf[i] != NULL)
1956 {
1957 // for the rare case (bug?) that the count of a diff block is
1958 // negative, do not run the algorithm because this will try to
1959 // allocate a negative amount of space and crash
1960 if (dp->df_count[i] < 0)
1961 return FALSE;
1962 tsize += dp->df_count[i];
1963 }
1964 }
1965
1966 // avoid allocating a huge array because it will lag
1967 return tsize <= linematch_lines;
1968}
1969
1970 static int
1971get_max_diff_length(const diff_T *dp)
1972{
1973 int maxlength = 0;
1974
1975 for (int k = 0; k < DB_COUNT; k++)
1976 {
1977 if (curtab->tp_diffbuf[k] != NULL)
1978 {
1979 if (dp->df_count[k] > maxlength)
1980 maxlength = dp->df_count[k];
1981 }
1982 }
1983 return maxlength;
1984}
1985
1986 static void
1987find_top_diff_block(
1988 diff_T **thistopdiff,
1989 diff_T **nextblockblock,
1990 int fromidx,
1991 int topline)
1992{
1993 diff_T *topdiff = NULL;
1994 diff_T *localtopdiff = NULL;
1995 int topdiffchange = 0;
1996
1997 for (topdiff = curtab->tp_first_diff; topdiff != NULL;
1998 topdiff = topdiff->df_next)
1999 {
2000 // set the top of the current overlapping diff block set as we
2001 // iterate through all of the sets of overlapping diff blocks
2002 if (!localtopdiff || topdiffchange)
2003 {
2004 localtopdiff = topdiff;
2005 topdiffchange = 0;
2006 }
2007
2008 // check if the fromwin topline is matched by the current diff. if so,
2009 // set it to the top of the diff block
2010 if (topline >= topdiff->df_lnum[fromidx] && topline <=
2011 (topdiff->df_lnum[fromidx] + topdiff->df_count[fromidx]))
2012 {
2013 // this line is inside the current diff block, so we will save the
2014 // top block of the set of blocks to refer to later
2015 if ((*thistopdiff) == NULL)
2016 (*thistopdiff) = localtopdiff;
2017 }
2018
2019 // check if the next set of overlapping diff blocks is next
2020 if (!(topdiff->df_next && (topdiff->df_next->df_lnum[fromidx] ==
2021 (topdiff->df_lnum[fromidx] +
2022 topdiff->df_count[fromidx]))))
2023 {
2024 // mark that the next diff block is belongs to a different set of
2025 // overlapping diff blocks
2026 topdiffchange = 1;
2027
2028 // if we already have found that the line number is inside a diff
2029 // block, set the marker of the next block and finish the iteration
2030 if (*thistopdiff)
2031 {
2032 (*nextblockblock) = topdiff->df_next;
2033 break;
2034 }
2035 }
2036 }
2037}
2038
2039 static void
2040count_filler_lines_and_topline(
2041 int *curlinenum_to,
2042 int *linesfiller,
2043 const diff_T *thistopdiff,
2044 const int toidx,
2045 int virtual_lines_passed)
2046{
2047 const diff_T *curdif = thistopdiff;
2048 int ch_virtual_lines = 0;
2049 int isfiller = FALSE;
2050
2051 while (virtual_lines_passed > 0)
2052 {
2053 if (ch_virtual_lines)
2054 {
2055 virtual_lines_passed--;
2056 ch_virtual_lines--;
2057 if (!isfiller)
2058 (*curlinenum_to)++;
2059 else
2060 (*linesfiller)++;
2061 }
2062 else
2063 {
2064 (*linesfiller) = 0;
Christian Brabandta9f77be2025-01-16 19:06:57 +01002065 if (curdif)
2066 {
2067 ch_virtual_lines = get_max_diff_length(curdif);
2068 isfiller = (curdif->df_count[toidx] ? FALSE : TRUE);
2069 }
Jonathon7c7a4e62025-01-12 09:58:00 +01002070 if (isfiller)
2071 {
2072 while (curdif && curdif->df_next &&
2073 curdif->df_lnum[toidx] ==
2074 curdif->df_next->df_lnum[toidx] &&
2075 curdif->df_next->df_count[toidx] == 0)
2076 {
2077 curdif = curdif->df_next;
2078 ch_virtual_lines += get_max_diff_length(curdif);
2079 }
2080 }
2081 if (curdif)
2082 curdif = curdif->df_next;
2083 }
2084 }
2085}
2086
2087 static void
2088calculate_topfill_and_topline(
2089 const int fromidx,
2090 const int toidx,
2091 const int from_topline,
2092 const int from_topfill,
2093 int *topfill,
2094 linenr_T *topline)
2095{
2096 // 1. find the position from the top of the diff block, and the start
2097 // of the next diff block
2098 diff_T *thistopdiff = NULL;
2099 diff_T *nextblockblock = NULL;
2100 int virtual_lines_passed = 0;
2101
2102 find_top_diff_block(&thistopdiff, &nextblockblock, fromidx, from_topline);
2103
2104 // count the virtual lines that have been passed
2105 diff_T *curdif = thistopdiff;
2106 while (curdif && (curdif->df_lnum[fromidx] + curdif->df_count[fromidx])
2107 <= from_topline)
2108 {
2109 virtual_lines_passed += get_max_diff_length(curdif);
2110
2111 curdif = curdif->df_next;
2112 }
2113
2114 if (curdif != nextblockblock)
2115 virtual_lines_passed += from_topline - curdif->df_lnum[fromidx];
2116 virtual_lines_passed -= from_topfill;
2117
2118 // count the same amount of virtual lines in the toidx buffer
2119 int curlinenum_to = thistopdiff->df_lnum[toidx];
2120 int linesfiller = 0;
2121
2122 count_filler_lines_and_topline(&curlinenum_to, &linesfiller, thistopdiff,
2123 toidx, virtual_lines_passed);
2124
2125 // count the number of filler lines that would normally be above this line
2126 int maxfiller = 0;
2127 for (diff_T *dpfillertest = thistopdiff; dpfillertest != NULL;
2128 dpfillertest = dpfillertest->df_next)
2129 {
2130 if (dpfillertest->df_lnum[toidx] == curlinenum_to)
2131 {
2132 while (dpfillertest && dpfillertest->df_lnum[toidx] ==
2133 curlinenum_to)
2134 {
2135 maxfiller += dpfillertest->df_count[toidx] ? 0 :
2136 get_max_diff_length(dpfillertest);
2137 dpfillertest = dpfillertest->df_next;
2138 }
2139 break;
2140 }
2141 }
2142 (*topfill) = maxfiller - linesfiller;
2143 (*topline) = curlinenum_to;
2144}
2145
2146 static int
2147linematched_filler_lines(diff_T *dp, int idx, linenr_T lnum, int *linestatus)
2148{
2149 int filler_lines_d1 = 0;
2150
2151 while (dp && dp->df_next &&
2152 lnum == (dp->df_lnum[idx] + dp->df_count[idx]) &&
2153 dp->df_next->df_lnum[idx] == lnum)
2154 {
2155 if (dp->df_count[idx] == 0)
2156 filler_lines_d1 += get_max_diff_length(dp);
2157 dp = dp->df_next;
2158 }
2159
2160 if (dp->df_count[idx] == 0)
2161 filler_lines_d1 += get_max_diff_length(dp);
2162
2163 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
2164 {
2165 int j = 0;
2166
2167 for (int i = 0; i < DB_COUNT; i++)
2168 {
2169 if (curtab->tp_diffbuf[i] != NULL)
2170 {
2171 if (dp->df_count[i])
2172 j++;
2173 }
2174 // is this an added line or a changed line?
2175 if (linestatus)
2176 (*linestatus) = (j == 1) ? -2 : -1;
2177 }
2178 }
2179
2180 return filler_lines_d1;
2181}
2182
2183// Apply results from the linematch algorithm and apply to 'dp' by splitting it
2184// into multiple adjacent diff blocks.
2185 static void
2186apply_linematch_results(
2187 diff_T *dp,
2188 size_t decisions_length,
2189 const int *decisions)
2190{
2191 // get the start line number here in each diff buffer, and then increment
2192 int line_numbers[DB_COUNT];
2193 int outputmap[DB_COUNT];
2194 size_t ndiffs = 0;
2195
2196 for (int i = 0; i < DB_COUNT; i++)
2197 {
2198 if (curtab->tp_diffbuf[i] != NULL)
2199 {
2200 line_numbers[i] = dp->df_lnum[i];
2201 dp->df_count[i] = 0;
2202
2203 // Keep track of the index of the diff buffer we are using here.
2204 // We will use this to write the output of the algorithm to
2205 // diff_T structs at the correct indexes
2206 outputmap[ndiffs] = i;
2207 ndiffs++;
2208 }
2209 }
2210
2211 // write the diffs starting with the current diff block
2212 diff_T *dp_s = dp;
2213 for (size_t i = 0; i < decisions_length; i++)
2214 {
2215 // Don't allocate on first iter since we can reuse the initial
2216 // diffblock
2217 if (i != 0 && (decisions[i - 1] != decisions[i]))
2218 {
2219 // create new sub diff blocks to segment the original diff block
2220 // which we further divided by running the linematch algorithm
2221 dp_s = diff_alloc_new(curtab, dp_s, dp_s->df_next);
2222 dp_s->is_linematched = TRUE;
2223 for (int j = 0; j < DB_COUNT; j++)
2224 {
2225 if (curtab->tp_diffbuf[j] != NULL)
2226 {
2227 dp_s->df_lnum[j] = line_numbers[j];
2228 dp_s->df_count[j] = 0;
2229 }
2230 }
2231 }
2232 for (size_t j = 0; j < ndiffs; j++)
2233 {
2234 if (decisions[i] & (1 << j))
2235 {
2236 // will need to use the map here
2237 dp_s->df_count[outputmap[j]]++;
2238 line_numbers[outputmap[j]]++;
2239 }
2240 }
2241 }
2242 dp->is_linematched = TRUE;
2243}
2244
2245 static void
2246run_linematch_algorithm(diff_T *dp)
2247{
2248 // define buffers for diff algorithm
2249 diffin_T diffbufs_mm[DB_COUNT];
2250 const mmfile_t *diffbufs[DB_COUNT];
2251 int diff_length[DB_COUNT];
2252 size_t ndiffs = 0;
2253
2254 for (int i = 0; i < DB_COUNT; i++)
2255 {
2256 if (curtab->tp_diffbuf[i] != NULL)
2257 {
2258 // write the contents of the entire buffer to
2259 // diffbufs_mm[diffbuffers_count]
2260 if (dp->df_count[i] > 0)
2261 {
2262 diff_write_buffer(curtab->tp_diffbuf[i], &diffbufs_mm[ndiffs],
2263 dp->df_lnum[i], dp->df_lnum[i] + dp->df_count[i] - 1);
2264 }
2265 else
2266 {
2267 diffbufs_mm[ndiffs].din_mmfile.size = 0;
2268 diffbufs_mm[ndiffs].din_mmfile.ptr = NULL;
2269 }
2270
2271 diffbufs[ndiffs] = &diffbufs_mm[ndiffs].din_mmfile;
2272
2273 // keep track of the length of this diff block to pass it to the
2274 // linematch algorithm
2275 diff_length[ndiffs] = dp->df_count[i];
2276
2277 // increment the amount of diff buffers we are passing to the
2278 // algorithm
2279 ndiffs++;
2280 }
2281 }
2282
2283 // we will get the output of the linematch algorithm in the format of an
2284 // array of integers (*decisions) and the length of that array
2285 // (decisions_length)
2286 int *decisions = NULL;
2287 const int iwhite = (diff_flags & (DIFF_IWHITEALL | DIFF_IWHITE)) > 0 ? 1 : 0;
2288 size_t decisions_length =
2289 linematch_nbuffers(diffbufs, diff_length, ndiffs, &decisions, iwhite);
2290
2291 for (size_t i = 0; i < ndiffs; i++)
2292 free(diffbufs_mm[i].din_mmfile.ptr); // TODO should this be vim_free ?
2293
2294 apply_linematch_results(dp, decisions_length, decisions);
2295
2296 free(decisions);
2297}
2298
2299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * Check diff status for line "lnum" in buffer "buf":
2301 * Returns 0 for nothing special
2302 * Returns -1 for a line that should be highlighted as changed.
2303 * Returns -2 for a line that should be highlighted as added/deleted.
2304 * Returns > 0 for inserting that many filler lines above it (never happens
2305 * when 'diffopt' doesn't contain "filler").
2306 * This should only be used for windows where 'diff' is set.
Jonathon7c7a4e62025-01-12 09:58:00 +01002307 * When diffopt contains linematch, a changed/added/deleted line
2308 * may also have filler lines above it. In such a case, the possibilities
2309 * are no longer mutually exclusive. The number of filler lines is
2310 * returned from diff_check, and the integer 'linestatus' passed by
2311 * pointer is set to -1 to indicate a changed line, and -2 to indicate an
2312 * added line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 */
2314 int
Jonathon7c7a4e62025-01-12 09:58:00 +01002315diff_check_with_linestatus(win_T *wp, linenr_T lnum, int *linestatus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 int idx; // index in tp_diffbuf[] for this buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 diff_T *dp;
2319 int maxcount;
2320 int i;
2321 buf_T *buf = wp->w_buffer;
2322 int cmp;
2323
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002324 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002325 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002327 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) // no diffs at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 return 0;
2329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002330 // safety check: "lnum" must be a buffer line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
2332 return 0;
2333
2334 idx = diff_buf_idx(buf);
2335 if (idx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002336 return 0; // no diffs for buffer "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338#ifdef FEAT_FOLDING
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002339 // A closed fold never has filler lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
2341 return 0;
2342#endif
2343
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002344 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002345 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2347 break;
2348 if (dp == NULL || lnum < dp->df_lnum[idx])
2349 return 0;
2350
Jonathon7c7a4e62025-01-12 09:58:00 +01002351 // Don't run linematch when lnum is offscreen. Useful for scrollbind
2352 // calculations which need to count all the filler lines above the screen.
2353 if (lnum >= wp->w_topline && lnum < wp->w_botline
2354 && !dp->is_linematched && diff_linematch(dp))
2355 run_linematch_algorithm(dp);
2356
2357 if (dp->is_linematched)
2358 return linematched_filler_lines(dp, idx, lnum, linestatus);
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
2361 {
2362 int zero = FALSE;
2363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002364 // Changed or inserted line. If the other buffers have a count of
2365 // zero, the lines were inserted. If the other buffers have the same
2366 // count, check if the lines are identical.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 cmp = FALSE;
2368 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002369 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 if (dp->df_count[i] == 0)
2372 zero = TRUE;
2373 else
2374 {
2375 if (dp->df_count[i] != dp->df_count[idx])
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 return -1; // nr of lines changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 cmp = TRUE;
2378 }
2379 }
2380 if (cmp)
2381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 // Compare all lines. If they are equal the lines were inserted
2383 // in some buffers, deleted in others, but not changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002385 if (i != idx && curtab->tp_diffbuf[i] != NULL
2386 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (!diff_equal_entry(dp, idx, i))
2388 return -1;
2389 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002390 // If there is no buffer with zero lines then there is no difference
2391 // any longer. Happens when making a change (or undo) that removes
2392 // the difference. Can't remove the entry here, we might be halfway
2393 // updating the window. Just report the text as unchanged. Other
2394 // windows might still show the change though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (zero == FALSE)
2396 return 0;
2397 return -2;
2398 }
2399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002400 // If 'diffopt' doesn't contain "filler", return 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (!(diff_flags & DIFF_FILLER))
2402 return 0;
2403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002404 // Insert filler lines above the line just below the change. Will return
2405 // 0 when this buf had the max count.
Jonathon7c7a4e62025-01-12 09:58:00 +01002406 maxcount = get_max_diff_length(dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 return maxcount - dp->df_count[idx];
2408}
2409
Jonathon7c7a4e62025-01-12 09:58:00 +01002410 int
2411diff_check(win_T *wp, linenr_T lnum)
2412{
2413 return diff_check_with_linestatus(wp, lnum, NULL);
2414}
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416/*
2417 * Compare two entries in diff "*dp" and return TRUE if they are equal.
2418 */
2419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002420diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 int i;
2423 char_u *line;
2424 int cmp;
2425
2426 if (dp->df_count[idx1] != dp->df_count[idx2])
2427 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002428 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return FALSE;
2430 for (i = 0; i < dp->df_count[idx1]; ++i)
2431 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002432 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 dp->df_lnum[idx1] + i, FALSE));
2434 if (line == NULL)
2435 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002436 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 dp->df_lnum[idx2] + i, FALSE));
2438 vim_free(line);
2439 if (cmp != 0)
2440 return FALSE;
2441 }
2442 return TRUE;
2443}
2444
2445/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002446 * Compare the characters at "p1" and "p2". If they are equal (possibly
2447 * ignoring case) return TRUE and set "len" to the number of bytes.
2448 */
2449 static int
2450diff_equal_char(char_u *p1, char_u *p2, int *len)
2451{
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002452 int l = (*mb_ptr2len)(p1);
2453
2454 if (l != (*mb_ptr2len)(p2))
2455 return FALSE;
2456 if (l > 1)
2457 {
2458 if (STRNCMP(p1, p2, l) != 0
2459 && (!enc_utf8
2460 || !(diff_flags & DIFF_ICASE)
2461 || utf_fold(utf_ptr2char(p1))
2462 != utf_fold(utf_ptr2char(p2))))
2463 return FALSE;
2464 *len = l;
2465 }
2466 else
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002467 {
2468 if ((*p1 != *p2)
2469 && (!(diff_flags & DIFF_ICASE)
2470 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
2471 return FALSE;
2472 *len = 1;
2473 }
2474 return TRUE;
2475}
2476
2477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 * Compare strings "s1" and "s2" according to 'diffopt'.
2479 * Return non-zero when they are different.
2480 */
2481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002482diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaar785fc652018-09-15 19:17:38 +02002487 if ((diff_flags & DIFF_IBLANK)
2488 && (*skipwhite(s1) == NUL || *skipwhite(s2) == NUL))
2489 return 0;
2490
2491 if ((diff_flags & (DIFF_ICASE | ALL_WHITE_DIFF)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 return STRCMP(s1, s2);
Bram Moolenaar785fc652018-09-15 19:17:38 +02002493 if ((diff_flags & DIFF_ICASE) && !(diff_flags & ALL_WHITE_DIFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 return MB_STRICMP(s1, s2);
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 p1 = s1;
2497 p2 = s2;
Bram Moolenaar785fc652018-09-15 19:17:38 +02002498
2499 // Ignore white space changes and possibly ignore case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 while (*p1 != NUL && *p2 != NUL)
2501 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002502 if (((diff_flags & DIFF_IWHITE)
2503 && VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
2504 || ((diff_flags & DIFF_IWHITEALL)
2505 && (VIM_ISWHITE(*p1) || VIM_ISWHITE(*p2))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 p1 = skipwhite(p1);
2508 p2 = skipwhite(p2);
2509 }
2510 else
2511 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002512 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002514 p1 += l;
2515 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 }
2518
Bram Moolenaar785fc652018-09-15 19:17:38 +02002519 // Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 p1 = skipwhite(p1);
2521 p2 = skipwhite(p2);
2522 if (*p1 != NUL || *p2 != NUL)
2523 return 1;
2524 return 0;
2525}
2526
2527/*
2528 * Return the number of filler lines above "lnum".
2529 */
2530 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002531diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533 int n;
2534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002535 // be quick when there are no filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (!(diff_flags & DIFF_FILLER))
2537 return 0;
2538 n = diff_check(wp, lnum);
2539 if (n <= 0)
2540 return 0;
2541 return n;
2542}
2543
2544/*
2545 * Set the topline of "towin" to match the position in "fromwin", so that they
2546 * show the same diff'ed lines.
2547 */
2548 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002551 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002553 int fromidx;
2554 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002556 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 int i;
2558
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002559 fromidx = diff_buf_idx(frombuf);
2560 if (fromidx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002561 return; // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002563 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002564 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 towin->w_topfill = 0;
2567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002568 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002569 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002570 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 break;
2572 if (dp == NULL)
2573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002574 // After last change, compute topline relative to end of file; no
2575 // filler lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002577 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579 else
2580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002581 // Find index for "towin".
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002582 toidx = diff_buf_idx(towin->w_buffer);
2583 if (toidx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002584 return; // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002586 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2587 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Jonathon7c7a4e62025-01-12 09:58:00 +01002589 if (dp->is_linematched)
2590 {
2591 calculate_topfill_and_topline(fromidx, toidx,
2592 fromwin->w_topline,
2593 fromwin->w_topfill,
2594 &towin->w_topfill,
2595 &towin->w_topline);
2596 }
2597 else
2598 {
2599 // Inside a change: compute filler lines. With three or more
2600 // buffers we need to know the largest count.
2601 max_count = 0;
2602 for (i = 0; i < DB_COUNT; ++i)
2603 if (curtab->tp_diffbuf[i] != NULL
2604 && max_count < dp->df_count[i])
2605 max_count = dp->df_count[i];
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002606
Jonathon7c7a4e62025-01-12 09:58:00 +01002607 if (dp->df_count[toidx] == dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Jonathon7c7a4e62025-01-12 09:58:00 +01002609 // same number of lines: use same filler count
2610 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002611 }
Jonathon7c7a4e62025-01-12 09:58:00 +01002612 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002613 {
2614 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Jonathon7c7a4e62025-01-12 09:58:00 +01002615 {
2616 // more lines in towin and fromwin doesn't show diff
2617 // lines, only filler lines
2618 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2619 {
2620 // towin also only shows filler lines
2621 towin->w_topline = dp->df_lnum[toidx]
2622 + dp->df_count[toidx];
2623 towin->w_topfill = fromwin->w_topfill;
2624 }
2625 else
2626 // towin still has some diff lines to show
2627 towin->w_topline = dp->df_lnum[toidx]
2628 + max_count - fromwin->w_topfill;
2629 }
2630 }
2631 else if (towin->w_topline >= dp->df_lnum[toidx]
2632 + dp->df_count[toidx])
2633 {
2634 // less lines in towin and no diff lines to show: compute
2635 // filler lines
2636 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2637 if (diff_flags & DIFF_FILLER)
2638 {
2639 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2640 // fromwin is also out of diff lines
2641 towin->w_topfill = fromwin->w_topfill;
2642 else
2643 // fromwin has some diff lines
2644 towin->w_topfill = dp->df_lnum[fromidx] +
2645 max_count - lnum;
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 }
2649 }
2650 }
2651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 // safety check (if diff info gets outdated strange things may happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 towin->w_botfill = FALSE;
2654 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2655 {
2656 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2657 towin->w_botfill = TRUE;
2658 }
2659 if (towin->w_topline < 1)
2660 {
2661 towin->w_topline = 1;
2662 towin->w_topfill = 0;
2663 }
2664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002665 // When w_topline changes need to recompute w_botline and cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 invalidate_botline_win(towin);
2667 changed_line_abv_curs_win(towin);
2668
2669 check_topfill(towin, FALSE);
2670#ifdef FEAT_FOLDING
2671 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2672 NULL, TRUE, NULL);
2673#endif
2674}
2675
2676/*
2677 * This is called when 'diffopt' is changed.
2678 */
2679 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002680diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 char_u *p;
2683 int diff_context_new = 6;
Jonathon7c7a4e62025-01-12 09:58:00 +01002684 int linematch_lines_new = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002686 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002687 long diff_algorithm_new = 0;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002688 long diff_indent_heuristic = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002689 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 p = p_dip;
2692 while (*p != NUL)
2693 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002694 // Note: Keep this in sync with p_dip_values
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (STRNCMP(p, "filler", 6) == 0)
2696 {
2697 p += 6;
2698 diff_flags_new |= DIFF_FILLER;
2699 }
2700 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2701 {
2702 p += 8;
2703 diff_context_new = getdigits(&p);
2704 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002705 else if (STRNCMP(p, "iblank", 6) == 0)
2706 {
2707 p += 6;
2708 diff_flags_new |= DIFF_IBLANK;
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 else if (STRNCMP(p, "icase", 5) == 0)
2711 {
2712 p += 5;
2713 diff_flags_new |= DIFF_ICASE;
2714 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002715 else if (STRNCMP(p, "iwhiteall", 9) == 0)
2716 {
2717 p += 9;
2718 diff_flags_new |= DIFF_IWHITEALL;
2719 }
2720 else if (STRNCMP(p, "iwhiteeol", 9) == 0)
2721 {
2722 p += 9;
2723 diff_flags_new |= DIFF_IWHITEEOL;
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 else if (STRNCMP(p, "iwhite", 6) == 0)
2726 {
2727 p += 6;
2728 diff_flags_new |= DIFF_IWHITE;
2729 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002730 else if (STRNCMP(p, "horizontal", 10) == 0)
2731 {
2732 p += 10;
2733 diff_flags_new |= DIFF_HORIZONTAL;
2734 }
2735 else if (STRNCMP(p, "vertical", 8) == 0)
2736 {
2737 p += 8;
2738 diff_flags_new |= DIFF_VERTICAL;
2739 }
2740 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2741 {
2742 p += 11;
2743 diff_foldcolumn_new = getdigits(&p);
2744 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002745 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2746 {
2747 p += 9;
2748 diff_flags_new |= DIFF_HIDDEN_OFF;
2749 }
Bram Moolenaarc8234772019-11-10 21:00:27 +01002750 else if (STRNCMP(p, "closeoff", 8) == 0)
2751 {
2752 p += 8;
2753 diff_flags_new |= DIFF_CLOSE_OFF;
2754 }
Bram Moolenaar4223d432021-02-10 13:18:17 +01002755 else if (STRNCMP(p, "followwrap", 10) == 0)
2756 {
2757 p += 10;
2758 diff_flags_new |= DIFF_FOLLOWWRAP;
2759 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002760 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2761 {
2762 p += 16;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002763 diff_indent_heuristic = XDF_INDENT_HEURISTIC;
Bram Moolenaare828b762018-09-10 17:51:58 +02002764 }
2765 else if (STRNCMP(p, "internal", 8) == 0)
2766 {
2767 p += 8;
2768 diff_flags_new |= DIFF_INTERNAL;
2769 }
2770 else if (STRNCMP(p, "algorithm:", 10) == 0)
2771 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002772 // Note: Keep this in sync with p_dip_algorithm_values.
Bram Moolenaare828b762018-09-10 17:51:58 +02002773 p += 10;
2774 if (STRNCMP(p, "myers", 5) == 0)
2775 {
2776 p += 5;
2777 diff_algorithm_new = 0;
2778 }
2779 else if (STRNCMP(p, "minimal", 7) == 0)
2780 {
2781 p += 7;
2782 diff_algorithm_new = XDF_NEED_MINIMAL;
2783 }
2784 else if (STRNCMP(p, "patience", 8) == 0)
2785 {
2786 p += 8;
2787 diff_algorithm_new = XDF_PATIENCE_DIFF;
2788 }
2789 else if (STRNCMP(p, "histogram", 9) == 0)
2790 {
2791 p += 9;
2792 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2793 }
Bram Moolenaard0721052018-11-05 21:21:33 +01002794 else
2795 return FAIL;
Bram Moolenaare828b762018-09-10 17:51:58 +02002796 }
Jonathon7c7a4e62025-01-12 09:58:00 +01002797 else if (STRNCMP(p, "linematch:", 10) == 0 && VIM_ISDIGIT(p[11]))
2798 {
2799 p += 10;
2800 linematch_lines_new = getdigits(&p);
2801 diff_flags_new |= DIFF_LINEMATCH;
2802 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (*p != ',' && *p != NUL)
2805 return FAIL;
2806 if (*p == ',')
2807 ++p;
2808 }
2809
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002810 diff_algorithm_new |= diff_indent_heuristic;
2811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002812 // Can't have both "horizontal" and "vertical".
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002813 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2814 return FAIL;
2815
Bram Moolenaar198fa062018-09-18 21:20:26 +02002816 // If flags were added or removed, or the algorithm was changed, need to
2817 // update the diff.
Bram Moolenaare828b762018-09-10 17:51:58 +02002818 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002819 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002820 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
2822 diff_flags = diff_flags_new;
Bram Moolenaarb9ddda62019-02-19 23:00:50 +01002823 diff_context = diff_context_new == 0 ? 1 : diff_context_new;
Jonathon7c7a4e62025-01-12 09:58:00 +01002824 linematch_lines = linematch_lines_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002825 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002826 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 diff_redraw(TRUE);
2829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 // recompute the scroll binding with the new option value, may
2831 // remove or add filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 check_scrollbind((linenr_T)0, 0L);
2833
2834 return OK;
2835}
2836
2837/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002838 * Return TRUE if 'diffopt' contains "horizontal".
2839 */
2840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002842{
2843 return (diff_flags & DIFF_HORIZONTAL) != 0;
2844}
2845
2846/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002847 * Return TRUE if 'diffopt' contains "hiddenoff".
2848 */
2849 int
2850diffopt_hiddenoff(void)
2851{
2852 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2853}
2854
2855/*
Bram Moolenaarc8234772019-11-10 21:00:27 +01002856 * Return TRUE if 'diffopt' contains "closeoff".
2857 */
2858 int
2859diffopt_closeoff(void)
2860{
2861 return (diff_flags & DIFF_CLOSE_OFF) != 0;
2862}
2863
2864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * Find the difference within a changed line.
2866 * Returns TRUE if the line was added, no other buffer has it.
2867 */
2868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869diff_find_change(
2870 win_T *wp,
2871 linenr_T lnum,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 int *startp, // first char of the change
2873 int *endp) // last char of the change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 char_u *line_org;
2876 char_u *line_new;
2877 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002878 int si_org, si_new;
2879 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 diff_T *dp;
2881 int idx;
2882 int off;
2883 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002884 char_u *p1, *p2;
2885 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002887 // Make a copy of the line, the next ml_get() will invalidate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2889 if (line_org == NULL)
2890 return FALSE;
2891
2892 idx = diff_buf_idx(wp->w_buffer);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002893 if (idx == DB_COUNT) // cannot happen
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002894 {
2895 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002899 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002900 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2902 break;
Jonathon7c7a4e62025-01-12 09:58:00 +01002903 if (dp->is_linematched)
2904 {
2905 while (dp && dp->df_next
2906 && lnum == dp->df_count[idx] + dp->df_lnum[idx]
2907 && dp->df_next->df_lnum[idx] == lnum)
2908 dp = dp->df_next;
2909 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002910 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002911 {
2912 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
2916 off = lnum - dp->df_lnum[idx];
2917
2918 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002919 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002921 // Skip lines that are not in the other change (filler lines).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (off >= dp->df_count[i])
2923 continue;
2924 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002925 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2926 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002928 // Search for start of difference
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002929 si_org = si_new = 0;
2930 while (line_org[si_org] != NUL)
2931 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002932 if (((diff_flags & DIFF_IWHITE)
2933 && VIM_ISWHITE(line_org[si_org])
2934 && VIM_ISWHITE(line_new[si_new]))
2935 || ((diff_flags & DIFF_IWHITEALL)
2936 && (VIM_ISWHITE(line_org[si_org])
2937 || VIM_ISWHITE(line_new[si_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002938 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002939 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2940 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002941 }
2942 else
2943 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002944 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2945 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002946 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002947 si_org += l;
2948 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002949 }
2950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (has_mbyte)
2952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002953 // Move back to first byte of character in both lines (may
2954 // have "nn^" in line_org and "n^ in line_new).
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002955 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2956 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002958 if (*startp > si_org)
2959 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002961 // Search for end of difference, if any.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002962 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 {
2964 ei_org = (int)STRLEN(line_org);
2965 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002966 while (ei_org >= *startp && ei_new >= si_new
2967 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002969 if (((diff_flags & DIFF_IWHITE)
2970 && VIM_ISWHITE(line_org[ei_org])
2971 && VIM_ISWHITE(line_new[ei_new]))
2972 || ((diff_flags & DIFF_IWHITEALL)
2973 && (VIM_ISWHITE(line_org[ei_org])
2974 || VIM_ISWHITE(line_new[ei_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002975 {
2976 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002977 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002978 --ei_org;
2979 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002980 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002981 --ei_new;
2982 }
2983 else
2984 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002985 p1 = line_org + ei_org;
2986 p2 = line_new + ei_new;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002987 p1 -= (*mb_head_off)(line_org, p1);
2988 p2 -= (*mb_head_off)(line_new, p2);
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002989 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002990 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002991 ei_org -= l;
2992 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 if (*endp < ei_org)
2996 *endp = ei_org;
2997 }
2998 }
2999
3000 vim_free(line_org);
3001 return added;
3002}
3003
3004#if defined(FEAT_FOLDING) || defined(PROTO)
3005/*
3006 * Return TRUE if line "lnum" is not close to a diff block, this line should
3007 * be in a fold.
3008 * Return FALSE if there are no diff blocks at all in this window.
3009 */
3010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003011diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 int i;
3014 int idx = -1;
3015 int other = FALSE;
3016 diff_T *dp;
3017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003018 // Return if 'diff' isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (!wp->w_p_diff)
3020 return FALSE;
3021
3022 for (i = 0; i < DB_COUNT; ++i)
3023 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003024 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003026 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 other = TRUE;
3028 }
3029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003030 // return here if there are no diffs in the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (idx == -1 || !other)
3032 return FALSE;
3033
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003034 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003037 // Return if there are no diff blocks. All lines will be folded.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003038 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 return TRUE;
3040
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003041 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003043 // If this change is below the line there can't be any further match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (dp->df_lnum[idx] - diff_context > lnum)
3045 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003046 // If this change ends before the line we have a match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
3048 return FALSE;
3049 }
3050 return TRUE;
3051}
3052#endif
3053
3054/*
3055 * "dp" and "do" commands.
3056 */
3057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003058nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01003061 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
Bram Moolenaarf2732452018-06-03 14:47:35 +02003063#ifdef FEAT_JOB_CHANNEL
3064 if (bt_prompt(curbuf))
3065 {
3066 vim_beep(BO_OPER);
3067 return;
3068 }
3069#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01003070 if (count == 0)
3071 ea.arg = (char_u *)"";
3072 else
3073 {
3074 vim_snprintf((char *)buf, 30, "%ld", count);
3075 ea.arg = buf;
3076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if (put)
3078 ea.cmdidx = CMD_diffput;
3079 else
3080 ea.cmdidx = CMD_diffget;
3081 ea.addr_count = 0;
3082 ea.line1 = curwin->w_cursor.lnum;
3083 ea.line2 = curwin->w_cursor.lnum;
3084 ex_diffgetput(&ea);
3085}
3086
3087/*
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003088 * Return TRUE if "diff" appears in the list of diff blocks of the current tab.
3089 */
3090 static int
3091valid_diff(diff_T *diff)
3092{
3093 diff_T *dp;
3094
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003095 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003096 if (dp == diff)
3097 return TRUE;
3098 return FALSE;
3099}
3100
3101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * ":diffget"
3103 * ":diffput"
3104 */
3105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003106ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 linenr_T lnum;
3109 int count;
3110 linenr_T off = 0;
3111 diff_T *dp;
3112 diff_T *dprev;
3113 diff_T *dfree;
3114 int idx_cur;
3115 int idx_other;
3116 int idx_from;
3117 int idx_to;
3118 int i;
3119 int added;
3120 char_u *p;
3121 aco_save_T aco;
3122 buf_T *buf;
3123 int start_skip, end_skip;
3124 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003125 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00003126 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003128 // Find the current buffer in the list of diff buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 idx_cur = diff_buf_idx(curbuf);
3130 if (idx_cur == DB_COUNT)
3131 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003132 emsg(_(e_current_buffer_is_not_in_diff_mode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 return;
3134 }
3135
3136 if (*eap->arg == NUL)
3137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003138 // No argument: Find the other buffer in the list of diff buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003140 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00003141 && curtab->tp_diffbuf[idx_other] != NULL)
3142 {
3143 if (eap->cmdidx != CMD_diffput
3144 || curtab->tp_diffbuf[idx_other]->b_p_ma)
3145 break;
3146 found_not_ma = TRUE;
3147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (idx_other == DB_COUNT)
3149 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00003150 if (found_not_ma)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003151 emsg(_(e_no_other_buffer_in_diff_mode_is_modifiable));
Bram Moolenaar602eb742007-02-20 03:43:38 +00003152 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003153 emsg(_(e_no_other_buffer_in_diff_mode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return;
3155 }
3156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003157 // Check that there isn't a third buffer in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003159 if (curtab->tp_diffbuf[i] != curbuf
3160 && curtab->tp_diffbuf[i] != NULL
3161 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003163 emsg(_(e_more_than_two_buffers_in_diff_mode_dont_know_which_one_to_use));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 return;
3165 }
3166 }
3167 else
3168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003169 // Buffer number or pattern given. Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003171 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 --p;
3173 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
3174 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003175 if (eap->arg + i == p) // digits only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 i = atol((char *)eap->arg);
3177 else
3178 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01003179 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (i < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003181 return; // error message already given
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 buf = buflist_findnr(i);
3184 if (buf == NULL)
3185 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003186 semsg(_(e_cant_find_buffer_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 return;
3188 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00003189 if (buf == curbuf)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003190 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 idx_other = diff_buf_idx(buf);
3192 if (idx_other == DB_COUNT)
3193 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003194 semsg(_(e_buffer_str_is_not_in_diff_mode), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 return;
3196 }
3197 }
3198
3199 diff_busy = TRUE;
3200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003201 // When no range given include the line above or below the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (eap->addr_count == 0)
3203 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003204 // Make it possible that ":diffget" on the last line gets line below
3205 // the cursor line when there is no difference above the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (eap->cmdidx == CMD_diffget
3207 && eap->line1 == curbuf->b_ml.ml_line_count
3208 && diff_check(curwin, eap->line1) == 0
3209 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
3210 ++eap->line2;
3211 else if (eap->line1 > 0)
3212 --eap->line1;
3213 }
3214
3215 if (eap->cmdidx == CMD_diffget)
3216 {
3217 idx_from = idx_other;
3218 idx_to = idx_cur;
3219 }
3220 else
3221 {
3222 idx_from = idx_cur;
3223 idx_to = idx_other;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003224 // Need to make the other buffer the current buffer to be able to make
3225 // changes in it.
Bram Moolenaare76062c2022-11-28 18:51:43 +00003226 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003227 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaare76062c2022-11-28 18:51:43 +00003228 if (curbuf != curtab->tp_diffbuf[idx_other])
3229 // Could not find a window for this buffer, the rest is likely to
3230 // fail.
3231 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003234 // May give the warning for a changed buffer here, which can trigger the
3235 // FileChangedRO autocommand, which may do nasty things and mess
3236 // everything up.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003237 if (!curbuf->b_changed)
3238 {
3239 change_warning(0);
3240 if (diff_buf_idx(curbuf) != idx_to)
3241 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003242 emsg(_(e_buffer_changed_unexpectedly));
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003243 goto theend;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003244 }
3245 }
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003248 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
Jonathon7c7a4e62025-01-12 09:58:00 +01003250 if (!eap->addr_count)
3251 {
3252 // handle the case with adjacent diff blocks
3253 while (dp->is_linematched
3254 && dp->df_next
3255 && dp->df_next->df_lnum[idx_cur] == dp->df_lnum[idx_cur] +
3256 dp->df_count[idx_cur]
3257 && dp->df_next->df_lnum[idx_cur] == eap->line1 + off + 1)
3258 {
3259 dprev = dp;
3260 dp = dp->df_next;
3261 }
3262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (dp->df_lnum[idx_cur] > eap->line2 + off)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003264 break; // past the range that was specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 dfree = NULL;
3267 lnum = dp->df_lnum[idx_to];
3268 count = dp->df_count[idx_to];
3269 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
3270 && u_save(lnum - 1, lnum + count) != FAIL)
3271 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003272 // Inside the specified range and saving for undo worked.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 start_skip = 0;
3274 end_skip = 0;
3275 if (eap->addr_count > 0)
3276 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003277 // A range was specified: check if lines need to be skipped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
3279 if (start_skip > 0)
3280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003281 // range starts below start of current diff block
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (start_skip > count)
3283 {
3284 lnum += count;
3285 count = 0;
3286 }
3287 else
3288 {
3289 count -= start_skip;
3290 lnum += start_skip;
3291 }
3292 }
3293 else
3294 start_skip = 0;
3295
3296 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
3297 - (eap->line2 + off);
3298 if (end_skip > 0)
3299 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003300 // range ends above end of current/from diff block
3301 if (idx_cur == idx_from) // :diffput
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
3303 i = dp->df_count[idx_cur] - start_skip - end_skip;
3304 if (count > i)
3305 count = i;
3306 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003307 else // :diffget
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 count -= end_skip;
3310 end_skip = dp->df_count[idx_from] - start_skip - count;
3311 if (end_skip < 0)
3312 end_skip = 0;
3313 }
3314 }
3315 else
3316 end_skip = 0;
3317 }
3318
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003319 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 added = 0;
3321 for (i = 0; i < count; ++i)
3322 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003323 // remember deleting the last line of the buffer
Bram Moolenaar280f1262006-01-30 00:14:18 +00003324 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar4e677b92022-07-28 18:44:27 +01003325 if (ml_delete(lnum) == OK)
3326 --added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
3329 {
3330 linenr_T nr;
3331
3332 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003333 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003335 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
3336 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (p != NULL)
3338 {
3339 ml_append(lnum + i - 1, p, 0, FALSE);
3340 vim_free(p);
3341 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003342 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
3343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003344 // Added the first line into an empty buffer, need to
3345 // delete the dummy empty line.
Bram Moolenaar280f1262006-01-30 00:14:18 +00003346 buf_empty = FALSE;
Bram Moolenaarca70c072020-05-30 20:30:46 +02003347 ml_delete((linenr_T)2);
Bram Moolenaar280f1262006-01-30 00:14:18 +00003348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 }
3351 new_count = dp->df_count[idx_to] + added;
3352 dp->df_count[idx_to] = new_count;
3353
3354 if (start_skip == 0 && end_skip == 0)
3355 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003356 // Check if there are any other buffers and if the diff is
3357 // equal in them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003359 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
3360 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 && !diff_equal_entry(dp, idx_from, i))
3362 break;
3363 if (i == DB_COUNT)
3364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 // delete the diff entry, the buffers are now equal here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 dfree = dp;
3367 dp = dp->df_next;
3368 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003369 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
3371 dprev->df_next = dp;
3372 }
3373 }
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (added != 0)
3376 {
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003377 // Adjust marks. This will change the following entries!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
3379 if (curwin->w_cursor.lnum >= lnum)
3380 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003381 // Adjust the cursor position if it's in/after the changed
3382 // lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 if (curwin->w_cursor.lnum >= lnum + count)
3384 curwin->w_cursor.lnum += added;
3385 else if (added < 0)
3386 curwin->w_cursor.lnum = lnum;
3387 }
3388 }
3389 changed_lines(lnum, 0, lnum + count, (long)added);
3390
3391 if (dfree != NULL)
3392 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003393 // Diff is deleted, update folds in other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#ifdef FEAT_FOLDING
3395 diff_fold_update(dfree, idx_to);
3396#endif
3397 vim_free(dfree);
3398 }
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003399
3400 // mark_adjust() may have made "dp" invalid. We don't know where
3401 // to continue then, bail out.
3402 if (added != 0 && !valid_diff(dp))
3403 break;
3404
3405 if (dfree == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003406 // mark_adjust() may have changed the count in a wrong way
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 dp->df_count[idx_to] = new_count;
3408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 // When changing the current buffer, keep track of line numbers
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (idx_cur == idx_to)
3411 off += added;
3412 }
3413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003414 // If before the range or not deleted, go to next diff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (dfree == NULL)
3416 {
3417 dprev = dp;
3418 dp = dp->df_next;
3419 }
3420 }
3421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003422 // restore curwin/curbuf and a few other things
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003423 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003425 // Syncing undo only works for the current buffer, but we change
3426 // another buffer. Sync undo if the command was typed. This isn't
3427 // 100% right when ":diffput" is used in a function or mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003429 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 aucmd_restbuf(&aco);
3431 }
3432
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003433theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 diff_busy = FALSE;
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003435 if (diff_need_update)
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003436 ex_diffupdate(NULL);
Bram Moolenaardf77cef2018-10-07 17:46:42 +02003437
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +02003438 // Check that the cursor is on a valid character and update its
Bram Moolenaardf77cef2018-10-07 17:46:42 +02003439 // position. When there were filler lines the topline has become
3440 // invalid.
3441 check_cursor();
3442 changed_line_abv_curs();
3443
3444 if (diff_need_update)
3445 // redraw already done by ex_diffupdate()
3446 diff_need_update = FALSE;
Bram Moolenaar198fa062018-09-18 21:20:26 +02003447 else
3448 {
Bram Moolenaar198fa062018-09-18 21:20:26 +02003449 // Also need to redraw the other buffers.
3450 diff_redraw(FALSE);
3451 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
3454
3455#ifdef FEAT_FOLDING
3456/*
3457 * Update folds for all diff buffers for entry "dp".
3458 * Skip buffer with index "skip_idx".
3459 * When there are no diffs, all folds are removed.
3460 */
3461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003462diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 int i;
3465 win_T *wp;
3466
Bram Moolenaar29323592016-07-24 22:04:11 +02003467 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003469 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 foldUpdate(wp, dp->df_lnum[i],
3471 dp->df_lnum[i] + dp->df_count[i]);
3472}
3473#endif
3474
3475/*
3476 * Return TRUE if buffer "buf" is in diff-mode.
3477 */
3478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003481 tabpage_T *tp;
3482
Bram Moolenaar29323592016-07-24 22:04:11 +02003483 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003484 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
3485 return TRUE;
3486 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487}
3488
3489/*
3490 * Move "count" times in direction "dir" to the next diff block.
3491 * Return FAIL if there isn't such a diff block.
3492 */
3493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 int idx;
3497 linenr_T lnum = curwin->w_cursor.lnum;
3498 diff_T *dp;
3499
3500 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003501 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 return FAIL;
3503
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003504 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003507 if (curtab->tp_first_diff == NULL) // no diffs today
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 return FAIL;
3509
3510 while (--count >= 0)
3511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003512 // Check if already before first diff.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003513 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 break;
3515
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003516 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
3518 if (dp == NULL)
3519 break;
3520 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
3521 || (dir == BACKWARD
3522 && (dp->df_next == NULL
3523 || lnum <= dp->df_next->df_lnum[idx])))
3524 {
3525 lnum = dp->df_lnum[idx];
3526 break;
3527 }
3528 }
3529 }
3530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003531 // don't end up past the end of the file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (lnum > curbuf->b_ml.ml_line_count)
3533 lnum = curbuf->b_ml.ml_line_count;
3534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003535 // When the cursor didn't move at all we fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (lnum == curwin->w_cursor.lnum)
3537 return FAIL;
3538
3539 setpcmark();
3540 curwin->w_cursor.lnum = lnum;
3541 curwin->w_cursor.col = 0;
3542
3543 return OK;
3544}
3545
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003546/*
3547 * Return the line number in the current window that is closest to "lnum1" in
3548 * "buf1" in diff mode.
3549 */
3550 static linenr_T
3551diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003552 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003553 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003554{
3555 int idx1;
3556 int idx2;
3557 diff_T *dp;
3558 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003559
3560 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003561 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003562 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
3563 return lnum1;
3564
3565 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar860cae12010-06-05 23:22:07 +02003567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003568 if (curtab->tp_first_diff == NULL) // no diffs today
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569 return lnum1;
3570
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003571 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572 {
3573 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003574 return lnum1 - baseline;
3575 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003577 // Inside the diffblock
Bram Moolenaar860cae12010-06-05 23:22:07 +02003578 baseline = lnum1 - dp->df_lnum[idx1];
3579 if (baseline > dp->df_count[idx2])
3580 baseline = dp->df_count[idx2];
3581
3582 return dp->df_lnum[idx2] + baseline;
3583 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003584 if ( (dp->df_lnum[idx1] == lnum1)
3585 && (dp->df_count[idx1] == 0)
3586 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
3587 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
3588 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 /*
3590 * Special case: if the cursor is just after a zero-count
3591 * block (i.e. all filler) and the target cursor is already
3592 * inside the corresponding block, leave the target cursor
3593 * unmoved. This makes repeated CTRL-W W operations work
3594 * as expected.
3595 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003596 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
3598 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
3599 }
3600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 // If we get here then the cursor is after the last diff
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003602 return lnum1 - baseline;
3603}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003605/*
3606 * Return the line number in the current window that is closest to "lnum1" in
3607 * "buf1" in diff mode. Checks the line number to be valid.
3608 */
3609 linenr_T
3610diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
3611{
3612 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
3613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003614 // don't end up past the end of the file
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003615 if (lnum > curbuf->b_ml.ml_line_count)
3616 return curbuf->b_ml.ml_line_count;
3617 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003618}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620/*
3621 * For line "lnum" in the current window find the equivalent lnum in window
3622 * "wp", compensating for inserted/deleted lines.
3623 */
3624 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003625diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 diff_T *dp;
3628 int idx;
3629 int i;
3630 linenr_T n;
3631
3632 idx = diff_buf_idx(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003633 if (idx == DB_COUNT) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 return (linenr_T)0;
3635
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003636 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003637 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003639 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003640 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
3642 break;
3643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003644 // When after the last change, compute relative to the last line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (dp == NULL)
3646 return wp->w_buffer->b_ml.ml_line_count
3647 - (curbuf->b_ml.ml_line_count - lnum);
3648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003649 // Find index for "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 i = diff_buf_idx(wp->w_buffer);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003651 if (i == DB_COUNT) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return (linenr_T)0;
3653
3654 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
3655 if (n > dp->df_lnum[i] + dp->df_count[i])
3656 n = dp->df_lnum[i] + dp->df_count[i];
3657 return n;
3658}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaare828b762018-09-10 17:51:58 +02003660/*
3661 * Handle an ED style diff line.
3662 * Return FAIL if the line does not contain diff info.
3663 */
3664 static int
3665parse_diff_ed(
3666 char_u *line,
Lewis Russelld9da86e2021-12-28 13:54:41 +00003667 diffhunk_T *hunk)
Bram Moolenaare828b762018-09-10 17:51:58 +02003668{
3669 char_u *p;
3670 long f1, l1, f2, l2;
3671 int difftype;
3672
3673 // The line must be one of three formats:
3674 // change: {first}[,{last}]c{first}[,{last}]
3675 // append: {first}a{first}[,{last}]
3676 // delete: {first}[,{last}]d{first}
3677 p = line;
3678 f1 = getdigits(&p);
3679 if (*p == ',')
3680 {
3681 ++p;
3682 l1 = getdigits(&p);
3683 }
3684 else
3685 l1 = f1;
3686 if (*p != 'a' && *p != 'c' && *p != 'd')
3687 return FAIL; // invalid diff format
3688 difftype = *p++;
3689 f2 = getdigits(&p);
3690 if (*p == ',')
3691 {
3692 ++p;
3693 l2 = getdigits(&p);
3694 }
3695 else
3696 l2 = f2;
3697 if (l1 < f1 || l2 < f2)
3698 return FAIL;
3699
3700 if (difftype == 'a')
3701 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003702 hunk->lnum_orig = f1 + 1;
3703 hunk->count_orig = 0;
Bram Moolenaare828b762018-09-10 17:51:58 +02003704 }
3705 else
3706 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003707 hunk->lnum_orig = f1;
3708 hunk->count_orig = l1 - f1 + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +02003709 }
3710 if (difftype == 'd')
3711 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003712 hunk->lnum_new = f2 + 1;
3713 hunk->count_new = 0;
Bram Moolenaare828b762018-09-10 17:51:58 +02003714 }
3715 else
3716 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003717 hunk->lnum_new = f2;
3718 hunk->count_new = l2 - f2 + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +02003719 }
3720 return OK;
3721}
3722
3723/*
3724 * Parses unified diff with zero(!) context lines.
3725 * Return FAIL if there is no diff information in "line".
3726 */
3727 static int
3728parse_diff_unified(
3729 char_u *line,
Lewis Russelld9da86e2021-12-28 13:54:41 +00003730 diffhunk_T *hunk)
Bram Moolenaare828b762018-09-10 17:51:58 +02003731{
3732 char_u *p;
3733 long oldline, oldcount, newline, newcount;
3734
3735 // Parse unified diff hunk header:
3736 // @@ -oldline,oldcount +newline,newcount @@
3737 p = line;
3738 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3739 {
3740 oldline = getdigits(&p);
3741 if (*p == ',')
3742 {
3743 ++p;
3744 oldcount = getdigits(&p);
3745 }
3746 else
3747 oldcount = 1;
3748 if (*p++ == ' ' && *p++ == '+')
3749 {
3750 newline = getdigits(&p);
3751 if (*p == ',')
3752 {
3753 ++p;
3754 newcount = getdigits(&p);
3755 }
3756 else
3757 newcount = 1;
3758 }
3759 else
3760 return FAIL; // invalid diff format
3761
3762 if (oldcount == 0)
3763 oldline += 1;
3764 if (newcount == 0)
3765 newline += 1;
3766 if (newline == 0)
3767 newline = 1;
3768
Lewis Russelld9da86e2021-12-28 13:54:41 +00003769 hunk->lnum_orig = oldline;
3770 hunk->count_orig = oldcount;
3771 hunk->lnum_new = newline;
3772 hunk->count_new = newcount;
Bram Moolenaare828b762018-09-10 17:51:58 +02003773
3774 return OK;
3775 }
3776
3777 return FAIL;
3778}
3779
3780/*
3781 * Callback function for the xdl_diff() function.
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003782 * Stores the diff output (indices) in a grow array.
Bram Moolenaare828b762018-09-10 17:51:58 +02003783 */
3784 static int
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003785xdiff_out_indices(
Lewis Russelld9da86e2021-12-28 13:54:41 +00003786 long start_a,
3787 long count_a,
3788 long start_b,
3789 long count_b,
3790 void *priv)
Bram Moolenaare828b762018-09-10 17:51:58 +02003791{
3792 diffout_T *dout = (diffout_T *)priv;
Lewis Russelld9da86e2021-12-28 13:54:41 +00003793 diffhunk_T *p = ALLOC_ONE(diffhunk_T);
Bram Moolenaare828b762018-09-10 17:51:58 +02003794
Lewis Russelld9da86e2021-12-28 13:54:41 +00003795 if (p == NULL)
3796 return -1;
Bram Moolenaarf080d702018-10-31 22:57:26 +01003797
3798 if (ga_grow(&dout->dout_ga, 1) == FAIL)
Bram Moolenaarfebb78f2021-12-29 11:59:53 +00003799 {
3800 vim_free(p);
Bram Moolenaarf080d702018-10-31 22:57:26 +01003801 return -1;
Bram Moolenaarfebb78f2021-12-29 11:59:53 +00003802 }
Lewis Russelld9da86e2021-12-28 13:54:41 +00003803
3804 p->lnum_orig = start_a + 1;
3805 p->count_orig = count_a;
3806 p->lnum_new = start_b + 1;
3807 p->count_new = count_b;
3808 ((diffhunk_T **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
Bram Moolenaare828b762018-09-10 17:51:58 +02003809 return 0;
3810}
3811
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003812/*
3813 * Callback function for the xdl_diff() function.
3814 * Stores the unified diff output in a grow array.
3815 */
3816 static int
3817xdiff_out_unified(
3818 void *priv,
3819 mmbuffer_t *mb,
3820 int nbuf)
3821{
3822 diffout_T *dout = (diffout_T *)priv;
3823 int i;
3824
3825 for (i = 0; i < nbuf; i++)
3826 ga_concat_len(&dout->dout_ga, (char_u *)mb[i].ptr, mb[i].size);
3827
3828 return 0;
3829}
3830
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003831#endif // FEAT_DIFF
3832
3833#if defined(FEAT_EVAL) || defined(PROTO)
3834
3835/*
3836 * "diff_filler()" function
3837 */
3838 void
3839f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3840{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003841# ifdef FEAT_DIFF
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003842 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
3843 return;
3844
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003845 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003846# endif
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003847}
3848
3849/*
3850 * "diff_hlID()" function
3851 */
3852 void
3853f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3854{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003855# ifdef FEAT_DIFF
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003856 linenr_T lnum;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003857 static linenr_T prev_lnum = 0;
3858 static varnumber_T changedtick = 0;
3859 static int fnum = 0;
3860 static int change_start = 0;
3861 static int change_end = 0;
3862 static hlf_T hlID = (hlf_T)0;
3863 int filler_lines;
3864 int col;
3865
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003866 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02003867 && (check_for_lnum_arg(argvars,0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003868 || check_for_number_arg(argvars, 1) == FAIL))
3869 return;
3870
3871 lnum = tv_get_lnum(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003872 if (lnum < 0) // ignore type error in {lnum} arg
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003873 lnum = 0;
3874 if (lnum != prev_lnum
3875 || changedtick != CHANGEDTICK(curbuf)
3876 || fnum != curbuf->b_fnum)
3877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003878 // New line, buffer, change: need to get the values.
Jonathon7c7a4e62025-01-12 09:58:00 +01003879 int linestatus = 0;
3880 filler_lines = diff_check_with_linestatus(curwin, lnum, &linestatus);
3881 if (filler_lines < 0 || linestatus < 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003882 {
Jonathon7c7a4e62025-01-12 09:58:00 +01003883 if (filler_lines == -1 || linestatus == -1)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003884 {
3885 change_start = MAXCOL;
3886 change_end = -1;
3887 if (diff_find_change(curwin, lnum, &change_start, &change_end))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003888 hlID = HLF_ADD; // added line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003889 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003890 hlID = HLF_CHD; // changed line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003891 }
3892 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003893 hlID = HLF_ADD; // added line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003894 }
3895 else
3896 hlID = (hlf_T)0;
3897 prev_lnum = lnum;
3898 changedtick = CHANGEDTICK(curbuf);
3899 fnum = curbuf->b_fnum;
3900 }
3901
3902 if (hlID == HLF_CHD || hlID == HLF_TXD)
3903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003904 col = tv_get_number(&argvars[1]) - 1; // ignore type error in {col}
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003905 if (col >= change_start && col <= change_end)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003906 hlID = HLF_TXD; // changed text
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003907 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003908 hlID = HLF_CHD; // changed line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003909 }
3910 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003911# endif
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003912}
3913
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003914# ifdef FEAT_DIFF
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003915/*
3916 * Parse the diff options passed in "optarg" to the diff() function and return
3917 * the options in "diffopts" and the diff algorithm in "diffalgo".
3918 */
3919 static int
3920parse_diff_optarg(
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003921 typval_T *opts,
3922 int *diffopts,
3923 long *diffalgo,
3924 dio_outfmt_T *diff_output_fmt,
3925 int *diff_ctxlen)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003926{
3927 dict_T *d = opts->vval.v_dict;
3928
3929 char_u *algo = dict_get_string(d, "algorithm", FALSE);
3930 if (algo != NULL)
3931 {
3932 if (STRNCMP(algo, "myers", 5) == 0)
3933 *diffalgo = 0;
3934 else if (STRNCMP(algo, "minimal", 7) == 0)
3935 *diffalgo = XDF_NEED_MINIMAL;
3936 else if (STRNCMP(algo, "patience", 8) == 0)
3937 *diffalgo = XDF_PATIENCE_DIFF;
3938 else if (STRNCMP(algo, "histogram", 9) == 0)
3939 *diffalgo = XDF_HISTOGRAM_DIFF;
3940 }
3941
3942 char_u *output_fmt = dict_get_string(d, "output", FALSE);
3943 if (output_fmt != NULL)
3944 {
3945 if (STRNCMP(output_fmt, "unified", 7) == 0)
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003946 *diff_output_fmt = DIO_OUTPUT_UNIFIED;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003947 else if (STRNCMP(output_fmt, "indices", 7) == 0)
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003948 *diff_output_fmt = DIO_OUTPUT_INDICES;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003949 else
3950 {
3951 semsg(_(e_unsupported_diff_output_format_str), output_fmt);
3952 return FAIL;
3953 }
3954 }
3955
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01003956 *diff_ctxlen = dict_get_number_def(d, "context", 0);
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003957 if (*diff_ctxlen < 0)
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01003958 *diff_ctxlen = 0;
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003959
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003960 if (dict_get_bool(d, "iblank", FALSE))
3961 *diffopts |= DIFF_IBLANK;
3962 if (dict_get_bool(d, "icase", FALSE))
3963 *diffopts |= DIFF_ICASE;
3964 if (dict_get_bool(d, "iwhite", FALSE))
3965 *diffopts |= DIFF_IWHITE;
3966 if (dict_get_bool(d, "iwhiteall", FALSE))
3967 *diffopts |= DIFF_IWHITEALL;
3968 if (dict_get_bool(d, "iwhiteeol", FALSE))
3969 *diffopts |= DIFF_IWHITEEOL;
3970 if (dict_get_bool(d, "indent-heuristic", FALSE))
3971 *diffalgo |= XDF_INDENT_HEURISTIC;
3972
3973 return OK;
3974}
3975
3976/*
3977 * Concatenate the List of strings in "l" and store the result in
3978 * "din->din_mmfile.ptr" and the length in "din->din_mmfile.size".
3979 */
3980 static void
3981list_to_diffin(list_T *l, diffin_T *din, int icase)
3982{
3983 garray_T ga;
3984 listitem_T *li;
3985 char_u *str;
3986
3987 ga_init2(&ga, 512, 4);
3988
3989 FOR_ALL_LIST_ITEMS(l, li)
3990 {
3991 str = tv_get_string(&li->li_tv);
3992 if (icase)
3993 {
3994 str = strlow_save(str);
3995 if (str == NULL)
3996 continue;
3997 }
3998 ga_concat(&ga, str);
3999 ga_concat(&ga, (char_u *)NL_STR);
4000 if (icase)
4001 vim_free(str);
4002 }
4003 if (ga.ga_len > 0)
4004 ((char *)ga.ga_data)[ga.ga_len] = NUL;
4005
4006 din->din_mmfile.ptr = (char *)ga.ga_data;
4007 din->din_mmfile.size = ga.ga_len;
4008}
4009
4010/*
4011 * Get the start and end indices from the diff "hunk".
4012 */
4013 static dict_T *
4014get_diff_hunk_indices(diffhunk_T *hunk)
4015{
4016 dict_T *hunk_dict;
4017
4018 hunk_dict = dict_alloc();
4019 if (hunk_dict == NULL)
4020 return NULL;
4021
4022 dict_add_number(hunk_dict, "from_idx", hunk->lnum_orig - 1);
4023 dict_add_number(hunk_dict, "from_count", hunk->count_orig);
4024 dict_add_number(hunk_dict, "to_idx", hunk->lnum_new - 1);
4025 dict_add_number(hunk_dict, "to_count", hunk->count_new);
4026
4027 return hunk_dict;
4028}
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004029# endif
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004030
4031/*
4032 * "diff()" function
4033 */
4034 void
4035f_diff(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4036{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004037# ifdef FEAT_DIFF
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004038 diffio_T dio;
4039
4040 if (check_for_nonnull_list_arg(argvars, 0) == FAIL
4041 || check_for_nonnull_list_arg(argvars, 1) == FAIL
4042 || check_for_opt_nonnull_dict_arg(argvars, 2) == FAIL)
4043 return;
4044
4045 CLEAR_FIELD(dio);
4046 dio.dio_internal = TRUE;
4047 ga_init2(&dio.dio_diff.dout_ga, sizeof(char *), 1000);
4048
4049 list_T *orig_list = argvars[0].vval.v_list;
4050 list_T *new_list = argvars[1].vval.v_list;
4051
4052 // Save the 'diffopt' option value and restore it after getting the diff.
4053 int save_diff_flags = diff_flags;
4054 long save_diff_algorithm = diff_algorithm;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004055 diff_flags = DIFF_INTERNAL;
4056 diff_algorithm = 0;
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004057 dio.dio_outfmt = DIO_OUTPUT_UNIFIED;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004058 if (argvars[2].v_type != VAR_UNKNOWN)
4059 if (parse_diff_optarg(&argvars[2], &diff_flags, &diff_algorithm,
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004060 &dio.dio_outfmt, &dio.dio_ctxlen) == FAIL)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004061 return;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004062
4063 // Concatenate the List of strings into a single string using newline
4064 // separator. Internal diff library expects a single string.
4065 list_to_diffin(orig_list, &dio.dio_orig, diff_flags & DIFF_ICASE);
4066 list_to_diffin(new_list, &dio.dio_new, diff_flags & DIFF_ICASE);
4067
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004068 // If 'diffexpr' is set, then the internal diff is not used. Set
4069 // 'diffexpr' to an empty string temporarily.
4070 int restore_diffexpr = FALSE;
4071 char_u cc = *p_dex;
4072 if (*p_dex != NUL)
4073 {
4074 restore_diffexpr = TRUE;
4075 *p_dex = NUL;
4076 }
4077
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004078 // Compute the diff
4079 int diff_status = diff_file(&dio);
4080
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004081 // restore 'diffexpr'
4082 if (restore_diffexpr)
4083 *p_dex = cc;
4084
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004085 if (diff_status == FAIL)
4086 goto done;
4087
4088 int hunk_idx = 0;
4089 dict_T *hunk_dict;
4090
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004091 if (dio.dio_outfmt == DIO_OUTPUT_INDICES)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004092 {
4093 if (rettv_list_alloc(rettv) != OK)
4094 goto done;
4095 list_T *l = rettv->vval.v_list;
4096
4097 // Process each diff hunk
4098 diffhunk_T *hunk = NULL;
4099 while (hunk_idx < dio.dio_diff.dout_ga.ga_len)
4100 {
4101 hunk = ((diffhunk_T **)dio.dio_diff.dout_ga.ga_data)[hunk_idx++];
4102
4103 hunk_dict = get_diff_hunk_indices(hunk);
4104 if (hunk_dict == NULL)
4105 goto done;
4106
4107 list_append_dict(l, hunk_dict);
4108 }
4109 }
4110 else
4111 {
4112 ga_append(&dio.dio_diff.dout_ga, NUL);
4113 rettv->v_type = VAR_STRING;
4114 rettv->vval.v_string =
4115 vim_strsave((char_u *)dio.dio_diff.dout_ga.ga_data);
4116 }
4117
4118done:
4119 clear_diffin(&dio.dio_new);
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004120 if (dio.dio_outfmt == DIO_OUTPUT_INDICES)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004121 clear_diffout(&dio.dio_diff);
4122 else
4123 ga_clear(&dio.dio_diff.dout_ga);
4124 clear_diffin(&dio.dio_orig);
4125 // Restore the 'diffopt' option value.
4126 diff_flags = save_diff_flags;
4127 diff_algorithm = save_diff_algorithm;
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004128# endif
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004129}
4130
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004131#endif