added save/load diffview options.
[midnight-commander.git] / src / diffviewer / ydiff.c
blobf0b3056a58efb1b757912954c06b5c549686e3f8
1 /*
2 Copyright (C) 2007, 2010 Free Software Foundation, Inc.
3 Written by:
4 2007 Daniel Borca <dborca@yahoo.com>
6 2010 Slava Zanko <slavazanko@gmail.com>
7 2010 Andrew Borodin <aborodin@vmail.ru>
8 2010 Ilia Maslakov <il.smind@gmail.com>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <config.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
35 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/tty/color.h"
38 #include "lib/tty/key.h"
40 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
41 #include "lib/vfs/mc-vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
43 #include "src/cmddef.h"
44 #include "src/keybind.h"
45 #include "src/cmd.h"
46 #include "src/dialog.h"
47 #include "src/help.h"
48 #include "src/wtools.h"
49 #include "src/charsets.h"
50 #include "src/history.h"
51 #include "src/panel.h" /* Needed for current_panel and other_panel */
52 #include "src/layout.h" /* Needed for get_current_index and get_other_panel */
53 #include "src/main.h" /* mc_run_mode */
54 #include "src/selcodepage.h"
56 #include "ydiff.h"
57 #include "internal.h"
59 /*** global variables ****************************************************************************/
61 const global_keymap_t *diff_map;
63 /*** file scope macro definitions ****************************************************************/
65 #define g_array_foreach(a, TP, cbf) \
66 do { \
67 size_t g_array_foreach_i;\
68 TP *g_array_foreach_var=NULL; \
69 for (g_array_foreach_i=0;g_array_foreach_i < a->len; g_array_foreach_i++) \
70 { \
71 g_array_foreach_var = &g_array_index(a,TP,g_array_foreach_i); \
72 (*cbf) (g_array_foreach_var); \
73 } \
74 } while (0)
76 #define FILE_READ_BUF 4096
77 #define FILE_FLAG_TEMP (1 << 0)
79 #define OPTX 50
80 #define OPTY 17
82 #define ADD_CH '+'
83 #define DEL_CH '-'
84 #define CHG_CH '*'
85 #define EQU_CH ' '
87 #define HDIFF_ENABLE 1
88 #define HDIFF_MINCTX 5
89 #define HDIFF_DEPTH 10
91 #define TAB_SKIP(ts, pos) ((ts) - (pos) % (ts))
93 #define FILE_DIRTY(fs) \
94 do { \
95 (fs)->pos = 0; \
96 (fs)->len = 0; \
97 } while (0)
99 /*** file scope type declarations ****************************************************************/
101 /*** file scope variables ************************************************************************/
103 /*** file scope functions ************************************************************************/
105 /* --------------------------------------------------------------------------------------------- */
107 static void
108 dview_set_codeset (WDiff * dview)
110 #ifdef HAVE_CHARSET
111 const char *encoding_id = NULL;
113 dview->utf8 = TRUE;
114 encoding_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
115 if (encoding_id != NULL)
117 GIConv conv;
118 conv = str_crt_conv_from (encoding_id);
119 if (conv != INVALID_CONV)
121 if (dview->converter != str_cnv_from_term)
122 str_close_conv (dview->converter);
123 dview->converter = conv;
125 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
127 #else
128 (void) dview;
129 #endif
132 /* --------------------------------------------------------------------------------------------- */
134 static void
135 dview_select_encoding (WDiff * dview)
137 #ifdef HAVE_CHARSET
138 if (do_select_codepage ())
139 dview_set_codeset (dview);
140 #else
141 (void) dview;
142 #endif
146 static gboolean
147 rewrite_backup_content (const char *from_file_name, const char *to_file_name)
149 FILE *backup_fd;
150 char *contents;
151 gsize length;
153 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
154 return FALSE;
156 backup_fd = fopen (to_file_name, "w");
157 if (backup_fd == NULL)
159 g_free (contents);
160 return FALSE;
163 fwrite ((const void *) contents, length, 1, backup_fd);
165 fflush (backup_fd);
166 fclose (backup_fd);
167 g_free (contents);
168 return TRUE;
171 /* buffered I/O ************************************************************* */
174 * Try to open a temporary file.
176 * \param[out] name address of a pointer to store the temporary name
178 * \return file descriptor on success, negative on error
180 * \note the name is not altered if this function fails
183 static int
184 open_temp (void **name)
186 int fd;
187 char *diff_file_name = NULL;
189 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
190 if (fd == -1)
192 message (D_ERROR, MSG_ERROR,
193 _(" Cannot create temporary diff file \n %s "),
194 unix_error_string (errno));
195 return -1;
197 *name = diff_file_name;
198 return fd;
201 /* --------------------------------------------------------------------------------------------- */
204 * Alocate file structure and associate file descriptor to it.
206 * \param fd file descriptor
208 * \return file structure
210 static FBUF *
211 f_dopen (int fd)
213 FBUF *fs;
215 if (fd < 0)
216 return NULL;
218 fs = g_try_malloc (sizeof (FBUF));
219 if (fs == NULL)
220 return NULL;
222 fs->buf = g_try_malloc (FILE_READ_BUF);
223 if (fs->buf == NULL)
225 g_free (fs);
226 return NULL;
229 fs->fd = fd;
230 FILE_DIRTY (fs);
231 fs->flags = 0;
232 fs->data = NULL;
234 return fs;
237 /* --------------------------------------------------------------------------------------------- */
240 * Free file structure without closing the file.
242 * \param fs file structure
244 * \return 0 on success, non-zero on error
246 static int
247 f_free (FBUF * fs)
249 int rv = 0;
250 if (fs->flags & FILE_FLAG_TEMP)
252 rv = unlink (fs->data);
253 g_free (fs->data);
255 g_free (fs->buf);
256 g_free (fs);
257 return rv;
261 /* --------------------------------------------------------------------------------------------- */
264 * Open a binary temporary file in R/W mode.
266 * \return file structure
268 * \note the file will be deleted when closed
270 static FBUF *
271 f_temp (void)
273 int fd;
274 FBUF *fs;
276 fs = f_dopen (0);
277 if (fs == NULL)
279 return NULL;
282 fd = open_temp (&fs->data);
283 if (fd < 0)
285 f_free (fs);
286 return NULL;
289 fs->fd = fd;
290 fs->flags = FILE_FLAG_TEMP;
291 return fs;
294 /* --------------------------------------------------------------------------------------------- */
297 * Open a binary file in specified mode.
299 * \param filename file name
300 * \param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
302 * \return file structure
304 static FBUF *
305 f_open (const char *filename, int flags)
307 int fd;
308 FBUF *fs;
310 fs = f_dopen (0);
311 if (fs == NULL)
313 return NULL;
316 fd = open (filename, flags);
317 if (fd < 0)
319 f_free (fs);
320 return NULL;
323 fs->fd = fd;
324 return fs;
327 /* --------------------------------------------------------------------------------------------- */
330 * Read a line of bytes from file until newline or EOF.
332 * \param buf destination buffer
333 * \param size size of buffer
334 * \param fs file structure
336 * \return number of bytes read
338 * \note does not stop on null-byte
339 * \note buf will not be null-terminated
341 static size_t
342 f_gets (char *buf, size_t size, FBUF * fs)
344 size_t j = 0;
348 int i;
349 int stop = 0;
351 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
353 buf[j] = fs->buf[i];
354 if (buf[j] == '\n')
356 stop = 1;
359 fs->pos = i;
361 if (j == size || stop)
363 break;
366 fs->pos = 0;
367 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
369 while (fs->len > 0);
371 return j;
374 /* --------------------------------------------------------------------------------------------- */
377 * Seek into file.
379 * \param fs file structure
380 * \param off offset
381 * \param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
383 * \return position in file, starting from begginning
385 * \note avoids thrashing read cache when possible
387 static off_t
388 f_seek (FBUF * fs, off_t off, int whence)
390 off_t rv;
392 if (fs->len && whence != SEEK_END)
394 rv = lseek (fs->fd, 0, SEEK_CUR);
395 if (rv != -1)
397 if (whence == SEEK_CUR)
399 whence = SEEK_SET;
400 off += rv - fs->len + fs->pos;
402 if (off - rv >= -fs->len && off - rv <= 0)
404 fs->pos = fs->len + off - rv;
405 return off;
410 rv = lseek (fs->fd, off, whence);
411 if (rv != -1)
413 FILE_DIRTY (fs);
415 return rv;
418 /* --------------------------------------------------------------------------------------------- */
421 * Seek to the beginning of file, thrashing read cache.
423 * \param fs file structure
425 * \return 0 if success, non-zero on error
427 static off_t
428 f_reset (FBUF * fs)
430 off_t rv = lseek (fs->fd, 0, SEEK_SET);
431 if (rv != -1)
433 FILE_DIRTY (fs);
435 return rv;
438 /* --------------------------------------------------------------------------------------------- */
441 * Write bytes to file.
443 * \param fs file structure
444 * \param buf source buffer
445 * \param size size of buffer
447 * \return number of written bytes, -1 on error
449 * \note thrashes read cache
451 static ssize_t
452 f_write (FBUF * fs, const char *buf, size_t size)
454 ssize_t rv = write (fs->fd, buf, size);
455 if (rv >= 0)
457 FILE_DIRTY (fs);
459 return rv;
462 /* --------------------------------------------------------------------------------------------- */
465 * Truncate file to the current position.
467 * \param fs file structure
469 * \return current file size on success, negative on error
471 * \note thrashes read cache
473 static off_t
474 f_trunc (FBUF * fs)
476 off_t off = lseek (fs->fd, 0, SEEK_CUR);
477 if (off != -1)
479 int rv = ftruncate (fs->fd, off);
480 if (rv != 0)
482 off = -1;
484 else
486 FILE_DIRTY (fs);
489 return off;
492 /* --------------------------------------------------------------------------------------------- */
495 * Close file.
497 * \param fs file structure
499 * \return 0 on success, non-zero on error
501 * \note if this is temporary file, it is deleted
503 static int
504 f_close (FBUF * fs)
506 int rv = close (fs->fd);
507 f_free (fs);
508 return rv;
511 /* --------------------------------------------------------------------------------------------- */
514 * Create pipe stream to process.
516 * \param cmd shell command line
517 * \param flags open mode, either O_RDONLY or O_WRONLY
519 * \return file structure
521 static FBUF *
522 p_open (const char *cmd, int flags)
524 FILE *f;
525 FBUF *fs;
526 const char *type = NULL;
528 if (flags == O_RDONLY)
530 type = "r";
532 if (flags == O_WRONLY)
534 type = "w";
537 if (type == NULL)
539 return NULL;
542 fs = f_dopen (0);
543 if (fs == NULL)
545 return NULL;
548 f = popen (cmd, type);
549 if (f == NULL)
551 f_free (fs);
552 return NULL;
555 fs->fd = fileno (f);
556 fs->data = f;
557 return fs;
560 /* --------------------------------------------------------------------------------------------- */
563 * Close pipe stream.
565 * \param fs structure
567 * \return 0 on success, non-zero on error
569 static int
570 p_close (FBUF * fs)
572 int rv = pclose (fs->data);
573 f_free (fs);
574 return rv;
578 * Get one char (byte) from string
580 * \param char * str, gboolean * result
582 * \return int as character or 0 and result == FALSE if fail
585 static int
586 dview_get_byte (char * str, gboolean * result)
588 if (str == NULL)
590 *result = FALSE;
591 return 0;
593 *result = TRUE;
594 return (unsigned char) *str;
599 * Get utf multibyte char from string
601 * \param char * str, int * char_width, gboolean * result
603 * \return int as utf character or 0 and result == FALSE if fail
606 static int
607 dview_get_utf (char * str, int * char_width, gboolean * result)
609 int res = -1;
610 gunichar ch;
611 gchar *next_ch = NULL;
612 int width = 0;
614 *result = TRUE;
616 if (str == NULL)
618 *result = FALSE;
619 width = 0;
620 return 0;
623 res = g_utf8_get_char_validated (str, -1);
625 if (res < 0)
627 ch = *str;
628 width = 0;
630 else
632 ch = res;
633 /* Calculate UTF-8 char width */
634 next_ch = g_utf8_next_char (str);
635 if (next_ch)
637 width = next_ch - str;
639 else
641 ch = 0;
642 width = 0;
645 *char_width = width;
646 return ch;
649 static int
650 dview_str_utf8_offset_to_pos (const char *text, size_t length)
652 ptrdiff_t result;
653 if (text == NULL || text[0] == '\0')
654 return length;
655 if (g_utf8_validate (text, -1, NULL))
657 result = g_utf8_offset_to_pointer (text, length) - text;
659 else
661 gunichar uni;
662 char *tmpbuf, *buffer;
663 buffer = tmpbuf = g_strdup (text);
664 while (tmpbuf[0] != '\0')
666 uni = g_utf8_get_char_validated (tmpbuf, -1);
667 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
669 tmpbuf = g_utf8_next_char (tmpbuf);
671 else
673 tmpbuf[0] = '.';
674 tmpbuf++;
678 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
679 g_free (buffer);
681 return max (length, (size_t) result);
685 /* --------------------------------------------------------------------------------------------- */
686 /* diff parse *************************************************************** */
689 * Read decimal number from string.
691 * \param[in,out] str string to parse
692 * \param[out] n extracted number
694 * \return 0 if success, otherwise non-zero
696 static int
697 scan_deci (const char **str, int *n)
699 const char *p = *str;
700 char *q;
701 errno = 0;
702 *n = strtol (p, &q, 10);
703 if (errno || p == q)
705 return -1;
707 *str = q;
708 return 0;
711 /* --------------------------------------------------------------------------------------------- */
714 * Parse line for diff statement.
716 * \param p string to parse
717 * \param ops list of diff statements
719 * \return 0 if success, otherwise non-zero
721 static int
722 scan_line (const char *p, GArray * ops)
724 DIFFCMD op;
726 int f1, f2;
727 int t1, t2;
728 int cmd;
730 int range;
732 /* handle the following cases:
733 * NUMaNUM[,NUM]
734 * NUM[,NUM]cNUM[,NUM]
735 * NUM[,NUM]dNUM
736 * where NUM is a positive integer
739 if (scan_deci (&p, &f1) != 0 || f1 < 0)
741 return -1;
743 f2 = f1;
744 range = 0;
745 if (*p == ',')
747 p++;
748 if (scan_deci (&p, &f2) != 0 || f2 < f1)
750 return -1;
752 range = 1;
755 cmd = *p++;
756 if (cmd == 'a')
758 if (range)
760 return -1;
763 else if (cmd != 'c' && cmd != 'd')
765 return -1;
768 if (scan_deci (&p, &t1) != 0 || t1 < 0)
770 return -1;
772 t2 = t1;
773 range = 0;
774 if (*p == ',')
776 p++;
777 if (scan_deci (&p, &t2) != 0 || t2 < t1)
779 return -1;
781 range = 1;
784 if (cmd == 'd')
786 if (range)
788 return -1;
792 op.a[0][0] = f1;
793 op.a[0][1] = f2;
794 op.cmd = cmd;
795 op.a[1][0] = t1;
796 op.a[1][1] = t2;
797 g_array_append_val (ops, op);
798 return 0;
801 /* --------------------------------------------------------------------------------------------- */
804 * Parse diff output and extract diff statements.
806 * \param f stream to read from
807 * \param ops list of diff statements to fill
809 * \return positive number indicating number of hunks, otherwise negative
811 static int
812 scan_diff (FBUF * f, GArray * ops)
814 int sz;
815 char buf[BUFSIZ];
817 while ((sz = f_gets (buf, sizeof (buf) - 1, f)))
819 if (isdigit (buf[0]))
821 if (buf[sz - 1] != '\n')
823 return -1;
825 buf[sz] = '\0';
826 if (scan_line (buf, ops) != 0)
828 return -1;
830 continue;
832 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)))
837 return ops->len;
840 /* --------------------------------------------------------------------------------------------- */
843 * Invoke diff and extract diff statements.
845 * \param args extra arguments to be passed to diff
846 * \param extra more arguments to be passed to diff
847 * \param file1 first file to compare
848 * \param file2 second file to compare
849 * \param ops list of diff statements to fill
851 * \return positive number indicating number of hunks, otherwise negative
853 static int
854 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
855 GArray * ops)
857 static const char *opt =
858 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
859 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
860 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
861 " --unchanged-group-format=''";
863 int rv;
864 FBUF *f;
865 char *cmd;
866 int code;
868 cmd = g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args, extra, opt, file1, file2);
869 if (cmd == NULL)
870 return -1;
872 f = p_open (cmd, O_RDONLY);
873 g_free (cmd);
875 if (f == NULL)
876 return -1;
878 rv = scan_diff (f, ops);
879 code = p_close (f);
881 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
882 return -1;
884 return rv;
887 /* --------------------------------------------------------------------------------------------- */
890 * Reparse and display file according to diff statements.
892 * \param ord 0 if displaying first file, 1 if displaying 2nd file
893 * \param filename file name to display
894 * \param ops list of diff statements
895 * \param printer printf-like function to be used for displaying
896 * \param ctx printer context
898 * \return 0 if success, otherwise non-zero
900 static int
901 dff_reparse (int ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
903 size_t i;
904 FBUF *f;
905 size_t sz;
906 char buf[BUFSIZ];
907 int line = 0;
908 off_t off = 0;
909 const DIFFCMD *op;
910 int eff;
911 int add_cmd;
912 int del_cmd;
914 f = f_open (filename, O_RDONLY);
915 if (f == NULL)
917 return -1;
920 ord &= 1;
921 eff = ord;
923 add_cmd = 'a';
924 del_cmd = 'd';
925 if (ord)
927 add_cmd = 'd';
928 del_cmd = 'a';
930 #define F1 a[eff][0]
931 #define F2 a[eff][1]
932 #define T1 a[ ord^1 ][0]
933 #define T2 a[ ord^1 ][1]
934 for (i = 0; i < ops->len; i++)
936 int n;
937 op = &g_array_index (ops, DIFFCMD, i);
938 n = op->F1 - (op->cmd != add_cmd);
939 while (line < n && (sz = f_gets (buf, sizeof (buf), f)))
941 line++;
942 printer (ctx, EQU_CH, line, off, sz, buf);
943 off += sz;
944 while (buf[sz - 1] != '\n')
946 if (!(sz = f_gets (buf, sizeof (buf), f)))
948 printer (ctx, 0, 0, 0, 1, "\n");
949 break;
951 printer (ctx, 0, 0, 0, sz, buf);
952 off += sz;
955 if (line != n)
957 goto err;
960 if (op->cmd == add_cmd)
962 n = op->T2 - op->T1 + 1;
963 while (n)
965 printer (ctx, DEL_CH, 0, 0, 1, "\n");
966 n--;
969 if (op->cmd == del_cmd)
971 n = op->F2 - op->F1 + 1;
972 while (n && (sz = f_gets (buf, sizeof (buf), f)))
974 line++;
975 printer (ctx, ADD_CH, line, off, sz, buf);
976 off += sz;
977 while (buf[sz - 1] != '\n')
979 if (!(sz = f_gets (buf, sizeof (buf), f)))
981 printer (ctx, 0, 0, 0, 1, "\n");
982 break;
984 printer (ctx, 0, 0, 0, sz, buf);
985 off += sz;
987 n--;
989 if (n)
991 goto err;
994 if (op->cmd == 'c')
996 n = op->F2 - op->F1 + 1;
997 while (n && (sz = f_gets (buf, sizeof (buf), f)))
999 line++;
1000 printer (ctx, CHG_CH, line, off, sz, buf);
1001 off += sz;
1002 while (buf[sz - 1] != '\n')
1004 if (!(sz = f_gets (buf, sizeof (buf), f)))
1006 printer (ctx, 0, 0, 0, 1, "\n");
1007 break;
1009 printer (ctx, 0, 0, 0, sz, buf);
1010 off += sz;
1012 n--;
1014 if (n)
1016 goto err;
1018 n = op->T2 - op->T1 - (op->F2 - op->F1);
1019 while (n > 0)
1021 printer (ctx, CHG_CH, 0, 0, 1, "\n");
1022 n--;
1026 #undef T2
1027 #undef T1
1028 #undef F2
1029 #undef F1
1031 while ((sz = f_gets (buf, sizeof (buf), f)))
1033 line++;
1034 printer (ctx, EQU_CH, line, off, sz, buf);
1035 off += sz;
1036 while (buf[sz - 1] != '\n')
1038 if (!(sz = f_gets (buf, sizeof (buf), f)))
1040 printer (ctx, 0, 0, 0, 1, "\n");
1041 break;
1043 printer (ctx, 0, 0, 0, sz, buf);
1044 off += sz;
1048 f_close (f);
1049 return 0;
1051 err:
1052 f_close (f);
1053 return -1;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /* horizontal diff ********************************************************** */
1060 * Longest common substring.
1062 * \param s first string
1063 * \param m length of first string
1064 * \param t second string
1065 * \param n length of second string
1066 * \param ret list of offsets for longest common substrings inside each string
1067 * \param min minimum length of common substrings
1069 * \return 0 if success, nonzero otherwise
1071 static int
1072 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1074 int i, j;
1076 int *Lprev, *Lcurr;
1078 int z = 0;
1080 if (m < min || n < min)
1082 /* XXX early culling */
1083 return 0;
1086 Lprev = g_new0 (int, n + 1);
1087 Lcurr = g_new0 (int, n + 1);
1089 if (Lprev == NULL || Lcurr == NULL)
1091 g_free (Lprev);
1092 g_free (Lcurr);
1093 return -1;
1096 for (i = 0; i < m; i++)
1098 int *L = Lprev;
1099 Lprev = Lcurr;
1100 Lcurr = L;
1101 #ifdef USE_MEMSET_IN_LCS
1102 memset (Lcurr, 0, (n + 1) * sizeof (int));
1103 #endif
1104 for (j = 0; j < n; j++)
1106 #ifndef USE_MEMSET_IN_LCS
1107 Lcurr[j + 1] = 0;
1108 #endif
1109 if (s[i] == t[j])
1111 int v = Lprev[j] + 1;
1112 Lcurr[j + 1] = v;
1113 if (z < v)
1115 z = v;
1116 g_array_set_size (ret, 0);
1118 if (z == v && z >= min)
1120 int off0 = i - z + 1;
1121 int off1 = j - z + 1;
1122 size_t k;
1123 for (k = 0; k < ret->len; k++)
1125 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1126 if ((*p)[0] == off0)
1128 break;
1130 if ((*p)[1] == off1)
1132 break;
1135 if (k == ret->len)
1137 PAIR p2;
1138 p2[0] = off0;
1139 p2[1] = off1;
1140 g_array_append_val (ret, p2);
1147 free (Lcurr);
1148 free (Lprev);
1149 return z;
1151 free (Lcurr);
1152 free (Lprev);
1153 return -1;
1156 /* --------------------------------------------------------------------------------------------- */
1159 * Scan recursively for common substrings and build ranges.
1161 * \param s first string
1162 * \param t second string
1163 * \param bracket current limits for both of the strings
1164 * \param min minimum length of common substrings
1165 * \param hdiff list of horizontal diff ranges to fill
1166 * \param depth recursion depth
1168 * \return 0 if success, nonzero otherwise
1170 static gboolean
1171 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1172 unsigned int depth)
1174 BRACKET p;
1176 if (depth--)
1178 GArray *ret;
1179 BRACKET b;
1180 int len;
1181 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1182 if (ret == NULL)
1183 return FALSE;
1185 len = lcsubstr (s + bracket[0].off, bracket[0].len,
1186 t + bracket[1].off, bracket[1].len, ret, min);
1187 if (ret->len)
1189 size_t k = 0;
1190 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1191 const PAIR *data2;
1193 b[0].off = bracket[0].off;
1194 b[0].len = (*data)[0];
1195 b[1].off = bracket[1].off;
1196 b[1].len = (*data)[1];
1197 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1198 return FALSE;
1200 for (k = 0; k < ret->len - 1; k++)
1202 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1203 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1204 b[0].off = bracket[0].off + (*data)[0] + len;
1205 b[0].len = (*data2)[0] - (*data)[0] - len;
1206 b[1].off = bracket[1].off + (*data)[1] + len;
1207 b[1].len = (*data2)[1] - (*data)[1] - len;
1208 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1209 return FALSE;
1211 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1212 b[0].off = bracket[0].off + (*data)[0] + len;
1213 b[0].len = bracket[0].len - (*data)[0] - len;
1214 b[1].off = bracket[1].off + (*data)[1] + len;
1215 b[1].len = bracket[1].len - (*data)[1] - len;
1216 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1217 return FALSE;
1219 g_array_free (ret, TRUE);
1220 return TRUE;
1224 p[0].off = bracket[0].off;
1225 p[0].len = bracket[0].len;
1226 p[1].off = bracket[1].off;
1227 p[1].len = bracket[1].len;
1228 g_array_append_val (hdiff, p);
1230 return TRUE;
1233 /* --------------------------------------------------------------------------------------------- */
1236 * Build list of horizontal diff ranges.
1238 * \param s first string
1239 * \param m length of first string
1240 * \param t second string
1241 * \param n length of second string
1242 * \param min minimum length of common substrings
1243 * \param hdiff list of horizontal diff ranges to fill
1244 * \param depth recursion depth
1246 * \return 0 if success, nonzero otherwise
1248 static gboolean
1249 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1251 int i;
1252 BRACKET b;
1254 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1255 for (i = 0; i < m && i < n && s[i] == t[i]; i++);
1256 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--);
1258 b[0].off = i;
1259 b[0].len = m - i;
1260 b[1].off = i;
1261 b[1].len = n - i;
1263 /* smartscan (multiple horizontal diff) */
1264 return hdiff_multi (s, t, b, min, hdiff, depth);
1267 /* --------------------------------------------------------------------------------------------- */
1268 /* read line **************************************************************** */
1271 * Check if character is inside horizontal diff limits.
1273 * \param k rank of character inside line
1274 * \param hdiff horizontal diff structure
1275 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1277 * \return TRUE if inside hdiff limits, FALSE otherwise
1279 static int
1280 is_inside (int k, GArray * hdiff, int ord)
1282 size_t i;
1283 BRACKET *b;
1284 for (i = 0; i < hdiff->len; i++)
1286 int start, end;
1287 b = &g_array_index (hdiff, BRACKET, i);
1289 start = (*b)[ord].off;
1290 end = start + (*b)[ord].len;
1291 if (k >= start && k < end)
1293 return 1;
1296 return 0;
1299 /* --------------------------------------------------------------------------------------------- */
1302 * Copy `src' to `dst' expanding tabs.
1304 * \param dst destination buffer
1305 * \param src source buffer
1306 * \param srcsize size of src buffer
1307 * \param base virtual base of this string, needed to calculate tabs
1308 * \param ts tab size
1310 * \return new virtual base
1312 * \note The procedure returns when all bytes are consumed from `src'
1314 static int
1315 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1317 int i;
1318 for (i = 0; srcsize; i++, src++, dst++, srcsize--)
1320 *dst = *src;
1321 if (*src == '\t')
1323 int j = TAB_SKIP (ts, i + base);
1324 i += j - 1;
1325 while (j-- > 0)
1327 *dst++ = ' ';
1329 dst--;
1332 return i + base;
1335 /* --------------------------------------------------------------------------------------------- */
1338 * Copy `src' to `dst' expanding tabs.
1340 * \param dst destination buffer
1341 * \param dstsize size of dst buffer
1342 * \param[in,out] _src source buffer
1343 * \param srcsize size of src buffer
1344 * \param base virtual base of this string, needed to calculate tabs
1345 * \param ts tab size
1347 * \return new virtual base
1349 * \note The procedure returns when all bytes are consumed from `src'
1350 * or `dstsize' bytes are written to `dst'
1351 * \note Upon return, `src' points to the first unwritten character in source
1353 static int
1354 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1356 int i;
1357 const char *src = *_src;
1358 for (i = 0; i < dstsize && srcsize; i++, src++, dst++, srcsize--)
1360 *dst = *src;
1361 if (*src == '\t')
1363 int j = TAB_SKIP (ts, i + base);
1364 if (j > dstsize - i)
1366 j = dstsize - i;
1368 i += j - 1;
1369 while (j-- > 0)
1371 *dst++ = ' ';
1373 dst--;
1376 *_src = src;
1377 return i + base;
1380 /* --------------------------------------------------------------------------------------------- */
1383 * Read line from memory, converting tabs to spaces and padding with spaces.
1385 * \param src buffer to read from
1386 * \param srcsize size of src buffer
1387 * \param dst buffer to read to
1388 * \param dstsize size of dst buffer, excluding trailing null
1389 * \param skip number of characters to skip
1390 * \param ts tab size
1391 * \param show_cr show trailing carriage return as ^M
1393 * \return negative on error, otherwise number of bytes except padding
1395 static int
1396 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr)
1398 int sz = 0;
1399 if (src != NULL)
1401 int i;
1402 char *tmp = dst;
1403 const int base = 0;
1404 for (i = 0; dstsize && srcsize && *src != '\n'; i++, src++, srcsize--)
1406 if (*src == '\t')
1408 int j = TAB_SKIP (ts, i + base);
1409 i += j - 1;
1410 while (j-- > 0)
1412 if (skip > 0)
1414 skip--;
1416 else if (dstsize)
1418 dstsize--;
1419 *dst++ = ' ';
1423 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1425 if (skip == 0 && show_cr)
1427 if (dstsize > 1)
1429 dstsize -= 2;
1430 *dst++ = '^';
1431 *dst++ = 'M';
1433 else
1435 dstsize--;
1436 *dst++ = '.';
1439 break;
1441 else
1443 if (skip > 0)
1445 int utf_ch = 0;
1446 gboolean res;
1447 int w;
1448 skip--;
1449 utf_ch = dview_get_utf ((char *)src, &w, &res);
1450 if (w > 1)
1451 skip += w - 1;
1452 if (!g_unichar_isprint (utf_ch))
1453 utf_ch = '.';
1455 else
1457 dstsize--;
1458 *dst++ = *src;
1462 sz = dst - tmp;
1464 while (dstsize)
1466 dstsize--;
1467 *dst++ = ' ';
1469 *dst = '\0';
1470 return sz;
1473 /* --------------------------------------------------------------------------------------------- */
1476 * Read line from memory and build attribute array.
1478 * \param src buffer to read from
1479 * \param srcsize size of src buffer
1480 * \param dst buffer to read to
1481 * \param dstsize size of dst buffer, excluding trailing null
1482 * \param skip number of characters to skip
1483 * \param ts tab size
1484 * \param show_cr show trailing carriage return as ^M
1485 * \param hdiff horizontal diff structure
1486 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1487 * \param att buffer of attributes
1489 * \return negative on error, otherwise number of bytes except padding
1491 static int
1492 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr,
1493 GArray * hdiff, int ord, char *att)
1495 int sz = 0;
1496 if (src != NULL)
1498 int i, k;
1499 char *tmp = dst;
1500 const int base = 0;
1501 for (i = 0, k = 0; dstsize && srcsize && *src != '\n'; i++, k++, src++, srcsize--)
1503 if (*src == '\t')
1505 int j = TAB_SKIP (ts, i + base);
1506 i += j - 1;
1507 while (j-- > 0)
1509 if (skip)
1511 skip--;
1513 else if (dstsize)
1515 dstsize--;
1516 *att++ = is_inside (k, hdiff, ord);
1517 *dst++ = ' ';
1521 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1523 if (!skip && show_cr)
1525 if (dstsize > 1)
1527 dstsize -= 2;
1528 *att++ = is_inside (k, hdiff, ord);
1529 *dst++ = '^';
1530 *att++ = is_inside (k, hdiff, ord);
1531 *dst++ = 'M';
1533 else
1535 dstsize--;
1536 *att++ = is_inside (k, hdiff, ord);
1537 *dst++ = '.';
1540 break;
1542 else
1544 if (skip)
1546 int utf_ch = 0;
1547 gboolean res;
1548 int w;
1549 skip--;
1550 utf_ch = dview_get_utf ((char *) src, &w, &res);
1551 if (w > 1)
1552 skip += w - 1;
1553 if (!g_unichar_isprint (utf_ch))
1554 utf_ch = '.';
1556 else
1558 dstsize--;
1559 *att++ = is_inside (k, hdiff, ord);
1560 *dst++ = *src;
1564 sz = dst - tmp;
1566 while (dstsize)
1568 dstsize--;
1569 *att++ = 0;
1570 *dst++ = ' ';
1572 *dst = '\0';
1573 return sz;
1576 /* --------------------------------------------------------------------------------------------- */
1579 * Read line from file, converting tabs to spaces and padding with spaces.
1581 * \param f file stream to read from
1582 * \param off offset of line inside file
1583 * \param dst buffer to read to
1584 * \param dstsize size of dst buffer, excluding trailing null
1585 * \param skip number of characters to skip
1586 * \param ts tab size
1587 * \param show_cr show trailing carriage return as ^M
1589 * \return negative on error, otherwise number of bytes except padding
1591 static int
1592 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, int show_cr)
1594 int base = 0;
1595 int old_base = base;
1596 const int amount = dstsize;
1598 size_t useful, offset;
1600 size_t i;
1601 size_t sz;
1603 int lastch = '\0';
1605 const char *q = NULL;
1606 char tmp[BUFSIZ]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1607 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1609 if ((int) sizeof (tmp) < amount || (int) sizeof (tmp) <= dstsize
1610 || (int) sizeof (cvt) < 8 * amount)
1612 /* abnormal, but avoid buffer overflow */
1613 memset (dst, ' ', dstsize);
1614 dst[dstsize] = '\0';
1615 return 0;
1618 f_seek (f, off, SEEK_SET);
1620 while (skip > base)
1622 old_base = base;
1623 if (!(sz = f_gets (tmp, amount, f)))
1625 break;
1627 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1628 if (cvt[base - old_base - 1] == '\n')
1630 q = &cvt[base - old_base - 1];
1631 base = old_base + q - cvt + 1;
1632 break;
1636 useful = base - skip;
1637 offset = skip - old_base;
1639 if (useful < 0)
1641 memset (dst, ' ', dstsize);
1642 dst[dstsize] = '\0';
1643 return 0;
1646 if (useful <= dstsize)
1648 if (useful)
1650 memmove (dst, cvt + offset, useful);
1652 if (q == NULL && (sz = f_gets (tmp, dstsize - useful + 1, f)))
1654 const char *ptr = tmp;
1655 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1656 if (ptr < tmp + sz)
1658 lastch = *ptr;
1661 sz = useful;
1663 else
1665 memmove (dst, cvt + offset, dstsize);
1666 sz = dstsize;
1667 lastch = cvt[offset + dstsize];
1670 dst[sz] = lastch;
1671 for (i = 0; i < sz && dst[i] != '\n'; i++)
1673 if (dst[i] == '\r' && dst[i + 1] == '\n')
1675 if (show_cr)
1677 if (i + 1 < dstsize)
1679 dst[i++] = '^';
1680 dst[i++] = 'M';
1682 else
1684 dst[i++] = '*';
1687 break;
1690 for (; i < dstsize; i++)
1692 dst[i] = ' ';
1694 dst[i] = '\0';
1695 return sz;
1698 /* --------------------------------------------------------------------------------------------- */
1699 /* diff printers et al ****************************************************** */
1701 static void
1702 cc_free_elt (void *elt)
1704 DIFFLN *p = elt;
1705 if (p != NULL)
1707 g_free (p->p);
1711 /* --------------------------------------------------------------------------------------------- */
1713 static int
1714 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1716 GArray *a = ((PRINTER_CTX *) ctx)->a;
1717 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1718 if (ch)
1720 DIFFLN p;
1721 p.p = NULL;
1722 p.ch = ch;
1723 p.line = line;
1724 p.u.off = off;
1725 if (dsrc == DATA_SRC_MEM && line)
1727 if (sz && str[sz - 1] == '\n')
1729 sz--;
1731 if (sz > 0)
1732 p.p = g_strndup (str, sz);
1733 p.u.len = sz;
1735 g_array_append_val (a, p);
1737 else if (dsrc == DATA_SRC_MEM)
1739 DIFFLN *p;
1740 p = &g_array_index (a, DIFFLN, a->len - 1);
1741 if (sz && str[sz - 1] == '\n')
1743 sz--;
1745 if (sz > 0)
1747 size_t new_size = p->u.len + sz;
1748 char *q = g_realloc (p->p, new_size);
1749 memcpy (q + p->u.len, str, sz);
1750 p->p = q;
1752 p->u.len += sz;
1754 if (dsrc == DATA_SRC_TMP && (line || !ch))
1756 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1757 f_write (f, str, sz);
1759 return 0;
1762 /* --------------------------------------------------------------------------------------------- */
1764 static int
1765 redo_diff (WDiff * dview)
1767 FBUF *const *f = dview->f;
1769 PRINTER_CTX ctx;
1770 GArray *ops;
1771 int ndiff;
1772 int rv;
1774 char extra[256];
1776 extra[0] = '\0';
1777 if (dview->opt.quality == 2)
1779 strcat (extra, " -d");
1781 if (dview->opt.quality == 1)
1783 strcat (extra, " --speed-large-files");
1785 if (dview->opt.strip_trailing_cr)
1787 strcat (extra, " --strip-trailing-cr");
1789 if (dview->opt.ignore_tab_expansion)
1791 strcat (extra, " -E");
1793 if (dview->opt.ignore_space_change)
1795 strcat (extra, " -b");
1797 if (dview->opt.ignore_all_space)
1799 strcat (extra, " -w");
1801 if (dview->opt.ignore_case)
1803 strcat (extra, " -i");
1806 if (dview->dsrc != DATA_SRC_MEM)
1808 f_reset (f[0]);
1809 f_reset (f[1]);
1812 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1813 ndiff = dff_execute (dview->args, extra, dview->file[0], dview->file[1], ops);
1814 if (ndiff < 0)
1816 g_array_free (ops, TRUE);
1817 return -1;
1820 ctx.dsrc = dview->dsrc;
1822 rv = 0;
1823 ctx.a = dview->a[0];
1824 ctx.f = f[0];
1825 rv |= dff_reparse (0, dview->file[0], ops, printer, &ctx);
1827 ctx.a = dview->a[1];
1828 ctx.f = f[1];
1829 rv |= dff_reparse (1, dview->file[1], ops, printer, &ctx);
1831 g_array_free (ops, TRUE);
1833 if (rv != 0 || dview->a[0]->len != dview->a[1]->len)
1834 return -1;
1836 if (dview->dsrc == DATA_SRC_TMP)
1838 f_trunc (f[0]);
1839 f_trunc (f[1]);
1842 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1844 dview->hdiff = g_ptr_array_new ();
1845 if (dview->hdiff != NULL)
1847 size_t i;
1848 const DIFFLN *p;
1849 const DIFFLN *q;
1850 for (i = 0; i < dview->a[0]->len; i++)
1852 GArray *h = NULL;
1853 p = &g_array_index (dview->a[0], DIFFLN, i);
1854 q = &g_array_index (dview->a[1], DIFFLN, i);
1855 if (p->line && q->line && p->ch == CHG_CH)
1857 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1858 if (h != NULL)
1860 gboolean runresult =
1861 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1862 HDIFF_DEPTH);
1863 if (!runresult)
1865 g_array_free (h, TRUE);
1866 h = NULL;
1870 g_ptr_array_add (dview->hdiff, h);
1874 return ndiff;
1877 /* --------------------------------------------------------------------------------------------- */
1879 static void
1880 destroy_hdiff (WDiff * dview)
1882 if (dview->hdiff != NULL)
1884 int i;
1885 int len = dview->a[0]->len;
1886 for (i = 0; i < len; i++)
1888 GArray *h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1889 if (h != NULL)
1891 g_array_free (h, TRUE);
1894 g_ptr_array_free (dview->hdiff, TRUE);
1895 dview->hdiff = NULL;
1898 mc_search_free (dview->search.handle);
1899 dview->search.handle = NULL;
1900 g_free (dview->search.last_string);
1901 dview->search.last_string = NULL;
1904 /* --------------------------------------------------------------------------------------------- */
1905 /* stuff ******************************************************************** */
1907 static int
1908 get_digits (unsigned int n)
1910 int d = 1;
1911 while (n /= 10)
1913 d++;
1915 return d;
1918 /* --------------------------------------------------------------------------------------------- */
1920 static int
1921 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1923 const DIFFLN *p;
1925 *linenum = 0;
1926 *lineofs = 0;
1928 if (a->len)
1930 if (pos >= a->len)
1932 pos = a->len - 1;
1935 p = &g_array_index (a, DIFFLN, pos);
1937 if (!p->line)
1939 int n;
1940 for (n = pos; n > 0; n--)
1942 p--;
1943 if (p->line)
1945 break;
1948 *lineofs = pos - n + 1;
1951 *linenum = p->line;
1953 return 0;
1956 /* --------------------------------------------------------------------------------------------- */
1958 static int
1959 calc_nwidth (const GArray ** const a)
1961 int l1, o1;
1962 int l2, o2;
1963 get_line_numbers (a[0], a[0]->len - 1, &l1, &o1);
1964 get_line_numbers (a[1], a[1]->len - 1, &l2, &o2);
1965 if (l1 < l2)
1967 l1 = l2;
1969 return get_digits (l1);
1972 /* --------------------------------------------------------------------------------------------- */
1974 static int
1975 find_prev_hunk (const GArray * a, int pos)
1977 #if 1
1978 while (pos > 0 && ((DIFFLN *) &g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1980 pos--;
1982 while (pos > 0 && ((DIFFLN *) &g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1984 pos--;
1986 while (pos > 0 && ((DIFFLN *) &g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1988 pos--;
1990 if (pos > 0 && (size_t) pos < a->len)
1991 pos++;
1992 #else
1993 while (pos > 0 && ((DIFFLN *) &g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1995 pos--;
1997 while (pos > 0 && ((DIFFLN *) &g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1999 pos--;
2001 #endif
2003 return pos;
2006 /* --------------------------------------------------------------------------------------------- */
2008 static size_t
2009 find_next_hunk (const GArray * a, size_t pos)
2011 while (pos < a->len && ((DIFFLN *) &g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
2013 pos++;
2015 while (pos < a->len && ((DIFFLN *) &g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
2017 pos++;
2019 return pos;
2023 * Find start and end lines of the current hunk.
2025 * \param dview - widget WDiff
2026 * \return boolean and
2027 * start_line1 first line of current hunk (file[0])
2028 * end_line1 last line of current hunk (file[0])
2029 * start_line1 first line of current hunk (file[0])
2030 * end_line1 last line of current hunk (file[0])
2032 static int
2033 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
2035 const GArray *a0 = dview->a[0];
2036 const GArray *a1 = dview->a[1];
2037 size_t pos;
2038 int ch;
2039 int res = 0;
2041 *start_line1 = 1;
2042 *start_line2 = 1;
2043 *end_line1 = 1;
2044 *end_line2 = 1;
2046 pos = dview->skip_rows;
2047 ch = ((DIFFLN *) &g_array_index (a0, DIFFLN, pos))->ch;
2048 if (ch != EQU_CH)
2050 switch (ch)
2052 case ADD_CH:
2053 res = DIFF_DEL;
2054 break;
2055 case DEL_CH:
2056 res = DIFF_ADD;
2057 break;
2058 case CHG_CH:
2059 res = DIFF_CHG;
2060 break;
2062 while (pos > 0 && ((DIFFLN *) &g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2064 pos--;
2066 if (pos > 0)
2068 *start_line1 = ((DIFFLN *) &g_array_index (a0, DIFFLN, pos))->line + 1;
2069 *start_line2 = ((DIFFLN *) &g_array_index (a1, DIFFLN, pos))->line + 1;
2071 pos = dview->skip_rows;
2072 while (pos < a0->len && ((DIFFLN *) &g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2074 int l0, l1;
2075 l0 = ((DIFFLN *) &g_array_index (a0, DIFFLN, pos))->line;
2076 l1 = ((DIFFLN *) &g_array_index (a1, DIFFLN, pos))->line;
2077 if (l0 > 0)
2078 *end_line1 = max (*start_line1, l0);
2079 if (l1 > 0)
2080 *end_line2 = max (*start_line2, l1);
2081 pos++;
2084 return res;
2087 static void
2088 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1)
2090 int line;
2091 char buf[BUF_10K];
2092 FILE *f0;
2093 f0 = fopen (dview->file[0], "r");
2094 line = 0;
2095 while (fgets (buf, sizeof (buf), f0) && line < from1 - 1)
2097 line++;
2098 fputs (buf, merge_file);
2100 while (fgets (buf, sizeof (buf), f0))
2102 line++;
2103 if (line >= to1)
2104 fputs (buf, merge_file);
2106 fclose (f0);
2109 static void
2110 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2)
2112 int line;
2113 char buf[BUF_10K];
2114 FILE *f0;
2115 FILE *f1;
2116 f0 = fopen (dview->file[0], "r");
2117 f1 = fopen (dview->file[1], "r");
2118 line = 0;
2119 while (fgets (buf, sizeof (buf), f0) && line < from1 - 1)
2121 line++;
2122 fputs (buf, merge_file);
2124 line = 0;
2125 while (fgets (buf, sizeof (buf), f1) && line <= to2)
2127 line++;
2128 if (line >= from2)
2129 fputs (buf, merge_file);
2131 while (fgets (buf, sizeof (buf), f0))
2133 fputs (buf, merge_file);
2135 fclose (f0);
2136 fclose (f1);
2139 static void
2140 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2)
2142 int line1, line2;
2143 char buf[BUF_10K];
2144 FILE *f0;
2145 FILE *f1;
2146 f0 = fopen (dview->file[0], "r");
2147 f1 = fopen (dview->file[1], "r");
2148 line1 = 0;
2149 while (fgets (buf, sizeof (buf), f0) && line1 < from1 - 1)
2151 line1++;
2152 fputs (buf, merge_file);
2154 line2 = 0;
2155 while (fgets (buf, sizeof (buf), f1) && line2 <= to2)
2157 line2++;
2158 if (line2 >= from2)
2159 fputs (buf, merge_file);
2161 while (fgets (buf, sizeof (buf), f0))
2163 line1++;
2164 if (line1 > to1)
2165 fputs (buf, merge_file);
2167 fclose (f0);
2168 fclose (f1);
2171 static void
2172 do_merge_hunk (WDiff * dview)
2174 int from1, to1, from2, to2;
2175 int res;
2176 int hunk;
2178 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2179 if (hunk > 0)
2181 int merge_file_fd;
2182 FILE *merge_file;
2183 char *merge_file_name = NULL;
2185 if (!dview->merged)
2187 dview->merged = mc_util_make_backup_if_possible (dview->file[0], "~~~");
2188 if (!dview->merged)
2190 message (D_ERROR, MSG_ERROR,
2191 _(" Cannot create backup file \n %s%s \n %s "),
2192 dview->file[0], "~~~", unix_error_string (errno));
2193 return;
2198 merge_file_fd = mc_mkstemps (&merge_file_name, "mcmerge", NULL);
2199 if (merge_file_fd == -1)
2201 message (D_ERROR, MSG_ERROR,
2202 _(" Cannot create temporary merge file \n %s "),
2203 unix_error_string (errno));
2204 return;
2207 merge_file = fdopen (merge_file_fd, "w");
2209 switch (hunk)
2211 case DIFF_DEL:
2212 dview_remove_hunk (dview, merge_file, from1, to1);
2213 break;
2214 case DIFF_ADD:
2215 dview_add_hunk (dview, merge_file, from1, from2, to2);
2216 break;
2217 case DIFF_CHG:
2218 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2);
2219 break;
2221 fflush (merge_file);
2222 fclose (merge_file);
2223 res = rewrite_backup_content (merge_file_name, dview->file[0]);
2224 unlink (merge_file_name);
2225 g_free (merge_file_name);
2229 /* --------------------------------------------------------------------------------------------- */
2230 /* view routines and callbacks ********************************************** */
2232 static void
2233 dview_compute_split (WDiff * dview, int i)
2235 dview->bias += i;
2236 if (dview->bias < 2 - dview->half1)
2238 dview->bias = 2 - dview->half1;
2240 if (dview->bias > dview->half2 - 2)
2242 dview->bias = dview->half2 - 2;
2246 /* --------------------------------------------------------------------------------------------- */
2248 static void
2249 dview_compute_areas (WDiff * dview)
2251 dview->height = LINES - 2;
2252 dview->half1 = COLS / 2;
2253 dview->half2 = COLS - dview->half1;
2255 dview_compute_split (dview, 0);
2258 /* --------------------------------------------------------------------------------------------- */
2260 static int
2261 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2262 const char *label1, const char *label2, DSRC dsrc)
2264 int ndiff;
2265 FBUF *f[2];
2267 f[0] = NULL;
2268 f[1] = NULL;
2270 if (dsrc == DATA_SRC_TMP)
2272 f[0] = f_temp ();
2273 if (f[0] == NULL)
2275 goto err_2;
2277 f[1] = f_temp ();
2278 if (f[1] == NULL)
2280 f_close (f[0]);
2281 goto err_2;
2284 if (dsrc == DATA_SRC_ORG)
2286 f[0] = f_open (file1, O_RDONLY);
2287 if (f[0] == NULL)
2289 goto err_2;
2291 f[1] = f_open (file2, O_RDONLY);
2292 if (f[1] == NULL)
2294 f_close (f[0]);
2295 goto err_2;
2299 dview->args = args;
2300 dview->file[0] = file1;
2301 dview->file[1] = file2;
2302 dview->label[0] = label1;
2303 dview->label[1] = label2;
2304 dview->f[0] = f[0];
2305 dview->f[1] = f[1];
2306 dview->hdiff = NULL;
2307 dview->dsrc = dsrc;
2308 dview->converter = str_cnv_from_term;
2309 dview_set_codeset (dview);
2311 dview->a[0] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2312 dview->a[1] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2314 ndiff = redo_diff (dview);
2315 if (ndiff < 0)
2317 g_array_foreach (dview->a[0], DIFFLN, cc_free_elt);
2318 g_array_free (dview->a[0], TRUE);
2319 g_array_foreach (dview->a[1], DIFFLN, cc_free_elt);
2320 g_array_free (dview->a[1], TRUE);
2321 goto err_3;
2324 dview->ndiff = ndiff;
2326 dview->view_quit = 0;
2328 dview->bias = 0;
2329 dview->new_frame = 1;
2330 dview->skip_rows = 0;
2331 dview->skip_cols = 0;
2332 dview->display_symbols = 0;
2333 dview->display_numbers = 0;
2334 dview->show_cr = 1;
2335 dview->tab_size = 8;
2336 dview->ord = 0;
2337 dview->full = 0;
2339 dview->search.handle=NULL;
2340 dview->search.last_string=NULL;
2341 dview->search.last_found_line = -1;
2342 dview->search.last_accessed_num_line = 0;
2345 dview->opt.quality = 0;
2346 dview->opt.strip_trailing_cr = 0;
2347 dview->opt.ignore_tab_expansion = 0;
2348 dview->opt.ignore_space_change = 0;
2349 dview->opt.ignore_all_space = 0;
2350 dview->opt.ignore_case = 0;
2352 dview_compute_areas (dview);
2354 return 0;
2356 err_3:
2357 if (dsrc != DATA_SRC_MEM)
2359 f_close (f[1]);
2360 f_close (f[0]);
2362 err_2:
2363 return -1;
2366 /* --------------------------------------------------------------------------------------------- */
2368 static void
2369 dview_reread (WDiff * dview)
2371 int ndiff = dview->ndiff;
2372 destroy_hdiff (dview);
2374 g_array_foreach (dview->a[0], DIFFLN, cc_free_elt);
2375 g_array_free (dview->a[0], TRUE);
2376 g_array_foreach (dview->a[1], DIFFLN, cc_free_elt);
2377 g_array_free (dview->a[1], TRUE);
2379 dview->a[0] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2380 dview->a[1] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2382 ndiff = redo_diff (dview);
2383 if (ndiff >= 0)
2385 dview->ndiff = ndiff;
2389 /* --------------------------------------------------------------------------------------------- */
2390 #if 0
2391 static void
2392 dview_reinit (WDiff * dview)
2394 const char *quality_str[] = {
2395 N_("&Normal"),
2396 N_("&Fastest (Assume large files)"),
2397 N_("&Minimal (Find a smaller set of change)")
2400 QuickWidget diffopt_widgets[] = {
2401 QUICK_BUTTON (6, 10, 14, OPTY, N_("&Cancel"), B_CANCEL, NULL),
2402 QUICK_BUTTON (2, 10, 14, OPTY, N_("&OK"), B_ENTER, NULL),
2404 QUICK_CHECKBOX (3, OPTX, 12, OPTY,
2405 N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr),
2406 QUICK_CHECKBOX (3, OPTX, 11, OPTY,
2407 N_("Ignore all &whitespace"), &dview->opt.ignore_all_space),
2408 QUICK_CHECKBOX (3, OPTX, 10, OPTY,
2409 N_("Ignore &space change"), &dview->opt.ignore_space_change),
2410 QUICK_CHECKBOX (3, OPTX, 9, OPTY,
2411 N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion),
2412 QUICK_CHECKBOX (3, OPTX, 8, OPTY,
2413 N_("&Ignore case"), &dview->opt.ignore_case),
2414 QUICK_LABEL (3, OPTX, 7, OPTY, N_("Diff extra options")),
2415 QUICK_RADIO (3, OPTX, 3, OPTY,
2416 3, (const char **) quality_str, (int *) &dview->opt.quality),
2417 QUICK_LABEL (3, OPTX, 2, OPTY, N_("Diff algorithm")),
2419 QUICK_END
2422 QuickDialog diffopt = {
2423 OPTX, OPTY, -1, -1,
2424 N_("Diff Options"), "[Diff Options]",
2425 diffopt_widgets, 0
2428 if (quick_dialog (&diffopt) != B_CANCEL)
2430 dview_reread (dview);
2433 #endif
2434 /* --------------------------------------------------------------------------------------------- */
2436 static void
2437 dview_fini (WDiff * dview)
2439 if (dview->dsrc != DATA_SRC_MEM)
2441 f_close (dview->f[1]);
2442 f_close (dview->f[0]);
2445 if (dview->converter != str_cnv_from_term)
2446 str_close_conv (dview->converter);
2448 destroy_hdiff (dview);
2449 g_array_foreach (dview->a[0], DIFFLN, cc_free_elt);
2450 g_array_free (dview->a[0], TRUE);
2451 g_array_foreach (dview->a[1], DIFFLN, cc_free_elt);
2452 g_array_free (dview->a[1], TRUE);
2454 dview->a[1] = NULL;
2455 dview->a[0] = NULL;
2458 /* --------------------------------------------------------------------------------------------- */
2460 static int
2461 dview_display_file (const WDiff * dview, int ord, int r, int c, int height, int width)
2463 size_t i, k;
2464 int j;
2465 char buf[BUFSIZ];
2466 FBUF *f = dview->f[ord];
2467 int skip = dview->skip_cols;
2468 int display_symbols = dview->display_symbols;
2469 int display_numbers = dview->display_numbers;
2470 int show_cr = dview->show_cr;
2471 int tab_size = dview->tab_size;
2472 const DIFFLN *p;
2473 int nwidth = display_numbers;
2474 int xwidth = display_symbols + display_numbers;
2475 if (xwidth)
2477 if (xwidth > width && display_symbols)
2479 xwidth--;
2480 display_symbols = 0;
2482 if (xwidth > width && display_numbers)
2484 xwidth = width;
2485 display_numbers = width;
2488 xwidth++;
2490 c += xwidth;
2491 width -= xwidth;
2493 if (width < 0)
2495 width = 0;
2499 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2501 /* abnormal, but avoid buffer overflow */
2502 return -1;
2505 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2507 int ch, next_ch, col;
2508 size_t cnt;
2509 p = (DIFFLN *) &g_array_index (dview->a[ord], DIFFLN, i);
2510 ch = p->ch;
2511 tty_setcolor (NORMAL_COLOR);
2512 if (display_symbols)
2514 tty_gotoyx (r + j, c - 2);
2515 tty_print_char (ch);
2517 if (p->line)
2519 if (display_numbers)
2521 tty_gotoyx (r + j, c - xwidth);
2522 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2523 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2525 if (ch == ADD_CH)
2527 tty_setcolor (DFF_ADD_COLOR);
2529 if (ch == CHG_CH)
2531 tty_setcolor (DFF_CHG_COLOR);
2533 if (f == NULL)
2535 if (i == (size_t) dview->search.last_found_line)
2537 tty_setcolor (MARKED_SELECTED_COLOR);
2539 else
2541 if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2543 char att[BUFSIZ];
2544 if (dview->utf8)
2545 k = dview_str_utf8_offset_to_pos (p->p, width);
2546 else
2547 k = width;
2548 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2549 g_ptr_array_index (dview->hdiff, i), ord, att);
2550 tty_gotoyx (r + j, c);
2551 col = 0;
2552 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2554 int w;
2555 gboolean ch_res;
2556 if (dview->utf8)
2558 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2559 if (w > 1)
2560 cnt += w - 1;
2561 if (!g_unichar_isprint (next_ch))
2562 next_ch = '.';
2564 else
2565 next_ch = dview_get_byte (buf + cnt, &ch_res);
2566 if (ch_res)
2568 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2569 #ifdef HAVE_CHARSET
2570 if (utf8_display) {
2571 if (!dview->utf8) {
2572 next_ch = convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2574 } else if (dview->utf8)
2575 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2576 else
2577 #endif
2578 next_ch = convert_to_display_c (next_ch);
2580 tty_print_anychar (next_ch);
2581 col++;
2584 continue;
2586 else if (ch == CHG_CH)
2588 tty_setcolor (DFF_CHH_COLOR);
2591 if (dview->utf8)
2592 k = dview_str_utf8_offset_to_pos (p->p, width);
2593 else
2594 k = width;
2595 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2597 else
2599 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2602 else
2604 if (display_numbers)
2606 tty_gotoyx (r + j, c - xwidth);
2607 memset (buf, ' ', display_numbers);
2608 buf[display_numbers] = '\0';
2609 tty_print_string (buf);
2611 if (ch == DEL_CH)
2613 tty_setcolor (DFF_DEL_COLOR);
2615 if (ch == CHG_CH)
2617 tty_setcolor (DFF_CHD_COLOR);
2619 memset (buf, ' ', width);
2620 buf[width] = '\0';
2622 tty_gotoyx (r + j, c);
2623 /* tty_print_nstring (buf, width); */
2624 col = 0;
2625 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2627 int w;
2628 gboolean ch_res;
2629 if (dview->utf8)
2631 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2632 if (w > 1)
2633 cnt += w - 1;
2634 if (!g_unichar_isprint (next_ch))
2635 next_ch = '.';
2637 else
2638 next_ch = dview_get_byte (buf + cnt, &ch_res);
2639 if (ch_res)
2641 #ifdef HAVE_CHARSET
2642 if (utf8_display) {
2643 if (!dview->utf8) {
2644 next_ch = convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2646 } else if (dview->utf8)
2647 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2648 else
2649 #endif
2650 next_ch = convert_to_display_c (next_ch);
2652 tty_print_anychar (next_ch);
2653 col++;
2656 continue;
2658 tty_setcolor (NORMAL_COLOR);
2659 k = width;
2660 if (width < xwidth - 1)
2662 k = xwidth - 1;
2664 memset (buf, ' ', k);
2665 buf[k] = '\0';
2666 for (; j < height; j++)
2668 if (xwidth)
2670 tty_gotoyx (r + j, c - xwidth);
2671 /* tty_print_nstring (buf, xwidth - 1); */
2672 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2674 tty_gotoyx (r + j, c);
2675 /* tty_print_nstring (buf, width); */
2676 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2679 return 0;
2682 /* --------------------------------------------------------------------------------------------- */
2684 static void
2685 dview_status (const WDiff * dview, int ord, int width, int c)
2687 int skip_rows = dview->skip_rows;
2688 int skip_cols = dview->skip_cols;
2690 char buf[BUFSIZ];
2691 int filename_width;
2692 int linenum, lineofs;
2694 tty_setcolor (SELECTED_COLOR);
2696 tty_gotoyx (0, c);
2697 get_line_numbers (dview->a[ord], skip_rows, &linenum, &lineofs);
2699 filename_width = width - 22;
2700 if (filename_width < 8)
2702 filename_width = 8;
2704 if (filename_width >= (int) sizeof (buf))
2706 /* abnormal, but avoid buffer overflow */
2707 filename_width = sizeof (buf) - 1;
2709 trim (strip_home_and_password (dview->label[ord]), buf, filename_width);
2710 if (ord == 0)
2712 tty_printf ("%-*s %6d+%-4d Col %-4d ", filename_width, buf, linenum, lineofs, skip_cols);
2714 else
2716 tty_printf ("%-*s %6d+%-4d Dif %-4d ", filename_width, buf, linenum, lineofs, dview->ndiff);
2720 /* --------------------------------------------------------------------------------------------- */
2722 static void
2723 dview_redo (WDiff * dview)
2725 if (dview->display_numbers)
2727 int old = dview->display_numbers;
2728 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2729 dview->new_frame = (old != dview->display_numbers);
2731 dview_reread (dview);
2734 /* --------------------------------------------------------------------------------------------- */
2736 static void
2737 dview_edit (WDiff * dview, int ord)
2739 int linenum, lineofs;
2741 if (dview->dsrc == DATA_SRC_TMP)
2743 error_dialog (_("Edit"), _(" Edit is disabled "));
2744 return;
2747 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2748 do_edit_at_line (dview->file[ord], linenum);
2749 dview_redo (dview);
2750 dview_update (dview);
2753 /* --------------------------------------------------------------------------------------------- */
2755 static void
2756 dview_goto_cmd (WDiff * dview, int ord)
2758 static const char *title[2] = { " Goto line (left) ", " Goto line (right) " };
2759 static char prev[256];
2760 /* XXX some statics here, to be remembered between runs */
2762 int newline;
2763 char *input;
2765 input = input_dialog (_(title[ord]), _(" Enter line: "), MC_HISTORY_YDIFF_GOTO_LINE, prev);
2766 if (input != NULL)
2768 const char *s = input;
2769 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2771 size_t i = 0;
2772 if (newline > 0)
2774 const DIFFLN *p;
2775 for (; i < dview->a[ord]->len; i++)
2777 p = &g_array_index (dview->a[ord], DIFFLN, i);
2778 if (p->line == newline)
2780 break;
2784 dview->skip_rows = dview->search.last_accessed_num_line = i;
2785 snprintf (prev, sizeof (prev), "%d", newline);
2787 g_free (input);
2791 /* --------------------------------------------------------------------------------------------- */
2793 static void
2794 dview_labels (WDiff * dview)
2796 Dlg_head *h = dview->widget.parent;
2797 WButtonBar *b = find_buttonbar (h);
2799 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, (Widget *) dview);
2800 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, (Widget *) dview);
2801 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, (Widget *) dview);
2802 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, (Widget *) dview);
2803 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, (Widget *) dview);
2804 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, (Widget *) dview);
2808 /* --------------------------------------------------------------------------------------------- */
2810 static int
2811 dview_event (Gpm_Event * event, void *x)
2813 WDiff *dview = (WDiff *) x;
2814 int result = MOU_NORMAL;
2816 /* We are not interested in the release events */
2817 if (!(event->type & (GPM_DOWN | GPM_DRAG)))
2819 return result;
2822 /* Wheel events */
2823 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
2825 dview->skip_rows -= 2;
2826 dview->search.last_accessed_num_line = dview->skip_rows;
2827 dview_update (dview);
2828 return result;
2830 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
2832 dview->skip_rows += 2;
2833 dview->search.last_accessed_num_line = dview->skip_rows;
2834 dview_update (dview);
2835 return result;
2838 return result;
2841 static gboolean
2842 dview_save (WDiff * dview)
2844 gboolean res = TRUE;
2845 if (!dview->merged)
2846 return res;
2847 res = mc_util_unlink_backup_if_possible (dview->file[0], "~~~");
2848 dview->merged = !res;
2849 return res;
2852 static void
2853 dview_do_save (WDiff * dview)
2855 (void) dview_save (dview);
2858 static void
2859 dview_save_options (WDiff * dview)
2861 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
2862 dview->display_symbols != 0 ? TRUE : FALSE);
2863 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
2864 dview->display_numbers != 0 ? TRUE : FALSE);
2865 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
2867 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
2869 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
2870 dview->opt.strip_trailing_cr);
2871 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
2872 dview->opt.ignore_all_space);
2873 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
2874 dview->opt.ignore_space_change);
2875 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
2876 dview->opt.ignore_tab_expansion);
2877 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
2880 static void
2881 dview_load_options (WDiff * dview)
2883 gboolean show_numbers, show_symbols;
2885 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
2886 if (show_symbols)
2887 dview->display_symbols = 1;
2888 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
2889 if (show_numbers)
2890 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
2891 dview->tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
2893 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
2895 dview->opt.strip_trailing_cr =
2896 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
2897 dview->opt.ignore_all_space =
2898 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
2899 dview->opt.ignore_space_change =
2900 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
2901 dview->opt.ignore_tab_expansion =
2902 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
2903 dview->opt.ignore_case =
2904 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
2906 dview->new_frame = 1;
2910 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2911 * ask user.
2913 static gboolean
2914 dview_ok_to_exit (WDiff * dview)
2916 gboolean res = TRUE;
2917 if (!dview->merged)
2918 return res;
2919 switch (query_dialog
2920 (_("Quit"), _(" File was modified, Save with exit? "), D_NORMAL, 2, _("&Yes"), _("&No")))
2922 case -1:
2923 res = FALSE;
2924 break;
2925 case 0:
2926 res = TRUE;
2927 (void) dview_save (dview);
2928 break;
2929 case 1:
2930 res = TRUE;
2931 if (mc_util_restore_from_backup_if_possible (dview->file[0], "~~~"))
2932 res = mc_util_unlink_backup_if_possible (dview->file[0], "~~~");
2933 break;
2935 return res;
2938 /* --------------------------------------------------------------------------------------------- */
2940 static cb_ret_t
2941 dview_execute_cmd (WDiff * dview, unsigned long command)
2943 cb_ret_t res = MSG_HANDLED;
2944 switch (command)
2946 case CK_DiffHelp:
2947 interactive_display (NULL, "[Diff Viewer]");
2948 break;
2949 case CK_DiffDisplaySymbols:
2950 dview->display_symbols ^= 1;
2951 dview->new_frame = 1;
2952 break;
2953 case CK_DiffDisplayNumbers:
2954 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
2955 dview->new_frame = 1;
2956 break;
2957 case CK_DiffFull:
2958 dview->full ^= 1;
2959 dview->new_frame = 1;
2960 break;
2961 case CK_DiffEqual:
2962 if (!dview->full)
2964 dview->bias = 0;
2965 dview->new_frame = 1;
2967 break;
2968 case CK_DiffSplitMore:
2969 if (!dview->full)
2971 dview_compute_split (dview, 1);
2972 dview->new_frame = 1;
2974 break;
2976 case CK_DiffSplitLess:
2977 if (!dview->full)
2979 dview_compute_split (dview, -1);
2980 dview->new_frame = 1;
2982 break;
2983 case CK_DiffSetTab2:
2984 dview->tab_size = 2;
2985 break;
2986 case CK_DiffSetTab3:
2987 dview->tab_size = 3;
2988 break;
2989 case CK_DiffSetTab4:
2990 dview->tab_size = 4;
2991 break;
2992 case CK_DiffSetTab8:
2993 dview->tab_size = 8;
2994 break;
2995 case CK_DiffSwapPanel:
2996 dview->ord ^= 1;
2997 break;
2998 case CK_DiffRedo:
2999 dview_redo (dview);
3000 break;
3001 case CK_DiffNextHunk:
3002 dview->skip_rows = dview->search.last_accessed_num_line = find_next_hunk (dview->a[0], dview->skip_rows);
3003 break;
3004 case CK_DiffPrevHunk:
3005 dview->skip_rows = dview->search.last_accessed_num_line = find_prev_hunk (dview->a[0], dview->skip_rows);
3006 break;
3007 case CK_DiffGoto:
3008 dview_goto_cmd (dview, TRUE);
3009 break;
3010 case CK_DiffEditCurrent:
3011 dview_edit (dview, dview->ord);
3012 break;
3013 case CK_DiffMergeCurrentHunk:
3014 do_merge_hunk (dview);
3015 dview_redo (dview);
3016 break;
3017 case CK_DiffEditOther:
3018 dview_edit (dview, dview->ord ^ 1);
3019 break;
3020 case CK_DiffSearch:
3021 dview_search_cmd (dview);
3022 break;
3023 case CK_DiffContinueSearch:
3024 dview_continue_search_cmd (dview);
3025 break;
3026 case CK_DiffBOF:
3027 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3028 break;
3029 case CK_DiffEOF:
3030 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[0]->len - 1;
3031 break;
3032 case CK_DiffUp:
3033 dview->skip_rows--;
3034 dview->search.last_accessed_num_line = dview->skip_rows;
3035 break;
3036 case CK_DiffDown:
3037 dview->skip_rows++;
3038 dview->search.last_accessed_num_line = dview->skip_rows;
3039 break;
3040 case CK_DiffPageDown:
3041 dview->skip_rows += dview->height - 2;
3042 dview->search.last_accessed_num_line = dview->skip_rows;
3043 break;
3044 case CK_DiffPageUp:
3045 dview->skip_rows -= dview->height - 2;
3046 dview->search.last_accessed_num_line = dview->skip_rows;
3047 break;
3048 case CK_DiffLeft:
3049 dview->skip_cols--;
3050 break;
3051 case CK_DiffRight:
3052 dview->skip_cols++;
3053 break;
3054 case CK_DiffQuickLeft:
3055 dview->skip_cols -= 8;
3056 break;
3057 case CK_DiffQuickRight:
3058 dview->skip_cols += 8;
3059 break;
3060 case CK_DiffHome:
3061 dview->skip_cols = 0;
3062 break;
3063 case CK_ShowCommandLine:
3064 view_other_cmd ();
3065 break;
3066 case CK_DiffQuit:
3067 dview->view_quit = 1;
3068 break;
3069 case CK_DiffSave:
3070 dview_do_save (dview);
3071 break;
3072 case CK_SelectCodepage:
3073 dview_select_encoding (dview);
3074 dview_reread (dview);
3075 tty_touch_screen ();
3076 repaint_screen ();
3077 break;
3078 default:
3079 res = MSG_NOT_HANDLED;
3081 return res;
3084 /* --------------------------------------------------------------------------------------------- */
3086 static cb_ret_t
3087 dview_handle_key (WDiff * dview, int key)
3089 unsigned long command;
3091 key = convert_from_input_c (key);
3093 command = lookup_keymap_command (diff_map, key);
3094 if ((command != CK_Ignore_Key) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3095 return MSG_HANDLED;
3097 /* Key not used */
3098 return MSG_NOT_HANDLED;
3101 /* --------------------------------------------------------------------------------------------- */
3103 static cb_ret_t
3104 dview_callback (Widget * w, widget_msg_t msg, int parm)
3106 WDiff *dview = (WDiff *) w;
3107 Dlg_head *h = dview->widget.parent;
3108 cb_ret_t i;
3110 switch (msg)
3112 case WIDGET_INIT:
3113 dview_labels (dview);
3114 dview_load_options (dview);
3115 dview_update (dview);
3116 return MSG_HANDLED;
3118 case WIDGET_DRAW:
3119 dview->new_frame = 1;
3120 dview_update (dview);
3121 return MSG_HANDLED;
3123 case WIDGET_KEY:
3124 i = dview_handle_key (dview, parm);
3125 if (dview->view_quit)
3126 dlg_stop (h);
3127 else
3128 dview_update (dview);
3129 return i;
3131 case WIDGET_COMMAND:
3132 i = dview_execute_cmd (dview, parm);
3133 if (dview->view_quit)
3134 dlg_stop (h);
3135 else
3136 dview_update (dview);
3137 return i;
3139 case WIDGET_DESTROY:
3140 dview_save_options (dview);
3141 dview_fini (dview);
3142 return MSG_HANDLED;
3144 default:
3145 return default_proc (msg, parm);
3149 /* --------------------------------------------------------------------------------------------- */
3151 static void
3152 dview_adjust_size (Dlg_head * h)
3154 WDiff *dview;
3155 WButtonBar *bar;
3157 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3158 dview = (WDiff *) find_widget_type (h, dview_callback);
3159 bar = find_buttonbar (h);
3160 widget_set_size (&dview->widget, 0, 0, LINES - 1, COLS);
3161 widget_set_size ((Widget *) bar, LINES - 1, 0, 1, COLS);
3163 dview_compute_areas (dview);
3166 /* --------------------------------------------------------------------------------------------- */
3168 static cb_ret_t
3169 dview_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
3171 WDiff *dview = (WDiff *) data;
3173 switch (msg)
3175 case DLG_RESIZE:
3176 dview_adjust_size (h);
3177 return MSG_HANDLED;
3179 case DLG_ACTION:
3180 /* command from buttonbar */
3181 return send_message ((Widget *) dview, WIDGET_COMMAND, parm);
3183 case DLG_VALIDATE:
3184 dview = (WDiff *) find_widget_type (h, dview_callback);
3185 if (!dview_ok_to_exit (dview))
3186 h->running = 1;
3187 return MSG_HANDLED;
3189 default:
3190 return default_dlg_callback (h, sender, msg, parm, data);
3194 /*** public functions ****************************************************************************/
3195 /* --------------------------------------------------------------------------------------------- */
3198 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3200 int error;
3201 WDiff *dview;
3202 WButtonBar *bar;
3203 Dlg_head *dview_dlg;
3205 /* Create dialog and widgets, put them on the dialog */
3206 dview_dlg =
3207 create_dlg (0, 0, LINES, COLS, NULL, dview_dialog_callback,
3208 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3210 dview = g_new0 (WDiff, 1);
3212 init_widget (&dview->widget, 0, 0, LINES - 1, COLS,
3213 (callback_fn) dview_callback, (mouse_h) dview_event);
3215 widget_want_cursor (dview->widget, 0);
3217 bar = buttonbar_new (1);
3219 add_widget (dview_dlg, dview);
3220 add_widget (dview_dlg, bar);
3222 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3224 /* Please note that if you add another widget,
3225 * you have to modify dview_adjust_size to
3226 * be aware of it
3228 if (error == 0)
3229 run_dlg (dview_dlg);
3230 destroy_dlg (dview_dlg);
3232 return error;
3235 /* --------------------------------------------------------------------------------------------- */
3237 #define GET_FILE_AND_STAMP(n) \
3238 do { \
3239 use_copy##n = 0; \
3240 real_file##n = file##n; \
3241 if (!vfs_file_is_local(file##n)) { \
3242 real_file##n = mc_getlocalcopy(file##n); \
3243 if (real_file##n != NULL) { \
3244 use_copy##n = 1; \
3245 if (mc_stat(real_file##n, &st##n) != 0) { \
3246 use_copy##n = -1; \
3250 } while (0)
3251 #define UNGET_FILE(n) \
3252 do { \
3253 if (use_copy##n) { \
3254 int changed = 0; \
3255 if (use_copy##n > 0) { \
3256 time_t mtime = st##n.st_mtime; \
3257 if (mc_stat(real_file##n, &st##n) == 0) { \
3258 changed = (mtime != st##n.st_mtime); \
3261 mc_ungetlocalcopy(file##n, real_file##n, changed); \
3262 g_free(real_file##n); \
3264 } while (0)
3266 void
3267 dview_diff_cmd (void)
3269 int rv = 0;
3270 char *file0 = NULL;
3271 char *file1 = NULL;
3272 int is_dir0 = 0;
3273 int is_dir1 = 0;
3275 if (mc_run_mode == MC_RUN_FULL)
3277 const WPanel *panel0 = current_panel;
3278 const WPanel *panel1 = other_panel;
3279 if (get_current_index ())
3281 panel0 = other_panel;
3282 panel1 = current_panel;
3284 file0 = concat_dir_and_file (panel0->cwd, selection (panel0)->fname);
3285 file1 = concat_dir_and_file (panel1->cwd, selection (panel1)->fname);
3286 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3287 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3290 if (rv == 0)
3292 rv = -1;
3293 if (file0 != NULL && !is_dir0 && file1 != NULL && !is_dir1)
3295 int use_copy0;
3296 int use_copy1;
3297 struct stat st0;
3298 struct stat st1;
3299 char *real_file0;
3300 char *real_file1;
3301 GET_FILE_AND_STAMP (0);
3302 GET_FILE_AND_STAMP (1);
3303 if (real_file0 != NULL && real_file1 != NULL)
3305 rv = diff_view (real_file0, real_file1, file0, file1);
3307 UNGET_FILE (1);
3308 UNGET_FILE (0);
3312 g_free (file1);
3313 g_free (file0);
3315 if (rv != 0)
3316 message (1, MSG_ERROR, _("Two files are needed to compare"));
3319 /* --------------------------------------------------------------------------------------------- */
3321 void
3322 dview_update (WDiff * dview)
3324 int height = dview->height;
3325 int width1;
3326 int width2;
3328 int last = dview->a[0]->len - 1;
3330 if (dview->skip_rows > last)
3332 dview->skip_rows = dview->search.last_accessed_num_line = last;
3334 if (dview->skip_rows < 0)
3336 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3338 if (dview->skip_cols < 0)
3340 dview->skip_cols = 0;
3343 if (height < 2)
3345 return;
3348 width1 = dview->half1 + dview->bias;
3349 width2 = dview->half2 - dview->bias;
3350 if (dview->full)
3352 width1 = COLS;
3353 width2 = 0;
3356 if (dview->new_frame)
3358 int xwidth = dview->display_symbols + dview->display_numbers;
3360 tty_setcolor (NORMAL_COLOR);
3361 if (width1 > 1)
3363 tty_draw_box (1, 0, height, width1, FALSE);
3365 if (width2 > 1)
3367 tty_draw_box (1, width1, height, width2, FALSE);
3370 if (xwidth)
3372 xwidth++;
3373 if (xwidth < width1 - 1)
3375 tty_gotoyx (1, xwidth);
3376 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
3377 tty_gotoyx (height, xwidth);
3378 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
3379 tty_draw_vline (2, xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
3381 if (xwidth < width2 - 1)
3383 tty_gotoyx (1, width1 + xwidth);
3384 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
3385 tty_gotoyx (height, width1 + xwidth);
3386 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
3387 tty_draw_vline (2, width1 + xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
3390 dview->new_frame = 0;
3393 if (width1 > 2)
3395 dview_status (dview, dview->ord, width1, 0);
3396 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
3398 if (width2 > 2)
3400 dview_status (dview, dview->ord ^ 1, width2, width1);
3401 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
3405 /* --------------------------------------------------------------------------------------------- */