Move widget add/del API from WDialog to WGroup.
[midnight-commander.git] / src / diffviewer / ydiff.c
blob8eb9f889d44746be05d56fdffcc88824b0ccbb81
1 /*
2 File difference viewer
4 Copyright (C) 2007-2020
5 Free Software Foundation, Inc.
7 Written by:
8 Daniel Borca <dborca@yahoo.com>, 2007
9 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
10 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013, 2016
11 Ilia Maslakov <il.smind@gmail.com>, 2010
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <config.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
38 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/color.h"
41 #include "lib/tty/key.h"
42 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
43 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
44 #include "lib/util.h"
45 #include "lib/widget.h"
46 #include "lib/strutil.h"
47 #include "lib/strescape.h" /* strutils_glob_escape() */
48 #ifdef HAVE_CHARSET
49 #include "lib/charsets.h"
50 #endif
51 #include "lib/event.h" /* mc_event_raise() */
53 #include "src/filemanager/cmd.h" /* edit_file_at_line() */
54 #include "src/filemanager/panel.h"
55 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
57 #include "src/execute.h" /* toggle_subshell() */
58 #include "src/keybind-defaults.h"
59 #include "src/setup.h"
60 #include "src/history.h"
61 #ifdef HAVE_CHARSET
62 #include "src/selcodepage.h"
63 #endif
65 #include "ydiff.h"
66 #include "internal.h"
68 /*** global variables ****************************************************************************/
70 /*** file scope macro definitions ****************************************************************/
72 #define g_array_foreach(a, TP, cbf) \
73 do { \
74 size_t g_array_foreach_i;\
76 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
77 { \
78 TP *g_array_foreach_var; \
80 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
81 (*cbf) (g_array_foreach_var); \
82 } \
83 } while (0)
85 #define FILE_READ_BUF 4096
86 #define FILE_FLAG_TEMP (1 << 0)
88 #define ADD_CH '+'
89 #define DEL_CH '-'
90 #define CHG_CH '*'
91 #define EQU_CH ' '
93 #define HDIFF_ENABLE 1
94 #define HDIFF_MINCTX 5
95 #define HDIFF_DEPTH 10
97 #define FILE_DIRTY(fs) \
98 do \
99 { \
100 (fs)->pos = 0; \
101 (fs)->len = 0; \
103 while (0)
105 /*** file scope type declarations ****************************************************************/
107 typedef enum
109 FROM_LEFT_TO_RIGHT,
110 FROM_RIGHT_TO_LEFT
111 } action_direction_t;
113 /*** file scope variables ************************************************************************/
115 /*** file scope functions ************************************************************************/
116 /* --------------------------------------------------------------------------------------------- */
118 static inline int
119 TAB_SKIP (int ts, int pos)
121 if (ts > 0 && ts < 9)
122 return ts - pos % ts;
123 else
124 return 8 - pos % 8;
127 /* --------------------------------------------------------------------------------------------- */
129 static gboolean
130 rewrite_backup_content (const vfs_path_t * from_file_name_vpath, const char *to_file_name)
132 FILE *backup_fd;
133 char *contents;
134 gsize length;
135 const char *from_file_name;
137 from_file_name = vfs_path_get_by_index (from_file_name_vpath, -1)->path;
138 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
139 return FALSE;
141 backup_fd = fopen (to_file_name, "w");
142 if (backup_fd == NULL)
144 g_free (contents);
145 return FALSE;
148 length = fwrite ((const void *) contents, length, 1, backup_fd);
150 fflush (backup_fd);
151 fclose (backup_fd);
152 g_free (contents);
153 return TRUE;
156 /* buffered I/O ************************************************************* */
159 * Try to open a temporary file.
160 * @note the name is not altered if this function fails
162 * @param[out] name address of a pointer to store the temporary name
163 * @return file descriptor on success, negative on error
166 static int
167 open_temp (void **name)
169 int fd;
170 vfs_path_t *diff_file_name = NULL;
172 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
173 if (fd == -1)
175 message (D_ERROR, MSG_ERROR,
176 _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
177 return -1;
179 *name = g_strdup (vfs_path_as_str (diff_file_name));
180 vfs_path_free (diff_file_name);
181 return fd;
184 /* --------------------------------------------------------------------------------------------- */
187 * Alocate file structure and associate file descriptor to it.
189 * @param fd file descriptor
190 * @return file structure
193 static FBUF *
194 f_dopen (int fd)
196 FBUF *fs;
198 if (fd < 0)
199 return NULL;
201 fs = g_try_malloc (sizeof (FBUF));
202 if (fs == NULL)
203 return NULL;
205 fs->buf = g_try_malloc (FILE_READ_BUF);
206 if (fs->buf == NULL)
208 g_free (fs);
209 return NULL;
212 fs->fd = fd;
213 FILE_DIRTY (fs);
214 fs->flags = 0;
215 fs->data = NULL;
217 return fs;
220 /* --------------------------------------------------------------------------------------------- */
223 * Free file structure without closing the file.
225 * @param fs file structure
226 * @return 0 on success, non-zero on error
229 static int
230 f_free (FBUF * fs)
232 int rv = 0;
234 if (fs->flags & FILE_FLAG_TEMP)
236 rv = unlink (fs->data);
237 g_free (fs->data);
239 g_free (fs->buf);
240 g_free (fs);
241 return rv;
244 /* --------------------------------------------------------------------------------------------- */
247 * Open a binary temporary file in R/W mode.
248 * @note the file will be deleted when closed
250 * @return file structure
252 static FBUF *
253 f_temp (void)
255 int fd;
256 FBUF *fs;
258 fs = f_dopen (0);
259 if (fs == NULL)
260 return NULL;
262 fd = open_temp (&fs->data);
263 if (fd < 0)
265 f_free (fs);
266 return NULL;
269 fs->fd = fd;
270 fs->flags = FILE_FLAG_TEMP;
271 return fs;
274 /* --------------------------------------------------------------------------------------------- */
277 * Open a binary file in specified mode.
279 * @param filename file name
280 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
282 * @return file structure
285 static FBUF *
286 f_open (const char *filename, int flags)
288 int fd;
289 FBUF *fs;
291 fs = f_dopen (0);
292 if (fs == NULL)
293 return NULL;
295 fd = open (filename, flags);
296 if (fd < 0)
298 f_free (fs);
299 return NULL;
302 fs->fd = fd;
303 return fs;
306 /* --------------------------------------------------------------------------------------------- */
309 * Read a line of bytes from file until newline or EOF.
310 * @note does not stop on null-byte
311 * @note buf will not be null-terminated
313 * @param buf destination buffer
314 * @param size size of buffer
315 * @param fs file structure
317 * @return number of bytes read
320 static size_t
321 f_gets (char *buf, size_t size, FBUF * fs)
323 size_t j = 0;
327 int i;
328 int stop = 0;
330 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
332 buf[j] = fs->buf[i];
333 if (buf[j] == '\n')
334 stop = 1;
336 fs->pos = i;
338 if (j == size || stop)
339 break;
341 fs->pos = 0;
342 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
344 while (fs->len > 0);
346 return j;
349 /* --------------------------------------------------------------------------------------------- */
352 * Seek into file.
353 * @note avoids thrashing read cache when possible
355 * @param fs file structure
356 * @param off offset
357 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
359 * @return position in file, starting from begginning
362 static off_t
363 f_seek (FBUF * fs, off_t off, int whence)
365 off_t rv;
367 if (fs->len && whence != SEEK_END)
369 rv = lseek (fs->fd, 0, SEEK_CUR);
370 if (rv != -1)
372 if (whence == SEEK_CUR)
374 whence = SEEK_SET;
375 off += rv - fs->len + fs->pos;
377 if (off - rv >= -fs->len && off - rv <= 0)
379 fs->pos = fs->len + off - rv;
380 return off;
385 rv = lseek (fs->fd, off, whence);
386 if (rv != -1)
387 FILE_DIRTY (fs);
388 return rv;
391 /* --------------------------------------------------------------------------------------------- */
394 * Seek to the beginning of file, thrashing read cache.
396 * @param fs file structure
398 * @return 0 if success, non-zero on error
401 static off_t
402 f_reset (FBUF * fs)
404 off_t rv;
406 rv = lseek (fs->fd, 0, SEEK_SET);
407 if (rv != -1)
408 FILE_DIRTY (fs);
409 return rv;
412 /* --------------------------------------------------------------------------------------------- */
415 * Write bytes to file.
416 * @note thrashes read cache
418 * @param fs file structure
419 * @param buf source buffer
420 * @param size size of buffer
422 * @return number of written bytes, -1 on error
425 static ssize_t
426 f_write (FBUF * fs, const char *buf, size_t size)
428 ssize_t rv;
430 rv = write (fs->fd, buf, size);
431 if (rv >= 0)
432 FILE_DIRTY (fs);
433 return rv;
436 /* --------------------------------------------------------------------------------------------- */
439 * Truncate file to the current position.
440 * @note thrashes read cache
442 * @param fs file structure
444 * @return current file size on success, negative on error
447 static off_t
448 f_trunc (FBUF * fs)
450 off_t off;
452 off = lseek (fs->fd, 0, SEEK_CUR);
453 if (off != -1)
455 int rv;
457 rv = ftruncate (fs->fd, off);
458 if (rv != 0)
459 off = -1;
460 else
461 FILE_DIRTY (fs);
463 return off;
466 /* --------------------------------------------------------------------------------------------- */
469 * Close file.
470 * @note if this is temporary file, it is deleted
472 * @param fs file structure
473 * @return 0 on success, non-zero on error
476 static int
477 f_close (FBUF * fs)
479 int rv = -1;
481 if (fs != NULL)
483 rv = close (fs->fd);
484 f_free (fs);
487 return rv;
490 /* --------------------------------------------------------------------------------------------- */
493 * Create pipe stream to process.
495 * @param cmd shell command line
496 * @param flags open mode, either O_RDONLY or O_WRONLY
498 * @return file structure
501 static FBUF *
502 p_open (const char *cmd, int flags)
504 FILE *f;
505 FBUF *fs;
506 const char *type = NULL;
508 if (flags == O_RDONLY)
509 type = "r";
510 else if (flags == O_WRONLY)
511 type = "w";
513 if (type == NULL)
514 return NULL;
516 fs = f_dopen (0);
517 if (fs == NULL)
518 return NULL;
520 f = popen (cmd, type);
521 if (f == NULL)
523 f_free (fs);
524 return NULL;
527 fs->fd = fileno (f);
528 fs->data = f;
529 return fs;
532 /* --------------------------------------------------------------------------------------------- */
535 * Close pipe stream.
537 * @param fs structure
538 * @return 0 on success, non-zero on error
541 static int
542 p_close (FBUF * fs)
544 int rv = -1;
546 if (fs != NULL)
548 rv = pclose (fs->data);
549 f_free (fs);
552 return rv;
555 /* --------------------------------------------------------------------------------------------- */
558 * Get one char (byte) from string
560 * @param str ...
561 * @param ch ...
562 * @return TRUE on success, FALSE otherwise
565 static gboolean
566 dview_get_byte (const char *str, int *ch)
568 if (str == NULL)
569 return FALSE;
571 *ch = (unsigned char) (*str);
572 return TRUE;
575 /* --------------------------------------------------------------------------------------------- */
577 #ifdef HAVE_CHARSET
579 * Get utf multibyte char from string
581 * @param str ...
582 * @param ch ...
583 * @param ch_length ...
584 * @return TRUE on success, FALSE otherwise
587 static gboolean
588 dview_get_utf (const char *str, int *ch, int *ch_length)
590 if (str == NULL)
591 return FALSE;
593 *ch = g_utf8_get_char_validated (str, -1);
595 if (*ch < 0)
597 *ch = (unsigned char) (*str);
598 *ch_length = 1;
600 else
602 char *next_ch;
604 /* Calculate UTF-8 char length */
605 next_ch = g_utf8_next_char (str);
606 *ch_length = next_ch - str;
609 return TRUE;
612 /* --------------------------------------------------------------------------------------------- */
614 static int
615 dview_str_utf8_offset_to_pos (const char *text, size_t length)
617 ptrdiff_t result;
619 if (text == NULL || text[0] == '\0')
620 return length;
622 if (g_utf8_validate (text, -1, NULL))
623 result = g_utf8_offset_to_pointer (text, length) - text;
624 else
626 gunichar uni;
627 char *tmpbuf, *buffer;
629 buffer = tmpbuf = g_strdup (text);
630 while (tmpbuf[0] != '\0')
632 uni = g_utf8_get_char_validated (tmpbuf, -1);
633 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
634 tmpbuf = g_utf8_next_char (tmpbuf);
635 else
637 tmpbuf[0] = '.';
638 tmpbuf++;
641 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
642 g_free (buffer);
644 return MAX (length, (size_t) result);
646 #endif /*HAVE_CHARSET */
648 /* --------------------------------------------------------------------------------------------- */
650 /* diff parse *************************************************************** */
653 * Read decimal number from string.
655 * @param[in,out] str string to parse
656 * @param[out] n extracted number
657 * @return 0 if success, otherwise non-zero
660 static int
661 scan_deci (const char **str, int *n)
663 const char *p = *str;
664 char *q;
666 errno = 0;
667 *n = strtol (p, &q, 10);
668 if (errno != 0 || p == q)
669 return -1;
670 *str = q;
671 return 0;
674 /* --------------------------------------------------------------------------------------------- */
677 * Parse line for diff statement.
679 * @param p string to parse
680 * @param ops list of diff statements
681 * @return 0 if success, otherwise non-zero
684 static int
685 scan_line (const char *p, GArray * ops)
687 DIFFCMD op;
689 int f1, f2;
690 int t1, t2;
691 int cmd;
692 int range;
694 /* handle the following cases:
695 * NUMaNUM[,NUM]
696 * NUM[,NUM]cNUM[,NUM]
697 * NUM[,NUM]dNUM
698 * where NUM is a positive integer
701 if (scan_deci (&p, &f1) != 0 || f1 < 0)
702 return -1;
704 f2 = f1;
705 range = 0;
706 if (*p == ',')
708 p++;
709 if (scan_deci (&p, &f2) != 0 || f2 < f1)
710 return -1;
712 range = 1;
715 cmd = *p++;
716 if (cmd == 'a')
718 if (range != 0)
719 return -1;
721 else if (cmd != 'c' && cmd != 'd')
722 return -1;
724 if (scan_deci (&p, &t1) != 0 || t1 < 0)
725 return -1;
727 t2 = t1;
728 range = 0;
729 if (*p == ',')
731 p++;
732 if (scan_deci (&p, &t2) != 0 || t2 < t1)
733 return -1;
735 range = 1;
738 if (cmd == 'd' && range != 0)
739 return -1;
741 op.a[0][0] = f1;
742 op.a[0][1] = f2;
743 op.cmd = cmd;
744 op.a[1][0] = t1;
745 op.a[1][1] = t2;
746 g_array_append_val (ops, op);
747 return 0;
750 /* --------------------------------------------------------------------------------------------- */
753 * Parse diff output and extract diff statements.
755 * @param f stream to read from
756 * @param ops list of diff statements to fill
757 * @return positive number indicating number of hunks, otherwise negative
760 static int
761 scan_diff (FBUF * f, GArray * ops)
763 int sz;
764 char buf[BUFSIZ];
766 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
768 if (isdigit (buf[0]))
770 if (buf[sz - 1] != '\n')
771 return -1;
773 buf[sz] = '\0';
774 if (scan_line (buf, ops) != 0)
775 return -1;
777 continue;
780 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
784 return ops->len;
787 /* --------------------------------------------------------------------------------------------- */
790 * Invoke diff and extract diff statements.
792 * @param args extra arguments to be passed to diff
793 * @param extra more arguments to be passed to diff
794 * @param file1 first file to compare
795 * @param file2 second file to compare
796 * @param ops list of diff statements to fill
798 * @return positive number indicating number of hunks, otherwise negative
801 static int
802 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
803 GArray * ops)
805 static const char *opt =
806 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
807 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
808 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
809 " --unchanged-group-format=''";
811 int rv;
812 FBUF *f;
813 char *cmd;
814 int code;
815 char *file1_esc, *file2_esc;
817 /* escape potential $ to avoid shell variable substitutions in popen() */
818 file1_esc = strutils_shell_escape (file1);
819 file2_esc = strutils_shell_escape (file2);
820 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
821 g_free (file1_esc);
822 g_free (file2_esc);
824 if (cmd == NULL)
825 return -1;
827 f = p_open (cmd, O_RDONLY);
828 g_free (cmd);
830 if (f == NULL)
831 return -1;
833 rv = scan_diff (f, ops);
834 code = p_close (f);
836 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
837 rv = -1;
839 return rv;
842 /* --------------------------------------------------------------------------------------------- */
845 * Reparse and display file according to diff statements.
847 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
848 * @param filename file name to display
849 * @param ops list of diff statements
850 * @param printer printf-like function to be used for displaying
851 * @param ctx printer context
853 * @return 0 if success, otherwise non-zero
856 static int
857 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
859 size_t i;
860 FBUF *f;
861 size_t sz;
862 char buf[BUFSIZ];
863 int line = 0;
864 off_t off = 0;
865 const DIFFCMD *op;
866 diff_place_t eff;
867 int add_cmd;
868 int del_cmd;
870 f = f_open (filename, O_RDONLY);
871 if (f == NULL)
872 return -1;
874 ord &= 1;
875 eff = ord;
877 add_cmd = 'a';
878 del_cmd = 'd';
879 if (ord != 0)
881 add_cmd = 'd';
882 del_cmd = 'a';
884 #define F1 a[eff][0]
885 #define F2 a[eff][1]
886 #define T1 a[ ord^1 ][0]
887 #define T2 a[ ord^1 ][1]
888 for (i = 0; i < ops->len; i++)
890 int n;
892 op = &g_array_index (ops, DIFFCMD, i);
893 n = op->F1 - (op->cmd != add_cmd);
895 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
897 line++;
898 printer (ctx, EQU_CH, line, off, sz, buf);
899 off += sz;
900 while (buf[sz - 1] != '\n')
902 sz = f_gets (buf, sizeof (buf), f);
903 if (sz == 0)
905 printer (ctx, 0, 0, 0, 1, "\n");
906 break;
908 printer (ctx, 0, 0, 0, sz, buf);
909 off += sz;
913 if (line != n)
914 goto err;
916 if (op->cmd == add_cmd)
918 n = op->T2 - op->T1 + 1;
919 while (n != 0)
921 printer (ctx, DEL_CH, 0, 0, 1, "\n");
922 n--;
926 if (op->cmd == del_cmd)
928 n = op->F2 - op->F1 + 1;
929 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
931 line++;
932 printer (ctx, ADD_CH, line, off, sz, buf);
933 off += sz;
934 while (buf[sz - 1] != '\n')
936 sz = f_gets (buf, sizeof (buf), f);
937 if (sz == 0)
939 printer (ctx, 0, 0, 0, 1, "\n");
940 break;
942 printer (ctx, 0, 0, 0, sz, buf);
943 off += sz;
945 n--;
948 if (n != 0)
949 goto err;
952 if (op->cmd == 'c')
954 n = op->F2 - op->F1 + 1;
955 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
957 line++;
958 printer (ctx, CHG_CH, line, off, sz, buf);
959 off += sz;
960 while (buf[sz - 1] != '\n')
962 sz = f_gets (buf, sizeof (buf), f);
963 if (sz == 0)
965 printer (ctx, 0, 0, 0, 1, "\n");
966 break;
968 printer (ctx, 0, 0, 0, sz, buf);
969 off += sz;
971 n--;
974 if (n != 0)
975 goto err;
977 n = op->T2 - op->T1 - (op->F2 - op->F1);
978 while (n > 0)
980 printer (ctx, CHG_CH, 0, 0, 1, "\n");
981 n--;
985 #undef T2
986 #undef T1
987 #undef F2
988 #undef F1
990 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
992 line++;
993 printer (ctx, EQU_CH, line, off, sz, buf);
994 off += sz;
995 while (buf[sz - 1] != '\n')
997 sz = f_gets (buf, sizeof (buf), f);
998 if (sz == 0)
1000 printer (ctx, 0, 0, 0, 1, "\n");
1001 break;
1003 printer (ctx, 0, 0, 0, sz, buf);
1004 off += sz;
1008 f_close (f);
1009 return 0;
1011 err:
1012 f_close (f);
1013 return -1;
1016 /* --------------------------------------------------------------------------------------------- */
1018 /* horizontal diff ********************************************************** */
1021 * Longest common substring.
1023 * @param s first string
1024 * @param m length of first string
1025 * @param t second string
1026 * @param n length of second string
1027 * @param ret list of offsets for longest common substrings inside each string
1028 * @param min minimum length of common substrings
1030 * @return 0 if success, nonzero otherwise
1033 static int
1034 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1036 int i, j;
1037 int *Lprev, *Lcurr;
1038 int z = 0;
1040 if (m < min || n < min)
1042 /* XXX early culling */
1043 return 0;
1046 Lprev = g_try_new0 (int, n + 1);
1047 if (Lprev == NULL)
1048 return -1;
1050 Lcurr = g_try_new0 (int, n + 1);
1051 if (Lcurr == NULL)
1053 g_free (Lprev);
1054 return -1;
1057 for (i = 0; i < m; i++)
1059 int *L;
1061 L = Lprev;
1062 Lprev = Lcurr;
1063 Lcurr = L;
1064 #ifdef USE_MEMSET_IN_LCS
1065 memset (Lcurr, 0, (n + 1) * sizeof (*Lcurr));
1066 #endif
1067 for (j = 0; j < n; j++)
1069 #ifndef USE_MEMSET_IN_LCS
1070 Lcurr[j + 1] = 0;
1071 #endif
1072 if (s[i] == t[j])
1074 int v;
1076 v = Lprev[j] + 1;
1077 Lcurr[j + 1] = v;
1078 if (z < v)
1080 z = v;
1081 g_array_set_size (ret, 0);
1083 if (z == v && z >= min)
1085 int off0, off1;
1086 size_t k;
1088 off0 = i - z + 1;
1089 off1 = j - z + 1;
1091 for (k = 0; k < ret->len; k++)
1093 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1094 if ((*p)[0] == off0 || (*p)[1] >= off1)
1095 break;
1097 if (k == ret->len)
1099 PAIR p2;
1101 p2[0] = off0;
1102 p2[1] = off1;
1103 g_array_append_val (ret, p2);
1110 g_free (Lcurr);
1111 g_free (Lprev);
1112 return z;
1115 /* --------------------------------------------------------------------------------------------- */
1118 * Scan recursively for common substrings and build ranges.
1120 * @param s first string
1121 * @param t second string
1122 * @param bracket current limits for both of the strings
1123 * @param min minimum length of common substrings
1124 * @param hdiff list of horizontal diff ranges to fill
1125 * @param depth recursion depth
1127 * @return 0 if success, nonzero otherwise
1130 static gboolean
1131 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1132 unsigned int depth)
1134 BRACKET p;
1136 if (depth-- != 0)
1138 GArray *ret;
1139 BRACKET b;
1140 int len;
1142 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1143 if (ret == NULL)
1144 return FALSE;
1146 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1147 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1148 if (ret->len != 0)
1150 size_t k = 0;
1151 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1152 const PAIR *data2;
1154 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1155 b[DIFF_LEFT].len = (*data)[0];
1156 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1157 b[DIFF_RIGHT].len = (*data)[1];
1158 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1159 return FALSE;
1161 for (k = 0; k < ret->len - 1; k++)
1163 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1164 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1165 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1166 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1167 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1168 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1169 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1170 return FALSE;
1172 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1173 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1174 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1175 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1176 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1177 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1178 return FALSE;
1180 g_array_free (ret, TRUE);
1181 return TRUE;
1185 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1186 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1187 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1188 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1189 g_array_append_val (hdiff, p);
1191 return TRUE;
1194 /* --------------------------------------------------------------------------------------------- */
1197 * Build list of horizontal diff ranges.
1199 * @param s first string
1200 * @param m length of first string
1201 * @param t second string
1202 * @param n length of second string
1203 * @param min minimum length of common substrings
1204 * @param hdiff list of horizontal diff ranges to fill
1205 * @param depth recursion depth
1207 * @return 0 if success, nonzero otherwise
1210 static gboolean
1211 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1213 int i;
1214 BRACKET b;
1216 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1217 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1219 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1222 b[DIFF_LEFT].off = i;
1223 b[DIFF_LEFT].len = m - i;
1224 b[DIFF_RIGHT].off = i;
1225 b[DIFF_RIGHT].len = n - i;
1227 /* smartscan (multiple horizontal diff) */
1228 return hdiff_multi (s, t, b, min, hdiff, depth);
1231 /* --------------------------------------------------------------------------------------------- */
1233 /* read line **************************************************************** */
1236 * Check if character is inside horizontal diff limits.
1238 * @param k rank of character inside line
1239 * @param hdiff horizontal diff structure
1240 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1242 * @return TRUE if inside hdiff limits, FALSE otherwise
1245 static gboolean
1246 is_inside (int k, GArray * hdiff, diff_place_t ord)
1248 size_t i;
1249 BRACKET *b;
1251 for (i = 0; i < hdiff->len; i++)
1253 int start, end;
1255 b = &g_array_index (hdiff, BRACKET, i);
1256 start = (*b)[ord].off;
1257 end = start + (*b)[ord].len;
1258 if (k >= start && k < end)
1259 return TRUE;
1261 return FALSE;
1264 /* --------------------------------------------------------------------------------------------- */
1267 * Copy 'src' to 'dst' expanding tabs.
1268 * @note The procedure returns when all bytes are consumed from 'src'
1270 * @param dst destination buffer
1271 * @param src source buffer
1272 * @param srcsize size of src buffer
1273 * @param base virtual base of this string, needed to calculate tabs
1274 * @param ts tab size
1276 * @return new virtual base
1279 static int
1280 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1282 int i;
1284 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1286 *dst = *src;
1287 if (*src == '\t')
1289 int j;
1291 j = TAB_SKIP (ts, i + base);
1292 i += j - 1;
1293 while (j-- > 0)
1294 *dst++ = ' ';
1295 dst--;
1298 return i + base;
1301 /* --------------------------------------------------------------------------------------------- */
1304 * Copy 'src' to 'dst' expanding tabs.
1306 * @param dst destination buffer
1307 * @param dstsize size of dst buffer
1308 * @param[in,out] _src source buffer
1309 * @param srcsize size of src buffer
1310 * @param base virtual base of this string, needed to calculate tabs
1311 * @param ts tab size
1313 * @return new virtual base
1315 * @note The procedure returns when all bytes are consumed from 'src'
1316 * or 'dstsize' bytes are written to 'dst'
1317 * @note Upon return, 'src' points to the first unwritten character in source
1320 static int
1321 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1323 int i;
1324 const char *src = *_src;
1326 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1328 *dst = *src;
1329 if (*src == '\t')
1331 int j;
1333 j = TAB_SKIP (ts, i + base);
1334 if (j > dstsize - i)
1335 j = dstsize - i;
1336 i += j - 1;
1337 while (j-- > 0)
1338 *dst++ = ' ';
1339 dst--;
1342 *_src = src;
1343 return i + base;
1346 /* --------------------------------------------------------------------------------------------- */
1349 * Read line from memory, converting tabs to spaces and padding with spaces.
1351 * @param src buffer to read from
1352 * @param srcsize size of src buffer
1353 * @param dst buffer to read to
1354 * @param dstsize size of dst buffer, excluding trailing null
1355 * @param skip number of characters to skip
1356 * @param ts tab size
1357 * @param show_cr show trailing carriage return as ^M
1359 * @return negative on error, otherwise number of bytes except padding
1362 static int
1363 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1364 gboolean show_cr)
1366 int sz = 0;
1368 if (src != NULL)
1370 int i;
1371 char *tmp = dst;
1372 const int base = 0;
1374 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1376 if (*src == '\t')
1378 int j;
1380 j = TAB_SKIP (ts, i + base);
1381 i += j - 1;
1382 while (j-- > 0)
1384 if (skip > 0)
1385 skip--;
1386 else if (dstsize != 0)
1388 dstsize--;
1389 *dst++ = ' ';
1393 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1395 if (skip == 0 && show_cr)
1397 if (dstsize > 1)
1399 dstsize -= 2;
1400 *dst++ = '^';
1401 *dst++ = 'M';
1403 else
1405 dstsize--;
1406 *dst++ = '.';
1409 break;
1411 else if (skip > 0)
1413 #ifdef HAVE_CHARSET
1414 int ch = 0;
1415 int ch_length = 1;
1417 (void) dview_get_utf (src, &ch, &ch_length);
1419 if (ch_length > 1)
1420 skip += ch_length - 1;
1421 #endif
1423 skip--;
1425 else
1427 dstsize--;
1428 *dst++ = *src;
1431 sz = dst - tmp;
1433 while (dstsize != 0)
1435 dstsize--;
1436 *dst++ = ' ';
1438 *dst = '\0';
1439 return sz;
1442 /* --------------------------------------------------------------------------------------------- */
1445 * Read line from memory and build attribute array.
1447 * @param src buffer to read from
1448 * @param srcsize size of src buffer
1449 * @param dst buffer to read to
1450 * @param dstsize size of dst buffer, excluding trailing null
1451 * @param skip number of characters to skip
1452 * @param ts tab size
1453 * @param show_cr show trailing carriage return as ^M
1454 * @param hdiff horizontal diff structure
1455 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1456 * @param att buffer of attributes
1458 * @return negative on error, otherwise number of bytes except padding
1461 static int
1462 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1463 gboolean show_cr, GArray * hdiff, diff_place_t ord, char *att)
1465 int sz = 0;
1467 if (src != NULL)
1469 int i, k;
1470 char *tmp = dst;
1471 const int base = 0;
1473 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1475 if (*src == '\t')
1477 int j;
1479 j = TAB_SKIP (ts, i + base);
1480 i += j - 1;
1481 while (j-- > 0)
1483 if (skip != 0)
1484 skip--;
1485 else if (dstsize != 0)
1487 dstsize--;
1488 *att++ = is_inside (k, hdiff, ord);
1489 *dst++ = ' ';
1493 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1495 if (skip == 0 && show_cr)
1497 if (dstsize > 1)
1499 dstsize -= 2;
1500 *att++ = is_inside (k, hdiff, ord);
1501 *dst++ = '^';
1502 *att++ = is_inside (k, hdiff, ord);
1503 *dst++ = 'M';
1505 else
1507 dstsize--;
1508 *att++ = is_inside (k, hdiff, ord);
1509 *dst++ = '.';
1512 break;
1514 else if (skip != 0)
1516 #ifdef HAVE_CHARSET
1517 int ch = 0;
1518 int ch_length = 1;
1520 (void) dview_get_utf (src, &ch, &ch_length);
1521 if (ch_length > 1)
1522 skip += ch_length - 1;
1523 #endif
1525 skip--;
1527 else
1529 dstsize--;
1530 *att++ = is_inside (k, hdiff, ord);
1531 *dst++ = *src;
1534 sz = dst - tmp;
1536 while (dstsize != 0)
1538 dstsize--;
1539 *att++ = '\0';
1540 *dst++ = ' ';
1542 *dst = '\0';
1543 return sz;
1546 /* --------------------------------------------------------------------------------------------- */
1549 * Read line from file, converting tabs to spaces and padding with spaces.
1551 * @param f file stream to read from
1552 * @param off offset of line inside file
1553 * @param dst buffer to read to
1554 * @param dstsize size of dst buffer, excluding trailing null
1555 * @param skip number of characters to skip
1556 * @param ts tab size
1557 * @param show_cr show trailing carriage return as ^M
1559 * @return negative on error, otherwise number of bytes except padding
1562 static int
1563 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, gboolean show_cr)
1565 int base = 0;
1566 int old_base = base;
1567 size_t amount = dstsize;
1568 size_t useful, offset;
1569 size_t i;
1570 size_t sz;
1571 int lastch = '\0';
1572 const char *q = NULL;
1573 char tmp[BUFSIZ]; /* XXX capacity must be >= MAX{dstsize + 1, amount} */
1574 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1576 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1578 /* abnormal, but avoid buffer overflow */
1579 memset (dst, ' ', dstsize);
1580 dst[dstsize] = '\0';
1581 return 0;
1584 f_seek (f, off, SEEK_SET);
1586 while (skip > base)
1588 old_base = base;
1589 sz = f_gets (tmp, amount, f);
1590 if (sz == 0)
1591 break;
1593 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1594 if (cvt[base - old_base - 1] == '\n')
1596 q = &cvt[base - old_base - 1];
1597 base = old_base + q - cvt + 1;
1598 break;
1602 if (base < skip)
1604 memset (dst, ' ', dstsize);
1605 dst[dstsize] = '\0';
1606 return 0;
1609 useful = base - skip;
1610 offset = skip - old_base;
1612 if (useful <= dstsize)
1614 if (useful != 0)
1615 memmove (dst, cvt + offset, useful);
1617 if (q == NULL)
1619 sz = f_gets (tmp, dstsize - useful + 1, f);
1620 if (sz != 0)
1622 const char *ptr = tmp;
1624 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1625 if (ptr < tmp + sz)
1626 lastch = *ptr;
1629 sz = useful;
1631 else
1633 memmove (dst, cvt + offset, dstsize);
1634 sz = dstsize;
1635 lastch = cvt[offset + dstsize];
1638 dst[sz] = lastch;
1639 for (i = 0; i < sz && dst[i] != '\n'; i++)
1641 if (dst[i] == '\r' && dst[i + 1] == '\n')
1643 if (show_cr)
1645 if (i + 1 < dstsize)
1647 dst[i++] = '^';
1648 dst[i++] = 'M';
1650 else
1652 dst[i++] = '*';
1655 break;
1659 for (; i < dstsize; i++)
1660 dst[i] = ' ';
1661 dst[i] = '\0';
1662 return sz;
1665 /* --------------------------------------------------------------------------------------------- */
1666 /* diff printers et al ****************************************************** */
1668 static void
1669 cc_free_elt (void *elt)
1671 DIFFLN *p = elt;
1673 if (p != NULL)
1674 g_free (p->p);
1677 /* --------------------------------------------------------------------------------------------- */
1679 static int
1680 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1682 GArray *a = ((PRINTER_CTX *) ctx)->a;
1683 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1685 if (ch != 0)
1687 DIFFLN p;
1689 p.p = NULL;
1690 p.ch = ch;
1691 p.line = line;
1692 p.u.off = off;
1693 if (dsrc == DATA_SRC_MEM && line != 0)
1695 if (sz != 0 && str[sz - 1] == '\n')
1696 sz--;
1697 if (sz > 0)
1698 p.p = g_strndup (str, sz);
1699 p.u.len = sz;
1701 g_array_append_val (a, p);
1703 else if (dsrc == DATA_SRC_MEM)
1705 DIFFLN *p;
1707 p = &g_array_index (a, DIFFLN, a->len - 1);
1708 if (sz != 0 && str[sz - 1] == '\n')
1709 sz--;
1710 if (sz != 0)
1712 size_t new_size;
1713 char *q;
1715 new_size = p->u.len + sz;
1716 q = g_realloc (p->p, new_size);
1717 memcpy (q + p->u.len, str, sz);
1718 p->p = q;
1720 p->u.len += sz;
1722 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1724 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1725 f_write (f, str, sz);
1727 return 0;
1730 /* --------------------------------------------------------------------------------------------- */
1732 static int
1733 redo_diff (WDiff * dview)
1735 FBUF *const *f = dview->f;
1736 PRINTER_CTX ctx;
1737 GArray *ops;
1738 int ndiff;
1739 int rv;
1740 char extra[256];
1742 extra[0] = '\0';
1743 if (dview->opt.quality == 2)
1744 strcat (extra, " -d");
1745 if (dview->opt.quality == 1)
1746 strcat (extra, " --speed-large-files");
1747 if (dview->opt.strip_trailing_cr)
1748 strcat (extra, " --strip-trailing-cr");
1749 if (dview->opt.ignore_tab_expansion)
1750 strcat (extra, " -E");
1751 if (dview->opt.ignore_space_change)
1752 strcat (extra, " -b");
1753 if (dview->opt.ignore_all_space)
1754 strcat (extra, " -w");
1755 if (dview->opt.ignore_case)
1756 strcat (extra, " -i");
1758 if (dview->dsrc != DATA_SRC_MEM)
1760 f_reset (f[DIFF_LEFT]);
1761 f_reset (f[DIFF_RIGHT]);
1764 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1765 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1766 if (ndiff < 0)
1768 if (ops != NULL)
1769 g_array_free (ops, TRUE);
1770 return -1;
1773 ctx.dsrc = dview->dsrc;
1775 rv = 0;
1776 ctx.a = dview->a[DIFF_LEFT];
1777 ctx.f = f[DIFF_LEFT];
1778 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1780 ctx.a = dview->a[DIFF_RIGHT];
1781 ctx.f = f[DIFF_RIGHT];
1782 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1784 if (ops != NULL)
1785 g_array_free (ops, TRUE);
1787 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1788 return -1;
1790 if (dview->dsrc == DATA_SRC_TMP)
1792 f_trunc (f[DIFF_LEFT]);
1793 f_trunc (f[DIFF_RIGHT]);
1796 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1798 dview->hdiff = g_ptr_array_new ();
1799 if (dview->hdiff != NULL)
1801 size_t i;
1803 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1805 GArray *h = NULL;
1806 const DIFFLN *p;
1807 const DIFFLN *q;
1809 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1810 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1811 if (p->line && q->line && p->ch == CHG_CH)
1813 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1814 if (h != NULL)
1816 gboolean runresult;
1818 runresult =
1819 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1820 HDIFF_DEPTH);
1821 if (!runresult)
1823 g_array_free (h, TRUE);
1824 h = NULL;
1828 g_ptr_array_add (dview->hdiff, h);
1832 return ndiff;
1835 /* --------------------------------------------------------------------------------------------- */
1837 static void
1838 destroy_hdiff (WDiff * dview)
1840 if (dview->hdiff != NULL)
1842 int i;
1843 int len;
1845 len = dview->a[DIFF_LEFT]->len;
1847 for (i = 0; i < len; i++)
1849 GArray *h;
1851 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1852 if (h != NULL)
1853 g_array_free (h, TRUE);
1855 g_ptr_array_free (dview->hdiff, TRUE);
1856 dview->hdiff = NULL;
1859 mc_search_free (dview->search.handle);
1860 dview->search.handle = NULL;
1861 MC_PTR_FREE (dview->search.last_string);
1864 /* --------------------------------------------------------------------------------------------- */
1865 /* stuff ******************************************************************** */
1867 static int
1868 get_digits (unsigned int n)
1870 int d = 1;
1872 while (n /= 10)
1873 d++;
1874 return d;
1877 /* --------------------------------------------------------------------------------------------- */
1879 static int
1880 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1882 const DIFFLN *p;
1884 *linenum = 0;
1885 *lineofs = 0;
1887 if (a->len != 0)
1889 if (pos >= a->len)
1890 pos = a->len - 1;
1892 p = &g_array_index (a, DIFFLN, pos);
1894 if (p->line == 0)
1896 int n;
1898 for (n = pos; n > 0; n--)
1900 p--;
1901 if (p->line != 0)
1902 break;
1904 *lineofs = pos - n + 1;
1907 *linenum = p->line;
1909 return 0;
1912 /* --------------------------------------------------------------------------------------------- */
1914 static int
1915 calc_nwidth (const GArray * const *a)
1917 int l1, o1;
1918 int l2, o2;
1920 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1921 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1922 if (l1 < l2)
1923 l1 = l2;
1924 return get_digits (l1);
1927 /* --------------------------------------------------------------------------------------------- */
1929 static int
1930 find_prev_hunk (const GArray * a, int pos)
1932 #if 1
1933 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1934 pos--;
1935 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1936 pos--;
1937 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1938 pos--;
1939 if (pos > 0 && (size_t) pos < a->len)
1940 pos++;
1941 #else
1942 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1943 pos--;
1944 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1945 pos--;
1946 #endif
1948 return pos;
1951 /* --------------------------------------------------------------------------------------------- */
1953 static size_t
1954 find_next_hunk (const GArray * a, size_t pos)
1956 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1957 pos++;
1958 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1959 pos++;
1960 return pos;
1963 /* --------------------------------------------------------------------------------------------- */
1965 * Find start and end lines of the current hunk.
1967 * @param dview WDiff widget
1968 * @return boolean and
1969 * start_line1 first line of current hunk (file[0])
1970 * end_line1 last line of current hunk (file[0])
1971 * start_line1 first line of current hunk (file[0])
1972 * end_line1 last line of current hunk (file[0])
1975 static int
1976 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1978 const GArray *a0 = dview->a[DIFF_LEFT];
1979 const GArray *a1 = dview->a[DIFF_RIGHT];
1980 size_t pos;
1981 int ch;
1982 int res = 0;
1984 *start_line1 = 1;
1985 *start_line2 = 1;
1986 *end_line1 = 1;
1987 *end_line2 = 1;
1989 pos = dview->skip_rows;
1990 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1991 if (ch != EQU_CH)
1993 switch (ch)
1995 case ADD_CH:
1996 res = DIFF_DEL;
1997 break;
1998 case DEL_CH:
1999 res = DIFF_ADD;
2000 break;
2001 case CHG_CH:
2002 res = DIFF_CHG;
2003 break;
2004 default:
2005 break;
2007 while (pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2008 pos--;
2009 if (pos > 0)
2011 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
2012 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
2014 pos = dview->skip_rows;
2015 while (pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2017 int l0, l1;
2019 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
2020 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
2021 if (l0 > 0)
2022 *end_line1 = MAX (*start_line1, l0);
2023 if (l1 > 0)
2024 *end_line2 = MAX (*start_line2, l1);
2025 pos++;
2028 return res;
2031 /* --------------------------------------------------------------------------------------------- */
2033 * Remove hunk from file.
2035 * @param dview WDiff widget
2036 * @param merge_file file stream for writing data
2037 * @param from1 first line of hunk
2038 * @param to1 last line of hunk
2039 * @param merge_direction in what direction files should be merged
2042 static void
2043 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
2044 action_direction_t merge_direction)
2046 int line;
2047 char buf[BUF_10K];
2048 FILE *f0;
2050 if (merge_direction == FROM_RIGHT_TO_LEFT)
2051 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2052 else
2053 f0 = fopen (dview->file[DIFF_LEFT], "r");
2055 line = 0;
2056 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2058 line++;
2059 fputs (buf, merge_file);
2061 while (fgets (buf, sizeof (buf), f0) != NULL)
2063 line++;
2064 if (line >= to1)
2065 fputs (buf, merge_file);
2067 fclose (f0);
2070 /* --------------------------------------------------------------------------------------------- */
2072 * Add hunk to file.
2074 * @param dview WDiff widget
2075 * @param merge_file file stream for writing data
2076 * @param from1 first line of source hunk
2077 * @param from2 first line of destination hunk
2078 * @param to1 last line of source hunk
2079 * @param merge_direction in what direction files should be merged
2082 static void
2083 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2084 action_direction_t merge_direction)
2086 int line;
2087 char buf[BUF_10K];
2088 FILE *f0;
2089 FILE *f1;
2091 if (merge_direction == FROM_RIGHT_TO_LEFT)
2093 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2094 f1 = fopen (dview->file[DIFF_LEFT], "r");
2096 else
2098 f0 = fopen (dview->file[DIFF_LEFT], "r");
2099 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2102 line = 0;
2103 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2105 line++;
2106 fputs (buf, merge_file);
2108 line = 0;
2109 while (fgets (buf, sizeof (buf), f1) != NULL && line <= to2)
2111 line++;
2112 if (line >= from2)
2113 fputs (buf, merge_file);
2115 while (fgets (buf, sizeof (buf), f0) != NULL)
2116 fputs (buf, merge_file);
2118 fclose (f0);
2119 fclose (f1);
2122 /* --------------------------------------------------------------------------------------------- */
2124 * Replace hunk in file.
2126 * @param dview WDiff widget
2127 * @param merge_file file stream for writing data
2128 * @param from1 first line of source hunk
2129 * @param to1 last line of source hunk
2130 * @param from2 first line of destination hunk
2131 * @param to2 last line of destination hunk
2132 * @param merge_direction in what direction files should be merged
2135 static void
2136 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2137 action_direction_t merge_direction)
2139 int line1 = 0, line2 = 0;
2140 char buf[BUF_10K];
2141 FILE *f0;
2142 FILE *f1;
2144 if (merge_direction == FROM_RIGHT_TO_LEFT)
2146 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2147 f1 = fopen (dview->file[DIFF_LEFT], "r");
2149 else
2151 f0 = fopen (dview->file[DIFF_LEFT], "r");
2152 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2155 while (fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1)
2157 line1++;
2158 fputs (buf, merge_file);
2160 while (fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2)
2162 line2++;
2163 if (line2 >= from2)
2164 fputs (buf, merge_file);
2166 while (fgets (buf, sizeof (buf), f0) != NULL)
2168 line1++;
2169 if (line1 > to1)
2170 fputs (buf, merge_file);
2172 fclose (f0);
2173 fclose (f1);
2176 /* --------------------------------------------------------------------------------------------- */
2178 * Merge hunk.
2180 * @param dview WDiff widget
2181 * @param merge_direction in what direction files should be merged
2184 static void
2185 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2187 int from1, to1, from2, to2;
2188 int hunk;
2189 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2191 if (merge_direction == FROM_RIGHT_TO_LEFT)
2192 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2193 else
2194 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2196 if (hunk > 0)
2198 int merge_file_fd;
2199 FILE *merge_file;
2200 vfs_path_t *merge_file_name_vpath = NULL;
2202 if (!dview->merged[n_merge])
2204 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2205 if (!dview->merged[n_merge])
2207 message (D_ERROR, MSG_ERROR,
2208 _("Cannot create backup file\n%s%s\n%s"),
2209 dview->file[n_merge], "~~~", unix_error_string (errno));
2210 return;
2214 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2215 if (merge_file_fd == -1)
2217 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2218 unix_error_string (errno));
2219 return;
2222 merge_file = fdopen (merge_file_fd, "w");
2224 switch (hunk)
2226 case DIFF_DEL:
2227 if (merge_direction == FROM_RIGHT_TO_LEFT)
2228 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2229 else
2230 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2231 break;
2232 case DIFF_ADD:
2233 if (merge_direction == FROM_RIGHT_TO_LEFT)
2234 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2235 else
2236 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2237 break;
2238 case DIFF_CHG:
2239 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2240 break;
2241 default:
2242 break;
2244 fflush (merge_file);
2245 fclose (merge_file);
2247 int res;
2249 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2250 (void) res;
2252 mc_unlink (merge_file_name_vpath);
2253 vfs_path_free (merge_file_name_vpath);
2257 /* --------------------------------------------------------------------------------------------- */
2258 /* view routines and callbacks ********************************************** */
2260 static void
2261 dview_compute_split (WDiff * dview, int i)
2263 dview->bias += i;
2264 if (dview->bias < 2 - dview->half1)
2265 dview->bias = 2 - dview->half1;
2266 if (dview->bias > dview->half2 - 2)
2267 dview->bias = dview->half2 - 2;
2270 /* --------------------------------------------------------------------------------------------- */
2272 static void
2273 dview_compute_areas (WDiff * dview)
2275 Widget *w = WIDGET (dview);
2277 dview->height = w->lines - 1;
2278 dview->half1 = w->cols / 2;
2279 dview->half2 = w->cols - dview->half1;
2281 dview_compute_split (dview, 0);
2284 /* --------------------------------------------------------------------------------------------- */
2286 static void
2287 dview_reread (WDiff * dview)
2289 int ndiff;
2291 destroy_hdiff (dview);
2292 if (dview->a[DIFF_LEFT] != NULL)
2294 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2295 g_array_free (dview->a[DIFF_LEFT], TRUE);
2297 if (dview->a[DIFF_RIGHT] != NULL)
2299 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2300 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2303 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2304 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2306 ndiff = redo_diff (dview);
2307 if (ndiff >= 0)
2308 dview->ndiff = ndiff;
2311 /* --------------------------------------------------------------------------------------------- */
2313 #ifdef HAVE_CHARSET
2314 static void
2315 dview_set_codeset (WDiff * dview)
2317 const char *encoding_id = NULL;
2319 dview->utf8 = TRUE;
2320 encoding_id =
2321 get_codepage_id (mc_global.source_codepage >=
2322 0 ? mc_global.source_codepage : mc_global.display_codepage);
2323 if (encoding_id != NULL)
2325 GIConv conv;
2327 conv = str_crt_conv_from (encoding_id);
2328 if (conv != INVALID_CONV)
2330 if (dview->converter != str_cnv_from_term)
2331 str_close_conv (dview->converter);
2332 dview->converter = conv;
2334 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2338 /* --------------------------------------------------------------------------------------------- */
2340 static void
2341 dview_select_encoding (WDiff * dview)
2343 if (do_select_codepage ())
2344 dview_set_codeset (dview);
2345 dview_reread (dview);
2346 tty_touch_screen ();
2347 repaint_screen ();
2349 #endif /* HAVE_CHARSET */
2351 /* --------------------------------------------------------------------------------------------- */
2353 static void
2354 dview_diff_options (WDiff * dview)
2356 const char *quality_str[] = {
2357 N_("No&rmal"),
2358 N_("&Fastest (Assume large files)"),
2359 N_("&Minimal (Find a smaller set of change)")
2362 quick_widget_t quick_widgets[] = {
2363 /* *INDENT-OFF* */
2364 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2365 QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2366 QUICK_STOP_GROUPBOX,
2367 QUICK_START_GROUPBOX (N_("Diff extra options")),
2368 QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2369 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2370 QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2371 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2372 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2373 NULL),
2374 QUICK_STOP_GROUPBOX,
2375 QUICK_BUTTONS_OK_CANCEL,
2376 QUICK_END
2377 /* *INDENT-ON* */
2380 quick_dialog_t qdlg = {
2381 -1, -1, 56,
2382 N_("Diff Options"), "[Diff Options]",
2383 quick_widgets, NULL, NULL
2386 if (quick_dialog (&qdlg) != B_CANCEL)
2387 dview_reread (dview);
2390 /* --------------------------------------------------------------------------------------------- */
2392 static int
2393 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2394 const char *label1, const char *label2, DSRC dsrc)
2396 int ndiff;
2397 FBUF *f[DIFF_COUNT];
2399 f[DIFF_LEFT] = NULL;
2400 f[DIFF_RIGHT] = NULL;
2402 if (dsrc == DATA_SRC_TMP)
2404 f[DIFF_LEFT] = f_temp ();
2405 if (f[DIFF_LEFT] == NULL)
2406 return -1;
2408 f[DIFF_RIGHT] = f_temp ();
2409 if (f[DIFF_RIGHT] == NULL)
2411 f_close (f[DIFF_LEFT]);
2412 return -1;
2415 else if (dsrc == DATA_SRC_ORG)
2417 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2418 if (f[DIFF_LEFT] == NULL)
2419 return -1;
2421 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2422 if (f[DIFF_RIGHT] == NULL)
2424 f_close (f[DIFF_LEFT]);
2425 return -1;
2429 dview->args = args;
2430 dview->file[DIFF_LEFT] = file1;
2431 dview->file[DIFF_RIGHT] = file2;
2432 dview->label[DIFF_LEFT] = g_strdup (label1);
2433 dview->label[DIFF_RIGHT] = g_strdup (label2);
2434 dview->f[DIFF_LEFT] = f[0];
2435 dview->f[DIFF_RIGHT] = f[1];
2436 dview->merged[DIFF_LEFT] = FALSE;
2437 dview->merged[DIFF_RIGHT] = FALSE;
2438 dview->hdiff = NULL;
2439 dview->dsrc = dsrc;
2440 #ifdef HAVE_CHARSET
2441 dview->converter = str_cnv_from_term;
2442 dview_set_codeset (dview);
2443 #endif
2444 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2445 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2447 ndiff = redo_diff (dview);
2448 if (ndiff < 0)
2450 /* goto MSG_DESTROY stage: dview_fini() */
2451 f_close (f[DIFF_LEFT]);
2452 f_close (f[DIFF_RIGHT]);
2453 return -1;
2456 dview->ndiff = ndiff;
2458 dview->view_quit = FALSE;
2460 dview->bias = 0;
2461 dview->new_frame = TRUE;
2462 dview->skip_rows = 0;
2463 dview->skip_cols = 0;
2464 dview->display_symbols = 0;
2465 dview->display_numbers = 0;
2466 dview->show_cr = TRUE;
2467 dview->tab_size = 8;
2468 dview->ord = DIFF_LEFT;
2469 dview->full = FALSE;
2471 dview->search.handle = NULL;
2472 dview->search.last_string = NULL;
2473 dview->search.last_found_line = -1;
2474 dview->search.last_accessed_num_line = -1;
2476 dview->opt.quality = 0;
2477 dview->opt.strip_trailing_cr = 0;
2478 dview->opt.ignore_tab_expansion = 0;
2479 dview->opt.ignore_space_change = 0;
2480 dview->opt.ignore_all_space = 0;
2481 dview->opt.ignore_case = 0;
2483 dview_compute_areas (dview);
2485 return 0;
2488 /* --------------------------------------------------------------------------------------------- */
2490 static void
2491 dview_fini (WDiff * dview)
2493 if (dview->dsrc != DATA_SRC_MEM)
2495 f_close (dview->f[DIFF_RIGHT]);
2496 f_close (dview->f[DIFF_LEFT]);
2499 #ifdef HAVE_CHARSET
2500 if (dview->converter != str_cnv_from_term)
2501 str_close_conv (dview->converter);
2502 #endif
2504 destroy_hdiff (dview);
2505 if (dview->a[DIFF_LEFT] != NULL)
2507 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2508 g_array_free (dview->a[DIFF_LEFT], TRUE);
2509 dview->a[DIFF_LEFT] = NULL;
2511 if (dview->a[DIFF_RIGHT] != NULL)
2513 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2514 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2515 dview->a[DIFF_RIGHT] = NULL;
2518 g_free (dview->label[DIFF_LEFT]);
2519 g_free (dview->label[DIFF_RIGHT]);
2522 /* --------------------------------------------------------------------------------------------- */
2524 static int
2525 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2527 size_t i, k;
2528 int j;
2529 char buf[BUFSIZ];
2530 FBUF *f = dview->f[ord];
2531 int skip = dview->skip_cols;
2532 int display_symbols = dview->display_symbols;
2533 int display_numbers = dview->display_numbers;
2534 gboolean show_cr = dview->show_cr;
2535 int tab_size = 8;
2536 const DIFFLN *p;
2537 int nwidth = display_numbers;
2538 int xwidth;
2540 xwidth = display_symbols + display_numbers;
2541 if (dview->tab_size > 0 && dview->tab_size < 9)
2542 tab_size = dview->tab_size;
2544 if (xwidth != 0)
2546 if (xwidth > width && display_symbols)
2548 xwidth--;
2549 display_symbols = 0;
2551 if (xwidth > width && display_numbers)
2553 xwidth = width;
2554 display_numbers = width;
2557 xwidth++;
2558 c += xwidth;
2559 width -= xwidth;
2560 if (width < 0)
2561 width = 0;
2564 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2566 /* abnormal, but avoid buffer overflow */
2567 return -1;
2570 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2572 int ch, next_ch = 0, col;
2573 size_t cnt;
2575 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2576 ch = p->ch;
2577 tty_setcolor (NORMAL_COLOR);
2578 if (display_symbols)
2580 tty_gotoyx (r + j, c - 2);
2581 tty_print_char (ch);
2583 if (p->line != 0)
2585 if (display_numbers)
2587 tty_gotoyx (r + j, c - xwidth);
2588 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2589 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2591 if (ch == ADD_CH)
2592 tty_setcolor (DFF_ADD_COLOR);
2593 if (ch == CHG_CH)
2594 tty_setcolor (DFF_CHG_COLOR);
2595 if (f == NULL)
2597 if (i == (size_t) dview->search.last_found_line)
2598 tty_setcolor (MARKED_SELECTED_COLOR);
2599 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2601 char att[BUFSIZ];
2603 #ifdef HAVE_CHARSET
2604 if (dview->utf8)
2605 k = dview_str_utf8_offset_to_pos (p->p, width);
2606 else
2607 #endif
2608 k = width;
2610 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2611 g_ptr_array_index (dview->hdiff, i), ord, att);
2612 tty_gotoyx (r + j, c);
2613 col = 0;
2615 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2617 gboolean ch_res;
2619 #ifdef HAVE_CHARSET
2620 if (dview->utf8)
2622 int ch_length = 0;
2624 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2625 if (ch_length > 1)
2626 cnt += ch_length - 1;
2627 if (!g_unichar_isprint (next_ch))
2628 next_ch = '.';
2630 else
2631 #endif
2632 ch_res = dview_get_byte (buf + cnt, &next_ch);
2634 if (ch_res)
2636 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2637 #ifdef HAVE_CHARSET
2638 if (mc_global.utf8_display)
2640 if (!dview->utf8)
2642 next_ch =
2643 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2644 dview->converter);
2647 else if (dview->utf8)
2648 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2649 else
2650 next_ch = convert_to_display_c (next_ch);
2651 #endif
2652 tty_print_anychar (next_ch);
2653 col++;
2656 continue;
2659 if (ch == CHG_CH)
2660 tty_setcolor (DFF_CHH_COLOR);
2662 #ifdef HAVE_CHARSET
2663 if (dview->utf8)
2664 k = dview_str_utf8_offset_to_pos (p->p, width);
2665 else
2666 #endif
2667 k = width;
2668 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2670 else
2671 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2673 else
2675 if (display_numbers)
2677 tty_gotoyx (r + j, c - xwidth);
2678 memset (buf, ' ', display_numbers);
2679 buf[display_numbers] = '\0';
2680 tty_print_string (buf);
2682 if (ch == DEL_CH)
2683 tty_setcolor (DFF_DEL_COLOR);
2684 if (ch == CHG_CH)
2685 tty_setcolor (DFF_CHD_COLOR);
2686 memset (buf, ' ', width);
2687 buf[width] = '\0';
2689 tty_gotoyx (r + j, c);
2690 /* tty_print_nstring (buf, width); */
2691 col = 0;
2692 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2694 gboolean ch_res;
2696 #ifdef HAVE_CHARSET
2697 if (dview->utf8)
2699 int ch_length = 0;
2701 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2702 if (ch_length > 1)
2703 cnt += ch_length - 1;
2704 if (!g_unichar_isprint (next_ch))
2705 next_ch = '.';
2707 else
2708 #endif
2709 ch_res = dview_get_byte (buf + cnt, &next_ch);
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 * const *) 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 Widget *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 (WIDGET (dview)->owner);
2896 h_modal = widget_get_state (h, WST_MODAL);
2898 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2900 /* disallow edit file in several editors */
2901 widget_set_state (h, WST_MODAL, TRUE);
2904 vfs_path_t *tmp_vpath;
2906 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2907 edit_file_at_line (tmp_vpath, use_internal_edit, linenum);
2908 vfs_path_free (tmp_vpath);
2911 widget_set_state (h, WST_MODAL, h_modal);
2912 dview_redo (dview);
2913 dview_update (dview);
2916 /* --------------------------------------------------------------------------------------------- */
2918 static void
2919 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2921 static gboolean first_run = TRUE;
2923 /* *INDENT-OFF* */
2924 static const char *title[2] = {
2925 N_("Goto line (left)"),
2926 N_("Goto line (right)")
2928 /* *INDENT-ON* */
2930 int newline;
2931 char *input;
2933 input =
2934 input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE,
2935 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
2936 if (input != NULL)
2938 const char *s = input;
2940 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2942 size_t i = 0;
2944 if (newline > 0)
2946 for (; i < dview->a[ord]->len; i++)
2948 const DIFFLN *p;
2950 p = &g_array_index (dview->a[ord], DIFFLN, i);
2951 if (p->line == newline)
2952 break;
2955 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2957 g_free (input);
2960 first_run = FALSE;
2963 /* --------------------------------------------------------------------------------------------- */
2965 static void
2966 dview_labels (WDiff * dview)
2968 Widget *d = WIDGET (dview);
2969 WButtonBar *b;
2971 b = find_buttonbar (DIALOG (d->owner));
2973 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, d);
2974 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, d);
2975 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, d);
2976 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, d);
2977 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, d);
2978 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, d);
2979 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, d);
2982 /* --------------------------------------------------------------------------------------------- */
2984 static gboolean
2985 dview_save (WDiff * dview)
2987 gboolean res = TRUE;
2989 if (dview->merged[DIFF_LEFT])
2991 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2992 dview->merged[DIFF_LEFT] = !res;
2994 if (dview->merged[DIFF_RIGHT])
2996 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2997 dview->merged[DIFF_RIGHT] = !res;
2999 return res;
3002 /* --------------------------------------------------------------------------------------------- */
3004 static void
3005 dview_do_save (WDiff * dview)
3007 (void) dview_save (dview);
3010 /* --------------------------------------------------------------------------------------------- */
3012 static void
3013 dview_save_options (WDiff * dview)
3015 mc_config_set_bool (mc_global.main_config, "DiffView", "show_symbols",
3016 dview->display_symbols != 0);
3017 mc_config_set_bool (mc_global.main_config, "DiffView", "show_numbers",
3018 dview->display_numbers != 0);
3019 mc_config_set_int (mc_global.main_config, "DiffView", "tab_size", dview->tab_size);
3021 mc_config_set_int (mc_global.main_config, "DiffView", "diff_quality", dview->opt.quality);
3023 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_tws",
3024 dview->opt.strip_trailing_cr);
3025 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space",
3026 dview->opt.ignore_all_space);
3027 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change",
3028 dview->opt.ignore_space_change);
3029 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_tab_expansion",
3030 dview->opt.ignore_tab_expansion);
3031 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_case",
3032 dview->opt.ignore_case);
3035 /* --------------------------------------------------------------------------------------------- */
3037 static void
3038 dview_load_options (WDiff * dview)
3040 gboolean show_numbers, show_symbols;
3041 int tab_size;
3043 show_symbols = mc_config_get_bool (mc_global.main_config, "DiffView", "show_symbols", FALSE);
3044 if (show_symbols)
3045 dview->display_symbols = 1;
3046 show_numbers = mc_config_get_bool (mc_global.main_config, "DiffView", "show_numbers", FALSE);
3047 if (show_numbers)
3048 dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
3049 tab_size = mc_config_get_int (mc_global.main_config, "DiffView", "tab_size", 8);
3050 if (tab_size > 0 && tab_size < 9)
3051 dview->tab_size = tab_size;
3052 else
3053 dview->tab_size = 8;
3055 dview->opt.quality = mc_config_get_int (mc_global.main_config, "DiffView", "diff_quality", 0);
3057 dview->opt.strip_trailing_cr =
3058 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_tws", FALSE);
3059 dview->opt.ignore_all_space =
3060 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space", FALSE);
3061 dview->opt.ignore_space_change =
3062 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change", FALSE);
3063 dview->opt.ignore_tab_expansion =
3064 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_tab_expansion", FALSE);
3065 dview->opt.ignore_case =
3066 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_case", FALSE);
3068 dview->new_frame = TRUE;
3071 /* --------------------------------------------------------------------------------------------- */
3074 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3075 * ask user.
3077 static gboolean
3078 dview_ok_to_exit (WDiff * dview)
3080 gboolean res = TRUE;
3081 int act;
3083 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3084 return res;
3086 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3087 _("File(s) was modified. Save with exit?") :
3088 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3089 D_NORMAL, 2, _("&Yes"), _("&No"));
3091 /* Esc is No */
3092 if (mc_global.midnight_shutdown || (act == -1))
3093 act = 1;
3095 switch (act)
3097 case -1: /* Esc */
3098 res = FALSE;
3099 break;
3100 case 0: /* Yes */
3101 (void) dview_save (dview);
3102 res = TRUE;
3103 break;
3104 case 1: /* No */
3105 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3106 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3107 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3108 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3109 MC_FALLTHROUGH;
3110 default:
3111 res = TRUE;
3112 break;
3114 return res;
3117 /* --------------------------------------------------------------------------------------------- */
3119 static cb_ret_t
3120 dview_execute_cmd (WDiff * dview, long command)
3122 cb_ret_t res = MSG_HANDLED;
3124 switch (command)
3126 case CK_ShowSymbols:
3127 dview->display_symbols ^= 1;
3128 dview->new_frame = TRUE;
3129 break;
3130 case CK_ShowNumbers:
3131 dview->display_numbers ^= calc_nwidth ((const GArray * const *) dview->a);
3132 dview->new_frame = TRUE;
3133 break;
3134 case CK_SplitFull:
3135 dview->full = !dview->full;
3136 dview->new_frame = TRUE;
3137 break;
3138 case CK_SplitEqual:
3139 if (!dview->full)
3141 dview->bias = 0;
3142 dview->new_frame = TRUE;
3144 break;
3145 case CK_SplitMore:
3146 if (!dview->full)
3148 dview_compute_split (dview, 1);
3149 dview->new_frame = TRUE;
3151 break;
3153 case CK_SplitLess:
3154 if (!dview->full)
3156 dview_compute_split (dview, -1);
3157 dview->new_frame = TRUE;
3159 break;
3160 case CK_Tab2:
3161 dview->tab_size = 2;
3162 break;
3163 case CK_Tab3:
3164 dview->tab_size = 3;
3165 break;
3166 case CK_Tab4:
3167 dview->tab_size = 4;
3168 break;
3169 case CK_Tab8:
3170 dview->tab_size = 8;
3171 break;
3172 case CK_Swap:
3173 dview->ord ^= 1;
3174 break;
3175 case CK_Redo:
3176 dview_redo (dview);
3177 break;
3178 case CK_HunkNext:
3179 dview->skip_rows = dview->search.last_accessed_num_line =
3180 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3181 break;
3182 case CK_HunkPrev:
3183 dview->skip_rows = dview->search.last_accessed_num_line =
3184 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3185 break;
3186 case CK_Goto:
3187 dview_goto_cmd (dview, DIFF_RIGHT);
3188 break;
3189 case CK_Edit:
3190 dview_edit (dview, dview->ord);
3191 break;
3192 case CK_Merge:
3193 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3194 dview_redo (dview);
3195 break;
3196 case CK_MergeOther:
3197 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3198 dview_redo (dview);
3199 break;
3200 case CK_EditOther:
3201 dview_edit (dview, dview->ord ^ 1);
3202 break;
3203 case CK_Search:
3204 dview_search_cmd (dview);
3205 break;
3206 case CK_SearchContinue:
3207 dview_continue_search_cmd (dview);
3208 break;
3209 case CK_Top:
3210 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3211 break;
3212 case CK_Bottom:
3213 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3214 break;
3215 case CK_Up:
3216 if (dview->skip_rows > 0)
3218 dview->skip_rows--;
3219 dview->search.last_accessed_num_line = dview->skip_rows;
3221 break;
3222 case CK_Down:
3223 dview->skip_rows++;
3224 dview->search.last_accessed_num_line = dview->skip_rows;
3225 break;
3226 case CK_PageDown:
3227 if (dview->height > 2)
3229 dview->skip_rows += dview->height - 2;
3230 dview->search.last_accessed_num_line = dview->skip_rows;
3232 break;
3233 case CK_PageUp:
3234 if (dview->height > 2)
3236 dview->skip_rows -= dview->height - 2;
3237 dview->search.last_accessed_num_line = dview->skip_rows;
3239 break;
3240 case CK_Left:
3241 dview->skip_cols--;
3242 break;
3243 case CK_Right:
3244 dview->skip_cols++;
3245 break;
3246 case CK_LeftQuick:
3247 dview->skip_cols -= 8;
3248 break;
3249 case CK_RightQuick:
3250 dview->skip_cols += 8;
3251 break;
3252 case CK_Home:
3253 dview->skip_cols = 0;
3254 break;
3255 case CK_Shell:
3256 toggle_subshell ();
3257 break;
3258 case CK_Quit:
3259 dview->view_quit = TRUE;
3260 break;
3261 case CK_Save:
3262 dview_do_save (dview);
3263 break;
3264 case CK_Options:
3265 dview_diff_options (dview);
3266 break;
3267 #ifdef HAVE_CHARSET
3268 case CK_SelectCodepage:
3269 dview_select_encoding (dview);
3270 break;
3271 #endif
3272 case CK_Cancel:
3273 /* don't close diffviewer due to SIGINT */
3274 break;
3275 default:
3276 res = MSG_NOT_HANDLED;
3278 return res;
3281 /* --------------------------------------------------------------------------------------------- */
3283 static cb_ret_t
3284 dview_handle_key (WDiff * dview, int key)
3286 long command;
3288 #ifdef HAVE_CHARSET
3289 key = convert_from_input_c (key);
3290 #endif
3292 command = keybind_lookup_keymap_command (diff_map, key);
3293 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3294 return MSG_HANDLED;
3296 /* Key not used */
3297 return MSG_NOT_HANDLED;
3300 /* --------------------------------------------------------------------------------------------- */
3302 static cb_ret_t
3303 dview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3305 WDiff *dview = (WDiff *) w;
3306 WDialog *h = DIALOG (w->owner);
3307 cb_ret_t i;
3309 switch (msg)
3311 case MSG_INIT:
3312 dview_labels (dview);
3313 dview_load_options (dview);
3314 dview_update (dview);
3315 return MSG_HANDLED;
3317 case MSG_DRAW:
3318 dview->new_frame = TRUE;
3319 dview_update (dview);
3320 return MSG_HANDLED;
3322 case MSG_KEY:
3323 i = dview_handle_key (dview, parm);
3324 if (dview->view_quit)
3325 dlg_stop (h);
3326 else
3327 dview_update (dview);
3328 return i;
3330 case MSG_ACTION:
3331 i = dview_execute_cmd (dview, parm);
3332 if (dview->view_quit)
3333 dlg_stop (h);
3334 else
3335 dview_update (dview);
3336 return i;
3338 case MSG_RESIZE:
3339 dview_compute_areas (dview);
3340 return MSG_HANDLED;
3342 case MSG_DESTROY:
3343 dview_save_options (dview);
3344 dview_fini (dview);
3345 return MSG_HANDLED;
3347 default:
3348 return widget_default_callback (w, sender, msg, parm, data);
3352 /* --------------------------------------------------------------------------------------------- */
3354 static void
3355 dview_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3357 WDiff *dview = (WDiff *) w;
3359 (void) event;
3361 switch (msg)
3363 case MSG_MOUSE_SCROLL_UP:
3364 case MSG_MOUSE_SCROLL_DOWN:
3365 if (msg == MSG_MOUSE_SCROLL_UP)
3366 dview->skip_rows -= 2;
3367 else
3368 dview->skip_rows += 2;
3370 dview->search.last_accessed_num_line = dview->skip_rows;
3371 dview_update (dview);
3372 break;
3374 default:
3375 break;
3379 /* --------------------------------------------------------------------------------------------- */
3381 static cb_ret_t
3382 dview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3384 WDiff *dview;
3385 WDialog *h = DIALOG (w);
3387 switch (msg)
3389 case MSG_ACTION:
3390 /* Handle shortcuts. */
3392 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3393 * here, which is why we can pass NULL in the following call. */
3394 return dview_execute_cmd (NULL, parm);
3396 case MSG_VALIDATE:
3397 dview = (WDiff *) find_widget_type (h, dview_callback);
3398 /* don't stop the dialog before final decision */
3399 widget_set_state (w, WST_ACTIVE, TRUE);
3400 if (dview_ok_to_exit (dview))
3401 dlg_stop (h);
3402 return MSG_HANDLED;
3404 default:
3405 return dlg_default_callback (w, sender, msg, parm, data);
3409 /* --------------------------------------------------------------------------------------------- */
3411 static char *
3412 dview_get_title (const WDialog * h, size_t len)
3414 const WDiff *dview;
3415 const char *modified = " (*) ";
3416 const char *notmodified = " ";
3417 size_t len1;
3418 GString *title;
3420 dview = (const WDiff *) find_widget_type (h, dview_callback);
3421 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3423 title = g_string_sized_new (len);
3424 g_string_append (title, _("Diff:"));
3425 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3426 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3427 g_string_append (title, " | ");
3428 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3429 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3431 return g_string_free (title, FALSE);
3434 /* --------------------------------------------------------------------------------------------- */
3436 static int
3437 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3439 int error;
3440 WDiff *dview;
3441 Widget *w;
3442 WDialog *dview_dlg;
3443 Widget *dw;
3444 WGroup *g;
3446 /* Create dialog and widgets, put them on the dialog */
3447 dview_dlg =
3448 dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, NULL, dview_dialog_callback, NULL,
3449 "[Diff Viewer]", NULL);
3450 dw = WIDGET (dview_dlg);
3451 widget_want_tab (dw, TRUE);
3453 g = GROUP (dview_dlg);
3455 dview = g_new0 (WDiff, 1);
3456 w = WIDGET (dview);
3457 widget_init (w, dw->y, dw->x, dw->lines - 1, dw->cols, dview_callback, dview_mouse_callback);
3458 w->options |= WOP_SELECTABLE;
3459 group_add_widget_autopos (g, w, WPOS_KEEP_ALL, NULL);
3461 w = WIDGET (buttonbar_new (TRUE));
3462 group_add_widget_autopos (g, w, w->pos_flags, NULL);
3464 dview_dlg->get_title = dview_get_title;
3466 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3468 if (error == 0)
3469 dlg_run (dview_dlg);
3471 if (error != 0 || widget_get_state (dw, WST_CLOSED))
3472 dlg_destroy (dview_dlg);
3474 return error == 0 ? 1 : 0;
3477 /*** public functions ****************************************************************************/
3478 /* --------------------------------------------------------------------------------------------- */
3480 #define GET_FILE_AND_STAMP(n) \
3481 do \
3483 use_copy##n = 0; \
3484 real_file##n = file##n; \
3485 if (!vfs_file_is_local (file##n)) \
3487 real_file##n = mc_getlocalcopy (file##n); \
3488 if (real_file##n != NULL) \
3490 use_copy##n = 1; \
3491 if (mc_stat (real_file##n, &st##n) != 0) \
3492 use_copy##n = -1; \
3496 while (0)
3498 #define UNGET_FILE(n) \
3499 do \
3501 if (use_copy##n) \
3503 int changed = 0; \
3504 if (use_copy##n > 0) \
3506 time_t mtime; \
3507 mtime = st##n.st_mtime; \
3508 if (mc_stat (real_file##n, &st##n) == 0) \
3509 changed = (mtime != st##n.st_mtime); \
3511 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3512 vfs_path_free (real_file##n); \
3515 while (0)
3517 gboolean
3518 dview_diff_cmd (const void *f0, const void *f1)
3520 int rv = 0;
3521 vfs_path_t *file0 = NULL;
3522 vfs_path_t *file1 = NULL;
3523 gboolean is_dir0 = FALSE;
3524 gboolean is_dir1 = FALSE;
3526 switch (mc_global.mc_run_mode)
3528 case MC_RUN_FULL:
3530 /* run from panels */
3531 const WPanel *panel0 = (const WPanel *) f0;
3532 const WPanel *panel1 = (const WPanel *) f1;
3534 file0 =
3535 vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, (char *) NULL);
3536 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3537 if (is_dir0)
3539 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3540 path_trunc (selection (panel0)->fname, 30));
3541 goto ret;
3544 file1 =
3545 vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, (char *) NULL);
3546 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3547 if (is_dir1)
3549 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3550 path_trunc (selection (panel1)->fname, 30));
3551 goto ret;
3553 break;
3556 case MC_RUN_DIFFVIEWER:
3558 /* run from command line */
3559 const char *p0 = (const char *) f0;
3560 const char *p1 = (const char *) f1;
3561 struct stat st;
3563 file0 = vfs_path_from_str (p0);
3564 if (mc_stat (file0, &st) == 0)
3566 is_dir0 = S_ISDIR (st.st_mode);
3567 if (is_dir0)
3569 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3570 goto ret;
3573 else
3575 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3576 path_trunc (p0, 30), unix_error_string (errno));
3577 goto ret;
3580 file1 = vfs_path_from_str (p1);
3581 if (mc_stat (file1, &st) == 0)
3583 is_dir1 = S_ISDIR (st.st_mode);
3584 if (is_dir1)
3586 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3587 goto ret;
3590 else
3592 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3593 path_trunc (p1, 30), unix_error_string (errno));
3594 goto ret;
3596 break;
3599 default:
3600 /* this should not happaned */
3601 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3602 return FALSE;
3605 if (rv == 0)
3607 rv = -1;
3608 if (file0 != NULL && file1 != NULL)
3610 int use_copy0;
3611 int use_copy1;
3612 struct stat st0;
3613 struct stat st1;
3614 vfs_path_t *real_file0;
3615 vfs_path_t *real_file1;
3617 GET_FILE_AND_STAMP (0);
3618 GET_FILE_AND_STAMP (1);
3620 if (real_file0 != NULL && real_file1 != NULL)
3621 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3622 vfs_path_as_str (file0), vfs_path_as_str (file1));
3624 UNGET_FILE (1);
3625 UNGET_FILE (0);
3629 if (rv == 0)
3630 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3632 ret:
3633 vfs_path_free (file1);
3634 vfs_path_free (file0);
3636 return (rv != 0);
3639 #undef GET_FILE_AND_STAMP
3640 #undef UNGET_FILE
3642 /* --------------------------------------------------------------------------------------------- */