tar: avoid need for base64_init and extra table.
[midnight-commander.git] / src / diffviewer / ydiff.c
blob6f7caf2395f1c99c953d54bd44d001d791d5f9a5
1 /*
2 File difference viewer
4 Copyright (C) 2007-2024
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-2022
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>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stddef.h> /* ptrdiff_t */
35 #include <stdlib.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
40 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/tty/color.h"
43 #include "lib/tty/key.h"
44 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
45 #include "lib/vfs/vfs.h"
46 #include "lib/util.h"
47 #include "lib/widget.h"
48 #include "lib/strutil.h"
49 #ifdef HAVE_CHARSET
50 #include "lib/charsets.h"
51 #endif
52 #include "lib/event.h" /* mc_event_raise() */
54 #include "src/filemanager/cmd.h" /* edit_file_at_line() */
55 #include "src/filemanager/panel.h"
56 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
58 #include "src/execute.h" /* toggle_subshell() */
59 #include "src/keymap.h"
60 #include "src/setup.h"
61 #include "src/history.h"
62 #ifdef HAVE_CHARSET
63 #include "src/selcodepage.h"
64 #endif
66 #include "ydiff.h"
67 #include "internal.h"
69 /*** global variables ****************************************************************************/
71 /*** file scope macro definitions ****************************************************************/
73 #define FILE_READ_BUF 4096
74 #define FILE_FLAG_TEMP (1 << 0)
76 #define ADD_CH '+'
77 #define DEL_CH '-'
78 #define CHG_CH '*'
79 #define EQU_CH ' '
81 #define HDIFF_ENABLE 1
82 #define HDIFF_MINCTX 5
83 #define HDIFF_DEPTH 10
85 #define FILE_DIRTY(fs) \
86 do \
87 { \
88 (fs)->pos = 0; \
89 (fs)->len = 0; \
90 } \
91 while (0)
93 /*** file scope type declarations ****************************************************************/
95 typedef enum
97 FROM_LEFT_TO_RIGHT,
98 FROM_RIGHT_TO_LEFT
99 } action_direction_t;
101 /*** forward declarations (file scope functions) *************************************************/
103 /*** file scope variables ************************************************************************/
105 /* --------------------------------------------------------------------------------------------- */
106 /*** file scope functions ************************************************************************/
107 /* --------------------------------------------------------------------------------------------- */
109 static inline int
110 TAB_SKIP (int ts, int pos)
112 if (ts > 0 && ts < 9)
113 return ts - pos % ts;
114 else
115 return 8 - pos % 8;
118 /* --------------------------------------------------------------------------------------------- */
121 * Fill buffer by spaces
123 * @param buf buffer
124 * @param n number of spaces
125 * @param zero_terminate add a nul after @n spaces
127 static void
128 fill_by_space (char *buf, size_t n, gboolean zero_terminate)
130 memset (buf, ' ', n);
131 if (zero_terminate)
132 buf[n] = '\0';
135 /* --------------------------------------------------------------------------------------------- */
137 static gboolean
138 rewrite_backup_content (const vfs_path_t *from_file_name_vpath, const char *to_file_name)
140 FILE *backup_fd;
141 char *contents;
142 gsize length;
143 const char *from_file_name;
145 from_file_name = vfs_path_get_last_path_str (from_file_name_vpath);
146 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
147 return FALSE;
149 backup_fd = fopen (to_file_name, "w");
150 if (backup_fd == NULL)
152 g_free (contents);
153 return FALSE;
156 length = fwrite ((const void *) contents, length, 1, backup_fd);
158 fflush (backup_fd);
159 fclose (backup_fd);
160 g_free (contents);
161 return TRUE;
164 /* buffered I/O ************************************************************* */
167 * Try to open a temporary file.
168 * @note the name is not altered if this function fails
170 * @param[out] name address of a pointer to store the temporary name
171 * @return file descriptor on success, negative on error
174 static int
175 open_temp (void **name)
177 int fd;
178 vfs_path_t *diff_file_name = NULL;
180 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
181 if (fd == -1)
183 message (D_ERROR, MSG_ERROR,
184 _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
185 return -1;
188 *name = vfs_path_free (diff_file_name, FALSE);
189 return fd;
192 /* --------------------------------------------------------------------------------------------- */
195 * Allocate file structure and associate file descriptor to it.
197 * @param fd file descriptor
198 * @return file structure
201 static FBUF *
202 dview_fdopen (int fd)
204 FBUF *fs;
206 if (fd < 0)
207 return NULL;
209 fs = g_try_malloc (sizeof (FBUF));
210 if (fs == NULL)
211 return NULL;
213 fs->buf = g_try_malloc (FILE_READ_BUF);
214 if (fs->buf == NULL)
216 g_free (fs);
217 return NULL;
220 fs->fd = fd;
221 FILE_DIRTY (fs);
222 fs->flags = 0;
223 fs->data = NULL;
225 return fs;
228 /* --------------------------------------------------------------------------------------------- */
231 * Free file structure without closing the file.
233 * @param fs file structure
234 * @return 0 on success, non-zero on error
237 static int
238 dview_ffree (FBUF *fs)
240 int rv = 0;
242 if ((fs->flags & FILE_FLAG_TEMP) != 0)
244 rv = unlink (fs->data);
245 g_free (fs->data);
247 g_free (fs->buf);
248 g_free (fs);
249 return rv;
252 /* --------------------------------------------------------------------------------------------- */
255 * Open a binary temporary file in R/W mode.
256 * @note the file will be deleted when closed
258 * @return file structure
260 static FBUF *
261 dview_ftemp (void)
263 int fd;
264 FBUF *fs;
266 fs = dview_fdopen (0);
267 if (fs == NULL)
268 return NULL;
270 fd = open_temp (&fs->data);
271 if (fd < 0)
273 dview_ffree (fs);
274 return NULL;
277 fs->fd = fd;
278 fs->flags = FILE_FLAG_TEMP;
279 return fs;
282 /* --------------------------------------------------------------------------------------------- */
285 * Open a binary file in specified mode.
287 * @param filename file name
288 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
290 * @return file structure
293 static FBUF *
294 dview_fopen (const char *filename, int flags)
296 int fd;
297 FBUF *fs;
299 fs = dview_fdopen (0);
300 if (fs == NULL)
301 return NULL;
303 fd = open (filename, flags);
304 if (fd < 0)
306 dview_ffree (fs);
307 return NULL;
310 fs->fd = fd;
311 return fs;
314 /* --------------------------------------------------------------------------------------------- */
317 * Read a line of bytes from file until newline or EOF.
318 * @note does not stop on null-byte
319 * @note buf will not be null-terminated
321 * @param buf destination buffer
322 * @param size size of buffer
323 * @param fs file structure
325 * @return number of bytes read
328 static size_t
329 dview_fgets (char *buf, size_t size, FBUF *fs)
331 size_t j = 0;
335 int i;
336 gboolean stop = FALSE;
338 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
340 buf[j] = fs->buf[i];
341 if (buf[j] == '\n')
342 stop = TRUE;
344 fs->pos = i;
346 if (j == size || stop)
347 break;
349 fs->pos = 0;
350 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
352 while (fs->len > 0);
354 return j;
357 /* --------------------------------------------------------------------------------------------- */
360 * Seek into file.
361 * @note avoids thrashing read cache when possible
363 * @param fs file structure
364 * @param off offset
365 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
367 * @return position in file, starting from beginning
370 static off_t
371 dview_fseek (FBUF *fs, off_t off, int whence)
373 off_t rv;
375 if (fs->len != 0 && whence != SEEK_END)
377 rv = lseek (fs->fd, 0, SEEK_CUR);
378 if (rv != -1)
380 if (whence == SEEK_CUR)
382 whence = SEEK_SET;
383 off += rv - fs->len + fs->pos;
385 if (off - rv >= -fs->len && off - rv <= 0)
387 fs->pos = fs->len + off - rv;
388 return off;
393 rv = lseek (fs->fd, off, whence);
394 if (rv != -1)
395 FILE_DIRTY (fs);
396 return rv;
399 /* --------------------------------------------------------------------------------------------- */
402 * Seek to the beginning of file, thrashing read cache.
404 * @param fs file structure
406 * @return 0 if success, non-zero on error
409 static off_t
410 dview_freset (FBUF *fs)
412 off_t rv;
414 rv = lseek (fs->fd, 0, SEEK_SET);
415 if (rv != -1)
416 FILE_DIRTY (fs);
417 return rv;
420 /* --------------------------------------------------------------------------------------------- */
423 * Write bytes to file.
424 * @note thrashes read cache
426 * @param fs file structure
427 * @param buf source buffer
428 * @param size size of buffer
430 * @return number of written bytes, -1 on error
433 static ssize_t
434 dview_fwrite (FBUF *fs, const char *buf, size_t size)
436 ssize_t rv;
438 rv = write (fs->fd, buf, size);
439 if (rv >= 0)
440 FILE_DIRTY (fs);
441 return rv;
444 /* --------------------------------------------------------------------------------------------- */
447 * Truncate file to the current position.
448 * @note thrashes read cache
450 * @param fs file structure
452 * @return current file size on success, negative on error
455 static off_t
456 dview_ftrunc (FBUF *fs)
458 off_t off;
460 off = lseek (fs->fd, 0, SEEK_CUR);
461 if (off != -1)
463 int rv;
465 rv = ftruncate (fs->fd, off);
466 if (rv != 0)
467 off = -1;
468 else
469 FILE_DIRTY (fs);
471 return off;
474 /* --------------------------------------------------------------------------------------------- */
477 * Close file.
478 * @note if this is temporary file, it is deleted
480 * @param fs file structure
481 * @return 0 on success, non-zero on error
484 static int
485 dview_fclose (FBUF *fs)
487 int rv = -1;
489 if (fs != NULL)
491 rv = close (fs->fd);
492 dview_ffree (fs);
495 return rv;
498 /* --------------------------------------------------------------------------------------------- */
501 * Create pipe stream to process.
503 * @param cmd shell command line
504 * @param flags open mode, either O_RDONLY or O_WRONLY
506 * @return file structure
509 static FBUF *
510 dview_popen (const char *cmd, int flags)
512 FILE *f;
513 FBUF *fs;
514 const char *type = NULL;
516 if (flags == O_RDONLY)
517 type = "r";
518 else if (flags == O_WRONLY)
519 type = "w";
521 if (type == NULL)
522 return NULL;
524 fs = dview_fdopen (0);
525 if (fs == NULL)
526 return NULL;
528 f = popen (cmd, type);
529 if (f == NULL)
531 dview_ffree (fs);
532 return NULL;
535 fs->fd = fileno (f);
536 fs->data = f;
537 return fs;
540 /* --------------------------------------------------------------------------------------------- */
543 * Close pipe stream.
545 * @param fs structure
546 * @return 0 on success, non-zero on error
549 static int
550 dview_pclose (FBUF *fs)
552 int rv = -1;
554 if (fs != NULL)
556 rv = pclose (fs->data);
557 dview_ffree (fs);
560 return rv;
563 /* --------------------------------------------------------------------------------------------- */
566 * Get one char (byte) from string
568 * @param str ...
569 * @param ch ...
570 * @return TRUE on success, FALSE otherwise
573 static gboolean
574 dview_get_byte (const char *str, int *ch)
576 if (str == NULL)
577 return FALSE;
579 *ch = (unsigned char) (*str);
580 return TRUE;
583 /* --------------------------------------------------------------------------------------------- */
585 #ifdef HAVE_CHARSET
587 * Get utf multibyte char from string
589 * @param str ...
590 * @param ch ...
591 * @param ch_length ...
592 * @return TRUE on success, FALSE otherwise
595 static gboolean
596 dview_get_utf (const char *str, int *ch, int *ch_length)
598 if (str == NULL)
599 return FALSE;
601 *ch = g_utf8_get_char_validated (str, -1);
603 if (*ch < 0)
605 *ch = (unsigned char) (*str);
606 *ch_length = 1;
608 else
610 const char *next_ch;
612 /* Calculate UTF-8 char length */
613 next_ch = g_utf8_next_char (str);
614 *ch_length = next_ch - str;
617 return TRUE;
620 /* --------------------------------------------------------------------------------------------- */
622 static int
623 dview_str_utf8_offset_to_pos (const char *text, size_t length)
625 ptrdiff_t result;
627 if (text == NULL || text[0] == '\0')
628 return length;
630 if (g_utf8_validate (text, -1, NULL))
631 result = g_utf8_offset_to_pointer (text, length) - text;
632 else
634 gunichar uni;
635 char *tmpbuf, *buffer;
637 buffer = tmpbuf = g_strdup (text);
638 while (tmpbuf[0] != '\0')
640 uni = g_utf8_get_char_validated (tmpbuf, -1);
641 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
642 tmpbuf = g_utf8_next_char (tmpbuf);
643 else
645 tmpbuf[0] = '.';
646 tmpbuf++;
649 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
650 g_free (buffer);
652 return MAX (length, (size_t) result);
654 #endif /*HAVE_CHARSET */
656 /* --------------------------------------------------------------------------------------------- */
658 /* diff parse *************************************************************** */
661 * Read decimal number from string.
663 * @param[in,out] str string to parse
664 * @param[out] n extracted number
665 * @return 0 if success, otherwise non-zero
668 static int
669 scan_deci (const char **str, int *n)
671 const char *p = *str;
672 char *q;
674 errno = 0;
675 *n = strtol (p, &q, 10);
676 if (errno != 0 || p == q)
677 return -1;
678 *str = q;
679 return 0;
682 /* --------------------------------------------------------------------------------------------- */
685 * Parse line for diff statement.
687 * @param p string to parse
688 * @param ops list of diff statements
689 * @return 0 if success, otherwise non-zero
692 static int
693 scan_line (const char *p, GArray *ops)
695 DIFFCMD op;
697 int f1, f2;
698 int t1, t2;
699 int cmd;
700 gboolean range = FALSE;
702 /* handle the following cases:
703 * NUMaNUM[,NUM]
704 * NUM[,NUM]cNUM[,NUM]
705 * NUM[,NUM]dNUM
706 * where NUM is a positive integer
709 if (scan_deci (&p, &f1) != 0 || f1 < 0)
710 return -1;
712 f2 = f1;
713 if (*p == ',')
715 p++;
716 if (scan_deci (&p, &f2) != 0 || f2 < f1)
717 return -1;
719 range = TRUE;
722 cmd = *p++;
723 if (cmd == 'a')
725 if (range)
726 return -1;
728 else if (cmd != 'c' && cmd != 'd')
729 return -1;
731 if (scan_deci (&p, &t1) != 0 || t1 < 0)
732 return -1;
734 t2 = t1;
735 range = FALSE;
736 if (*p == ',')
738 p++;
739 if (scan_deci (&p, &t2) != 0 || t2 < t1)
740 return -1;
742 range = TRUE;
745 if (cmd == 'd' && range)
746 return -1;
748 op.a[0][0] = f1;
749 op.a[0][1] = f2;
750 op.cmd = cmd;
751 op.a[1][0] = t1;
752 op.a[1][1] = t2;
753 g_array_append_val (ops, op);
754 return 0;
757 /* --------------------------------------------------------------------------------------------- */
760 * Parse diff output and extract diff statements.
762 * @param f stream to read from
763 * @param ops list of diff statements to fill
764 * @return positive number indicating number of hunks, otherwise negative
767 static int
768 scan_diff (FBUF *f, GArray *ops)
770 int sz;
771 char buf[BUFSIZ];
773 while ((sz = dview_fgets (buf, sizeof (buf) - 1, f)) != 0)
775 if (isdigit (buf[0]))
777 if (buf[sz - 1] != '\n')
778 return -1;
780 buf[sz] = '\0';
781 if (scan_line (buf, ops) != 0)
782 return -1;
784 else
785 while (buf[sz - 1] != '\n' && (sz = dview_fgets (buf, sizeof (buf), f)) != 0)
789 return ops->len;
792 /* --------------------------------------------------------------------------------------------- */
795 * Invoke diff and extract diff statements.
797 * @param args extra arguments to be passed to diff
798 * @param extra more arguments to be passed to diff
799 * @param file1 first file to compare
800 * @param file2 second file to compare
801 * @param ops list of diff statements to fill
803 * @return positive number indicating number of hunks, otherwise negative
806 static int
807 dff_execute (const char *args, const char *extra, const char *file1, const char *file2, GArray *ops)
809 static const char *opt =
810 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
811 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
812 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
813 " --unchanged-group-format=''";
815 int rv;
816 FBUF *f;
817 char *cmd;
818 int code;
819 char *file1_esc, *file2_esc;
821 /* escape potential $ to avoid shell variable substitutions in popen() */
822 file1_esc = str_shell_escape (file1);
823 file2_esc = str_shell_escape (file2);
824 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
825 g_free (file1_esc);
826 g_free (file2_esc);
828 if (cmd == NULL)
829 return -1;
831 f = dview_popen (cmd, O_RDONLY);
832 g_free (cmd);
834 if (f == NULL)
835 return -1;
837 rv = scan_diff (f, ops);
838 code = dview_pclose (f);
840 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
841 rv = -1;
843 return rv;
846 /* --------------------------------------------------------------------------------------------- */
848 static gboolean
849 printer_for (char ch, DFUNC printer, void *ctx, FBUF *f, int *line, off_t *off)
851 size_t sz;
852 char buf[BUFSIZ];
854 sz = dview_fgets (buf, sizeof (buf), f);
855 if (sz == 0)
856 return FALSE;
858 (*line)++;
859 printer (ctx, ch, *line, *off, sz, buf);
860 *off += sz;
862 while (buf[sz - 1] != '\n')
864 sz = dview_fgets (buf, sizeof (buf), f);
865 if (sz == 0)
867 printer (ctx, 0, 0, 0, 1, "\n");
868 break;
871 printer (ctx, 0, 0, 0, sz, buf);
872 *off += sz;
875 return TRUE;
878 /* --------------------------------------------------------------------------------------------- */
881 * Reparse and display file according to diff statements.
883 * @param ord DIFF_LEFT if 1st file is displayed , DIFF_RIGHT if 2nd file is displayed.
884 * @param filename file name to display
885 * @param ops list of diff statements
886 * @param printer printf-like function to be used for displaying
887 * @param ctx printer context
889 * @return 0 if success, otherwise non-zero
892 static int
893 dff_reparse (diff_place_t ord, const char *filename, const GArray *ops, DFUNC printer, void *ctx)
895 size_t i;
896 FBUF *f;
897 int line = 0;
898 off_t off = 0;
899 const DIFFCMD *op;
900 diff_place_t eff;
901 int add_cmd, del_cmd;
903 f = dview_fopen (filename, O_RDONLY);
904 if (f == NULL)
905 return -1;
907 if (ord != DIFF_LEFT)
908 ord = DIFF_RIGHT;
909 eff = ord;
911 if (ord != DIFF_LEFT)
913 add_cmd = 'd';
914 del_cmd = 'a';
916 else
918 add_cmd = 'a';
919 del_cmd = 'd';
921 #define F1 a[eff][0]
922 #define F2 a[eff][1]
923 #define T1 a[ ord^1 ][0]
924 #define T2 a[ ord^1 ][1]
925 for (i = 0; i < ops->len; i++)
927 int n;
929 op = &g_array_index (ops, DIFFCMD, i);
931 n = op->F1;
932 if (op->cmd != add_cmd)
933 n--;
935 while (line < n && printer_for (EQU_CH, printer, ctx, f, &line, &off))
938 if (line != n)
939 goto err;
941 if (op->cmd == add_cmd)
942 for (n = op->T2 - op->T1 + 1; n != 0; n--)
943 printer (ctx, DEL_CH, 0, 0, 1, "\n");
945 if (op->cmd == del_cmd)
947 for (n = op->F2 - op->F1 + 1;
948 n != 0 && printer_for (ADD_CH, printer, ctx, f, &line, &off); n--)
951 if (n != 0)
952 goto err;
955 if (op->cmd == 'c')
957 for (n = op->F2 - op->F1 + 1;
958 n != 0 && printer_for (CHG_CH, printer, ctx, f, &line, &off); n--)
961 if (n != 0)
962 goto err;
964 for (n = op->T2 - op->T1 - (op->F2 - op->F1); n > 0; n--)
965 printer (ctx, CHG_CH, 0, 0, 1, "\n");
968 #undef T2
969 #undef T1
970 #undef F2
971 #undef F1
973 while (printer_for (EQU_CH, printer, ctx, f, &line, &off))
976 dview_fclose (f);
977 return 0;
979 err:
980 dview_fclose (f);
981 return -1;
984 /* --------------------------------------------------------------------------------------------- */
986 /* horizontal diff ********************************************************** */
989 * Longest common substring.
991 * @param s first string
992 * @param m length of first string
993 * @param t second string
994 * @param n length of second string
995 * @param ret list of offsets for longest common substrings inside each string
996 * @param min minimum length of common substrings
998 * @return 0 if success, nonzero otherwise
1001 static int
1002 lcsubstr (const char *s, int m, const char *t, int n, GArray *ret, int min)
1004 int i, j;
1005 int *Lprev, *Lcurr;
1006 int z = 0;
1008 if (m < min || n < min)
1010 /* XXX early culling */
1011 return 0;
1014 Lprev = g_try_new0 (int, n + 1);
1015 if (Lprev == NULL)
1016 return -1;
1018 Lcurr = g_try_new0 (int, n + 1);
1019 if (Lcurr == NULL)
1021 g_free (Lprev);
1022 return -1;
1025 for (i = 0; i < m; i++)
1027 int *L;
1029 L = Lprev;
1030 Lprev = Lcurr;
1031 Lcurr = L;
1032 #ifdef USE_MEMSET_IN_LCS
1033 memset (Lcurr, 0, (n + 1) * sizeof (*Lcurr));
1034 #endif
1035 for (j = 0; j < n; j++)
1037 #ifndef USE_MEMSET_IN_LCS
1038 Lcurr[j + 1] = 0;
1039 #endif
1040 if (s[i] == t[j])
1042 int v;
1044 v = Lprev[j] + 1;
1045 Lcurr[j + 1] = v;
1046 if (z < v)
1048 z = v;
1049 g_array_set_size (ret, 0);
1051 if (z == v && z >= min)
1053 int off0, off1;
1054 size_t k;
1056 off0 = i - z + 1;
1057 off1 = j - z + 1;
1059 for (k = 0; k < ret->len; k++)
1061 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1062 if ((*p)[0] == off0 || (*p)[1] >= off1)
1063 break;
1065 if (k == ret->len)
1067 PAIR p2;
1069 p2[0] = off0;
1070 p2[1] = off1;
1071 g_array_append_val (ret, p2);
1078 g_free (Lcurr);
1079 g_free (Lprev);
1080 return z;
1083 /* --------------------------------------------------------------------------------------------- */
1086 * Scan recursively for common substrings and build ranges.
1088 * @param s first string
1089 * @param t second string
1090 * @param bracket current limits for both of the strings
1091 * @param min minimum length of common substrings
1092 * @param hdiff list of horizontal diff ranges to fill
1093 * @param depth recursion depth
1095 * @return 0 if success, nonzero otherwise
1098 static gboolean
1099 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray *hdiff,
1100 unsigned int depth)
1102 BRACKET p;
1104 if (depth-- != 0)
1106 GArray *ret;
1107 BRACKET b;
1108 int len;
1110 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1112 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1113 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1114 if (ret->len != 0)
1116 size_t k = 0;
1117 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1118 const PAIR *data2;
1120 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1121 b[DIFF_LEFT].len = (*data)[0];
1122 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1123 b[DIFF_RIGHT].len = (*data)[1];
1124 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1125 return FALSE;
1127 for (k = 0; k < ret->len - 1; k++)
1129 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1130 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1131 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1132 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1133 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1134 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1135 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1136 return FALSE;
1138 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1139 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1140 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1141 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1142 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1143 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1144 return FALSE;
1146 g_array_free (ret, TRUE);
1147 return TRUE;
1151 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1152 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1153 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1154 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1155 g_array_append_val (hdiff, p);
1157 return TRUE;
1160 /* --------------------------------------------------------------------------------------------- */
1163 * Build list of horizontal diff ranges.
1165 * @param s first string
1166 * @param m length of first string
1167 * @param t second string
1168 * @param n length of second string
1169 * @param min minimum length of common substrings
1170 * @param hdiff list of horizontal diff ranges to fill
1171 * @param depth recursion depth
1173 * @return 0 if success, nonzero otherwise
1176 static gboolean
1177 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray *hdiff, unsigned int depth)
1179 int i;
1180 BRACKET b;
1182 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1183 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1185 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1188 b[DIFF_LEFT].off = i;
1189 b[DIFF_LEFT].len = m - i;
1190 b[DIFF_RIGHT].off = i;
1191 b[DIFF_RIGHT].len = n - i;
1193 /* smartscan (multiple horizontal diff) */
1194 return hdiff_multi (s, t, b, min, hdiff, depth);
1197 /* --------------------------------------------------------------------------------------------- */
1199 /* read line **************************************************************** */
1202 * Check if character is inside horizontal diff limits.
1204 * @param k rank of character inside line
1205 * @param hdiff horizontal diff structure
1206 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1208 * @return TRUE if inside hdiff limits, FALSE otherwise
1211 static gboolean
1212 is_inside (int k, GArray *hdiff, diff_place_t ord)
1214 size_t i;
1215 BRACKET *b;
1217 for (i = 0; i < hdiff->len; i++)
1219 int start, end;
1221 b = &g_array_index (hdiff, BRACKET, i);
1222 start = (*b)[ord].off;
1223 end = start + (*b)[ord].len;
1224 if (k >= start && k < end)
1225 return TRUE;
1227 return FALSE;
1230 /* --------------------------------------------------------------------------------------------- */
1233 * Copy 'src' to 'dst' expanding tabs.
1234 * @note The procedure returns when all bytes are consumed from 'src'
1236 * @param dst destination buffer
1237 * @param src source buffer
1238 * @param srcsize size of src buffer
1239 * @param base virtual base of this string, needed to calculate tabs
1240 * @param ts tab size
1242 * @return new virtual base
1245 static int
1246 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1248 int i;
1250 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1252 *dst = *src;
1253 if (*src == '\t')
1255 int j;
1257 j = TAB_SKIP (ts, i + base);
1258 i += j - 1;
1259 fill_by_space (dst, j, FALSE);
1260 dst += j - 1;
1263 return i + base;
1266 /* --------------------------------------------------------------------------------------------- */
1269 * Copy 'src' to 'dst' expanding tabs.
1271 * @param dst destination buffer
1272 * @param dstsize size of dst buffer
1273 * @param[in,out] _src source buffer
1274 * @param srcsize size of src buffer
1275 * @param base virtual base of this string, needed to calculate tabs
1276 * @param ts tab size
1278 * @return new virtual base
1280 * @note The procedure returns when all bytes are consumed from 'src'
1281 * or 'dstsize' bytes are written to 'dst'
1282 * @note Upon return, 'src' points to the first unwritten character in source
1285 static int
1286 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1288 int i;
1289 const char *src = *_src;
1291 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1293 *dst = *src;
1294 if (*src == '\t')
1296 int j;
1298 j = TAB_SKIP (ts, i + base);
1299 if (j > dstsize - i)
1300 j = dstsize - i;
1301 i += j - 1;
1302 fill_by_space (dst, j, FALSE);
1303 dst += j - 1;
1306 *_src = src;
1307 return i + base;
1310 /* --------------------------------------------------------------------------------------------- */
1313 * Read line from memory, converting tabs to spaces and padding with spaces.
1315 * @param src buffer to read from
1316 * @param srcsize size of src buffer
1317 * @param dst buffer to read to
1318 * @param dstsize size of dst buffer, excluding trailing null
1319 * @param skip number of characters to skip
1320 * @param ts tab size
1321 * @param show_cr show trailing carriage return as ^M
1323 * @return negative on error, otherwise number of bytes except padding
1326 static int
1327 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1328 gboolean show_cr)
1330 int sz = 0;
1332 if (src != NULL)
1334 int i;
1335 char *tmp = dst;
1336 const int base = 0;
1338 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1340 if (*src == '\t')
1342 int j;
1344 j = TAB_SKIP (ts, i + base);
1345 i += j - 1;
1346 while (j-- > 0)
1348 if (skip > 0)
1349 skip--;
1350 else if (dstsize != 0)
1352 dstsize--;
1353 *dst++ = ' ';
1357 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1359 if (skip == 0 && show_cr)
1361 if (dstsize > 1)
1363 dstsize -= 2;
1364 *dst++ = '^';
1365 *dst++ = 'M';
1367 else
1369 dstsize--;
1370 *dst++ = '.';
1373 break;
1375 else if (skip > 0)
1377 #ifdef HAVE_CHARSET
1378 int ch = 0;
1379 int ch_length = 1;
1381 (void) dview_get_utf (src, &ch, &ch_length);
1383 if (ch_length > 1)
1384 skip += ch_length - 1;
1385 #endif
1387 skip--;
1389 else
1391 dstsize--;
1392 *dst++ = *src;
1395 sz = dst - tmp;
1398 fill_by_space (dst, dstsize, TRUE);
1400 return sz;
1403 /* --------------------------------------------------------------------------------------------- */
1406 * Read line from memory and build attribute array.
1408 * @param src buffer to read from
1409 * @param srcsize size of src buffer
1410 * @param dst buffer to read to
1411 * @param dstsize size of dst buffer, excluding trailing null
1412 * @param skip number of characters to skip
1413 * @param ts tab size
1414 * @param show_cr show trailing carriage return as ^M
1415 * @param hdiff horizontal diff structure
1416 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1417 * @param att buffer of attributes
1419 * @return negative on error, otherwise number of bytes except padding
1422 static int
1423 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1424 gboolean show_cr, GArray *hdiff, diff_place_t ord, char *att)
1426 int sz = 0;
1428 if (src != NULL)
1430 int i, k;
1431 char *tmp = dst;
1432 const int base = 0;
1434 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1436 if (*src == '\t')
1438 int j;
1440 j = TAB_SKIP (ts, i + base);
1441 i += j - 1;
1442 while (j-- > 0)
1444 if (skip != 0)
1445 skip--;
1446 else if (dstsize != 0)
1448 dstsize--;
1449 *att++ = is_inside (k, hdiff, ord);
1450 *dst++ = ' ';
1454 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1456 if (skip == 0 && show_cr)
1458 if (dstsize > 1)
1460 dstsize -= 2;
1461 *att++ = is_inside (k, hdiff, ord);
1462 *dst++ = '^';
1463 *att++ = is_inside (k, hdiff, ord);
1464 *dst++ = 'M';
1466 else
1468 dstsize--;
1469 *att++ = is_inside (k, hdiff, ord);
1470 *dst++ = '.';
1473 break;
1475 else if (skip != 0)
1477 #ifdef HAVE_CHARSET
1478 int ch = 0;
1479 int ch_length = 1;
1481 (void) dview_get_utf (src, &ch, &ch_length);
1482 if (ch_length > 1)
1483 skip += ch_length - 1;
1484 #endif
1486 skip--;
1488 else
1490 dstsize--;
1491 *att++ = is_inside (k, hdiff, ord);
1492 *dst++ = *src;
1495 sz = dst - tmp;
1498 memset (att, '\0', dstsize);
1499 fill_by_space (dst, dstsize, TRUE);
1501 return sz;
1504 /* --------------------------------------------------------------------------------------------- */
1507 * Read line from file, converting tabs to spaces and padding with spaces.
1509 * @param f file stream to read from
1510 * @param off offset of line inside file
1511 * @param dst buffer to read to
1512 * @param dstsize size of dst buffer, excluding trailing null
1513 * @param skip number of characters to skip
1514 * @param ts tab size
1515 * @param show_cr show trailing carriage return as ^M
1517 * @return negative on error, otherwise number of bytes except padding
1520 static int
1521 cvt_fget (FBUF *f, off_t off, char *dst, size_t dstsize, int skip, int ts, gboolean show_cr)
1523 int base = 0;
1524 int old_base = base;
1525 size_t amount = dstsize;
1526 size_t useful, offset;
1527 size_t i;
1528 size_t sz;
1529 int lastch = '\0';
1530 const char *q = NULL;
1531 char tmp[BUFSIZ]; /* XXX capacity must be >= MAX{dstsize + 1, amount} */
1532 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1534 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1536 /* abnormal, but avoid buffer overflow */
1537 fill_by_space (dst, dstsize, TRUE);
1538 return 0;
1541 dview_fseek (f, off, SEEK_SET);
1543 while (skip > base)
1545 old_base = base;
1546 sz = dview_fgets (tmp, amount, f);
1547 if (sz == 0)
1548 break;
1550 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1551 if (cvt[base - old_base - 1] == '\n')
1553 q = &cvt[base - old_base - 1];
1554 base = old_base + q - cvt + 1;
1555 break;
1559 if (base < skip)
1561 fill_by_space (dst, dstsize, TRUE);
1562 return 0;
1565 useful = base - skip;
1566 offset = skip - old_base;
1568 if (useful <= dstsize)
1570 if (useful != 0)
1571 memmove (dst, cvt + offset, useful);
1573 if (q == NULL)
1575 sz = dview_fgets (tmp, dstsize - useful + 1, f);
1576 if (sz != 0)
1578 const char *ptr = tmp;
1580 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1581 if (ptr < tmp + sz)
1582 lastch = *ptr;
1585 sz = useful;
1587 else
1589 memmove (dst, cvt + offset, dstsize);
1590 sz = dstsize;
1591 lastch = cvt[offset + dstsize];
1594 dst[sz] = lastch;
1595 for (i = 0; i < sz && dst[i] != '\n'; i++)
1596 if (dst[i] == '\r' && dst[i + 1] == '\n')
1598 if (show_cr)
1600 if (i + 1 < dstsize)
1602 dst[i++] = '^';
1603 dst[i++] = 'M';
1605 else
1606 dst[i++] = '*';
1608 break;
1611 fill_by_space (dst, dstsize, TRUE);
1613 return sz;
1616 /* --------------------------------------------------------------------------------------------- */
1617 /* diff printers et al ****************************************************** */
1619 static void
1620 cc_free_elt (gpointer elt)
1622 DIFFLN *p = (DIFFLN *) elt;
1624 if (p != NULL)
1625 g_free (p->p);
1628 /* --------------------------------------------------------------------------------------------- */
1630 static int
1631 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1633 GArray *a = ((PRINTER_CTX *) ctx)->a;
1634 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1636 if (ch != 0)
1638 DIFFLN p;
1640 p.p = NULL;
1641 p.ch = ch;
1642 p.line = line;
1643 p.u.off = off;
1644 if (dsrc == DATA_SRC_MEM && line != 0)
1646 if (sz != 0 && str[sz - 1] == '\n')
1647 sz--;
1648 if (sz > 0)
1649 p.p = g_strndup (str, sz);
1650 p.u.len = sz;
1652 g_array_append_val (a, p);
1654 else if (dsrc == DATA_SRC_MEM)
1656 DIFFLN *p;
1658 p = &g_array_index (a, DIFFLN, a->len - 1);
1659 if (sz != 0 && str[sz - 1] == '\n')
1660 sz--;
1661 if (sz != 0)
1663 size_t new_size;
1664 char *q;
1666 new_size = p->u.len + sz;
1667 q = g_realloc (p->p, new_size);
1668 memcpy (q + p->u.len, str, sz);
1669 p->p = q;
1671 p->u.len += sz;
1673 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1675 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1676 dview_fwrite (f, str, sz);
1678 return 0;
1681 /* --------------------------------------------------------------------------------------------- */
1683 static int
1684 redo_diff (WDiff *dview)
1686 FBUF *const *f = dview->f;
1687 PRINTER_CTX ctx;
1688 GArray *ops;
1689 int ndiff;
1690 int rv = 0;
1691 char extra[BUF_MEDIUM];
1693 extra[0] = '\0';
1694 if (dview->opt.quality == 2)
1695 strcat (extra, " -d");
1696 if (dview->opt.quality == 1)
1697 strcat (extra, " --speed-large-files");
1698 if (dview->opt.strip_trailing_cr)
1699 strcat (extra, " --strip-trailing-cr");
1700 if (dview->opt.ignore_tab_expansion)
1701 strcat (extra, " -E");
1702 if (dview->opt.ignore_space_change)
1703 strcat (extra, " -b");
1704 if (dview->opt.ignore_all_space)
1705 strcat (extra, " -w");
1706 if (dview->opt.ignore_case)
1707 strcat (extra, " -i");
1709 if (dview->dsrc != DATA_SRC_MEM)
1711 dview_freset (f[DIFF_LEFT]);
1712 dview_freset (f[DIFF_RIGHT]);
1715 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1716 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1717 if (ndiff < 0)
1719 if (ops != NULL)
1720 g_array_free (ops, TRUE);
1721 return -1;
1724 ctx.dsrc = dview->dsrc;
1725 ctx.a = dview->a[DIFF_LEFT];
1726 ctx.f = f[DIFF_LEFT];
1727 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1729 ctx.a = dview->a[DIFF_RIGHT];
1730 ctx.f = f[DIFF_RIGHT];
1731 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1733 if (ops != NULL)
1734 g_array_free (ops, TRUE);
1736 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1737 return -1;
1739 if (dview->dsrc == DATA_SRC_TMP)
1741 dview_ftrunc (f[DIFF_LEFT]);
1742 dview_ftrunc (f[DIFF_RIGHT]);
1745 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1747 size_t i;
1749 dview->hdiff = g_ptr_array_new ();
1751 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1753 GArray *h = NULL;
1754 const DIFFLN *p;
1755 const DIFFLN *q;
1757 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1758 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1759 if (p->line != 0 && q->line != 0 && p->ch == CHG_CH)
1761 gboolean runresult;
1763 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1765 runresult =
1766 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h, HDIFF_DEPTH);
1767 if (!runresult)
1769 g_array_free (h, TRUE);
1770 h = NULL;
1774 g_ptr_array_add (dview->hdiff, h);
1777 return ndiff;
1780 /* --------------------------------------------------------------------------------------------- */
1782 static void
1783 destroy_hdiff (WDiff *dview)
1785 if (dview->hdiff != NULL)
1787 int i;
1788 int len;
1790 len = dview->a[DIFF_LEFT]->len;
1792 for (i = 0; i < len; i++)
1794 GArray *h;
1796 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1797 if (h != NULL)
1798 g_array_free (h, TRUE);
1800 g_ptr_array_free (dview->hdiff, TRUE);
1801 dview->hdiff = NULL;
1804 mc_search_free (dview->search.handle);
1805 dview->search.handle = NULL;
1806 MC_PTR_FREE (dview->search.last_string);
1809 /* --------------------------------------------------------------------------------------------- */
1810 /* stuff ******************************************************************** */
1812 static int
1813 get_digits (unsigned int n)
1815 int d = 1;
1817 while ((n /= 10) != 0)
1818 d++;
1819 return d;
1822 /* --------------------------------------------------------------------------------------------- */
1824 static int
1825 get_line_numbers (const GArray *a, size_t pos, int *linenum, int *lineofs)
1827 const DIFFLN *p;
1829 *linenum = 0;
1830 *lineofs = 0;
1832 if (a->len != 0)
1834 if (pos >= a->len)
1835 pos = a->len - 1;
1837 p = &g_array_index (a, DIFFLN, pos);
1839 if (p->line == 0)
1841 int n;
1843 for (n = pos; n > 0; n--)
1845 p--;
1846 if (p->line != 0)
1847 break;
1849 *lineofs = pos - n + 1;
1852 *linenum = p->line;
1854 return 0;
1857 /* --------------------------------------------------------------------------------------------- */
1859 static int
1860 calc_nwidth (const GArray *const *a)
1862 int l1, o1;
1863 int l2, o2;
1865 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1866 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1867 if (l1 < l2)
1868 l1 = l2;
1869 return get_digits (l1);
1872 /* --------------------------------------------------------------------------------------------- */
1874 static int
1875 find_prev_hunk (const GArray *a, int pos)
1877 #if 1
1878 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos--)
1880 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH; pos--)
1882 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos--)
1884 if (pos > 0 && (size_t) pos < a->len)
1885 pos++;
1886 #else
1887 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH; pos--)
1889 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH; pos--)
1891 #endif
1893 return pos;
1896 /* --------------------------------------------------------------------------------------------- */
1898 static size_t
1899 find_next_hunk (const GArray *a, size_t pos)
1901 for (; pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos++)
1903 for (; pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH; pos++)
1905 return pos;
1908 /* --------------------------------------------------------------------------------------------- */
1910 * Find start and end lines of the current hunk.
1912 * @param dview WDiff widget
1913 * @return boolean and
1914 * start_line1 first line of current hunk (file[0])
1915 * end_line1 last line of current hunk (file[0])
1916 * start_line1 first line of current hunk (file[0])
1917 * end_line1 last line of current hunk (file[0])
1920 static int
1921 get_current_hunk (WDiff *dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1923 const GArray *a0 = dview->a[DIFF_LEFT];
1924 const GArray *a1 = dview->a[DIFF_RIGHT];
1925 size_t pos;
1926 int ch;
1927 int res = 0;
1929 *start_line1 = 1;
1930 *start_line2 = 1;
1931 *end_line1 = 1;
1932 *end_line2 = 1;
1934 pos = dview->skip_rows;
1935 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1936 if (ch != EQU_CH)
1938 switch (ch)
1940 case ADD_CH:
1941 res = DIFF_DEL;
1942 break;
1943 case DEL_CH:
1944 res = DIFF_ADD;
1945 break;
1946 case CHG_CH:
1947 res = DIFF_CHG;
1948 break;
1949 default:
1950 break;
1953 for (; pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH; pos--)
1955 if (pos > 0)
1957 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
1958 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
1961 for (pos = dview->skip_rows;
1962 pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH; pos++)
1964 int l0, l1;
1966 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
1967 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
1968 if (l0 > 0)
1969 *end_line1 = MAX (*start_line1, l0);
1970 if (l1 > 0)
1971 *end_line2 = MAX (*start_line2, l1);
1974 return res;
1977 /* --------------------------------------------------------------------------------------------- */
1979 * Remove hunk from file.
1981 * @param dview WDiff widget
1982 * @param merge_file file stream for writing data
1983 * @param from1 first line of hunk
1984 * @param to1 last line of hunk
1985 * @param merge_direction in what direction files should be merged
1988 static void
1989 dview_remove_hunk (WDiff *dview, FILE *merge_file, int from1, int to1,
1990 action_direction_t merge_direction)
1992 int line;
1993 char buf[BUF_10K];
1994 FILE *f0;
1996 if (merge_direction == FROM_RIGHT_TO_LEFT)
1997 f0 = fopen (dview->file[DIFF_RIGHT], "r");
1998 else
1999 f0 = fopen (dview->file[DIFF_LEFT], "r");
2001 for (line = 0; fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1; line++)
2002 fputs (buf, merge_file);
2004 while (fgets (buf, sizeof (buf), f0) != NULL)
2006 line++;
2007 if (line >= to1)
2008 fputs (buf, merge_file);
2010 fclose (f0);
2013 /* --------------------------------------------------------------------------------------------- */
2015 * Add hunk to file.
2017 * @param dview WDiff widget
2018 * @param merge_file file stream for writing data
2019 * @param from1 first line of source hunk
2020 * @param from2 first line of destination hunk
2021 * @param to1 last line of source hunk
2022 * @param merge_direction in what direction files should be merged
2025 static void
2026 dview_add_hunk (WDiff *dview, FILE *merge_file, int from1, int from2, int to2,
2027 action_direction_t merge_direction)
2029 int line;
2030 char buf[BUF_10K];
2031 FILE *f0, *f1;
2033 if (merge_direction == FROM_RIGHT_TO_LEFT)
2035 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2036 f1 = fopen (dview->file[DIFF_LEFT], "r");
2038 else
2040 f0 = fopen (dview->file[DIFF_LEFT], "r");
2041 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2044 for (line = 0; fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1; line++)
2045 fputs (buf, merge_file);
2046 for (line = 0; fgets (buf, sizeof (buf), f1) != NULL && line <= to2;)
2048 line++;
2049 if (line >= from2)
2050 fputs (buf, merge_file);
2052 while (fgets (buf, sizeof (buf), f0) != NULL)
2053 fputs (buf, merge_file);
2055 fclose (f0);
2056 fclose (f1);
2059 /* --------------------------------------------------------------------------------------------- */
2061 * Replace hunk in file.
2063 * @param dview WDiff widget
2064 * @param merge_file file stream for writing data
2065 * @param from1 first line of source hunk
2066 * @param to1 last line of source hunk
2067 * @param from2 first line of destination hunk
2068 * @param to2 last line of destination hunk
2069 * @param merge_direction in what direction files should be merged
2072 static void
2073 dview_replace_hunk (WDiff *dview, FILE *merge_file, int from1, int to1, int from2, int to2,
2074 action_direction_t merge_direction)
2076 int line1, line2;
2077 char buf[BUF_10K];
2078 FILE *f0, *f1;
2080 if (merge_direction == FROM_RIGHT_TO_LEFT)
2082 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2083 f1 = fopen (dview->file[DIFF_LEFT], "r");
2085 else
2087 f0 = fopen (dview->file[DIFF_LEFT], "r");
2088 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2091 for (line1 = 0; fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1; line1++)
2092 fputs (buf, merge_file);
2093 for (line2 = 0; fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2;)
2095 line2++;
2096 if (line2 >= from2)
2097 fputs (buf, merge_file);
2099 while (fgets (buf, sizeof (buf), f0) != NULL)
2101 line1++;
2102 if (line1 > to1)
2103 fputs (buf, merge_file);
2105 fclose (f0);
2106 fclose (f1);
2109 /* --------------------------------------------------------------------------------------------- */
2111 * Merge hunk.
2113 * @param dview WDiff widget
2114 * @param merge_direction in what direction files should be merged
2117 static void
2118 do_merge_hunk (WDiff *dview, action_direction_t merge_direction)
2120 int from1, to1, from2, to2;
2121 int hunk;
2122 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2124 if (merge_direction == FROM_RIGHT_TO_LEFT)
2125 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2126 else
2127 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2129 if (hunk > 0)
2131 int merge_file_fd;
2132 FILE *merge_file;
2133 vfs_path_t *merge_file_name_vpath = NULL;
2135 if (!dview->merged[n_merge])
2137 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2138 if (!dview->merged[n_merge])
2140 message (D_ERROR, MSG_ERROR,
2141 _("Cannot create backup file\n%s%s\n%s"),
2142 dview->file[n_merge], "~~~", unix_error_string (errno));
2143 return;
2147 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2148 if (merge_file_fd == -1)
2150 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2151 unix_error_string (errno));
2152 return;
2155 merge_file = fdopen (merge_file_fd, "w");
2157 switch (hunk)
2159 case DIFF_DEL:
2160 if (merge_direction == FROM_RIGHT_TO_LEFT)
2161 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2162 else
2163 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2164 break;
2165 case DIFF_ADD:
2166 if (merge_direction == FROM_RIGHT_TO_LEFT)
2167 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2168 else
2169 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2170 break;
2171 case DIFF_CHG:
2172 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2173 break;
2174 default:
2175 break;
2177 fflush (merge_file);
2178 fclose (merge_file);
2180 int res;
2182 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2183 (void) res;
2185 mc_unlink (merge_file_name_vpath);
2186 vfs_path_free (merge_file_name_vpath, TRUE);
2190 /* --------------------------------------------------------------------------------------------- */
2191 /* view routines and callbacks ********************************************** */
2193 static void
2194 dview_compute_split (WDiff *dview, int i)
2196 dview->bias += i;
2197 if (dview->bias < 2 - dview->half1)
2198 dview->bias = 2 - dview->half1;
2199 if (dview->bias > dview->half2 - 2)
2200 dview->bias = dview->half2 - 2;
2203 /* --------------------------------------------------------------------------------------------- */
2205 static void
2206 dview_compute_areas (WDiff *dview)
2208 Widget *w = WIDGET (dview);
2210 dview->height = w->rect.lines - 1;
2211 dview->half1 = w->rect.cols / 2;
2212 dview->half2 = w->rect.cols - dview->half1;
2214 dview_compute_split (dview, 0);
2217 /* --------------------------------------------------------------------------------------------- */
2219 static void
2220 dview_reread (WDiff *dview)
2222 int ndiff;
2224 destroy_hdiff (dview);
2225 if (dview->a[DIFF_LEFT] != NULL)
2226 g_array_free (dview->a[DIFF_LEFT], TRUE);
2227 if (dview->a[DIFF_RIGHT] != NULL)
2228 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2230 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2231 g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2232 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2233 g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2235 ndiff = redo_diff (dview);
2236 if (ndiff >= 0)
2237 dview->ndiff = ndiff;
2240 /* --------------------------------------------------------------------------------------------- */
2242 #ifdef HAVE_CHARSET
2243 static void
2244 dview_set_codeset (WDiff *dview)
2246 const char *encoding_id = NULL;
2248 dview->utf8 = TRUE;
2249 encoding_id =
2250 get_codepage_id (mc_global.source_codepage >=
2251 0 ? mc_global.source_codepage : mc_global.display_codepage);
2252 if (encoding_id != NULL)
2254 GIConv conv;
2256 conv = str_crt_conv_from (encoding_id);
2257 if (conv != INVALID_CONV)
2259 if (dview->converter != str_cnv_from_term)
2260 str_close_conv (dview->converter);
2261 dview->converter = conv;
2263 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2267 /* --------------------------------------------------------------------------------------------- */
2269 static void
2270 dview_select_encoding (WDiff *dview)
2272 if (do_select_codepage ())
2273 dview_set_codeset (dview);
2274 dview_reread (dview);
2275 tty_touch_screen ();
2276 repaint_screen ();
2278 #endif /* HAVE_CHARSET */
2280 /* --------------------------------------------------------------------------------------------- */
2282 static void
2283 dview_load_options (WDiff *dview)
2285 gboolean show_numbers;
2286 int tab_size;
2288 dview->display_symbols =
2289 mc_config_get_bool (mc_global.main_config, "DiffView", "show_symbols", FALSE);
2290 show_numbers = mc_config_get_bool (mc_global.main_config, "DiffView", "show_numbers", FALSE);
2291 if (show_numbers)
2292 dview->display_numbers = 1;
2293 tab_size = mc_config_get_int (mc_global.main_config, "DiffView", "tab_size", 8);
2294 if (tab_size > 0 && tab_size < 9)
2295 dview->tab_size = tab_size;
2296 else
2297 dview->tab_size = 8;
2299 dview->opt.quality = mc_config_get_int (mc_global.main_config, "DiffView", "diff_quality", 0);
2301 dview->opt.strip_trailing_cr =
2302 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_tws", FALSE);
2303 dview->opt.ignore_all_space =
2304 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space", FALSE);
2305 dview->opt.ignore_space_change =
2306 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change", FALSE);
2307 dview->opt.ignore_tab_expansion =
2308 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_tab_expansion", FALSE);
2309 dview->opt.ignore_case =
2310 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_case", FALSE);
2312 dview->new_frame = TRUE;
2315 /* --------------------------------------------------------------------------------------------- */
2317 static void
2318 dview_save_options (WDiff *dview)
2320 mc_config_set_bool (mc_global.main_config, "DiffView", "show_symbols", dview->display_symbols);
2321 mc_config_set_bool (mc_global.main_config, "DiffView", "show_numbers",
2322 dview->display_numbers != 0);
2323 mc_config_set_int (mc_global.main_config, "DiffView", "tab_size", dview->tab_size);
2325 mc_config_set_int (mc_global.main_config, "DiffView", "diff_quality", dview->opt.quality);
2327 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_tws",
2328 dview->opt.strip_trailing_cr);
2329 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space",
2330 dview->opt.ignore_all_space);
2331 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change",
2332 dview->opt.ignore_space_change);
2333 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_tab_expansion",
2334 dview->opt.ignore_tab_expansion);
2335 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_case",
2336 dview->opt.ignore_case);
2339 /* --------------------------------------------------------------------------------------------- */
2341 static void
2342 dview_diff_options (WDiff *dview)
2344 const char *quality_str[] = {
2345 N_("No&rmal"),
2346 N_("&Fastest (Assume large files)"),
2347 N_("&Minimal (Find a smaller set of change)")
2350 quick_widget_t quick_widgets[] = {
2351 /* *INDENT-OFF* */
2352 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2353 QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2354 QUICK_STOP_GROUPBOX,
2355 QUICK_START_GROUPBOX (N_("Diff extra options")),
2356 QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2357 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2358 QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2359 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2360 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2361 NULL),
2362 QUICK_STOP_GROUPBOX,
2363 QUICK_BUTTONS_OK_CANCEL,
2364 QUICK_END
2365 /* *INDENT-ON* */
2368 WRect r = { -1, -1, 0, 56 };
2370 quick_dialog_t qdlg = {
2371 r, N_("Diff Options"), "[Diff Options]",
2372 quick_widgets, NULL, NULL
2375 if (quick_dialog (&qdlg) != B_CANCEL)
2376 dview_reread (dview);
2379 /* --------------------------------------------------------------------------------------------- */
2381 static int
2382 dview_init (WDiff *dview, const char *args, const char *file1, const char *file2,
2383 const char *label1, const char *label2, DSRC dsrc)
2385 FBUF *f[DIFF_COUNT];
2387 f[DIFF_LEFT] = NULL;
2388 f[DIFF_RIGHT] = NULL;
2390 if (dsrc == DATA_SRC_TMP)
2392 f[DIFF_LEFT] = dview_ftemp ();
2393 if (f[DIFF_LEFT] == NULL)
2394 return -1;
2396 f[DIFF_RIGHT] = dview_ftemp ();
2397 if (f[DIFF_RIGHT] == NULL)
2399 dview_fclose (f[DIFF_LEFT]);
2400 return -1;
2403 else if (dsrc == DATA_SRC_ORG)
2405 f[DIFF_LEFT] = dview_fopen (file1, O_RDONLY);
2406 if (f[DIFF_LEFT] == NULL)
2407 return -1;
2409 f[DIFF_RIGHT] = dview_fopen (file2, O_RDONLY);
2410 if (f[DIFF_RIGHT] == NULL)
2412 dview_fclose (f[DIFF_LEFT]);
2413 return -1;
2417 dview->view_quit = FALSE;
2419 dview->bias = 0;
2420 dview->new_frame = TRUE;
2421 dview->skip_rows = 0;
2422 dview->skip_cols = 0;
2423 dview->display_symbols = FALSE;
2424 dview->display_numbers = 0;
2425 dview->show_cr = TRUE;
2426 dview->tab_size = 8;
2427 dview->ord = DIFF_LEFT;
2428 dview->full = FALSE;
2430 dview->search.handle = NULL;
2431 dview->search.last_string = NULL;
2432 dview->search.last_found_line = -1;
2433 dview->search.last_accessed_num_line = -1;
2435 dview_load_options (dview);
2437 dview->args = args;
2438 dview->file[DIFF_LEFT] = file1;
2439 dview->file[DIFF_RIGHT] = file2;
2440 dview->label[DIFF_LEFT] = g_strdup (label1);
2441 dview->label[DIFF_RIGHT] = g_strdup (label2);
2442 dview->f[DIFF_LEFT] = f[0];
2443 dview->f[DIFF_RIGHT] = f[1];
2444 dview->merged[DIFF_LEFT] = FALSE;
2445 dview->merged[DIFF_RIGHT] = FALSE;
2446 dview->hdiff = NULL;
2447 dview->dsrc = dsrc;
2448 #ifdef HAVE_CHARSET
2449 dview->converter = str_cnv_from_term;
2450 dview_set_codeset (dview);
2451 #endif
2452 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2453 g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2454 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2455 g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2457 return 0;
2460 /* --------------------------------------------------------------------------------------------- */
2462 static void
2463 dview_fini (WDiff *dview)
2465 if (dview->dsrc != DATA_SRC_MEM)
2467 dview_fclose (dview->f[DIFF_RIGHT]);
2468 dview_fclose (dview->f[DIFF_LEFT]);
2471 #ifdef HAVE_CHARSET
2472 if (dview->converter != str_cnv_from_term)
2473 str_close_conv (dview->converter);
2474 #endif
2476 destroy_hdiff (dview);
2477 if (dview->a[DIFF_LEFT] != NULL)
2479 g_array_free (dview->a[DIFF_LEFT], TRUE);
2480 dview->a[DIFF_LEFT] = NULL;
2482 if (dview->a[DIFF_RIGHT] != NULL)
2484 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2485 dview->a[DIFF_RIGHT] = NULL;
2488 g_free (dview->label[DIFF_LEFT]);
2489 g_free (dview->label[DIFF_RIGHT]);
2492 /* --------------------------------------------------------------------------------------------- */
2494 static int
2495 dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int height, int width)
2497 size_t i, k;
2498 int j;
2499 char buf[BUFSIZ];
2500 FBUF *f = dview->f[ord];
2501 int skip = dview->skip_cols;
2502 gboolean display_symbols = dview->display_symbols;
2503 int display_numbers = dview->display_numbers;
2504 gboolean show_cr = dview->show_cr;
2505 int tab_size = 8;
2506 const DIFFLN *p;
2507 int nwidth = display_numbers;
2508 int xwidth;
2510 xwidth = display_numbers;
2511 if (display_symbols)
2512 xwidth++;
2513 if (dview->tab_size > 0 && dview->tab_size < 9)
2514 tab_size = dview->tab_size;
2516 if (xwidth != 0)
2518 if (xwidth > width && display_symbols)
2520 xwidth--;
2521 display_symbols = FALSE;
2523 if (xwidth > width && display_numbers != 0)
2525 xwidth = width;
2526 display_numbers = width;
2529 xwidth++;
2530 c += xwidth;
2531 width -= xwidth;
2532 if (width < 0)
2533 width = 0;
2536 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2538 /* abnormal, but avoid buffer overflow */
2539 return -1;
2542 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2544 int ch;
2545 int next_ch = 0;
2546 int col;
2547 size_t cnt;
2549 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2550 ch = p->ch;
2551 tty_setcolor (NORMAL_COLOR);
2552 if (display_symbols)
2554 tty_gotoyx (r + j, c - 2);
2555 tty_print_char (ch);
2557 if (p->line != 0)
2559 if (display_numbers != 0)
2561 tty_gotoyx (r + j, c - xwidth);
2562 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2563 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2565 if (ch == ADD_CH)
2566 tty_setcolor (DFF_ADD_COLOR);
2567 if (ch == CHG_CH)
2568 tty_setcolor (DFF_CHG_COLOR);
2569 if (f == NULL)
2571 if (i == (size_t) dview->search.last_found_line)
2572 tty_setcolor (MARKED_SELECTED_COLOR);
2573 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2575 char att[BUFSIZ];
2577 #ifdef HAVE_CHARSET
2578 if (dview->utf8)
2579 k = dview_str_utf8_offset_to_pos (p->p, width);
2580 else
2581 #endif
2582 k = width;
2584 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2585 g_ptr_array_index (dview->hdiff, i), ord, att);
2586 tty_gotoyx (r + j, c);
2587 col = 0;
2589 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2591 gboolean ch_res;
2593 #ifdef HAVE_CHARSET
2594 if (dview->utf8)
2596 int ch_length = 0;
2598 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2599 if (ch_length > 1)
2600 cnt += ch_length - 1;
2601 if (!g_unichar_isprint (next_ch))
2602 next_ch = '.';
2604 else
2605 #endif
2606 ch_res = dview_get_byte (buf + cnt, &next_ch);
2608 if (ch_res)
2610 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2611 #ifdef HAVE_CHARSET
2612 if (mc_global.utf8_display)
2614 if (!dview->utf8)
2616 next_ch =
2617 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2618 dview->converter);
2621 else if (dview->utf8)
2622 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2623 else
2624 next_ch = convert_to_display_c (next_ch);
2625 #endif
2626 tty_print_anychar (next_ch);
2627 col++;
2630 continue;
2633 if (ch == CHG_CH)
2634 tty_setcolor (DFF_CHH_COLOR);
2636 #ifdef HAVE_CHARSET
2637 if (dview->utf8)
2638 k = dview_str_utf8_offset_to_pos (p->p, width);
2639 else
2640 #endif
2641 k = width;
2642 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2644 else
2645 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2647 else
2649 if (display_numbers != 0)
2651 tty_gotoyx (r + j, c - xwidth);
2652 fill_by_space (buf, display_numbers, TRUE);
2653 tty_print_string (buf);
2655 if (ch == DEL_CH)
2656 tty_setcolor (DFF_DEL_COLOR);
2657 if (ch == CHG_CH)
2658 tty_setcolor (DFF_CHD_COLOR);
2659 fill_by_space (buf, width, TRUE);
2661 tty_gotoyx (r + j, c);
2662 /* tty_print_nstring (buf, width); */
2663 col = 0;
2664 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2666 gboolean ch_res;
2668 #ifdef HAVE_CHARSET
2669 if (dview->utf8)
2671 int ch_length = 0;
2673 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2674 if (ch_length > 1)
2675 cnt += ch_length - 1;
2676 if (!g_unichar_isprint (next_ch))
2677 next_ch = '.';
2679 else
2680 #endif
2681 ch_res = dview_get_byte (buf + cnt, &next_ch);
2683 if (ch_res)
2685 #ifdef HAVE_CHARSET
2686 if (mc_global.utf8_display)
2688 if (!dview->utf8)
2689 next_ch =
2690 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2692 else if (dview->utf8)
2693 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2694 else
2695 next_ch = convert_to_display_c (next_ch);
2696 #endif
2698 tty_print_anychar (next_ch);
2699 col++;
2703 tty_setcolor (NORMAL_COLOR);
2704 k = width;
2705 if (width < xwidth - 1)
2706 k = xwidth - 1;
2707 fill_by_space (buf, k, TRUE);
2708 for (; j < height; j++)
2710 if (xwidth != 0)
2712 tty_gotoyx (r + j, c - xwidth);
2713 /* tty_print_nstring (buf, xwidth - 1); */
2714 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2716 tty_gotoyx (r + j, c);
2717 /* tty_print_nstring (buf, width); */
2718 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2721 return 0;
2724 /* --------------------------------------------------------------------------------------------- */
2726 static void
2727 dview_status (const WDiff *dview, diff_place_t ord, int width, int c)
2729 const char *buf;
2730 int filename_width;
2731 int linenum, lineofs;
2732 vfs_path_t *vpath;
2733 char *path;
2735 tty_setcolor (STATUSBAR_COLOR);
2737 tty_gotoyx (0, c);
2738 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2740 filename_width = width - 24;
2741 if (filename_width < 8)
2742 filename_width = 8;
2744 vpath = vfs_path_from_str (dview->label[ord]);
2745 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2746 vfs_path_free (vpath, TRUE);
2747 buf = str_term_trim (path, filename_width);
2748 if (ord == DIFF_LEFT)
2749 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2750 buf, linenum, lineofs, dview->skip_cols);
2751 else
2752 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2753 buf, linenum, lineofs, dview->ndiff);
2754 g_free (path);
2757 /* --------------------------------------------------------------------------------------------- */
2759 static void
2760 dview_redo (WDiff *dview)
2762 if (dview->display_numbers != 0)
2764 int old;
2766 old = dview->display_numbers;
2767 dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
2768 dview->new_frame = (old != dview->display_numbers);
2770 dview_reread (dview);
2773 /* --------------------------------------------------------------------------------------------- */
2775 static void
2776 dview_update (WDiff *dview)
2778 int height = dview->height;
2779 int width1, width2;
2780 int last;
2782 last = dview->a[DIFF_LEFT]->len - 1;
2784 if (dview->skip_rows > last)
2785 dview->skip_rows = dview->search.last_accessed_num_line = last;
2786 if (dview->skip_rows < 0)
2787 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2788 if (dview->skip_cols < 0)
2789 dview->skip_cols = 0;
2791 if (height < 2)
2792 return;
2794 /* use an actual length of dview->a */
2795 if (dview->display_numbers != 0)
2796 dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
2798 width1 = dview->half1 + dview->bias;
2799 width2 = dview->half2 - dview->bias;
2800 if (dview->full)
2802 width1 = COLS;
2803 width2 = 0;
2806 if (dview->new_frame)
2808 int xwidth;
2810 tty_setcolor (NORMAL_COLOR);
2811 xwidth = dview->display_numbers;
2812 if (dview->display_symbols)
2813 xwidth++;
2814 if (width1 > 1)
2815 tty_draw_box (1, 0, height, width1, FALSE);
2816 if (width2 > 1)
2817 tty_draw_box (1, width1, height, width2, FALSE);
2819 if (xwidth != 0)
2821 xwidth++;
2822 if (xwidth < width1 - 1)
2824 tty_gotoyx (1, xwidth);
2825 tty_print_alt_char (ACS_TTEE, FALSE);
2826 tty_gotoyx (height, xwidth);
2827 tty_print_alt_char (ACS_BTEE, FALSE);
2828 tty_draw_vline (2, xwidth, ACS_VLINE, height - 2);
2830 if (xwidth < width2 - 1)
2832 tty_gotoyx (1, width1 + xwidth);
2833 tty_print_alt_char (ACS_TTEE, FALSE);
2834 tty_gotoyx (height, width1 + xwidth);
2835 tty_print_alt_char (ACS_BTEE, FALSE);
2836 tty_draw_vline (2, width1 + xwidth, ACS_VLINE, height - 2);
2839 dview->new_frame = FALSE;
2842 if (width1 > 2)
2844 dview_status (dview, dview->ord, width1, 0);
2845 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2847 if (width2 > 2)
2849 diff_place_t ord;
2851 ord = dview->ord == DIFF_LEFT ? DIFF_RIGHT : DIFF_LEFT;
2852 dview_status (dview, ord, width2, width1);
2853 dview_display_file (dview, ord, 2, width1 + 1, height - 2, width2 - 2);
2857 /* --------------------------------------------------------------------------------------------- */
2859 static void
2860 dview_edit (WDiff *dview, diff_place_t ord)
2862 Widget *h;
2863 gboolean h_modal;
2864 int linenum, lineofs;
2866 if (dview->dsrc == DATA_SRC_TMP)
2868 error_dialog (_("Edit"), _("Edit is disabled"));
2869 return;
2872 h = WIDGET (WIDGET (dview)->owner);
2873 h_modal = widget_get_state (h, WST_MODAL);
2875 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2877 /* disallow edit file in several editors */
2878 widget_set_state (h, WST_MODAL, TRUE);
2881 vfs_path_t *tmp_vpath;
2883 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2884 edit_file_at_line (tmp_vpath, use_internal_edit, linenum);
2885 vfs_path_free (tmp_vpath, TRUE);
2888 widget_set_state (h, WST_MODAL, h_modal);
2889 dview_redo (dview);
2890 dview_update (dview);
2893 /* --------------------------------------------------------------------------------------------- */
2895 static void
2896 dview_goto_cmd (WDiff *dview, diff_place_t ord)
2898 static gboolean first_run = TRUE;
2900 /* *INDENT-OFF* */
2901 static const char *title[2] = {
2902 N_("Goto line (left)"),
2903 N_("Goto line (right)")
2905 /* *INDENT-ON* */
2907 int newline;
2908 char *input;
2910 input =
2911 input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE,
2912 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
2913 if (input != NULL)
2915 const char *s = input;
2917 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2919 size_t i = 0;
2921 if (newline > 0)
2922 for (; i < dview->a[ord]->len; i++)
2924 const DIFFLN *p;
2926 p = &g_array_index (dview->a[ord], DIFFLN, i);
2927 if (p->line == newline)
2928 break;
2931 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2934 g_free (input);
2937 first_run = FALSE;
2940 /* --------------------------------------------------------------------------------------------- */
2942 static void
2943 dview_labels (WDiff *dview)
2945 Widget *d = WIDGET (dview);
2946 WButtonBar *b;
2948 b = buttonbar_find (DIALOG (d->owner));
2950 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), d->keymap, d);
2951 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), d->keymap, d);
2952 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), d->keymap, d);
2953 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), d->keymap, d);
2954 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), d->keymap, d);
2955 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), d->keymap, d);
2956 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), d->keymap, d);
2959 /* --------------------------------------------------------------------------------------------- */
2961 static gboolean
2962 dview_save (WDiff *dview)
2964 gboolean res = TRUE;
2966 if (dview->merged[DIFF_LEFT])
2968 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2969 dview->merged[DIFF_LEFT] = !res;
2971 if (dview->merged[DIFF_RIGHT])
2973 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2974 dview->merged[DIFF_RIGHT] = !res;
2976 return res;
2979 /* --------------------------------------------------------------------------------------------- */
2981 static void
2982 dview_do_save (WDiff *dview)
2984 (void) dview_save (dview);
2987 /* --------------------------------------------------------------------------------------------- */
2990 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2991 * ask user.
2993 static gboolean
2994 dview_ok_to_exit (WDiff *dview)
2996 gboolean res = TRUE;
2997 int act;
2999 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3000 return res;
3002 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3003 _("File(s) was modified. Save with exit?") :
3004 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3005 D_NORMAL, 2, _("&Yes"), _("&No"));
3007 /* Esc is No */
3008 if (mc_global.midnight_shutdown || (act == -1))
3009 act = 1;
3011 switch (act)
3013 case -1: /* Esc */
3014 res = FALSE;
3015 break;
3016 case 0: /* Yes */
3017 (void) dview_save (dview);
3018 res = TRUE;
3019 break;
3020 case 1: /* No */
3021 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3022 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3023 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3024 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3025 MC_FALLTHROUGH;
3026 default:
3027 res = TRUE;
3028 break;
3030 return res;
3033 /* --------------------------------------------------------------------------------------------- */
3035 static cb_ret_t
3036 dview_execute_cmd (WDiff *dview, long command)
3038 cb_ret_t res = MSG_HANDLED;
3040 switch (command)
3042 case CK_ShowSymbols:
3043 dview->display_symbols = !dview->display_symbols;
3044 dview->new_frame = TRUE;
3045 break;
3046 case CK_ShowNumbers:
3047 dview->display_numbers ^= calc_nwidth ((const GArray * const *) dview->a);
3048 dview->new_frame = TRUE;
3049 break;
3050 case CK_SplitFull:
3051 dview->full = !dview->full;
3052 dview->new_frame = TRUE;
3053 break;
3054 case CK_SplitEqual:
3055 if (!dview->full)
3057 dview->bias = 0;
3058 dview->new_frame = TRUE;
3060 break;
3061 case CK_SplitMore:
3062 if (!dview->full)
3064 dview_compute_split (dview, 1);
3065 dview->new_frame = TRUE;
3067 break;
3069 case CK_SplitLess:
3070 if (!dview->full)
3072 dview_compute_split (dview, -1);
3073 dview->new_frame = TRUE;
3075 break;
3076 case CK_Tab2:
3077 dview->tab_size = 2;
3078 break;
3079 case CK_Tab3:
3080 dview->tab_size = 3;
3081 break;
3082 case CK_Tab4:
3083 dview->tab_size = 4;
3084 break;
3085 case CK_Tab8:
3086 dview->tab_size = 8;
3087 break;
3088 case CK_Swap:
3089 dview->ord ^= 1;
3090 break;
3091 case CK_Redo:
3092 dview_redo (dview);
3093 break;
3094 case CK_HunkNext:
3095 dview->skip_rows = dview->search.last_accessed_num_line =
3096 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3097 break;
3098 case CK_HunkPrev:
3099 dview->skip_rows = dview->search.last_accessed_num_line =
3100 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3101 break;
3102 case CK_Goto:
3103 dview_goto_cmd (dview, DIFF_RIGHT);
3104 break;
3105 case CK_Edit:
3106 dview_edit (dview, dview->ord);
3107 break;
3108 case CK_Merge:
3109 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3110 dview_redo (dview);
3111 break;
3112 case CK_MergeOther:
3113 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3114 dview_redo (dview);
3115 break;
3116 case CK_EditOther:
3117 dview_edit (dview, dview->ord ^ 1);
3118 break;
3119 case CK_Search:
3120 dview_search_cmd (dview);
3121 break;
3122 case CK_SearchContinue:
3123 dview_continue_search_cmd (dview);
3124 break;
3125 case CK_Top:
3126 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3127 break;
3128 case CK_Bottom:
3129 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3130 break;
3131 case CK_Up:
3132 if (dview->skip_rows > 0)
3134 dview->skip_rows--;
3135 dview->search.last_accessed_num_line = dview->skip_rows;
3137 break;
3138 case CK_Down:
3139 dview->skip_rows++;
3140 dview->search.last_accessed_num_line = dview->skip_rows;
3141 break;
3142 case CK_PageDown:
3143 if (dview->height > 2)
3145 dview->skip_rows += dview->height - 2;
3146 dview->search.last_accessed_num_line = dview->skip_rows;
3148 break;
3149 case CK_PageUp:
3150 if (dview->height > 2)
3152 dview->skip_rows -= dview->height - 2;
3153 dview->search.last_accessed_num_line = dview->skip_rows;
3155 break;
3156 case CK_Left:
3157 dview->skip_cols--;
3158 break;
3159 case CK_Right:
3160 dview->skip_cols++;
3161 break;
3162 case CK_LeftQuick:
3163 dview->skip_cols -= 8;
3164 break;
3165 case CK_RightQuick:
3166 dview->skip_cols += 8;
3167 break;
3168 case CK_Home:
3169 dview->skip_cols = 0;
3170 break;
3171 case CK_Shell:
3172 toggle_subshell ();
3173 break;
3174 case CK_Quit:
3175 dview->view_quit = TRUE;
3176 break;
3177 case CK_Save:
3178 dview_do_save (dview);
3179 break;
3180 case CK_Options:
3181 dview_diff_options (dview);
3182 break;
3183 #ifdef HAVE_CHARSET
3184 case CK_SelectCodepage:
3185 dview_select_encoding (dview);
3186 break;
3187 #endif
3188 case CK_Cancel:
3189 /* don't close diffviewer due to SIGINT */
3190 break;
3191 default:
3192 res = MSG_NOT_HANDLED;
3194 return res;
3197 /* --------------------------------------------------------------------------------------------- */
3199 static cb_ret_t
3200 dview_handle_key (WDiff *dview, int key)
3202 long command;
3204 #ifdef HAVE_CHARSET
3205 key = convert_from_input_c (key);
3206 #endif
3208 command = widget_lookup_key (WIDGET (dview), key);
3209 if (command == CK_IgnoreKey)
3210 return MSG_NOT_HANDLED;
3212 return dview_execute_cmd (dview, command);
3215 /* --------------------------------------------------------------------------------------------- */
3217 static cb_ret_t
3218 dview_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
3220 WDiff *dview = (WDiff *) w;
3221 WDialog *h = DIALOG (w->owner);
3222 cb_ret_t i;
3224 switch (msg)
3226 case MSG_INIT:
3227 dview_labels (dview);
3228 dview_update (dview);
3229 return MSG_HANDLED;
3231 case MSG_DRAW:
3232 dview->new_frame = TRUE;
3233 dview_update (dview);
3234 return MSG_HANDLED;
3236 case MSG_KEY:
3237 i = dview_handle_key (dview, parm);
3238 if (dview->view_quit)
3239 dlg_close (h);
3240 else
3241 dview_update (dview);
3242 return i;
3244 case MSG_ACTION:
3245 i = dview_execute_cmd (dview, parm);
3246 if (dview->view_quit)
3247 dlg_close (h);
3248 else
3249 dview_update (dview);
3250 return i;
3252 case MSG_RESIZE:
3253 widget_default_callback (w, NULL, MSG_RESIZE, 0, data);
3254 dview_compute_areas (dview);
3255 return MSG_HANDLED;
3257 case MSG_DESTROY:
3258 dview_save_options (dview);
3259 dview_fini (dview);
3260 return MSG_HANDLED;
3262 default:
3263 return widget_default_callback (w, sender, msg, parm, data);
3267 /* --------------------------------------------------------------------------------------------- */
3269 static void
3270 dview_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
3272 WDiff *dview = (WDiff *) w;
3274 (void) event;
3276 switch (msg)
3278 case MSG_MOUSE_SCROLL_UP:
3279 case MSG_MOUSE_SCROLL_DOWN:
3280 if (msg == MSG_MOUSE_SCROLL_UP)
3281 dview->skip_rows -= 2;
3282 else
3283 dview->skip_rows += 2;
3285 dview->search.last_accessed_num_line = dview->skip_rows;
3286 dview_update (dview);
3287 break;
3289 default:
3290 break;
3294 /* --------------------------------------------------------------------------------------------- */
3296 static cb_ret_t
3297 dview_dialog_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
3299 WDiff *dview;
3300 WDialog *h = DIALOG (w);
3302 switch (msg)
3304 case MSG_ACTION:
3305 /* Handle shortcuts. */
3307 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3308 * here, which is why we can pass NULL in the following call. */
3309 return dview_execute_cmd (NULL, parm);
3311 case MSG_VALIDATE:
3312 dview = (WDiff *) widget_find_by_type (CONST_WIDGET (h), dview_callback);
3313 /* don't stop the dialog before final decision */
3314 widget_set_state (w, WST_ACTIVE, TRUE);
3315 if (dview_ok_to_exit (dview))
3316 dlg_close (h);
3317 return MSG_HANDLED;
3319 default:
3320 return dlg_default_callback (w, sender, msg, parm, data);
3324 /* --------------------------------------------------------------------------------------------- */
3326 static char *
3327 dview_get_title (const WDialog *h, size_t len)
3329 const WDiff *dview;
3330 const char *modified = " (*) ";
3331 const char *notmodified = " ";
3332 size_t len1;
3333 GString *title;
3335 dview = (const WDiff *) widget_find_by_type (CONST_WIDGET (h), dview_callback);
3336 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3338 title = g_string_sized_new (len);
3339 g_string_append (title, _("Diff:"));
3340 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3341 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3342 g_string_append (title, " | ");
3343 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3344 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3346 return g_string_free (title, FALSE);
3349 /* --------------------------------------------------------------------------------------------- */
3351 static int
3352 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3354 int error;
3355 WDiff *dview;
3356 Widget *w;
3357 WDialog *dview_dlg;
3358 Widget *dw;
3359 WRect r;
3360 WGroup *g;
3362 /* Create dialog and widgets, put them on the dialog */
3363 dview_dlg =
3364 dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, NULL, dview_dialog_callback, NULL,
3365 "[Diff Viewer]", NULL);
3366 dw = WIDGET (dview_dlg);
3367 widget_want_tab (dw, TRUE);
3368 r = dw->rect;
3370 g = GROUP (dview_dlg);
3372 dview = g_new0 (WDiff, 1);
3373 w = WIDGET (dview);
3374 r.lines--;
3375 widget_init (w, &r, dview_callback, dview_mouse_callback);
3376 w->options |= WOP_SELECTABLE;
3377 w->keymap = diff_map;
3378 group_add_widget_autopos (g, w, WPOS_KEEP_ALL, NULL);
3380 w = WIDGET (buttonbar_new ());
3381 group_add_widget_autopos (g, w, w->pos_flags, NULL);
3383 dview_dlg->get_title = dview_get_title;
3385 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3386 if (error >= 0)
3387 error = redo_diff (dview);
3388 if (error >= 0)
3390 dview->ndiff = error;
3391 dview_compute_areas (dview);
3392 error = 0;
3395 if (error == 0)
3396 dlg_run (dview_dlg);
3398 if (error != 0 || widget_get_state (dw, WST_CLOSED))
3399 widget_destroy (dw);
3401 return error == 0 ? 1 : 0;
3404 /*** public functions ****************************************************************************/
3405 /* --------------------------------------------------------------------------------------------- */
3407 #define GET_FILE_AND_STAMP(n) \
3408 do \
3410 use_copy##n = 0; \
3411 real_file##n = file##n; \
3412 if (!vfs_file_is_local (file##n)) \
3414 real_file##n = mc_getlocalcopy (file##n); \
3415 if (real_file##n != NULL) \
3417 use_copy##n = 1; \
3418 if (mc_stat (real_file##n, &st##n) != 0) \
3419 use_copy##n = -1; \
3423 while (0)
3425 #define UNGET_FILE(n) \
3426 do \
3428 if (use_copy##n != 0) \
3430 gboolean changed = FALSE; \
3431 if (use_copy##n > 0) \
3433 time_t mtime; \
3434 mtime = st##n.st_mtime; \
3435 if (mc_stat (real_file##n, &st##n) == 0) \
3436 changed = (mtime != st##n.st_mtime); \
3438 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3439 vfs_path_free (real_file##n, TRUE); \
3442 while (0)
3444 gboolean
3445 dview_diff_cmd (const void *f0, const void *f1)
3447 int rv = 0;
3448 vfs_path_t *file0 = NULL;
3449 vfs_path_t *file1 = NULL;
3450 gboolean is_dir0 = FALSE;
3451 gboolean is_dir1 = FALSE;
3453 switch (mc_global.mc_run_mode)
3455 case MC_RUN_FULL:
3457 /* run from panels */
3458 const WPanel *panel0 = (const WPanel *) f0;
3459 const WPanel *panel1 = (const WPanel *) f1;
3460 const file_entry_t *fe0, *fe1;
3462 fe0 = panel_current_entry (panel0);
3463 file0 = vfs_path_append_new (panel0->cwd_vpath, fe0->fname->str, (char *) NULL);
3464 is_dir0 = S_ISDIR (fe0->st.st_mode);
3465 if (is_dir0)
3467 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3468 path_trunc (fe0->fname->str, 30));
3469 goto ret;
3472 fe1 = panel_current_entry (panel1);
3473 file1 = vfs_path_append_new (panel1->cwd_vpath, fe1->fname->str, (char *) NULL);
3474 is_dir1 = S_ISDIR (fe1->st.st_mode);
3475 if (is_dir1)
3477 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3478 path_trunc (fe1->fname->str, 30));
3479 goto ret;
3481 break;
3484 case MC_RUN_DIFFVIEWER:
3486 /* run from command line */
3487 const char *p0 = (const char *) f0;
3488 const char *p1 = (const char *) f1;
3489 struct stat st;
3491 file0 = vfs_path_from_str (p0);
3492 if (mc_stat (file0, &st) == 0)
3494 is_dir0 = S_ISDIR (st.st_mode);
3495 if (is_dir0)
3497 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3498 goto ret;
3501 else
3503 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3504 path_trunc (p0, 30), unix_error_string (errno));
3505 goto ret;
3508 file1 = vfs_path_from_str (p1);
3509 if (mc_stat (file1, &st) == 0)
3511 is_dir1 = S_ISDIR (st.st_mode);
3512 if (is_dir1)
3514 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3515 goto ret;
3518 else
3520 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3521 path_trunc (p1, 30), unix_error_string (errno));
3522 goto ret;
3524 break;
3527 default:
3528 /* this should not happened */
3529 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3530 return FALSE;
3533 if (rv == 0)
3535 rv = -1;
3536 if (file0 != NULL && file1 != NULL)
3538 int use_copy0, use_copy1;
3539 struct stat st0, st1;
3540 vfs_path_t *real_file0, *real_file1;
3542 GET_FILE_AND_STAMP (0);
3543 GET_FILE_AND_STAMP (1);
3545 if (real_file0 != NULL && real_file1 != NULL)
3546 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3547 vfs_path_as_str (file0), vfs_path_as_str (file1));
3549 UNGET_FILE (1);
3550 UNGET_FILE (0);
3554 if (rv == 0)
3555 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3557 ret:
3558 vfs_path_free (file1, TRUE);
3559 vfs_path_free (file0, TRUE);
3561 return (rv != 0);
3564 #undef GET_FILE_AND_STAMP
3565 #undef UNGET_FILE
3567 /* --------------------------------------------------------------------------------------------- */