Drop old mouse API and use the new one.
[midnight-commander.git] / src / diffviewer / ydiff.c
blob8d8bfa5a178668a570ab852a0b8f27a6a543d829
1 /*
2 Copyright (C) 2007-2016
3 Free Software Foundation, Inc.
5 Written by:
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
8 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013, 2016
9 Ilia Maslakov <il.smind@gmail.com>, 2010
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
36 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/tty/color.h"
39 #include "lib/tty/key.h"
40 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
41 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
42 #include "lib/util.h"
43 #include "lib/widget.h"
44 #include "lib/strutil.h"
45 #include "lib/strescape.h" /* strutils_glob_escape() */
46 #ifdef HAVE_CHARSET
47 #include "lib/charsets.h"
48 #endif
49 #include "lib/event.h" /* mc_event_raise() */
51 #include "src/filemanager/cmd.h" /* edit_file_at_line(), view_other_cmd() */
52 #include "src/filemanager/panel.h"
53 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
55 #include "src/keybind-defaults.h"
56 #include "src/setup.h"
57 #include "src/history.h"
58 #ifdef HAVE_CHARSET
59 #include "src/selcodepage.h"
60 #endif
62 #include "ydiff.h"
63 #include "internal.h"
65 /*** global variables ****************************************************************************/
67 /*** file scope macro definitions ****************************************************************/
69 #define g_array_foreach(a, TP, cbf) \
70 do { \
71 size_t g_array_foreach_i;\
73 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
74 { \
75 TP *g_array_foreach_var; \
77 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
78 (*cbf) (g_array_foreach_var); \
79 } \
80 } while (0)
82 #define FILE_READ_BUF 4096
83 #define FILE_FLAG_TEMP (1 << 0)
85 #define ADD_CH '+'
86 #define DEL_CH '-'
87 #define CHG_CH '*'
88 #define EQU_CH ' '
90 #define HDIFF_ENABLE 1
91 #define HDIFF_MINCTX 5
92 #define HDIFF_DEPTH 10
94 #define FILE_DIRTY(fs) \
95 do \
96 { \
97 (fs)->pos = 0; \
98 (fs)->len = 0; \
99 } \
100 while (0)
102 /*** file scope type declarations ****************************************************************/
104 typedef enum
106 FROM_LEFT_TO_RIGHT,
107 FROM_RIGHT_TO_LEFT
108 } action_direction_t;
110 /*** file scope variables ************************************************************************/
112 /*** file scope functions ************************************************************************/
113 /* --------------------------------------------------------------------------------------------- */
115 static inline int
116 TAB_SKIP (int ts, int pos)
118 if (ts > 0 && ts < 9)
119 return ts - pos % ts;
120 else
121 return 8 - pos % 8;
124 /* --------------------------------------------------------------------------------------------- */
126 static gboolean
127 rewrite_backup_content (const vfs_path_t * from_file_name_vpath, const char *to_file_name)
129 FILE *backup_fd;
130 char *contents;
131 gsize length;
132 const char *from_file_name;
134 from_file_name = vfs_path_get_by_index (from_file_name_vpath, -1)->path;
135 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
136 return FALSE;
138 backup_fd = fopen (to_file_name, "w");
139 if (backup_fd == NULL)
141 g_free (contents);
142 return FALSE;
145 length = fwrite ((const void *) contents, length, 1, backup_fd);
147 fflush (backup_fd);
148 fclose (backup_fd);
149 g_free (contents);
150 return TRUE;
153 /* buffered I/O ************************************************************* */
156 * Try to open a temporary file.
157 * @note the name is not altered if this function fails
159 * @param[out] name address of a pointer to store the temporary name
160 * @return file descriptor on success, negative on error
163 static int
164 open_temp (void **name)
166 int fd;
167 vfs_path_t *diff_file_name = NULL;
169 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
170 if (fd == -1)
172 message (D_ERROR, MSG_ERROR,
173 _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
174 return -1;
176 *name = g_strdup (vfs_path_as_str (diff_file_name));
177 vfs_path_free (diff_file_name);
178 return fd;
181 /* --------------------------------------------------------------------------------------------- */
184 * Alocate file structure and associate file descriptor to it.
186 * @param fd file descriptor
187 * @return file structure
190 static FBUF *
191 f_dopen (int fd)
193 FBUF *fs;
195 if (fd < 0)
196 return NULL;
198 fs = g_try_malloc (sizeof (FBUF));
199 if (fs == NULL)
200 return NULL;
202 fs->buf = g_try_malloc (FILE_READ_BUF);
203 if (fs->buf == NULL)
205 g_free (fs);
206 return NULL;
209 fs->fd = fd;
210 FILE_DIRTY (fs);
211 fs->flags = 0;
212 fs->data = NULL;
214 return fs;
217 /* --------------------------------------------------------------------------------------------- */
220 * Free file structure without closing the file.
222 * @param fs file structure
223 * @return 0 on success, non-zero on error
226 static int
227 f_free (FBUF * fs)
229 int rv = 0;
231 if (fs->flags & FILE_FLAG_TEMP)
233 rv = unlink (fs->data);
234 g_free (fs->data);
236 g_free (fs->buf);
237 g_free (fs);
238 return rv;
241 /* --------------------------------------------------------------------------------------------- */
244 * Open a binary temporary file in R/W mode.
245 * @note the file will be deleted when closed
247 * @return file structure
249 static FBUF *
250 f_temp (void)
252 int fd;
253 FBUF *fs;
255 fs = f_dopen (0);
256 if (fs == NULL)
257 return NULL;
259 fd = open_temp (&fs->data);
260 if (fd < 0)
262 f_free (fs);
263 return NULL;
266 fs->fd = fd;
267 fs->flags = FILE_FLAG_TEMP;
268 return fs;
271 /* --------------------------------------------------------------------------------------------- */
274 * Open a binary file in specified mode.
276 * @param filename file name
277 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
279 * @return file structure
282 static FBUF *
283 f_open (const char *filename, int flags)
285 int fd;
286 FBUF *fs;
288 fs = f_dopen (0);
289 if (fs == NULL)
290 return NULL;
292 fd = open (filename, flags);
293 if (fd < 0)
295 f_free (fs);
296 return NULL;
299 fs->fd = fd;
300 return fs;
303 /* --------------------------------------------------------------------------------------------- */
306 * Read a line of bytes from file until newline or EOF.
307 * @note does not stop on null-byte
308 * @note buf will not be null-terminated
310 * @param buf destination buffer
311 * @param size size of buffer
312 * @param fs file structure
314 * @return number of bytes read
317 static size_t
318 f_gets (char *buf, size_t size, FBUF * fs)
320 size_t j = 0;
324 int i;
325 int stop = 0;
327 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
329 buf[j] = fs->buf[i];
330 if (buf[j] == '\n')
331 stop = 1;
333 fs->pos = i;
335 if (j == size || stop)
336 break;
338 fs->pos = 0;
339 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
341 while (fs->len > 0);
343 return j;
346 /* --------------------------------------------------------------------------------------------- */
349 * Seek into file.
350 * @note avoids thrashing read cache when possible
352 * @param fs file structure
353 * @param off offset
354 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
356 * @return position in file, starting from begginning
359 static off_t
360 f_seek (FBUF * fs, off_t off, int whence)
362 off_t rv;
364 if (fs->len && whence != SEEK_END)
366 rv = lseek (fs->fd, 0, SEEK_CUR);
367 if (rv != -1)
369 if (whence == SEEK_CUR)
371 whence = SEEK_SET;
372 off += rv - fs->len + fs->pos;
374 if (off - rv >= -fs->len && off - rv <= 0)
376 fs->pos = fs->len + off - rv;
377 return off;
382 rv = lseek (fs->fd, off, whence);
383 if (rv != -1)
384 FILE_DIRTY (fs);
385 return rv;
388 /* --------------------------------------------------------------------------------------------- */
391 * Seek to the beginning of file, thrashing read cache.
393 * @param fs file structure
395 * @return 0 if success, non-zero on error
398 static off_t
399 f_reset (FBUF * fs)
401 off_t rv;
403 rv = lseek (fs->fd, 0, SEEK_SET);
404 if (rv != -1)
405 FILE_DIRTY (fs);
406 return rv;
409 /* --------------------------------------------------------------------------------------------- */
412 * Write bytes to file.
413 * @note thrashes read cache
415 * @param fs file structure
416 * @param buf source buffer
417 * @param size size of buffer
419 * @return number of written bytes, -1 on error
422 static ssize_t
423 f_write (FBUF * fs, const char *buf, size_t size)
425 ssize_t rv;
427 rv = write (fs->fd, buf, size);
428 if (rv >= 0)
429 FILE_DIRTY (fs);
430 return rv;
433 /* --------------------------------------------------------------------------------------------- */
436 * Truncate file to the current position.
437 * @note thrashes read cache
439 * @param fs file structure
441 * @return current file size on success, negative on error
444 static off_t
445 f_trunc (FBUF * fs)
447 off_t off;
449 off = lseek (fs->fd, 0, SEEK_CUR);
450 if (off != -1)
452 int rv;
454 rv = ftruncate (fs->fd, off);
455 if (rv != 0)
456 off = -1;
457 else
458 FILE_DIRTY (fs);
460 return off;
463 /* --------------------------------------------------------------------------------------------- */
466 * Close file.
467 * @note if this is temporary file, it is deleted
469 * @param fs file structure
470 * @return 0 on success, non-zero on error
473 static int
474 f_close (FBUF * fs)
476 int rv = -1;
478 if (fs != NULL)
480 rv = close (fs->fd);
481 f_free (fs);
484 return rv;
487 /* --------------------------------------------------------------------------------------------- */
490 * Create pipe stream to process.
492 * @param cmd shell command line
493 * @param flags open mode, either O_RDONLY or O_WRONLY
495 * @return file structure
498 static FBUF *
499 p_open (const char *cmd, int flags)
501 FILE *f;
502 FBUF *fs;
503 const char *type = NULL;
505 if (flags == O_RDONLY)
506 type = "r";
507 else if (flags == O_WRONLY)
508 type = "w";
510 if (type == NULL)
511 return NULL;
513 fs = f_dopen (0);
514 if (fs == NULL)
515 return NULL;
517 f = popen (cmd, type);
518 if (f == NULL)
520 f_free (fs);
521 return NULL;
524 fs->fd = fileno (f);
525 fs->data = f;
526 return fs;
529 /* --------------------------------------------------------------------------------------------- */
532 * Close pipe stream.
534 * @param fs structure
535 * @return 0 on success, non-zero on error
538 static int
539 p_close (FBUF * fs)
541 int rv = -1;
543 if (fs != NULL)
545 rv = pclose (fs->data);
546 f_free (fs);
549 return rv;
552 /* --------------------------------------------------------------------------------------------- */
555 * Get one char (byte) from string
557 * @param char * str, gboolean * result
558 * @return int as character or 0 and result == FALSE if fail
561 static int
562 dview_get_byte (char *str, gboolean * result)
564 if (str == NULL)
566 *result = FALSE;
567 return 0;
569 *result = TRUE;
570 return (unsigned char) *str;
573 /* --------------------------------------------------------------------------------------------- */
575 #ifdef HAVE_CHARSET
577 * Get utf multibyte char from string
579 * @param char * str, int * char_length, gboolean * result
580 * @return int as utf character or 0 and result == FALSE if fail
583 static int
584 dview_get_utf (char *str, int *char_length, gboolean * result)
586 int res = -1;
587 gunichar ch;
588 int ch_len = 0;
590 *result = TRUE;
592 if (str == NULL)
594 *result = FALSE;
595 return 0;
598 res = g_utf8_get_char_validated (str, -1);
600 if (res < 0)
601 ch = *str;
602 else
604 gchar *next_ch;
606 ch = res;
607 /* Calculate UTF-8 char length */
608 next_ch = g_utf8_next_char (str);
609 ch_len = next_ch - str;
611 *char_length = ch_len;
612 return ch;
615 /* --------------------------------------------------------------------------------------------- */
617 static int
618 dview_str_utf8_offset_to_pos (const char *text, size_t length)
620 ptrdiff_t result;
622 if (text == NULL || text[0] == '\0')
623 return length;
625 if (g_utf8_validate (text, -1, NULL))
626 result = g_utf8_offset_to_pointer (text, length) - text;
627 else
629 gunichar uni;
630 char *tmpbuf, *buffer;
632 buffer = tmpbuf = g_strdup (text);
633 while (tmpbuf[0] != '\0')
635 uni = g_utf8_get_char_validated (tmpbuf, -1);
636 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
637 tmpbuf = g_utf8_next_char (tmpbuf);
638 else
640 tmpbuf[0] = '.';
641 tmpbuf++;
644 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
645 g_free (buffer);
647 return max (length, (size_t) result);
649 #endif /*HAVE_CHARSET */
651 /* --------------------------------------------------------------------------------------------- */
653 /* diff parse *************************************************************** */
656 * Read decimal number from string.
658 * @param[in,out] str string to parse
659 * @param[out] n extracted number
660 * @return 0 if success, otherwise non-zero
663 static int
664 scan_deci (const char **str, int *n)
666 const char *p = *str;
667 char *q;
669 errno = 0;
670 *n = strtol (p, &q, 10);
671 if (errno != 0 || p == q)
672 return -1;
673 *str = q;
674 return 0;
677 /* --------------------------------------------------------------------------------------------- */
680 * Parse line for diff statement.
682 * @param p string to parse
683 * @param ops list of diff statements
684 * @return 0 if success, otherwise non-zero
687 static int
688 scan_line (const char *p, GArray * ops)
690 DIFFCMD op;
692 int f1, f2;
693 int t1, t2;
694 int cmd;
695 int range;
697 /* handle the following cases:
698 * NUMaNUM[,NUM]
699 * NUM[,NUM]cNUM[,NUM]
700 * NUM[,NUM]dNUM
701 * where NUM is a positive integer
704 if (scan_deci (&p, &f1) != 0 || f1 < 0)
705 return -1;
707 f2 = f1;
708 range = 0;
709 if (*p == ',')
711 p++;
712 if (scan_deci (&p, &f2) != 0 || f2 < f1)
713 return -1;
715 range = 1;
718 cmd = *p++;
719 if (cmd == 'a')
721 if (range != 0)
722 return -1;
724 else if (cmd != 'c' && cmd != 'd')
725 return -1;
727 if (scan_deci (&p, &t1) != 0 || t1 < 0)
728 return -1;
730 t2 = t1;
731 range = 0;
732 if (*p == ',')
734 p++;
735 if (scan_deci (&p, &t2) != 0 || t2 < t1)
736 return -1;
738 range = 1;
741 if (cmd == 'd' && range != 0)
742 return -1;
744 op.a[0][0] = f1;
745 op.a[0][1] = f2;
746 op.cmd = cmd;
747 op.a[1][0] = t1;
748 op.a[1][1] = t2;
749 g_array_append_val (ops, op);
750 return 0;
753 /* --------------------------------------------------------------------------------------------- */
756 * Parse diff output and extract diff statements.
758 * @param f stream to read from
759 * @param ops list of diff statements to fill
760 * @return positive number indicating number of hunks, otherwise negative
763 static int
764 scan_diff (FBUF * f, GArray * ops)
766 int sz;
767 char buf[BUFSIZ];
769 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
771 if (isdigit (buf[0]))
773 if (buf[sz - 1] != '\n')
774 return -1;
776 buf[sz] = '\0';
777 if (scan_line (buf, ops) != 0)
778 return -1;
780 continue;
783 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
787 return ops->len;
790 /* --------------------------------------------------------------------------------------------- */
793 * Invoke diff and extract diff statements.
795 * @param args extra arguments to be passed to diff
796 * @param extra more arguments to be passed to diff
797 * @param file1 first file to compare
798 * @param file2 second file to compare
799 * @param ops list of diff statements to fill
801 * @return positive number indicating number of hunks, otherwise negative
804 static int
805 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
806 GArray * ops)
808 static const char *opt =
809 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
810 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
811 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
812 " --unchanged-group-format=''";
814 int rv;
815 FBUF *f;
816 char *cmd;
817 int code;
818 char *file1_esc, *file2_esc;
820 /* escape potential $ to avoid shell variable substitutions in popen() */
821 file1_esc = strutils_shell_escape (file1);
822 file2_esc = strutils_shell_escape (file2);
823 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
824 g_free (file1_esc);
825 g_free (file2_esc);
827 if (cmd == NULL)
828 return -1;
830 f = p_open (cmd, O_RDONLY);
831 g_free (cmd);
833 if (f == NULL)
834 return -1;
836 rv = scan_diff (f, ops);
837 code = p_close (f);
839 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
840 rv = -1;
842 return rv;
845 /* --------------------------------------------------------------------------------------------- */
848 * Reparse and display file according to diff statements.
850 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
851 * @param filename file name to display
852 * @param ops list of diff statements
853 * @param printer printf-like function to be used for displaying
854 * @param ctx printer context
856 * @return 0 if success, otherwise non-zero
859 static int
860 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
862 size_t i;
863 FBUF *f;
864 size_t sz;
865 char buf[BUFSIZ];
866 int line = 0;
867 off_t off = 0;
868 const DIFFCMD *op;
869 diff_place_t eff;
870 int add_cmd;
871 int del_cmd;
873 f = f_open (filename, O_RDONLY);
874 if (f == NULL)
875 return -1;
877 ord &= 1;
878 eff = ord;
880 add_cmd = 'a';
881 del_cmd = 'd';
882 if (ord != 0)
884 add_cmd = 'd';
885 del_cmd = 'a';
887 #define F1 a[eff][0]
888 #define F2 a[eff][1]
889 #define T1 a[ ord^1 ][0]
890 #define T2 a[ ord^1 ][1]
891 for (i = 0; i < ops->len; i++)
893 int n;
895 op = &g_array_index (ops, DIFFCMD, i);
896 n = op->F1 - (op->cmd != add_cmd);
898 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
900 line++;
901 printer (ctx, EQU_CH, line, off, sz, buf);
902 off += sz;
903 while (buf[sz - 1] != '\n')
905 sz = f_gets (buf, sizeof (buf), f);
906 if (sz == 0)
908 printer (ctx, 0, 0, 0, 1, "\n");
909 break;
911 printer (ctx, 0, 0, 0, sz, buf);
912 off += sz;
916 if (line != n)
917 goto err;
919 if (op->cmd == add_cmd)
921 n = op->T2 - op->T1 + 1;
922 while (n != 0)
924 printer (ctx, DEL_CH, 0, 0, 1, "\n");
925 n--;
929 if (op->cmd == del_cmd)
931 n = op->F2 - op->F1 + 1;
932 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
934 line++;
935 printer (ctx, ADD_CH, line, off, sz, buf);
936 off += sz;
937 while (buf[sz - 1] != '\n')
939 sz = f_gets (buf, sizeof (buf), f);
940 if (sz == 0)
942 printer (ctx, 0, 0, 0, 1, "\n");
943 break;
945 printer (ctx, 0, 0, 0, sz, buf);
946 off += sz;
948 n--;
951 if (n != 0)
952 goto err;
955 if (op->cmd == 'c')
957 n = op->F2 - op->F1 + 1;
958 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
960 line++;
961 printer (ctx, CHG_CH, line, off, sz, buf);
962 off += sz;
963 while (buf[sz - 1] != '\n')
965 sz = f_gets (buf, sizeof (buf), f);
966 if (sz == 0)
968 printer (ctx, 0, 0, 0, 1, "\n");
969 break;
971 printer (ctx, 0, 0, 0, sz, buf);
972 off += sz;
974 n--;
977 if (n != 0)
978 goto err;
980 n = op->T2 - op->T1 - (op->F2 - op->F1);
981 while (n > 0)
983 printer (ctx, CHG_CH, 0, 0, 1, "\n");
984 n--;
988 #undef T2
989 #undef T1
990 #undef F2
991 #undef F1
993 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
995 line++;
996 printer (ctx, EQU_CH, line, off, sz, buf);
997 off += sz;
998 while (buf[sz - 1] != '\n')
1000 sz = f_gets (buf, sizeof (buf), f);
1001 if (sz == 0)
1003 printer (ctx, 0, 0, 0, 1, "\n");
1004 break;
1006 printer (ctx, 0, 0, 0, sz, buf);
1007 off += sz;
1011 f_close (f);
1012 return 0;
1014 err:
1015 f_close (f);
1016 return -1;
1019 /* --------------------------------------------------------------------------------------------- */
1021 /* horizontal diff ********************************************************** */
1024 * Longest common substring.
1026 * @param s first string
1027 * @param m length of first string
1028 * @param t second string
1029 * @param n length of second string
1030 * @param ret list of offsets for longest common substrings inside each string
1031 * @param min minimum length of common substrings
1033 * @return 0 if success, nonzero otherwise
1036 static int
1037 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1039 int i, j;
1040 int *Lprev, *Lcurr;
1041 int z = 0;
1043 if (m < min || n < min)
1045 /* XXX early culling */
1046 return 0;
1049 Lprev = g_try_new0 (int, n + 1);
1050 if (Lprev == NULL)
1051 return -1;
1053 Lcurr = g_try_new0 (int, n + 1);
1054 if (Lcurr == NULL)
1056 g_free (Lprev);
1057 return -1;
1060 for (i = 0; i < m; i++)
1062 int *L;
1064 L = Lprev;
1065 Lprev = Lcurr;
1066 Lcurr = L;
1067 #ifdef USE_MEMSET_IN_LCS
1068 memset (Lcurr, 0, (n + 1) * sizeof (*Lcurr));
1069 #endif
1070 for (j = 0; j < n; j++)
1072 #ifndef USE_MEMSET_IN_LCS
1073 Lcurr[j + 1] = 0;
1074 #endif
1075 if (s[i] == t[j])
1077 int v;
1079 v = Lprev[j] + 1;
1080 Lcurr[j + 1] = v;
1081 if (z < v)
1083 z = v;
1084 g_array_set_size (ret, 0);
1086 if (z == v && z >= min)
1088 int off0, off1;
1089 size_t k;
1091 off0 = i - z + 1;
1092 off1 = j - z + 1;
1094 for (k = 0; k < ret->len; k++)
1096 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1097 if ((*p)[0] == off0 || (*p)[1] >= off1)
1098 break;
1100 if (k == ret->len)
1102 PAIR p2;
1104 p2[0] = off0;
1105 p2[1] = off1;
1106 g_array_append_val (ret, p2);
1113 g_free (Lcurr);
1114 g_free (Lprev);
1115 return z;
1118 /* --------------------------------------------------------------------------------------------- */
1121 * Scan recursively for common substrings and build ranges.
1123 * @param s first string
1124 * @param t second string
1125 * @param bracket current limits for both of the strings
1126 * @param min minimum length of common substrings
1127 * @param hdiff list of horizontal diff ranges to fill
1128 * @param depth recursion depth
1130 * @return 0 if success, nonzero otherwise
1133 static gboolean
1134 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1135 unsigned int depth)
1137 BRACKET p;
1139 if (depth-- != 0)
1141 GArray *ret;
1142 BRACKET b;
1143 int len;
1145 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1146 if (ret == NULL)
1147 return FALSE;
1149 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1150 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1151 if (ret->len != 0)
1153 size_t k = 0;
1154 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1155 const PAIR *data2;
1157 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1158 b[DIFF_LEFT].len = (*data)[0];
1159 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1160 b[DIFF_RIGHT].len = (*data)[1];
1161 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1162 return FALSE;
1164 for (k = 0; k < ret->len - 1; k++)
1166 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1167 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1168 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1169 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1170 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1171 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1172 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1173 return FALSE;
1175 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1176 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1177 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1178 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1179 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1180 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1181 return FALSE;
1183 g_array_free (ret, TRUE);
1184 return TRUE;
1188 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1189 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1190 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1191 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1192 g_array_append_val (hdiff, p);
1194 return TRUE;
1197 /* --------------------------------------------------------------------------------------------- */
1200 * Build list of horizontal diff ranges.
1202 * @param s first string
1203 * @param m length of first string
1204 * @param t second string
1205 * @param n length of second string
1206 * @param min minimum length of common substrings
1207 * @param hdiff list of horizontal diff ranges to fill
1208 * @param depth recursion depth
1210 * @return 0 if success, nonzero otherwise
1213 static gboolean
1214 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1216 int i;
1217 BRACKET b;
1219 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1220 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1222 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1225 b[DIFF_LEFT].off = i;
1226 b[DIFF_LEFT].len = m - i;
1227 b[DIFF_RIGHT].off = i;
1228 b[DIFF_RIGHT].len = n - i;
1230 /* smartscan (multiple horizontal diff) */
1231 return hdiff_multi (s, t, b, min, hdiff, depth);
1234 /* --------------------------------------------------------------------------------------------- */
1236 /* read line **************************************************************** */
1239 * Check if character is inside horizontal diff limits.
1241 * @param k rank of character inside line
1242 * @param hdiff horizontal diff structure
1243 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1245 * @return TRUE if inside hdiff limits, FALSE otherwise
1248 static gboolean
1249 is_inside (int k, GArray * hdiff, diff_place_t ord)
1251 size_t i;
1252 BRACKET *b;
1254 for (i = 0; i < hdiff->len; i++)
1256 int start, end;
1258 b = &g_array_index (hdiff, BRACKET, i);
1259 start = (*b)[ord].off;
1260 end = start + (*b)[ord].len;
1261 if (k >= start && k < end)
1262 return TRUE;
1264 return FALSE;
1267 /* --------------------------------------------------------------------------------------------- */
1270 * Copy 'src' to 'dst' expanding tabs.
1271 * @note The procedure returns when all bytes are consumed from 'src'
1273 * @param dst destination buffer
1274 * @param src source buffer
1275 * @param srcsize size of src buffer
1276 * @param base virtual base of this string, needed to calculate tabs
1277 * @param ts tab size
1279 * @return new virtual base
1282 static int
1283 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1285 int i;
1287 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1289 *dst = *src;
1290 if (*src == '\t')
1292 int j;
1294 j = TAB_SKIP (ts, i + base);
1295 i += j - 1;
1296 while (j-- > 0)
1297 *dst++ = ' ';
1298 dst--;
1301 return i + base;
1304 /* --------------------------------------------------------------------------------------------- */
1307 * Copy 'src' to 'dst' expanding tabs.
1309 * @param dst destination buffer
1310 * @param dstsize size of dst buffer
1311 * @param[in,out] _src source buffer
1312 * @param srcsize size of src buffer
1313 * @param base virtual base of this string, needed to calculate tabs
1314 * @param ts tab size
1316 * @return new virtual base
1318 * @note The procedure returns when all bytes are consumed from 'src'
1319 * or 'dstsize' bytes are written to 'dst'
1320 * @note Upon return, 'src' points to the first unwritten character in source
1323 static int
1324 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1326 int i;
1327 const char *src = *_src;
1329 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1331 *dst = *src;
1332 if (*src == '\t')
1334 int j;
1336 j = TAB_SKIP (ts, i + base);
1337 if (j > dstsize - i)
1338 j = dstsize - i;
1339 i += j - 1;
1340 while (j-- > 0)
1341 *dst++ = ' ';
1342 dst--;
1345 *_src = src;
1346 return i + base;
1349 /* --------------------------------------------------------------------------------------------- */
1352 * Read line from memory, converting tabs to spaces and padding with spaces.
1354 * @param src buffer to read from
1355 * @param srcsize size of src buffer
1356 * @param dst buffer to read to
1357 * @param dstsize size of dst buffer, excluding trailing null
1358 * @param skip number of characters to skip
1359 * @param ts tab size
1360 * @param show_cr show trailing carriage return as ^M
1362 * @return negative on error, otherwise number of bytes except padding
1365 static int
1366 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1367 gboolean show_cr)
1369 int sz = 0;
1371 if (src != NULL)
1373 int i;
1374 char *tmp = dst;
1375 const int base = 0;
1377 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1379 if (*src == '\t')
1381 int j;
1383 j = TAB_SKIP (ts, i + base);
1384 i += j - 1;
1385 while (j-- > 0)
1387 if (skip > 0)
1388 skip--;
1389 else if (dstsize != 0)
1391 dstsize--;
1392 *dst++ = ' ';
1396 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1398 if (skip == 0 && show_cr)
1400 if (dstsize > 1)
1402 dstsize -= 2;
1403 *dst++ = '^';
1404 *dst++ = 'M';
1406 else
1408 dstsize--;
1409 *dst++ = '.';
1412 break;
1414 else if (skip > 0)
1416 #ifdef HAVE_CHARSET
1417 gboolean res;
1418 int ch_len = 1;
1420 (void) dview_get_utf ((char *) src, &ch_len, &res);
1422 if (ch_len > 1)
1423 skip += ch_len - 1;
1424 #endif
1426 skip--;
1428 else
1430 dstsize--;
1431 *dst++ = *src;
1434 sz = dst - tmp;
1436 while (dstsize != 0)
1438 dstsize--;
1439 *dst++ = ' ';
1441 *dst = '\0';
1442 return sz;
1445 /* --------------------------------------------------------------------------------------------- */
1448 * Read line from memory and build attribute array.
1450 * @param src buffer to read from
1451 * @param srcsize size of src buffer
1452 * @param dst buffer to read to
1453 * @param dstsize size of dst buffer, excluding trailing null
1454 * @param skip number of characters to skip
1455 * @param ts tab size
1456 * @param show_cr show trailing carriage return as ^M
1457 * @param hdiff horizontal diff structure
1458 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1459 * @param att buffer of attributes
1461 * @return negative on error, otherwise number of bytes except padding
1464 static int
1465 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1466 gboolean show_cr, GArray * hdiff, diff_place_t ord, char *att)
1468 int sz = 0;
1470 if (src != NULL)
1472 int i, k;
1473 char *tmp = dst;
1474 const int base = 0;
1476 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1478 if (*src == '\t')
1480 int j;
1482 j = TAB_SKIP (ts, i + base);
1483 i += j - 1;
1484 while (j-- > 0)
1486 if (skip != 0)
1487 skip--;
1488 else if (dstsize != 0)
1490 dstsize--;
1491 *att++ = is_inside (k, hdiff, ord);
1492 *dst++ = ' ';
1496 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1498 if (skip == 0 && show_cr)
1500 if (dstsize > 1)
1502 dstsize -= 2;
1503 *att++ = is_inside (k, hdiff, ord);
1504 *dst++ = '^';
1505 *att++ = is_inside (k, hdiff, ord);
1506 *dst++ = 'M';
1508 else
1510 dstsize--;
1511 *att++ = is_inside (k, hdiff, ord);
1512 *dst++ = '.';
1515 break;
1517 else if (skip != 0)
1519 #ifdef HAVE_CHARSET
1520 gboolean res;
1521 int ch_len = 1;
1523 (void) dview_get_utf ((char *) src, &ch_len, &res);
1524 if (ch_len > 1)
1525 skip += ch_len - 1;
1526 #endif
1528 skip--;
1530 else
1532 dstsize--;
1533 *att++ = is_inside (k, hdiff, ord);
1534 *dst++ = *src;
1537 sz = dst - tmp;
1539 while (dstsize != 0)
1541 dstsize--;
1542 *att++ = '\0';
1543 *dst++ = ' ';
1545 *dst = '\0';
1546 return sz;
1549 /* --------------------------------------------------------------------------------------------- */
1552 * Read line from file, converting tabs to spaces and padding with spaces.
1554 * @param f file stream to read from
1555 * @param off offset of line inside file
1556 * @param dst buffer to read to
1557 * @param dstsize size of dst buffer, excluding trailing null
1558 * @param skip number of characters to skip
1559 * @param ts tab size
1560 * @param show_cr show trailing carriage return as ^M
1562 * @return negative on error, otherwise number of bytes except padding
1565 static int
1566 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, gboolean show_cr)
1568 int base = 0;
1569 int old_base = base;
1570 size_t amount = dstsize;
1571 size_t useful, offset;
1572 size_t i;
1573 size_t sz;
1574 int lastch = '\0';
1575 const char *q = NULL;
1576 char tmp[BUFSIZ]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1577 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1579 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1581 /* abnormal, but avoid buffer overflow */
1582 memset (dst, ' ', dstsize);
1583 dst[dstsize] = '\0';
1584 return 0;
1587 f_seek (f, off, SEEK_SET);
1589 while (skip > base)
1591 old_base = base;
1592 sz = f_gets (tmp, amount, f);
1593 if (sz == 0)
1594 break;
1596 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1597 if (cvt[base - old_base - 1] == '\n')
1599 q = &cvt[base - old_base - 1];
1600 base = old_base + q - cvt + 1;
1601 break;
1605 if (base < skip)
1607 memset (dst, ' ', dstsize);
1608 dst[dstsize] = '\0';
1609 return 0;
1612 useful = base - skip;
1613 offset = skip - old_base;
1615 if (useful <= dstsize)
1617 if (useful != 0)
1618 memmove (dst, cvt + offset, useful);
1620 if (q == NULL)
1622 sz = f_gets (tmp, dstsize - useful + 1, f);
1623 if (sz != 0)
1625 const char *ptr = tmp;
1627 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1628 if (ptr < tmp + sz)
1629 lastch = *ptr;
1632 sz = useful;
1634 else
1636 memmove (dst, cvt + offset, dstsize);
1637 sz = dstsize;
1638 lastch = cvt[offset + dstsize];
1641 dst[sz] = lastch;
1642 for (i = 0; i < sz && dst[i] != '\n'; i++)
1644 if (dst[i] == '\r' && dst[i + 1] == '\n')
1646 if (show_cr)
1648 if (i + 1 < dstsize)
1650 dst[i++] = '^';
1651 dst[i++] = 'M';
1653 else
1655 dst[i++] = '*';
1658 break;
1662 for (; i < dstsize; i++)
1663 dst[i] = ' ';
1664 dst[i] = '\0';
1665 return sz;
1668 /* --------------------------------------------------------------------------------------------- */
1669 /* diff printers et al ****************************************************** */
1671 static void
1672 cc_free_elt (void *elt)
1674 DIFFLN *p = elt;
1676 if (p != NULL)
1677 g_free (p->p);
1680 /* --------------------------------------------------------------------------------------------- */
1682 static int
1683 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1685 GArray *a = ((PRINTER_CTX *) ctx)->a;
1686 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1688 if (ch != 0)
1690 DIFFLN p;
1692 p.p = NULL;
1693 p.ch = ch;
1694 p.line = line;
1695 p.u.off = off;
1696 if (dsrc == DATA_SRC_MEM && line != 0)
1698 if (sz != 0 && str[sz - 1] == '\n')
1699 sz--;
1700 if (sz > 0)
1701 p.p = g_strndup (str, sz);
1702 p.u.len = sz;
1704 g_array_append_val (a, p);
1706 else if (dsrc == DATA_SRC_MEM)
1708 DIFFLN *p;
1710 p = &g_array_index (a, DIFFLN, a->len - 1);
1711 if (sz != 0 && str[sz - 1] == '\n')
1712 sz--;
1713 if (sz != 0)
1715 size_t new_size;
1716 char *q;
1718 new_size = p->u.len + sz;
1719 q = g_realloc (p->p, new_size);
1720 memcpy (q + p->u.len, str, sz);
1721 p->p = q;
1723 p->u.len += sz;
1725 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1727 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1728 f_write (f, str, sz);
1730 return 0;
1733 /* --------------------------------------------------------------------------------------------- */
1735 static int
1736 redo_diff (WDiff * dview)
1738 FBUF *const *f = dview->f;
1739 PRINTER_CTX ctx;
1740 GArray *ops;
1741 int ndiff;
1742 int rv;
1743 char extra[256];
1745 extra[0] = '\0';
1746 if (dview->opt.quality == 2)
1747 strcat (extra, " -d");
1748 if (dview->opt.quality == 1)
1749 strcat (extra, " --speed-large-files");
1750 if (dview->opt.strip_trailing_cr)
1751 strcat (extra, " --strip-trailing-cr");
1752 if (dview->opt.ignore_tab_expansion)
1753 strcat (extra, " -E");
1754 if (dview->opt.ignore_space_change)
1755 strcat (extra, " -b");
1756 if (dview->opt.ignore_all_space)
1757 strcat (extra, " -w");
1758 if (dview->opt.ignore_case)
1759 strcat (extra, " -i");
1761 if (dview->dsrc != DATA_SRC_MEM)
1763 f_reset (f[DIFF_LEFT]);
1764 f_reset (f[DIFF_RIGHT]);
1767 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1768 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1769 if (ndiff < 0)
1771 if (ops != NULL)
1772 g_array_free (ops, TRUE);
1773 return -1;
1776 ctx.dsrc = dview->dsrc;
1778 rv = 0;
1779 ctx.a = dview->a[DIFF_LEFT];
1780 ctx.f = f[DIFF_LEFT];
1781 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1783 ctx.a = dview->a[DIFF_RIGHT];
1784 ctx.f = f[DIFF_RIGHT];
1785 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1787 if (ops != NULL)
1788 g_array_free (ops, TRUE);
1790 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1791 return -1;
1793 if (dview->dsrc == DATA_SRC_TMP)
1795 f_trunc (f[DIFF_LEFT]);
1796 f_trunc (f[DIFF_RIGHT]);
1799 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1801 dview->hdiff = g_ptr_array_new ();
1802 if (dview->hdiff != NULL)
1804 size_t i;
1806 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1808 GArray *h = NULL;
1809 const DIFFLN *p;
1810 const DIFFLN *q;
1812 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1813 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1814 if (p->line && q->line && p->ch == CHG_CH)
1816 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1817 if (h != NULL)
1819 gboolean runresult;
1821 runresult =
1822 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1823 HDIFF_DEPTH);
1824 if (!runresult)
1826 g_array_free (h, TRUE);
1827 h = NULL;
1831 g_ptr_array_add (dview->hdiff, h);
1835 return ndiff;
1838 /* --------------------------------------------------------------------------------------------- */
1840 static void
1841 destroy_hdiff (WDiff * dview)
1843 if (dview->hdiff != NULL)
1845 int i;
1846 int len;
1848 len = dview->a[DIFF_LEFT]->len;
1850 for (i = 0; i < len; i++)
1852 GArray *h;
1854 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1855 if (h != NULL)
1856 g_array_free (h, TRUE);
1858 g_ptr_array_free (dview->hdiff, TRUE);
1859 dview->hdiff = NULL;
1862 mc_search_free (dview->search.handle);
1863 dview->search.handle = NULL;
1864 MC_PTR_FREE (dview->search.last_string);
1867 /* --------------------------------------------------------------------------------------------- */
1868 /* stuff ******************************************************************** */
1870 static int
1871 get_digits (unsigned int n)
1873 int d = 1;
1875 while (n /= 10)
1876 d++;
1877 return d;
1880 /* --------------------------------------------------------------------------------------------- */
1882 static int
1883 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1885 const DIFFLN *p;
1887 *linenum = 0;
1888 *lineofs = 0;
1890 if (a->len != 0)
1892 if (pos >= a->len)
1893 pos = a->len - 1;
1895 p = &g_array_index (a, DIFFLN, pos);
1897 if (p->line == 0)
1899 int n;
1901 for (n = pos; n > 0; n--)
1903 p--;
1904 if (p->line != 0)
1905 break;
1907 *lineofs = pos - n + 1;
1910 *linenum = p->line;
1912 return 0;
1915 /* --------------------------------------------------------------------------------------------- */
1917 static int
1918 calc_nwidth (const GArray ** const a)
1920 int l1, o1;
1921 int l2, o2;
1923 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1924 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1925 if (l1 < l2)
1926 l1 = l2;
1927 return get_digits (l1);
1930 /* --------------------------------------------------------------------------------------------- */
1932 static int
1933 find_prev_hunk (const GArray * a, int pos)
1935 #if 1
1936 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1937 pos--;
1938 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1939 pos--;
1940 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1941 pos--;
1942 if (pos > 0 && (size_t) pos < a->len)
1943 pos++;
1944 #else
1945 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1946 pos--;
1947 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1948 pos--;
1949 #endif
1951 return pos;
1954 /* --------------------------------------------------------------------------------------------- */
1956 static size_t
1957 find_next_hunk (const GArray * a, size_t pos)
1959 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1960 pos++;
1961 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1962 pos++;
1963 return pos;
1966 /* --------------------------------------------------------------------------------------------- */
1968 * Find start and end lines of the current hunk.
1970 * @param dview WDiff widget
1971 * @return boolean and
1972 * start_line1 first line of current hunk (file[0])
1973 * end_line1 last line of current hunk (file[0])
1974 * start_line1 first line of current hunk (file[0])
1975 * end_line1 last line of current hunk (file[0])
1978 static int
1979 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1981 const GArray *a0 = dview->a[DIFF_LEFT];
1982 const GArray *a1 = dview->a[DIFF_RIGHT];
1983 size_t pos;
1984 int ch;
1985 int res = 0;
1987 *start_line1 = 1;
1988 *start_line2 = 1;
1989 *end_line1 = 1;
1990 *end_line2 = 1;
1992 pos = dview->skip_rows;
1993 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1994 if (ch != EQU_CH)
1996 switch (ch)
1998 case ADD_CH:
1999 res = DIFF_DEL;
2000 break;
2001 case DEL_CH:
2002 res = DIFF_ADD;
2003 break;
2004 case CHG_CH:
2005 res = DIFF_CHG;
2006 break;
2007 default:
2008 break;
2010 while (pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2011 pos--;
2012 if (pos > 0)
2014 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
2015 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
2017 pos = dview->skip_rows;
2018 while (pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2020 int l0, l1;
2022 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
2023 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
2024 if (l0 > 0)
2025 *end_line1 = max (*start_line1, l0);
2026 if (l1 > 0)
2027 *end_line2 = max (*start_line2, l1);
2028 pos++;
2031 return res;
2034 /* --------------------------------------------------------------------------------------------- */
2036 * Remove hunk from file.
2038 * @param dview WDiff widget
2039 * @param merge_file file stream for writing data
2040 * @param from1 first line of hunk
2041 * @param to1 last line of hunk
2042 * @param merge_direction in what direction files should be merged
2045 static void
2046 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
2047 action_direction_t merge_direction)
2049 int line;
2050 char buf[BUF_10K];
2051 FILE *f0;
2053 if (merge_direction == FROM_RIGHT_TO_LEFT)
2054 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2055 else
2056 f0 = fopen (dview->file[DIFF_LEFT], "r");
2058 line = 0;
2059 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2061 line++;
2062 fputs (buf, merge_file);
2064 while (fgets (buf, sizeof (buf), f0) != NULL)
2066 line++;
2067 if (line >= to1)
2068 fputs (buf, merge_file);
2070 fclose (f0);
2073 /* --------------------------------------------------------------------------------------------- */
2075 * Add hunk to file.
2077 * @param dview WDiff widget
2078 * @param merge_file file stream for writing data
2079 * @param from1 first line of source hunk
2080 * @param from2 first line of destination hunk
2081 * @param to1 last line of source hunk
2082 * @param merge_direction in what direction files should be merged
2085 static void
2086 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2087 action_direction_t merge_direction)
2089 int line;
2090 char buf[BUF_10K];
2091 FILE *f0;
2092 FILE *f1;
2094 if (merge_direction == FROM_RIGHT_TO_LEFT)
2096 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2097 f1 = fopen (dview->file[DIFF_LEFT], "r");
2099 else
2101 f0 = fopen (dview->file[DIFF_LEFT], "r");
2102 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2105 line = 0;
2106 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2108 line++;
2109 fputs (buf, merge_file);
2111 line = 0;
2112 while (fgets (buf, sizeof (buf), f1) != NULL && line <= to2)
2114 line++;
2115 if (line >= from2)
2116 fputs (buf, merge_file);
2118 while (fgets (buf, sizeof (buf), f0) != NULL)
2119 fputs (buf, merge_file);
2121 fclose (f0);
2122 fclose (f1);
2125 /* --------------------------------------------------------------------------------------------- */
2127 * Replace hunk in file.
2129 * @param dview WDiff widget
2130 * @param merge_file file stream for writing data
2131 * @param from1 first line of source hunk
2132 * @param to1 last line of source hunk
2133 * @param from2 first line of destination hunk
2134 * @param to2 last line of destination hunk
2135 * @param merge_direction in what direction files should be merged
2138 static void
2139 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2140 action_direction_t merge_direction)
2142 int line1 = 0, line2 = 0;
2143 char buf[BUF_10K];
2144 FILE *f0;
2145 FILE *f1;
2147 if (merge_direction == FROM_RIGHT_TO_LEFT)
2149 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2150 f1 = fopen (dview->file[DIFF_LEFT], "r");
2152 else
2154 f0 = fopen (dview->file[DIFF_LEFT], "r");
2155 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2158 while (fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1)
2160 line1++;
2161 fputs (buf, merge_file);
2163 while (fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2)
2165 line2++;
2166 if (line2 >= from2)
2167 fputs (buf, merge_file);
2169 while (fgets (buf, sizeof (buf), f0) != NULL)
2171 line1++;
2172 if (line1 > to1)
2173 fputs (buf, merge_file);
2175 fclose (f0);
2176 fclose (f1);
2179 /* --------------------------------------------------------------------------------------------- */
2181 * Merge hunk.
2183 * @param dview WDiff widget
2184 * @param merge_direction in what direction files should be merged
2187 static void
2188 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2190 int from1, to1, from2, to2;
2191 int hunk;
2192 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2194 if (merge_direction == FROM_RIGHT_TO_LEFT)
2195 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2196 else
2197 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2199 if (hunk > 0)
2201 int merge_file_fd;
2202 FILE *merge_file;
2203 vfs_path_t *merge_file_name_vpath = NULL;
2205 if (!dview->merged[n_merge])
2207 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2208 if (!dview->merged[n_merge])
2210 message (D_ERROR, MSG_ERROR,
2211 _("Cannot create backup file\n%s%s\n%s"),
2212 dview->file[n_merge], "~~~", unix_error_string (errno));
2213 return;
2217 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2218 if (merge_file_fd == -1)
2220 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2221 unix_error_string (errno));
2222 return;
2225 merge_file = fdopen (merge_file_fd, "w");
2227 switch (hunk)
2229 case DIFF_DEL:
2230 if (merge_direction == FROM_RIGHT_TO_LEFT)
2231 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2232 else
2233 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2234 break;
2235 case DIFF_ADD:
2236 if (merge_direction == FROM_RIGHT_TO_LEFT)
2237 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2238 else
2239 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2240 break;
2241 case DIFF_CHG:
2242 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2243 break;
2244 default:
2245 break;
2247 fflush (merge_file);
2248 fclose (merge_file);
2250 int res;
2252 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2253 (void) res;
2255 mc_unlink (merge_file_name_vpath);
2256 vfs_path_free (merge_file_name_vpath);
2260 /* --------------------------------------------------------------------------------------------- */
2261 /* view routines and callbacks ********************************************** */
2263 static void
2264 dview_compute_split (WDiff * dview, int i)
2266 dview->bias += i;
2267 if (dview->bias < 2 - dview->half1)
2268 dview->bias = 2 - dview->half1;
2269 if (dview->bias > dview->half2 - 2)
2270 dview->bias = dview->half2 - 2;
2273 /* --------------------------------------------------------------------------------------------- */
2275 static void
2276 dview_compute_areas (WDiff * dview)
2278 dview->height = LINES - 2;
2279 dview->half1 = COLS / 2;
2280 dview->half2 = COLS - dview->half1;
2282 dview_compute_split (dview, 0);
2285 /* --------------------------------------------------------------------------------------------- */
2287 static void
2288 dview_reread (WDiff * dview)
2290 int ndiff;
2292 destroy_hdiff (dview);
2293 if (dview->a[DIFF_LEFT] != NULL)
2295 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2296 g_array_free (dview->a[DIFF_LEFT], TRUE);
2298 if (dview->a[DIFF_RIGHT] != NULL)
2300 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2301 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2304 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2305 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2307 ndiff = redo_diff (dview);
2308 if (ndiff >= 0)
2309 dview->ndiff = ndiff;
2312 /* --------------------------------------------------------------------------------------------- */
2314 #ifdef HAVE_CHARSET
2315 static void
2316 dview_set_codeset (WDiff * dview)
2318 const char *encoding_id = NULL;
2320 dview->utf8 = TRUE;
2321 encoding_id =
2322 get_codepage_id (mc_global.source_codepage >=
2323 0 ? mc_global.source_codepage : mc_global.display_codepage);
2324 if (encoding_id != NULL)
2326 GIConv conv;
2328 conv = str_crt_conv_from (encoding_id);
2329 if (conv != INVALID_CONV)
2331 if (dview->converter != str_cnv_from_term)
2332 str_close_conv (dview->converter);
2333 dview->converter = conv;
2335 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2339 /* --------------------------------------------------------------------------------------------- */
2341 static void
2342 dview_select_encoding (WDiff * dview)
2344 if (do_select_codepage ())
2345 dview_set_codeset (dview);
2346 dview_reread (dview);
2347 tty_touch_screen ();
2348 repaint_screen ();
2350 #endif /* HAVE_CHARSET */
2352 /* --------------------------------------------------------------------------------------------- */
2354 static void
2355 dview_diff_options (WDiff * dview)
2357 const char *quality_str[] = {
2358 N_("No&rmal"),
2359 N_("&Fastest (Assume large files)"),
2360 N_("&Minimal (Find a smaller set of change)")
2363 quick_widget_t quick_widgets[] = {
2364 /* *INDENT-OFF* */
2365 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2366 QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2367 QUICK_STOP_GROUPBOX,
2368 QUICK_START_GROUPBOX (N_("Diff extra options")),
2369 QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2370 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2371 QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2372 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2373 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2374 NULL),
2375 QUICK_STOP_GROUPBOX,
2376 QUICK_BUTTONS_OK_CANCEL,
2377 QUICK_END
2378 /* *INDENT-ON* */
2381 quick_dialog_t qdlg = {
2382 -1, -1, 56,
2383 N_("Diff Options"), "[Diff Options]",
2384 quick_widgets, NULL, NULL
2387 if (quick_dialog (&qdlg) != B_CANCEL)
2388 dview_reread (dview);
2391 /* --------------------------------------------------------------------------------------------- */
2393 static int
2394 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2395 const char *label1, const char *label2, DSRC dsrc)
2397 int ndiff;
2398 FBUF *f[DIFF_COUNT];
2400 f[DIFF_LEFT] = NULL;
2401 f[DIFF_RIGHT] = NULL;
2403 if (dsrc == DATA_SRC_TMP)
2405 f[DIFF_LEFT] = f_temp ();
2406 if (f[DIFF_LEFT] == NULL)
2407 return -1;
2409 f[DIFF_RIGHT] = f_temp ();
2410 if (f[DIFF_RIGHT] == NULL)
2412 f_close (f[DIFF_LEFT]);
2413 return -1;
2416 else if (dsrc == DATA_SRC_ORG)
2418 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2419 if (f[DIFF_LEFT] == NULL)
2420 return -1;
2422 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2423 if (f[DIFF_RIGHT] == NULL)
2425 f_close (f[DIFF_LEFT]);
2426 return -1;
2430 dview->args = args;
2431 dview->file[DIFF_LEFT] = file1;
2432 dview->file[DIFF_RIGHT] = file2;
2433 dview->label[DIFF_LEFT] = g_strdup (label1);
2434 dview->label[DIFF_RIGHT] = g_strdup (label2);
2435 dview->f[DIFF_LEFT] = f[0];
2436 dview->f[DIFF_RIGHT] = f[1];
2437 dview->merged[DIFF_LEFT] = FALSE;
2438 dview->merged[DIFF_RIGHT] = FALSE;
2439 dview->hdiff = NULL;
2440 dview->dsrc = dsrc;
2441 #ifdef HAVE_CHARSET
2442 dview->converter = str_cnv_from_term;
2443 dview_set_codeset (dview);
2444 #endif
2445 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2446 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2448 ndiff = redo_diff (dview);
2449 if (ndiff < 0)
2451 /* goto MSG_DESTROY stage: dview_fini() */
2452 f_close (f[DIFF_LEFT]);
2453 f_close (f[DIFF_RIGHT]);
2454 return -1;
2457 dview->ndiff = ndiff;
2459 dview->view_quit = FALSE;
2461 dview->bias = 0;
2462 dview->new_frame = TRUE;
2463 dview->skip_rows = 0;
2464 dview->skip_cols = 0;
2465 dview->display_symbols = 0;
2466 dview->display_numbers = 0;
2467 dview->show_cr = TRUE;
2468 dview->tab_size = 8;
2469 dview->ord = DIFF_LEFT;
2470 dview->full = FALSE;
2472 dview->search.handle = NULL;
2473 dview->search.last_string = NULL;
2474 dview->search.last_found_line = -1;
2475 dview->search.last_accessed_num_line = -1;
2477 dview->opt.quality = 0;
2478 dview->opt.strip_trailing_cr = 0;
2479 dview->opt.ignore_tab_expansion = 0;
2480 dview->opt.ignore_space_change = 0;
2481 dview->opt.ignore_all_space = 0;
2482 dview->opt.ignore_case = 0;
2484 dview_compute_areas (dview);
2486 return 0;
2489 /* --------------------------------------------------------------------------------------------- */
2491 static void
2492 dview_fini (WDiff * dview)
2494 if (dview->dsrc != DATA_SRC_MEM)
2496 f_close (dview->f[DIFF_RIGHT]);
2497 f_close (dview->f[DIFF_LEFT]);
2500 #ifdef HAVE_CHARSET
2501 if (dview->converter != str_cnv_from_term)
2502 str_close_conv (dview->converter);
2503 #endif
2505 destroy_hdiff (dview);
2506 if (dview->a[DIFF_LEFT] != NULL)
2508 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2509 g_array_free (dview->a[DIFF_LEFT], TRUE);
2510 dview->a[DIFF_LEFT] = NULL;
2512 if (dview->a[DIFF_RIGHT] != NULL)
2514 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2515 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2516 dview->a[DIFF_RIGHT] = NULL;
2519 g_free (dview->label[DIFF_LEFT]);
2520 g_free (dview->label[DIFF_RIGHT]);
2523 /* --------------------------------------------------------------------------------------------- */
2525 static int
2526 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2528 size_t i, k;
2529 int j;
2530 char buf[BUFSIZ];
2531 FBUF *f = dview->f[ord];
2532 int skip = dview->skip_cols;
2533 int display_symbols = dview->display_symbols;
2534 int display_numbers = dview->display_numbers;
2535 gboolean show_cr = dview->show_cr;
2536 int tab_size = 8;
2537 const DIFFLN *p;
2538 int nwidth = display_numbers;
2539 int xwidth;
2541 xwidth = display_symbols + display_numbers;
2542 if (dview->tab_size > 0 && dview->tab_size < 9)
2543 tab_size = dview->tab_size;
2545 if (xwidth != 0)
2547 if (xwidth > width && display_symbols)
2549 xwidth--;
2550 display_symbols = 0;
2552 if (xwidth > width && display_numbers)
2554 xwidth = width;
2555 display_numbers = width;
2558 xwidth++;
2559 c += xwidth;
2560 width -= xwidth;
2561 if (width < 0)
2562 width = 0;
2565 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2567 /* abnormal, but avoid buffer overflow */
2568 return -1;
2571 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2573 int ch, next_ch, col;
2574 size_t cnt;
2576 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2577 ch = p->ch;
2578 tty_setcolor (NORMAL_COLOR);
2579 if (display_symbols)
2581 tty_gotoyx (r + j, c - 2);
2582 tty_print_char (ch);
2584 if (p->line != 0)
2586 if (display_numbers)
2588 tty_gotoyx (r + j, c - xwidth);
2589 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2590 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2592 if (ch == ADD_CH)
2593 tty_setcolor (DFF_ADD_COLOR);
2594 if (ch == CHG_CH)
2595 tty_setcolor (DFF_CHG_COLOR);
2596 if (f == NULL)
2598 if (i == (size_t) dview->search.last_found_line)
2599 tty_setcolor (MARKED_SELECTED_COLOR);
2600 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2602 char att[BUFSIZ];
2604 #ifdef HAVE_CHARSET
2605 if (dview->utf8)
2606 k = dview_str_utf8_offset_to_pos (p->p, width);
2607 else
2608 #endif
2609 k = width;
2611 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2612 g_ptr_array_index (dview->hdiff, i), ord, att);
2613 tty_gotoyx (r + j, c);
2614 col = 0;
2616 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2618 gboolean ch_res;
2620 #ifdef HAVE_CHARSET
2621 if (dview->utf8)
2623 int ch_len = 0;
2625 next_ch = dview_get_utf (buf + cnt, &ch_len, &ch_res);
2626 if (ch_len > 1)
2627 cnt += ch_len - 1;
2628 if (!g_unichar_isprint (next_ch))
2629 next_ch = '.';
2631 else
2632 #endif
2633 next_ch = dview_get_byte (buf + cnt, &ch_res);
2635 if (ch_res)
2637 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2638 #ifdef HAVE_CHARSET
2639 if (mc_global.utf8_display)
2641 if (!dview->utf8)
2643 next_ch =
2644 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2645 dview->converter);
2648 else if (dview->utf8)
2649 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2650 else
2651 next_ch = convert_to_display_c (next_ch);
2652 #endif
2653 tty_print_anychar (next_ch);
2654 col++;
2657 continue;
2660 if (ch == CHG_CH)
2661 tty_setcolor (DFF_CHH_COLOR);
2663 #ifdef HAVE_CHARSET
2664 if (dview->utf8)
2665 k = dview_str_utf8_offset_to_pos (p->p, width);
2666 else
2667 #endif
2668 k = width;
2669 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2671 else
2672 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2674 else
2676 if (display_numbers)
2678 tty_gotoyx (r + j, c - xwidth);
2679 memset (buf, ' ', display_numbers);
2680 buf[display_numbers] = '\0';
2681 tty_print_string (buf);
2683 if (ch == DEL_CH)
2684 tty_setcolor (DFF_DEL_COLOR);
2685 if (ch == CHG_CH)
2686 tty_setcolor (DFF_CHD_COLOR);
2687 memset (buf, ' ', width);
2688 buf[width] = '\0';
2690 tty_gotoyx (r + j, c);
2691 /* tty_print_nstring (buf, width); */
2692 col = 0;
2693 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2695 gboolean ch_res;
2697 #ifdef HAVE_CHARSET
2698 if (dview->utf8)
2700 int ch_len = 0;
2702 next_ch = dview_get_utf (buf + cnt, &ch_len, &ch_res);
2703 if (ch_len > 1)
2704 cnt += ch_len - 1;
2705 if (!g_unichar_isprint (next_ch))
2706 next_ch = '.';
2708 else
2709 #endif
2710 next_ch = dview_get_byte (buf + cnt, &ch_res);
2711 if (ch_res)
2713 #ifdef HAVE_CHARSET
2714 if (mc_global.utf8_display)
2716 if (!dview->utf8)
2718 next_ch =
2719 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2722 else if (dview->utf8)
2723 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2724 else
2725 next_ch = convert_to_display_c (next_ch);
2726 #endif
2728 tty_print_anychar (next_ch);
2729 col++;
2733 tty_setcolor (NORMAL_COLOR);
2734 k = width;
2735 if (width < xwidth - 1)
2736 k = xwidth - 1;
2737 memset (buf, ' ', k);
2738 buf[k] = '\0';
2739 for (; j < height; j++)
2741 if (xwidth != 0)
2743 tty_gotoyx (r + j, c - xwidth);
2744 /* tty_print_nstring (buf, xwidth - 1); */
2745 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2747 tty_gotoyx (r + j, c);
2748 /* tty_print_nstring (buf, width); */
2749 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2752 return 0;
2755 /* --------------------------------------------------------------------------------------------- */
2757 static void
2758 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2760 const char *buf;
2761 int filename_width;
2762 int linenum, lineofs;
2763 vfs_path_t *vpath;
2764 char *path;
2766 tty_setcolor (STATUSBAR_COLOR);
2768 tty_gotoyx (0, c);
2769 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2771 filename_width = width - 24;
2772 if (filename_width < 8)
2773 filename_width = 8;
2775 vpath = vfs_path_from_str (dview->label[ord]);
2776 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2777 vfs_path_free (vpath);
2778 buf = str_term_trim (path, filename_width);
2779 if (ord == DIFF_LEFT)
2780 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2781 buf, linenum, lineofs, dview->skip_cols);
2782 else
2783 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2784 buf, linenum, lineofs, dview->ndiff);
2785 g_free (path);
2788 /* --------------------------------------------------------------------------------------------- */
2790 static void
2791 dview_redo (WDiff * dview)
2793 if (dview->display_numbers)
2795 int old;
2797 old = dview->display_numbers;
2798 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2799 dview->new_frame = (old != dview->display_numbers);
2801 dview_reread (dview);
2804 /* --------------------------------------------------------------------------------------------- */
2806 static void
2807 dview_update (WDiff * dview)
2809 int height = dview->height;
2810 int width1;
2811 int width2;
2812 int last;
2814 last = dview->a[DIFF_LEFT]->len - 1;
2816 if (dview->skip_rows > last)
2817 dview->skip_rows = dview->search.last_accessed_num_line = last;
2818 if (dview->skip_rows < 0)
2819 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2820 if (dview->skip_cols < 0)
2821 dview->skip_cols = 0;
2823 if (height < 2)
2824 return;
2826 width1 = dview->half1 + dview->bias;
2827 width2 = dview->half2 - dview->bias;
2828 if (dview->full)
2830 width1 = COLS;
2831 width2 = 0;
2834 if (dview->new_frame)
2836 int xwidth;
2838 tty_setcolor (NORMAL_COLOR);
2839 xwidth = dview->display_symbols + dview->display_numbers;
2840 if (width1 > 1)
2841 tty_draw_box (1, 0, height, width1, FALSE);
2842 if (width2 > 1)
2843 tty_draw_box (1, width1, height, width2, FALSE);
2845 if (xwidth != 0)
2847 xwidth++;
2848 if (xwidth < width1 - 1)
2850 tty_gotoyx (1, xwidth);
2851 tty_print_alt_char (ACS_TTEE, FALSE);
2852 tty_gotoyx (height, xwidth);
2853 tty_print_alt_char (ACS_BTEE, FALSE);
2854 tty_draw_vline (2, xwidth, ACS_VLINE, height - 2);
2856 if (xwidth < width2 - 1)
2858 tty_gotoyx (1, width1 + xwidth);
2859 tty_print_alt_char (ACS_TTEE, FALSE);
2860 tty_gotoyx (height, width1 + xwidth);
2861 tty_print_alt_char (ACS_BTEE, FALSE);
2862 tty_draw_vline (2, width1 + xwidth, ACS_VLINE, height - 2);
2865 dview->new_frame = FALSE;
2868 if (width1 > 2)
2870 dview_status (dview, dview->ord, width1, 0);
2871 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2873 if (width2 > 2)
2875 dview_status (dview, dview->ord ^ 1, width2, width1);
2876 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2880 /* --------------------------------------------------------------------------------------------- */
2882 static void
2883 dview_edit (WDiff * dview, diff_place_t ord)
2885 WDialog *h;
2886 gboolean h_modal;
2887 int linenum, lineofs;
2889 if (dview->dsrc == DATA_SRC_TMP)
2891 error_dialog (_("Edit"), _("Edit is disabled"));
2892 return;
2895 h = WIDGET (dview)->owner;
2896 h_modal = h->modal;
2898 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2899 h->modal = TRUE; /* not allow edit file in several editors */
2901 vfs_path_t *tmp_vpath;
2903 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2904 edit_file_at_line (tmp_vpath, use_internal_edit != 0, linenum);
2905 vfs_path_free (tmp_vpath);
2907 h->modal = h_modal;
2908 dview_redo (dview);
2909 dview_update (dview);
2912 /* --------------------------------------------------------------------------------------------- */
2914 static void
2915 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2917 static gboolean first_run = TRUE;
2919 /* *INDENT-OFF* */
2920 static const char *title[2] = {
2921 N_("Goto line (left)"),
2922 N_("Goto line (right)")
2924 /* *INDENT-ON* */
2926 int newline;
2927 char *input;
2929 input =
2930 input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE,
2931 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
2932 if (input != NULL)
2934 const char *s = input;
2936 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2938 size_t i = 0;
2940 if (newline > 0)
2942 for (; i < dview->a[ord]->len; i++)
2944 const DIFFLN *p;
2946 p = &g_array_index (dview->a[ord], DIFFLN, i);
2947 if (p->line == newline)
2948 break;
2951 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2953 g_free (input);
2956 first_run = FALSE;
2959 /* --------------------------------------------------------------------------------------------- */
2961 static void
2962 dview_labels (WDiff * dview)
2964 Widget *d;
2965 WDialog *h;
2966 WButtonBar *b;
2968 d = WIDGET (dview);
2969 h = d->owner;
2970 b = find_buttonbar (h);
2972 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, d);
2973 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, d);
2974 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, d);
2975 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, d);
2976 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, d);
2977 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, d);
2978 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, d);
2981 /* --------------------------------------------------------------------------------------------- */
2983 static gboolean
2984 dview_save (WDiff * dview)
2986 gboolean res = TRUE;
2988 if (dview->merged[DIFF_LEFT])
2990 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2991 dview->merged[DIFF_LEFT] = !res;
2993 if (dview->merged[DIFF_RIGHT])
2995 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2996 dview->merged[DIFF_RIGHT] = !res;
2998 return res;
3001 /* --------------------------------------------------------------------------------------------- */
3003 static void
3004 dview_do_save (WDiff * dview)
3006 (void) dview_save (dview);
3009 /* --------------------------------------------------------------------------------------------- */
3011 static void
3012 dview_save_options (WDiff * dview)
3014 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
3015 dview->display_symbols != 0 ? TRUE : FALSE);
3016 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
3017 dview->display_numbers != 0 ? TRUE : FALSE);
3018 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
3020 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
3022 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
3023 dview->opt.strip_trailing_cr);
3024 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
3025 dview->opt.ignore_all_space);
3026 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
3027 dview->opt.ignore_space_change);
3028 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
3029 dview->opt.ignore_tab_expansion);
3030 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
3033 /* --------------------------------------------------------------------------------------------- */
3035 static void
3036 dview_load_options (WDiff * dview)
3038 gboolean show_numbers, show_symbols;
3039 int tab_size;
3041 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
3042 if (show_symbols)
3043 dview->display_symbols = 1;
3044 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
3045 if (show_numbers)
3046 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
3047 tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
3048 if (tab_size > 0 && tab_size < 9)
3049 dview->tab_size = tab_size;
3050 else
3051 dview->tab_size = 8;
3053 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
3055 dview->opt.strip_trailing_cr =
3056 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
3057 dview->opt.ignore_all_space =
3058 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
3059 dview->opt.ignore_space_change =
3060 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
3061 dview->opt.ignore_tab_expansion =
3062 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
3063 dview->opt.ignore_case =
3064 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
3066 dview->new_frame = TRUE;
3069 /* --------------------------------------------------------------------------------------------- */
3072 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3073 * ask user.
3075 static gboolean
3076 dview_ok_to_exit (WDiff * dview)
3078 gboolean res = TRUE;
3079 int act;
3081 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3082 return res;
3084 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3085 _("File(s) was modified. Save with exit?") :
3086 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3087 D_NORMAL, 2, _("&Yes"), _("&No"));
3089 /* Esc is No */
3090 if (mc_global.midnight_shutdown || (act == -1))
3091 act = 1;
3093 switch (act)
3095 case -1: /* Esc */
3096 res = FALSE;
3097 break;
3098 case 0: /* Yes */
3099 (void) dview_save (dview);
3100 res = TRUE;
3101 break;
3102 case 1: /* No */
3103 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3104 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3105 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3106 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3107 /* fall through */
3108 default:
3109 res = TRUE;
3110 break;
3112 return res;
3115 /* --------------------------------------------------------------------------------------------- */
3117 static cb_ret_t
3118 dview_execute_cmd (WDiff * dview, long command)
3120 cb_ret_t res = MSG_HANDLED;
3122 switch (command)
3124 case CK_ShowSymbols:
3125 dview->display_symbols ^= 1;
3126 dview->new_frame = TRUE;
3127 break;
3128 case CK_ShowNumbers:
3129 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
3130 dview->new_frame = TRUE;
3131 break;
3132 case CK_SplitFull:
3133 dview->full = !dview->full;
3134 dview->new_frame = TRUE;
3135 break;
3136 case CK_SplitEqual:
3137 if (!dview->full)
3139 dview->bias = 0;
3140 dview->new_frame = TRUE;
3142 break;
3143 case CK_SplitMore:
3144 if (!dview->full)
3146 dview_compute_split (dview, 1);
3147 dview->new_frame = TRUE;
3149 break;
3151 case CK_SplitLess:
3152 if (!dview->full)
3154 dview_compute_split (dview, -1);
3155 dview->new_frame = TRUE;
3157 break;
3158 case CK_Tab2:
3159 dview->tab_size = 2;
3160 break;
3161 case CK_Tab3:
3162 dview->tab_size = 3;
3163 break;
3164 case CK_Tab4:
3165 dview->tab_size = 4;
3166 break;
3167 case CK_Tab8:
3168 dview->tab_size = 8;
3169 break;
3170 case CK_Swap:
3171 dview->ord ^= 1;
3172 break;
3173 case CK_Redo:
3174 dview_redo (dview);
3175 break;
3176 case CK_HunkNext:
3177 dview->skip_rows = dview->search.last_accessed_num_line =
3178 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3179 break;
3180 case CK_HunkPrev:
3181 dview->skip_rows = dview->search.last_accessed_num_line =
3182 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3183 break;
3184 case CK_Goto:
3185 dview_goto_cmd (dview, DIFF_RIGHT);
3186 break;
3187 case CK_Edit:
3188 dview_edit (dview, dview->ord);
3189 break;
3190 case CK_Merge:
3191 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3192 dview_redo (dview);
3193 break;
3194 case CK_MergeOther:
3195 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3196 dview_redo (dview);
3197 break;
3198 case CK_EditOther:
3199 dview_edit (dview, dview->ord ^ 1);
3200 break;
3201 case CK_Search:
3202 dview_search_cmd (dview);
3203 break;
3204 case CK_SearchContinue:
3205 dview_continue_search_cmd (dview);
3206 break;
3207 case CK_Top:
3208 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3209 break;
3210 case CK_Bottom:
3211 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3212 break;
3213 case CK_Up:
3214 if (dview->skip_rows > 0)
3216 dview->skip_rows--;
3217 dview->search.last_accessed_num_line = dview->skip_rows;
3219 break;
3220 case CK_Down:
3221 dview->skip_rows++;
3222 dview->search.last_accessed_num_line = dview->skip_rows;
3223 break;
3224 case CK_PageDown:
3225 if (dview->height > 2)
3227 dview->skip_rows += dview->height - 2;
3228 dview->search.last_accessed_num_line = dview->skip_rows;
3230 break;
3231 case CK_PageUp:
3232 if (dview->height > 2)
3234 dview->skip_rows -= dview->height - 2;
3235 dview->search.last_accessed_num_line = dview->skip_rows;
3237 break;
3238 case CK_Left:
3239 dview->skip_cols--;
3240 break;
3241 case CK_Right:
3242 dview->skip_cols++;
3243 break;
3244 case CK_LeftQuick:
3245 dview->skip_cols -= 8;
3246 break;
3247 case CK_RightQuick:
3248 dview->skip_cols += 8;
3249 break;
3250 case CK_Home:
3251 dview->skip_cols = 0;
3252 break;
3253 case CK_Shell:
3254 view_other_cmd ();
3255 break;
3256 case CK_Quit:
3257 dview->view_quit = TRUE;
3258 break;
3259 case CK_Save:
3260 dview_do_save (dview);
3261 break;
3262 case CK_Options:
3263 dview_diff_options (dview);
3264 break;
3265 #ifdef HAVE_CHARSET
3266 case CK_SelectCodepage:
3267 dview_select_encoding (dview);
3268 break;
3269 #endif
3270 case CK_Cancel:
3271 /* don't close diffviewer due to SIGINT */
3272 break;
3273 default:
3274 res = MSG_NOT_HANDLED;
3276 return res;
3279 /* --------------------------------------------------------------------------------------------- */
3281 static cb_ret_t
3282 dview_handle_key (WDiff * dview, int key)
3284 long command;
3286 #ifdef HAVE_CHARSET
3287 key = convert_from_input_c (key);
3288 #endif
3290 command = keybind_lookup_keymap_command (diff_map, key);
3291 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3292 return MSG_HANDLED;
3294 /* Key not used */
3295 return MSG_NOT_HANDLED;
3298 /* --------------------------------------------------------------------------------------------- */
3300 static cb_ret_t
3301 dview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3303 WDiff *dview = (WDiff *) w;
3304 WDialog *h = w->owner;
3305 cb_ret_t i;
3307 switch (msg)
3309 case MSG_INIT:
3310 dview_labels (dview);
3311 dview_load_options (dview);
3312 dview_update (dview);
3313 return MSG_HANDLED;
3315 case MSG_DRAW:
3316 dview->new_frame = TRUE;
3317 dview_update (dview);
3318 return MSG_HANDLED;
3320 case MSG_KEY:
3321 i = dview_handle_key (dview, parm);
3322 if (dview->view_quit)
3323 dlg_stop (h);
3324 else
3325 dview_update (dview);
3326 return i;
3328 case MSG_ACTION:
3329 i = dview_execute_cmd (dview, parm);
3330 if (dview->view_quit)
3331 dlg_stop (h);
3332 else
3333 dview_update (dview);
3334 return i;
3336 case MSG_DESTROY:
3337 dview_save_options (dview);
3338 dview_fini (dview);
3339 return MSG_HANDLED;
3341 default:
3342 return widget_default_callback (w, sender, msg, parm, data);
3346 /* --------------------------------------------------------------------------------------------- */
3348 static void
3349 dview_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3351 WDiff *dview = (WDiff *) w;
3353 (void) event;
3355 switch (msg)
3357 case MSG_MOUSE_SCROLL_UP:
3358 case MSG_MOUSE_SCROLL_DOWN:
3359 if (msg == MSG_MOUSE_SCROLL_UP)
3360 dview->skip_rows -= 2;
3361 else
3362 dview->skip_rows += 2;
3364 dview->search.last_accessed_num_line = dview->skip_rows;
3365 dview_update (dview);
3366 break;
3368 default:
3369 break;
3373 /* --------------------------------------------------------------------------------------------- */
3375 static void
3376 dview_adjust_size (WDialog * h)
3378 WDiff *dview;
3379 WButtonBar *bar;
3381 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3382 dview = (WDiff *) find_widget_type (h, dview_callback);
3383 bar = find_buttonbar (h);
3384 widget_set_size (WIDGET (dview), 0, 0, LINES - 1, COLS);
3385 widget_set_size (WIDGET (bar), LINES - 1, 0, 1, COLS);
3387 dview_compute_areas (dview);
3390 /* --------------------------------------------------------------------------------------------- */
3392 static cb_ret_t
3393 dview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3395 WDiff *dview = (WDiff *) data;
3396 WDialog *h = DIALOG (w);
3398 switch (msg)
3400 case MSG_RESIZE:
3401 dview_adjust_size (h);
3402 return MSG_HANDLED;
3404 case MSG_ACTION:
3405 /* Handle shortcuts. */
3407 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3408 * here, which is why we can pass NULL in the following call. */
3409 return dview_execute_cmd (NULL, parm);
3411 case MSG_VALIDATE:
3412 dview = (WDiff *) find_widget_type (h, dview_callback);
3413 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
3414 if (dview_ok_to_exit (dview))
3415 h->state = DLG_CLOSED;
3416 return MSG_HANDLED;
3418 default:
3419 return dlg_default_callback (w, sender, msg, parm, data);
3423 /* --------------------------------------------------------------------------------------------- */
3425 static char *
3426 dview_get_title (const WDialog * h, size_t len)
3428 const WDiff *dview;
3429 const char *modified = " (*) ";
3430 const char *notmodified = " ";
3431 size_t len1;
3432 GString *title;
3434 dview = (const WDiff *) find_widget_type (h, dview_callback);
3435 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3437 title = g_string_sized_new (len);
3438 g_string_append (title, _("Diff:"));
3439 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3440 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3441 g_string_append (title, " | ");
3442 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3443 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3445 return g_string_free (title, FALSE);
3448 /* --------------------------------------------------------------------------------------------- */
3450 static int
3451 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3453 int error;
3454 WDiff *dview;
3455 Widget *w;
3456 WDialog *dview_dlg;
3458 /* Create dialog and widgets, put them on the dialog */
3459 dview_dlg =
3460 dlg_create (FALSE, 0, 0, LINES, COLS, NULL, dview_dialog_callback, NULL,
3461 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3463 dview = g_new0 (WDiff, 1);
3464 w = WIDGET (dview);
3465 widget_init (w, 0, 0, LINES - 1, COLS, dview_callback, dview_mouse_callback);
3466 widget_want_cursor (w, FALSE);
3468 add_widget (dview_dlg, dview);
3469 add_widget (dview_dlg, buttonbar_new (TRUE));
3471 dview_dlg->get_title = dview_get_title;
3473 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3475 /* Please note that if you add another widget,
3476 * you have to modify dview_adjust_size to
3477 * be aware of it
3479 if (error == 0)
3480 dlg_run (dview_dlg);
3482 if ((error != 0) || (dview_dlg->state == DLG_CLOSED))
3483 dlg_destroy (dview_dlg);
3485 return error == 0 ? 1 : 0;
3488 /*** public functions ****************************************************************************/
3489 /* --------------------------------------------------------------------------------------------- */
3491 #define GET_FILE_AND_STAMP(n) \
3492 do \
3494 use_copy##n = 0; \
3495 real_file##n = file##n; \
3496 if (!vfs_file_is_local (file##n)) \
3498 real_file##n = mc_getlocalcopy (file##n); \
3499 if (real_file##n != NULL) \
3501 use_copy##n = 1; \
3502 if (mc_stat (real_file##n, &st##n) != 0) \
3503 use_copy##n = -1; \
3507 while (0)
3509 #define UNGET_FILE(n) \
3510 do \
3512 if (use_copy##n) \
3514 int changed = 0; \
3515 if (use_copy##n > 0) \
3517 time_t mtime; \
3518 mtime = st##n.st_mtime; \
3519 if (mc_stat (real_file##n, &st##n) == 0) \
3520 changed = (mtime != st##n.st_mtime); \
3522 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3523 vfs_path_free (real_file##n); \
3526 while (0)
3528 gboolean
3529 dview_diff_cmd (const void *f0, const void *f1)
3531 int rv = 0;
3532 vfs_path_t *file0 = NULL;
3533 vfs_path_t *file1 = NULL;
3534 gboolean is_dir0 = FALSE;
3535 gboolean is_dir1 = FALSE;
3537 switch (mc_global.mc_run_mode)
3539 case MC_RUN_FULL:
3541 /* run from panels */
3542 const WPanel *panel0 = (const WPanel *) f0;
3543 const WPanel *panel1 = (const WPanel *) f1;
3545 file0 = vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, NULL);
3546 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3547 if (is_dir0)
3549 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3550 path_trunc (selection (panel0)->fname, 30));
3551 goto ret;
3554 file1 = vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, NULL);
3555 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3556 if (is_dir1)
3558 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3559 path_trunc (selection (panel1)->fname, 30));
3560 goto ret;
3562 break;
3565 case MC_RUN_DIFFVIEWER:
3567 /* run from command line */
3568 const char *p0 = (const char *) f0;
3569 const char *p1 = (const char *) f1;
3570 struct stat st;
3572 file0 = vfs_path_from_str (p0);
3573 if (mc_stat (file0, &st) == 0)
3575 is_dir0 = S_ISDIR (st.st_mode);
3576 if (is_dir0)
3578 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3579 goto ret;
3582 else
3584 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3585 path_trunc (p0, 30), unix_error_string (errno));
3586 goto ret;
3589 file1 = vfs_path_from_str (p1);
3590 if (mc_stat (file1, &st) == 0)
3592 is_dir1 = S_ISDIR (st.st_mode);
3593 if (is_dir1)
3595 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3596 goto ret;
3599 else
3601 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3602 path_trunc (p1, 30), unix_error_string (errno));
3603 goto ret;
3605 break;
3608 default:
3609 /* this should not happaned */
3610 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3611 return FALSE;
3614 if (rv == 0)
3616 rv = -1;
3617 if (file0 != NULL && file1 != NULL)
3619 int use_copy0;
3620 int use_copy1;
3621 struct stat st0;
3622 struct stat st1;
3623 vfs_path_t *real_file0;
3624 vfs_path_t *real_file1;
3626 GET_FILE_AND_STAMP (0);
3627 GET_FILE_AND_STAMP (1);
3629 if (real_file0 != NULL && real_file1 != NULL)
3630 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3631 vfs_path_as_str (file0), vfs_path_as_str (file1));
3633 UNGET_FILE (1);
3634 UNGET_FILE (0);
3638 if (rv == 0)
3639 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3641 ret:
3642 vfs_path_free (file1);
3643 vfs_path_free (file0);
3645 return (rv != 0);
3648 #undef GET_FILE_AND_STAMP
3649 #undef UNGET_FILE
3651 /* --------------------------------------------------------------------------------------------- */