-
Notifications
You must be signed in to change notification settings - Fork 7
/
vpatch.c
3222 lines (3051 loc) · 81.1 KB
/
vpatch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* wiggle - apply rejected patches
*
* Copyright (C) 2005 Neil Brown <neilb@cse.unsw.edu.au>
* Copyright (C) 2010-2013 Neil Brown <neilb@suse.de>
* Copyright (C) 2014-2020 Neil Brown <neil@brown.name>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
*
* Author: Neil Brown
* Email: <neil@brown.name>
*/
/*
* vpatch - visual front end for wiggle - aka Browse mode.
*
* "files" display, lists all files with statistics
* - can hide various lines including subdirectories
* and files without wiggles or conflicts
* "merge" display shows various views of merged file with different
* parts in different colours.
*
* The window can be split horizontally to show the original and result
* beside the diff, and each different branch can be shown alone.
*
*/
#include "wiggle.h"
#include <curses.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/wait.h>
static void term_init(int raw);
static int intr_kills = 0;
/* global attributes */
static unsigned int a_delete, a_added, a_common, a_sep, a_void,
a_unmatched, a_extra, a_already;
static unsigned int a_has_conflicts, a_has_wiggles, a_no_wiggles, a_saved;
/******************************************************************
* Help window
* We display help in an insert, leaving 5 columns left and right,
* and 2 rows top and bottom, but at most 58x15 plus border
* In help mode:
* SPC or RTN moves down or to next page
* BKSPC goes backwards
* 'q' returns to origin screen
* '?' show help on help
* left and right scroll help view
*
* A help text is an array of lines of text
*/
static char *help_help[] = {
" You are viewing the help page for the help viewer.",
"You normally get here by typing '?'",
"",
"The following keystrokes work in the help viewer:",
" ? display this help message",
" q return to previous view",
" SPC move forward through help document",
" RTN same as SPC",
" BKSP move backward through help document",
" RIGHT scroll help window so text on the right appears",
" LEFT scroll help window so text on the left appears",
NULL
};
static char *help_missing[] = {
"The file that this patch applies to appears",
"to be missing.",
"Please type 'q' to continue",
NULL
};
static char *help_corrupt[] = {
"This patch appears to be corrupt",
"Please type 'q' to continue",
NULL
};
/* We can give one or two pages to display in the help window.
* The first is specific to the current context. The second
* is optional and may provide help in a more broad context.
*/
static int help_window(char *page1[], char *page2[], int query)
{
int rows, cols;
int top, left;
int r, c;
int ch;
char **page = page1;
int line = 0;
int shift = 0;
getmaxyx(stdscr, rows, cols);
if (cols < 70) {
left = 6;
cols = cols-12;
} else {
left = (cols-58)/2 - 1;
cols = 58;
}
if (rows < 21) {
top = 3;
rows = rows - 6;
} else {
top = (rows-15)/2 - 1;
rows = 15;
}
/* Draw a border around the 'help' area */
(void)attrset(A_STANDOUT);
for (c = left; c < left+cols; c++) {
mvaddch(top-1, c, '-');
mvaddch(top+rows, c, '-');
}
for (r = top; r < top + rows ; r++) {
mvaddch(r, left-1, '|');
mvaddch(r, left+cols, '|');
}
mvaddch(top-1, left-1, '/');
mvaddch(top-1, left+cols, '\\');
mvaddch(top+rows, left-1, '\\');
mvaddch(top+rows, left+cols, '/');
if (query) {
mvaddstr(top-1, left + cols/2 - 4, "Question");
mvaddstr(top+rows, left + cols/2 - 9,
"Answer Y, N, or Q.");
} else {
mvaddstr(top-1, left + cols/2 - 9,
"HELP - 'q' to exit");
mvaddstr(top+rows, left+cols/2 - 17,
"Press SPACE for more, '?' for help");
}
(void)attrset(A_NORMAL);
while (1) {
char **lnp = page + line;
/* Draw as much of the page at the current offset
* as fits.
*/
for (r = 0; r < rows; r++) {
char *ln = *lnp;
int sh = shift;
if (ln)
lnp++;
else
ln = "";
while (*ln && sh > 0) {
ln++;
sh--;
}
for (c = 0; c < cols; c++) {
int chr = *ln;
if (chr)
ln++;
else
chr = ' ';
mvaddch(top+r, left+c, chr);
}
}
move(top+rows-1, left);
ch = getch();
switch (ch) {
case 'C' - 64:
case 'Q':
case 'q':
return -1;
break;
case 'Y':
case 'y':
if (query)
return 1;
break;
case 'N':
case 'n':
if (query)
return 0;
break;
case '?':
if (page1 != help_help)
help_window(help_help, NULL, 0);
break;
case ' ':
case '\r': /* page-down */
for (r = 0; r < rows-2; r++)
if (page[line])
line++;
if (!page[line] && !query) {
line = 0;
if (page == page1)
page = page2;
else
page = NULL;
if (page == NULL)
return -1;
}
break;
case '\b': /* page up */
if (line > 0) {
line -= (rows-2);
if (line < 0)
line = 0;
} else {
if (page == page2)
page = page1;
else
page = page2;
if (page == NULL)
page = page1;
line = 0;
}
break;
case KEY_LEFT:
if (shift > 0)
shift--;
break;
case KEY_RIGHT:
shift++;
break;
case KEY_UP:
if (line > 0)
line--;
break;
case KEY_DOWN:
if (page[line])
line++;
break;
}
}
}
static char *typenames[] = {
[End] = "End",
[Unmatched] = "Unmatched",
[Unchanged] = "Unchanged",
[Extraneous] = "Extraneous",
[Changed] = "Changed",
[Conflict] = "Conflict",
[AlreadyApplied] = "AlreadyApplied",
};
/* When we merge the original and the diff together we need
* to keep track of where everything came from.
* When we display the different views, we need to be able to
* select certain portions of the whole document.
* These flags are used to identify what is present, and to
* request different parts be extracted. They also help
* guide choice of colour.
*/
#define BEFORE 1
#define AFTER 2
#define ORIG 4
#define RESULT 8
#define CHANGES 16 /* A change is visible here,
* so 2 streams need to be shown */
#define WIGGLED 32 /* a conflict that was successfully resolved */
#define CONFLICTED 64 /* a conflict that was not successfully resolved */
/* Displaying a Merge.
* The first step is to linearise the merge. The merge in inherently
* parallel with before/after streams. However much of the whole document
* is linear as normally much of the original in unchanged.
* All parallelism comes from the patch. This normally produces two
* parallel stream, but in the case of a conflict can produce three.
* For browsing the merge we only ever show two alternates in-line.
* When there are three we use two panes with 1 or 2 alternates in each.
* So to linearise the two streams we find lines that are completely
* unchanged (same for all 3 streams, or missing in 2nd and 3rd) which bound
* a region where there are changes. We include everything between
* these twice, in two separate passes. The exact interpretation of the
* passes is handled at a higher level but will be one of:
* original and result
* before and after
* original and after (for a conflict)
* This is all encoded in the 'struct merge'. An array of these describes
* the whole document.
*
* At any position in the merge we can be in one of 3 states:
* 0: unchanged section
* 1: first pass
* 2: second pass
*
* So to walk a merge in display order we need a position in the merge,
* a current state, and when in a changed section, we need to know the
* bounds of that changed section.
* This is all encoded in 'struct mpos'.
*
* Each location may or may not be visible depending on certain
* display options.
*
* Also, some locations might be 'invalid' in that they don't need to be displayed.
* For example when the patch leaves a section of the original unchanged,
* we only need to see the original - the before/after sections are treated
* as invalid and are not displayed.
* The visibility of newlines is crucial and guides the display. One line
* of displayed text is all the visible sections between two visible newlines.
*
* Counting lines is a bit tricky. We only worry about line numbers in the
* original (stream 0) as these could compare with line numbers mentioned in
* patch chunks.
* We count 2 for every line: 1 for everything before the newline and 1 for the newline.
* That way we don't get a full counted line until we see the first char after the
* newline, so '+' lines are counted with the previous line.
*
*/
struct mp {
int m; /* merger index */
int s; /* stream 0,1,2 for a,b,c */
int o; /* offset in that stream */
int lineno; /* Counts newlines in stream 0
* set lsb when see newline.
* add one when not newline and lsb set
*/
};
struct mpos {
struct mp p, /* the current point (end of a line) */
lo, /* eol for start of the current group */
hi; /* eol for end of the current group */
int state; /*
* 0 if on an unchanged (lo/hi not meaningful)
* 1 if on the '-' of a diff,
* 2 if on the '+' of a diff
*/
};
struct cursor {
struct mp pos; /* where in the document we are (an element) */
int offset; /* which char in that element */
int target; /* display column - or -1 if we are looking for 'pos' */
int col; /* where we found pos or target */
int width; /* Size of char, for moving to the right */
int alt; /* Cursor is in alternate window */
};
/* used for checking location during search */
static int same_mp(struct mp a, struct mp b)
{
return a.m == b.m &&
a.s == b.s &&
a.o == b.o;
}
static int same_mpos(struct mpos a, struct mpos b)
{
return same_mp(a.p, b.p) &&
(a.state == b.state || a.state == 0 || b.state == 0);
}
/* Check if a particular stream is meaningful in a particular merge
* section. e.g. in an Unchanged section, only stream 0, the
* original, is meaningful. This is used to avoid walking down
* pointless paths.
*/
static int stream_valid(int s, enum mergetype type)
{
switch (type) {
case End:
return 1;
case Unmatched:
return s == 0;
case Unchanged:
return s == 0;
case Extraneous:
return s == 2;
case Changed:
return s != 1;
case Conflict:
return 1;
case AlreadyApplied:
return 1;
}
return 0;
}
/*
* Advance the 'pos' in the current mergepos returning the next
* element (word).
* This walks the merges in sequence, and the streams within
* each merge.
*/
static struct elmnt next_melmnt(struct mp *pos,
struct file fm, struct file fb, struct file fa,
struct merge *m)
{
pos->o++;
while (pos->m < 0 || m[pos->m].type != End) {
int l = 0; /* Length remaining in current merge section */
if (pos->m >= 0)
switch (pos->s) {
case 0:
l = m[pos->m].al;
break;
case 1:
l = m[pos->m].bl;
break;
case 2:
l = m[pos->m].cl;
break;
}
if (pos->o >= l) {
/* Offset has reached length, choose new stream or
* new merge */
pos->o = 0;
do {
pos->s++;
if (pos->s > 2) {
pos->s = 0;
pos->m++;
}
} while (!stream_valid(pos->s, m[pos->m].oldtype));
} else
break;
}
if (pos->m == -1 || m[pos->m].type == End) {
struct elmnt e;
e.start = NULL; e.hash = 0; e.len = 0;
return e;
}
switch (pos->s) {
default: /* keep compiler happy */
case 0:
if (pos->lineno & 1)
pos->lineno++;
if (ends_line(fm.list[m[pos->m].a + pos->o]))
pos->lineno++;
return fm.list[m[pos->m].a + pos->o];
case 1: return fb.list[m[pos->m].b + pos->o];
case 2: return fa.list[m[pos->m].c + pos->o];
}
}
/* step current position.p backwards */
static struct elmnt prev_melmnt(struct mp *pos,
struct file fm, struct file fb, struct file fa,
struct merge *m)
{
if (pos->s == 0) {
if (m[pos->m].a + pos->o < fm.elcnt &&
ends_line(fm.list[m[pos->m].a + pos->o]))
pos->lineno--;
if (pos->lineno & 1)
pos->lineno--;
}
pos->o--;
while (pos->m >= 0 && pos->o < 0) {
do {
pos->s--;
if (pos->s < 0) {
pos->s = 2;
pos->m--;
}
} while (pos->m >= 0 &&
!stream_valid(pos->s, m[pos->m].oldtype));
if (pos->m >= 0) {
switch (pos->s) {
case 0:
pos->o = m[pos->m].al-1;
break;
case 1:
pos->o = m[pos->m].bl-1;
break;
case 2:
pos->o = m[pos->m].cl-1;
break;
}
}
}
if (pos->m < 0 || m[pos->m].type == End) {
struct elmnt e;
e.start = NULL; e.hash = 0; e.len = 0;
return e;
}
switch (pos->s) {
default: /* keep compiler happy */
case 0: return fm.list[m[pos->m].a + pos->o];
case 1: return fb.list[m[pos->m].b + pos->o];
case 2: return fa.list[m[pos->m].c + pos->o];
}
}
/* 'visible' not only checks if this stream in this merge should be
* visible in this mode, but also chooses which colour/highlight to use
* to display it.
*/
static int visible(int mode, struct merge *m, struct mpos *pos)
{
enum mergetype type;
int stream = pos->p.s;
if (mode == 0)
return -1;
if (pos->p.m < 0)
type = End;
else if (mode & RESULT)
type = m[pos->p.m].type;
else
type = m[pos->p.m].oldtype;
/* mode can be any combination of ORIG RESULT BEFORE AFTER */
switch (type) {
case End: /* The END is always visible */
return A_NORMAL;
case Unmatched: /* Visible in ORIG and RESULT */
if (mode & (ORIG|RESULT))
return a_unmatched;
break;
case Unchanged: /* visible everywhere, but only show stream 0 */
if (stream == 0)
return a_common;
break;
case Extraneous: /* stream 2 is visible in BEFORE and AFTER */
if ((mode & (BEFORE|AFTER))
&& stream == 2)
return a_extra;
break;
case Changed: /* stream zero visible ORIG and BEFORE, stream 2 elsewhere */
if (stream == 0 &&
(mode & (ORIG|BEFORE)))
return a_delete;
if (stream == 2 &&
(mode & (RESULT|AFTER)))
return a_added;
break;
case Conflict:
switch (stream) {
case 0:
if (mode & ORIG)
return a_unmatched | A_REVERSE;
break;
case 1:
if (mode & BEFORE)
return a_extra | A_UNDERLINE;
break;
case 2:
if (mode & (AFTER|RESULT))
return a_added | A_UNDERLINE;
break;
}
break;
case AlreadyApplied:
switch (stream) {
case 0:
if (mode & (ORIG|RESULT))
return a_already;
break;
case 1:
if (mode & BEFORE)
return a_delete | A_UNDERLINE;
break;
case 2:
if (mode & AFTER)
return a_added | A_UNDERLINE;
break;
}
break;
}
return -1;
}
/* checkline creates a summary of the sort of changes that
* are in a line, returning an "or" of
* CHANGES
* WIGGLED
* CONFLICTED
*/
static int check_line(struct mpos pos, struct file fm, struct file fb,
struct file fa,
struct merge *m, int mode)
{
int rv = 0;
struct elmnt e;
int unmatched = 0;
if (pos.p.m < 0)
return 0;
do {
int type = m[pos.p.m].oldtype;
if (mode & RESULT)
type = m[pos.p.m].type;
if (type == Changed)
rv |= CHANGES;
else if (type == Conflict) {
rv |= CONFLICTED | CHANGES;
} else if (type == AlreadyApplied) {
rv |= CONFLICTED;
if (mode & (BEFORE|AFTER))
rv |= CHANGES;
} else if (type == Extraneous) {
if (fb.list[m[pos.p.m].b].start[0] == '\0') {
/* hunk headers don't count as wiggles
* and nothing before a hunk header
* can possibly be part of this 'line' */
e.start = NULL;
break;
} else
rv |= WIGGLED;
} else if (type == Unmatched)
unmatched = 1;
if (m[pos.p.m].in_conflict > 1)
rv |= CONFLICTED | CHANGES;
if (m[pos.p.m].in_conflict == 1 &&
(pos.p.o < m[pos.p.m].lo ||
pos.p.o > m[pos.p.m].hi))
rv |= CONFLICTED | CHANGES;
e = prev_melmnt(&pos.p, fm, fb, fa, m);
} while (e.start != NULL &&
(!ends_line(e)
|| visible(mode, m, &pos) == -1));
/* This is a bit of a hack... If the end-of-line just
* before this line was changed, then quite possibly this
* line is part of a change too. This is particularly important
* when --ignore-blanks is in effect as newlines are not separate
* from other words. It could be that this test needs to be
* strengthened when I have examined more cases.
*/
if (e.start && m[pos.p.m].oldtype == Changed)
rv |= CHANGES;
if (unmatched && (rv & CHANGES))
rv |= WIGGLED;
return rv;
}
/* Find the next line in the merge which is visible.
* If we hit the end of a conflicted set during pass-1
* we rewind for pass-2.
* 'mode' tells which bits we want to see, possible one of
* the 4 parts (before/after/orig/result) or one of the pairs
* before+after or orig+result.
*/
static void next_mline(struct mpos *pos, struct file fm, struct file fb,
struct file fa,
struct merge *m, int mode)
{
int mask;
do {
struct mp prv;
int mode2;
prv = pos->p;
while (1) {
struct elmnt e = next_melmnt(&pos->p, fm, fb, fa, m);
if (e.start == NULL)
break;
if (ends_line(e) &&
visible(mode, m, pos) >= 0)
break;
}
mode2 = check_line(*pos, fm, fb, fa, m, mode);
if ((mode2 & CHANGES) && pos->state == 0) {
/* Just entered a diff-set */
pos->lo = pos->p;
pos->state = 1;
} else if (!(mode2 & CHANGES) && pos->state) {
/* Come to the end of a diff-set */
switch (pos->state) {
case 1:
/* Need to record the end */
pos->hi = prv;
/* time for another pass */
pos->p = pos->lo;
pos->state++;
break;
case 2:
/* finished final pass */
pos->state = 0;
break;
}
}
mask = ORIG|RESULT|BEFORE|AFTER;
switch (pos->state) {
case 1:
mask &= ~(RESULT|AFTER);
break;
case 2:
mask &= ~(ORIG|BEFORE);
break;
}
} while (visible(mode&mask, m, pos) < 0);
}
/* Move to previous line - simply the reverse of next_mline */
static void prev_mline(struct mpos *pos, struct file fm, struct file fb,
struct file fa,
struct merge *m, int mode)
{
int mask;
do {
struct mp prv;
int mode2;
prv = pos->p;
if (pos->p.m < 0)
return;
while (1) {
struct elmnt e = prev_melmnt(&pos->p, fm, fb, fa, m);
if (e.start == NULL)
break;
if (ends_line(e) &&
visible(mode, m, pos) >= 0)
break;
}
mode2 = check_line(*pos, fm, fb, fa, m, mode);
if ((mode2 & CHANGES) && pos->state == 0) {
/* Just entered a diff-set */
pos->hi = pos->p;
pos->state = 2;
} else if (!(mode2 & CHANGES) && pos->state) {
/* Come to the end (start) of a diff-set */
switch (pos->state) {
case 1:
/* finished final pass */
pos->state = 0;
break;
case 2:
/* Need to record the start */
pos->lo = prv;
/* time for another pass */
pos->p = pos->hi;
pos->state--;
break;
}
}
mask = ORIG|RESULT|BEFORE|AFTER;
switch (pos->state) {
case 1:
mask &= ~(RESULT|AFTER);
break;
case 2:
mask &= ~(ORIG|BEFORE);
break;
}
} while (visible(mode&mask, m, pos) < 0);
}
/* blank a whole row of display */
static void blank(int row, int start, int cols, unsigned int attr)
{
(void)attrset(attr);
move(row, start);
while (cols-- > 0)
addch(' ');
}
/* search of a string on one display line. If found, update the
* cursor.
*/
static int mcontains(struct mpos pos,
struct file fm, struct file fb, struct file fa,
struct merge *m,
int mode, char *search, struct cursor *curs,
int dir, int ignore_case)
{
/* See if any of the files, between start of this line and here,
* contain the search string.
* However this is modified by dir:
* -2: find last match *before* curs
* -1: find last match at-or-before curs
* 1: find first match at-or-after curs
* 2: find first match *after* curs
*
* We only test for equality with curs, so if it is on a different
* line it will not be found and everything is before/after.
* As we search from end-of-line to start we find the last
* match first.
* For a forward search, we stop when we find curs.
* For a backward search, we forget anything found when we find curs.
*/
struct elmnt e;
int found = 0;
struct mp mp;
int o = 0;
int len = strlen(search);
do {
e = prev_melmnt(&pos.p, fm, fb, fa, m);
if (e.start && e.start[0]) {
int i;
int curs_i;
if (same_mp(pos.p, curs->pos))
curs_i = curs->offset;
else
curs_i = -1;
for (i = e.len-1; i >= 0; i--) {
if (i == curs_i && dir == -1)
/* next match is the one we want */
found = 0;
if (i == curs_i && dir == 2)
/* future matches not accepted */
goto break_while;
if ((!found || dir > 0) &&
(ignore_case ? strncasecmp : strncmp)
(e.start+i, search, len) == 0) {
mp = pos.p;
o = i;
found = 1;
}
if (i == curs_i && dir == -2)
/* next match is the one we want */
found = 0;
if (i == curs_i && dir == 1)
/* future matches not accepted */
goto break_while;
}
}
} while (e.start != NULL &&
(!ends_line(e)
|| visible(mode, m, &pos) == -1));
break_while:
if (found) {
curs->pos = mp;
curs->offset = o;
}
return found;
}
/* Drawing the display window.
* There are 7 different ways we can display the data, each
* of which can be configured by a keystroke:
* o original - just show the original file with no changes, but still
* with highlights of what is changed or unmatched
* r result - show just the result of the merge. Conflicts just show
* the original, not the before/after options
* b before - show the 'before' stream of the patch
* a after - show the 'after' stream of the patch
* d diff - show just the patch, both before and after
* m merge - show the full merge with -+ sections for changes.
* If point is in a wiggled or conflicted section the
* window is split horizontally and the diff is shown
* in the bottom window
* | sidebyside - two panes, left and right. Left holds the merge,
* right holds the diff. In the case of a conflict,
* left holds orig/after, right holds before/after
*
* The horizontal split for 'merge' mode is managed as follows.
* - The window is split when we first visit a line that contains
* a wiggle or a conflict, and the second pane is removed when
* we next visit a line that contains no changes (is fully Unchanged).
* - to display the second pane, we find a visible end-of-line in the
* (BEFORE|AFTER) mode at-or-before the current end-of-line and
* the we centre that line.
* - We need to rewind to an unchanged section, and wind forward again
* to make sure that 'lo' and 'hi' are set properly.
* - every time we move, we redraw the second pane (see how that goes).
*/
/* draw_mside draws one text line or, in the case of sidebyside, one side
* of a textline.
* The 'mode' tells us what to draw via the 'visible()' function.
* It is one of ORIG RESULT BEFORE AFTER or ORIG|RESULT or BEFORE|AFTER
* It may also have WIGGLED or CONFLICTED ored in to trigger extra highlights.
* The desired cursor position is given in 'target' the actual end
* cursor position (allowing e.g. for tabs) is returned in *colp.
*/
static void draw_mside(int mode, int row, int offset, int start, int cols,
struct file fm, struct file fb, struct file fa,
struct merge *m,
struct mpos pos,
struct cursor *curs)
{
struct elmnt e;
int col = 0;
char tag;
unsigned int tag_attr;
int changed = 0;
switch (pos.state) {
default: /* keep compiler happy */
case 0: /* unchanged line */
tag = ' ';
tag_attr = A_NORMAL;
break;
case 1: /* 'before' text */
tag = '-';
tag_attr = a_delete;
if ((mode & ORIG) && (mode & CONFLICTED)) {
tag = '|';
tag_attr = a_delete | A_REVERSE;
}
mode &= (ORIG|BEFORE);
break;
case 2: /* the 'after' part */
tag = '+';
tag_attr = a_added;
mode &= (AFTER|RESULT);
break;
}
if (visible(mode, m, &pos) < 0) {
/* Not visible, just draw a blank */
blank(row, offset, cols, a_void);
if (curs) {
curs->width = -1;
curs->col = 0;
curs->pos = pos.p;
curs->offset = 0;
}
return;
}
(void)attrset(tag_attr);
mvaddch(row, offset, tag);
offset++;
cols--;
(void)attrset(A_NORMAL);
if (check_line(pos, fm, fb, fa, m, mode))
changed = 1;
/* find previous visible newline, or start of file */
do
e = prev_melmnt(&pos.p, fm, fb, fa, m);
while (e.start != NULL &&
(!ends_line(e) ||
visible(mode, m, &pos) == -1));
while (1) {
unsigned char *c;
unsigned int attr;
int highlight_space;
int l;
e = next_melmnt(&pos.p, fm, fb, fa, m);
if (!e.start)
break;
if (visible(mode, m, &pos) == -1)
continue;
if (e.start[0] == 0)
break;
c = (unsigned char *)e.start - e.prefix;
highlight_space = 0;
attr = visible(mode, m, &pos);
if ((attr == a_unmatched || attr == a_extra) &&
changed)
/* Only highlight spaces if there is a tab nearby */
for (l = 0; l < e.plen + e.prefix; l++)
if (c[l] == '\t')
highlight_space = 1;
if (!highlight_space && (c[0] == ' ' || c[0] == '\t')) {
/* always highlight space/tab at end-of-line */
struct mp nxt = pos.p;
struct elmnt nxte = next_melmnt(&nxt, fm, fb, fa, m);
if (nxte.start[0] == '\n')
highlight_space = 1;
}
for (l = 0; l < e.plen + e.prefix; l++) {
int scol = col;
if (*c == '\n')
break;
(void)attrset(attr);
if (*c >= ' ' && *c != 0x7f) {
if (highlight_space)
(void)attrset(attr|A_REVERSE);
if (col >= start && col < start+cols)
mvaddch(row, col-start+offset, *c);
col++;
} else if (*c == '\t') {
if (highlight_space)
(void)attrset(attr|A_UNDERLINE);
do {
if (col >= start && col < start+cols) {
mvaddch(row, col-start+offset, ' ');
} col++;
} while ((col&7) != 0);
} else {
if (col >= start && col < start+cols)
mvaddch(row, col-start+offset, '?');