2 * wiggle - apply rejected patches
4 * Copyright (C) 2005 Neil Brown <neilb@cse.unsw.edu.au>
5 * Copyright (C) 2010-2011 Neil Brown <neilb@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Email: <neilb@suse.de>
27 * vpatch - visual front end for wiggle - aka Browse mode.
29 * "files" display, lists all files with statistics
30 * - can hide various lines including subdirectories
31 * and files without wiggles or conflicts
32 * "merge" display shows various views of merged file with different
33 * parts in different colours.
35 * The window can be split horizontally to show the original and result
36 * beside the diff, and each different branch can be shown alone.
49 static void term_init(int raw
);
51 static int intr_kills
= 0;
53 /* global attributes */
54 unsigned int a_delete
, a_added
, a_common
, a_sep
, a_void
,
55 a_unmatched
, a_extra
, a_already
;
56 unsigned int a_has_conflicts
, a_has_wiggles
, a_no_wiggles
, a_saved
;
58 /******************************************************************
60 * We display help in an insert, leaving 5 columns left and right,
61 * and 2 rows top and bottom, but at most 58x15 plus border
63 * SPC or RTN moves down or to next page
64 * BKSPC goes backwards
65 * 'q' returns to origin screen
66 * '?' show help on help
67 * left and right scroll help view
69 * A help text is an array of lines of text
73 " You are viewing the help page for the help viewer.",
74 "You normally get here by typing '?'",
76 "The following keystrokes work in the help viewer:",
77 " ? display this help message",
78 " q return to previous view",
79 " SPC move forward through help document",
81 " BKSP move backward through help document",
82 " RIGHT scroll help window so text on the right appears",
83 " LEFT scroll help window so text on the left appears",
87 char *help_missing
[] = {
88 "The file that this patch applies to appears",
90 "Please type 'q' to continue",
94 char *help_corrupt
[] = {
95 "This patch appears to be corrupt",
96 "Please type 'q' to continue",
100 /* We can give one or two pages to display in the help window.
101 * The first is specific to the current context. The second
102 * is optional and may provide help in a more broad context.
104 static int help_window(char *page1
[], char *page2
[], int query
)
114 getmaxyx(stdscr
, rows
, cols
);
120 left
= (cols
-58)/2 - 1;
128 top
= (rows
-15)/2 - 1;
132 /* Draw a border around the 'help' area */
133 (void)attrset(A_STANDOUT
);
134 for (c
= left
; c
< left
+cols
; c
++) {
135 mvaddch(top
-1, c
, '-');
136 mvaddch(top
+rows
, c
, '-');
138 for (r
= top
; r
< top
+ rows
; r
++) {
139 mvaddch(r
, left
-1, '|');
140 mvaddch(r
, left
+cols
, '|');
142 mvaddch(top
-1, left
-1, '/');
143 mvaddch(top
-1, left
+cols
, '\\');
144 mvaddch(top
+rows
, left
-1, '\\');
145 mvaddch(top
+rows
, left
+cols
, '/');
147 mvaddstr(top
-1, left
+ cols
/2 - 4, "Question");
148 mvaddstr(top
+rows
, left
+ cols
/2 - 9,
149 "Answer Y, N, or Q.");
151 mvaddstr(top
-1, left
+ cols
/2 - 9,
152 "HELP - 'q' to exit");
153 mvaddstr(top
+rows
, left
+cols
/2 - 17,
154 "Press SPACE for more, '?' for help");
156 (void)attrset(A_NORMAL
);
159 char **lnp
= page
+ line
;
161 /* Draw as much of the page at the current offset
164 for (r
= 0; r
< rows
; r
++) {
172 while (*ln
&& sh
> 0) {
176 for (c
= 0; c
< cols
; c
++) {
182 mvaddch(top
+r
, left
+c
, chr
);
185 move(top
+rows
-1, left
);
206 if (page1
!= help_help
)
207 help_window(help_help
, NULL
, 0);
210 case '\r': /* page-down */
211 for (r
= 0; r
< rows
-2; r
++)
214 if (!page
[line
] && !query
) {
225 case '\b': /* page up */
261 static char *typenames
[] = {
263 [Unmatched
] = "Unmatched",
264 [Unchanged
] = "Unchanged",
265 [Extraneous
] = "Extraneous",
266 [Changed
] = "Changed",
267 [Conflict
] = "Conflict",
268 [AlreadyApplied
] = "AlreadyApplied",
271 /* When we merge the original and the diff together we need
272 * to keep track of where everything came from.
273 * When we display the different views, we need to be able to
274 * select certain portions of the whole document.
275 * These flags are used to identify what is present, and to
276 * request different parts be extracted. They also help
277 * guide choice of colour.
283 #define CHANGES 16 /* A change is visible here,
284 * so 2 streams need to be shown */
285 #define WIGGLED 32 /* a conflict that was successfully resolved */
286 #define CONFLICTED 64 /* a conflict that was not successfully resolved */
288 /* Displaying a Merge.
289 * The first step is to linearise the merge. The merge in inherently
290 * parallel with before/after streams. However much of the whole document
291 * is linear as normally much of the original in unchanged.
292 * All parallelism comes from the patch. This normally produces two
293 * parallel stream, but in the case of a conflict can produce three.
294 * For browsing the merge we only ever show two alternates in-line.
295 * When there are three we use two panes with 1 or 2 alternates in each.
296 * So to linearise the two streams we find lines that are completely
297 * unchanged (same for all 3 streams, or missing in 2nd and 3rd) which bound
298 * a region where there are changes. We include everything between
299 * these twice, in two separate passes. The exact interpretation of the
300 * passes is handled at a higher level but will be one of:
301 * original and result
303 * original and after (for a conflict)
304 * This is all encoded in the 'struct merge'. An array of these describes
305 * the whole document.
307 * At any position in the merge we can be in one of 3 states:
308 * 0: unchanged section
312 * So to walk a merge in display order we need a position in the merge,
313 * a current state, and when in a changed section, we need to know the
314 * bounds of that changed section.
315 * This is all encoded in 'struct mpos'.
317 * Each location may or may not be visible depending on certain
320 * Also, some locations might be 'invalid' in that they don't need to be displayed.
321 * For example when the patch leaves a section of the original unchanged,
322 * we only need to see the original - the before/after sections are treated
323 * as invalid and are not displayed.
324 * The visibility of newlines is crucial and guides the display. One line
325 * of displayed text is all the visible sections between two visible newlines.
327 * Counting lines is a bit tricky. We only worry about line numbers in the
328 * original (stream 0) as these could compare with line numbers mentioned in
330 * We count 2 for every line: 1 for everything before the newline and 1 for the newline.
331 * That way we don't get a full counted line until we see the first char after the
332 * newline, so '+' lines are counted with the previous line.
336 int m
; /* merger index */
337 int s
; /* stream 0,1,2 for a,b,c */
338 int o
; /* offset in that stream */
339 int lineno
; /* Counts newlines in stream 0
340 * set lsb when see newline.
341 * add one when not newline and lsb set
345 struct mp p
, /* the current point (end of a line) */
346 lo
, /* eol for start of the current group */
347 hi
; /* eol for end of the current group */
349 * 0 if on an unchanged (lo/hi not meaningful)
350 * 1 if on the '-' of a diff,
351 * 2 if on the '+' of a diff
356 struct mp pos
; /* where in the document we are (an element) */
357 int offset
; /* which char in that element */
358 int target
; /* display column - or -1 if we are looking for 'pos' */
359 int col
; /* where we found pos or target */
360 int width
; /* Size of char, for moving to the right */
361 int alt
; /* Cursor is in alternate window */
364 /* used for checking location during search */
365 static int same_mp(struct mp a
, struct mp b
)
371 static int same_mpos(struct mpos a
, struct mpos b
)
373 return same_mp(a
.p
, b
.p
) &&
374 (a
.state
== b
.state
|| a
.state
== 0 || b
.state
== 0);
377 /* Check if a particular stream is meaningful in a particular merge
378 * section. e.g. in an Unchanged section, only stream 0, the
379 * original, is meaningful. This is used to avoid walking down
382 static int stream_valid(int s
, enum mergetype type
)
404 * Advance the 'pos' in the current mergepos returning the next
406 * This walks the merges in sequence, and the streams within
409 static struct elmnt
next_melmnt(struct mp
*pos
,
410 struct file fm
, struct file fb
, struct file fa
,
414 while (pos
->m
< 0 || m
[pos
->m
].type
!= End
) {
415 int l
= 0; /* Length remaining in current merge section */
429 /* Offset has reached length, choose new stream or
438 } while (!stream_valid(pos
->s
, m
[pos
->m
].oldtype
));
442 if (pos
->m
== -1 || m
[pos
->m
].type
== End
) {
444 e
.start
= NULL
; e
.hash
= 0; e
.len
= 0;
448 default: /* keep compiler happy */
452 if (ends_line(fm
.list
[m
[pos
->m
].a
+ pos
->o
]))
454 return fm
.list
[m
[pos
->m
].a
+ pos
->o
];
455 case 1: return fb
.list
[m
[pos
->m
].b
+ pos
->o
];
456 case 2: return fa
.list
[m
[pos
->m
].c
+ pos
->o
];
460 /* step current position.p backwards */
461 static struct elmnt
prev_melmnt(struct mp
*pos
,
462 struct file fm
, struct file fb
, struct file fa
,
466 if (m
[pos
->m
].a
+ pos
->o
< fm
.elcnt
&&
467 ends_line(fm
.list
[m
[pos
->m
].a
+ pos
->o
]))
474 while (pos
->m
>= 0 && pos
->o
< 0) {
481 } while (pos
->m
>= 0 &&
482 !stream_valid(pos
->s
, m
[pos
->m
].oldtype
));
486 pos
->o
= m
[pos
->m
].al
-1;
489 pos
->o
= m
[pos
->m
].bl
-1;
492 pos
->o
= m
[pos
->m
].cl
-1;
497 if (pos
->m
< 0 || m
[pos
->m
].type
== End
) {
499 e
.start
= NULL
; e
.hash
= 0; e
.len
= 0;
503 default: /* keep compiler happy */
504 case 0: return fm
.list
[m
[pos
->m
].a
+ pos
->o
];
505 case 1: return fb
.list
[m
[pos
->m
].b
+ pos
->o
];
506 case 2: return fa
.list
[m
[pos
->m
].c
+ pos
->o
];
510 /* 'visible' not only checks if this stream in this merge should be
511 * visible in this mode, but also chooses which colour/highlight to use
514 static int visible(int mode
, struct merge
*m
, struct mpos
*pos
)
517 int stream
= pos
->p
.s
;
523 else if (mode
& RESULT
)
524 type
= m
[pos
->p
.m
].type
;
526 type
= m
[pos
->p
.m
].oldtype
;
527 /* mode can be any combination of ORIG RESULT BEFORE AFTER */
529 case End
: /* The END is always visible */
531 case Unmatched
: /* Visible in ORIG and RESULT */
532 if (mode
& (ORIG
|RESULT
))
535 case Unchanged
: /* visible everywhere, but only show stream 0 */
539 case Extraneous
: /* stream 2 is visible in BEFORE and AFTER */
540 if ((mode
& (BEFORE
|AFTER
))
544 case Changed
: /* stream zero visible ORIG and BEFORE, stream 2 elsewhere */
546 (mode
& (ORIG
|BEFORE
)))
549 (mode
& (RESULT
|AFTER
)))
556 return a_unmatched
| A_REVERSE
;
560 return a_extra
| A_UNDERLINE
;
563 if (mode
& (AFTER
|RESULT
))
564 return a_added
| A_UNDERLINE
;
571 if (mode
& (ORIG
|RESULT
))
576 return a_delete
| A_UNDERLINE
;
580 return a_added
| A_UNDERLINE
;
588 /* checkline creates a summary of the sort of changes that
589 * are in a line, returning an "or" of
594 static int check_line(struct mpos pos
, struct file fm
, struct file fb
,
596 struct merge
*m
, int mode
)
605 int type
= m
[pos
.p
.m
].oldtype
;
607 type
= m
[pos
.p
.m
].type
;
610 else if (type
== Conflict
) {
611 rv
|= CONFLICTED
| CHANGES
;
612 } else if (type
== AlreadyApplied
) {
614 if (mode
& (BEFORE
|AFTER
))
616 } else if (type
== Extraneous
) {
617 if (fb
.list
[m
[pos
.p
.m
].b
].start
[0] == '\0')
618 /* hunk headers don't count as wiggles
619 * and nothing before a hunk header
620 * can possibly be part of this 'line' */
624 } else if (type
== Unmatched
)
627 if (m
[pos
.p
.m
].in_conflict
> 1)
628 rv
|= CONFLICTED
| CHANGES
;
629 if (m
[pos
.p
.m
].in_conflict
== 1 &&
630 (pos
.p
.o
< m
[pos
.p
.m
].lo
||
631 pos
.p
.o
> m
[pos
.p
.m
].hi
))
632 rv
|= CONFLICTED
| CHANGES
;
633 e
= prev_melmnt(&pos
.p
, fm
, fb
, fa
, m
);
634 } while (e
.start
!= NULL
&&
636 || visible(mode
, m
, &pos
) == -1));
638 if (unmatched
&& (rv
& CHANGES
))
643 /* Find the next line in the merge which is visible.
644 * If we hit the end of a conflicted set during pass-1
645 * we rewind for pass-2.
646 * 'mode' tells which bits we want to see, possible one of
647 * the 4 parts (before/after/orig/result) or one of the pairs
648 * before+after or orig+result.
650 static void next_mline(struct mpos
*pos
, struct file fm
, struct file fb
,
652 struct merge
*m
, int mode
)
661 struct elmnt e
= next_melmnt(&pos
->p
, fm
, fb
, fa
, m
);
665 visible(mode
, m
, pos
) >= 0)
668 mode2
= check_line(*pos
, fm
, fb
, fa
, m
, mode
);
670 if ((mode2
& CHANGES
) && pos
->state
== 0) {
671 /* Just entered a diff-set */
674 } else if (!(mode2
& CHANGES
) && pos
->state
) {
675 /* Come to the end of a diff-set */
676 switch (pos
->state
) {
678 /* Need to record the end */
680 /* time for another pass */
685 /* finished final pass */
690 mask
= ORIG
|RESULT
|BEFORE
|AFTER
;
691 switch (pos
->state
) {
693 mask
&= ~(RESULT
|AFTER
);
696 mask
&= ~(ORIG
|BEFORE
);
699 } while (visible(mode
&mask
, m
, pos
) < 0);
703 /* Move to previous line - simply the reverse of next_mline */
704 static void prev_mline(struct mpos
*pos
, struct file fm
, struct file fb
,
706 struct merge
*m
, int mode
)
717 struct elmnt e
= prev_melmnt(&pos
->p
, fm
, fb
, fa
, m
);
721 visible(mode
, m
, pos
) >= 0)
724 mode2
= check_line(*pos
, fm
, fb
, fa
, m
, mode
);
726 if ((mode2
& CHANGES
) && pos
->state
== 0) {
727 /* Just entered a diff-set */
730 } else if (!(mode2
& CHANGES
) && pos
->state
) {
731 /* Come to the end (start) of a diff-set */
732 switch (pos
->state
) {
734 /* finished final pass */
738 /* Need to record the start */
740 /* time for another pass */
746 mask
= ORIG
|RESULT
|BEFORE
|AFTER
;
747 switch (pos
->state
) {
749 mask
&= ~(RESULT
|AFTER
);
752 mask
&= ~(ORIG
|BEFORE
);
755 } while (visible(mode
&mask
, m
, pos
) < 0);
758 /* blank a whole row of display */
759 static void blank(int row
, int start
, int cols
, unsigned int attr
)
767 /* search of a string on one display line. If found, update the
771 static int mcontains(struct mpos pos
,
772 struct file fm
, struct file fb
, struct file fa
,
774 int mode
, char *search
, struct cursor
*curs
,
775 int dir
, int ignore_case
)
777 /* See if any of the files, between start of this line and here,
778 * contain the search string.
779 * However this is modified by dir:
780 * -2: find last match *before* curs
781 * -1: find last match at-or-before curs
782 * 1: find first match at-or-after curs
783 * 2: find first match *after* curs
785 * We only test for equality with curs, so if it is on a different
786 * line it will not be found and everything is before/after.
787 * As we search from end-of-line to start we find the last
789 * For a forward search, we stop when we find curs.
790 * For a backward search, we forget anything found when we find curs.
796 int len
= strlen(search
);
799 e
= prev_melmnt(&pos
.p
, fm
, fb
, fa
, m
);
800 if (e
.start
&& e
.start
[0]) {
803 if (same_mp(pos
.p
, curs
->pos
))
804 curs_i
= curs
->offset
;
807 for (i
= e
.len
-1; i
>= 0; i
--) {
808 if (i
== curs_i
&& dir
== -1)
809 /* next match is the one we want */
811 if (i
== curs_i
&& dir
== 2)
812 /* future matches not accepted */
814 if ((!found
|| dir
> 0) &&
815 (ignore_case
? strncasecmp
: strncmp
)
816 (e
.start
+i
, search
, len
) == 0) {
821 if (i
== curs_i
&& dir
== -2)
822 /* next match is the one we want */
824 if (i
== curs_i
&& dir
== 1)
825 /* future matches not accepted */
829 } while (e
.start
!= NULL
&&
831 || visible(mode
, m
, &pos
) == -1));
840 /* Drawing the display window.
841 * There are 7 different ways we can display the data, each
842 * of which can be configured by a keystroke:
843 * o original - just show the original file with no changes, but still
844 * with highlights of what is changed or unmatched
845 * r result - show just the result of the merge. Conflicts just show
846 * the original, not the before/after options
847 * b before - show the 'before' stream of the patch
848 * a after - show the 'after' stream of the patch
849 * d diff - show just the patch, both before and after
850 * m merge - show the full merge with -+ sections for changes.
851 * If point is in a wiggled or conflicted section the
852 * window is split horizontally and the diff is shown
853 * in the bottom window
854 * | sidebyside - two panes, left and right. Left holds the merge,
855 * right holds the diff. In the case of a conflict,
856 * left holds orig/after, right holds before/after
858 * The horizontal split for 'merge' mode is managed as follows.
859 * - The window is split when we first visit a line that contains
860 * a wiggle or a conflict, and the second pane is removed when
861 * we next visit a line that contains no changes (is fully Unchanged).
862 * - to display the second pane, we find a visible end-of-line in the
863 * (BEFORE|AFTER) mode at-or-before the current end-of-line and
864 * the we centre that line.
865 * - We need to rewind to an unchanged section, and wind forward again
866 * to make sure that 'lo' and 'hi' are set properly.
867 * - every time we move, we redraw the second pane (see how that goes).
870 /* draw_mside draws one text line or, in the case of sidebyside, one side
872 * The 'mode' tells us what to draw via the 'visible()' function.
873 * It is one of ORIG RESULT BEFORE AFTER or ORIG|RESULT or BEFORE|AFTER
874 * It may also have WIGGLED or CONFLICTED ored in to trigger extra highlights.
875 * The desired cursor position is given in 'target' the actual end
876 * cursor position (allowing e.g. for tabs) is returned in *colp.
878 static void draw_mside(int mode
, int row
, int offset
, int start
, int cols
,
879 struct file fm
, struct file fb
, struct file fa
,
887 unsigned int tag_attr
;
891 default: /* keep compiler happy */
892 case 0: /* unchanged line */
896 case 1: /* 'before' text */
899 if ((mode
& ORIG
) && (mode
& CONFLICTED
)) {
901 tag_attr
= a_delete
| A_REVERSE
;
903 mode
&= (ORIG
|BEFORE
);
905 case 2: /* the 'after' part */
908 mode
&= (AFTER
|RESULT
);
912 if (visible(mode
, m
, &pos
) < 0) {
913 /* Not visible, just draw a blank */
914 blank(row
, offset
, cols
, a_void
);
924 (void)attrset(tag_attr
);
925 mvaddch(row
, offset
, tag
);
928 (void)attrset(A_NORMAL
);
930 if (check_line(pos
, fm
, fb
, fa
, m
, mode
))
933 /* find previous visible newline, or start of file */
935 e
= prev_melmnt(&pos
.p
, fm
, fb
, fa
, m
);
936 while (e
.start
!= NULL
&&
938 visible(mode
, m
, &pos
) == -1));
945 e
= next_melmnt(&pos
.p
, fm
, fb
, fa
, m
);
949 if (visible(mode
, m
, &pos
) == -1)
953 c
= (unsigned char *)e
.start
- e
.prefix
;
955 attr
= visible(mode
, m
, &pos
);
956 if ((attr
== a_unmatched
|| attr
== a_extra
) &&
958 /* Only highlight spaces if there is a tab nearby */
959 for (l
= 0; l
< e
.plen
+ e
.prefix
; l
++)
962 if (!highlight_space
&& (c
[0] == ' ' || c
[0] == '\t')) {
963 /* always highlight space/tab at end-of-line */
964 struct mp nxt
= pos
.p
;
965 struct elmnt nxte
= next_melmnt(&nxt
, fm
, fb
, fa
, m
);
966 if (nxte
.start
[0] == '\n')
969 for (l
= 0; l
< e
.plen
+ e
.prefix
; l
++) {
974 if (*c
>= ' ' && *c
!= 0x7f) {
976 (void)attrset(attr
|A_REVERSE
);
977 if (col
>= start
&& col
< start
+cols
)
978 mvaddch(row
, col
-start
+offset
, *c
);
980 } else if (*c
== '\t') {
982 (void)attrset(attr
|A_UNDERLINE
);
984 if (col
>= start
&& col
< start
+cols
) {
985 mvaddch(row
, col
-start
+offset
, ' ');
987 } while ((col
&7) != 0);
989 if (col
>= start
&& col
< start
+cols
)
990 mvaddch(row
, col
-start
+offset
, '?');
994 if (curs
->target
>= 0) {
995 if (curs
->target
< col
) {
996 /* Found target column */
1000 if (scol
>= start
+ cols
)
1001 /* Didn't appear on screen */
1004 curs
->width
= col
- scol
;
1007 } else if (l
== curs
->offset
&&
1008 same_mp(pos
.p
, curs
->pos
)) {
1010 curs
->target
= scol
;
1012 if (scol
>= start
+ cols
)
1013 /* Didn't appear on screen */
1016 curs
->width
= col
- scol
;
1023 && visible(mode
, m
, &pos
) != -1))
1027 /* We have reached the end of visible line, or end of file */
1030 if (col
>= start
+ cols
)
1033 curs
->width
= -1; /* end of line */
1034 if (curs
->target
>= 0) {
1037 } else if (same_mp(pos
.p
, curs
->pos
))
1042 if (e
.start
&& e
.start
[0] == 0) {
1045 if (pos
.p
.s
== 2 && m
[pos
.p
.m
].type
== Extraneous
) {
1046 int A
, B
, C
, D
, E
, F
;
1047 e1
= fb
.list
[m
[pos
.p
.m
].b
+ pos
.p
.o
];
1048 sscanf(e1
.start
+1, "%d %d %d", &A
, &B
, &C
);
1049 sscanf(e
.start
+1, "%d %d %d", &D
, &E
, &F
);
1050 snprintf(b
, sizeof(b
), "@@ -%d,%d +%d,%d @@%s", B
, C
, E
, F
, e1
.start
+18);
1051 (void)attrset(a_sep
);
1053 (void)attrset(visible(mode
, m
, &pos
));
1054 sprintf(b
, "<%.17s>", e
.start
+1);
1056 mvaddstr(row
, col
-start
+offset
, b
);
1059 blank(row
, col
-start
+offset
, start
+cols
-col
,
1061 ? (unsigned)visible(mode
, m
, &pos
)
1065 /* Draw either 1 or 2 sides depending on the mode. */
1067 static void draw_mline(int mode
, int row
, int start
, int cols
,
1068 struct file fm
, struct file fb
, struct file fa
,
1071 struct cursor
*curs
)
1074 * Draw the left and right images of this line
1075 * One side might be a_blank depending on the
1076 * visibility of this newline
1080 mode
|= check_line(pos
, fm
, fb
, fa
, m
, mode
);
1082 if ((mode
& (BEFORE
|AFTER
)) &&
1083 (mode
& (ORIG
|RESULT
))) {
1086 rcols
= cols
- lcols
- 1;
1088 (void)attrset(A_STANDOUT
);
1089 mvaddch(row
, lcols
, '|');
1091 draw_mside(mode
&~(BEFORE
|AFTER
), row
, 0, start
, lcols
,
1092 fm
, fb
, fa
, m
, pos
, curs
&& !curs
->alt
? curs
: NULL
);
1094 draw_mside(mode
&~(ORIG
|RESULT
), row
, lcols
+1, start
, rcols
,
1095 fm
, fb
, fa
, m
, pos
, curs
&& curs
->alt
? curs
: NULL
);
1097 draw_mside(mode
, row
, 0, start
, cols
,
1098 fm
, fb
, fa
, m
, pos
, curs
);
1101 static char *merge_help
[] = {
1102 "This view shows the merge of the patch with the",
1103 "original file. It is like a full-context diff showing",
1104 "removed lines with a '-' prefix and added lines with a",
1106 "In cases where a patch chunk could not be successfully",
1107 "applied, the original text is prefixed with a '|', and",
1108 "the text that the patch wanted to add is prefixed with",
1110 "When the cursor is over such a conflict, or over a chunk",
1111 "which required wiggling to apply (i.e. there was unmatched",
1112 "text in the original, or extraneous unchanged text in",
1113 "the patch), the terminal is split and the bottom pane is",
1114 "use to display the part of the patch that applied to",
1115 "this section of the original. This allows you to confirm",
1116 "that a wiggled patch applied correctly, and to see",
1117 "why there was a conflict",
1120 static char *diff_help
[] = {
1121 "This is the 'diff' or 'patch' view. It shows",
1122 "only the patch that is being applied without the",
1123 "original to which it is being applied.",
1124 "Underlined text indicates parts of the patch which",
1125 "resulted in a conflict when applied to the",
1129 static char *orig_help
[] = {
1130 "This is the 'original' view which simply shows",
1131 "the original file before applying the patch.",
1132 "Sections of code that would be changed by the patch",
1133 "are highlighted in red.",
1136 static char *result_help
[] = {
1137 "This is the 'result' view which shows just the",
1138 "result of applying the patch. When a conflict",
1139 "occurred this view does not show the full conflict",
1140 "but only the 'after' part of the patch. To see",
1141 "the full conflict, use the 'merge' or 'sidebyside'",
1145 static char *before_help
[] = {
1146 "This view shows the 'before' section of a patch.",
1147 "It allows the expected match text to be seen uncluttered",
1148 "by text that is meant to replaced it.",
1149 "Red text is text that will be removed by the patch",
1152 static char *after_help
[] = {
1153 "This view shows the 'after' section of a patch.",
1154 "It allows the intended result to be seen uncluttered",
1155 "by text that was meant to be matched and replaced.",
1156 "Green text is text that was added by the patch - it",
1157 "was not present in the 'before' part of the patch",
1160 static char *sidebyside_help
[] = {
1161 "This is the Side By Side view of a patched file.",
1162 "The left side shows the original and the result.",
1163 "The right side shows the patch which was applied",
1164 "and lines up with the original/result as much as",
1167 "Where one side has no line which matches the",
1168 "other side it is displayed as a solid colour in the",
1169 "yellow family (depending on your terminal window).",
1172 static char *merge_window_help
[] = {
1173 " Highlight Colours and Keystroke commands",
1175 "In all different views of a merge, highlight colours",
1176 "are used to show which parts of lines were added,",
1177 "removed, already changed, unchanged or in conflict.",
1178 "Colours and their use are:",
1179 " normal unchanged text",
1180 " red text that was removed or changed",
1181 " green text that was added or the result",
1183 " yellow background used in side-by-side for a line",
1184 " which has no match on the other",
1186 " blue text in the original which did not",
1187 " match anything in the patch",
1188 " cyan text in the patch which did not",
1189 " match anything in the original",
1190 " cyan background already changed text: the result",
1191 " of the patch matches the original",
1192 " underline remove or added text can also be",
1193 " underlined indicating that it",
1194 " was involved in a conflict",
1196 "While viewing a merge various keystroke commands can",
1197 "be used to move around and change the view. Basic",
1198 "movement commands from both 'vi' and 'emacs' are",
1201 " p control-p k UP Move to previous line",
1202 " n control-n j DOWN Move to next line",
1203 " l LEFT Move one char to right",
1204 " h RIGHT Move one char to left",
1205 " / control-s Enter incremental search mode",
1206 " control-r Enter reverse-search mode",
1207 " control-g Search again",
1208 " ? Display help message",
1209 " ESC-< 0-G Go to start of file",
1210 " ESC-> G Go to end of file",
1211 " q Return to list of files or exit",
1212 " S Arrange for merge to be saved on exit",
1213 " control-C Disable auto-save-on-exit",
1214 " control-L recenter current line",
1215 " control-V SPACE page down",
1216 " ESC-v BACKSPC page up",
1217 " N go to next patch chunk",
1218 " P go to previous patch chunk",
1219 " C go to next conflicted chunk",
1220 " C-X-o O move cursor to alternate pane",
1221 " ^ control-A go to start of line",
1222 " $ control-E go to end of line",
1224 " a display 'after' view",
1225 " b display 'before' view",
1226 " o display 'original' view",
1227 " r display 'result' view",
1228 " d display 'diff' or 'patch' view",
1229 " m display 'merge' view",
1230 " | display side-by-side view",
1232 " I toggle whether spaces are ignored",
1233 " when matching text.",
1234 " x toggle ignoring of current Changed,",
1235 " Conflict, or Unmatched item",
1236 " c toggle accepting of result of conflict",
1237 " X toggle ignored of all Change, Conflict",
1238 " and Unmatched items in current line",
1239 " v Save the current merge and run the",
1240 " default editor on the file.",
1243 static char *save_query
[] = {
1245 "You have modified the merge.",
1246 "Would you like to save it?",
1247 " Y = save the modified merge",
1248 " N = discard modifications, don't save",
1249 " Q = return to viewing modified merge",
1253 static char *toggle_ignore
[] = {
1255 "You have modified the merge.",
1256 "Toggling ignoring of spaces will discard changes.",
1257 "Do you want to proceed?",
1258 " Y = discard changes and toggle ignoring of spaces",
1259 " N = keep changes, don't toggle",
1263 static void do_edit(char *file
)
1265 char *ed
= getenv("VISUAL");
1267 ed
= getenv("EDITOR");
1269 ed
= "/usr/bin/edit";
1272 execlp(ed
, ed
, file
, NULL
);
1282 static int merge_window(struct plist
*p
, FILE *f
, int reverse
, int replace
,
1283 int selftest
, int ignore_blanks
)
1285 /* Display the merge window in one of the selectable modes,
1286 * starting with the 'merge' mode.
1288 * Newlines are the key to display.
1289 * 'pos' is always a visible newline (or eof).
1290 * In sidebyside mode it might only be visible on one side,
1291 * in which case the other side will be blank.
1292 * Where the newline is visible, we rewind the previous visible
1293 * newline visible and display the stuff in between
1295 * A 'position' is a struct mpos
1298 struct stream sm
, sb
, sa
, sp
; /* main, before, after, patch */
1299 struct file fm
, fb
, fa
;
1300 struct csl
*csl1
, *csl2
;
1302 int ch
; /* count of chunks */
1303 /* Always refresh the current line.
1304 * If refresh == 1, refresh all lines. If == 2, clear first
1307 int rows
= 0, cols
= 0;
1308 int splitrow
= -1; /* screen row for split - diff appears below */
1309 int lastrow
= 0; /* end of screen, or just above 'splitrow' */
1312 int mode
= ORIG
|RESULT
;
1313 int mmode
= mode
; /* Mode for moving - used when in 'other' pane */
1314 char *modename
= "merge";
1315 char **modehelp
= merge_help
;
1319 int trow
; /* screen-row while searching. If we cannot find,
1320 * we forget this number */
1322 struct mpos pos
; /* current point */
1323 struct mpos tpos
, /* temp point while drawing lines above and below pos */
1324 toppos
, /* pos at top of screen - for page-up */
1325 botpos
; /* pos at bottom of screen - for page-down */
1326 struct mpos vpos
, tvpos
, vispos
;
1328 int meta
= 0, /* mode for multi-key commands- SEARCH or META */
1330 int num
= -1, /* numeric arg being typed. */
1332 int changes
= 0; /* If any edits have been made to the merge */
1333 int answer
; /* answer to 'save changes?' question */
1336 char search
[80]; /* string we are searching for */
1337 unsigned int searchlen
= 0;
1338 int search_notfound
= 0;
1342 * 1 == no because there are upper-case chars
1343 * 2 == yes as there are no upper-case chars
1346 int ignore_case
= 2;
1347 /* We record all the places we find so 'backspace'
1348 * can easily return to the previous one
1350 struct search_anchor
{
1351 struct search_anchor
*next
;
1356 unsigned int searchlen
;
1359 void free_stuff(void)
1368 void find_line(int ln
)
1370 pos
.p
.m
= 0; /* merge node */
1371 pos
.p
.s
= 0; /* stream number */
1372 pos
.p
.o
= -1; /* offset */
1375 memset(&curs
, 0, sizeof(curs
));
1377 next_mline(&pos
, fm
, fb
, fa
, ci
.merger
, mode
);
1378 while (pos
.p
.lineno
< ln
&& ci
.merger
[pos
.p
.m
].type
!= End
);
1381 void prepare_merge(int ch
)
1383 /* FIXME check for errors in the stream */
1384 fm
= split_stream(sm
, ByWord
| ignore_blanks
);
1385 fb
= split_stream(sb
, ByWord
| ignore_blanks
);
1386 fa
= split_stream(sa
, ByWord
| ignore_blanks
);
1389 csl1
= pdiff(fm
, fb
, ch
);
1391 csl1
= diff(fm
, fb
);
1392 csl2
= diff_patch(fb
, fa
);
1394 ci
= make_merger(fm
, fb
, fa
, csl1
, csl2
, 0, 1, 0);
1395 for (i
= 0; ci
.merger
[i
].type
!= End
; i
++)
1396 ci
.merger
[i
].oldtype
= ci
.merger
[i
].type
;
1406 /* three separate files */
1407 sm
= load_file(p
->file
);
1408 sb
= load_file(p
->before
);
1409 sa
= load_file(p
->after
);
1411 /* One merge file */
1412 sp
= load_file(p
->file
);
1414 split_merge(sp
, &sm
, &sa
, &sb
);
1416 split_merge(sp
, &sm
, &sb
, &sa
);
1421 sp
= load_segment(f
, p
->start
, p
->end
);
1424 split_merge(sp
, &sm
, &sa
, &sb
);
1426 split_merge(sp
, &sm
, &sb
, &sa
);
1430 ch
= split_patch(sp
, &sa
, &sb
);
1432 ch
= split_patch(sp
, &sb
, &sa
);
1434 sm
= load_file(p
->file
);
1438 if (!sm
.body
|| !sb
.body
|| !sa
.body
) {
1444 help_window(help_missing
, NULL
, 0);
1446 help_window(help_corrupt
, NULL
, 0);
1451 term_init(!selftest
);
1462 if (row
< 1 || row
>= lastrow
)
1467 if (mode
== (ORIG
|RESULT
)) {
1468 int cmode
= check_line(pos
, fm
, fb
, fa
, ci
.merger
, mode
);
1469 if (cmode
& (WIGGLED
| CONFLICTED
)) {
1471 splitrow
= (rows
+1)/2;
1472 lastrow
= splitrow
- 1;
1475 } else if (!curs
.alt
&& splitrow
>= 0) {
1480 } else if (splitrow
>= 0) {
1487 getmaxyx(stdscr
, rows
, cols
);
1488 rows
--; /* keep last row clear */
1489 if (splitrow
>= 0) {
1490 splitrow
= (rows
+1)/2;
1491 lastrow
= splitrow
- 1;
1499 if (row
> lastrow
+3)
1504 if (getenv("WIGGLE_VTRACE")) {
1509 default: /* keep compiler happy */
1511 e
= fm
.list
[ci
.merger
[vpos
.p
.m
].a
+ vpos
.p
.o
].start
;
1514 e
= fb
.list
[ci
.merger
[vpos
.p
.m
].b
+ vpos
.p
.o
].start
;
1517 e
= fa
.list
[ci
.merger
[vpos
.p
.m
].c
+ vpos
.p
.o
].start
;
1520 for (i
= 0; i
< 6; i
++) {
1522 if (e2
[i
] < 32 || e2
[i
] >= 127)
1525 sprintf(b
, "st=%d str=%d o=%d m=%d mt=%s(%d,%d,%d) ic=%d <%.3s>", vpos
.state
,
1527 vpos
.p
.m
, typenames
[ci
.merger
[vpos
.p
.m
].type
],
1528 ci
.merger
[vpos
.p
.m
].al
,
1529 ci
.merger
[vpos
.p
.m
].bl
,
1530 ci
.merger
[vpos
.p
.m
].cl
,
1531 ci
.merger
[vpos
.p
.m
].in_conflict
,
1534 (void)attrset(A_NORMAL
);
1539 /* Always refresh the line */
1540 while (start
> curs
.target
) {
1546 vispos
= pos
; /* visible position - if cursor is in
1547 * alternate pane, pos might not be visible
1549 if (check_line(vispos
, fm
, fb
, fa
, ci
.merger
, mode
)
1551 if (vispos
.state
== 0)
1557 if (visible(mode
, ci
.merger
, &vispos
) < 0)
1558 prev_mline(&vispos
, fm
, fb
, fa
, ci
.merger
, mode
);
1562 draw_mline(mode
, row
, start
, cols
, fm
, fb
, fa
, ci
.merger
,
1563 vispos
, (splitrow
>= 0 && curs
.alt
) ? NULL
: &curs
);
1564 if (curs
.width
== 0 && start
< curs
.col
) {
1565 /* width == 0 implies it appear after end-of-screen */
1570 if (curs
.col
< start
) {
1582 for (i
= row
-1; i
>= 1 && tpos
.p
.m
>= 0; ) {
1583 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mode
);
1584 draw_mline(mode
, i
--, start
, cols
,
1585 fm
, fb
, fa
, ci
.merger
,
1596 blank(i
--, 0, cols
, a_void
);
1598 for (i
= row
; i
<= lastrow
&& ci
.merger
[tpos
.p
.m
].type
!= End
; ) {
1599 draw_mline(mode
, i
++, start
, cols
,
1600 fm
, fb
, fa
, ci
.merger
,
1602 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mode
);
1604 botpos
= tpos
; botrow
= i
;
1605 while (i
<= lastrow
)
1606 blank(i
++, 0, cols
, a_void
);
1609 if (splitrow
>= 0) {
1610 struct mpos spos
= pos
;
1611 int smode
= BEFORE
|AFTER
;
1612 int srow
= (rows
+ splitrow
)/2;
1613 if (check_line(spos
, fm
, fb
, fa
, ci
.merger
, smode
)
1615 if (spos
.state
== 0)
1620 if (visible(smode
, ci
.merger
, &spos
) < 0)
1621 prev_mline(&spos
, fm
, fb
, fa
, ci
.merger
, smode
);
1622 /* Now hi/lo might be wrong, so lets fix it. */
1625 /* 'hi' might be wrong so we mustn't depend
1626 * on it while walking back. So set state
1627 * to 1 to avoid ever testing it.
1630 while (spos
.p
.m
>= 0 && spos
.state
!= 0)
1631 prev_mline(&spos
, fm
, fb
, fa
, ci
.merger
, smode
);
1632 while (!same_mpos(spos
, tpos
))
1633 next_mline(&spos
, fm
, fb
, fa
, ci
.merger
, smode
);
1635 (void)attrset(a_sep
);
1636 for (i
= 0; i
< cols
; i
++)
1637 mvaddstr(splitrow
, i
, "-");
1640 for (i
= srow
-1; i
> splitrow
; i
--) {
1641 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, smode
);
1642 draw_mline(smode
, i
, start
, cols
, fm
, fb
, fa
, ci
.merger
,
1645 while (i
> splitrow
)
1646 blank(i
--, 0, cols
, a_void
);
1649 i
< rows
&& ci
.merger
[tpos
.p
.m
].type
!= End
;
1651 draw_mline(smode
, i
, start
, cols
, fm
, fb
, fa
, ci
.merger
,
1653 (i
== srow
&& curs
.alt
) ? &curs
: NULL
);
1654 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, smode
);
1657 blank(i
++, 0, cols
, a_void
);
1659 /* Now that curs is accurate, report the type */
1663 snprintf(buf
, 100, "File: %s%s Mode: %s",
1664 p
->file
, reverse
? " - reversed" : "", modename
);
1665 (void)attrset(A_BOLD
);
1666 mvaddstr(0, 0, buf
);
1667 (void)attrset(A_NORMAL
);
1669 addstr(" (ignoring blanks)");
1671 (void)attrset(A_BOLD
);
1672 if (ci
.merger
[curs
.pos
.m
].type
!= ci
.merger
[curs
.pos
.m
].oldtype
)
1673 l
= snprintf(buf
, sizeof(buf
), "%s->", typenames
[ci
.merger
[curs
.pos
.m
].oldtype
]);
1674 snprintf(buf
+l
, sizeof(buf
)-l
, "%s ln:%d",
1675 typenames
[ci
.merger
[curs
.pos
.m
].type
],
1676 (pos
.p
.lineno
-1)/2);
1677 mvaddstr(0, cols
- strlen(buf
) - 1, buf
);
1679 #define META(c) ((c)|0x1000)
1680 #define SEARCH(c) ((c)|0x2000)
1681 #define CTRLX(c) ((c)|0x4000)
1683 (void)attrset(A_NORMAL
);
1692 snprintf(buf
, 10, "%d ", num
);
1697 if (meta
& CTRLX(0))
1699 if (meta
& SEARCH(0)) {
1701 addstr("Backwards ");
1704 if (search_notfound
)
1705 addstr(" - Not Found.");
1706 search_notfound
= 0;
1709 /* '+1' to skip over the leading +/-/| char */
1710 if (curs
.alt
&& splitrow
> 0)
1711 move((rows
+ splitrow
)/2, curs
.col
- start
+ 1);
1712 else if (curs
.alt
&& ((mode
& (BEFORE
|AFTER
)) &&
1713 (mode
& (ORIG
|RESULT
))))
1714 move(row
, curs
.col
-start
+ (cols
-1)/2+2);
1716 move(row
, curs
.col
-start
+1);
1725 tmeta
= meta
; meta
= 0;
1726 tnum
= num
; num
= -1;
1727 tvpos
= vpos
; vpos
= pos
;
1728 cswitch
= c
| tmeta
;
1729 /* Handle some ranges */
1730 /* case '0' ... '9': */
1731 if (cswitch
>= '0' && cswitch
<= '9')
1733 /* case SEARCH(' ') ... SEARCH('~'): */
1734 if (cswitch
>= SEARCH(' ') && cswitch
<= SEARCH('~'))
1735 cswitch
= SEARCH(' ');
1738 case 27: /* escape */
1748 case META('<'): /* start of file */
1753 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
1754 } while (tpos
.p
.m
>= 0);
1758 case META('>'): /* end of file */
1765 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
1766 } while (ci
.merger
[tpos
.p
.m
].type
!= End
);
1770 case '0': /* actually '0'...'9' */
1773 num
= tnum
*10 + (c
-'0');
1777 mesg
= "Autosave disabled";
1779 mesg
= "Use 'q' to quit";
1783 mesg
= "Will auto-save on exit, using Ctrl-C to cancel";
1792 answer
= help_window(save_query
, NULL
, 1);
1797 p
->conflicts
= isolate_conflicts(
1798 fm
, fb
, fa
, csl1
, csl2
, 0,
1799 ci
.merger
, 0, &p
->wiggles
);
1800 p
->chunks
= p
->conflicts
;
1801 save_merge(fm
, fb
, fa
, ci
.merger
,
1802 p
->file
, !p
->is_merge
);
1811 case 'I': /* Toggle ignoring of spaces */
1814 answer
= help_window(toggle_ignore
, NULL
, 1);
1820 ignore_blanks
= ignore_blanks
? 0 : IgnoreBlanks
;
1822 find_line(pos
.p
.lineno
);
1828 save_merge(fm
, fb
, fa
, ci
.merger
,
1829 p
->file
, !p
->is_merge
);
1834 sp
= load_file(p
->file
);
1835 split_merge(sp
, &sm
, &sb
, &sa
);
1839 p
->conflicts
= isolate_conflicts(
1840 fm
, fb
, fa
, csl1
, csl2
, 0,
1841 ci
.merger
, 0, &p
->wiggles
);
1842 p
->chunks
= p
->conflicts
;
1846 find_line(pos
.p
.lineno
);
1853 /* incr search forward */
1856 search
[searchlen
] = 0;
1861 /* incr search backwards */
1864 search
[searchlen
] = 0;
1867 case SEARCH('G'-64):
1868 case SEARCH('S'-64):
1869 case SEARCH('R'-64):
1871 if ((c
|tmeta
) == SEARCH('R'-64))
1876 tpos
= pos
; trow
= row
;
1879 case SEARCH('H'-64):
1880 case SEARCH(KEY_BACKSPACE
):
1883 struct search_anchor
*a
;
1889 struct search_anchor
*a
;
1897 search_notfound
= a
->notfound
;
1898 searchlen
= a
->searchlen
;
1899 search
[searchlen
] = 0;
1904 case SEARCH(' '): /* actually ' '...'~' */
1907 if (searchlen
< sizeof(search
)-1)
1908 search
[searchlen
++] = c
& (0x7f);
1909 search
[searchlen
] = 0;
1910 tpos
= pos
; trow
= row
;
1912 search_notfound
= 1;
1913 if (ignore_case
== 1 || ignore_case
== 2) {
1916 for (i
=0; i
< searchlen
; i
++)
1917 if (isupper(search
[i
])) {
1923 if (mcontains(tpos
, fm
, fb
, fa
, ci
.merger
,
1924 mmode
, search
, &curs
, searchdir
,
1925 ignore_case
>= 2)) {
1929 search_notfound
= 0;
1932 if (searchdir
< 0) {
1934 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
1937 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
1939 } while (tpos
.p
.m
>= 0 && ci
.merger
[tpos
.p
.m
].type
!= End
);
1940 searchdir
/= abs(searchdir
);
1949 case 'V'-64: /* page down */
1951 if (botrow
<= lastrow
) {
1960 case META('v'): /* page up */
1967 if (getmouse(&mevent
) != OK
)
1969 /* First see if this is on the 'other' pane */
1971 /* merge mode, top and bottom */
1972 if ((curs
.alt
&& mevent
.y
< splitrow
) ||
1973 (!curs
.alt
&& mevent
.y
> splitrow
)) {
1976 } else if (mode
== (ORIG
|RESULT
|BEFORE
|AFTER
)) {
1977 /* side-by-side mode */
1978 if ((curs
.alt
&& mevent
.x
< cols
/2) ||
1979 (!curs
.alt
&& mevent
.x
> cols
/2)) {
1983 /* Now try to find the right line */
1984 if (splitrow
< 0 || !curs
.alt
)
1987 trow
= (rows
+ splitrow
)/2;
1988 while (trow
> mevent
.y
) {
1990 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
1991 if (tpos
.p
.m
>= 0) {
1997 while (trow
< mevent
.y
) {
1999 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2000 if (ci
.merger
[tpos
.p
.m
].type
!= End
) {
2006 if (splitrow
< 0 || !curs
.alt
)
2007 /* it is OK to change the row */
2010 /* Now set the target column */
2011 if (mode
== (ORIG
|RESULT
|BEFORE
|AFTER
) &&
2013 curs
.target
= start
+ mevent
.x
- cols
/ 2 - 1;
2015 curs
.target
= start
+ mevent
.x
- 1;
2023 for (; tnum
> 0 ; tnum
--) {
2025 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2026 if (ci
.merger
[tpos
.p
.m
].type
!= End
) {
2041 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2042 } while (!(pos
.state
== 0
2043 && (check_line(pos
, fm
, fb
, fa
, ci
.merger
, mmode
)
2044 & (CONFLICTED
|WIGGLED
)) == 0)
2045 && ci
.merger
[tpos
.p
.m
].type
!= End
);
2049 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2050 } while (pos
.state
== 0
2051 && (check_line(pos
, fm
, fb
, fa
, ci
.merger
, mmode
)
2052 & (CONFLICTED
|WIGGLED
)) == 0
2053 && ci
.merger
[tpos
.p
.m
].type
!= End
);
2061 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2062 } while (!(check_line(pos
, fm
, fb
, fa
, ci
.merger
, mmode
)
2064 && ci
.merger
[tpos
.p
.m
].type
!= End
);
2068 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2069 } while ((check_line(pos
, fm
, fb
, fa
, ci
.merger
, mmode
)
2071 && ci
.merger
[tpos
.p
.m
].type
!= End
);
2080 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2081 } while (tpos
.state
== 0
2082 && (check_line(tpos
, fm
, fb
, fa
, ci
.merger
, mmode
)
2083 & (CONFLICTED
|WIGGLED
)) == 0
2088 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2089 } while (!(tpos
.state
== 0
2090 && (check_line(tpos
, fm
, fb
, fa
, ci
.merger
, mmode
)
2091 & (CONFLICTED
|WIGGLED
)) == 0)
2101 for (; tnum
> 0 ; tnum
--) {
2103 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2104 if (tpos
.p
.m
>= 0) {
2115 curs
.target
= curs
.col
- 1;
2116 if (curs
.target
< 0) {
2117 /* Try to go to end of previous line */
2119 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2120 if (tpos
.p
.m
>= 0) {
2132 if (curs
.width
>= 0)
2133 curs
.target
= curs
.col
+ curs
.width
;
2135 /* end of line, go to next */
2137 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2138 if (ci
.merger
[tpos
.p
.m
].type
!= End
) {
2161 curs
.alt
= !curs
.alt
;
2162 if (curs
.alt
&& mode
== (ORIG
|RESULT
))
2163 mmode
= (BEFORE
|AFTER
);
2168 case 'a': /* 'after' view in patch window */
2171 mode
= AFTER
; modename
= "after"; modehelp
= after_help
;
2172 mmode
= mode
; curs
.alt
= 0;
2175 case 'b': /* 'before' view in patch window */
2178 mode
= BEFORE
; modename
= "before"; modehelp
= before_help
;
2179 mmode
= mode
; curs
.alt
= 0;
2182 case 'o': /* 'original' view in the merge window */
2185 mode
= ORIG
; modename
= "original"; modehelp
= orig_help
;
2186 mmode
= mode
; curs
.alt
= 0;
2189 case 'r': /* the 'result' view in the merge window */
2192 mode
= RESULT
; modename
= "result"; modehelp
= result_help
;
2193 mmode
= mode
; curs
.alt
= 0;
2197 if (mode
== (BEFORE
|AFTER
))
2199 mode
= BEFORE
|AFTER
; modename
= "diff"; modehelp
= diff_help
;
2200 mmode
= mode
; curs
.alt
= 0;
2205 mode
= ORIG
|RESULT
; modename
= "merge"; modehelp
= merge_help
;
2206 mmode
= mode
; curs
.alt
= 0;
2211 if (mode
== (ORIG
|RESULT
|BEFORE
|AFTER
))
2213 mode
= ORIG
|RESULT
|BEFORE
|AFTER
; modename
= "sidebyside"; modehelp
= sidebyside_help
;
2214 mmode
= mode
; curs
.alt
= 0;
2218 case 'H': /* scroll window to the right */
2221 curs
.target
= start
+ 1;
2224 case 'L': /* scroll window to the left */
2227 curs
.target
= start
+ 1;
2232 prev_melmnt(&tvpos
.p
, fm
, fb
, fa
, ci
.merger
);
2237 next_melmnt(&tvpos
.p
, fm
, fb
, fa
, ci
.merger
);
2238 if (ci
.merger
[tvpos
.p
.m
].type
!= End
)
2242 case 'x': /* Toggle rejecting of conflict.
2243 * A 'Conflict' or 'Changed' becomes 'Unchanged'
2244 * 'Unmatched' becomes 'Changed'
2246 if (ci
.merger
[curs
.pos
.m
].oldtype
== Conflict
||
2247 ci
.merger
[curs
.pos
.m
].oldtype
== Changed
)
2249 else if (ci
.merger
[curs
.pos
.m
].oldtype
== Unmatched
)
2254 if (ci
.merger
[curs
.pos
.m
].type
== next
)
2255 ci
.merger
[curs
.pos
.m
].type
= ci
.merger
[curs
.pos
.m
].oldtype
;
2257 ci
.merger
[curs
.pos
.m
].type
= next
;
2258 p
->conflicts
= isolate_conflicts(
2259 fm
, fb
, fa
, csl1
, csl2
, 0,
2260 ci
.merger
, 0, &p
->wiggles
);
2265 case 'c': /* Toggle accepting of conflict.
2266 * A 'Conflict' becomes 'Changed'
2268 if (ci
.merger
[curs
.pos
.m
].oldtype
!= Conflict
)
2271 if (ci
.merger
[curs
.pos
.m
].type
== Changed
)
2272 ci
.merger
[curs
.pos
.m
].type
= ci
.merger
[curs
.pos
.m
].oldtype
;
2274 ci
.merger
[curs
.pos
.m
].type
= Changed
;
2275 p
->conflicts
= isolate_conflicts(
2276 fm
, fb
, fa
, csl1
, csl2
, 0,
2277 ci
.merger
, 0, &p
->wiggles
);
2282 case 'X': /* toggle where all Conflicts and Changeds
2283 * in the current line are marked Unchanged.
2284 * If any are not mark, mark them all, else
2290 if ((ci
.merger
[tpos
.p
.m
].oldtype
== Conflict
||
2291 ci
.merger
[tpos
.p
.m
].oldtype
== Changed
||
2292 ci
.merger
[tpos
.p
.m
].oldtype
== Unmatched
)
2293 && (ci
.merger
[tpos
.p
.m
].type
==
2294 ci
.merger
[tpos
.p
.m
].oldtype
))
2296 e
= prev_melmnt(&tpos
.p
, fm
, fb
, fa
, ci
.merger
);
2299 } while (!ends_line(e
) ||
2300 visible(mode
& (RESULT
|AFTER
), ci
.merger
, &tpos
) < 0);
2303 if (ci
.merger
[tpos
.p
.m
].oldtype
== Conflict
||
2304 ci
.merger
[tpos
.p
.m
].oldtype
== Changed
||
2305 ci
.merger
[tpos
.p
.m
].oldtype
== Unmatched
) {
2307 if (ci
.merger
[tpos
.p
.m
].oldtype
== Unmatched
)
2310 ci
.merger
[tpos
.p
.m
].type
= next
;
2312 ci
.merger
[tpos
.p
.m
].type
=
2313 ci
.merger
[tpos
.p
.m
].oldtype
;
2315 e
= prev_melmnt(&tpos
.p
, fm
, fb
, fa
, ci
.merger
);
2318 } while (!ends_line(e
) ||
2319 visible(mode
& (RESULT
|AFTER
), ci
.merger
, &tpos
) < 0);
2320 p
->conflicts
= isolate_conflicts(
2321 fm
, fb
, fa
, csl1
, csl2
, 0,
2322 ci
.merger
, 0, &p
->wiggles
);
2328 help_window(modehelp
, merge_window_help
, 0);
2337 if (meta
== SEARCH(0)) {
2338 if (anchor
== NULL
||
2339 !same_mpos(anchor
->pos
, pos
) ||
2340 anchor
->searchlen
!= searchlen
||
2341 !same_mp(anchor
->curs
.pos
, curs
.pos
)) {
2342 struct search_anchor
*a
= xmalloc(sizeof(*a
));
2347 a
->searchlen
= searchlen
;
2348 a
->notfound
= search_notfound
;
2354 struct search_anchor
*a
= anchor
;
2360 /* move backward and forward to make sure we
2361 * are on a visible line
2364 prev_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2368 next_mline(&tpos
, fm
, fb
, fa
, ci
.merger
, mmode
);
2369 if (ci
.merger
[tpos
.p
.m
].type
!= End
)
2375 static int show_merge(char *origname
, FILE *patch
, int reverse
,
2376 int is_merge
, char *before
, char *after
,
2377 int replace
, int selftest
, int ignore_blanks
)
2384 fseek(patch
, 0, SEEK_END
);
2385 p
.end
= ftell(patch
);
2386 fseek(patch
, 0, SEEK_SET
);
2389 p
.is_merge
= is_merge
;
2393 freopen("/dev/null","w",stderr
);
2394 return merge_window(&p
, patch
, reverse
, replace
, selftest
,
2398 static void calc_one(struct plist
*pl
, FILE *f
, int reverse
,
2401 struct stream s1
, s2
;
2402 struct stream s
= load_segment(f
, pl
->start
, pl
->end
);
2406 split_merge(s
, &sf
, &s2
, &s1
);
2408 split_merge(s
, &sf
, &s1
, &s2
);
2411 sf
= load_file(pl
->file
);
2413 pl
->chunks
= split_patch(s
, &s2
, &s1
);
2415 pl
->chunks
= split_patch(s
, &s1
, &s2
);
2417 if (sf
.body
== NULL
|| s1
.body
== NULL
|| s1
.body
== NULL
) {
2418 pl
->wiggles
= pl
->conflicts
= -1;
2420 struct file ff
, fp1
, fp2
;
2421 struct csl
*csl1
, *csl2
;
2423 ff
= split_stream(sf
, ByWord
| ignore_blanks
);
2424 fp1
= split_stream(s1
, ByWord
| ignore_blanks
);
2425 fp2
= split_stream(s2
, ByWord
| ignore_blanks
);
2427 csl1
= pdiff(ff
, fp1
, pl
->chunks
);
2429 csl1
= diff(ff
, fp1
);
2430 csl2
= diff_patch(fp1
, fp2
);
2431 ci
= make_merger(ff
, fp1
, fp2
, csl1
, csl2
, 0, 1, 0);
2432 pl
->wiggles
= ci
.wiggles
;
2433 pl
->conflicts
= ci
.conflicts
;
2449 static int get_prev(int pos
, struct plist
*pl
, int n
, int mode
)
2455 if (pl
[pos
].prev
== -1)
2456 return pl
[pos
].parent
;
2458 while (pl
[pos
].open
&&
2461 if (pl
[pos
].last
>= 0)
2462 /* always see directories */
2466 else if (mode
<= 1 && pl
[pos
].wiggles
> 0)
2468 else if (mode
<= 2 && pl
[pos
].conflicts
> 0)
2470 } while (pos
>= 0 && !found
);
2474 static int get_next(int pos
, struct plist
*pl
, int n
, int mode
,
2475 FILE *f
, int reverse
, int ignore_blanks
)
2487 while (pos
>= 0 && pl
[pos
].next
== -1)
2488 pos
= pl
[pos
].parent
;
2494 if (pl
[pos
].calced
== 0 && pl
[pos
].end
)
2495 calc_one(pl
+pos
, f
, reverse
, ignore_blanks
);
2496 if (pl
[pos
].last
>= 0)
2497 /* always see directories */
2501 else if (mode
<= 1 && pl
[pos
].wiggles
> 0)
2503 else if (mode
<= 2 && pl
[pos
].conflicts
> 0)
2505 } while (pos
>= 0 && !found
);
2509 static void draw_one(int row
, struct plist
*pl
, FILE *f
, int reverse
,
2520 if (pl
->calced
== 0 && pl
->end
)
2521 /* better load the patch and count the chunks */
2522 calc_one(pl
, f
, reverse
, ignore_blanks
);
2526 if (pl
->chunks
> 99)
2529 sprintf(hdr
, "%2d", pl
->chunks
);
2530 if (pl
->wiggles
> 99)
2531 strcpy(hdr
+2, " XX");
2533 sprintf(hdr
+2, " %2d", pl
->wiggles
);
2534 if (pl
->conflicts
> 99)
2535 strcpy(hdr
+5, " XX ");
2537 sprintf(hdr
+5, " %2d ", pl
->conflicts
);
2540 strcpy(hdr
+9, "= ");
2542 strcpy(hdr
+9, "+ ");
2544 strcpy(hdr
+9, "- ");
2548 else if (pl
->is_merge
)
2550 else if (pl
->conflicts
)
2551 attrset(a_has_conflicts
);
2552 else if (pl
->wiggles
)
2553 attrset(a_has_wiggles
);
2555 attrset(a_no_wiggles
);
2557 mvaddstr(row
, 0, hdr
);
2558 mvaddstr(row
, 11, pl
->file
);
2562 static int save_one(FILE *f
, struct plist
*pl
, int reverse
,
2565 struct stream sp
, sa
, sb
, sm
;
2566 struct file fa
, fb
, fm
;
2567 struct csl
*csl1
, *csl2
;
2570 sp
= load_segment(f
, pl
->start
,
2573 chunks
= split_patch(sp
, &sa
, &sb
);
2575 chunks
= split_patch(sp
, &sb
, &sa
);
2576 fb
= split_stream(sb
, ByWord
| ignore_blanks
);
2577 fa
= split_stream(sa
, ByWord
| ignore_blanks
);
2578 sm
= load_file(pl
->file
);
2579 fm
= split_stream(sm
, ByWord
| ignore_blanks
);
2580 csl1
= pdiff(fm
, fb
, chunks
);
2581 csl2
= diff_patch(fb
, fa
);
2582 ci
= make_merger(fm
, fb
, fa
, csl1
, csl2
, 0, 1, 0);
2583 return save_merge(fm
, fb
, fa
, ci
.merger
,
2587 static char *main_help
[] = {
2588 " You are using the \"browse\" mode of wiggle.",
2589 "This page shows a list of files in a patch together with",
2590 "the directories that contain them.",
2591 "A directory is indicated by a '+' if the contents are",
2592 "listed or a '-' if the contents are hidden. A file is",
2593 "indicated by an '='. Typing <space> or <return> will",
2594 "expose or hide a directory, and will visit a file.",
2596 "The three columns of numbers are:",
2597 " Ch The number of patch chunks which applied to",
2599 " Wi The number of chunks that needed to be wiggled",
2601 " Co The number of chunks that created an unresolvable",
2604 "Keystrokes recognised in this page are:",
2605 " ? Display this help",
2606 " SPC On a directory, toggle hiding of contents",
2607 " On file, visit the file",
2610 " control-C Disable auto-save-on-exit",
2611 " n,j,DOWN Go to next line",
2612 " p,k,UP Go to previous line",
2614 " A list All files",
2615 " W only list files with a wiggle or a conflict",
2616 " C only list files with a conflict",
2618 " S Save this file with changes applied. If",
2619 " some but not all files are saved, wiggle will",
2620 " prompt on exit to save the rest.",
2621 " R Revert the current saved file to its original",
2623 " I toggle whether spaces are ignored",
2624 " when matching text.",
2627 static char *saveall_msg
= " %d file%s (of %d) have not been saved.";
2628 static char saveall_buf
[200];
2629 static char *saveall_query
[] = {
2632 " Would you like to save them?",
2633 " Y = yes, save them all",
2634 " N = no, exit without saving anything else",
2635 " Q = Don't quit just yet",
2638 static void main_window(struct plist
*pl
, int *np
, FILE *f
, int reverse
,
2639 int replace
, int ignore_blanks
)
2641 /* The main window lists all files together with summary information:
2642 * number of chunks, number of wiggles, number of conflicts.
2643 * The list is scrollable
2644 * When a entry is 'selected', we switch to the 'file' window
2645 * The list can be condensed by removing files with no conflict
2646 * or no wiggles, or removing subdirectories
2648 * We record which file in the list is 'current', and which
2649 * screen line it is on. We try to keep things stable while
2652 * Counts are printed before the name using at most 2 digits.
2653 * Numbers greater than 99 are XX
2655 * 27 5 1 drivers/md/md.c
2657 * A directory show the sum in all children.
2660 * select: enter, space, mouseclick
2661 * on file, go to file window
2662 * on directory, toggle open
2663 * up: k, p, control-p uparrow
2664 * Move to previous open object
2665 * down: j, n, control-n, downarrow
2666 * Move to next open object
2668 * A W C: select All Wiggles or Conflicts
2673 char mesg_buf
[1024];
2674 int last_mesg_len
= 0;
2675 int pos
= 0; /* position in file */
2676 int row
= 1; /* position on screen */
2677 int rows
= 0; /* size of screen in rows */
2682 int mode
= 0; /* 0=all, 1= only wiggled, 2=only conflicted */
2683 int cnt
; /* count of files that need saving */
2684 int any
; /* count of files that have been save*/
2687 char *debug
= getenv("WIGGLE_DEBUG");
2689 if (debug
&& !*debug
)
2692 freopen("/dev/null","w",stderr
);
2694 pl
= sort_patches(pl
, np
);
2698 clear(); (void)attrset(0);
2700 mvaddstr(0, 0, "Ch Wi Co Patched Files");
2703 addstr(" (ignoring blanks)");
2707 if (row
< 1 || row
>= rows
)
2711 getmaxyx(stdscr
, rows
, cols
);
2713 if (row
>= rows
+ 3)
2718 for (i
= row
; i
> 1; i
--) {
2719 tpos
= get_prev(tpos
, pl
, *np
, mode
);
2725 /* Ok, row and pos could be trustworthy now */
2727 for (i
= row
; i
>= 1; i
--) {
2728 draw_one(i
, &pl
[tpos
], f
, reverse
, ignore_blanks
);
2729 tpos
= get_prev(tpos
, pl
, *np
, mode
);
2732 for (i
= row
+1; i
< rows
; i
++) {
2733 tpos
= get_next(tpos
, pl
, *np
, mode
, f
, reverse
,ignore_blanks
);
2735 draw_one(i
, &pl
[tpos
], f
, reverse
, ignore_blanks
);
2737 draw_one(i
, NULL
, f
, reverse
, ignore_blanks
);
2741 if (last_mesg_len
) {
2742 move(0, cols
- last_mesg_len
);
2747 last_mesg_len
= strlen(mesg
);
2748 move(0, cols
- last_mesg_len
);
2752 /* debugging help: report last keystroke */
2754 sprintf(bb
, "last-key = 0%o", c
);
2756 last_mesg_len
= strlen(bb
);
2757 mvaddstr(0, cols
- last_mesg_len
, bb
);
2767 tpos
= get_next(pos
, pl
, *np
, mode
, f
, reverse
, ignore_blanks
);
2778 tpos
= get_prev(pos
, pl
, *np
, mode
);
2786 if (getmouse(&mevent
) != OK
)
2788 while (row
< mevent
.y
&&
2789 (tpos
= get_next(pos
, pl
, *np
, mode
, f
, reverse
, ignore_blanks
))
2794 while (row
> mevent
.y
&&
2795 (tpos
= get_prev(pos
, pl
, *np
, mode
)) >= 0) {
2799 if (row
!= mevent
.y
)
2800 /* couldn't find the line */
2805 if (pl
[pos
].end
== 0) {
2806 pl
[pos
].open
= !pl
[pos
].open
;
2809 mesg
= "Opened folder";
2811 mesg
= "Closed folder";
2814 if (pl
[pos
].is_merge
)
2815 c
= merge_window(&pl
[pos
], NULL
, reverse
, 0, 0, ignore_blanks
);
2817 c
= merge_window(&pl
[pos
], f
, reverse
, 0, 0, ignore_blanks
);
2820 pl
[pos
].is_merge
= 1;
2821 snprintf(mesg_buf
, cols
,
2828 case 27: /* escape */
2830 mvaddstr(0, cols
-10, "ESC..."); clrtoeol();
2834 move(0, cols
-10); clrtoeol();
2838 mesg
= "Save-on-exit disabled. Use 'q' to quit.";
2840 mesg
= "Use 'q' to quit.";
2847 for (i
= 0; i
< *np
; i
++)
2848 if (pl
[i
].end
&& !pl
[i
].is_merge
)
2860 sprintf(saveall_buf
, saveall_msg
,
2861 cnt
, cnt
== 1 ? "" : "s", cnt
+any
);
2862 ans
= help_window(saveall_query
, NULL
, 1);
2868 for (i
= 0; i
< *np
; i
++) {
2879 printf("%d file%s saved\n", cnt
,
2880 cnt
== 1 ? "" : "s");
2884 mode
= 0; refresh
= 1;
2885 mesg
= "Showing ALL files";
2888 mode
= 1; refresh
= 1;
2889 mesg
= "Showing Wiggled files";
2892 mode
= 2; refresh
= 1;
2893 mesg
= "Showing Conflicted files";
2896 case 'S': /* Save updated file */
2897 if (pl
[pos
].end
== 0) {
2899 mesg
= "Cannot save a folder.";
2900 } else if (pl
[pos
].is_merge
) {
2902 mesg
= "File is already saved.";
2904 if (save_one(f
, &pl
[pos
], reverse
, ignore_blanks
) == 0) {
2905 pl
[pos
].is_merge
= 1;
2906 snprintf(mesg_buf
, cols
,
2909 pl
[pos
].chunks
= pl
[pos
].conflicts
;
2910 pl
[pos
].wiggles
= 0;
2912 snprintf(mesg_buf
, cols
,
2913 "Failed to save file %s.",
2920 case 'R': /* Restore updated file */
2921 if (pl
[pos
].end
== 0)
2922 mesg
= "Cannot restore a folder.";
2923 else if (!pl
[pos
].is_merge
)
2924 mesg
= "File has not been saved, cannot restore.";
2926 /* rename foo.porig to foo, and clear is_merge */
2927 char *file
= pl
[pos
].file
;
2928 char *orignew
= xmalloc(strlen(file
) + 20);
2929 strcpy(orignew
, file
);
2930 strcat(orignew
, ".porig");
2931 if (rename(orignew
, file
) == 0) {
2932 mesg
= "File has been restored.";
2933 pl
[pos
].is_merge
= 0;
2935 calc_one(&pl
[pos
], f
, reverse
, ignore_blanks
);
2937 mesg
= "Could not restore file!";
2941 case 'I': /* Toggle ignoring blanks */
2942 ignore_blanks
= ignore_blanks
? 0 : IgnoreBlanks
;
2944 for (i
= 0; i
< *np
; i
++)
2949 help_window(main_help
, NULL
, 0);
2960 static void catch(int sig
)
2962 if (sig
== SIGINT
&& !intr_kills
) {
2969 printf("Died on signal %d\n", sig
);
2971 if (sig
!= SIGBUS
&& sig
!= SIGSEGV
)
2974 /* Otherwise return and die */
2978 static void term_init(int doraw
)
2981 static int init_done
= 0;
2987 signal(SIGINT
, catch);
2988 signal(SIGQUIT
, catch);
2989 signal(SIGTERM
, catch);
2990 signal(SIGBUS
, catch);
2991 signal(SIGSEGV
, catch);
3000 use_default_colors();
3001 if (!has_colors()) {
3002 a_delete
= A_UNDERLINE
;
3004 a_common
= A_NORMAL
;
3006 a_already
= A_STANDOUT
;
3007 a_has_conflicts
= A_UNDERLINE
;
3008 a_has_wiggles
= A_BOLD
;
3009 a_no_wiggles
= A_NORMAL
;
3011 init_pair(1, COLOR_RED
, -1);
3012 a_delete
= COLOR_PAIR(1);
3013 init_pair(2, COLOR_GREEN
, -1);
3014 a_added
= COLOR_PAIR(2);
3015 a_common
= A_NORMAL
;
3016 init_pair(3, COLOR_WHITE
, COLOR_GREEN
);
3017 a_sep
= COLOR_PAIR(3); a_sep
= A_STANDOUT
;
3018 init_pair(4, -1, COLOR_YELLOW
);
3019 a_void
= COLOR_PAIR(4);
3020 init_pair(5, COLOR_BLUE
, -1);
3021 a_unmatched
= COLOR_PAIR(5);
3022 init_pair(6, COLOR_CYAN
, -1);
3023 a_extra
= COLOR_PAIR(6);
3025 init_pair(7, COLOR_BLACK
, COLOR_CYAN
);
3026 a_already
= COLOR_PAIR(7);
3028 a_has_conflicts
= a_delete
;
3029 a_has_wiggles
= a_added
;
3030 a_no_wiggles
= a_unmatched
;
3033 nonl(); intrflush(stdscr
, FALSE
); keypad(stdscr
, TRUE
);
3034 mousemask(ALL_MOUSE_EVENTS
, NULL
);
3037 int vpatch(int argc
, char *argv
[], int patch
, int strip
,
3038 int reverse
, int replace
, int selftest
, int ignore_blanks
)
3040 /* NOTE argv[0] is first arg...
3041 * Behaviour depends on number of args:
3042 * 0: A multi-file patch is read from stdin
3043 * 1: if 'patch', parse it as a multi-file patch and allow
3044 * the files to be browsed.
3045 * if filename ends '.rej', then treat it as a patch again
3046 * a file with the same basename
3047 * Else treat the file as a merge (with conflicts) and view it.
3048 * 2: First file is original, second is patch
3049 * 3: Files are: original previous new. The diff between 'previous' and
3050 * 'new' needs to be applied to 'original'.
3052 * If a multi-file patch is being read, 'strip' tells how many
3053 * path components to strip. If it is -1, we guess based on
3055 * If 'reverse' is given, when we invert any patch or diff
3056 * If 'replace' then we save the resulting merge.
3065 fprintf(stderr
, "%s: too many file names given.\n", Cmd
);
3068 case 0: /* stdin is a patch */
3069 if (lseek(fileno(stdin
), 0L, 1) == -1) {
3070 /* cannot seek, so need to copy to a temp file */
3073 fprintf(stderr
, "%s: Cannot create temp file\n", Cmd
);
3076 pl
= parse_patch(stdin
, f
, &num_patches
);
3079 pl
= parse_patch(stdin
, NULL
, &num_patches
);
3080 in
= fdopen(dup(0), "r");
3082 /* use stderr for keyboard input */
3084 if (set_prefix(pl
, num_patches
, strip
) == 0) {
3085 fprintf(stderr
, "%s: aborting\n", Cmd
);
3088 main_window(pl
, &num_patches
, in
, reverse
, replace
, ignore_blanks
);
3089 plist_free(pl
, num_patches
);
3093 case 1: /* a patch, a .rej, or a merge file */
3094 f
= fopen(argv
[0], "r");
3096 fprintf(stderr
, "%s: cannot open %s\n", Cmd
, argv
[0]);
3099 check_dir(argv
[0], fileno(f
));
3101 pl
= parse_patch(f
, NULL
, &num_patches
);
3102 if (set_prefix(pl
, num_patches
, strip
) == 0) {
3103 fprintf(stderr
, "%s: aborting\n", Cmd
);
3106 main_window(pl
, &num_patches
, f
, reverse
, replace
,ignore_blanks
);
3107 plist_free(pl
, num_patches
);
3108 } else if (strlen(argv
[0]) > 4 &&
3109 strcmp(argv
[0]+strlen(argv
[0])-4, ".rej") == 0) {
3110 char *origname
= strdup(argv
[0]);
3111 origname
[strlen(origname
) - 4] = '\0';
3112 show_merge(origname
, f
, reverse
, 0, NULL
, NULL
,
3113 replace
, selftest
, ignore_blanks
);
3115 show_merge(argv
[0], f
, reverse
, 1, NULL
, NULL
,
3116 replace
, selftest
, ignore_blanks
);
3119 case 2: /* an orig and a diff/.ref */
3120 f
= fopen(argv
[1], "r");
3121 check_dir(argv
[1], fileno(f
));
3123 fprintf(stderr
, "%s: cannot open %s\n", Cmd
, argv
[0]);
3126 show_merge(argv
[0], f
, reverse
, 0, NULL
, NULL
,
3127 replace
, selftest
, ignore_blanks
);
3129 case 3: /* orig, before, after */
3130 show_merge(argv
[0], NULL
, reverse
, 0, argv
[1], argv
[2],
3131 replace
, selftest
, ignore_blanks
);