blob: d7433349a46fe3311bba5bd0d1bcaa6e462fb3c0 [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
Jonathonca307ef2025-01-17 13:37:35 +01002354 && !dp->is_linematched && diff_linematch(dp)
2355 && diff_check_sanity(curtab, dp))
Jonathon7c7a4e62025-01-12 09:58:00 +01002356 run_linematch_algorithm(dp);
2357
2358 if (dp->is_linematched)
2359 return linematched_filler_lines(dp, idx, lnum, linestatus);
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
2362 {
2363 int zero = FALSE;
2364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002365 // Changed or inserted line. If the other buffers have a count of
2366 // zero, the lines were inserted. If the other buffers have the same
2367 // count, check if the lines are identical.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 cmp = FALSE;
2369 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002370 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 if (dp->df_count[i] == 0)
2373 zero = TRUE;
2374 else
2375 {
2376 if (dp->df_count[i] != dp->df_count[idx])
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002377 return -1; // nr of lines changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 cmp = TRUE;
2379 }
2380 }
2381 if (cmp)
2382 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002383 // Compare all lines. If they are equal the lines were inserted
2384 // in some buffers, deleted in others, but not changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002386 if (i != idx && curtab->tp_diffbuf[i] != NULL
2387 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (!diff_equal_entry(dp, idx, i))
2389 return -1;
2390 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002391 // If there is no buffer with zero lines then there is no difference
2392 // any longer. Happens when making a change (or undo) that removes
2393 // the difference. Can't remove the entry here, we might be halfway
2394 // updating the window. Just report the text as unchanged. Other
2395 // windows might still show the change though.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (zero == FALSE)
2397 return 0;
2398 return -2;
2399 }
2400
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002401 // If 'diffopt' doesn't contain "filler", return 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (!(diff_flags & DIFF_FILLER))
2403 return 0;
2404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002405 // Insert filler lines above the line just below the change. Will return
2406 // 0 when this buf had the max count.
Jonathon7c7a4e62025-01-12 09:58:00 +01002407 maxcount = get_max_diff_length(dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 return maxcount - dp->df_count[idx];
2409}
2410
Jonathon7c7a4e62025-01-12 09:58:00 +01002411 int
2412diff_check(win_T *wp, linenr_T lnum)
2413{
2414 return diff_check_with_linestatus(wp, lnum, NULL);
2415}
2416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417/*
2418 * Compare two entries in diff "*dp" and return TRUE if they are equal.
2419 */
2420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002421diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 int i;
2424 char_u *line;
2425 int cmp;
2426
2427 if (dp->df_count[idx1] != dp->df_count[idx2])
2428 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002429 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 return FALSE;
2431 for (i = 0; i < dp->df_count[idx1]; ++i)
2432 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002433 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 dp->df_lnum[idx1] + i, FALSE));
2435 if (line == NULL)
2436 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002437 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 dp->df_lnum[idx2] + i, FALSE));
2439 vim_free(line);
2440 if (cmp != 0)
2441 return FALSE;
2442 }
2443 return TRUE;
2444}
2445
2446/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002447 * Compare the characters at "p1" and "p2". If they are equal (possibly
2448 * ignoring case) return TRUE and set "len" to the number of bytes.
2449 */
2450 static int
2451diff_equal_char(char_u *p1, char_u *p2, int *len)
2452{
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002453 int l = (*mb_ptr2len)(p1);
2454
2455 if (l != (*mb_ptr2len)(p2))
2456 return FALSE;
2457 if (l > 1)
2458 {
2459 if (STRNCMP(p1, p2, l) != 0
2460 && (!enc_utf8
2461 || !(diff_flags & DIFF_ICASE)
2462 || utf_fold(utf_ptr2char(p1))
2463 != utf_fold(utf_ptr2char(p2))))
2464 return FALSE;
2465 *len = l;
2466 }
2467 else
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002468 {
2469 if ((*p1 != *p2)
2470 && (!(diff_flags & DIFF_ICASE)
2471 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
2472 return FALSE;
2473 *len = 1;
2474 }
2475 return TRUE;
2476}
2477
2478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 * Compare strings "s1" and "s2" according to 'diffopt'.
2480 * Return non-zero when they are different.
2481 */
2482 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002483diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
2485 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar785fc652018-09-15 19:17:38 +02002488 if ((diff_flags & DIFF_IBLANK)
2489 && (*skipwhite(s1) == NUL || *skipwhite(s2) == NUL))
2490 return 0;
2491
2492 if ((diff_flags & (DIFF_ICASE | ALL_WHITE_DIFF)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 return STRCMP(s1, s2);
Bram Moolenaar785fc652018-09-15 19:17:38 +02002494 if ((diff_flags & DIFF_ICASE) && !(diff_flags & ALL_WHITE_DIFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return MB_STRICMP(s1, s2);
2496
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 p1 = s1;
2498 p2 = s2;
Bram Moolenaar785fc652018-09-15 19:17:38 +02002499
2500 // Ignore white space changes and possibly ignore case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 while (*p1 != NUL && *p2 != NUL)
2502 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002503 if (((diff_flags & DIFF_IWHITE)
2504 && VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
2505 || ((diff_flags & DIFF_IWHITEALL)
2506 && (VIM_ISWHITE(*p1) || VIM_ISWHITE(*p2))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 p1 = skipwhite(p1);
2509 p2 = skipwhite(p2);
2510 }
2511 else
2512 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002513 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002515 p1 += l;
2516 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
2518 }
2519
Bram Moolenaar785fc652018-09-15 19:17:38 +02002520 // Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 p1 = skipwhite(p1);
2522 p2 = skipwhite(p2);
2523 if (*p1 != NUL || *p2 != NUL)
2524 return 1;
2525 return 0;
2526}
2527
2528/*
2529 * Return the number of filler lines above "lnum".
2530 */
2531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002532diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534 int n;
2535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002536 // be quick when there are no filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (!(diff_flags & DIFF_FILLER))
2538 return 0;
2539 n = diff_check(wp, lnum);
2540 if (n <= 0)
2541 return 0;
2542 return n;
2543}
2544
2545/*
2546 * Set the topline of "towin" to match the position in "fromwin", so that they
2547 * show the same diff'ed lines.
2548 */
2549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002550diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002552 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002554 int fromidx;
2555 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002557 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 int i;
2559
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002560 fromidx = diff_buf_idx(frombuf);
2561 if (fromidx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002562 return; // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002564 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002565 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
2567 towin->w_topfill = 0;
2568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002569 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002570 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002571 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 break;
2573 if (dp == NULL)
2574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002575 // After last change, compute topline relative to end of file; no
2576 // filler lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002578 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 else
2581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002582 // Find index for "towin".
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002583 toidx = diff_buf_idx(towin->w_buffer);
2584 if (toidx == DB_COUNT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002585 return; // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002587 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2588 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
Jonathon7c7a4e62025-01-12 09:58:00 +01002590 if (dp->is_linematched)
2591 {
2592 calculate_topfill_and_topline(fromidx, toidx,
2593 fromwin->w_topline,
2594 fromwin->w_topfill,
2595 &towin->w_topfill,
2596 &towin->w_topline);
2597 }
2598 else
2599 {
2600 // Inside a change: compute filler lines. With three or more
2601 // buffers we need to know the largest count.
2602 max_count = 0;
2603 for (i = 0; i < DB_COUNT; ++i)
2604 if (curtab->tp_diffbuf[i] != NULL
2605 && max_count < dp->df_count[i])
2606 max_count = dp->df_count[i];
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002607
Jonathon7c7a4e62025-01-12 09:58:00 +01002608 if (dp->df_count[toidx] == dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Jonathon7c7a4e62025-01-12 09:58:00 +01002610 // same number of lines: use same filler count
2611 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002612 }
Jonathon7c7a4e62025-01-12 09:58:00 +01002613 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002614 {
2615 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Jonathon7c7a4e62025-01-12 09:58:00 +01002616 {
2617 // more lines in towin and fromwin doesn't show diff
2618 // lines, only filler lines
2619 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2620 {
2621 // towin also only shows filler lines
2622 towin->w_topline = dp->df_lnum[toidx]
2623 + dp->df_count[toidx];
2624 towin->w_topfill = fromwin->w_topfill;
2625 }
2626 else
2627 // towin still has some diff lines to show
2628 towin->w_topline = dp->df_lnum[toidx]
2629 + max_count - fromwin->w_topfill;
2630 }
2631 }
2632 else if (towin->w_topline >= dp->df_lnum[toidx]
2633 + dp->df_count[toidx])
2634 {
2635 // less lines in towin and no diff lines to show: compute
2636 // filler lines
2637 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2638 if (diff_flags & DIFF_FILLER)
2639 {
2640 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2641 // fromwin is also out of diff lines
2642 towin->w_topfill = fromwin->w_topfill;
2643 else
2644 // fromwin has some diff lines
2645 towin->w_topfill = dp->df_lnum[fromidx] +
2646 max_count - lnum;
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 }
2650 }
2651 }
2652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002653 // safety check (if diff info gets outdated strange things may happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 towin->w_botfill = FALSE;
2655 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2656 {
2657 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2658 towin->w_botfill = TRUE;
2659 }
2660 if (towin->w_topline < 1)
2661 {
2662 towin->w_topline = 1;
2663 towin->w_topfill = 0;
2664 }
2665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002666 // When w_topline changes need to recompute w_botline and cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 invalidate_botline_win(towin);
2668 changed_line_abv_curs_win(towin);
2669
2670 check_topfill(towin, FALSE);
2671#ifdef FEAT_FOLDING
2672 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2673 NULL, TRUE, NULL);
2674#endif
2675}
2676
2677/*
2678 * This is called when 'diffopt' is changed.
2679 */
2680 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002681diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 char_u *p;
2684 int diff_context_new = 6;
Jonathon7c7a4e62025-01-12 09:58:00 +01002685 int linematch_lines_new = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002687 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002688 long diff_algorithm_new = 0;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002689 long diff_indent_heuristic = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002690 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 p = p_dip;
2693 while (*p != NUL)
2694 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002695 // Note: Keep this in sync with p_dip_values
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (STRNCMP(p, "filler", 6) == 0)
2697 {
2698 p += 6;
2699 diff_flags_new |= DIFF_FILLER;
2700 }
2701 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2702 {
2703 p += 8;
2704 diff_context_new = getdigits(&p);
2705 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002706 else if (STRNCMP(p, "iblank", 6) == 0)
2707 {
2708 p += 6;
2709 diff_flags_new |= DIFF_IBLANK;
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else if (STRNCMP(p, "icase", 5) == 0)
2712 {
2713 p += 5;
2714 diff_flags_new |= DIFF_ICASE;
2715 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002716 else if (STRNCMP(p, "iwhiteall", 9) == 0)
2717 {
2718 p += 9;
2719 diff_flags_new |= DIFF_IWHITEALL;
2720 }
2721 else if (STRNCMP(p, "iwhiteeol", 9) == 0)
2722 {
2723 p += 9;
2724 diff_flags_new |= DIFF_IWHITEEOL;
2725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 else if (STRNCMP(p, "iwhite", 6) == 0)
2727 {
2728 p += 6;
2729 diff_flags_new |= DIFF_IWHITE;
2730 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002731 else if (STRNCMP(p, "horizontal", 10) == 0)
2732 {
2733 p += 10;
2734 diff_flags_new |= DIFF_HORIZONTAL;
2735 }
2736 else if (STRNCMP(p, "vertical", 8) == 0)
2737 {
2738 p += 8;
2739 diff_flags_new |= DIFF_VERTICAL;
2740 }
2741 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2742 {
2743 p += 11;
2744 diff_foldcolumn_new = getdigits(&p);
2745 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002746 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2747 {
2748 p += 9;
2749 diff_flags_new |= DIFF_HIDDEN_OFF;
2750 }
Bram Moolenaarc8234772019-11-10 21:00:27 +01002751 else if (STRNCMP(p, "closeoff", 8) == 0)
2752 {
2753 p += 8;
2754 diff_flags_new |= DIFF_CLOSE_OFF;
2755 }
Bram Moolenaar4223d432021-02-10 13:18:17 +01002756 else if (STRNCMP(p, "followwrap", 10) == 0)
2757 {
2758 p += 10;
2759 diff_flags_new |= DIFF_FOLLOWWRAP;
2760 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002761 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2762 {
2763 p += 16;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002764 diff_indent_heuristic = XDF_INDENT_HEURISTIC;
Bram Moolenaare828b762018-09-10 17:51:58 +02002765 }
2766 else if (STRNCMP(p, "internal", 8) == 0)
2767 {
2768 p += 8;
2769 diff_flags_new |= DIFF_INTERNAL;
2770 }
2771 else if (STRNCMP(p, "algorithm:", 10) == 0)
2772 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002773 // Note: Keep this in sync with p_dip_algorithm_values.
Bram Moolenaare828b762018-09-10 17:51:58 +02002774 p += 10;
2775 if (STRNCMP(p, "myers", 5) == 0)
2776 {
2777 p += 5;
2778 diff_algorithm_new = 0;
2779 }
2780 else if (STRNCMP(p, "minimal", 7) == 0)
2781 {
2782 p += 7;
2783 diff_algorithm_new = XDF_NEED_MINIMAL;
2784 }
2785 else if (STRNCMP(p, "patience", 8) == 0)
2786 {
2787 p += 8;
2788 diff_algorithm_new = XDF_PATIENCE_DIFF;
2789 }
2790 else if (STRNCMP(p, "histogram", 9) == 0)
2791 {
2792 p += 9;
2793 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2794 }
Bram Moolenaard0721052018-11-05 21:21:33 +01002795 else
2796 return FAIL;
Bram Moolenaare828b762018-09-10 17:51:58 +02002797 }
zeertzjqccd7f452025-02-03 18:49:49 +01002798 else if (STRNCMP(p, "linematch:", 10) == 0 && VIM_ISDIGIT(p[10]))
Jonathon7c7a4e62025-01-12 09:58:00 +01002799 {
2800 p += 10;
2801 linematch_lines_new = getdigits(&p);
2802 diff_flags_new |= DIFF_LINEMATCH;
2803 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (*p != ',' && *p != NUL)
2806 return FAIL;
2807 if (*p == ',')
2808 ++p;
2809 }
2810
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002811 diff_algorithm_new |= diff_indent_heuristic;
2812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002813 // Can't have both "horizontal" and "vertical".
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002814 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2815 return FAIL;
2816
Bram Moolenaar198fa062018-09-18 21:20:26 +02002817 // If flags were added or removed, or the algorithm was changed, need to
2818 // update the diff.
Bram Moolenaare828b762018-09-10 17:51:58 +02002819 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002820 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002821 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 diff_flags = diff_flags_new;
Bram Moolenaarb9ddda62019-02-19 23:00:50 +01002824 diff_context = diff_context_new == 0 ? 1 : diff_context_new;
Jonathon7c7a4e62025-01-12 09:58:00 +01002825 linematch_lines = linematch_lines_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002826 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002827 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
2829 diff_redraw(TRUE);
2830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002831 // recompute the scroll binding with the new option value, may
2832 // remove or add filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 check_scrollbind((linenr_T)0, 0L);
2834
2835 return OK;
2836}
2837
2838/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002839 * Return TRUE if 'diffopt' contains "horizontal".
2840 */
2841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002842diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002843{
2844 return (diff_flags & DIFF_HORIZONTAL) != 0;
2845}
2846
2847/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002848 * Return TRUE if 'diffopt' contains "hiddenoff".
2849 */
2850 int
2851diffopt_hiddenoff(void)
2852{
2853 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2854}
2855
2856/*
Bram Moolenaarc8234772019-11-10 21:00:27 +01002857 * Return TRUE if 'diffopt' contains "closeoff".
2858 */
2859 int
2860diffopt_closeoff(void)
2861{
2862 return (diff_flags & DIFF_CLOSE_OFF) != 0;
2863}
2864
2865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 * Find the difference within a changed line.
2867 * Returns TRUE if the line was added, no other buffer has it.
2868 */
2869 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002870diff_find_change(
2871 win_T *wp,
2872 linenr_T lnum,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002873 int *startp, // first char of the change
2874 int *endp) // last char of the change
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 char_u *line_org;
2877 char_u *line_new;
2878 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002879 int si_org, si_new;
2880 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 diff_T *dp;
2882 int idx;
2883 int off;
2884 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002885 char_u *p1, *p2;
2886 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002888 // Make a copy of the line, the next ml_get() will invalidate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2890 if (line_org == NULL)
2891 return FALSE;
2892
2893 idx = diff_buf_idx(wp->w_buffer);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002894 if (idx == DB_COUNT) // cannot happen
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002895 {
2896 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002900 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002901 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2903 break;
Jonathon7c7a4e62025-01-12 09:58:00 +01002904 if (dp->is_linematched)
2905 {
2906 while (dp && dp->df_next
2907 && lnum == dp->df_count[idx] + dp->df_lnum[idx]
2908 && dp->df_next->df_lnum[idx] == lnum)
2909 dp = dp->df_next;
2910 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002911 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002912 {
2913 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
2917 off = lnum - dp->df_lnum[idx];
2918
2919 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002920 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002922 // Skip lines that are not in the other change (filler lines).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 if (off >= dp->df_count[i])
2924 continue;
2925 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002926 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2927 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002929 // Search for start of difference
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002930 si_org = si_new = 0;
2931 while (line_org[si_org] != NUL)
2932 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002933 if (((diff_flags & DIFF_IWHITE)
2934 && VIM_ISWHITE(line_org[si_org])
2935 && VIM_ISWHITE(line_new[si_new]))
2936 || ((diff_flags & DIFF_IWHITEALL)
2937 && (VIM_ISWHITE(line_org[si_org])
2938 || VIM_ISWHITE(line_new[si_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002939 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002940 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2941 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002942 }
2943 else
2944 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002945 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2946 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002947 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002948 si_org += l;
2949 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002950 }
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (has_mbyte)
2953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002954 // Move back to first byte of character in both lines (may
2955 // have "nn^" in line_org and "n^ in line_new).
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002956 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2957 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002959 if (*startp > si_org)
2960 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002962 // Search for end of difference, if any.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002963 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
2965 ei_org = (int)STRLEN(line_org);
2966 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002967 while (ei_org >= *startp && ei_new >= si_new
2968 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002970 if (((diff_flags & DIFF_IWHITE)
2971 && VIM_ISWHITE(line_org[ei_org])
2972 && VIM_ISWHITE(line_new[ei_new]))
2973 || ((diff_flags & DIFF_IWHITEALL)
2974 && (VIM_ISWHITE(line_org[ei_org])
2975 || VIM_ISWHITE(line_new[ei_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002976 {
2977 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002978 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002979 --ei_org;
2980 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002981 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002982 --ei_new;
2983 }
2984 else
2985 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002986 p1 = line_org + ei_org;
2987 p2 = line_new + ei_new;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002988 p1 -= (*mb_head_off)(line_org, p1);
2989 p2 -= (*mb_head_off)(line_new, p2);
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002990 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002991 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002992 ei_org -= l;
2993 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 if (*endp < ei_org)
2997 *endp = ei_org;
2998 }
2999 }
3000
3001 vim_free(line_org);
3002 return added;
3003}
3004
3005#if defined(FEAT_FOLDING) || defined(PROTO)
3006/*
3007 * Return TRUE if line "lnum" is not close to a diff block, this line should
3008 * be in a fold.
3009 * Return FALSE if there are no diff blocks at all in this window.
3010 */
3011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003012diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int i;
3015 int idx = -1;
3016 int other = FALSE;
3017 diff_T *dp;
3018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003019 // Return if 'diff' isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (!wp->w_p_diff)
3021 return FALSE;
3022
3023 for (i = 0; i < DB_COUNT; ++i)
3024 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003025 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003027 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 other = TRUE;
3029 }
3030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003031 // return here if there are no diffs in the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (idx == -1 || !other)
3033 return FALSE;
3034
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003035 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003036 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003038 // Return if there are no diff blocks. All lines will be folded.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003039 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 return TRUE;
3041
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003042 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003044 // If this change is below the line there can't be any further match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (dp->df_lnum[idx] - diff_context > lnum)
3046 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003047 // If this change ends before the line we have a match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
3049 return FALSE;
3050 }
3051 return TRUE;
3052}
3053#endif
3054
3055/*
3056 * "dp" and "do" commands.
3057 */
3058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01003062 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaarf2732452018-06-03 14:47:35 +02003064#ifdef FEAT_JOB_CHANNEL
3065 if (bt_prompt(curbuf))
3066 {
3067 vim_beep(BO_OPER);
3068 return;
3069 }
3070#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01003071 if (count == 0)
3072 ea.arg = (char_u *)"";
3073 else
3074 {
3075 vim_snprintf((char *)buf, 30, "%ld", count);
3076 ea.arg = buf;
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 if (put)
3079 ea.cmdidx = CMD_diffput;
3080 else
3081 ea.cmdidx = CMD_diffget;
3082 ea.addr_count = 0;
3083 ea.line1 = curwin->w_cursor.lnum;
3084 ea.line2 = curwin->w_cursor.lnum;
3085 ex_diffgetput(&ea);
3086}
3087
3088/*
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003089 * Return TRUE if "diff" appears in the list of diff blocks of the current tab.
3090 */
3091 static int
3092valid_diff(diff_T *diff)
3093{
3094 diff_T *dp;
3095
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003096 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003097 if (dp == diff)
3098 return TRUE;
3099 return FALSE;
3100}
3101
3102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * ":diffget"
3104 * ":diffput"
3105 */
3106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 linenr_T lnum;
3110 int count;
3111 linenr_T off = 0;
3112 diff_T *dp;
3113 diff_T *dprev;
3114 diff_T *dfree;
3115 int idx_cur;
3116 int idx_other;
3117 int idx_from;
3118 int idx_to;
3119 int i;
3120 int added;
3121 char_u *p;
3122 aco_save_T aco;
3123 buf_T *buf;
3124 int start_skip, end_skip;
3125 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003126 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00003127 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003129 // Find the current buffer in the list of diff buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 idx_cur = diff_buf_idx(curbuf);
3131 if (idx_cur == DB_COUNT)
3132 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003133 emsg(_(e_current_buffer_is_not_in_diff_mode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 return;
3135 }
3136
3137 if (*eap->arg == NUL)
3138 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003139 // No argument: Find the other buffer in the list of diff buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003141 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00003142 && curtab->tp_diffbuf[idx_other] != NULL)
3143 {
3144 if (eap->cmdidx != CMD_diffput
3145 || curtab->tp_diffbuf[idx_other]->b_p_ma)
3146 break;
3147 found_not_ma = TRUE;
3148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (idx_other == DB_COUNT)
3150 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00003151 if (found_not_ma)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003152 emsg(_(e_no_other_buffer_in_diff_mode_is_modifiable));
Bram Moolenaar602eb742007-02-20 03:43:38 +00003153 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003154 emsg(_(e_no_other_buffer_in_diff_mode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 return;
3156 }
3157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003158 // Check that there isn't a third buffer in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003160 if (curtab->tp_diffbuf[i] != curbuf
3161 && curtab->tp_diffbuf[i] != NULL
3162 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003164 emsg(_(e_more_than_two_buffers_in_diff_mode_dont_know_which_one_to_use));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 return;
3166 }
3167 }
3168 else
3169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003170 // Buffer number or pattern given. Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003172 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 --p;
3174 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
3175 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003176 if (eap->arg + i == p) // digits only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 i = atol((char *)eap->arg);
3178 else
3179 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01003180 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (i < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003182 return; // error message already given
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
3184 buf = buflist_findnr(i);
3185 if (buf == NULL)
3186 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003187 semsg(_(e_cant_find_buffer_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 return;
3189 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00003190 if (buf == curbuf)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003191 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 idx_other = diff_buf_idx(buf);
3193 if (idx_other == DB_COUNT)
3194 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003195 semsg(_(e_buffer_str_is_not_in_diff_mode), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 return;
3197 }
3198 }
3199
3200 diff_busy = TRUE;
3201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003202 // When no range given include the line above or below the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (eap->addr_count == 0)
3204 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003205 // Make it possible that ":diffget" on the last line gets line below
3206 // the cursor line when there is no difference above the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (eap->cmdidx == CMD_diffget
3208 && eap->line1 == curbuf->b_ml.ml_line_count
3209 && diff_check(curwin, eap->line1) == 0
3210 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
3211 ++eap->line2;
3212 else if (eap->line1 > 0)
3213 --eap->line1;
3214 }
3215
3216 if (eap->cmdidx == CMD_diffget)
3217 {
3218 idx_from = idx_other;
3219 idx_to = idx_cur;
3220 }
3221 else
3222 {
3223 idx_from = idx_cur;
3224 idx_to = idx_other;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003225 // Need to make the other buffer the current buffer to be able to make
3226 // changes in it.
Bram Moolenaare76062c2022-11-28 18:51:43 +00003227 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003228 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaare76062c2022-11-28 18:51:43 +00003229 if (curbuf != curtab->tp_diffbuf[idx_other])
3230 // Could not find a window for this buffer, the rest is likely to
3231 // fail.
3232 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003235 // May give the warning for a changed buffer here, which can trigger the
3236 // FileChangedRO autocommand, which may do nasty things and mess
3237 // everything up.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003238 if (!curbuf->b_changed)
3239 {
3240 change_warning(0);
3241 if (diff_buf_idx(curbuf) != idx_to)
3242 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00003243 emsg(_(e_buffer_changed_unexpectedly));
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003244 goto theend;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003245 }
3246 }
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003249 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
Jonathon7c7a4e62025-01-12 09:58:00 +01003251 if (!eap->addr_count)
3252 {
3253 // handle the case with adjacent diff blocks
3254 while (dp->is_linematched
3255 && dp->df_next
3256 && dp->df_next->df_lnum[idx_cur] == dp->df_lnum[idx_cur] +
3257 dp->df_count[idx_cur]
3258 && dp->df_next->df_lnum[idx_cur] == eap->line1 + off + 1)
3259 {
3260 dprev = dp;
3261 dp = dp->df_next;
3262 }
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (dp->df_lnum[idx_cur] > eap->line2 + off)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003265 break; // past the range that was specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
3267 dfree = NULL;
3268 lnum = dp->df_lnum[idx_to];
3269 count = dp->df_count[idx_to];
3270 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
3271 && u_save(lnum - 1, lnum + count) != FAIL)
3272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 // Inside the specified range and saving for undo worked.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 start_skip = 0;
3275 end_skip = 0;
3276 if (eap->addr_count > 0)
3277 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003278 // A range was specified: check if lines need to be skipped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
3280 if (start_skip > 0)
3281 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003282 // range starts below start of current diff block
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (start_skip > count)
3284 {
3285 lnum += count;
3286 count = 0;
3287 }
3288 else
3289 {
3290 count -= start_skip;
3291 lnum += start_skip;
3292 }
3293 }
3294 else
3295 start_skip = 0;
3296
3297 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
3298 - (eap->line2 + off);
3299 if (end_skip > 0)
3300 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003301 // range ends above end of current/from diff block
3302 if (idx_cur == idx_from) // :diffput
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 i = dp->df_count[idx_cur] - start_skip - end_skip;
3305 if (count > i)
3306 count = i;
3307 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003308 else // :diffget
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 count -= end_skip;
3311 end_skip = dp->df_count[idx_from] - start_skip - count;
3312 if (end_skip < 0)
3313 end_skip = 0;
3314 }
3315 }
3316 else
3317 end_skip = 0;
3318 }
3319
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003320 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 added = 0;
3322 for (i = 0; i < count; ++i)
3323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003324 // remember deleting the last line of the buffer
Bram Moolenaar280f1262006-01-30 00:14:18 +00003325 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar4e677b92022-07-28 18:44:27 +01003326 if (ml_delete(lnum) == OK)
3327 --added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
3329 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
3330 {
3331 linenr_T nr;
3332
3333 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003334 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003336 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
3337 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (p != NULL)
3339 {
3340 ml_append(lnum + i - 1, p, 0, FALSE);
3341 vim_free(p);
3342 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003343 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
3344 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 // Added the first line into an empty buffer, need to
3346 // delete the dummy empty line.
Bram Moolenaar280f1262006-01-30 00:14:18 +00003347 buf_empty = FALSE;
Bram Moolenaarca70c072020-05-30 20:30:46 +02003348 ml_delete((linenr_T)2);
Bram Moolenaar280f1262006-01-30 00:14:18 +00003349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 }
3351 }
3352 new_count = dp->df_count[idx_to] + added;
3353 dp->df_count[idx_to] = new_count;
3354
3355 if (start_skip == 0 && end_skip == 0)
3356 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003357 // Check if there are any other buffers and if the diff is
3358 // equal in them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003360 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
3361 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 && !diff_equal_entry(dp, idx_from, i))
3363 break;
3364 if (i == DB_COUNT)
3365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003366 // delete the diff entry, the buffers are now equal here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 dfree = dp;
3368 dp = dp->df_next;
3369 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003370 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
3372 dprev->df_next = dp;
3373 }
3374 }
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (added != 0)
3377 {
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003378 // Adjust marks. This will change the following entries!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
3380 if (curwin->w_cursor.lnum >= lnum)
3381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003382 // Adjust the cursor position if it's in/after the changed
3383 // lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (curwin->w_cursor.lnum >= lnum + count)
3385 curwin->w_cursor.lnum += added;
3386 else if (added < 0)
3387 curwin->w_cursor.lnum = lnum;
3388 }
3389 }
3390 changed_lines(lnum, 0, lnum + count, (long)added);
3391
3392 if (dfree != NULL)
3393 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003394 // Diff is deleted, update folds in other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#ifdef FEAT_FOLDING
3396 diff_fold_update(dfree, idx_to);
3397#endif
3398 vim_free(dfree);
3399 }
Bram Moolenaarc5274dd2022-07-02 15:10:00 +01003400
3401 // mark_adjust() may have made "dp" invalid. We don't know where
3402 // to continue then, bail out.
3403 if (added != 0 && !valid_diff(dp))
3404 break;
3405
3406 if (dfree == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 // mark_adjust() may have changed the count in a wrong way
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 dp->df_count[idx_to] = new_count;
3409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003410 // When changing the current buffer, keep track of line numbers
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (idx_cur == idx_to)
3412 off += added;
3413 }
3414
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003415 // If before the range or not deleted, go to next diff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (dfree == NULL)
3417 {
3418 dprev = dp;
3419 dp = dp->df_next;
3420 }
3421 }
3422
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003423 // restore curwin/curbuf and a few other things
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003424 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003426 // Syncing undo only works for the current buffer, but we change
3427 // another buffer. Sync undo if the command was typed. This isn't
3428 // 100% right when ":diffput" is used in a function or mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003430 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 aucmd_restbuf(&aco);
3432 }
3433
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003434theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 diff_busy = FALSE;
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003436 if (diff_need_update)
Bram Moolenaard2b58c02018-09-16 18:10:48 +02003437 ex_diffupdate(NULL);
Bram Moolenaardf77cef2018-10-07 17:46:42 +02003438
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +02003439 // Check that the cursor is on a valid character and update its
Bram Moolenaardf77cef2018-10-07 17:46:42 +02003440 // position. When there were filler lines the topline has become
3441 // invalid.
3442 check_cursor();
3443 changed_line_abv_curs();
3444
3445 if (diff_need_update)
3446 // redraw already done by ex_diffupdate()
3447 diff_need_update = FALSE;
Bram Moolenaar198fa062018-09-18 21:20:26 +02003448 else
3449 {
Bram Moolenaar198fa062018-09-18 21:20:26 +02003450 // Also need to redraw the other buffers.
3451 diff_redraw(FALSE);
3452 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
3453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454}
3455
3456#ifdef FEAT_FOLDING
3457/*
3458 * Update folds for all diff buffers for entry "dp".
3459 * Skip buffer with index "skip_idx".
3460 * When there are no diffs, all folds are removed.
3461 */
3462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003463diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
3465 int i;
3466 win_T *wp;
3467
Bram Moolenaar29323592016-07-24 22:04:11 +02003468 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003470 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 foldUpdate(wp, dp->df_lnum[i],
3472 dp->df_lnum[i] + dp->df_count[i]);
3473}
3474#endif
3475
3476/*
3477 * Return TRUE if buffer "buf" is in diff-mode.
3478 */
3479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003480diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003482 tabpage_T *tp;
3483
Bram Moolenaar29323592016-07-24 22:04:11 +02003484 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003485 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
3486 return TRUE;
3487 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488}
3489
3490/*
3491 * Move "count" times in direction "dir" to the next diff block.
3492 * Return FAIL if there isn't such a diff block.
3493 */
3494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003495diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 int idx;
3498 linenr_T lnum = curwin->w_cursor.lnum;
3499 diff_T *dp;
3500
3501 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003502 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return FAIL;
3504
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003505 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003506 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 if (curtab->tp_first_diff == NULL) // no diffs today
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 return FAIL;
3510
3511 while (--count >= 0)
3512 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003513 // Check if already before first diff.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003514 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 break;
3516
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003517 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 if (dp == NULL)
3520 break;
3521 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
3522 || (dir == BACKWARD
3523 && (dp->df_next == NULL
3524 || lnum <= dp->df_next->df_lnum[idx])))
3525 {
3526 lnum = dp->df_lnum[idx];
3527 break;
3528 }
3529 }
3530 }
3531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003532 // don't end up past the end of the file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (lnum > curbuf->b_ml.ml_line_count)
3534 lnum = curbuf->b_ml.ml_line_count;
3535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003536 // When the cursor didn't move at all we fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (lnum == curwin->w_cursor.lnum)
3538 return FAIL;
3539
3540 setpcmark();
3541 curwin->w_cursor.lnum = lnum;
3542 curwin->w_cursor.col = 0;
3543
3544 return OK;
3545}
3546
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003547/*
3548 * Return the line number in the current window that is closest to "lnum1" in
3549 * "buf1" in diff mode.
3550 */
3551 static linenr_T
3552diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003554 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555{
3556 int idx1;
3557 int idx2;
3558 diff_T *dp;
3559 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003560
3561 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003562 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
3564 return lnum1;
3565
3566 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003567 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar860cae12010-06-05 23:22:07 +02003568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003569 if (curtab->tp_first_diff == NULL) // no diffs today
Bram Moolenaar860cae12010-06-05 23:22:07 +02003570 return lnum1;
3571
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003572 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573 {
3574 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003575 return lnum1 - baseline;
3576 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003578 // Inside the diffblock
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579 baseline = lnum1 - dp->df_lnum[idx1];
3580 if (baseline > dp->df_count[idx2])
3581 baseline = dp->df_count[idx2];
3582
3583 return dp->df_lnum[idx2] + baseline;
3584 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003585 if ( (dp->df_lnum[idx1] == lnum1)
3586 && (dp->df_count[idx1] == 0)
3587 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
3588 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
3589 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 /*
3591 * Special case: if the cursor is just after a zero-count
3592 * block (i.e. all filler) and the target cursor is already
3593 * inside the corresponding block, leave the target cursor
3594 * unmoved. This makes repeated CTRL-W W operations work
3595 * as expected.
3596 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003597 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
3599 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
3600 }
3601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003602 // If we get here then the cursor is after the last diff
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003603 return lnum1 - baseline;
3604}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003605
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003606/*
3607 * Return the line number in the current window that is closest to "lnum1" in
3608 * "buf1" in diff mode. Checks the line number to be valid.
3609 */
3610 linenr_T
3611diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
3612{
3613 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
3614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003615 // don't end up past the end of the file
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003616 if (lnum > curbuf->b_ml.ml_line_count)
3617 return curbuf->b_ml.ml_line_count;
3618 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621/*
3622 * For line "lnum" in the current window find the equivalent lnum in window
3623 * "wp", compensating for inserted/deleted lines.
3624 */
3625 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
3628 diff_T *dp;
3629 int idx;
3630 int i;
3631 linenr_T n;
3632
3633 idx = diff_buf_idx(curbuf);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003634 if (idx == DB_COUNT) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 return (linenr_T)0;
3636
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003637 if (curtab->tp_diff_invalid)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003638 ex_diffupdate(NULL); // update after a big change
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003640 // search for a change that includes "lnum" in the list of diffblocks.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003641 FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
3643 break;
3644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003645 // When after the last change, compute relative to the last line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (dp == NULL)
3647 return wp->w_buffer->b_ml.ml_line_count
3648 - (curbuf->b_ml.ml_line_count - lnum);
3649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003650 // Find index for "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 i = diff_buf_idx(wp->w_buffer);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003652 if (i == DB_COUNT) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return (linenr_T)0;
3654
3655 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
3656 if (n > dp->df_lnum[i] + dp->df_count[i])
3657 n = dp->df_lnum[i] + dp->df_count[i];
3658 return n;
3659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaare828b762018-09-10 17:51:58 +02003661/*
3662 * Handle an ED style diff line.
3663 * Return FAIL if the line does not contain diff info.
3664 */
3665 static int
3666parse_diff_ed(
3667 char_u *line,
Lewis Russelld9da86e2021-12-28 13:54:41 +00003668 diffhunk_T *hunk)
Bram Moolenaare828b762018-09-10 17:51:58 +02003669{
3670 char_u *p;
3671 long f1, l1, f2, l2;
3672 int difftype;
3673
3674 // The line must be one of three formats:
3675 // change: {first}[,{last}]c{first}[,{last}]
3676 // append: {first}a{first}[,{last}]
3677 // delete: {first}[,{last}]d{first}
3678 p = line;
3679 f1 = getdigits(&p);
3680 if (*p == ',')
3681 {
3682 ++p;
3683 l1 = getdigits(&p);
3684 }
3685 else
3686 l1 = f1;
3687 if (*p != 'a' && *p != 'c' && *p != 'd')
3688 return FAIL; // invalid diff format
3689 difftype = *p++;
3690 f2 = getdigits(&p);
3691 if (*p == ',')
3692 {
3693 ++p;
3694 l2 = getdigits(&p);
3695 }
3696 else
3697 l2 = f2;
3698 if (l1 < f1 || l2 < f2)
3699 return FAIL;
3700
3701 if (difftype == 'a')
3702 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003703 hunk->lnum_orig = f1 + 1;
3704 hunk->count_orig = 0;
Bram Moolenaare828b762018-09-10 17:51:58 +02003705 }
3706 else
3707 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003708 hunk->lnum_orig = f1;
3709 hunk->count_orig = l1 - f1 + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +02003710 }
3711 if (difftype == 'd')
3712 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003713 hunk->lnum_new = f2 + 1;
3714 hunk->count_new = 0;
Bram Moolenaare828b762018-09-10 17:51:58 +02003715 }
3716 else
3717 {
Lewis Russelld9da86e2021-12-28 13:54:41 +00003718 hunk->lnum_new = f2;
3719 hunk->count_new = l2 - f2 + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +02003720 }
3721 return OK;
3722}
3723
3724/*
3725 * Parses unified diff with zero(!) context lines.
3726 * Return FAIL if there is no diff information in "line".
3727 */
3728 static int
3729parse_diff_unified(
3730 char_u *line,
Lewis Russelld9da86e2021-12-28 13:54:41 +00003731 diffhunk_T *hunk)
Bram Moolenaare828b762018-09-10 17:51:58 +02003732{
3733 char_u *p;
3734 long oldline, oldcount, newline, newcount;
3735
3736 // Parse unified diff hunk header:
3737 // @@ -oldline,oldcount +newline,newcount @@
3738 p = line;
3739 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3740 {
3741 oldline = getdigits(&p);
3742 if (*p == ',')
3743 {
3744 ++p;
3745 oldcount = getdigits(&p);
3746 }
3747 else
3748 oldcount = 1;
3749 if (*p++ == ' ' && *p++ == '+')
3750 {
3751 newline = getdigits(&p);
3752 if (*p == ',')
3753 {
3754 ++p;
3755 newcount = getdigits(&p);
3756 }
3757 else
3758 newcount = 1;
3759 }
3760 else
3761 return FAIL; // invalid diff format
3762
3763 if (oldcount == 0)
3764 oldline += 1;
3765 if (newcount == 0)
3766 newline += 1;
3767 if (newline == 0)
3768 newline = 1;
3769
Lewis Russelld9da86e2021-12-28 13:54:41 +00003770 hunk->lnum_orig = oldline;
3771 hunk->count_orig = oldcount;
3772 hunk->lnum_new = newline;
3773 hunk->count_new = newcount;
Bram Moolenaare828b762018-09-10 17:51:58 +02003774
3775 return OK;
3776 }
3777
3778 return FAIL;
3779}
3780
3781/*
3782 * Callback function for the xdl_diff() function.
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003783 * Stores the diff output (indices) in a grow array.
Bram Moolenaare828b762018-09-10 17:51:58 +02003784 */
3785 static int
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003786xdiff_out_indices(
Lewis Russelld9da86e2021-12-28 13:54:41 +00003787 long start_a,
3788 long count_a,
3789 long start_b,
3790 long count_b,
3791 void *priv)
Bram Moolenaare828b762018-09-10 17:51:58 +02003792{
3793 diffout_T *dout = (diffout_T *)priv;
Lewis Russelld9da86e2021-12-28 13:54:41 +00003794 diffhunk_T *p = ALLOC_ONE(diffhunk_T);
Bram Moolenaare828b762018-09-10 17:51:58 +02003795
Lewis Russelld9da86e2021-12-28 13:54:41 +00003796 if (p == NULL)
3797 return -1;
Bram Moolenaarf080d702018-10-31 22:57:26 +01003798
3799 if (ga_grow(&dout->dout_ga, 1) == FAIL)
Bram Moolenaarfebb78f2021-12-29 11:59:53 +00003800 {
3801 vim_free(p);
Bram Moolenaarf080d702018-10-31 22:57:26 +01003802 return -1;
Bram Moolenaarfebb78f2021-12-29 11:59:53 +00003803 }
Lewis Russelld9da86e2021-12-28 13:54:41 +00003804
3805 p->lnum_orig = start_a + 1;
3806 p->count_orig = count_a;
3807 p->lnum_new = start_b + 1;
3808 p->count_new = count_b;
3809 ((diffhunk_T **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
Bram Moolenaare828b762018-09-10 17:51:58 +02003810 return 0;
3811}
3812
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003813/*
3814 * Callback function for the xdl_diff() function.
3815 * Stores the unified diff output in a grow array.
3816 */
3817 static int
3818xdiff_out_unified(
3819 void *priv,
3820 mmbuffer_t *mb,
3821 int nbuf)
3822{
3823 diffout_T *dout = (diffout_T *)priv;
3824 int i;
3825
3826 for (i = 0; i < nbuf; i++)
3827 ga_concat_len(&dout->dout_ga, (char_u *)mb[i].ptr, mb[i].size);
3828
3829 return 0;
3830}
3831
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003832#endif // FEAT_DIFF
3833
3834#if defined(FEAT_EVAL) || defined(PROTO)
3835
3836/*
3837 * "diff_filler()" function
3838 */
3839 void
3840f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3841{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003842# ifdef FEAT_DIFF
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003843 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
3844 return;
3845
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003846 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003847# endif
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003848}
3849
3850/*
3851 * "diff_hlID()" function
3852 */
3853 void
3854f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3855{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003856# ifdef FEAT_DIFF
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003857 linenr_T lnum;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003858 static linenr_T prev_lnum = 0;
3859 static varnumber_T changedtick = 0;
3860 static int fnum = 0;
3861 static int change_start = 0;
3862 static int change_end = 0;
3863 static hlf_T hlID = (hlf_T)0;
3864 int filler_lines;
3865 int col;
3866
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003867 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02003868 && (check_for_lnum_arg(argvars,0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02003869 || check_for_number_arg(argvars, 1) == FAIL))
3870 return;
3871
3872 lnum = tv_get_lnum(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003873 if (lnum < 0) // ignore type error in {lnum} arg
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003874 lnum = 0;
3875 if (lnum != prev_lnum
3876 || changedtick != CHANGEDTICK(curbuf)
3877 || fnum != curbuf->b_fnum)
3878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003879 // New line, buffer, change: need to get the values.
Jonathon7c7a4e62025-01-12 09:58:00 +01003880 int linestatus = 0;
3881 filler_lines = diff_check_with_linestatus(curwin, lnum, &linestatus);
3882 if (filler_lines < 0 || linestatus < 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003883 {
Jonathon7c7a4e62025-01-12 09:58:00 +01003884 if (filler_lines == -1 || linestatus == -1)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003885 {
3886 change_start = MAXCOL;
3887 change_end = -1;
3888 if (diff_find_change(curwin, lnum, &change_start, &change_end))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003889 hlID = HLF_ADD; // added line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003890 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003891 hlID = HLF_CHD; // changed line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003892 }
3893 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003894 hlID = HLF_ADD; // added line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003895 }
3896 else
3897 hlID = (hlf_T)0;
3898 prev_lnum = lnum;
3899 changedtick = CHANGEDTICK(curbuf);
3900 fnum = curbuf->b_fnum;
3901 }
3902
3903 if (hlID == HLF_CHD || hlID == HLF_TXD)
3904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003905 col = tv_get_number(&argvars[1]) - 1; // ignore type error in {col}
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003906 if (col >= change_start && col <= change_end)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003907 hlID = HLF_TXD; // changed text
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003908 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 hlID = HLF_CHD; // changed line
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003910 }
3911 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003912# endif
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003913}
3914
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01003915# ifdef FEAT_DIFF
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003916/*
3917 * Parse the diff options passed in "optarg" to the diff() function and return
3918 * the options in "diffopts" and the diff algorithm in "diffalgo".
3919 */
3920 static int
3921parse_diff_optarg(
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003922 typval_T *opts,
3923 int *diffopts,
3924 long *diffalgo,
3925 dio_outfmt_T *diff_output_fmt,
3926 int *diff_ctxlen)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003927{
3928 dict_T *d = opts->vval.v_dict;
3929
3930 char_u *algo = dict_get_string(d, "algorithm", FALSE);
3931 if (algo != NULL)
3932 {
3933 if (STRNCMP(algo, "myers", 5) == 0)
3934 *diffalgo = 0;
3935 else if (STRNCMP(algo, "minimal", 7) == 0)
3936 *diffalgo = XDF_NEED_MINIMAL;
3937 else if (STRNCMP(algo, "patience", 8) == 0)
3938 *diffalgo = XDF_PATIENCE_DIFF;
3939 else if (STRNCMP(algo, "histogram", 9) == 0)
3940 *diffalgo = XDF_HISTOGRAM_DIFF;
3941 }
3942
3943 char_u *output_fmt = dict_get_string(d, "output", FALSE);
3944 if (output_fmt != NULL)
3945 {
3946 if (STRNCMP(output_fmt, "unified", 7) == 0)
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003947 *diff_output_fmt = DIO_OUTPUT_UNIFIED;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003948 else if (STRNCMP(output_fmt, "indices", 7) == 0)
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003949 *diff_output_fmt = DIO_OUTPUT_INDICES;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003950 else
3951 {
3952 semsg(_(e_unsupported_diff_output_format_str), output_fmt);
3953 return FAIL;
3954 }
3955 }
3956
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01003957 *diff_ctxlen = dict_get_number_def(d, "context", 0);
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003958 if (*diff_ctxlen < 0)
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01003959 *diff_ctxlen = 0;
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01003960
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01003961 if (dict_get_bool(d, "iblank", FALSE))
3962 *diffopts |= DIFF_IBLANK;
3963 if (dict_get_bool(d, "icase", FALSE))
3964 *diffopts |= DIFF_ICASE;
3965 if (dict_get_bool(d, "iwhite", FALSE))
3966 *diffopts |= DIFF_IWHITE;
3967 if (dict_get_bool(d, "iwhiteall", FALSE))
3968 *diffopts |= DIFF_IWHITEALL;
3969 if (dict_get_bool(d, "iwhiteeol", FALSE))
3970 *diffopts |= DIFF_IWHITEEOL;
3971 if (dict_get_bool(d, "indent-heuristic", FALSE))
3972 *diffalgo |= XDF_INDENT_HEURISTIC;
3973
3974 return OK;
3975}
3976
3977/*
3978 * Concatenate the List of strings in "l" and store the result in
3979 * "din->din_mmfile.ptr" and the length in "din->din_mmfile.size".
3980 */
3981 static void
3982list_to_diffin(list_T *l, diffin_T *din, int icase)
3983{
3984 garray_T ga;
3985 listitem_T *li;
3986 char_u *str;
3987
3988 ga_init2(&ga, 512, 4);
3989
3990 FOR_ALL_LIST_ITEMS(l, li)
3991 {
3992 str = tv_get_string(&li->li_tv);
3993 if (icase)
3994 {
3995 str = strlow_save(str);
3996 if (str == NULL)
3997 continue;
3998 }
3999 ga_concat(&ga, str);
4000 ga_concat(&ga, (char_u *)NL_STR);
4001 if (icase)
4002 vim_free(str);
4003 }
4004 if (ga.ga_len > 0)
4005 ((char *)ga.ga_data)[ga.ga_len] = NUL;
4006
4007 din->din_mmfile.ptr = (char *)ga.ga_data;
4008 din->din_mmfile.size = ga.ga_len;
4009}
4010
4011/*
4012 * Get the start and end indices from the diff "hunk".
4013 */
4014 static dict_T *
4015get_diff_hunk_indices(diffhunk_T *hunk)
4016{
4017 dict_T *hunk_dict;
4018
4019 hunk_dict = dict_alloc();
4020 if (hunk_dict == NULL)
4021 return NULL;
4022
4023 dict_add_number(hunk_dict, "from_idx", hunk->lnum_orig - 1);
4024 dict_add_number(hunk_dict, "from_count", hunk->count_orig);
4025 dict_add_number(hunk_dict, "to_idx", hunk->lnum_new - 1);
4026 dict_add_number(hunk_dict, "to_count", hunk->count_new);
4027
4028 return hunk_dict;
4029}
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004030# endif
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004031
4032/*
4033 * "diff()" function
4034 */
4035 void
4036f_diff(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4037{
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004038# ifdef FEAT_DIFF
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004039 diffio_T dio;
4040
4041 if (check_for_nonnull_list_arg(argvars, 0) == FAIL
4042 || check_for_nonnull_list_arg(argvars, 1) == FAIL
4043 || check_for_opt_nonnull_dict_arg(argvars, 2) == FAIL)
4044 return;
4045
4046 CLEAR_FIELD(dio);
4047 dio.dio_internal = TRUE;
4048 ga_init2(&dio.dio_diff.dout_ga, sizeof(char *), 1000);
4049
4050 list_T *orig_list = argvars[0].vval.v_list;
4051 list_T *new_list = argvars[1].vval.v_list;
4052
4053 // Save the 'diffopt' option value and restore it after getting the diff.
4054 int save_diff_flags = diff_flags;
4055 long save_diff_algorithm = diff_algorithm;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004056 diff_flags = DIFF_INTERNAL;
4057 diff_algorithm = 0;
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004058 dio.dio_outfmt = DIO_OUTPUT_UNIFIED;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004059 if (argvars[2].v_type != VAR_UNKNOWN)
4060 if (parse_diff_optarg(&argvars[2], &diff_flags, &diff_algorithm,
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004061 &dio.dio_outfmt, &dio.dio_ctxlen) == FAIL)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004062 return;
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004063
4064 // Concatenate the List of strings into a single string using newline
4065 // separator. Internal diff library expects a single string.
4066 list_to_diffin(orig_list, &dio.dio_orig, diff_flags & DIFF_ICASE);
4067 list_to_diffin(new_list, &dio.dio_new, diff_flags & DIFF_ICASE);
4068
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004069 // If 'diffexpr' is set, then the internal diff is not used. Set
4070 // 'diffexpr' to an empty string temporarily.
4071 int restore_diffexpr = FALSE;
4072 char_u cc = *p_dex;
4073 if (*p_dex != NUL)
4074 {
4075 restore_diffexpr = TRUE;
4076 *p_dex = NUL;
4077 }
4078
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004079 // Compute the diff
4080 int diff_status = diff_file(&dio);
4081
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004082 // restore 'diffexpr'
4083 if (restore_diffexpr)
4084 *p_dex = cc;
4085
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004086 if (diff_status == FAIL)
4087 goto done;
4088
4089 int hunk_idx = 0;
4090 dict_T *hunk_dict;
4091
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004092 if (dio.dio_outfmt == DIO_OUTPUT_INDICES)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004093 {
4094 if (rettv_list_alloc(rettv) != OK)
4095 goto done;
4096 list_T *l = rettv->vval.v_list;
4097
4098 // Process each diff hunk
4099 diffhunk_T *hunk = NULL;
4100 while (hunk_idx < dio.dio_diff.dout_ga.ga_len)
4101 {
4102 hunk = ((diffhunk_T **)dio.dio_diff.dout_ga.ga_data)[hunk_idx++];
4103
4104 hunk_dict = get_diff_hunk_indices(hunk);
4105 if (hunk_dict == NULL)
4106 goto done;
4107
4108 list_append_dict(l, hunk_dict);
4109 }
4110 }
4111 else
4112 {
4113 ga_append(&dio.dio_diff.dout_ga, NUL);
4114 rettv->v_type = VAR_STRING;
4115 rettv->vval.v_string =
4116 vim_strsave((char_u *)dio.dio_diff.dout_ga.ga_data);
4117 }
4118
4119done:
4120 clear_diffin(&dio.dio_new);
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01004121 if (dio.dio_outfmt == DIO_OUTPUT_INDICES)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004122 clear_diffout(&dio.dio_diff);
4123 else
4124 ga_clear(&dio.dio_diff.dout_ga);
4125 clear_diffin(&dio.dio_orig);
4126 // Restore the 'diffopt' option value.
4127 diff_flags = save_diff_flags;
4128 diff_algorithm = save_diff_algorithm;
Yegappan Lakshmanan60937032024-02-03 17:41:54 +01004129# endif
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01004130}
4131
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004132#endif