2 * wiggle - apply rejected patches
4 * Copyright (C) 2005 Neil Brown <neilb@cse.unsw.edu.au>
5 * Copyright (C) 2010-2013 Neil Brown <neilb@suse.de>
6 * Copyright (C) 2014-2020 Neil Brown <neil@brown.name>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program.
23 * Email: <neil@brown.name>
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 static unsigned int a_delete, a_added, a_common, a_sep, a_void,
55 a_unmatched, a_extra, a_already;
56 static 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
72 static char *help_help[] = {
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 static char *help_missing[] = {
88 "The file that this patch applies to appears",
90 "Please type 'q' to continue",
94 static 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' */
625 } else if (type == Unmatched)
628 if (m[pos.p.m].in_conflict > 1)
629 rv |= CONFLICTED | CHANGES;
630 if (m[pos.p.m].in_conflict == 1 &&
631 (pos.p.o < m[pos.p.m].lo ||
632 pos.p.o > m[pos.p.m].hi))
633 rv |= CONFLICTED | CHANGES;
634 e = prev_melmnt(&pos.p, fm, fb, fa, m);
635 } while (e.start != NULL &&
637 || visible(mode, m, &pos) == -1));
638 /* This is a bit of a hack... If the end-of-line just
639 * before this line was changed, then quite possibly this
640 * line is part of a change too. This is particularly important
641 * when --ignore-blanks is in effect as newlines are not separate
642 * from other words. It could be that this test needs to be
643 * strengthened when I have examined more cases.
645 if (e.start && m[pos.p.m].oldtype == Changed)
648 if (unmatched && (rv & CHANGES))
653 /* Find the next line in the merge which is visible.
654 * If we hit the end of a conflicted set during pass-1
655 * we rewind for pass-2.
656 * 'mode' tells which bits we want to see, possible one of
657 * the 4 parts (before/after/orig/result) or one of the pairs
658 * before+after or orig+result.
660 static void next_mline(struct mpos *pos, struct file fm, struct file fb,
662 struct merge *m, int mode)
671 struct elmnt e = next_melmnt(&pos->p, fm, fb, fa, m);
675 visible(mode, m, pos) >= 0)
678 mode2 = check_line(*pos, fm, fb, fa, m, mode);
680 if ((mode2 & CHANGES) && pos->state == 0) {
681 /* Just entered a diff-set */
684 } else if (!(mode2 & CHANGES) && pos->state) {
685 /* Come to the end of a diff-set */
686 switch (pos->state) {
688 /* Need to record the end */
690 /* time for another pass */
695 /* finished final pass */
700 mask = ORIG|RESULT|BEFORE|AFTER;
701 switch (pos->state) {
703 mask &= ~(RESULT|AFTER);
706 mask &= ~(ORIG|BEFORE);
709 } while (visible(mode&mask, m, pos) < 0);
713 /* Move to previous line - simply the reverse of next_mline */
714 static void prev_mline(struct mpos *pos, struct file fm, struct file fb,
716 struct merge *m, int mode)
727 struct elmnt e = prev_melmnt(&pos->p, fm, fb, fa, m);
731 visible(mode, m, pos) >= 0)
734 mode2 = check_line(*pos, fm, fb, fa, m, mode);
736 if ((mode2 & CHANGES) && pos->state == 0) {
737 /* Just entered a diff-set */
740 } else if (!(mode2 & CHANGES) && pos->state) {
741 /* Come to the end (start) of a diff-set */
742 switch (pos->state) {
744 /* finished final pass */
748 /* Need to record the start */
750 /* time for another pass */
756 mask = ORIG|RESULT|BEFORE|AFTER;
757 switch (pos->state) {
759 mask &= ~(RESULT|AFTER);
762 mask &= ~(ORIG|BEFORE);
765 } while (visible(mode&mask, m, pos) < 0);
768 /* blank a whole row of display */
769 static void blank(int row, int start, int cols, unsigned int attr)
777 /* search of a string on one display line. If found, update the
781 static int mcontains(struct mpos pos,
782 struct file fm, struct file fb, struct file fa,
784 int mode, char *search, struct cursor *curs,
785 int dir, int ignore_case)
787 /* See if any of the files, between start of this line and here,
788 * contain the search string.
789 * However this is modified by dir:
790 * -2: find last match *before* curs
791 * -1: find last match at-or-before curs
792 * 1: find first match at-or-after curs
793 * 2: find first match *after* curs
795 * We only test for equality with curs, so if it is on a different
796 * line it will not be found and everything is before/after.
797 * As we search from end-of-line to start we find the last
799 * For a forward search, we stop when we find curs.
800 * For a backward search, we forget anything found when we find curs.
806 int len = strlen(search);
809 e = prev_melmnt(&pos.p, fm, fb, fa, m);
810 if (e.start && e.start[0]) {
813 if (same_mp(pos.p, curs->pos))
814 curs_i = curs->offset;
817 for (i = e.len-1; i >= 0; i--) {
818 if (i == curs_i && dir == -1)
819 /* next match is the one we want */
821 if (i == curs_i && dir == 2)
822 /* future matches not accepted */
824 if ((!found || dir > 0) &&
825 (ignore_case ? strncasecmp : strncmp)
826 (e.start+i, search, len) == 0) {
831 if (i == curs_i && dir == -2)
832 /* next match is the one we want */
834 if (i == curs_i && dir == 1)
835 /* future matches not accepted */
839 } while (e.start != NULL &&
841 || visible(mode, m, &pos) == -1));
850 /* Drawing the display window.
851 * There are 7 different ways we can display the data, each
852 * of which can be configured by a keystroke:
853 * o original - just show the original file with no changes, but still
854 * with highlights of what is changed or unmatched
855 * r result - show just the result of the merge. Conflicts just show
856 * the original, not the before/after options
857 * b before - show the 'before' stream of the patch
858 * a after - show the 'after' stream of the patch
859 * d diff - show just the patch, both before and after
860 * m merge - show the full merge with -+ sections for changes.
861 * If point is in a wiggled or conflicted section the
862 * window is split horizontally and the diff is shown
863 * in the bottom window
864 * | sidebyside - two panes, left and right. Left holds the merge,
865 * right holds the diff. In the case of a conflict,
866 * left holds orig/after, right holds before/after
868 * The horizontal split for 'merge' mode is managed as follows.
869 * - The window is split when we first visit a line that contains
870 * a wiggle or a conflict, and the second pane is removed when
871 * we next visit a line that contains no changes (is fully Unchanged).
872 * - to display the second pane, we find a visible end-of-line in the
873 * (BEFORE|AFTER) mode at-or-before the current end-of-line and
874 * the we centre that line.
875 * - We need to rewind to an unchanged section, and wind forward again
876 * to make sure that 'lo' and 'hi' are set properly.
877 * - every time we move, we redraw the second pane (see how that goes).
880 /* draw_mside draws one text line or, in the case of sidebyside, one side
882 * The 'mode' tells us what to draw via the 'visible()' function.
883 * It is one of ORIG RESULT BEFORE AFTER or ORIG|RESULT or BEFORE|AFTER
884 * It may also have WIGGLED or CONFLICTED ored in to trigger extra highlights.
885 * The desired cursor position is given in 'target' the actual end
886 * cursor position (allowing e.g. for tabs) is returned in *colp.
888 static void draw_mside(int mode, int row, int offset, int start, int cols,
889 struct file fm, struct file fb, struct file fa,
897 unsigned int tag_attr;
901 default: /* keep compiler happy */
902 case 0: /* unchanged line */
906 case 1: /* 'before' text */
909 if ((mode & ORIG) && (mode & CONFLICTED)) {
911 tag_attr = a_delete | A_REVERSE;
913 mode &= (ORIG|BEFORE);
915 case 2: /* the 'after' part */
918 mode &= (AFTER|RESULT);
922 if (visible(mode, m, &pos) < 0) {
923 /* Not visible, just draw a blank */
924 blank(row, offset, cols, a_void);
934 (void)attrset(tag_attr);
935 mvaddch(row, offset, tag);
938 (void)attrset(A_NORMAL);
940 if (check_line(pos, fm, fb, fa, m, mode))
943 /* find previous visible newline, or start of file */
945 e = prev_melmnt(&pos.p, fm, fb, fa, m);
946 while (e.start != NULL &&
948 visible(mode, m, &pos) == -1));
955 e = next_melmnt(&pos.p, fm, fb, fa, m);
959 if (visible(mode, m, &pos) == -1)
963 c = (unsigned char *)e.start - e.prefix;
965 attr = visible(mode, m, &pos);
966 if ((attr == a_unmatched || attr == a_extra) &&
968 /* Only highlight spaces if there is a tab nearby */
969 for (l = 0; l < e.plen + e.prefix; l++)
972 if (!highlight_space && (c[0] == ' ' || c[0] == '\t')) {
973 /* always highlight space/tab at end-of-line */
974 struct mp nxt = pos.p;
975 struct elmnt nxte = next_melmnt(&nxt, fm, fb, fa, m);
976 if (nxte.start[0] == '\n')
979 for (l = 0; l < e.plen + e.prefix; l++) {
984 if (*c >= ' ' && *c != 0x7f) {
986 (void)attrset(attr|A_REVERSE);
987 if (col >= start && col < start+cols)
988 mvaddch(row, col-start+offset, *c);
990 } else if (*c == '\t') {
992 (void)attrset(attr|A_UNDERLINE);
994 if (col >= start && col < start+cols) {
995 mvaddch(row, col-start+offset, ' ');
997 } while ((col&7) != 0);
999 if (col >= start && col < start+cols)
1000 mvaddch(row, col-start+offset, '?');
1004 if (curs->target >= 0) {
1005 if (curs->target < col) {
1006 /* Found target column */
1010 if (scol >= start + cols)
1011 /* Didn't appear on screen */
1014 curs->width = col - scol;
1017 } else if (l == curs->offset &&
1018 same_mp(pos.p, curs->pos)) {
1020 curs->target = scol;
1022 if (scol >= start + cols)
1023 /* Didn't appear on screen */
1026 curs->width = col - scol;
1033 && visible(mode, m, &pos) != -1))
1037 /* We have reached the end of visible line, or end of file */
1040 if (col >= start + cols)
1043 curs->width = -1; /* end of line */
1044 if (curs->target >= 0) {
1047 } else if (same_mp(pos.p, curs->pos))
1052 if (e.start && e.start[0] == 0) {
1055 int A, B, C, D, E, F;
1056 e1 = fb.list[m[pos.p.m].b + pos.p.o];
1057 sscanf(e1.start+1, "%d %d %d", &A, &B, &C);
1058 sscanf(e.start+1, "%d %d %d", &D, &E, &F);
1059 snprintf(b, sizeof(b), "@@ -%d,%d +%d,%d @@%s", B, C, E, F, e1.start+18);
1060 (void)attrset(a_sep);
1062 mvaddstr(row, col-start+offset, b);
1065 blank(row, col-start+offset, start+cols-col,
1067 ? (unsigned)visible(mode, m, &pos)
1071 /* Draw either 1 or 2 sides depending on the mode. */
1073 static void draw_mline(int mode, int row, int start, int cols,
1074 struct file fm, struct file fb, struct file fa,
1077 struct cursor *curs)
1080 * Draw the left and right images of this line
1081 * One side might be a_blank depending on the
1082 * visibility of this newline
1086 mode |= check_line(pos, fm, fb, fa, m, mode);
1088 if ((mode & (BEFORE|AFTER)) &&
1089 (mode & (ORIG|RESULT))) {
1092 rcols = cols - lcols - 1;
1094 (void)attrset(A_STANDOUT);
1095 mvaddch(row, lcols, '|');
1097 draw_mside(mode&~(BEFORE|AFTER), row, 0, start, lcols,
1098 fm, fb, fa, m, pos, curs && !curs->alt ? curs : NULL);
1100 draw_mside(mode&~(ORIG|RESULT), row, lcols+1, start, rcols,
1101 fm, fb, fa, m, pos, curs && curs->alt ? curs : NULL);
1103 draw_mside(mode, row, 0, start, cols,
1104 fm, fb, fa, m, pos, curs);
1107 static char *merge_help[] = {
1108 "This view shows the merge of the patch with the",
1109 "original file. It is like a full-context diff showing",
1110 "removed lines with a '-' prefix and added lines with a",
1112 "In cases where a patch chunk could not be successfully",
1113 "applied, the original text is prefixed with a '|', and",
1114 "the text that the patch wanted to add is prefixed with",
1116 "When the cursor is over such a conflict, or over a chunk",
1117 "which required wiggling to apply (i.e. there was unmatched",
1118 "text in the original, or extraneous unchanged text in",
1119 "the patch), the terminal is split and the bottom pane is",
1120 "use to display the part of the patch that applied to",
1121 "this section of the original. This allows you to confirm",
1122 "that a wiggled patch applied correctly, and to see",
1123 "why there was a conflict",
1126 static char *diff_help[] = {
1127 "This is the 'diff' or 'patch' view. It shows",
1128 "only the patch that is being applied without the",
1129 "original to which it is being applied.",
1130 "Underlined text indicates parts of the patch which",
1131 "resulted in a conflict when applied to the",
1135 static char *orig_help[] = {
1136 "This is the 'original' view which simply shows",
1137 "the original file before applying the patch.",
1138 "Sections of code that would be changed by the patch",
1139 "are highlighted in red.",
1142 static char *result_help[] = {
1143 "This is the 'result' view which shows just the",
1144 "result of applying the patch. When a conflict",
1145 "occurred this view does not show the full conflict",
1146 "but only the 'after' part of the patch. To see",
1147 "the full conflict, use the 'merge' or 'sidebyside'",
1151 static char *before_help[] = {
1152 "This view shows the 'before' section of a patch.",
1153 "It allows the expected match text to be seen uncluttered",
1154 "by text that is meant to replaced it.",
1155 "Red text is text that will be removed by the patch",
1158 static char *after_help[] = {
1159 "This view shows the 'after' section of a patch.",
1160 "It allows the intended result to be seen uncluttered",
1161 "by text that was meant to be matched and replaced.",
1162 "Green text is text that was added by the patch - it",
1163 "was not present in the 'before' part of the patch",
1166 static char *sidebyside_help[] = {
1167 "This is the Side By Side view of a patched file.",
1168 "The left side shows the original and the result.",
1169 "The right side shows the patch which was applied",
1170 "and lines up with the original/result as much as",
1173 "Where one side has no line which matches the",
1174 "other side it is displayed as a solid colour in the",
1175 "yellow family (depending on your terminal window).",
1178 static char *merge_window_help[] = {
1179 " Highlight Colours and Keystroke commands",
1181 "In all different views of a merge, highlight colours",
1182 "are used to show which parts of lines were added,",
1183 "removed, already changed, unchanged or in conflict.",
1184 "Colours and their use are:",
1185 " normal unchanged text",
1186 " red text that was removed or changed",
1187 " green text that was added or the result",
1189 " yellow background used in side-by-side for a line",
1190 " which has no match on the other",
1192 " blue text in the original which did not",
1193 " match anything in the patch",
1194 " cyan text in the patch which did not",
1195 " match anything in the original",
1196 " cyan background already changed text: the result",
1197 " of the patch matches the original",
1198 " underline remove or added text can also be",
1199 " underlined indicating that it",
1200 " was involved in a conflict",
1202 "While viewing a merge various keystroke commands can",
1203 "be used to move around and change the view. Basic",
1204 "movement commands from both 'vi' and 'emacs' are",
1207 " p control-p k UP Move to previous line",
1208 " n control-n j DOWN Move to next line",
1209 " l LEFT Move one char to right",
1210 " h RIGHT Move one char to left",
1211 " / control-s Enter incremental search mode",
1212 " control-r Enter reverse-search mode",
1213 " control-g Search again",
1214 " ? Display help message",
1215 " ESC-< 0-G Go to start of file",
1216 " ESC-> G Go to end of file",
1217 " q Return to list of files or exit",
1218 " S Arrange for merge to be saved on exit",
1219 " control-C Disable auto-save-on-exit",
1220 " control-L recenter current line",
1221 " control-V SPACE page down",
1222 " ESC-v BACKSPC page up",
1223 " N go to next patch chunk",
1224 " P go to previous patch chunk",
1225 " C go to next conflicted chunk",
1226 " C-X-o O move cursor to alternate pane",
1227 " ^ control-A go to start of line",
1228 " $ control-E go to end of line",
1230 " a display 'after' view",
1231 " b display 'before' view",
1232 " o display 'original' view",
1233 " r display 'result' view",
1234 " d display 'diff' or 'patch' view",
1235 " m display 'merge' view",
1236 " | display side-by-side view",
1238 " I toggle whether spaces are ignored",
1239 " when matching text.",
1240 " x toggle ignoring of current Changed,",
1241 " Conflict, or Unmatched item",
1242 " c toggle accepting of result of conflict",
1243 " X Revert 'c' and 'x' changes on this line",
1244 " v Save the current merge and run the",
1245 " default editor on the file.",
1248 static char *save_query[] = {
1250 "You have modified the merge.",
1251 "Would you like to save it?",
1252 " Y = save the modified merge",
1253 " N = discard modifications, don't save",
1254 " Q = return to viewing modified merge",
1258 static char *toggle_ignore[] = {
1260 "You have modified the merge.",
1261 "Toggling ignoring of spaces will discard changes.",
1262 "Do you want to proceed?",
1263 " Y = discard changes and toggle ignoring of spaces",
1264 " N = keep changes, don't toggle",
1268 static void do_edit(char *file, int line)
1270 char *ed = getenv("VISUAL");
1273 ed = getenv("EDITOR");
1275 ed = "/usr/bin/edit";
1276 snprintf(linebuf, sizeof(linebuf), "+%d", line);
1279 execlp(ed, ed, linebuf, file, NULL);
1288 static void *memdup(void *a, int len)
1290 char *r = malloc(len);
1296 static int save_merge(struct file a, struct file b, struct file c,
1297 struct merge *merger, char *file, int backup)
1299 char *replacename = wiggle_xmalloc(strlen(file) + 20);
1300 char *orignew = wiggle_xmalloc(strlen(file) + 20);
1305 strcpy(replacename, file);
1306 strcat(replacename, "XXXXXX");
1307 strcpy(orignew, file);
1308 strcat(orignew, ".porig");
1310 fd = mkstemp(replacename);
1315 outfile = fdopen(fd, "w");
1316 lineno = wiggle_print_merge(outfile, &a, &b, &c, 0, merger,
1319 if (backup && rename(file, orignew) != 0)
1321 else if (rename(replacename, file) != 0)
1327 return err < 0 ? err : lineno;
1330 static int save_tmp_merge(struct file a, struct file b, struct file c,
1331 struct merge *merger, char **filep,
1332 struct merge *mpos, int streampos, int offsetpos)
1341 dir = getenv("TMPDIR");
1345 asprintf(&fname, "%s/wiggle-tmp-XXXXXX", dir);
1349 base = strrchr(dir, '/');
1354 asprintf(&fname, "%.*stmp-XXXXXX-%s", (int)(base-dir), dir, base);
1355 suffix = strlen(base)+1;
1357 fd = mkstemps(fname, suffix);
1364 outfile = fdopen(fd, "w");
1365 lineno = wiggle_print_merge(outfile, &a, &b, &c, 0, merger,
1366 mpos, streampos, offsetpos);
1372 static int merge_window(struct plist *p, FILE *f, int reverse, int replace,
1373 int selftest, int ignore_blanks, int just_diff, int backup)
1375 /* Display the merge window in one of the selectable modes,
1376 * starting with the 'merge' mode.
1378 * Newlines are the key to display.
1379 * 'pos' is always a visible newline (or eof).
1380 * In sidebyside mode it might only be visible on one side,
1381 * in which case the other side will be blank.
1382 * Where the newline is visible, we rewind the previous visible
1383 * newline visible and display the stuff in between
1385 * A 'position' is a struct mpos
1388 struct stream sm, sb, sa, sp; /* main, before, after, patch */
1389 struct file fm, fb, fa;
1390 struct csl *csl1, *csl2;
1392 int ch; /* count of chunks */
1393 /* Always refresh the current line.
1394 * If refresh == 1, refresh all lines. If == 2, clear first
1397 int rows = 0, cols = 0;
1398 int splitrow = -1; /* screen row for split - diff appears below */
1399 int lastrow = 0; /* end of screen, or just above 'splitrow' */
1402 int mode = just_diff ? (BEFORE|AFTER) : (ORIG|RESULT);
1403 int mmode = mode; /* Mode for moving - used when in 'other' pane */
1404 char *modename = just_diff ? "diff" : "merge";
1405 char **modehelp = just_diff ? diff_help : merge_help;
1409 int trow; /* screen-row while searching. If we cannot find,
1410 * we forget this number */
1412 struct mpos pos; /* current point */
1413 struct mpos tpos, /* temp point while drawing lines above and below pos */
1414 toppos, /* pos at top of screen - for page-up */
1415 botpos; /* pos at bottom of screen - for page-down */
1418 int meta = 0, /* mode for multi-key commands- SEARCH or META */
1420 int num = -1, /* numeric arg being typed. */
1423 int changes = 0; /* If any edits have been made to the merge */
1424 int answer; /* answer to 'save changes?' question */
1427 char search[80]; /* string we are searching for */
1428 unsigned int searchlen = 0;
1429 int search_notfound = 0;
1433 * 1 == no because there are upper-case chars
1434 * 2 == yes as there are no upper-case chars
1437 int ignore_case = 2;
1438 /* We record all the places we find so 'backspace'
1439 * can easily return to the previous one
1441 struct search_anchor {
1442 struct search_anchor *next;
1447 unsigned int searchlen;
1450 #define free_stuff(none) \
1460 #define find_line(ln) \
1462 pos.p.m = 0; /* merge node */ \
1463 pos.p.s = 0; /* stream number */ \
1464 pos.p.o = -1; /* offset */ \
1467 memset(&curs, 0, sizeof(curs)); \
1469 next_mline(&pos, fm, fb, fa, ci.merger, mode); \
1470 while (pos.p.lineno < ln && ci.merger[pos.p.m].type != End); \
1473 #define prepare_merge(ch) \
1475 /* FIXME check for errors in the stream */ \
1476 fm = wiggle_split_stream(sm, ByWord | ignore_blanks); \
1477 fb = wiggle_split_stream(sb, ByWord | ignore_blanks); \
1478 fa = wiggle_split_stream(sa, ByWord | ignore_blanks); \
1480 if (ch && !just_diff) \
1481 csl1 = wiggle_pdiff(fm, fb, ch); \
1483 csl1 = wiggle_diff(fm, fb, 1); \
1484 csl2 = wiggle_diff_patch(fb, fa, 1); \
1486 ci = wiggle_make_merger(fm, fb, fa, csl1, csl2, 0, 1, 0); \
1487 for (i = 0; ci.merger[i].type != End; i++) \
1488 ci.merger[i].oldtype = ci.merger[i].type; \
1498 /* three separate files */
1499 sb = wiggle_load_file(p->before);
1500 sa = wiggle_load_file(p->after);
1504 sm = wiggle_load_file(p->file);
1506 /* One merge file */
1507 sp = wiggle_load_file(p->file);
1509 wiggle_split_merge(sp, &sm, &sa, &sb);
1511 wiggle_split_merge(sp, &sm, &sb, &sa);
1516 sp = wiggle_load_segment(f, p->start, p->end);
1519 wiggle_split_merge(sp, &sm, &sa, &sb);
1521 wiggle_split_merge(sp, &sm, &sb, &sa);
1525 ch = wiggle_split_patch(sp, &sa, &sb);
1527 ch = wiggle_split_patch(sp, &sb, &sa);
1531 sm = wiggle_load_file(p->file);
1535 if (!sm.body || !sb.body || !sa.body) {
1542 help_window(help_missing, NULL, 0);
1544 help_window(help_corrupt, NULL, 0);
1549 term_init(!selftest);
1560 if (row < 1 || row >= lastrow)
1565 if (mode == (ORIG|RESULT)) {
1566 int cmode = check_line(pos, fm, fb, fa, ci.merger, mode);
1567 if (cmode & (WIGGLED | CONFLICTED)) {
1569 splitrow = (rows+1)/2;
1570 lastrow = splitrow - 1;
1573 } else if (!curs.alt && splitrow >= 0) {
1578 } else if (splitrow >= 0) {
1585 getmaxyx(stdscr, rows, cols);
1586 rows--; /* keep last row clear */
1587 if (splitrow >= 0) {
1588 splitrow = (rows+1)/2;
1589 lastrow = splitrow - 1;
1597 if (row > lastrow+3)
1603 /* Always refresh the line */
1604 while (start > curs.target) {
1610 vispos = pos; /* visible position - if cursor is in
1611 * alternate pane, pos might not be visible
1613 if (check_line(vispos, fm, fb, fa, ci.merger, mode)
1615 if (vispos.state == 0) {
1617 vispos.lo = vispos.p;
1618 vispos.hi = vispos.p;
1624 if (visible(mode, ci.merger, &vispos) < 0)
1625 prev_mline(&vispos, fm, fb, fa, ci.merger, mode);
1629 draw_mline(mode, row, start, cols, fm, fb, fa, ci.merger,
1630 vispos, (splitrow >= 0 && curs.alt) ? NULL : &curs);
1631 if (curs.width == 0 && start < curs.col) {
1632 /* width == 0 implies it appear after end-of-screen */
1637 if (curs.col < start) {
1649 for (i = row-1; i >= 1 && tpos.p.m >= 0; ) {
1650 prev_mline(&tpos, fm, fb, fa, ci.merger, mode);
1651 draw_mline(mode, i--, start, cols,
1652 fm, fb, fa, ci.merger,
1663 blank(i--, 0, cols, a_void);
1665 for (i = row; i <= lastrow && ci.merger[tpos.p.m].type != End; ) {
1666 draw_mline(mode, i++, start, cols,
1667 fm, fb, fa, ci.merger,
1669 next_mline(&tpos, fm, fb, fa, ci.merger, mode);
1671 botpos = tpos; botrow = i;
1672 while (i <= lastrow)
1673 blank(i++, 0, cols, a_void);
1676 if (splitrow >= 0) {
1677 struct mpos spos = pos;
1678 int smode = BEFORE|AFTER;
1679 int srow = (rows + splitrow)/2;
1680 if (check_line(spos, fm, fb, fa, ci.merger, smode)
1682 if (spos.state == 0)
1687 if (visible(smode, ci.merger, &spos) < 0)
1688 prev_mline(&spos, fm, fb, fa, ci.merger, smode);
1689 /* Now hi/lo might be wrong, so lets fix it. */
1692 /* 'hi' might be wrong so we mustn't depend
1693 * on it while walking back. So set state
1694 * to 1 to avoid ever testing it.
1697 while (spos.p.m >= 0 && spos.state != 0)
1698 prev_mline(&spos, fm, fb, fa, ci.merger, smode);
1699 while (!same_mpos(spos, tpos) &&
1701 ci.merger[spos.p.m].type != End)
1702 next_mline(&spos, fm, fb, fa, ci.merger, smode);
1704 (void)attrset(a_sep);
1705 for (i = 0; i < cols; i++)
1706 mvaddstr(splitrow, i, "-");
1709 for (i = srow-1; i > splitrow; i--) {
1710 prev_mline(&tpos, fm, fb, fa, ci.merger, smode);
1711 draw_mline(smode, i, start, cols, fm, fb, fa, ci.merger,
1714 while (i > splitrow)
1715 blank(i--, 0, cols, a_void);
1718 i < rows && tpos.p.m >= 0 &&
1719 ci.merger[tpos.p.m].type != End;
1721 draw_mline(smode, i, start, cols, fm, fb, fa, ci.merger,
1723 (i == srow && curs.alt) ? &curs : NULL);
1724 next_mline(&tpos, fm, fb, fa, ci.merger, smode);
1727 blank(i++, 0, cols, a_void);
1729 /* Now that curs is accurate, report the type */
1734 l = snprintf(buf, 100, "File: %s%s ",
1735 p->file, reverse ? " - reversed" : "");
1736 snprintf(buf+l, 100-l, "Mode: %s", modename);
1737 (void)attrset(A_BOLD);
1738 mvaddstr(0, 0, buf);
1739 (void)attrset(A_NORMAL);
1741 addstr(" (ignoring blanks)");
1743 (void)attrset(A_BOLD);
1744 if (ci.merger[curs.pos.m].type != ci.merger[curs.pos.m].oldtype)
1745 l = snprintf(buf, sizeof(buf), "%s->", typenames[ci.merger[curs.pos.m].oldtype]);
1746 snprintf(buf+l, sizeof(buf)-l, "%s ln:%d",
1747 typenames[ci.merger[curs.pos.m].type],
1748 (pos.p.lineno-1)/2);
1749 mvaddstr(0, cols - strlen(buf) - 1, buf);
1751 #define META(c) ((c)|0x1000)
1752 #define SEARCH(c) ((c)|0x2000)
1753 #define CTRLX(c) ((c)|0x4000)
1755 (void)attrset(A_NORMAL);
1764 snprintf(buf, sizeof(buf), "%d ", num);
1769 if (meta & CTRLX(0))
1771 if (meta & SEARCH(0)) {
1773 addstr("Backwards ");
1776 if (search_notfound)
1777 addstr(" - Not Found.");
1778 search_notfound = 0;
1781 /* '+1' to skip over the leading +/-/| char */
1782 if (curs.alt && splitrow > 0)
1783 move((rows + splitrow)/2, curs.col - start + 1);
1784 else if (curs.alt && ((mode & (BEFORE|AFTER)) &&
1785 (mode & (ORIG|RESULT))))
1786 move(row, curs.col-start + (cols-1)/2+2);
1788 move(row, curs.col-start+1);
1797 tmeta = meta; meta = 0;
1798 tnum = num; num = -1;
1799 cswitch = c | tmeta;
1800 /* Handle some ranges */
1801 /* case '0' ... '9': */
1802 if (cswitch >= '0' && cswitch <= '9')
1804 /* case SEARCH(' ') ... SEARCH('~'): */
1805 if (cswitch >= SEARCH(' ') && cswitch <= SEARCH('~'))
1806 cswitch = SEARCH(' ');
1809 case 27: /* escape */
1819 case META('<'): /* start of file */
1824 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
1825 } while (tpos.p.m >= 0);
1829 case META('>'): /* end of file */
1836 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
1837 } while (ci.merger[tpos.p.m].type != End);
1841 case '0': /* actually '0'...'9' */
1844 num = tnum*10 + (c-'0');
1848 mesg = "Autosave disabled";
1850 mesg = "Use 'q' to quit";
1854 mesg = "Will auto-save on exit, using Ctrl-C to cancel";
1863 answer = help_window(save_query, NULL, 1);
1868 p->conflicts = wiggle_isolate_conflicts(
1869 fm, fb, fa, csl1, csl2, 0,
1870 ci.merger, 0, &p->wiggles);
1871 p->chunks = p->conflicts;
1872 save_merge(fm, fb, fa, ci.merger,
1873 p->outfile ? p->outfile : p->file,
1874 backup && (p->outfile ? 0 : !p->is_merge));
1884 case 'I': /* Toggle ignoring of spaces */
1887 answer = help_window(toggle_ignore, NULL, 1);
1893 ignore_blanks = ignore_blanks ? 0 : IgnoreBlanks;
1895 find_line(pos.p.lineno);
1901 if (!p->file || just_diff) {
1902 mesg = "Cannot run editor when diffing";
1906 lineno = save_tmp_merge(fm, fb, fa, ci.merger,
1908 ci.merger + pos.p.m,
1913 do_edit(tempname, lineno);
1914 sp = wiggle_load_file(tempname);
1916 wiggle_split_merge(sp, &sm, &sb, &sa);
1917 if (sp.len == sm.len &&
1918 memcmp(sp.body, sm.body, sm.len) == 0 &&
1920 /* no conflicts left, so display diff */
1922 sm = wiggle_load_file(p->file);
1925 sb.body = memdup(sm.body, sm.len);
1933 find_line(pos.p.lineno);
1940 /* incr search forward */
1943 search[searchlen] = 0;
1948 /* incr search backwards */
1951 search[searchlen] = 0;
1954 case SEARCH('G'-64):
1955 case SEARCH('S'-64):
1956 case SEARCH('R'-64):
1958 if ((c|tmeta) == SEARCH('R'-64))
1963 tpos = pos; trow = row;
1966 case SEARCH('H'-64):
1967 case SEARCH(KEY_BACKSPACE):
1970 struct search_anchor *a;
1976 struct search_anchor *a;
1984 search_notfound = a->notfound;
1985 searchlen = a->searchlen;
1986 search[searchlen] = 0;
1991 case SEARCH(' '): /* actually ' '...'~' */
1994 if (searchlen < sizeof(search)-1)
1995 search[searchlen++] = c & (0x7f);
1996 search[searchlen] = 0;
1997 tpos = pos; trow = row;
1999 search_notfound = 1;
2000 if (ignore_case == 1 || ignore_case == 2) {
2003 for (i=0; i < searchlen; i++)
2004 if (isupper(search[i])) {
2010 if (mcontains(tpos, fm, fb, fa, ci.merger,
2011 mmode, search, &curs, searchdir,
2012 ignore_case >= 2)) {
2016 search_notfound = 0;
2019 if (searchdir < 0) {
2021 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2024 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2026 } while (tpos.p.m >= 0 && ci.merger[tpos.p.m].type != End);
2027 searchdir /= abs(searchdir);
2037 case 'V'-64: /* page down */
2039 if (botrow <= lastrow) {
2049 case META('v'): /* page up */
2056 if (getmouse(&mevent) != OK)
2058 /* First see if this is on the 'other' pane */
2060 /* merge mode, top and bottom */
2061 if ((curs.alt && mevent.y < splitrow) ||
2062 (!curs.alt && mevent.y > splitrow)) {
2065 } else if (mode == (ORIG|RESULT|BEFORE|AFTER)) {
2066 /* side-by-side mode */
2067 if ((curs.alt && mevent.x < cols/2) ||
2068 (!curs.alt && mevent.x > cols/2)) {
2072 /* Now try to find the right line */
2073 if (splitrow < 0 || !curs.alt)
2076 trow = (rows + splitrow)/2;
2077 while (trow > mevent.y) {
2079 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2080 if (tpos.p.m >= 0) {
2086 while (trow < mevent.y) {
2088 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2089 if (ci.merger[tpos.p.m].type != End) {
2095 if (splitrow < 0 || !curs.alt)
2096 /* it is OK to change the row */
2099 /* Now set the target column */
2100 if (mode == (ORIG|RESULT|BEFORE|AFTER) &&
2102 curs.target = start + mevent.x - cols / 2 - 1;
2104 curs.target = start + mevent.x - 1;
2112 for (; tnum > 0 ; tnum--) {
2114 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2115 if (ci.merger[tpos.p.m].type != End) {
2130 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2131 } while (!(pos.state == 0
2132 && (check_line(pos, fm, fb, fa, ci.merger, mmode)
2133 & (CONFLICTED|WIGGLED)) == 0)
2134 && ci.merger[tpos.p.m].type != End);
2138 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2139 } while (pos.state == 0
2140 && (check_line(pos, fm, fb, fa, ci.merger, mmode)
2141 & (CONFLICTED|WIGGLED)) == 0
2142 && ci.merger[tpos.p.m].type != End);
2150 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2151 } while ((check_line(pos, fm, fb, fa, ci.merger, mmode)
2153 && ci.merger[tpos.p.m].type != End);
2157 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2158 } while ((check_line(pos, fm, fb, fa, ci.merger, mmode)
2160 && ci.merger[tpos.p.m].type != End);
2169 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2170 } while (tpos.state == 0
2171 && (check_line(tpos, fm, fb, fa, ci.merger, mmode)
2172 & (CONFLICTED|WIGGLED)) == 0
2177 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2178 } while (!(tpos.state == 0
2179 && (check_line(tpos, fm, fb, fa, ci.merger, mmode)
2180 & (CONFLICTED|WIGGLED)) == 0)
2190 for (; tnum > 0 ; tnum--) {
2192 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2193 if (tpos.p.m >= 0) {
2204 curs.target = curs.col - 1;
2205 if (curs.target < 0) {
2206 /* Try to go to end of previous line */
2208 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2209 if (tpos.p.m >= 0) {
2221 if (curs.width >= 0)
2222 curs.target = curs.col + curs.width;
2224 /* end of line, go to next */
2226 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2227 if (ci.merger[tpos.p.m].type != End) {
2250 curs.alt = !curs.alt;
2251 if (curs.alt && mode == (ORIG|RESULT))
2252 mmode = (BEFORE|AFTER);
2257 case 'a': /* 'after' view in patch window */
2260 mode = AFTER; modename = "after"; modehelp = after_help;
2261 mmode = mode; curs.alt = 0;
2264 case 'b': /* 'before' view in patch window */
2267 mode = BEFORE; modename = "before"; modehelp = before_help;
2268 mmode = mode; curs.alt = 0;
2271 case 'o': /* 'original' view in the merge window */
2274 mode = ORIG; modename = "original"; modehelp = orig_help;
2275 mmode = mode; curs.alt = 0;
2278 case 'r': /* the 'result' view in the merge window */
2281 mode = RESULT; modename = "result"; modehelp = result_help;
2282 mmode = mode; curs.alt = 0;
2286 if (mode == (BEFORE|AFTER))
2288 mode = BEFORE|AFTER; modename = "diff"; modehelp = diff_help;
2289 mmode = mode; curs.alt = 0;
2294 mode = ORIG|RESULT; modename = "merge"; modehelp = merge_help;
2295 mmode = mode; curs.alt = 0;
2300 if (mode == (ORIG|RESULT|BEFORE|AFTER))
2302 mode = ORIG|RESULT|BEFORE|AFTER; modename = "sidebyside"; modehelp = sidebyside_help;
2303 mmode = mode; curs.alt = 0;
2307 case 'H': /* scroll window to the right */
2310 curs.target = start + 1;
2313 case 'L': /* scroll window to the left */
2316 curs.target = start + 1;
2320 case 'x': /* Toggle rejecting of conflict.
2321 * A 'Conflict' or 'Changed' becomes 'Unchanged'
2322 * 'Unmatched' becomes 'Changed'
2324 if (ci.merger[curs.pos.m].oldtype == Conflict ||
2325 ci.merger[curs.pos.m].oldtype == Changed)
2327 else if (ci.merger[curs.pos.m].oldtype == Unmatched)
2332 if (ci.merger[curs.pos.m].type == next)
2333 ci.merger[curs.pos.m].type = ci.merger[curs.pos.m].oldtype;
2335 ci.merger[curs.pos.m].type = next;
2336 p->conflicts = wiggle_isolate_conflicts(
2337 fm, fb, fa, csl1, csl2, 0,
2338 ci.merger, 0, &p->wiggles);
2343 case 'c': /* Toggle accepting of conflict.
2344 * A 'Conflict' or 'Extraneous' becomes 'Changed'
2346 if (ci.merger[curs.pos.m].oldtype != Conflict &&
2347 ci.merger[curs.pos.m].oldtype != Extraneous)
2350 if (ci.merger[curs.pos.m].type == Changed)
2351 ci.merger[curs.pos.m].type = ci.merger[curs.pos.m].oldtype;
2353 ci.merger[curs.pos.m].type = Changed;
2354 p->conflicts = wiggle_isolate_conflicts(
2355 fm, fb, fa, csl1, csl2, 0,
2356 ci.merger, 0, &p->wiggles);
2361 case 'X': /* Reset all changes on the current line */
2364 ci.merger[tpos.p.m].type =
2365 ci.merger[tpos.p.m].oldtype;
2366 e = prev_melmnt(&tpos.p, fm, fb, fa, ci.merger);
2369 } while (!ends_line(e) ||
2370 visible(mode & (RESULT|AFTER), ci.merger, &tpos) < 0);
2371 p->conflicts = wiggle_isolate_conflicts(
2372 fm, fb, fa, csl1, csl2, 0,
2373 ci.merger, 0, &p->wiggles);
2379 help_window(modehelp, merge_window_help, 0);
2388 if (meta == SEARCH(0)) {
2389 if (anchor == NULL ||
2390 !same_mpos(anchor->pos, pos) ||
2391 anchor->searchlen != searchlen ||
2392 !same_mp(anchor->curs.pos, curs.pos)) {
2393 struct search_anchor *a = wiggle_xmalloc(sizeof(*a));
2398 a->searchlen = searchlen;
2399 a->notfound = search_notfound;
2405 struct search_anchor *a = anchor;
2411 /* move backward and forward to make sure we
2412 * are on a visible line
2415 prev_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2419 next_mline(&tpos, fm, fb, fa, ci.merger, mmode);
2420 if (ci.merger[tpos.p.m].type != End)
2426 static int show_merge(char *origname, FILE *patch, int reverse,
2427 int is_merge, char *before, char *after,
2428 int replace, char *outfile,
2429 int selftest, int ignore_blanks,
2430 int just_diff, int backup)
2432 struct plist p = {0};
2435 p.outfile = replace ? outfile : NULL;
2438 fseek(patch, 0, SEEK_END);
2439 p.end = ftell(patch);
2440 fseek(patch, 0, SEEK_SET);
2443 p.is_merge = is_merge;
2447 freopen("/dev/null","w",stderr);
2448 return merge_window(&p, patch, reverse, replace, selftest,
2449 ignore_blanks, just_diff, backup);
2452 static void calc_one(struct plist *pl, FILE *f, int reverse,
2453 int ignore_blanks, int just_diff)
2455 struct stream s1, s2;
2456 struct stream s = wiggle_load_segment(f, pl->start, pl->end);
2460 wiggle_split_merge(s, &sf, &s2, &s1);
2462 wiggle_split_merge(s, &sf, &s1, &s2);
2466 pl->chunks = wiggle_split_patch(s, &s2, &s1);
2468 pl->chunks = wiggle_split_patch(s, &s1, &s2);
2472 sf = wiggle_load_file(pl->file);
2474 if (sf.body == NULL || s1.body == NULL || s1.body == NULL) {
2475 pl->wiggles = pl->conflicts = -1;
2477 struct file ff, fp1, fp2;
2478 struct csl *csl1, *csl2;
2480 ff = wiggle_split_stream(sf, ByWord | ignore_blanks);
2481 fp1 = wiggle_split_stream(s1, ByWord | ignore_blanks);
2482 fp2 = wiggle_split_stream(s2, ByWord | ignore_blanks);
2483 if (pl->chunks && !just_diff)
2484 csl1 = wiggle_pdiff(ff, fp1, pl->chunks);
2486 csl1 = wiggle_diff(ff, fp1, 1);
2487 csl2 = wiggle_diff_patch(fp1, fp2, 1);
2488 ci = wiggle_make_merger(ff, fp1, fp2, csl1, csl2, 0, 1, 0);
2489 pl->wiggles = ci.wiggles;
2490 pl->conflicts = ci.conflicts;
2507 static int get_prev(int pos, struct plist *pl, int n, int mode)
2510 if (pos == -1 || pl == NULL)
2513 if (pl[pos].prev == -1)
2514 return pl[pos].parent;
2516 while (pl[pos].open &&
2519 if (pl[pos].last >= 0)
2520 /* always see directories */
2524 else if (mode <= 1 && pl[pos].wiggles > 0)
2526 else if (mode <= 2 && pl[pos].conflicts > 0)
2528 } while (pos >= 0 && !found);
2532 static int get_next(int pos, struct plist *pl, int n, int mode,
2533 FILE *f, int reverse, int ignore_blanks, int just_diff)
2545 while (pos >= 0 && pl[pos].next == -1)
2546 pos = pl[pos].parent;
2552 if (pl[pos].calced == 0 && pl[pos].end)
2553 calc_one(pl+pos, f, reverse, ignore_blanks, just_diff);
2554 if (pl[pos].last >= 0)
2555 /* always see directories */
2559 else if (mode <= 1 && pl[pos].wiggles > 0)
2561 else if (mode <= 2 && pl[pos].conflicts > 0)
2563 } while (pos >= 0 && !found);
2567 static void draw_one(int row, struct plist *pl, FILE *f, int reverse,
2568 int ignore_blanks, int just_diff)
2578 if (pl->calced == 0 && pl->end)
2579 /* better load the patch and count the chunks */
2580 calc_one(pl, f, reverse, ignore_blanks, just_diff);
2584 if (pl->chunks > 99)
2587 sprintf(hdr, "%2d", pl->chunks);
2588 if (pl->wiggles > 99)
2589 strcpy(hdr+2, " XX");
2591 sprintf(hdr+2, " %2d", pl->wiggles);
2592 if (pl->conflicts > 99)
2593 strcpy(hdr+5, " XX ");
2595 sprintf(hdr+5, " %2d ", pl->conflicts);
2598 strcpy(hdr+9, "= ");
2600 strcpy(hdr+9, "+ ");
2602 strcpy(hdr+9, "- ");
2606 else if (pl->is_merge)
2608 else if (pl->conflicts)
2609 attrset(a_has_conflicts);
2610 else if (pl->wiggles)
2611 attrset(a_has_wiggles);
2613 attrset(a_no_wiggles);
2615 mvaddstr(row, 0, hdr);
2616 mvaddstr(row, 11, pl->file);
2620 static int save_one(FILE *f, struct plist *pl, int reverse,
2621 int ignore_blanks, int backup)
2623 struct stream sp, sa, sb, sm;
2624 struct file fa, fb, fm;
2625 struct csl *csl1, *csl2;
2628 sp = wiggle_load_segment(f, pl->start,
2631 chunks = wiggle_split_patch(sp, &sa, &sb);
2633 chunks = wiggle_split_patch(sp, &sb, &sa);
2634 fb = wiggle_split_stream(sb, ByWord | ignore_blanks);
2635 fa = wiggle_split_stream(sa, ByWord | ignore_blanks);
2636 sm = wiggle_load_file(pl->file);
2637 fm = wiggle_split_stream(sm, ByWord | ignore_blanks);
2638 csl1 = wiggle_pdiff(fm, fb, chunks);
2639 csl2 = wiggle_diff_patch(fb, fa, 1);
2640 ci = wiggle_make_merger(fm, fb, fa, csl1, csl2, 0, 1, 0);
2641 return save_merge(fm, fb, fa, ci.merger,
2645 static char *main_help[] = {
2646 " You are using the \"browse\" mode of wiggle.",
2647 "This page shows a list of files in a patch together with",
2648 "the directories that contain them.",
2649 "A directory is indicated by a '+' if the contents are",
2650 "listed or a '-' if the contents are hidden. A file is",
2651 "indicated by an '='. Typing <space> or <return> will",
2652 "expose or hide a directory, and will visit a file.",
2654 "The three columns of numbers are:",
2655 " Ch The number of patch chunks which applied to",
2657 " Wi The number of chunks that needed to be wiggled",
2659 " Co The number of chunks that created an unresolvable",
2662 "Keystrokes recognised in this page are:",
2663 " ? Display this help",
2664 " SPC On a directory, toggle hiding of contents",
2665 " On file, visit the file",
2668 " control-C Disable auto-save-on-exit",
2669 " n,j,DOWN Go to next line",
2670 " p,k,UP Go to previous line",
2672 " A list All files",
2673 " W only list files with a wiggle or a conflict",
2674 " C only list files with a conflict",
2676 " S Save this file with changes applied. If",
2677 " some but not all files are saved, wiggle will",
2678 " prompt on exit to save the rest.",
2679 " R Revert the current saved file to its original",
2681 " I toggle whether spaces are ignored",
2682 " when matching text.",
2685 static char *saveall_msg = " %d file%s (of %d) have not been saved.";
2686 static char saveall_buf[200];
2687 static char *saveall_query[] = {
2690 " Would you like to save them?",
2691 " Y = yes, save them all",
2692 " N = no, exit without saving anything else",
2693 " Q = Don't quit just yet",
2696 static void main_window(struct plist *pl, int np, FILE *f, int reverse,
2697 int replace, int ignore_blanks, int just_diff, int backup)
2699 /* The main window lists all files together with summary information:
2700 * number of chunks, number of wiggles, number of conflicts.
2701 * The list is scrollable
2702 * When a entry is 'selected', we switch to the 'file' window
2703 * The list can be condensed by removing files with no conflict
2704 * or no wiggles, or removing subdirectories
2706 * We record which file in the list is 'current', and which
2707 * screen line it is on. We try to keep things stable while
2710 * Counts are printed before the name using at most 2 digits.
2711 * Numbers greater than 99 are XX
2713 * 27 5 1 drivers/md/md.c
2715 * A directory show the sum in all children.
2718 * select: enter, space, mouseclick
2719 * on file, go to file window
2720 * on directory, toggle open
2721 * up: k, p, control-p uparrow
2722 * Move to previous open object
2723 * down: j, n, control-n, downarrow
2724 * Move to next open object
2726 * A W C: select All Wiggles or Conflicts
2731 char mesg_buf[1024];
2732 int last_mesg_len = 0;
2733 int pos = 0; /* position in file */
2734 int row = 1; /* position on screen */
2735 int rows = 0; /* size of screen in rows */
2740 int mode = 0; /* 0=all, 1= only wiggled, 2=only conflicted */
2741 int cnt; /* count of files that need saving */
2742 int any; /* count of files that have been save*/
2745 char *debug = getenv("WIGGLE_DEBUG");
2747 if (debug && !*debug)
2750 freopen("/dev/null","w",stderr);
2755 clear(); (void)attrset(0);
2757 mvaddstr(0, 0, "Ch Wi Co Patched Files");
2760 addstr(" (ignoring blanks)");
2764 if (row < 1 || row >= rows)
2768 getmaxyx(stdscr, rows, cols);
2770 if (row >= rows + 3)
2775 for (i = row; i > 1; i--) {
2776 tpos = get_prev(tpos, pl, np, mode);
2782 /* Ok, row and pos could be trustworthy now */
2784 for (i = row; i >= 1; i--) {
2785 draw_one(i, &pl[tpos], f, reverse, ignore_blanks, just_diff);
2786 tpos = get_prev(tpos, pl, np, mode);
2789 for (i = row+1; i < rows; i++) {
2790 tpos = get_next(tpos, pl, np, mode, f, reverse,ignore_blanks, just_diff);
2792 draw_one(i, &pl[tpos], f, reverse, ignore_blanks, just_diff);
2794 draw_one(i, NULL, f, reverse, ignore_blanks, just_diff);
2798 if (last_mesg_len) {
2799 move(0, cols - last_mesg_len);
2804 last_mesg_len = strlen(mesg);
2805 move(0, cols - last_mesg_len);
2809 /* debugging help: report last keystroke */
2811 sprintf(bb, "last-key = 0%o", c);
2813 last_mesg_len = strlen(bb);
2814 mvaddstr(0, cols - last_mesg_len, bb);
2824 tpos = get_next(pos, pl, np, mode, f, reverse, ignore_blanks, just_diff);
2835 tpos = get_prev(pos, pl, np, mode);
2843 if (getmouse(&mevent) != OK)
2845 while (row < mevent.y &&
2846 (tpos = get_next(pos, pl, np, mode, f, reverse, ignore_blanks, just_diff))
2851 while (row > mevent.y &&
2852 (tpos = get_prev(pos, pl, np, mode)) >= 0) {
2856 if (row != mevent.y)
2857 /* couldn't find the line */
2862 if (pl[pos].end == 0) {
2863 pl[pos].open = !pl[pos].open;
2866 mesg = "Opened folder";
2868 mesg = "Closed folder";
2871 if (pl[pos].is_merge)
2872 c = merge_window(&pl[pos], NULL, reverse, 0, 0,
2873 ignore_blanks, just_diff, backup);
2875 c = merge_window(&pl[pos], f, reverse, 0, 0,
2876 ignore_blanks, just_diff, backup);
2879 pl[pos].is_merge = 1;
2880 snprintf(mesg_buf, cols,
2887 case 27: /* escape */
2889 mvaddstr(0, cols-10, "ESC..."); clrtoeol();
2893 move(0, cols-10); clrtoeol();
2897 mesg = "Save-on-exit disabled. Use 'q' to quit.";
2899 mesg = "Use 'q' to quit.";
2906 for (i = 0; i < np; i++)
2907 if (pl[i].end && !pl[i].is_merge)
2919 sprintf(saveall_buf, saveall_msg,
2920 cnt, cnt == 1 ? "" : "s", cnt+any);
2921 ans = help_window(saveall_query, NULL, 1);
2927 for (i = 0; i < np; i++) {
2932 ignore_blanks, backup);
2938 printf("%d file%s saved\n", cnt,
2939 cnt == 1 ? "" : "s");
2943 mode = 0; refresh = 1;
2944 mesg = "Showing ALL files";
2947 mode = 1; refresh = 1;
2948 mesg = "Showing Wiggled files";
2951 mode = 2; refresh = 1;
2952 mesg = "Showing Conflicted files";
2955 case 'S': /* Save updated file */
2956 if (pl[pos].end == 0) {
2958 mesg = "Cannot save a folder.";
2959 } else if (pl[pos].is_merge) {
2961 mesg = "File is already saved.";
2963 if (save_one(f, &pl[pos], reverse, ignore_blanks, backup) == 0) {
2964 pl[pos].is_merge = 1;
2965 snprintf(mesg_buf, cols,
2968 pl[pos].chunks = pl[pos].conflicts;
2969 pl[pos].wiggles = 0;
2971 snprintf(mesg_buf, cols,
2972 "Failed to save file %s.",
2979 case 'R': /* Restore updated file */
2980 if (pl[pos].end == 0)
2981 mesg = "Cannot restore a folder.";
2982 else if (!pl[pos].is_merge)
2983 mesg = "File has not been saved, cannot restore.";
2985 mesg = "Backups are disabled, nothing to restore!";
2987 /* rename foo.porig to foo, and clear is_merge */
2988 char *file = pl[pos].file;
2989 char *orignew = wiggle_xmalloc(strlen(file) + 20);
2990 strcpy(orignew, file);
2991 strcat(orignew, ".porig");
2992 if (rename(orignew, file) == 0) {
2993 mesg = "File has been restored.";
2994 pl[pos].is_merge = 0;
2996 calc_one(&pl[pos], f, reverse, ignore_blanks, just_diff);
2998 mesg = "Could not restore file!";
3002 case 'I': /* Toggle ignoring blanks */
3003 ignore_blanks = ignore_blanks ? 0 : IgnoreBlanks;
3005 for (i = 0; i < np; i++)
3010 help_window(main_help, NULL, 0);
3021 static void catch(int sig)
3023 if (sig == SIGINT && !intr_kills) {
3030 printf("Died on signal %d\n", sig);
3032 if (sig != SIGBUS && sig != SIGSEGV)
3035 /* Otherwise return and wiggle_die */
3039 static void term_init(int doraw)
3042 static int init_done = 0;
3048 signal(SIGINT, catch);
3049 signal(SIGQUIT, catch);
3050 signal(SIGTERM, catch);
3051 signal(SIGBUS, catch);
3052 signal(SIGSEGV, catch);
3061 use_default_colors();
3062 if (!has_colors()) {
3063 a_delete = A_UNDERLINE;
3065 a_common = A_NORMAL;
3067 a_already = A_STANDOUT;
3068 a_has_conflicts = A_UNDERLINE;
3069 a_has_wiggles = A_BOLD;
3070 a_no_wiggles = A_NORMAL;
3072 init_pair(1, COLOR_RED, -1);
3073 a_delete = COLOR_PAIR(1);
3074 init_pair(2, COLOR_GREEN, -1);
3075 a_added = COLOR_PAIR(2);
3076 a_common = A_NORMAL;
3077 init_pair(3, COLOR_WHITE, COLOR_GREEN);
3078 a_sep = COLOR_PAIR(3); a_sep = A_STANDOUT;
3079 init_pair(4, -1, COLOR_YELLOW);
3080 a_void = COLOR_PAIR(4);
3081 init_pair(5, COLOR_BLUE, -1);
3082 a_unmatched = COLOR_PAIR(5);
3083 init_pair(6, COLOR_CYAN, -1);
3084 a_extra = COLOR_PAIR(6);
3086 init_pair(7, COLOR_BLACK, COLOR_CYAN);
3087 a_already = COLOR_PAIR(7);
3089 a_has_conflicts = a_delete;
3090 a_has_wiggles = a_added;
3091 a_no_wiggles = a_unmatched;
3094 nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE);
3095 mousemask(ALL_MOUSE_EVENTS, NULL);
3098 int vpatch(int argc, char *argv[], int patch, int strip,
3099 int reverse, int replace, char *outfilename,
3100 int selftest, int ignore_blanks, int backup)
3102 /* NOTE argv[0] is first arg...
3103 * Behaviour depends on number of args and 'patch'.
3104 * If 'patch' is '1', assume a patch. if '2', assume a diff.
3105 * 0: A multi-file patch or diff is read from stdin.
3106 * A 'patch' is applies to relevant files. A 'diff' is just
3108 * 1: if 'patch', parse it as a multi-file patch/diff and allow
3109 * the files to be browsed.
3110 * if filename ends '.rej', then treat it as a patch/diff again
3111 * a file with the same basename
3112 * Else treat the file as a merge (with conflicts) and view it.
3114 * 2: First file is original, second is patch unless patch==2,
3115 * then two files need to be diffed.
3116 * 3: Files are: original previous new. The diff between 'previous' and
3117 * 'new' needs to be applied to 'original'.
3119 * If a multi-file patch is being read, 'strip' tells how many
3120 * path components to strip. If it is -1, we guess based on
3122 * If 'reverse' is given, when we invert any patch or diff
3123 * If 'replace' then we save the resulting merge.
3129 int just_diff = (patch == 2);
3133 fprintf(stderr, "%s: too many file names given.\n", wiggle_Cmd);
3136 case 0: /* stdin is a patch or diff */
3137 if (lseek(fileno(stdin), 0L, 1) == -1) {
3138 /* cannot seek, so need to copy to a temp file */
3141 fprintf(stderr, "%s: Cannot create temp file\n", wiggle_Cmd);
3144 pl = wiggle_parse_patch(stdin, f, &num_patches);
3147 pl = wiggle_parse_patch(stdin, NULL, &num_patches);
3148 in = fdopen(dup(0), "r");
3150 /* use stderr for keyboard input */
3153 wiggle_set_prefix(pl, num_patches, strip) == 0) {
3154 fprintf(stderr, "%s: aborting\n", wiggle_Cmd);
3157 pl = wiggle_sort_patches(pl, &num_patches);
3158 main_window(pl, num_patches, in, reverse, replace, ignore_blanks,
3160 wiggle_plist_free(pl, num_patches);
3164 case 1: /* a patch/diff, a .rej, or a merge file */
3165 f = fopen(argv[0], "r");
3167 fprintf(stderr, "%s: cannot open %s\n", wiggle_Cmd, argv[0]);
3170 wiggle_check_dir(argv[0], fileno(f));
3172 pl = wiggle_parse_patch(f, NULL, &num_patches);
3173 if (!just_diff && wiggle_set_prefix(pl, num_patches, strip) == 0) {
3174 fprintf(stderr, "%s: aborting\n", wiggle_Cmd);
3177 pl = wiggle_sort_patches(pl, &num_patches);
3178 main_window(pl, num_patches, f, reverse, replace,
3179 ignore_blanks, just_diff, backup);
3180 wiggle_plist_free(pl, num_patches);
3181 } else if (strlen(argv[0]) > 4 &&
3182 strcmp(argv[0]+strlen(argv[0])-4, ".rej") == 0) {
3183 char *origname = strdup(argv[0]);
3184 origname[strlen(origname) - 4] = '\0';
3185 show_merge(origname, f, reverse, 0, NULL, NULL,
3186 replace, outfilename,
3187 selftest, ignore_blanks, just_diff, backup);
3189 show_merge(argv[0], f, reverse, 1, NULL, NULL,
3190 replace, outfilename,
3191 selftest, ignore_blanks, just_diff, backup);
3194 case 2: /* an orig and a diff/.rej or two files */
3196 show_merge(NULL, NULL, reverse, 0, argv[0], argv[1],
3197 replace, outfilename,
3198 selftest, ignore_blanks, just_diff, backup);
3201 f = fopen(argv[1], "r");
3203 fprintf(stderr, "%s: cannot open %s\n", wiggle_Cmd, argv[0]);
3206 wiggle_check_dir(argv[1], fileno(f));
3207 show_merge(argv[0], f, reverse, 0, NULL, NULL,
3208 replace, outfilename,
3209 selftest, ignore_blanks, just_diff, backup);
3211 case 3: /* orig, before, after */
3212 show_merge(argv[0], NULL, reverse, 0, argv[1], argv[2],
3213 replace, outfilename,
3214 selftest, ignore_blanks, just_diff, backup);