Mercurial > vim
annotate src/diff.c @ 8982:57e814658ff0
Added tag v7.4.1776 for changeset 3b51b0aeb9a37160639fea313b7346b8db752fe5
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 22 Apr 2016 20:30:05 +0200 |
parents | db2a07b710ed |
children | f094d4085014 |
rev | line source |
---|---|
7 | 1 /* vim:set ts=8 sts=4 sw=4: |
2 * | |
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 /* | |
1788 | 11 * diff.c: code for diff'ing two, three or four buffers. |
7 | 12 */ |
13 | |
14 #include "vim.h" | |
15 | |
16 #if defined(FEAT_DIFF) || defined(PROTO) | |
17 | |
18 static int diff_busy = FALSE; /* ex_diffgetput() is busy */ | |
19 | |
20 /* flags obtained from the 'diffopt' option */ | |
21 #define DIFF_FILLER 1 /* display filler lines */ | |
22 #define DIFF_ICASE 2 /* ignore case */ | |
23 #define DIFF_IWHITE 4 /* ignore change in white space */ | |
764 | 24 #define DIFF_HORIZONTAL 8 /* horizontal splits */ |
25 #define DIFF_VERTICAL 16 /* vertical splits */ | |
7 | 26 static int diff_flags = DIFF_FILLER; |
27 | |
28 #define LBUFLEN 50 /* length of line in diff file */ | |
29 | |
30 static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it | |
31 doesn't work, MAYBE when not checked yet */ | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7817
diff
changeset
|
32 #if defined(MSWIN) |
7 | 33 static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE |
34 when it doesn't work, MAYBE when not | |
35 checked yet */ | |
36 #endif | |
37 | |
7799
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
38 static int diff_buf_idx(buf_T *buf); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
39 static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
40 static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
41 static void diff_check_unchanged(tabpage_T *tp, diff_T *dp); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
42 static int diff_check_sanity(tabpage_T *tp, diff_T *dp); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
43 static void diff_redraw(int dofold); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
44 static int diff_write(buf_T *buf, char_u *fname); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
45 static void diff_file(char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
46 static int diff_equal_entry(diff_T *dp, int idx1, int idx2); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
47 static int diff_cmp(char_u *s1, char_u *s2); |
7 | 48 #ifdef FEAT_FOLDING |
7799
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
49 static void diff_fold_update(diff_T *dp, int skip_idx); |
7 | 50 #endif |
7799
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
51 static void diff_read(int idx_orig, int idx_new, char_u *fname); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
52 static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new); |
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7338
diff
changeset
|
53 static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp); |
7 | 54 |
55 #ifndef USE_CR | |
56 # define tag_fgets vim_fgets | |
57 #endif | |
58 | |
59 /* | |
60 * Called when deleting or unloading a buffer: No longer make a diff with it. | |
61 */ | |
62 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
63 diff_buf_delete(buf_T *buf) |
7 | 64 { |
65 int i; | |
672 | 66 tabpage_T *tp; |
7 | 67 |
672 | 68 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) |
7 | 69 { |
672 | 70 i = diff_buf_idx_tp(buf, tp); |
71 if (i != DB_COUNT) | |
72 { | |
73 tp->tp_diffbuf[i] = NULL; | |
74 tp->tp_diff_invalid = TRUE; | |
1761 | 75 if (tp == curtab) |
76 diff_redraw(TRUE); | |
672 | 77 } |
7 | 78 } |
79 } | |
80 | |
81 /* | |
16 | 82 * Check if the current buffer should be added to or removed from the list of |
83 * diff buffers. | |
84 */ | |
85 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
86 diff_buf_adjust(win_T *win) |
16 | 87 { |
88 win_T *wp; | |
672 | 89 int i; |
16 | 90 |
91 if (!win->w_p_diff) | |
92 { | |
93 /* When there is no window showing a diff for this buffer, remove | |
94 * it from the diffs. */ | |
95 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
96 if (wp->w_buffer == win->w_buffer && wp->w_p_diff) | |
97 break; | |
98 if (wp == NULL) | |
672 | 99 { |
100 i = diff_buf_idx(win->w_buffer); | |
101 if (i != DB_COUNT) | |
102 { | |
103 curtab->tp_diffbuf[i] = NULL; | |
104 curtab->tp_diff_invalid = TRUE; | |
1761 | 105 diff_redraw(TRUE); |
672 | 106 } |
107 } | |
16 | 108 } |
109 else | |
110 diff_buf_add(win->w_buffer); | |
111 } | |
112 | |
113 /* | |
7 | 114 * Add a buffer to make diffs for. |
672 | 115 * Call this when a new buffer is being edited in the current window where |
116 * 'diff' is set. | |
1788 | 117 * Marks the current buffer as being part of the diff and requiring updating. |
672 | 118 * This must be done before any autocmd, because a command may use info |
119 * about the screen contents. | |
7 | 120 */ |
121 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
122 diff_buf_add(buf_T *buf) |
7 | 123 { |
124 int i; | |
125 | |
126 if (diff_buf_idx(buf) != DB_COUNT) | |
127 return; /* It's already there. */ | |
128 | |
129 for (i = 0; i < DB_COUNT; ++i) | |
672 | 130 if (curtab->tp_diffbuf[i] == NULL) |
7 | 131 { |
672 | 132 curtab->tp_diffbuf[i] = buf; |
133 curtab->tp_diff_invalid = TRUE; | |
1761 | 134 diff_redraw(TRUE); |
7 | 135 return; |
136 } | |
137 | |
138 EMSGN(_("E96: Can not diff more than %ld buffers"), DB_COUNT); | |
139 } | |
140 | |
141 /* | |
672 | 142 * Find buffer "buf" in the list of diff buffers for the current tab page. |
7 | 143 * Return its index or DB_COUNT if not found. |
144 */ | |
145 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
146 diff_buf_idx(buf_T *buf) |
7 | 147 { |
148 int idx; | |
149 | |
150 for (idx = 0; idx < DB_COUNT; ++idx) | |
672 | 151 if (curtab->tp_diffbuf[idx] == buf) |
152 break; | |
153 return idx; | |
154 } | |
155 | |
156 /* | |
157 * Find buffer "buf" in the list of diff buffers for tab page "tp". | |
158 * Return its index or DB_COUNT if not found. | |
159 */ | |
160 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
161 diff_buf_idx_tp(buf_T *buf, tabpage_T *tp) |
672 | 162 { |
163 int idx; | |
164 | |
165 for (idx = 0; idx < DB_COUNT; ++idx) | |
166 if (tp->tp_diffbuf[idx] == buf) | |
7 | 167 break; |
168 return idx; | |
169 } | |
170 | |
171 /* | |
672 | 172 * Mark the diff info involving buffer "buf" as invalid, it will be updated |
173 * when info is requested. | |
7 | 174 */ |
175 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
176 diff_invalidate(buf_T *buf) |
7 | 177 { |
672 | 178 tabpage_T *tp; |
179 int i; | |
180 | |
181 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) | |
7 | 182 { |
672 | 183 i = diff_buf_idx_tp(buf, tp); |
184 if (i != DB_COUNT) | |
185 { | |
186 tp->tp_diff_invalid = TRUE; | |
187 if (tp == curtab) | |
188 diff_redraw(TRUE); | |
189 } | |
7 | 190 } |
191 } | |
192 | |
193 /* | |
672 | 194 * Called by mark_adjust(): update line numbers in "curbuf". |
7 | 195 */ |
196 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
197 diff_mark_adjust( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
198 linenr_T line1, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
199 linenr_T line2, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
200 long amount, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
201 long amount_after) |
7 | 202 { |
672 | 203 int idx; |
204 tabpage_T *tp; | |
205 | |
206 /* Handle all tab pages that use the current buffer in a diff. */ | |
207 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) | |
208 { | |
209 idx = diff_buf_idx_tp(curbuf, tp); | |
210 if (idx != DB_COUNT) | |
211 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after); | |
212 } | |
213 } | |
214 | |
215 /* | |
216 * Update line numbers in tab page "tp" for "curbuf" with index "idx". | |
217 * This attempts to update the changes as much as possible: | |
218 * When inserting/deleting lines outside of existing change blocks, create a | |
219 * new change block and update the line numbers in following blocks. | |
220 * When inserting/deleting lines in existing change blocks, update them. | |
221 */ | |
222 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
223 diff_mark_adjust_tp( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
224 tabpage_T *tp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
225 int idx, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
226 linenr_T line1, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
227 linenr_T line2, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
228 long amount, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
229 long amount_after) |
672 | 230 { |
7 | 231 diff_T *dp; |
232 diff_T *dprev; | |
233 diff_T *dnext; | |
234 int i; | |
235 int inserted, deleted; | |
236 int n, off; | |
237 linenr_T last; | |
238 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */ | |
239 int check_unchanged; | |
240 | |
241 if (line2 == MAXLNUM) | |
242 { | |
243 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */ | |
244 inserted = amount; | |
245 deleted = 0; | |
246 } | |
247 else if (amount_after > 0) | |
248 { | |
249 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/ | |
250 inserted = amount_after; | |
251 deleted = 0; | |
252 } | |
253 else | |
254 { | |
255 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */ | |
256 inserted = 0; | |
257 deleted = -amount_after; | |
258 } | |
259 | |
260 dprev = NULL; | |
672 | 261 dp = tp->tp_first_diff; |
7 | 262 for (;;) |
263 { | |
264 /* If the change is after the previous diff block and before the next | |
265 * diff block, thus not touching an existing change, create a new diff | |
266 * block. Don't do this when ex_diffgetput() is busy. */ | |
267 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2 | |
268 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1)) | |
269 && (dprev == NULL | |
270 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1) | |
271 && !diff_busy) | |
272 { | |
672 | 273 dnext = diff_alloc_new(tp, dprev, dp); |
7 | 274 if (dnext == NULL) |
275 return; | |
276 | |
277 dnext->df_lnum[idx] = line1; | |
278 dnext->df_count[idx] = inserted; | |
279 for (i = 0; i < DB_COUNT; ++i) | |
672 | 280 if (tp->tp_diffbuf[i] != NULL && i != idx) |
7 | 281 { |
282 if (dprev == NULL) | |
283 dnext->df_lnum[i] = line1; | |
284 else | |
285 dnext->df_lnum[i] = line1 | |
286 + (dprev->df_lnum[i] + dprev->df_count[i]) | |
287 - (dprev->df_lnum[idx] + dprev->df_count[idx]); | |
288 dnext->df_count[i] = deleted; | |
289 } | |
290 } | |
291 | |
292 /* if at end of the list, quit */ | |
293 if (dp == NULL) | |
294 break; | |
295 | |
296 /* | |
297 * Check for these situations: | |
298 * 1 2 3 | |
299 * 1 2 3 | |
300 * line1 2 3 4 5 | |
301 * 2 3 4 5 | |
302 * 2 3 4 5 | |
303 * line2 2 3 4 5 | |
304 * 3 5 6 | |
305 * 3 5 6 | |
306 */ | |
307 /* compute last line of this change */ | |
308 last = dp->df_lnum[idx] + dp->df_count[idx] - 1; | |
309 | |
310 /* 1. change completely above line1: nothing to do */ | |
311 if (last >= line1 - 1) | |
312 { | |
313 /* 6. change below line2: only adjust for amount_after; also when | |
314 * "deleted" became zero when deleted all lines between two diffs */ | |
315 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2) | |
316 { | |
317 if (amount_after == 0) | |
318 break; /* nothing left to change */ | |
319 dp->df_lnum[idx] += amount_after; | |
320 } | |
321 else | |
322 { | |
323 check_unchanged = FALSE; | |
324 | |
325 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */ | |
326 if (deleted > 0) | |
327 { | |
328 if (dp->df_lnum[idx] >= line1) | |
329 { | |
330 off = dp->df_lnum[idx] - lnum_deleted; | |
331 if (last <= line2) | |
332 { | |
333 /* 4. delete all lines of diff */ | |
334 if (dp->df_next != NULL | |
335 && dp->df_next->df_lnum[idx] - 1 <= line2) | |
336 { | |
337 /* delete continues in next diff, only do | |
338 * lines until that one */ | |
339 n = dp->df_next->df_lnum[idx] - lnum_deleted; | |
340 deleted -= n; | |
341 n -= dp->df_count[idx]; | |
342 lnum_deleted = dp->df_next->df_lnum[idx]; | |
343 } | |
344 else | |
345 n = deleted - dp->df_count[idx]; | |
346 dp->df_count[idx] = 0; | |
347 } | |
348 else | |
349 { | |
350 /* 5. delete lines at or just before top of diff */ | |
351 n = off; | |
352 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1; | |
353 check_unchanged = TRUE; | |
354 } | |
355 dp->df_lnum[idx] = line1; | |
356 } | |
357 else | |
358 { | |
359 off = 0; | |
360 if (last < line2) | |
361 { | |
362 /* 2. delete at end of of diff */ | |
363 dp->df_count[idx] -= last - lnum_deleted + 1; | |
364 if (dp->df_next != NULL | |
365 && dp->df_next->df_lnum[idx] - 1 <= line2) | |
366 { | |
367 /* delete continues in next diff, only do | |
368 * lines until that one */ | |
369 n = dp->df_next->df_lnum[idx] - 1 - last; | |
370 deleted -= dp->df_next->df_lnum[idx] | |
371 - lnum_deleted; | |
372 lnum_deleted = dp->df_next->df_lnum[idx]; | |
373 } | |
374 else | |
375 n = line2 - last; | |
376 check_unchanged = TRUE; | |
377 } | |
378 else | |
379 { | |
380 /* 3. delete lines inside the diff */ | |
381 n = 0; | |
382 dp->df_count[idx] -= deleted; | |
383 } | |
384 } | |
385 | |
386 for (i = 0; i < DB_COUNT; ++i) | |
672 | 387 if (tp->tp_diffbuf[i] != NULL && i != idx) |
7 | 388 { |
389 dp->df_lnum[i] -= off; | |
390 dp->df_count[i] += n; | |
391 } | |
392 } | |
393 else | |
394 { | |
395 if (dp->df_lnum[idx] <= line1) | |
396 { | |
397 /* inserted lines somewhere in this diff */ | |
398 dp->df_count[idx] += inserted; | |
399 check_unchanged = TRUE; | |
400 } | |
401 else | |
402 /* inserted lines somewhere above this diff */ | |
403 dp->df_lnum[idx] += inserted; | |
404 } | |
405 | |
406 if (check_unchanged) | |
407 /* Check if inserted lines are equal, may reduce the | |
408 * size of the diff. TODO: also check for equal lines | |
409 * in the middle and perhaps split the block. */ | |
672 | 410 diff_check_unchanged(tp, dp); |
7 | 411 } |
412 } | |
413 | |
414 /* check if this block touches the previous one, may merge them. */ | |
415 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx] | |
416 == dp->df_lnum[idx]) | |
417 { | |
418 for (i = 0; i < DB_COUNT; ++i) | |
672 | 419 if (tp->tp_diffbuf[i] != NULL) |
7 | 420 dprev->df_count[i] += dp->df_count[i]; |
421 dprev->df_next = dp->df_next; | |
422 vim_free(dp); | |
423 dp = dprev->df_next; | |
424 } | |
425 else | |
426 { | |
427 /* Advance to next entry. */ | |
428 dprev = dp; | |
429 dp = dp->df_next; | |
430 } | |
431 } | |
432 | |
433 dprev = NULL; | |
672 | 434 dp = tp->tp_first_diff; |
7 | 435 while (dp != NULL) |
436 { | |
437 /* All counts are zero, remove this entry. */ | |
438 for (i = 0; i < DB_COUNT; ++i) | |
672 | 439 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0) |
7 | 440 break; |
441 if (i == DB_COUNT) | |
442 { | |
443 dnext = dp->df_next; | |
444 vim_free(dp); | |
445 dp = dnext; | |
446 if (dprev == NULL) | |
672 | 447 tp->tp_first_diff = dnext; |
7 | 448 else |
449 dprev->df_next = dnext; | |
450 } | |
451 else | |
452 { | |
453 /* Advance to next entry. */ | |
454 dprev = dp; | |
455 dp = dp->df_next; | |
456 } | |
457 | |
458 } | |
672 | 459 |
460 if (tp == curtab) | |
461 { | |
462 diff_redraw(TRUE); | |
7 | 463 |
672 | 464 /* Need to recompute the scroll binding, may remove or add filler |
465 * lines (e.g., when adding lines above w_topline). But it's slow when | |
466 * making many changes, postpone until redrawing. */ | |
467 diff_need_scrollbind = TRUE; | |
468 } | |
7 | 469 } |
470 | |
471 /* | |
472 * Allocate a new diff block and link it between "dprev" and "dp". | |
473 */ | |
474 static diff_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
475 diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp) |
7 | 476 { |
477 diff_T *dnew; | |
478 | |
479 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T)); | |
480 if (dnew != NULL) | |
481 { | |
482 dnew->df_next = dp; | |
483 if (dprev == NULL) | |
672 | 484 tp->tp_first_diff = dnew; |
7 | 485 else |
486 dprev->df_next = dnew; | |
487 } | |
488 return dnew; | |
489 } | |
490 | |
491 /* | |
492 * Check if the diff block "dp" can be made smaller for lines at the start and | |
493 * end that are equal. Called after inserting lines. | |
494 * This may result in a change where all buffers have zero lines, the caller | |
495 * must take care of removing it. | |
496 */ | |
497 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
498 diff_check_unchanged(tabpage_T *tp, diff_T *dp) |
7 | 499 { |
500 int i_org; | |
501 int i_new; | |
502 int off_org, off_new; | |
503 char_u *line_org; | |
504 int dir = FORWARD; | |
505 | |
506 /* Find the first buffers, use it as the original, compare the other | |
507 * buffer lines against this one. */ | |
508 for (i_org = 0; i_org < DB_COUNT; ++i_org) | |
672 | 509 if (tp->tp_diffbuf[i_org] != NULL) |
7 | 510 break; |
511 if (i_org == DB_COUNT) /* safety check */ | |
512 return; | |
513 | |
672 | 514 if (diff_check_sanity(tp, dp) == FAIL) |
7 | 515 return; |
516 | |
517 /* First check lines at the top, then at the bottom. */ | |
518 off_org = 0; | |
519 off_new = 0; | |
520 for (;;) | |
521 { | |
522 /* Repeat until a line is found which is different or the number of | |
523 * lines has become zero. */ | |
524 while (dp->df_count[i_org] > 0) | |
525 { | |
526 /* Copy the line, the next ml_get() will invalidate it. */ | |
527 if (dir == BACKWARD) | |
528 off_org = dp->df_count[i_org] - 1; | |
672 | 529 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org], |
7 | 530 dp->df_lnum[i_org] + off_org, FALSE)); |
531 if (line_org == NULL) | |
532 return; | |
533 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new) | |
534 { | |
672 | 535 if (tp->tp_diffbuf[i_new] == NULL) |
7 | 536 continue; |
537 if (dir == BACKWARD) | |
538 off_new = dp->df_count[i_new] - 1; | |
539 /* if other buffer doesn't have this line, it was inserted */ | |
540 if (off_new < 0 || off_new >= dp->df_count[i_new]) | |
541 break; | |
672 | 542 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new], |
7 | 543 dp->df_lnum[i_new] + off_new, FALSE)) != 0) |
544 break; | |
545 } | |
546 vim_free(line_org); | |
547 | |
548 /* Stop when a line isn't equal in all diff buffers. */ | |
549 if (i_new != DB_COUNT) | |
550 break; | |
551 | |
552 /* Line matched in all buffers, remove it from the diff. */ | |
553 for (i_new = i_org; i_new < DB_COUNT; ++i_new) | |
672 | 554 if (tp->tp_diffbuf[i_new] != NULL) |
7 | 555 { |
556 if (dir == FORWARD) | |
557 ++dp->df_lnum[i_new]; | |
558 --dp->df_count[i_new]; | |
559 } | |
560 } | |
561 if (dir == BACKWARD) | |
562 break; | |
563 dir = BACKWARD; | |
564 } | |
565 } | |
566 | |
567 /* | |
568 * Check if a diff block doesn't contain invalid line numbers. | |
569 * This can happen when the diff program returns invalid results. | |
570 */ | |
571 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
572 diff_check_sanity(tabpage_T *tp, diff_T *dp) |
7 | 573 { |
574 int i; | |
575 | |
576 for (i = 0; i < DB_COUNT; ++i) | |
672 | 577 if (tp->tp_diffbuf[i] != NULL) |
7 | 578 if (dp->df_lnum[i] + dp->df_count[i] - 1 |
672 | 579 > tp->tp_diffbuf[i]->b_ml.ml_line_count) |
7 | 580 return FAIL; |
581 return OK; | |
582 } | |
583 | |
584 /* | |
672 | 585 * Mark all diff buffers in the current tab page for redraw. |
7 | 586 */ |
587 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
588 diff_redraw( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
589 int dofold) /* also recompute the folds */ |
7 | 590 { |
591 win_T *wp; | |
592 int n; | |
593 | |
594 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
595 if (wp->w_p_diff) | |
596 { | |
738 | 597 redraw_win_later(wp, SOME_VALID); |
7 | 598 #ifdef FEAT_FOLDING |
599 if (dofold && foldmethodIsDiff(wp)) | |
600 foldUpdateAll(wp); | |
601 #endif | |
602 /* A change may have made filler lines invalid, need to take care | |
603 * of that for other windows. */ | |
3904 | 604 n = diff_check(wp, wp->w_topline); |
605 if ((wp != curwin && wp->w_topfill > 0) || n > 0) | |
7 | 606 { |
607 if (wp->w_topfill > n) | |
608 wp->w_topfill = (n < 0 ? 0 : n); | |
3904 | 609 else if (n > 0 && n > wp->w_topfill) |
610 wp->w_topfill = n; | |
5934 | 611 check_topfill(wp, FALSE); |
7 | 612 } |
613 } | |
614 } | |
615 | |
616 /* | |
617 * Write buffer "buf" to file "name". | |
618 * Always use 'fileformat' set to "unix". | |
619 * Return FAIL for failure | |
620 */ | |
621 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
622 diff_write(buf_T *buf, char_u *fname) |
7 | 623 { |
624 int r; | |
625 char_u *save_ff; | |
626 | |
627 save_ff = buf->b_p_ff; | |
628 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); | |
629 r = buf_write(buf, fname, NULL, (linenr_T)1, buf->b_ml.ml_line_count, | |
630 NULL, FALSE, FALSE, FALSE, TRUE); | |
631 free_string_option(buf->b_p_ff); | |
632 buf->b_p_ff = save_ff; | |
633 return r; | |
634 } | |
635 | |
636 /* | |
637 * Completely update the diffs for the buffers involved. | |
638 * This uses the ordinary "diff" command. | |
639 * The buffers are written to a file, also for unmodified buffers (the file | |
640 * could have been produced by autocommands, e.g. the netrw plugin). | |
641 */ | |
642 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
643 ex_diffupdate( |
8368
db2a07b710ed
commit https://github.com/vim/vim/commit/f1d2501ebe33e148886c2914acd33140e20ee222
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
644 exarg_T *eap) /* can be NULL */ |
7 | 645 { |
646 buf_T *buf; | |
647 int idx_orig; | |
648 int idx_new; | |
649 char_u *tmp_orig; | |
650 char_u *tmp_new; | |
651 char_u *tmp_diff; | |
652 FILE *fd; | |
653 int ok; | |
1757 | 654 int io_error = FALSE; |
7 | 655 |
656 /* Delete all diffblocks. */ | |
672 | 657 diff_clear(curtab); |
658 curtab->tp_diff_invalid = FALSE; | |
7 | 659 |
660 /* Use the first buffer as the original text. */ | |
661 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig) | |
672 | 662 if (curtab->tp_diffbuf[idx_orig] != NULL) |
7 | 663 break; |
664 if (idx_orig == DB_COUNT) | |
665 return; | |
666 | |
667 /* Only need to do something when there is another buffer. */ | |
668 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new) | |
672 | 669 if (curtab->tp_diffbuf[idx_new] != NULL) |
7 | 670 break; |
671 if (idx_new == DB_COUNT) | |
672 return; | |
673 | |
674 /* We need three temp file names. */ | |
6721 | 675 tmp_orig = vim_tempname('o', TRUE); |
676 tmp_new = vim_tempname('n', TRUE); | |
677 tmp_diff = vim_tempname('d', TRUE); | |
7 | 678 if (tmp_orig == NULL || tmp_new == NULL || tmp_diff == NULL) |
679 goto theend; | |
680 | |
681 /* | |
682 * Do a quick test if "diff" really works. Otherwise it looks like there | |
683 * are no differences. Can't use the return value, it's non-zero when | |
684 * there are differences. | |
685 * May try twice, first with "-a" and then without. | |
686 */ | |
687 for (;;) | |
688 { | |
689 ok = FALSE; | |
531 | 690 fd = mch_fopen((char *)tmp_orig, "w"); |
1757 | 691 if (fd == NULL) |
692 io_error = TRUE; | |
693 else | |
7 | 694 { |
1757 | 695 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1) |
696 io_error = TRUE; | |
7 | 697 fclose(fd); |
531 | 698 fd = mch_fopen((char *)tmp_new, "w"); |
1757 | 699 if (fd == NULL) |
700 io_error = TRUE; | |
701 else | |
7 | 702 { |
1757 | 703 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1) |
704 io_error = TRUE; | |
7 | 705 fclose(fd); |
706 diff_file(tmp_orig, tmp_new, tmp_diff); | |
531 | 707 fd = mch_fopen((char *)tmp_diff, "r"); |
1757 | 708 if (fd == NULL) |
709 io_error = TRUE; | |
710 else | |
7 | 711 { |
712 char_u linebuf[LBUFLEN]; | |
713 | |
714 for (;;) | |
715 { | |
716 /* There must be a line that contains "1c1". */ | |
717 if (tag_fgets(linebuf, LBUFLEN, fd)) | |
718 break; | |
719 if (STRNCMP(linebuf, "1c1", 3) == 0) | |
720 ok = TRUE; | |
721 } | |
722 fclose(fd); | |
723 } | |
724 mch_remove(tmp_diff); | |
725 mch_remove(tmp_new); | |
726 } | |
727 mch_remove(tmp_orig); | |
728 } | |
729 | |
730 #ifdef FEAT_EVAL | |
731 /* When using 'diffexpr' break here. */ | |
732 if (*p_dex != NUL) | |
733 break; | |
734 #endif | |
735 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7817
diff
changeset
|
736 #if defined(MSWIN) |
7 | 737 /* If the "-a" argument works, also check if "--binary" works. */ |
738 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE) | |
739 { | |
740 diff_a_works = TRUE; | |
741 diff_bin_works = TRUE; | |
742 continue; | |
743 } | |
744 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE) | |
745 { | |
746 /* Tried --binary, but it failed. "-a" works though. */ | |
747 diff_bin_works = FALSE; | |
748 ok = TRUE; | |
749 } | |
750 #endif | |
751 | |
752 /* If we checked if "-a" works already, break here. */ | |
753 if (diff_a_works != MAYBE) | |
754 break; | |
755 diff_a_works = ok; | |
756 | |
757 /* If "-a" works break here, otherwise retry without "-a". */ | |
758 if (ok) | |
759 break; | |
760 } | |
761 if (!ok) | |
762 { | |
1757 | 763 if (io_error) |
764 EMSG(_("E810: Cannot read or write temp files")); | |
7 | 765 EMSG(_("E97: Cannot create diffs")); |
766 diff_a_works = MAYBE; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7817
diff
changeset
|
767 #if defined(MSWIN) |
7 | 768 diff_bin_works = MAYBE; |
769 #endif | |
770 goto theend; | |
771 } | |
772 | |
3524 | 773 /* :diffupdate! */ |
774 if (eap != NULL && eap->forceit) | |
775 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new) | |
776 { | |
777 buf = curtab->tp_diffbuf[idx_new]; | |
778 if (buf_valid(buf)) | |
779 buf_check_timestamp(buf, FALSE); | |
780 } | |
781 | |
7 | 782 /* Write the first buffer to a tempfile. */ |
672 | 783 buf = curtab->tp_diffbuf[idx_orig]; |
7 | 784 if (diff_write(buf, tmp_orig) == FAIL) |
785 goto theend; | |
786 | |
787 /* Make a difference between the first buffer and every other. */ | |
788 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new) | |
789 { | |
672 | 790 buf = curtab->tp_diffbuf[idx_new]; |
6985 | 791 if (buf == NULL || buf->b_ml.ml_mfp == NULL) |
792 continue; /* skip buffer that isn't loaded */ | |
7 | 793 if (diff_write(buf, tmp_new) == FAIL) |
794 continue; | |
795 diff_file(tmp_orig, tmp_new, tmp_diff); | |
796 | |
797 /* Read the diff output and add each entry to the diff list. */ | |
798 diff_read(idx_orig, idx_new, tmp_diff); | |
799 mch_remove(tmp_diff); | |
800 mch_remove(tmp_new); | |
801 } | |
802 mch_remove(tmp_orig); | |
803 | |
1429 | 804 /* force updating cursor position on screen */ |
805 curwin->w_valid_cursor.lnum = 0; | |
806 | |
7 | 807 diff_redraw(TRUE); |
808 | |
809 theend: | |
810 vim_free(tmp_orig); | |
811 vim_free(tmp_new); | |
812 vim_free(tmp_diff); | |
813 } | |
814 | |
815 /* | |
816 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff". | |
817 */ | |
818 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
819 diff_file( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
820 char_u *tmp_orig, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
821 char_u *tmp_new, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
822 char_u *tmp_diff) |
7 | 823 { |
824 char_u *cmd; | |
1872 | 825 size_t len; |
7 | 826 |
827 #ifdef FEAT_EVAL | |
828 if (*p_dex != NUL) | |
829 /* Use 'diffexpr' to generate the diff file. */ | |
830 eval_diff(tmp_orig, tmp_new, tmp_diff); | |
831 else | |
832 #endif | |
833 { | |
1872 | 834 len = STRLEN(tmp_orig) + STRLEN(tmp_new) |
835 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27; | |
836 cmd = alloc((unsigned)len); | |
7 | 837 if (cmd != NULL) |
838 { | |
193 | 839 /* We don't want $DIFF_OPTIONS to get in the way. */ |
840 if (getenv("DIFF_OPTIONS")) | |
841 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)""); | |
842 | |
7 | 843 /* Build the diff command and execute it. Always use -a, binary |
844 * differences are of no use. Ignore errors, diff returns | |
845 * non-zero when differences have been found. */ | |
1872 | 846 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s %s", |
7 | 847 diff_a_works == FALSE ? "" : "-a ", |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7817
diff
changeset
|
848 #if defined(MSWIN) |
7 | 849 diff_bin_works == TRUE ? "--binary " : "", |
850 #else | |
851 "", | |
852 #endif | |
853 (diff_flags & DIFF_IWHITE) ? "-b " : "", | |
854 (diff_flags & DIFF_ICASE) ? "-i " : "", | |
855 tmp_orig, tmp_new); | |
1872 | 856 append_redir(cmd, (int)len, p_srr, tmp_diff); |
828 | 857 #ifdef FEAT_AUTOCMD |
1410 | 858 block_autocmds(); /* Avoid ShellCmdPost stuff */ |
828 | 859 #endif |
7 | 860 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT); |
828 | 861 #ifdef FEAT_AUTOCMD |
1410 | 862 unblock_autocmds(); |
828 | 863 #endif |
7 | 864 vim_free(cmd); |
865 } | |
866 } | |
867 } | |
868 | |
869 /* | |
870 * Create a new version of a file from the current buffer and a diff file. | |
871 * The buffer is written to a file, also for unmodified buffers (the file | |
872 * could have been produced by autocommands, e.g. the netrw plugin). | |
873 */ | |
874 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
875 ex_diffpatch(exarg_T *eap) |
7 | 876 { |
877 char_u *tmp_orig; /* name of original temp file */ | |
878 char_u *tmp_new; /* name of patched temp file */ | |
879 char_u *buf = NULL; | |
1872 | 880 size_t buflen; |
7 | 881 win_T *old_curwin = curwin; |
882 char_u *newname = NULL; /* name of patched file buffer */ | |
883 #ifdef UNIX | |
884 char_u dirbuf[MAXPATHL]; | |
885 char_u *fullname = NULL; | |
886 #endif | |
887 #ifdef FEAT_BROWSE | |
888 char_u *browseFile = NULL; | |
889 int browse_flag = cmdmod.browse; | |
890 #endif | |
1942 | 891 struct stat st; |
7 | 892 |
893 #ifdef FEAT_BROWSE | |
894 if (cmdmod.browse) | |
895 { | |
28 | 896 browseFile = do_browse(0, (char_u *)_("Patch file"), |
7 | 897 eap->arg, NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL); |
898 if (browseFile == NULL) | |
899 return; /* operation cancelled */ | |
900 eap->arg = browseFile; | |
901 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */ | |
902 } | |
903 #endif | |
904 | |
905 /* We need two temp file names. */ | |
6721 | 906 tmp_orig = vim_tempname('o', FALSE); |
907 tmp_new = vim_tempname('n', FALSE); | |
7 | 908 if (tmp_orig == NULL || tmp_new == NULL) |
909 goto theend; | |
910 | |
911 /* Write the current buffer to "tmp_orig". */ | |
912 if (buf_write(curbuf, tmp_orig, NULL, | |
913 (linenr_T)1, curbuf->b_ml.ml_line_count, | |
914 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL) | |
915 goto theend; | |
916 | |
917 #ifdef UNIX | |
918 /* Get the absolute path of the patchfile, changing directory below. */ | |
919 fullname = FullName_save(eap->arg, FALSE); | |
920 #endif | |
1872 | 921 buflen = STRLEN(tmp_orig) + ( |
7 | 922 # ifdef UNIX |
923 fullname != NULL ? STRLEN(fullname) : | |
924 # endif | |
1872 | 925 STRLEN(eap->arg)) + STRLEN(tmp_new) + 16; |
926 buf = alloc((unsigned)buflen); | |
7 | 927 if (buf == NULL) |
928 goto theend; | |
929 | |
930 #ifdef UNIX | |
1788 | 931 /* Temporarily chdir to /tmp, to avoid patching files in the current |
7 | 932 * directory when the patch file contains more than one patch. When we |
933 * have our own temp dir use that instead, it will be cleaned up when we | |
934 * exit (any .rej files created). Don't change directory if we can't | |
935 * return to the current. */ | |
936 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0) | |
937 dirbuf[0] = NUL; | |
938 else | |
939 { | |
940 # ifdef TEMPDIRNAMES | |
941 if (vim_tempdir != NULL) | |
1757 | 942 ignored = mch_chdir((char *)vim_tempdir); |
7 | 943 else |
944 # endif | |
1757 | 945 ignored = mch_chdir("/tmp"); |
7 | 946 shorten_fnames(TRUE); |
947 } | |
948 #endif | |
949 | |
950 #ifdef FEAT_EVAL | |
951 if (*p_pex != NUL) | |
952 /* Use 'patchexpr' to generate the new file. */ | |
953 eval_patch(tmp_orig, | |
954 # ifdef UNIX | |
955 fullname != NULL ? fullname : | |
956 # endif | |
957 eap->arg, tmp_new); | |
958 else | |
959 #endif | |
960 { | |
961 /* Build the patch command and execute it. Ignore errors. Switch to | |
962 * cooked mode to allow the user to respond to prompts. */ | |
1872 | 963 vim_snprintf((char *)buf, buflen, "patch -o %s %s < \"%s\"", |
964 tmp_new, tmp_orig, | |
7 | 965 # ifdef UNIX |
966 fullname != NULL ? fullname : | |
967 # endif | |
968 eap->arg); | |
828 | 969 #ifdef FEAT_AUTOCMD |
1410 | 970 block_autocmds(); /* Avoid ShellCmdPost stuff */ |
828 | 971 #endif |
7 | 972 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED); |
828 | 973 #ifdef FEAT_AUTOCMD |
1410 | 974 unblock_autocmds(); |
828 | 975 #endif |
7 | 976 } |
977 | |
978 #ifdef UNIX | |
979 if (dirbuf[0] != NUL) | |
980 { | |
981 if (mch_chdir((char *)dirbuf) != 0) | |
982 EMSG(_(e_prev_dir)); | |
983 shorten_fnames(TRUE); | |
984 } | |
985 #endif | |
986 | |
987 /* patch probably has written over the screen */ | |
988 redraw_later(CLEAR); | |
989 | |
990 /* Delete any .orig or .rej file created. */ | |
991 STRCPY(buf, tmp_new); | |
992 STRCAT(buf, ".orig"); | |
993 mch_remove(buf); | |
994 STRCPY(buf, tmp_new); | |
995 STRCAT(buf, ".rej"); | |
996 mch_remove(buf); | |
997 | |
1942 | 998 /* Only continue if the output file was created. */ |
999 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0) | |
1000 EMSG(_("E816: Cannot read patch output")); | |
1001 else | |
7 | 1002 { |
1942 | 1003 if (curbuf->b_fname != NULL) |
1004 { | |
1005 newname = vim_strnsave(curbuf->b_fname, | |
7 | 1006 (int)(STRLEN(curbuf->b_fname) + 4)); |
1942 | 1007 if (newname != NULL) |
1008 STRCAT(newname, ".new"); | |
1009 } | |
7 | 1010 |
1011 #ifdef FEAT_GUI | |
1942 | 1012 need_mouse_correct = TRUE; |
7 | 1013 #endif |
1942 | 1014 /* don't use a new tab page, each tab page has its own diffs */ |
1015 cmdmod.tab = 0; | |
682 | 1016 |
1942 | 1017 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL) |
1018 { | |
1019 /* Pretend it was a ":split fname" command */ | |
1020 eap->cmdidx = CMD_split; | |
1021 eap->arg = tmp_new; | |
1022 do_exedit(eap, old_curwin); | |
7 | 1023 |
1942 | 1024 /* check that split worked and editing tmp_new */ |
1025 if (curwin != old_curwin && win_valid(old_curwin)) | |
1026 { | |
1027 /* Set 'diff', 'scrollbind' on and 'wrap' off. */ | |
1028 diff_win_options(curwin, TRUE); | |
1029 diff_win_options(old_curwin, TRUE); | |
7 | 1030 |
1942 | 1031 if (newname != NULL) |
1032 { | |
1033 /* do a ":file filename.new" on the patched buffer */ | |
1034 eap->arg = newname; | |
1035 ex_file(eap); | |
7 | 1036 |
1037 #ifdef FEAT_AUTOCMD | |
1942 | 1038 /* Do filetype detection with the new name. */ |
1039 if (au_has_group((char_u *)"filetypedetect")) | |
1040 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead"); | |
7 | 1041 #endif |
1942 | 1042 } |
7 | 1043 } |
1044 } | |
1045 } | |
1046 | |
1047 theend: | |
1048 if (tmp_orig != NULL) | |
1049 mch_remove(tmp_orig); | |
1050 vim_free(tmp_orig); | |
1051 if (tmp_new != NULL) | |
1052 mch_remove(tmp_new); | |
1053 vim_free(tmp_new); | |
1054 vim_free(newname); | |
1055 vim_free(buf); | |
1056 #ifdef UNIX | |
1057 vim_free(fullname); | |
1058 #endif | |
1059 #ifdef FEAT_BROWSE | |
1060 vim_free(browseFile); | |
1061 cmdmod.browse = browse_flag; | |
1062 #endif | |
1063 } | |
1064 | |
1065 /* | |
1066 * Split the window and edit another file, setting options to show the diffs. | |
1067 */ | |
1068 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1069 ex_diffsplit(exarg_T *eap) |
7 | 1070 { |
1071 win_T *old_curwin = curwin; | |
7338
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1072 buf_T *old_curbuf = curbuf; |
7 | 1073 |
1074 #ifdef FEAT_GUI | |
1075 need_mouse_correct = TRUE; | |
1076 #endif | |
682 | 1077 /* don't use a new tab page, each tab page has its own diffs */ |
1078 cmdmod.tab = 0; | |
1079 | |
764 | 1080 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL) |
7 | 1081 { |
1082 /* Pretend it was a ":split fname" command */ | |
1083 eap->cmdidx = CMD_split; | |
449 | 1084 curwin->w_p_diff = TRUE; |
7 | 1085 do_exedit(eap, old_curwin); |
1086 | |
1087 if (curwin != old_curwin) /* split must have worked */ | |
1088 { | |
1089 /* Set 'diff', 'scrollbind' on and 'wrap' off. */ | |
1090 diff_win_options(curwin, TRUE); | |
7338
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1091 if (win_valid(old_curwin)) |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1092 { |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1093 diff_win_options(old_curwin, TRUE); |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1094 |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1095 if (buf_valid(old_curbuf)) |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1096 /* Move the cursor position to that of the old window. */ |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1097 curwin->w_cursor.lnum = diff_get_corresponding_line( |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1098 old_curbuf, |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1099 old_curwin->w_cursor.lnum, |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1100 curbuf, |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1101 curwin->w_cursor.lnum); |
96d5dd9e7bc9
commit https://github.com/vim/vim/commit/f29a82dcd0914c76f595d475ddac4517371fab2b
Christian Brabandt <cb@256bit.org>
parents:
6985
diff
changeset
|
1102 } |
7 | 1103 } |
1104 } | |
1105 } | |
1106 | |
1107 /* | |
4352 | 1108 * Set options to show diffs for the current window. |
7 | 1109 */ |
1110 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1111 ex_diffthis(exarg_T *eap UNUSED) |
7 | 1112 { |
1113 /* Set 'diff', 'scrollbind' on and 'wrap' off. */ | |
1114 diff_win_options(curwin, TRUE); | |
1115 } | |
1116 | |
1117 /* | |
1118 * Set options in window "wp" for diff mode. | |
1119 */ | |
1120 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1121 diff_win_options( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1122 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1123 int addbuf) /* Add buffer to diff. */ |
7 | 1124 { |
2086
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1125 # ifdef FEAT_FOLDING |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1126 win_T *old_curwin = curwin; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1127 |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1128 /* close the manually opened folds */ |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1129 curwin = wp; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1130 newFoldLevel(); |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1131 curwin = old_curwin; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1132 # endif |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1133 |
2583 | 1134 /* Use 'scrollbind' and 'cursorbind' when available */ |
1135 #ifdef FEAT_SCROLLBIND | |
6897 | 1136 if (!wp->w_p_diff) |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1137 wp->w_p_scb_save = wp->w_p_scb; |
2583 | 1138 wp->w_p_scb = TRUE; |
1139 #endif | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
1140 #ifdef FEAT_CURSORBIND |
6897 | 1141 if (!wp->w_p_diff) |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1142 wp->w_p_crb_save = wp->w_p_crb; |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
1143 wp->w_p_crb = TRUE; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
1144 #endif |
6897 | 1145 if (!wp->w_p_diff) |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1146 wp->w_p_wrap_save = wp->w_p_wrap; |
7 | 1147 wp->w_p_wrap = FALSE; |
1148 # ifdef FEAT_FOLDING | |
2086
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1149 curwin = wp; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1150 curbuf = curwin->w_buffer; |
6897 | 1151 if (!wp->w_p_diff) |
1152 { | |
1153 if (wp->w_p_diff_saved) | |
1154 free_string_option(wp->w_p_fdm_save); | |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1155 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm); |
6897 | 1156 } |
2086
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1157 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff", |
694 | 1158 OPT_LOCAL|OPT_FREE, 0); |
2086
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1159 curwin = old_curwin; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1160 curbuf = curwin->w_buffer; |
6897 | 1161 if (!wp->w_p_diff) |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1162 { |
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1163 wp->w_p_fdc_save = wp->w_p_fdc; |
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1164 wp->w_p_fen_save = wp->w_p_fen; |
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1165 wp->w_p_fdl_save = wp->w_p_fdl; |
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1166 } |
2086
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1167 wp->w_p_fdc = diff_foldcolumn; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1168 wp->w_p_fen = TRUE; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1169 wp->w_p_fdl = 0; |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1170 foldUpdateAll(wp); |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1171 /* make sure topline is not halfway a fold */ |
c11845a465ae
updated for version 7.2.370
Bram Moolenaar <bram@zimbu.org>
parents:
1942
diff
changeset
|
1172 changed_window_setting_win(wp); |
7 | 1173 # endif |
1174 #ifdef FEAT_SCROLLBIND | |
1175 if (vim_strchr(p_sbo, 'h') == NULL) | |
1176 do_cmdline_cmd((char_u *)"set sbo+=hor"); | |
1177 #endif | |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1178 /* Saved the current values, to be restored in ex_diffoff(). */ |
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1179 wp->w_p_diff_saved = TRUE; |
7 | 1180 |
6897 | 1181 wp->w_p_diff = TRUE; |
1182 | |
7 | 1183 if (addbuf) |
1184 diff_buf_add(wp->w_buffer); | |
1185 redraw_win_later(wp, NOT_VALID); | |
1186 } | |
1187 | |
1188 /* | |
16 | 1189 * Set options not to show diffs. For the current window or all windows. |
671 | 1190 * Only in the current tab page. |
16 | 1191 */ |
1192 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1193 ex_diffoff(exarg_T *eap) |
16 | 1194 { |
1195 win_T *wp; | |
1196 #ifdef FEAT_SCROLLBIND | |
1197 int diffwin = FALSE; | |
1198 #endif | |
1199 | |
1200 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
1201 { | |
5358 | 1202 if (eap->forceit ? wp->w_p_diff : wp == curwin) |
16 | 1203 { |
6897 | 1204 /* Set 'diff' off. If option values were saved in |
1205 * diff_win_options(), restore the ones whose settings seem to have | |
1206 * been left over from diff mode. */ | |
16 | 1207 wp->w_p_diff = FALSE; |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1208 |
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1209 if (wp->w_p_diff_saved) |
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1210 { |
6897 | 1211 |
1212 #ifdef FEAT_SCROLLBIND | |
1213 if (wp->w_p_scb) | |
1214 wp->w_p_scb = wp->w_p_scb_save; | |
1215 #endif | |
1216 #ifdef FEAT_CURSORBIND | |
1217 if (wp->w_p_crb) | |
1218 wp->w_p_crb = wp->w_p_crb_save; | |
1219 #endif | |
1220 if (!wp->w_p_wrap) | |
1221 wp->w_p_wrap = wp->w_p_wrap_save; | |
1222 #ifdef FEAT_FOLDING | |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1223 free_string_option(wp->w_p_fdm); |
6897 | 1224 wp->w_p_fdm = vim_strsave(wp->w_p_fdm_save); |
5200
b3ff17862b4c
updated for version 7.4a.026
Bram Moolenaar <bram@vim.org>
parents:
5102
diff
changeset
|
1225 |
6897 | 1226 if (wp->w_p_fdc == diff_foldcolumn) |
1227 wp->w_p_fdc = wp->w_p_fdc_save; | |
1228 if (wp->w_p_fdl == 0) | |
1229 wp->w_p_fdl = wp->w_p_fdl_save; | |
1230 | |
5200
b3ff17862b4c
updated for version 7.4a.026
Bram Moolenaar <bram@vim.org>
parents:
5102
diff
changeset
|
1231 /* Only restore 'foldenable' when 'foldmethod' is not |
b3ff17862b4c
updated for version 7.4a.026
Bram Moolenaar <bram@vim.org>
parents:
5102
diff
changeset
|
1232 * "manual", otherwise we continue to show the diff folds. */ |
6897 | 1233 if (wp->w_p_fen) |
1234 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE | |
1235 : wp->w_p_fen_save; | |
1236 | |
1237 foldUpdateAll(wp); | |
1238 /* make sure topline is not halfway a fold */ | |
1239 changed_window_setting_win(wp); | |
1240 #endif | |
5200
b3ff17862b4c
updated for version 7.4a.026
Bram Moolenaar <bram@vim.org>
parents:
5102
diff
changeset
|
1241 } |
b3ff17862b4c
updated for version 7.4a.026
Bram Moolenaar <bram@vim.org>
parents:
5102
diff
changeset
|
1242 |
5102
11d0c6df1d7b
updated for version 7.3.1294
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1243 /* Note: 'sbo' is not restored, it's a global option. */ |
16 | 1244 diff_buf_adjust(wp); |
1245 } | |
1246 #ifdef FEAT_SCROLLBIND | |
1247 diffwin |= wp->w_p_diff; | |
1248 #endif | |
1249 } | |
1250 | |
1251 #ifdef FEAT_SCROLLBIND | |
1252 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */ | |
1253 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL) | |
1254 do_cmdline_cmd((char_u *)"set sbo-=hor"); | |
1255 #endif | |
1256 } | |
1257 | |
1258 /* | |
7 | 1259 * Read the diff output and add each entry to the diff list. |
1260 */ | |
1261 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1262 diff_read( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1263 int idx_orig, /* idx of original file */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1264 int idx_new, /* idx of new file */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1265 char_u *fname) /* name of diff output file */ |
7 | 1266 { |
1267 FILE *fd; | |
1268 diff_T *dprev = NULL; | |
672 | 1269 diff_T *dp = curtab->tp_first_diff; |
7 | 1270 diff_T *dn, *dpl; |
1271 long f1, l1, f2, l2; | |
1272 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */ | |
1273 int difftype; | |
1274 char_u *p; | |
1275 long off; | |
1276 int i; | |
1277 linenr_T lnum_orig, lnum_new; | |
1278 long count_orig, count_new; | |
1279 int notset = TRUE; /* block "*dp" not set yet */ | |
1280 | |
531 | 1281 fd = mch_fopen((char *)fname, "r"); |
7 | 1282 if (fd == NULL) |
1283 { | |
1284 EMSG(_("E98: Cannot read diff output")); | |
1285 return; | |
1286 } | |
1287 | |
1288 for (;;) | |
1289 { | |
1290 if (tag_fgets(linebuf, LBUFLEN, fd)) | |
1291 break; /* end of file */ | |
1292 if (!isdigit(*linebuf)) | |
1293 continue; /* not the start of a diff block */ | |
1294 | |
1295 /* This line must be one of three formats: | |
1296 * {first}[,{last}]c{first}[,{last}] | |
1297 * {first}a{first}[,{last}] | |
1298 * {first}[,{last}]d{first} | |
1299 */ | |
1300 p = linebuf; | |
1301 f1 = getdigits(&p); | |
1302 if (*p == ',') | |
1303 { | |
1304 ++p; | |
1305 l1 = getdigits(&p); | |
1306 } | |
1307 else | |
1308 l1 = f1; | |
1309 if (*p != 'a' && *p != 'c' && *p != 'd') | |
1310 continue; /* invalid diff format */ | |
1311 difftype = *p++; | |
1312 f2 = getdigits(&p); | |
1313 if (*p == ',') | |
1314 { | |
1315 ++p; | |
1316 l2 = getdigits(&p); | |
1317 } | |
1318 else | |
1319 l2 = f2; | |
1320 if (l1 < f1 || l2 < f2) | |
1321 continue; /* invalid line range */ | |
1322 | |
1323 if (difftype == 'a') | |
1324 { | |
1325 lnum_orig = f1 + 1; | |
1326 count_orig = 0; | |
1327 } | |
1328 else | |
1329 { | |
1330 lnum_orig = f1; | |
1331 count_orig = l1 - f1 + 1; | |
1332 } | |
1333 if (difftype == 'd') | |
1334 { | |
1335 lnum_new = f2 + 1; | |
1336 count_new = 0; | |
1337 } | |
1338 else | |
1339 { | |
1340 lnum_new = f2; | |
1341 count_new = l2 - f2 + 1; | |
1342 } | |
1343 | |
1344 /* Go over blocks before the change, for which orig and new are equal. | |
1345 * Copy blocks from orig to new. */ | |
1346 while (dp != NULL | |
1347 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig]) | |
1348 { | |
1349 if (notset) | |
1350 diff_copy_entry(dprev, dp, idx_orig, idx_new); | |
1351 dprev = dp; | |
1352 dp = dp->df_next; | |
1353 notset = TRUE; | |
1354 } | |
1355 | |
1356 if (dp != NULL | |
1357 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig] | |
1358 && lnum_orig + count_orig >= dp->df_lnum[idx_orig]) | |
1359 { | |
1360 /* New block overlaps with existing block(s). | |
1361 * First find last block that overlaps. */ | |
1362 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next) | |
1363 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig]) | |
1364 break; | |
1365 | |
1366 /* If the newly found block starts before the old one, set the | |
1367 * start back a number of lines. */ | |
1368 off = dp->df_lnum[idx_orig] - lnum_orig; | |
1369 if (off > 0) | |
1370 { | |
1371 for (i = idx_orig; i < idx_new; ++i) | |
672 | 1372 if (curtab->tp_diffbuf[i] != NULL) |
7 | 1373 dp->df_lnum[i] -= off; |
1374 dp->df_lnum[idx_new] = lnum_new; | |
1375 dp->df_count[idx_new] = count_new; | |
1376 } | |
1377 else if (notset) | |
1378 { | |
1379 /* new block inside existing one, adjust new block */ | |
1380 dp->df_lnum[idx_new] = lnum_new + off; | |
1381 dp->df_count[idx_new] = count_new - off; | |
1382 } | |
1383 else | |
1384 /* second overlap of new block with existing block */ | |
1519 | 1385 dp->df_count[idx_new] += count_new - count_orig |
1386 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig] | |
1387 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]); | |
7 | 1388 |
1389 /* Adjust the size of the block to include all the lines to the | |
1390 * end of the existing block or the new diff, whatever ends last. */ | |
1391 off = (lnum_orig + count_orig) | |
1392 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]); | |
1393 if (off < 0) | |
1394 { | |
1395 /* new change ends in existing block, adjust the end if not | |
1396 * done already */ | |
1397 if (notset) | |
1398 dp->df_count[idx_new] += -off; | |
1399 off = 0; | |
1400 } | |
1428 | 1401 for (i = idx_orig; i < idx_new; ++i) |
672 | 1402 if (curtab->tp_diffbuf[i] != NULL) |
7 | 1403 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i] |
1404 - dp->df_lnum[i] + off; | |
1405 | |
1406 /* Delete the diff blocks that have been merged into one. */ | |
1407 dn = dp->df_next; | |
1408 dp->df_next = dpl->df_next; | |
1409 while (dn != dp->df_next) | |
1410 { | |
1411 dpl = dn->df_next; | |
1412 vim_free(dn); | |
1413 dn = dpl; | |
1414 } | |
1415 } | |
1416 else | |
1417 { | |
1418 /* Allocate a new diffblock. */ | |
672 | 1419 dp = diff_alloc_new(curtab, dprev, dp); |
7 | 1420 if (dp == NULL) |
840 | 1421 goto done; |
7 | 1422 |
1423 dp->df_lnum[idx_orig] = lnum_orig; | |
1424 dp->df_count[idx_orig] = count_orig; | |
1425 dp->df_lnum[idx_new] = lnum_new; | |
1426 dp->df_count[idx_new] = count_new; | |
1427 | |
1428 /* Set values for other buffers, these must be equal to the | |
1429 * original buffer, otherwise there would have been a change | |
1430 * already. */ | |
1431 for (i = idx_orig + 1; i < idx_new; ++i) | |
672 | 1432 if (curtab->tp_diffbuf[i] != NULL) |
7 | 1433 diff_copy_entry(dprev, dp, idx_orig, i); |
1434 } | |
1435 notset = FALSE; /* "*dp" has been set */ | |
1436 } | |
1437 | |
1438 /* for remaining diff blocks orig and new are equal */ | |
1439 while (dp != NULL) | |
1440 { | |
1441 if (notset) | |
1442 diff_copy_entry(dprev, dp, idx_orig, idx_new); | |
1443 dprev = dp; | |
1444 dp = dp->df_next; | |
1445 notset = TRUE; | |
1446 } | |
1447 | |
840 | 1448 done: |
7 | 1449 fclose(fd); |
1450 } | |
1451 | |
1452 /* | |
1453 * Copy an entry at "dp" from "idx_orig" to "idx_new". | |
1454 */ | |
1455 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1456 diff_copy_entry( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1457 diff_T *dprev, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1458 diff_T *dp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1459 int idx_orig, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1460 int idx_new) |
7 | 1461 { |
1462 long off; | |
1463 | |
1464 if (dprev == NULL) | |
1465 off = 0; | |
1466 else | |
1467 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig]) | |
1468 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]); | |
1469 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off; | |
1470 dp->df_count[idx_new] = dp->df_count[idx_orig]; | |
1471 } | |
1472 | |
1473 /* | |
672 | 1474 * Clear the list of diffblocks for tab page "tp". |
7 | 1475 */ |
358 | 1476 void |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1477 diff_clear(tabpage_T *tp) |
7 | 1478 { |
1479 diff_T *p, *next_p; | |
1480 | |
672 | 1481 for (p = tp->tp_first_diff; p != NULL; p = next_p) |
7 | 1482 { |
1483 next_p = p->df_next; | |
1484 vim_free(p); | |
1485 } | |
672 | 1486 tp->tp_first_diff = NULL; |
7 | 1487 } |
1488 | |
1489 /* | |
1490 * Check diff status for line "lnum" in buffer "buf": | |
1491 * Returns 0 for nothing special | |
1492 * Returns -1 for a line that should be highlighted as changed. | |
1493 * Returns -2 for a line that should be highlighted as added/deleted. | |
1494 * Returns > 0 for inserting that many filler lines above it (never happens | |
1495 * when 'diffopt' doesn't contain "filler"). | |
1496 * This should only be used for windows where 'diff' is set. | |
1497 */ | |
1498 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1499 diff_check(win_T *wp, linenr_T lnum) |
7 | 1500 { |
672 | 1501 int idx; /* index in tp_diffbuf[] for this buffer */ |
7 | 1502 diff_T *dp; |
1503 int maxcount; | |
1504 int i; | |
1505 buf_T *buf = wp->w_buffer; | |
1506 int cmp; | |
1507 | |
672 | 1508 if (curtab->tp_diff_invalid) |
7 | 1509 ex_diffupdate(NULL); /* update after a big change */ |
1510 | |
672 | 1511 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */ |
7 | 1512 return 0; |
1513 | |
1514 /* safety check: "lnum" must be a buffer line */ | |
1515 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1) | |
1516 return 0; | |
1517 | |
1518 idx = diff_buf_idx(buf); | |
1519 if (idx == DB_COUNT) | |
1520 return 0; /* no diffs for buffer "buf" */ | |
1521 | |
1522 #ifdef FEAT_FOLDING | |
1523 /* A closed fold never has filler lines. */ | |
1524 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL)) | |
1525 return 0; | |
1526 #endif | |
1527 | |
1528 /* search for a change that includes "lnum" in the list of diffblocks. */ | |
672 | 1529 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) |
7 | 1530 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx]) |
1531 break; | |
1532 if (dp == NULL || lnum < dp->df_lnum[idx]) | |
1533 return 0; | |
1534 | |
1535 if (lnum < dp->df_lnum[idx] + dp->df_count[idx]) | |
1536 { | |
1537 int zero = FALSE; | |
1538 | |
1539 /* Changed or inserted line. If the other buffers have a count of | |
1540 * zero, the lines were inserted. If the other buffers have the same | |
1541 * count, check if the lines are identical. */ | |
1542 cmp = FALSE; | |
1543 for (i = 0; i < DB_COUNT; ++i) | |
672 | 1544 if (i != idx && curtab->tp_diffbuf[i] != NULL) |
7 | 1545 { |
1546 if (dp->df_count[i] == 0) | |
1547 zero = TRUE; | |
1548 else | |
1549 { | |
1550 if (dp->df_count[i] != dp->df_count[idx]) | |
1551 return -1; /* nr of lines changed. */ | |
1552 cmp = TRUE; | |
1553 } | |
1554 } | |
1555 if (cmp) | |
1556 { | |
1557 /* Compare all lines. If they are equal the lines were inserted | |
1558 * in some buffers, deleted in others, but not changed. */ | |
1559 for (i = 0; i < DB_COUNT; ++i) | |
672 | 1560 if (i != idx && curtab->tp_diffbuf[i] != NULL && dp->df_count[i] != 0) |
7 | 1561 if (!diff_equal_entry(dp, idx, i)) |
1562 return -1; | |
1563 } | |
1564 /* If there is no buffer with zero lines then there is no difference | |
1565 * any longer. Happens when making a change (or undo) that removes | |
1566 * the difference. Can't remove the entry here, we might be halfway | |
1567 * updating the window. Just report the text as unchanged. Other | |
1568 * windows might still show the change though. */ | |
1569 if (zero == FALSE) | |
1570 return 0; | |
1571 return -2; | |
1572 } | |
1573 | |
1574 /* If 'diffopt' doesn't contain "filler", return 0. */ | |
1575 if (!(diff_flags & DIFF_FILLER)) | |
1576 return 0; | |
1577 | |
1578 /* Insert filler lines above the line just below the change. Will return | |
1579 * 0 when this buf had the max count. */ | |
1580 maxcount = 0; | |
1581 for (i = 0; i < DB_COUNT; ++i) | |
672 | 1582 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount) |
7 | 1583 maxcount = dp->df_count[i]; |
1584 return maxcount - dp->df_count[idx]; | |
1585 } | |
1586 | |
1587 /* | |
1588 * Compare two entries in diff "*dp" and return TRUE if they are equal. | |
1589 */ | |
1590 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1591 diff_equal_entry(diff_T *dp, int idx1, int idx2) |
7 | 1592 { |
1593 int i; | |
1594 char_u *line; | |
1595 int cmp; | |
1596 | |
1597 if (dp->df_count[idx1] != dp->df_count[idx2]) | |
1598 return FALSE; | |
672 | 1599 if (diff_check_sanity(curtab, dp) == FAIL) |
7 | 1600 return FALSE; |
1601 for (i = 0; i < dp->df_count[idx1]; ++i) | |
1602 { | |
672 | 1603 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1], |
7 | 1604 dp->df_lnum[idx1] + i, FALSE)); |
1605 if (line == NULL) | |
1606 return FALSE; | |
672 | 1607 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2], |
7 | 1608 dp->df_lnum[idx2] + i, FALSE)); |
1609 vim_free(line); | |
1610 if (cmp != 0) | |
1611 return FALSE; | |
1612 } | |
1613 return TRUE; | |
1614 } | |
1615 | |
1616 /* | |
1617 * Compare strings "s1" and "s2" according to 'diffopt'. | |
1618 * Return non-zero when they are different. | |
1619 */ | |
1620 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1621 diff_cmp(char_u *s1, char_u *s2) |
7 | 1622 { |
1623 char_u *p1, *p2; | |
1624 #ifdef FEAT_MBYTE | |
1625 int l; | |
1626 #endif | |
1627 | |
1628 if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0) | |
1629 return STRCMP(s1, s2); | |
1630 if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE)) | |
1631 return MB_STRICMP(s1, s2); | |
1632 | |
1633 /* Ignore white space changes and possibly ignore case. */ | |
1634 p1 = s1; | |
1635 p2 = s2; | |
1636 while (*p1 != NUL && *p2 != NUL) | |
1637 { | |
1638 if (vim_iswhite(*p1) && vim_iswhite(*p2)) | |
1639 { | |
1640 p1 = skipwhite(p1); | |
1641 p2 = skipwhite(p2); | |
1642 } | |
1643 else | |
1644 { | |
1645 #ifdef FEAT_MBYTE | |
474 | 1646 l = (*mb_ptr2len)(p1); |
1647 if (l != (*mb_ptr2len)(p2)) | |
7 | 1648 break; |
1649 if (l > 1) | |
1650 { | |
1651 if (STRNCMP(p1, p2, l) != 0 | |
1652 && (!enc_utf8 | |
1653 || !(diff_flags & DIFF_ICASE) | |
1654 || utf_fold(utf_ptr2char(p1)) | |
1655 != utf_fold(utf_ptr2char(p2)))) | |
1656 break; | |
1657 p1 += l; | |
1658 p2 += l; | |
1659 } | |
1660 else | |
1661 #endif | |
1662 { | |
1663 if (*p1 != *p2 && (!(diff_flags & DIFF_ICASE) | |
1664 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2))) | |
1665 break; | |
1666 ++p1; | |
1667 ++p2; | |
1668 } | |
1669 } | |
1670 } | |
1671 | |
1672 /* Ignore trailing white space. */ | |
1673 p1 = skipwhite(p1); | |
1674 p2 = skipwhite(p2); | |
1675 if (*p1 != NUL || *p2 != NUL) | |
1676 return 1; | |
1677 return 0; | |
1678 } | |
1679 | |
1680 /* | |
1681 * Return the number of filler lines above "lnum". | |
1682 */ | |
1683 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1684 diff_check_fill(win_T *wp, linenr_T lnum) |
7 | 1685 { |
1686 int n; | |
1687 | |
1688 /* be quick when there are no filler lines */ | |
1689 if (!(diff_flags & DIFF_FILLER)) | |
1690 return 0; | |
1691 n = diff_check(wp, lnum); | |
1692 if (n <= 0) | |
1693 return 0; | |
1694 return n; | |
1695 } | |
1696 | |
1697 /* | |
1698 * Set the topline of "towin" to match the position in "fromwin", so that they | |
1699 * show the same diff'ed lines. | |
1700 */ | |
1701 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1702 diff_set_topline(win_T *fromwin, win_T *towin) |
7 | 1703 { |
1519 | 1704 buf_T *frombuf = fromwin->w_buffer; |
7 | 1705 linenr_T lnum = fromwin->w_topline; |
1519 | 1706 int fromidx; |
1707 int toidx; | |
7 | 1708 diff_T *dp; |
1519 | 1709 int max_count; |
7 | 1710 int i; |
1711 | |
1519 | 1712 fromidx = diff_buf_idx(frombuf); |
1713 if (fromidx == DB_COUNT) | |
7 | 1714 return; /* safety check */ |
1715 | |
672 | 1716 if (curtab->tp_diff_invalid) |
7 | 1717 ex_diffupdate(NULL); /* update after a big change */ |
1718 | |
1719 towin->w_topfill = 0; | |
1720 | |
1721 /* search for a change that includes "lnum" in the list of diffblocks. */ | |
672 | 1722 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) |
1519 | 1723 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx]) |
7 | 1724 break; |
1725 if (dp == NULL) | |
1726 { | |
1727 /* After last change, compute topline relative to end of file; no | |
1728 * filler lines. */ | |
1729 towin->w_topline = towin->w_buffer->b_ml.ml_line_count | |
1519 | 1730 - (frombuf->b_ml.ml_line_count - lnum); |
7 | 1731 } |
1732 else | |
1733 { | |
1734 /* Find index for "towin". */ | |
1519 | 1735 toidx = diff_buf_idx(towin->w_buffer); |
1736 if (toidx == DB_COUNT) | |
7 | 1737 return; /* safety check */ |
1738 | |
1519 | 1739 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]); |
1740 if (lnum >= dp->df_lnum[fromidx]) | |
7 | 1741 { |
1519 | 1742 /* Inside a change: compute filler lines. With three or more |
1743 * buffers we need to know the largest count. */ | |
1744 max_count = 0; | |
1745 for (i = 0; i < DB_COUNT; ++i) | |
1746 if (curtab->tp_diffbuf[i] != NULL | |
1747 && max_count < dp->df_count[i]) | |
1748 max_count = dp->df_count[i]; | |
1749 | |
1750 if (dp->df_count[toidx] == dp->df_count[fromidx]) | |
1751 { | |
1752 /* same number of lines: use same filler count */ | |
7 | 1753 towin->w_topfill = fromwin->w_topfill; |
1519 | 1754 } |
1755 else if (dp->df_count[toidx] > dp->df_count[fromidx]) | |
7 | 1756 { |
1519 | 1757 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx]) |
1758 { | |
1759 /* more lines in towin and fromwin doesn't show diff | |
1760 * lines, only filler lines */ | |
1761 if (max_count - fromwin->w_topfill >= dp->df_count[toidx]) | |
1762 { | |
1763 /* towin also only shows filler lines */ | |
1764 towin->w_topline = dp->df_lnum[toidx] | |
1765 + dp->df_count[toidx]; | |
1766 towin->w_topfill = fromwin->w_topfill; | |
1767 } | |
1768 else | |
1769 /* towin still has some diff lines to show */ | |
1770 towin->w_topline = dp->df_lnum[toidx] | |
1771 + max_count - fromwin->w_topfill; | |
1772 } | |
7 | 1773 } |
1519 | 1774 else if (towin->w_topline >= dp->df_lnum[toidx] |
1775 + dp->df_count[toidx]) | |
7 | 1776 { |
1519 | 1777 /* less lines in towin and no diff lines to show: compute |
1778 * filler lines */ | |
1779 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx]; | |
1780 if (diff_flags & DIFF_FILLER) | |
7 | 1781 { |
1519 | 1782 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx]) |
1783 /* fromwin is also out of diff lines */ | |
1784 towin->w_topfill = fromwin->w_topfill; | |
1785 else | |
1786 /* fromwin has some diff lines */ | |
1787 towin->w_topfill = dp->df_lnum[fromidx] | |
1788 + max_count - lnum; | |
7 | 1789 } |
1790 } | |
1791 } | |
1792 } | |
1793 | |
1794 /* safety check (if diff info gets outdated strange things may happen) */ | |
1795 towin->w_botfill = FALSE; | |
1796 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count) | |
1797 { | |
1798 towin->w_topline = towin->w_buffer->b_ml.ml_line_count; | |
1799 towin->w_botfill = TRUE; | |
1800 } | |
1801 if (towin->w_topline < 1) | |
1802 { | |
1803 towin->w_topline = 1; | |
1804 towin->w_topfill = 0; | |
1805 } | |
1806 | |
1807 /* When w_topline changes need to recompute w_botline and cursor position */ | |
1808 invalidate_botline_win(towin); | |
1809 changed_line_abv_curs_win(towin); | |
1810 | |
1811 check_topfill(towin, FALSE); | |
1812 #ifdef FEAT_FOLDING | |
1813 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline, | |
1814 NULL, TRUE, NULL); | |
1815 #endif | |
1816 } | |
1817 | |
1818 /* | |
1819 * This is called when 'diffopt' is changed. | |
1820 */ | |
1821 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1822 diffopt_changed(void) |
7 | 1823 { |
1824 char_u *p; | |
1825 int diff_context_new = 6; | |
1826 int diff_flags_new = 0; | |
764 | 1827 int diff_foldcolumn_new = 2; |
672 | 1828 tabpage_T *tp; |
7 | 1829 |
1830 p = p_dip; | |
1831 while (*p != NUL) | |
1832 { | |
1833 if (STRNCMP(p, "filler", 6) == 0) | |
1834 { | |
1835 p += 6; | |
1836 diff_flags_new |= DIFF_FILLER; | |
1837 } | |
1838 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8])) | |
1839 { | |
1840 p += 8; | |
1841 diff_context_new = getdigits(&p); | |
1842 } | |
1843 else if (STRNCMP(p, "icase", 5) == 0) | |
1844 { | |
1845 p += 5; | |
1846 diff_flags_new |= DIFF_ICASE; | |
1847 } | |
1848 else if (STRNCMP(p, "iwhite", 6) == 0) | |
1849 { | |
1850 p += 6; | |
1851 diff_flags_new |= DIFF_IWHITE; | |
1852 } | |
764 | 1853 else if (STRNCMP(p, "horizontal", 10) == 0) |
1854 { | |
1855 p += 10; | |
1856 diff_flags_new |= DIFF_HORIZONTAL; | |
1857 } | |
1858 else if (STRNCMP(p, "vertical", 8) == 0) | |
1859 { | |
1860 p += 8; | |
1861 diff_flags_new |= DIFF_VERTICAL; | |
1862 } | |
1863 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11])) | |
1864 { | |
1865 p += 11; | |
1866 diff_foldcolumn_new = getdigits(&p); | |
1867 } | |
7 | 1868 if (*p != ',' && *p != NUL) |
1869 return FAIL; | |
1870 if (*p == ',') | |
1871 ++p; | |
1872 } | |
1873 | |
764 | 1874 /* Can't have both "horizontal" and "vertical". */ |
1875 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL)) | |
1876 return FAIL; | |
1877 | |
7 | 1878 /* If "icase" or "iwhite" was added or removed, need to update the diff. */ |
1879 if (diff_flags != diff_flags_new) | |
672 | 1880 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) |
1881 tp->tp_diff_invalid = TRUE; | |
7 | 1882 |
1883 diff_flags = diff_flags_new; | |
1884 diff_context = diff_context_new; | |
764 | 1885 diff_foldcolumn = diff_foldcolumn_new; |
7 | 1886 |
1887 diff_redraw(TRUE); | |
1888 | |
1889 /* recompute the scroll binding with the new option value, may | |
1890 * remove or add filler lines */ | |
1891 check_scrollbind((linenr_T)0, 0L); | |
1892 | |
1893 return OK; | |
1894 } | |
1895 | |
1896 /* | |
764 | 1897 * Return TRUE if 'diffopt' contains "horizontal". |
1898 */ | |
1899 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1900 diffopt_horizontal(void) |
764 | 1901 { |
1902 return (diff_flags & DIFF_HORIZONTAL) != 0; | |
1903 } | |
1904 | |
1905 /* | |
7 | 1906 * Find the difference within a changed line. |
1907 * Returns TRUE if the line was added, no other buffer has it. | |
1908 */ | |
1909 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1910 diff_find_change( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1911 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1912 linenr_T lnum, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1913 int *startp, /* first char of the change */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1914 int *endp) /* last char of the change */ |
7 | 1915 { |
1916 char_u *line_org; | |
1917 char_u *line_new; | |
1918 int i; | |
829 | 1919 int si_org, si_new; |
1920 int ei_org, ei_new; | |
7 | 1921 diff_T *dp; |
1922 int idx; | |
1923 int off; | |
1924 int added = TRUE; | |
1925 | |
1926 /* Make a copy of the line, the next ml_get() will invalidate it. */ | |
1927 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE)); | |
1928 if (line_org == NULL) | |
1929 return FALSE; | |
1930 | |
1931 idx = diff_buf_idx(wp->w_buffer); | |
1932 if (idx == DB_COUNT) /* cannot happen */ | |
1074 | 1933 { |
1934 vim_free(line_org); | |
7 | 1935 return FALSE; |
1074 | 1936 } |
7 | 1937 |
1938 /* search for a change that includes "lnum" in the list of diffblocks. */ | |
672 | 1939 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) |
7 | 1940 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx]) |
1941 break; | |
672 | 1942 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL) |
1074 | 1943 { |
1944 vim_free(line_org); | |
7 | 1945 return FALSE; |
1074 | 1946 } |
7 | 1947 |
1948 off = lnum - dp->df_lnum[idx]; | |
1949 | |
1950 for (i = 0; i < DB_COUNT; ++i) | |
672 | 1951 if (curtab->tp_diffbuf[i] != NULL && i != idx) |
7 | 1952 { |
1953 /* Skip lines that are not in the other change (filler lines). */ | |
1954 if (off >= dp->df_count[i]) | |
1955 continue; | |
1956 added = FALSE; | |
829 | 1957 line_new = ml_get_buf(curtab->tp_diffbuf[i], |
1958 dp->df_lnum[i] + off, FALSE); | |
7 | 1959 |
1960 /* Search for start of difference */ | |
829 | 1961 si_org = si_new = 0; |
1962 while (line_org[si_org] != NUL) | |
1963 { | |
1964 if ((diff_flags & DIFF_IWHITE) | |
1965 && vim_iswhite(line_org[si_org]) | |
1966 && vim_iswhite(line_new[si_new])) | |
1967 { | |
835 | 1968 si_org = (int)(skipwhite(line_org + si_org) - line_org); |
1969 si_new = (int)(skipwhite(line_new + si_new) - line_new); | |
829 | 1970 } |
1971 else | |
1972 { | |
1973 if (line_org[si_org] != line_new[si_new]) | |
1974 break; | |
1975 ++si_org; | |
1976 ++si_new; | |
1977 } | |
1978 } | |
7 | 1979 #ifdef FEAT_MBYTE |
1980 if (has_mbyte) | |
1981 { | |
1982 /* Move back to first byte of character in both lines (may | |
1983 * have "nn^" in line_org and "n^ in line_new). */ | |
829 | 1984 si_org -= (*mb_head_off)(line_org, line_org + si_org); |
1985 si_new -= (*mb_head_off)(line_new, line_new + si_new); | |
7 | 1986 } |
1987 #endif | |
829 | 1988 if (*startp > si_org) |
1989 *startp = si_org; | |
7 | 1990 |
1991 /* Search for end of difference, if any. */ | |
829 | 1992 if (line_org[si_org] != NUL || line_new[si_new] != NUL) |
7 | 1993 { |
1994 ei_org = (int)STRLEN(line_org); | |
1995 ei_new = (int)STRLEN(line_new); | |
829 | 1996 while (ei_org >= *startp && ei_new >= si_new |
1997 && ei_org >= 0 && ei_new >= 0) | |
7 | 1998 { |
829 | 1999 if ((diff_flags & DIFF_IWHITE) |
2000 && vim_iswhite(line_org[ei_org]) | |
2001 && vim_iswhite(line_new[ei_new])) | |
2002 { | |
2003 while (ei_org >= *startp | |
2004 && vim_iswhite(line_org[ei_org])) | |
2005 --ei_org; | |
2006 while (ei_new >= si_new | |
2007 && vim_iswhite(line_new[ei_new])) | |
2008 --ei_new; | |
2009 } | |
2010 else | |
2011 { | |
2012 if (line_org[ei_org] != line_new[ei_new]) | |
2013 break; | |
2014 --ei_org; | |
2015 --ei_new; | |
2016 } | |
7 | 2017 } |
2018 if (*endp < ei_org) | |
2019 *endp = ei_org; | |
2020 } | |
2021 } | |
2022 | |
2023 vim_free(line_org); | |
2024 return added; | |
2025 } | |
2026 | |
2027 #if defined(FEAT_FOLDING) || defined(PROTO) | |
2028 /* | |
2029 * Return TRUE if line "lnum" is not close to a diff block, this line should | |
2030 * be in a fold. | |
2031 * Return FALSE if there are no diff blocks at all in this window. | |
2032 */ | |
2033 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2034 diff_infold(win_T *wp, linenr_T lnum) |
7 | 2035 { |
2036 int i; | |
2037 int idx = -1; | |
2038 int other = FALSE; | |
2039 diff_T *dp; | |
2040 | |
2041 /* Return if 'diff' isn't set. */ | |
2042 if (!wp->w_p_diff) | |
2043 return FALSE; | |
2044 | |
2045 for (i = 0; i < DB_COUNT; ++i) | |
2046 { | |
672 | 2047 if (curtab->tp_diffbuf[i] == wp->w_buffer) |
7 | 2048 idx = i; |
672 | 2049 else if (curtab->tp_diffbuf[i] != NULL) |
7 | 2050 other = TRUE; |
2051 } | |
2052 | |
2053 /* return here if there are no diffs in the window */ | |
2054 if (idx == -1 || !other) | |
2055 return FALSE; | |
2056 | |
672 | 2057 if (curtab->tp_diff_invalid) |
7 | 2058 ex_diffupdate(NULL); /* update after a big change */ |
2059 | |
2060 /* Return if there are no diff blocks. All lines will be folded. */ | |
672 | 2061 if (curtab->tp_first_diff == NULL) |
7 | 2062 return TRUE; |
2063 | |
672 | 2064 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) |
7 | 2065 { |
2066 /* If this change is below the line there can't be any further match. */ | |
2067 if (dp->df_lnum[idx] - diff_context > lnum) | |
2068 break; | |
2069 /* If this change ends before the line we have a match. */ | |
2070 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum) | |
2071 return FALSE; | |
2072 } | |
2073 return TRUE; | |
2074 } | |
2075 #endif | |
2076 | |
2077 /* | |
2078 * "dp" and "do" commands. | |
2079 */ | |
2080 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2081 nv_diffgetput(int put, long count) |
7 | 2082 { |
2083 exarg_T ea; | |
6314 | 2084 char_u buf[30]; |
7 | 2085 |
6314 | 2086 if (count == 0) |
2087 ea.arg = (char_u *)""; | |
2088 else | |
2089 { | |
2090 vim_snprintf((char *)buf, 30, "%ld", count); | |
2091 ea.arg = buf; | |
2092 } | |
7 | 2093 if (put) |
2094 ea.cmdidx = CMD_diffput; | |
2095 else | |
2096 ea.cmdidx = CMD_diffget; | |
2097 ea.addr_count = 0; | |
2098 ea.line1 = curwin->w_cursor.lnum; | |
2099 ea.line2 = curwin->w_cursor.lnum; | |
2100 ex_diffgetput(&ea); | |
2101 } | |
2102 | |
2103 /* | |
2104 * ":diffget" | |
2105 * ":diffput" | |
2106 */ | |
2107 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2108 ex_diffgetput(exarg_T *eap) |
7 | 2109 { |
2110 linenr_T lnum; | |
2111 int count; | |
2112 linenr_T off = 0; | |
2113 diff_T *dp; | |
2114 diff_T *dprev; | |
2115 diff_T *dfree; | |
2116 int idx_cur; | |
2117 int idx_other; | |
2118 int idx_from; | |
2119 int idx_to; | |
2120 int i; | |
2121 int added; | |
2122 char_u *p; | |
2123 aco_save_T aco; | |
2124 buf_T *buf; | |
2125 int start_skip, end_skip; | |
2126 int new_count; | |
648 | 2127 int buf_empty; |
1075 | 2128 int found_not_ma = FALSE; |
7 | 2129 |
2130 /* Find the current buffer in the list of diff buffers. */ | |
2131 idx_cur = diff_buf_idx(curbuf); | |
2132 if (idx_cur == DB_COUNT) | |
2133 { | |
2134 EMSG(_("E99: Current buffer is not in diff mode")); | |
2135 return; | |
2136 } | |
2137 | |
2138 if (*eap->arg == NUL) | |
2139 { | |
2140 /* No argument: Find the other buffer in the list of diff buffers. */ | |
2141 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other) | |
672 | 2142 if (curtab->tp_diffbuf[idx_other] != curbuf |
1075 | 2143 && curtab->tp_diffbuf[idx_other] != NULL) |
2144 { | |
2145 if (eap->cmdidx != CMD_diffput | |
2146 || curtab->tp_diffbuf[idx_other]->b_p_ma) | |
2147 break; | |
2148 found_not_ma = TRUE; | |
2149 } | |
7 | 2150 if (idx_other == DB_COUNT) |
2151 { | |
1075 | 2152 if (found_not_ma) |
2153 EMSG(_("E793: No other buffer in diff mode is modifiable")); | |
2154 else | |
2155 EMSG(_("E100: No other buffer in diff mode")); | |
7 | 2156 return; |
2157 } | |
2158 | |
2159 /* Check that there isn't a third buffer in the list */ | |
2160 for (i = idx_other + 1; i < DB_COUNT; ++i) | |
672 | 2161 if (curtab->tp_diffbuf[i] != curbuf |
2162 && curtab->tp_diffbuf[i] != NULL | |
2163 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma)) | |
7 | 2164 { |
2165 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use")); | |
2166 return; | |
2167 } | |
2168 } | |
2169 else | |
2170 { | |
2171 /* Buffer number or pattern given. Ignore trailing white space. */ | |
2172 p = eap->arg + STRLEN(eap->arg); | |
2173 while (p > eap->arg && vim_iswhite(p[-1])) | |
2174 --p; | |
2175 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i) | |
2176 ; | |
2177 if (eap->arg + i == p) /* digits only */ | |
2178 i = atol((char *)eap->arg); | |
2179 else | |
2180 { | |
4236 | 2181 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE); |
7 | 2182 if (i < 0) |
2183 return; /* error message already given */ | |
2184 } | |
2185 buf = buflist_findnr(i); | |
2186 if (buf == NULL) | |
2187 { | |
2188 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg); | |
2189 return; | |
2190 } | |
1788 | 2191 if (buf == curbuf) |
2192 return; /* nothing to do */ | |
7 | 2193 idx_other = diff_buf_idx(buf); |
2194 if (idx_other == DB_COUNT) | |
2195 { | |
2196 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg); | |
2197 return; | |
2198 } | |
2199 } | |
2200 | |
2201 diff_busy = TRUE; | |
2202 | |
2203 /* When no range given include the line above or below the cursor. */ | |
2204 if (eap->addr_count == 0) | |
2205 { | |
2206 /* Make it possible that ":diffget" on the last line gets line below | |
2207 * the cursor line when there is no difference above the cursor. */ | |
2208 if (eap->cmdidx == CMD_diffget | |
2209 && eap->line1 == curbuf->b_ml.ml_line_count | |
2210 && diff_check(curwin, eap->line1) == 0 | |
2211 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0)) | |
2212 ++eap->line2; | |
2213 else if (eap->line1 > 0) | |
2214 --eap->line1; | |
2215 } | |
2216 | |
2217 if (eap->cmdidx == CMD_diffget) | |
2218 { | |
2219 idx_from = idx_other; | |
2220 idx_to = idx_cur; | |
2221 } | |
2222 else | |
2223 { | |
2224 idx_from = idx_cur; | |
2225 idx_to = idx_other; | |
2226 /* Need to make the other buffer the current buffer to be able to make | |
2227 * changes in it. */ | |
2228 /* set curwin/curbuf to buf and save a few things */ | |
672 | 2229 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]); |
7 | 2230 } |
2231 | |
819 | 2232 /* May give the warning for a changed buffer here, which can trigger the |
2233 * FileChangedRO autocommand, which may do nasty things and mess | |
2234 * everything up. */ | |
2235 if (!curbuf->b_changed) | |
2236 { | |
2237 change_warning(0); | |
2238 if (diff_buf_idx(curbuf) != idx_to) | |
2239 { | |
2240 EMSG(_("E787: Buffer changed unexpectedly")); | |
2241 return; | |
2242 } | |
2243 } | |
2244 | |
7 | 2245 dprev = NULL; |
672 | 2246 for (dp = curtab->tp_first_diff; dp != NULL; ) |
7 | 2247 { |
2248 if (dp->df_lnum[idx_cur] > eap->line2 + off) | |
2249 break; /* past the range that was specified */ | |
2250 | |
2251 dfree = NULL; | |
2252 lnum = dp->df_lnum[idx_to]; | |
2253 count = dp->df_count[idx_to]; | |
2254 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off | |
2255 && u_save(lnum - 1, lnum + count) != FAIL) | |
2256 { | |
2257 /* Inside the specified range and saving for undo worked. */ | |
2258 start_skip = 0; | |
2259 end_skip = 0; | |
2260 if (eap->addr_count > 0) | |
2261 { | |
2262 /* A range was specified: check if lines need to be skipped. */ | |
2263 start_skip = eap->line1 + off - dp->df_lnum[idx_cur]; | |
2264 if (start_skip > 0) | |
2265 { | |
2266 /* range starts below start of current diff block */ | |
2267 if (start_skip > count) | |
2268 { | |
2269 lnum += count; | |
2270 count = 0; | |
2271 } | |
2272 else | |
2273 { | |
2274 count -= start_skip; | |
2275 lnum += start_skip; | |
2276 } | |
2277 } | |
2278 else | |
2279 start_skip = 0; | |
2280 | |
2281 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1 | |
2282 - (eap->line2 + off); | |
2283 if (end_skip > 0) | |
2284 { | |
2285 /* range ends above end of current/from diff block */ | |
2286 if (idx_cur == idx_from) /* :diffput */ | |
2287 { | |
2288 i = dp->df_count[idx_cur] - start_skip - end_skip; | |
2289 if (count > i) | |
2290 count = i; | |
2291 } | |
2292 else /* :diffget */ | |
2293 { | |
2294 count -= end_skip; | |
2295 end_skip = dp->df_count[idx_from] - start_skip - count; | |
2296 if (end_skip < 0) | |
2297 end_skip = 0; | |
2298 } | |
2299 } | |
2300 else | |
2301 end_skip = 0; | |
2302 } | |
2303 | |
6286 | 2304 buf_empty = bufempty(); |
7 | 2305 added = 0; |
2306 for (i = 0; i < count; ++i) | |
2307 { | |
648 | 2308 /* remember deleting the last line of the buffer */ |
2309 buf_empty = curbuf->b_ml.ml_line_count == 1; | |
7 | 2310 ml_delete(lnum, FALSE); |
2311 --added; | |
2312 } | |
2313 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i) | |
2314 { | |
2315 linenr_T nr; | |
2316 | |
2317 nr = dp->df_lnum[idx_from] + start_skip + i; | |
672 | 2318 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count) |
7 | 2319 break; |
819 | 2320 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from], |
2321 nr, FALSE)); | |
7 | 2322 if (p != NULL) |
2323 { | |
2324 ml_append(lnum + i - 1, p, 0, FALSE); | |
2325 vim_free(p); | |
2326 ++added; | |
648 | 2327 if (buf_empty && curbuf->b_ml.ml_line_count == 2) |
2328 { | |
2329 /* Added the first line into an empty buffer, need to | |
2330 * delete the dummy empty line. */ | |
2331 buf_empty = FALSE; | |
2332 ml_delete((linenr_T)2, FALSE); | |
2333 } | |
7 | 2334 } |
2335 } | |
2336 new_count = dp->df_count[idx_to] + added; | |
2337 dp->df_count[idx_to] = new_count; | |
2338 | |
2339 if (start_skip == 0 && end_skip == 0) | |
2340 { | |
2341 /* Check if there are any other buffers and if the diff is | |
2342 * equal in them. */ | |
2343 for (i = 0; i < DB_COUNT; ++i) | |
819 | 2344 if (curtab->tp_diffbuf[i] != NULL && i != idx_from |
2345 && i != idx_to | |
7 | 2346 && !diff_equal_entry(dp, idx_from, i)) |
2347 break; | |
2348 if (i == DB_COUNT) | |
2349 { | |
2350 /* delete the diff entry, the buffers are now equal here */ | |
2351 dfree = dp; | |
2352 dp = dp->df_next; | |
2353 if (dprev == NULL) | |
672 | 2354 curtab->tp_first_diff = dp; |
7 | 2355 else |
2356 dprev->df_next = dp; | |
2357 } | |
2358 } | |
2359 | |
2360 /* Adjust marks. This will change the following entries! */ | |
2361 if (added != 0) | |
2362 { | |
2363 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added); | |
2364 if (curwin->w_cursor.lnum >= lnum) | |
2365 { | |
2366 /* Adjust the cursor position if it's in/after the changed | |
2367 * lines. */ | |
2368 if (curwin->w_cursor.lnum >= lnum + count) | |
2369 curwin->w_cursor.lnum += added; | |
2370 else if (added < 0) | |
2371 curwin->w_cursor.lnum = lnum; | |
2372 } | |
2373 } | |
2374 changed_lines(lnum, 0, lnum + count, (long)added); | |
2375 | |
2376 if (dfree != NULL) | |
2377 { | |
2378 /* Diff is deleted, update folds in other windows. */ | |
2379 #ifdef FEAT_FOLDING | |
2380 diff_fold_update(dfree, idx_to); | |
2381 #endif | |
2382 vim_free(dfree); | |
2383 } | |
2384 else | |
2385 /* mark_adjust() may have changed the count in a wrong way */ | |
2386 dp->df_count[idx_to] = new_count; | |
2387 | |
2388 /* When changing the current buffer, keep track of line numbers */ | |
2389 if (idx_cur == idx_to) | |
2390 off += added; | |
2391 } | |
2392 | |
2393 /* If before the range or not deleted, go to next diff. */ | |
2394 if (dfree == NULL) | |
2395 { | |
2396 dprev = dp; | |
2397 dp = dp->df_next; | |
2398 } | |
2399 } | |
2400 | |
2401 /* restore curwin/curbuf and a few other things */ | |
2445
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2402 if (eap->cmdidx != CMD_diffget) |
7 | 2403 { |
2404 /* Syncing undo only works for the current buffer, but we change | |
2405 * another buffer. Sync undo if the command was typed. This isn't | |
2406 * 100% right when ":diffput" is used in a function or mapping. */ | |
2407 if (KeyTyped) | |
825 | 2408 u_sync(FALSE); |
7 | 2409 aucmd_restbuf(&aco); |
2410 } | |
2411 | |
2412 diff_busy = FALSE; | |
2413 | |
2414 /* Check that the cursor is on a valid character and update it's position. | |
2415 * When there were filler lines the topline has become invalid. */ | |
2416 check_cursor(); | |
2417 changed_line_abv_curs(); | |
2418 | |
2419 /* Also need to redraw the other buffers. */ | |
2420 diff_redraw(FALSE); | |
2421 } | |
2422 | |
2423 #ifdef FEAT_FOLDING | |
2424 /* | |
2425 * Update folds for all diff buffers for entry "dp". | |
2426 * Skip buffer with index "skip_idx". | |
2427 * When there are no diffs, all folds are removed. | |
2428 */ | |
2429 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2430 diff_fold_update(diff_T *dp, int skip_idx) |
7 | 2431 { |
2432 int i; | |
2433 win_T *wp; | |
2434 | |
2435 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2436 for (i = 0; i < DB_COUNT; ++i) | |
672 | 2437 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx) |
7 | 2438 foldUpdate(wp, dp->df_lnum[i], |
2439 dp->df_lnum[i] + dp->df_count[i]); | |
2440 } | |
2441 #endif | |
2442 | |
2443 /* | |
2444 * Return TRUE if buffer "buf" is in diff-mode. | |
2445 */ | |
2446 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2447 diff_mode_buf(buf_T *buf) |
7 | 2448 { |
672 | 2449 tabpage_T *tp; |
2450 | |
2451 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) | |
2452 if (diff_buf_idx_tp(buf, tp) != DB_COUNT) | |
2453 return TRUE; | |
2454 return FALSE; | |
7 | 2455 } |
2456 | |
2457 /* | |
2458 * Move "count" times in direction "dir" to the next diff block. | |
2459 * Return FAIL if there isn't such a diff block. | |
2460 */ | |
2461 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2462 diff_move_to(int dir, long count) |
7 | 2463 { |
2464 int idx; | |
2465 linenr_T lnum = curwin->w_cursor.lnum; | |
2466 diff_T *dp; | |
2467 | |
2468 idx = diff_buf_idx(curbuf); | |
672 | 2469 if (idx == DB_COUNT || curtab->tp_first_diff == NULL) |
7 | 2470 return FAIL; |
2471 | |
672 | 2472 if (curtab->tp_diff_invalid) |
7 | 2473 ex_diffupdate(NULL); /* update after a big change */ |
2474 | |
672 | 2475 if (curtab->tp_first_diff == NULL) /* no diffs today */ |
7 | 2476 return FAIL; |
2477 | |
2478 while (--count >= 0) | |
2479 { | |
2480 /* Check if already before first diff. */ | |
672 | 2481 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx]) |
7 | 2482 break; |
2483 | |
672 | 2484 for (dp = curtab->tp_first_diff; ; dp = dp->df_next) |
7 | 2485 { |
2486 if (dp == NULL) | |
2487 break; | |
2488 if ((dir == FORWARD && lnum < dp->df_lnum[idx]) | |
2489 || (dir == BACKWARD | |
2490 && (dp->df_next == NULL | |
2491 || lnum <= dp->df_next->df_lnum[idx]))) | |
2492 { | |
2493 lnum = dp->df_lnum[idx]; | |
2494 break; | |
2495 } | |
2496 } | |
2497 } | |
2498 | |
2499 /* don't end up past the end of the file */ | |
2500 if (lnum > curbuf->b_ml.ml_line_count) | |
2501 lnum = curbuf->b_ml.ml_line_count; | |
2502 | |
2503 /* When the cursor didn't move at all we fail. */ | |
2504 if (lnum == curwin->w_cursor.lnum) | |
2505 return FAIL; | |
2506 | |
2507 setpcmark(); | |
2508 curwin->w_cursor.lnum = lnum; | |
2509 curwin->w_cursor.col = 0; | |
2510 | |
2511 return OK; | |
2512 } | |
2513 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2514 linenr_T |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2515 diff_get_corresponding_line( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2516 buf_T *buf1, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2517 linenr_T lnum1, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2518 buf_T *buf2, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2519 linenr_T lnum3) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2520 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2521 int idx1; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2522 int idx2; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2523 diff_T *dp; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2524 int baseline = 0; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2525 linenr_T lnum2; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2526 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2527 idx1 = diff_buf_idx(buf1); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2528 idx2 = diff_buf_idx(buf2); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2529 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2530 return lnum1; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2531 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2532 if (curtab->tp_diff_invalid) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2533 ex_diffupdate(NULL); /* update after a big change */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2534 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2535 if (curtab->tp_first_diff == NULL) /* no diffs today */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2536 return lnum1; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2537 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2538 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2539 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2540 if (dp->df_lnum[idx1] > lnum1) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2541 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2542 lnum2 = lnum1 - baseline; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2543 /* don't end up past the end of the file */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2544 if (lnum2 > buf2->b_ml.ml_line_count) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2545 lnum2 = buf2->b_ml.ml_line_count; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2546 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2547 return lnum2; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2548 } |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2549 else if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2550 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2551 /* Inside the diffblock */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2552 baseline = lnum1 - dp->df_lnum[idx1]; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2553 if (baseline > dp->df_count[idx2]) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2554 baseline = dp->df_count[idx2]; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2555 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2556 return dp->df_lnum[idx2] + baseline; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2557 } |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2558 else if ( (dp->df_lnum[idx1] == lnum1) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2559 && (dp->df_count[idx1] == 0) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2560 && (dp->df_lnum[idx2] <= lnum3) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2561 && ((dp->df_lnum[idx2] + dp->df_count[idx2]) > lnum3)) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2562 /* |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2563 * Special case: if the cursor is just after a zero-count |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2564 * block (i.e. all filler) and the target cursor is already |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2565 * inside the corresponding block, leave the target cursor |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2566 * unmoved. This makes repeated CTRL-W W operations work |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2567 * as expected. |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2568 */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2569 return lnum3; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2570 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1]) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2571 - (dp->df_lnum[idx2] + dp->df_count[idx2]); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2572 } |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2573 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2574 /* If we get here then the cursor is after the last diff */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2575 lnum2 = lnum1 - baseline; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2576 /* don't end up past the end of the file */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2577 if (lnum2 > buf2->b_ml.ml_line_count) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2578 lnum2 = buf2->b_ml.ml_line_count; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2579 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2580 return lnum2; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2581 } |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2086
diff
changeset
|
2582 |
7 | 2583 #if defined(FEAT_FOLDING) || defined(PROTO) |
2584 /* | |
2585 * For line "lnum" in the current window find the equivalent lnum in window | |
2586 * "wp", compensating for inserted/deleted lines. | |
2587 */ | |
2588 linenr_T | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2589 diff_lnum_win(linenr_T lnum, win_T *wp) |
7 | 2590 { |
2591 diff_T *dp; | |
2592 int idx; | |
2593 int i; | |
2594 linenr_T n; | |
2595 | |
2596 idx = diff_buf_idx(curbuf); | |
2597 if (idx == DB_COUNT) /* safety check */ | |
2598 return (linenr_T)0; | |
2599 | |
672 | 2600 if (curtab->tp_diff_invalid) |
7 | 2601 ex_diffupdate(NULL); /* update after a big change */ |
2602 | |
2603 /* search for a change that includes "lnum" in the list of diffblocks. */ | |
672 | 2604 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) |
7 | 2605 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx]) |
2606 break; | |
2607 | |
2608 /* When after the last change, compute relative to the last line number. */ | |
2609 if (dp == NULL) | |
2610 return wp->w_buffer->b_ml.ml_line_count | |
2611 - (curbuf->b_ml.ml_line_count - lnum); | |
2612 | |
2613 /* Find index for "wp". */ | |
2614 i = diff_buf_idx(wp->w_buffer); | |
2615 if (i == DB_COUNT) /* safety check */ | |
2616 return (linenr_T)0; | |
2617 | |
2618 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]); | |
2619 if (n > dp->df_lnum[i] + dp->df_count[i]) | |
2620 n = dp->df_lnum[i] + dp->df_count[i]; | |
2621 return n; | |
2622 } | |
2623 #endif | |
2624 | |
2625 #endif /* FEAT_DIFF */ |