Merge branch '4292_autocomplete_slashes'
[midnight-commander.git] / src / diffviewer / ydiff.c
blobf35a8fd8314b6e82dc490e636628c823e674cebb
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 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,
808 GArray * ops)
810 static const char *opt =
811 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
812 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
813 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
814 " --unchanged-group-format=''";
816 int rv;
817 FBUF *f;
818 char *cmd;
819 int code;
820 char *file1_esc, *file2_esc;
822 /* escape potential $ to avoid shell variable substitutions in popen() */
823 file1_esc = str_shell_escape (file1);
824 file2_esc = str_shell_escape (file2);
825 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
826 g_free (file1_esc);
827 g_free (file2_esc);
829 if (cmd == NULL)
830 return -1;
832 f = dview_popen (cmd, O_RDONLY);
833 g_free (cmd);
835 if (f == NULL)
836 return -1;
838 rv = scan_diff (f, ops);
839 code = dview_pclose (f);
841 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
842 rv = -1;
844 return rv;
847 /* --------------------------------------------------------------------------------------------- */
849 static gboolean
850 printer_for (char ch, DFUNC printer, void *ctx, FBUF * f, int *line, off_t * off)
852 size_t sz;
853 char buf[BUFSIZ];
855 sz = dview_fgets (buf, sizeof (buf), f);
856 if (sz == 0)
857 return FALSE;
859 (*line)++;
860 printer (ctx, ch, *line, *off, sz, buf);
861 *off += sz;
863 while (buf[sz - 1] != '\n')
865 sz = dview_fgets (buf, sizeof (buf), f);
866 if (sz == 0)
868 printer (ctx, 0, 0, 0, 1, "\n");
869 break;
872 printer (ctx, 0, 0, 0, sz, buf);
873 *off += sz;
876 return TRUE;
879 /* --------------------------------------------------------------------------------------------- */
882 * Reparse and display file according to diff statements.
884 * @param ord DIFF_LEFT if 1st file is displayed , DIFF_RIGHT if 2nd file is displayed.
885 * @param filename file name to display
886 * @param ops list of diff statements
887 * @param printer printf-like function to be used for displaying
888 * @param ctx printer context
890 * @return 0 if success, otherwise non-zero
893 static int
894 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
896 size_t i;
897 FBUF *f;
898 int line = 0;
899 off_t off = 0;
900 const DIFFCMD *op;
901 diff_place_t eff;
902 int add_cmd, del_cmd;
904 f = dview_fopen (filename, O_RDONLY);
905 if (f == NULL)
906 return -1;
908 if (ord != DIFF_LEFT)
909 ord = DIFF_RIGHT;
910 eff = ord;
912 if (ord != DIFF_LEFT)
914 add_cmd = 'd';
915 del_cmd = 'a';
917 else
919 add_cmd = 'a';
920 del_cmd = 'd';
922 #define F1 a[eff][0]
923 #define F2 a[eff][1]
924 #define T1 a[ ord^1 ][0]
925 #define T2 a[ ord^1 ][1]
926 for (i = 0; i < ops->len; i++)
928 int n;
930 op = &g_array_index (ops, DIFFCMD, i);
932 n = op->F1;
933 if (op->cmd != add_cmd)
934 n--;
936 while (line < n && printer_for (EQU_CH, printer, ctx, f, &line, &off))
939 if (line != n)
940 goto err;
942 if (op->cmd == add_cmd)
943 for (n = op->T2 - op->T1 + 1; n != 0; n--)
944 printer (ctx, DEL_CH, 0, 0, 1, "\n");
946 if (op->cmd == del_cmd)
948 for (n = op->F2 - op->F1 + 1;
949 n != 0 && printer_for (ADD_CH, printer, ctx, f, &line, &off); n--)
952 if (n != 0)
953 goto err;
956 if (op->cmd == 'c')
958 for (n = op->F2 - op->F1 + 1;
959 n != 0 && printer_for (CHG_CH, printer, ctx, f, &line, &off); n--)
962 if (n != 0)
963 goto err;
965 for (n = op->T2 - op->T1 - (op->F2 - op->F1); n > 0; n--)
966 printer (ctx, CHG_CH, 0, 0, 1, "\n");
969 #undef T2
970 #undef T1
971 #undef F2
972 #undef F1
974 while (printer_for (EQU_CH, printer, ctx, f, &line, &off))
977 dview_fclose (f);
978 return 0;
980 err:
981 dview_fclose (f);
982 return -1;
985 /* --------------------------------------------------------------------------------------------- */
987 /* horizontal diff ********************************************************** */
990 * Longest common substring.
992 * @param s first string
993 * @param m length of first string
994 * @param t second string
995 * @param n length of second string
996 * @param ret list of offsets for longest common substrings inside each string
997 * @param min minimum length of common substrings
999 * @return 0 if success, nonzero otherwise
1002 static int
1003 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1005 int i, j;
1006 int *Lprev, *Lcurr;
1007 int z = 0;
1009 if (m < min || n < min)
1011 /* XXX early culling */
1012 return 0;
1015 Lprev = g_try_new0 (int, n + 1);
1016 if (Lprev == NULL)
1017 return -1;
1019 Lcurr = g_try_new0 (int, n + 1);
1020 if (Lcurr == NULL)
1022 g_free (Lprev);
1023 return -1;
1026 for (i = 0; i < m; i++)
1028 int *L;
1030 L = Lprev;
1031 Lprev = Lcurr;
1032 Lcurr = L;
1033 #ifdef USE_MEMSET_IN_LCS
1034 memset (Lcurr, 0, (n + 1) * sizeof (*Lcurr));
1035 #endif
1036 for (j = 0; j < n; j++)
1038 #ifndef USE_MEMSET_IN_LCS
1039 Lcurr[j + 1] = 0;
1040 #endif
1041 if (s[i] == t[j])
1043 int v;
1045 v = Lprev[j] + 1;
1046 Lcurr[j + 1] = v;
1047 if (z < v)
1049 z = v;
1050 g_array_set_size (ret, 0);
1052 if (z == v && z >= min)
1054 int off0, off1;
1055 size_t k;
1057 off0 = i - z + 1;
1058 off1 = j - z + 1;
1060 for (k = 0; k < ret->len; k++)
1062 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1063 if ((*p)[0] == off0 || (*p)[1] >= off1)
1064 break;
1066 if (k == ret->len)
1068 PAIR p2;
1070 p2[0] = off0;
1071 p2[1] = off1;
1072 g_array_append_val (ret, p2);
1079 g_free (Lcurr);
1080 g_free (Lprev);
1081 return z;
1084 /* --------------------------------------------------------------------------------------------- */
1087 * Scan recursively for common substrings and build ranges.
1089 * @param s first string
1090 * @param t second string
1091 * @param bracket current limits for both of the strings
1092 * @param min minimum length of common substrings
1093 * @param hdiff list of horizontal diff ranges to fill
1094 * @param depth recursion depth
1096 * @return 0 if success, nonzero otherwise
1099 static gboolean
1100 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1101 unsigned int depth)
1103 BRACKET p;
1105 if (depth-- != 0)
1107 GArray *ret;
1108 BRACKET b;
1109 int len;
1111 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1113 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1114 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1115 if (ret->len != 0)
1117 size_t k = 0;
1118 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1119 const PAIR *data2;
1121 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1122 b[DIFF_LEFT].len = (*data)[0];
1123 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1124 b[DIFF_RIGHT].len = (*data)[1];
1125 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1126 return FALSE;
1128 for (k = 0; k < ret->len - 1; k++)
1130 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1131 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1132 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1133 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1134 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1135 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1136 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1137 return FALSE;
1139 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1140 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1141 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1142 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1143 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1144 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1145 return FALSE;
1147 g_array_free (ret, TRUE);
1148 return TRUE;
1152 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1153 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1154 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1155 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1156 g_array_append_val (hdiff, p);
1158 return TRUE;
1161 /* --------------------------------------------------------------------------------------------- */
1164 * Build list of horizontal diff ranges.
1166 * @param s first string
1167 * @param m length of first string
1168 * @param t second string
1169 * @param n length of second string
1170 * @param min minimum length of common substrings
1171 * @param hdiff list of horizontal diff ranges to fill
1172 * @param depth recursion depth
1174 * @return 0 if success, nonzero otherwise
1177 static gboolean
1178 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1180 int i;
1181 BRACKET b;
1183 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1184 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1186 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1189 b[DIFF_LEFT].off = i;
1190 b[DIFF_LEFT].len = m - i;
1191 b[DIFF_RIGHT].off = i;
1192 b[DIFF_RIGHT].len = n - i;
1194 /* smartscan (multiple horizontal diff) */
1195 return hdiff_multi (s, t, b, min, hdiff, depth);
1198 /* --------------------------------------------------------------------------------------------- */
1200 /* read line **************************************************************** */
1203 * Check if character is inside horizontal diff limits.
1205 * @param k rank of character inside line
1206 * @param hdiff horizontal diff structure
1207 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1209 * @return TRUE if inside hdiff limits, FALSE otherwise
1212 static gboolean
1213 is_inside (int k, GArray * hdiff, diff_place_t ord)
1215 size_t i;
1216 BRACKET *b;
1218 for (i = 0; i < hdiff->len; i++)
1220 int start, end;
1222 b = &g_array_index (hdiff, BRACKET, i);
1223 start = (*b)[ord].off;
1224 end = start + (*b)[ord].len;
1225 if (k >= start && k < end)
1226 return TRUE;
1228 return FALSE;
1231 /* --------------------------------------------------------------------------------------------- */
1234 * Copy 'src' to 'dst' expanding tabs.
1235 * @note The procedure returns when all bytes are consumed from 'src'
1237 * @param dst destination buffer
1238 * @param src source buffer
1239 * @param srcsize size of src buffer
1240 * @param base virtual base of this string, needed to calculate tabs
1241 * @param ts tab size
1243 * @return new virtual base
1246 static int
1247 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1249 int i;
1251 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1253 *dst = *src;
1254 if (*src == '\t')
1256 int j;
1258 j = TAB_SKIP (ts, i + base);
1259 i += j - 1;
1260 fill_by_space (dst, j, FALSE);
1261 dst += j - 1;
1264 return i + base;
1267 /* --------------------------------------------------------------------------------------------- */
1270 * Copy 'src' to 'dst' expanding tabs.
1272 * @param dst destination buffer
1273 * @param dstsize size of dst buffer
1274 * @param[in,out] _src source buffer
1275 * @param srcsize size of src buffer
1276 * @param base virtual base of this string, needed to calculate tabs
1277 * @param ts tab size
1279 * @return new virtual base
1281 * @note The procedure returns when all bytes are consumed from 'src'
1282 * or 'dstsize' bytes are written to 'dst'
1283 * @note Upon return, 'src' points to the first unwritten character in source
1286 static int
1287 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1289 int i;
1290 const char *src = *_src;
1292 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1294 *dst = *src;
1295 if (*src == '\t')
1297 int j;
1299 j = TAB_SKIP (ts, i + base);
1300 if (j > dstsize - i)
1301 j = dstsize - i;
1302 i += j - 1;
1303 fill_by_space (dst, j, FALSE);
1304 dst += j - 1;
1307 *_src = src;
1308 return i + base;
1311 /* --------------------------------------------------------------------------------------------- */
1314 * Read line from memory, converting tabs to spaces and padding with spaces.
1316 * @param src buffer to read from
1317 * @param srcsize size of src buffer
1318 * @param dst buffer to read to
1319 * @param dstsize size of dst buffer, excluding trailing null
1320 * @param skip number of characters to skip
1321 * @param ts tab size
1322 * @param show_cr show trailing carriage return as ^M
1324 * @return negative on error, otherwise number of bytes except padding
1327 static int
1328 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1329 gboolean show_cr)
1331 int sz = 0;
1333 if (src != NULL)
1335 int i;
1336 char *tmp = dst;
1337 const int base = 0;
1339 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1341 if (*src == '\t')
1343 int j;
1345 j = TAB_SKIP (ts, i + base);
1346 i += j - 1;
1347 while (j-- > 0)
1349 if (skip > 0)
1350 skip--;
1351 else if (dstsize != 0)
1353 dstsize--;
1354 *dst++ = ' ';
1358 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1360 if (skip == 0 && show_cr)
1362 if (dstsize > 1)
1364 dstsize -= 2;
1365 *dst++ = '^';
1366 *dst++ = 'M';
1368 else
1370 dstsize--;
1371 *dst++ = '.';
1374 break;
1376 else if (skip > 0)
1378 #ifdef HAVE_CHARSET
1379 int ch = 0;
1380 int ch_length = 1;
1382 (void) dview_get_utf (src, &ch, &ch_length);
1384 if (ch_length > 1)
1385 skip += ch_length - 1;
1386 #endif
1388 skip--;
1390 else
1392 dstsize--;
1393 *dst++ = *src;
1396 sz = dst - tmp;
1399 fill_by_space (dst, dstsize, TRUE);
1401 return sz;
1404 /* --------------------------------------------------------------------------------------------- */
1407 * Read line from memory and build attribute array.
1409 * @param src buffer to read from
1410 * @param srcsize size of src buffer
1411 * @param dst buffer to read to
1412 * @param dstsize size of dst buffer, excluding trailing null
1413 * @param skip number of characters to skip
1414 * @param ts tab size
1415 * @param show_cr show trailing carriage return as ^M
1416 * @param hdiff horizontal diff structure
1417 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1418 * @param att buffer of attributes
1420 * @return negative on error, otherwise number of bytes except padding
1423 static int
1424 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1425 gboolean show_cr, GArray * hdiff, diff_place_t ord, char *att)
1427 int sz = 0;
1429 if (src != NULL)
1431 int i, k;
1432 char *tmp = dst;
1433 const int base = 0;
1435 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1437 if (*src == '\t')
1439 int j;
1441 j = TAB_SKIP (ts, i + base);
1442 i += j - 1;
1443 while (j-- > 0)
1445 if (skip != 0)
1446 skip--;
1447 else if (dstsize != 0)
1449 dstsize--;
1450 *att++ = is_inside (k, hdiff, ord);
1451 *dst++ = ' ';
1455 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1457 if (skip == 0 && show_cr)
1459 if (dstsize > 1)
1461 dstsize -= 2;
1462 *att++ = is_inside (k, hdiff, ord);
1463 *dst++ = '^';
1464 *att++ = is_inside (k, hdiff, ord);
1465 *dst++ = 'M';
1467 else
1469 dstsize--;
1470 *att++ = is_inside (k, hdiff, ord);
1471 *dst++ = '.';
1474 break;
1476 else if (skip != 0)
1478 #ifdef HAVE_CHARSET
1479 int ch = 0;
1480 int ch_length = 1;
1482 (void) dview_get_utf (src, &ch, &ch_length);
1483 if (ch_length > 1)
1484 skip += ch_length - 1;
1485 #endif
1487 skip--;
1489 else
1491 dstsize--;
1492 *att++ = is_inside (k, hdiff, ord);
1493 *dst++ = *src;
1496 sz = dst - tmp;
1499 memset (att, '\0', dstsize);
1500 fill_by_space (dst, dstsize, TRUE);
1502 return sz;
1505 /* --------------------------------------------------------------------------------------------- */
1508 * Read line from file, converting tabs to spaces and padding with spaces.
1510 * @param f file stream to read from
1511 * @param off offset of line inside file
1512 * @param dst buffer to read to
1513 * @param dstsize size of dst buffer, excluding trailing null
1514 * @param skip number of characters to skip
1515 * @param ts tab size
1516 * @param show_cr show trailing carriage return as ^M
1518 * @return negative on error, otherwise number of bytes except padding
1521 static int
1522 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, gboolean show_cr)
1524 int base = 0;
1525 int old_base = base;
1526 size_t amount = dstsize;
1527 size_t useful, offset;
1528 size_t i;
1529 size_t sz;
1530 int lastch = '\0';
1531 const char *q = NULL;
1532 char tmp[BUFSIZ]; /* XXX capacity must be >= MAX{dstsize + 1, amount} */
1533 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1535 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1537 /* abnormal, but avoid buffer overflow */
1538 fill_by_space (dst, dstsize, TRUE);
1539 return 0;
1542 dview_fseek (f, off, SEEK_SET);
1544 while (skip > base)
1546 old_base = base;
1547 sz = dview_fgets (tmp, amount, f);
1548 if (sz == 0)
1549 break;
1551 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1552 if (cvt[base - old_base - 1] == '\n')
1554 q = &cvt[base - old_base - 1];
1555 base = old_base + q - cvt + 1;
1556 break;
1560 if (base < skip)
1562 fill_by_space (dst, dstsize, TRUE);
1563 return 0;
1566 useful = base - skip;
1567 offset = skip - old_base;
1569 if (useful <= dstsize)
1571 if (useful != 0)
1572 memmove (dst, cvt + offset, useful);
1574 if (q == NULL)
1576 sz = dview_fgets (tmp, dstsize - useful + 1, f);
1577 if (sz != 0)
1579 const char *ptr = tmp;
1581 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1582 if (ptr < tmp + sz)
1583 lastch = *ptr;
1586 sz = useful;
1588 else
1590 memmove (dst, cvt + offset, dstsize);
1591 sz = dstsize;
1592 lastch = cvt[offset + dstsize];
1595 dst[sz] = lastch;
1596 for (i = 0; i < sz && dst[i] != '\n'; i++)
1597 if (dst[i] == '\r' && dst[i + 1] == '\n')
1599 if (show_cr)
1601 if (i + 1 < dstsize)
1603 dst[i++] = '^';
1604 dst[i++] = 'M';
1606 else
1607 dst[i++] = '*';
1609 break;
1612 fill_by_space (dst, dstsize, TRUE);
1614 return sz;
1617 /* --------------------------------------------------------------------------------------------- */
1618 /* diff printers et al ****************************************************** */
1620 static void
1621 cc_free_elt (gpointer elt)
1623 DIFFLN *p = (DIFFLN *) elt;
1625 if (p != NULL)
1626 g_free (p->p);
1629 /* --------------------------------------------------------------------------------------------- */
1631 static int
1632 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1634 GArray *a = ((PRINTER_CTX *) ctx)->a;
1635 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1637 if (ch != 0)
1639 DIFFLN p;
1641 p.p = NULL;
1642 p.ch = ch;
1643 p.line = line;
1644 p.u.off = off;
1645 if (dsrc == DATA_SRC_MEM && line != 0)
1647 if (sz != 0 && str[sz - 1] == '\n')
1648 sz--;
1649 if (sz > 0)
1650 p.p = g_strndup (str, sz);
1651 p.u.len = sz;
1653 g_array_append_val (a, p);
1655 else if (dsrc == DATA_SRC_MEM)
1657 DIFFLN *p;
1659 p = &g_array_index (a, DIFFLN, a->len - 1);
1660 if (sz != 0 && str[sz - 1] == '\n')
1661 sz--;
1662 if (sz != 0)
1664 size_t new_size;
1665 char *q;
1667 new_size = p->u.len + sz;
1668 q = g_realloc (p->p, new_size);
1669 memcpy (q + p->u.len, str, sz);
1670 p->p = q;
1672 p->u.len += sz;
1674 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1676 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1677 dview_fwrite (f, str, sz);
1679 return 0;
1682 /* --------------------------------------------------------------------------------------------- */
1684 static int
1685 redo_diff (WDiff * dview)
1687 FBUF *const *f = dview->f;
1688 PRINTER_CTX ctx;
1689 GArray *ops;
1690 int ndiff;
1691 int rv = 0;
1692 char extra[BUF_MEDIUM];
1694 extra[0] = '\0';
1695 if (dview->opt.quality == 2)
1696 strcat (extra, " -d");
1697 if (dview->opt.quality == 1)
1698 strcat (extra, " --speed-large-files");
1699 if (dview->opt.strip_trailing_cr)
1700 strcat (extra, " --strip-trailing-cr");
1701 if (dview->opt.ignore_tab_expansion)
1702 strcat (extra, " -E");
1703 if (dview->opt.ignore_space_change)
1704 strcat (extra, " -b");
1705 if (dview->opt.ignore_all_space)
1706 strcat (extra, " -w");
1707 if (dview->opt.ignore_case)
1708 strcat (extra, " -i");
1710 if (dview->dsrc != DATA_SRC_MEM)
1712 dview_freset (f[DIFF_LEFT]);
1713 dview_freset (f[DIFF_RIGHT]);
1716 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1717 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1718 if (ndiff < 0)
1720 if (ops != NULL)
1721 g_array_free (ops, TRUE);
1722 return -1;
1725 ctx.dsrc = dview->dsrc;
1726 ctx.a = dview->a[DIFF_LEFT];
1727 ctx.f = f[DIFF_LEFT];
1728 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1730 ctx.a = dview->a[DIFF_RIGHT];
1731 ctx.f = f[DIFF_RIGHT];
1732 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1734 if (ops != NULL)
1735 g_array_free (ops, TRUE);
1737 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1738 return -1;
1740 if (dview->dsrc == DATA_SRC_TMP)
1742 dview_ftrunc (f[DIFF_LEFT]);
1743 dview_ftrunc (f[DIFF_RIGHT]);
1746 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1748 size_t i;
1750 dview->hdiff = g_ptr_array_new ();
1752 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1754 GArray *h = NULL;
1755 const DIFFLN *p;
1756 const DIFFLN *q;
1758 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1759 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1760 if (p->line != 0 && q->line != 0 && p->ch == CHG_CH)
1762 gboolean runresult;
1764 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1766 runresult =
1767 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h, HDIFF_DEPTH);
1768 if (!runresult)
1770 g_array_free (h, TRUE);
1771 h = NULL;
1775 g_ptr_array_add (dview->hdiff, h);
1778 return ndiff;
1781 /* --------------------------------------------------------------------------------------------- */
1783 static void
1784 destroy_hdiff (WDiff * dview)
1786 if (dview->hdiff != NULL)
1788 int i;
1789 int len;
1791 len = dview->a[DIFF_LEFT]->len;
1793 for (i = 0; i < len; i++)
1795 GArray *h;
1797 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1798 if (h != NULL)
1799 g_array_free (h, TRUE);
1801 g_ptr_array_free (dview->hdiff, TRUE);
1802 dview->hdiff = NULL;
1805 mc_search_free (dview->search.handle);
1806 dview->search.handle = NULL;
1807 MC_PTR_FREE (dview->search.last_string);
1810 /* --------------------------------------------------------------------------------------------- */
1811 /* stuff ******************************************************************** */
1813 static int
1814 get_digits (unsigned int n)
1816 int d = 1;
1818 while ((n /= 10) != 0)
1819 d++;
1820 return d;
1823 /* --------------------------------------------------------------------------------------------- */
1825 static int
1826 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1828 const DIFFLN *p;
1830 *linenum = 0;
1831 *lineofs = 0;
1833 if (a->len != 0)
1835 if (pos >= a->len)
1836 pos = a->len - 1;
1838 p = &g_array_index (a, DIFFLN, pos);
1840 if (p->line == 0)
1842 int n;
1844 for (n = pos; n > 0; n--)
1846 p--;
1847 if (p->line != 0)
1848 break;
1850 *lineofs = pos - n + 1;
1853 *linenum = p->line;
1855 return 0;
1858 /* --------------------------------------------------------------------------------------------- */
1860 static int
1861 calc_nwidth (const GArray * const *a)
1863 int l1, o1;
1864 int l2, o2;
1866 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1867 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1868 if (l1 < l2)
1869 l1 = l2;
1870 return get_digits (l1);
1873 /* --------------------------------------------------------------------------------------------- */
1875 static int
1876 find_prev_hunk (const GArray * a, int pos)
1878 #if 1
1879 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos--)
1881 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH; pos--)
1883 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos--)
1885 if (pos > 0 && (size_t) pos < a->len)
1886 pos++;
1887 #else
1888 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH; pos--)
1890 for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH; pos--)
1892 #endif
1894 return pos;
1897 /* --------------------------------------------------------------------------------------------- */
1899 static size_t
1900 find_next_hunk (const GArray * a, size_t pos)
1902 for (; pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos++)
1904 for (; pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH; pos++)
1906 return pos;
1909 /* --------------------------------------------------------------------------------------------- */
1911 * Find start and end lines of the current hunk.
1913 * @param dview WDiff widget
1914 * @return boolean and
1915 * start_line1 first line of current hunk (file[0])
1916 * end_line1 last line of current hunk (file[0])
1917 * start_line1 first line of current hunk (file[0])
1918 * end_line1 last line of current hunk (file[0])
1921 static int
1922 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1924 const GArray *a0 = dview->a[DIFF_LEFT];
1925 const GArray *a1 = dview->a[DIFF_RIGHT];
1926 size_t pos;
1927 int ch;
1928 int res = 0;
1930 *start_line1 = 1;
1931 *start_line2 = 1;
1932 *end_line1 = 1;
1933 *end_line2 = 1;
1935 pos = dview->skip_rows;
1936 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1937 if (ch != EQU_CH)
1939 switch (ch)
1941 case ADD_CH:
1942 res = DIFF_DEL;
1943 break;
1944 case DEL_CH:
1945 res = DIFF_ADD;
1946 break;
1947 case CHG_CH:
1948 res = DIFF_CHG;
1949 break;
1950 default:
1951 break;
1954 for (; pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH; pos--)
1956 if (pos > 0)
1958 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
1959 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
1962 for (pos = dview->skip_rows;
1963 pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH; pos++)
1965 int l0, l1;
1967 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
1968 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
1969 if (l0 > 0)
1970 *end_line1 = MAX (*start_line1, l0);
1971 if (l1 > 0)
1972 *end_line2 = MAX (*start_line2, l1);
1975 return res;
1978 /* --------------------------------------------------------------------------------------------- */
1980 * Remove hunk from file.
1982 * @param dview WDiff widget
1983 * @param merge_file file stream for writing data
1984 * @param from1 first line of hunk
1985 * @param to1 last line of hunk
1986 * @param merge_direction in what direction files should be merged
1989 static void
1990 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
1991 action_direction_t merge_direction)
1993 int line;
1994 char buf[BUF_10K];
1995 FILE *f0;
1997 if (merge_direction == FROM_RIGHT_TO_LEFT)
1998 f0 = fopen (dview->file[DIFF_RIGHT], "r");
1999 else
2000 f0 = fopen (dview->file[DIFF_LEFT], "r");
2002 for (line = 0; fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1; line++)
2003 fputs (buf, merge_file);
2005 while (fgets (buf, sizeof (buf), f0) != NULL)
2007 line++;
2008 if (line >= to1)
2009 fputs (buf, merge_file);
2011 fclose (f0);
2014 /* --------------------------------------------------------------------------------------------- */
2016 * Add hunk to file.
2018 * @param dview WDiff widget
2019 * @param merge_file file stream for writing data
2020 * @param from1 first line of source hunk
2021 * @param from2 first line of destination hunk
2022 * @param to1 last line of source hunk
2023 * @param merge_direction in what direction files should be merged
2026 static void
2027 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2028 action_direction_t merge_direction)
2030 int line;
2031 char buf[BUF_10K];
2032 FILE *f0, *f1;
2034 if (merge_direction == FROM_RIGHT_TO_LEFT)
2036 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2037 f1 = fopen (dview->file[DIFF_LEFT], "r");
2039 else
2041 f0 = fopen (dview->file[DIFF_LEFT], "r");
2042 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2045 for (line = 0; fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1; line++)
2046 fputs (buf, merge_file);
2047 for (line = 0; fgets (buf, sizeof (buf), f1) != NULL && line <= to2;)
2049 line++;
2050 if (line >= from2)
2051 fputs (buf, merge_file);
2053 while (fgets (buf, sizeof (buf), f0) != NULL)
2054 fputs (buf, merge_file);
2056 fclose (f0);
2057 fclose (f1);
2060 /* --------------------------------------------------------------------------------------------- */
2062 * Replace hunk in file.
2064 * @param dview WDiff widget
2065 * @param merge_file file stream for writing data
2066 * @param from1 first line of source hunk
2067 * @param to1 last line of source hunk
2068 * @param from2 first line of destination hunk
2069 * @param to2 last line of destination hunk
2070 * @param merge_direction in what direction files should be merged
2073 static void
2074 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2075 action_direction_t merge_direction)
2077 int line1, line2;
2078 char buf[BUF_10K];
2079 FILE *f0, *f1;
2081 if (merge_direction == FROM_RIGHT_TO_LEFT)
2083 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2084 f1 = fopen (dview->file[DIFF_LEFT], "r");
2086 else
2088 f0 = fopen (dview->file[DIFF_LEFT], "r");
2089 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2092 for (line1 = 0; fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1; line1++)
2093 fputs (buf, merge_file);
2094 for (line2 = 0; fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2;)
2096 line2++;
2097 if (line2 >= from2)
2098 fputs (buf, merge_file);
2100 while (fgets (buf, sizeof (buf), f0) != NULL)
2102 line1++;
2103 if (line1 > to1)
2104 fputs (buf, merge_file);
2106 fclose (f0);
2107 fclose (f1);
2110 /* --------------------------------------------------------------------------------------------- */
2112 * Merge hunk.
2114 * @param dview WDiff widget
2115 * @param merge_direction in what direction files should be merged
2118 static void
2119 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2121 int from1, to1, from2, to2;
2122 int hunk;
2123 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2125 if (merge_direction == FROM_RIGHT_TO_LEFT)
2126 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2127 else
2128 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2130 if (hunk > 0)
2132 int merge_file_fd;
2133 FILE *merge_file;
2134 vfs_path_t *merge_file_name_vpath = NULL;
2136 if (!dview->merged[n_merge])
2138 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2139 if (!dview->merged[n_merge])
2141 message (D_ERROR, MSG_ERROR,
2142 _("Cannot create backup file\n%s%s\n%s"),
2143 dview->file[n_merge], "~~~", unix_error_string (errno));
2144 return;
2148 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2149 if (merge_file_fd == -1)
2151 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2152 unix_error_string (errno));
2153 return;
2156 merge_file = fdopen (merge_file_fd, "w");
2158 switch (hunk)
2160 case DIFF_DEL:
2161 if (merge_direction == FROM_RIGHT_TO_LEFT)
2162 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2163 else
2164 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2165 break;
2166 case DIFF_ADD:
2167 if (merge_direction == FROM_RIGHT_TO_LEFT)
2168 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2169 else
2170 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2171 break;
2172 case DIFF_CHG:
2173 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2174 break;
2175 default:
2176 break;
2178 fflush (merge_file);
2179 fclose (merge_file);
2181 int res;
2183 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2184 (void) res;
2186 mc_unlink (merge_file_name_vpath);
2187 vfs_path_free (merge_file_name_vpath, TRUE);
2191 /* --------------------------------------------------------------------------------------------- */
2192 /* view routines and callbacks ********************************************** */
2194 static void
2195 dview_compute_split (WDiff * dview, int i)
2197 dview->bias += i;
2198 if (dview->bias < 2 - dview->half1)
2199 dview->bias = 2 - dview->half1;
2200 if (dview->bias > dview->half2 - 2)
2201 dview->bias = dview->half2 - 2;
2204 /* --------------------------------------------------------------------------------------------- */
2206 static void
2207 dview_compute_areas (WDiff * dview)
2209 Widget *w = WIDGET (dview);
2211 dview->height = w->rect.lines - 1;
2212 dview->half1 = w->rect.cols / 2;
2213 dview->half2 = w->rect.cols - dview->half1;
2215 dview_compute_split (dview, 0);
2218 /* --------------------------------------------------------------------------------------------- */
2220 static void
2221 dview_reread (WDiff * dview)
2223 int ndiff;
2225 destroy_hdiff (dview);
2226 if (dview->a[DIFF_LEFT] != NULL)
2227 g_array_free (dview->a[DIFF_LEFT], TRUE);
2228 if (dview->a[DIFF_RIGHT] != NULL)
2229 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2231 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2232 g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2233 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2234 g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2236 ndiff = redo_diff (dview);
2237 if (ndiff >= 0)
2238 dview->ndiff = ndiff;
2241 /* --------------------------------------------------------------------------------------------- */
2243 #ifdef HAVE_CHARSET
2244 static void
2245 dview_set_codeset (WDiff * dview)
2247 const char *encoding_id = NULL;
2249 dview->utf8 = TRUE;
2250 encoding_id =
2251 get_codepage_id (mc_global.source_codepage >=
2252 0 ? mc_global.source_codepage : mc_global.display_codepage);
2253 if (encoding_id != NULL)
2255 GIConv conv;
2257 conv = str_crt_conv_from (encoding_id);
2258 if (conv != INVALID_CONV)
2260 if (dview->converter != str_cnv_from_term)
2261 str_close_conv (dview->converter);
2262 dview->converter = conv;
2264 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2268 /* --------------------------------------------------------------------------------------------- */
2270 static void
2271 dview_select_encoding (WDiff * dview)
2273 if (do_select_codepage ())
2274 dview_set_codeset (dview);
2275 dview_reread (dview);
2276 tty_touch_screen ();
2277 repaint_screen ();
2279 #endif /* HAVE_CHARSET */
2281 /* --------------------------------------------------------------------------------------------- */
2283 static void
2284 dview_load_options (WDiff * dview)
2286 gboolean show_numbers;
2287 int tab_size;
2289 dview->display_symbols =
2290 mc_config_get_bool (mc_global.main_config, "DiffView", "show_symbols", FALSE);
2291 show_numbers = mc_config_get_bool (mc_global.main_config, "DiffView", "show_numbers", FALSE);
2292 if (show_numbers)
2293 dview->display_numbers = 1;
2294 tab_size = mc_config_get_int (mc_global.main_config, "DiffView", "tab_size", 8);
2295 if (tab_size > 0 && tab_size < 9)
2296 dview->tab_size = tab_size;
2297 else
2298 dview->tab_size = 8;
2300 dview->opt.quality = mc_config_get_int (mc_global.main_config, "DiffView", "diff_quality", 0);
2302 dview->opt.strip_trailing_cr =
2303 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_tws", FALSE);
2304 dview->opt.ignore_all_space =
2305 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space", FALSE);
2306 dview->opt.ignore_space_change =
2307 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change", FALSE);
2308 dview->opt.ignore_tab_expansion =
2309 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_tab_expansion", FALSE);
2310 dview->opt.ignore_case =
2311 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_case", FALSE);
2313 dview->new_frame = TRUE;
2316 /* --------------------------------------------------------------------------------------------- */
2318 static void
2319 dview_save_options (WDiff * dview)
2321 mc_config_set_bool (mc_global.main_config, "DiffView", "show_symbols", dview->display_symbols);
2322 mc_config_set_bool (mc_global.main_config, "DiffView", "show_numbers",
2323 dview->display_numbers != 0);
2324 mc_config_set_int (mc_global.main_config, "DiffView", "tab_size", dview->tab_size);
2326 mc_config_set_int (mc_global.main_config, "DiffView", "diff_quality", dview->opt.quality);
2328 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_tws",
2329 dview->opt.strip_trailing_cr);
2330 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space",
2331 dview->opt.ignore_all_space);
2332 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change",
2333 dview->opt.ignore_space_change);
2334 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_tab_expansion",
2335 dview->opt.ignore_tab_expansion);
2336 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_case",
2337 dview->opt.ignore_case);
2340 /* --------------------------------------------------------------------------------------------- */
2342 static void
2343 dview_diff_options (WDiff * dview)
2345 const char *quality_str[] = {
2346 N_("No&rmal"),
2347 N_("&Fastest (Assume large files)"),
2348 N_("&Minimal (Find a smaller set of change)")
2351 quick_widget_t quick_widgets[] = {
2352 /* *INDENT-OFF* */
2353 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2354 QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2355 QUICK_STOP_GROUPBOX,
2356 QUICK_START_GROUPBOX (N_("Diff extra options")),
2357 QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2358 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2359 QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2360 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2361 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2362 NULL),
2363 QUICK_STOP_GROUPBOX,
2364 QUICK_BUTTONS_OK_CANCEL,
2365 QUICK_END
2366 /* *INDENT-ON* */
2369 WRect r = { -1, -1, 0, 56 };
2371 quick_dialog_t qdlg = {
2372 r, N_("Diff Options"), "[Diff Options]",
2373 quick_widgets, NULL, NULL
2376 if (quick_dialog (&qdlg) != B_CANCEL)
2377 dview_reread (dview);
2380 /* --------------------------------------------------------------------------------------------- */
2382 static int
2383 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2384 const char *label1, const char *label2, DSRC dsrc)
2386 FBUF *f[DIFF_COUNT];
2388 f[DIFF_LEFT] = NULL;
2389 f[DIFF_RIGHT] = NULL;
2391 if (dsrc == DATA_SRC_TMP)
2393 f[DIFF_LEFT] = dview_ftemp ();
2394 if (f[DIFF_LEFT] == NULL)
2395 return -1;
2397 f[DIFF_RIGHT] = dview_ftemp ();
2398 if (f[DIFF_RIGHT] == NULL)
2400 dview_fclose (f[DIFF_LEFT]);
2401 return -1;
2404 else if (dsrc == DATA_SRC_ORG)
2406 f[DIFF_LEFT] = dview_fopen (file1, O_RDONLY);
2407 if (f[DIFF_LEFT] == NULL)
2408 return -1;
2410 f[DIFF_RIGHT] = dview_fopen (file2, O_RDONLY);
2411 if (f[DIFF_RIGHT] == NULL)
2413 dview_fclose (f[DIFF_LEFT]);
2414 return -1;
2418 dview->view_quit = FALSE;
2420 dview->bias = 0;
2421 dview->new_frame = TRUE;
2422 dview->skip_rows = 0;
2423 dview->skip_cols = 0;
2424 dview->display_symbols = FALSE;
2425 dview->display_numbers = 0;
2426 dview->show_cr = TRUE;
2427 dview->tab_size = 8;
2428 dview->ord = DIFF_LEFT;
2429 dview->full = FALSE;
2431 dview->search.handle = NULL;
2432 dview->search.last_string = NULL;
2433 dview->search.last_found_line = -1;
2434 dview->search.last_accessed_num_line = -1;
2436 dview_load_options (dview);
2438 dview->args = args;
2439 dview->file[DIFF_LEFT] = file1;
2440 dview->file[DIFF_RIGHT] = file2;
2441 dview->label[DIFF_LEFT] = g_strdup (label1);
2442 dview->label[DIFF_RIGHT] = g_strdup (label2);
2443 dview->f[DIFF_LEFT] = f[0];
2444 dview->f[DIFF_RIGHT] = f[1];
2445 dview->merged[DIFF_LEFT] = FALSE;
2446 dview->merged[DIFF_RIGHT] = FALSE;
2447 dview->hdiff = NULL;
2448 dview->dsrc = dsrc;
2449 #ifdef HAVE_CHARSET
2450 dview->converter = str_cnv_from_term;
2451 dview_set_codeset (dview);
2452 #endif
2453 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2454 g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2455 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2456 g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2458 return 0;
2461 /* --------------------------------------------------------------------------------------------- */
2463 static void
2464 dview_fini (WDiff * dview)
2466 if (dview->dsrc != DATA_SRC_MEM)
2468 dview_fclose (dview->f[DIFF_RIGHT]);
2469 dview_fclose (dview->f[DIFF_LEFT]);
2472 #ifdef HAVE_CHARSET
2473 if (dview->converter != str_cnv_from_term)
2474 str_close_conv (dview->converter);
2475 #endif
2477 destroy_hdiff (dview);
2478 if (dview->a[DIFF_LEFT] != NULL)
2480 g_array_free (dview->a[DIFF_LEFT], TRUE);
2481 dview->a[DIFF_LEFT] = NULL;
2483 if (dview->a[DIFF_RIGHT] != NULL)
2485 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2486 dview->a[DIFF_RIGHT] = NULL;
2489 g_free (dview->label[DIFF_LEFT]);
2490 g_free (dview->label[DIFF_RIGHT]);
2493 /* --------------------------------------------------------------------------------------------- */
2495 static int
2496 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2498 size_t i, k;
2499 int j;
2500 char buf[BUFSIZ];
2501 FBUF *f = dview->f[ord];
2502 int skip = dview->skip_cols;
2503 gboolean display_symbols = dview->display_symbols;
2504 int display_numbers = dview->display_numbers;
2505 gboolean show_cr = dview->show_cr;
2506 int tab_size = 8;
2507 const DIFFLN *p;
2508 int nwidth = display_numbers;
2509 int xwidth;
2511 xwidth = display_numbers;
2512 if (display_symbols)
2513 xwidth++;
2514 if (dview->tab_size > 0 && dview->tab_size < 9)
2515 tab_size = dview->tab_size;
2517 if (xwidth != 0)
2519 if (xwidth > width && display_symbols)
2521 xwidth--;
2522 display_symbols = FALSE;
2524 if (xwidth > width && display_numbers != 0)
2526 xwidth = width;
2527 display_numbers = width;
2530 xwidth++;
2531 c += xwidth;
2532 width -= xwidth;
2533 if (width < 0)
2534 width = 0;
2537 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2539 /* abnormal, but avoid buffer overflow */
2540 return -1;
2543 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2545 int ch;
2546 int next_ch = 0;
2547 int col;
2548 size_t cnt;
2550 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2551 ch = p->ch;
2552 tty_setcolor (NORMAL_COLOR);
2553 if (display_symbols)
2555 tty_gotoyx (r + j, c - 2);
2556 tty_print_char (ch);
2558 if (p->line != 0)
2560 if (display_numbers != 0)
2562 tty_gotoyx (r + j, c - xwidth);
2563 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2564 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2566 if (ch == ADD_CH)
2567 tty_setcolor (DFF_ADD_COLOR);
2568 if (ch == CHG_CH)
2569 tty_setcolor (DFF_CHG_COLOR);
2570 if (f == NULL)
2572 if (i == (size_t) dview->search.last_found_line)
2573 tty_setcolor (MARKED_SELECTED_COLOR);
2574 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2576 char att[BUFSIZ];
2578 #ifdef HAVE_CHARSET
2579 if (dview->utf8)
2580 k = dview_str_utf8_offset_to_pos (p->p, width);
2581 else
2582 #endif
2583 k = width;
2585 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2586 g_ptr_array_index (dview->hdiff, i), ord, att);
2587 tty_gotoyx (r + j, c);
2588 col = 0;
2590 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2592 gboolean ch_res;
2594 #ifdef HAVE_CHARSET
2595 if (dview->utf8)
2597 int ch_length = 0;
2599 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2600 if (ch_length > 1)
2601 cnt += ch_length - 1;
2602 if (!g_unichar_isprint (next_ch))
2603 next_ch = '.';
2605 else
2606 #endif
2607 ch_res = dview_get_byte (buf + cnt, &next_ch);
2609 if (ch_res)
2611 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2612 #ifdef HAVE_CHARSET
2613 if (mc_global.utf8_display)
2615 if (!dview->utf8)
2617 next_ch =
2618 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2619 dview->converter);
2622 else if (dview->utf8)
2623 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2624 else
2625 next_ch = convert_to_display_c (next_ch);
2626 #endif
2627 tty_print_anychar (next_ch);
2628 col++;
2631 continue;
2634 if (ch == CHG_CH)
2635 tty_setcolor (DFF_CHH_COLOR);
2637 #ifdef HAVE_CHARSET
2638 if (dview->utf8)
2639 k = dview_str_utf8_offset_to_pos (p->p, width);
2640 else
2641 #endif
2642 k = width;
2643 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2645 else
2646 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2648 else
2650 if (display_numbers != 0)
2652 tty_gotoyx (r + j, c - xwidth);
2653 fill_by_space (buf, display_numbers, TRUE);
2654 tty_print_string (buf);
2656 if (ch == DEL_CH)
2657 tty_setcolor (DFF_DEL_COLOR);
2658 if (ch == CHG_CH)
2659 tty_setcolor (DFF_CHD_COLOR);
2660 fill_by_space (buf, width, TRUE);
2662 tty_gotoyx (r + j, c);
2663 /* tty_print_nstring (buf, width); */
2664 col = 0;
2665 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2667 gboolean ch_res;
2669 #ifdef HAVE_CHARSET
2670 if (dview->utf8)
2672 int ch_length = 0;
2674 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2675 if (ch_length > 1)
2676 cnt += ch_length - 1;
2677 if (!g_unichar_isprint (next_ch))
2678 next_ch = '.';
2680 else
2681 #endif
2682 ch_res = dview_get_byte (buf + cnt, &next_ch);
2684 if (ch_res)
2686 #ifdef HAVE_CHARSET
2687 if (mc_global.utf8_display)
2689 if (!dview->utf8)
2690 next_ch =
2691 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2693 else if (dview->utf8)
2694 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2695 else
2696 next_ch = convert_to_display_c (next_ch);
2697 #endif
2699 tty_print_anychar (next_ch);
2700 col++;
2704 tty_setcolor (NORMAL_COLOR);
2705 k = width;
2706 if (width < xwidth - 1)
2707 k = xwidth - 1;
2708 fill_by_space (buf, k, TRUE);
2709 for (; j < height; j++)
2711 if (xwidth != 0)
2713 tty_gotoyx (r + j, c - xwidth);
2714 /* tty_print_nstring (buf, xwidth - 1); */
2715 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2717 tty_gotoyx (r + j, c);
2718 /* tty_print_nstring (buf, width); */
2719 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2722 return 0;
2725 /* --------------------------------------------------------------------------------------------- */
2727 static void
2728 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2730 const char *buf;
2731 int filename_width;
2732 int linenum, lineofs;
2733 vfs_path_t *vpath;
2734 char *path;
2736 tty_setcolor (STATUSBAR_COLOR);
2738 tty_gotoyx (0, c);
2739 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2741 filename_width = width - 24;
2742 if (filename_width < 8)
2743 filename_width = 8;
2745 vpath = vfs_path_from_str (dview->label[ord]);
2746 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2747 vfs_path_free (vpath, TRUE);
2748 buf = str_term_trim (path, filename_width);
2749 if (ord == DIFF_LEFT)
2750 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2751 buf, linenum, lineofs, dview->skip_cols);
2752 else
2753 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2754 buf, linenum, lineofs, dview->ndiff);
2755 g_free (path);
2758 /* --------------------------------------------------------------------------------------------- */
2760 static void
2761 dview_redo (WDiff * dview)
2763 if (dview->display_numbers != 0)
2765 int old;
2767 old = dview->display_numbers;
2768 dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
2769 dview->new_frame = (old != dview->display_numbers);
2771 dview_reread (dview);
2774 /* --------------------------------------------------------------------------------------------- */
2776 static void
2777 dview_update (WDiff * dview)
2779 int height = dview->height;
2780 int width1, width2;
2781 int last;
2783 last = dview->a[DIFF_LEFT]->len - 1;
2785 if (dview->skip_rows > last)
2786 dview->skip_rows = dview->search.last_accessed_num_line = last;
2787 if (dview->skip_rows < 0)
2788 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2789 if (dview->skip_cols < 0)
2790 dview->skip_cols = 0;
2792 if (height < 2)
2793 return;
2795 /* use an actual length of dview->a */
2796 if (dview->display_numbers != 0)
2797 dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
2799 width1 = dview->half1 + dview->bias;
2800 width2 = dview->half2 - dview->bias;
2801 if (dview->full)
2803 width1 = COLS;
2804 width2 = 0;
2807 if (dview->new_frame)
2809 int xwidth;
2811 tty_setcolor (NORMAL_COLOR);
2812 xwidth = dview->display_numbers;
2813 if (dview->display_symbols)
2814 xwidth++;
2815 if (width1 > 1)
2816 tty_draw_box (1, 0, height, width1, FALSE);
2817 if (width2 > 1)
2818 tty_draw_box (1, width1, height, width2, FALSE);
2820 if (xwidth != 0)
2822 xwidth++;
2823 if (xwidth < width1 - 1)
2825 tty_gotoyx (1, xwidth);
2826 tty_print_alt_char (ACS_TTEE, FALSE);
2827 tty_gotoyx (height, xwidth);
2828 tty_print_alt_char (ACS_BTEE, FALSE);
2829 tty_draw_vline (2, xwidth, ACS_VLINE, height - 2);
2831 if (xwidth < width2 - 1)
2833 tty_gotoyx (1, width1 + xwidth);
2834 tty_print_alt_char (ACS_TTEE, FALSE);
2835 tty_gotoyx (height, width1 + xwidth);
2836 tty_print_alt_char (ACS_BTEE, FALSE);
2837 tty_draw_vline (2, width1 + xwidth, ACS_VLINE, height - 2);
2840 dview->new_frame = FALSE;
2843 if (width1 > 2)
2845 dview_status (dview, dview->ord, width1, 0);
2846 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2848 if (width2 > 2)
2850 diff_place_t ord;
2852 ord = dview->ord == DIFF_LEFT ? DIFF_RIGHT : DIFF_LEFT;
2853 dview_status (dview, ord, width2, width1);
2854 dview_display_file (dview, ord, 2, width1 + 1, height - 2, width2 - 2);
2858 /* --------------------------------------------------------------------------------------------- */
2860 static void
2861 dview_edit (WDiff * dview, diff_place_t ord)
2863 Widget *h;
2864 gboolean h_modal;
2865 int linenum, lineofs;
2867 if (dview->dsrc == DATA_SRC_TMP)
2869 error_dialog (_("Edit"), _("Edit is disabled"));
2870 return;
2873 h = WIDGET (WIDGET (dview)->owner);
2874 h_modal = widget_get_state (h, WST_MODAL);
2876 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2878 /* disallow edit file in several editors */
2879 widget_set_state (h, WST_MODAL, TRUE);
2882 vfs_path_t *tmp_vpath;
2884 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2885 edit_file_at_line (tmp_vpath, use_internal_edit, linenum);
2886 vfs_path_free (tmp_vpath, TRUE);
2889 widget_set_state (h, WST_MODAL, h_modal);
2890 dview_redo (dview);
2891 dview_update (dview);
2894 /* --------------------------------------------------------------------------------------------- */
2896 static void
2897 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2899 static gboolean first_run = TRUE;
2901 /* *INDENT-OFF* */
2902 static const char *title[2] = {
2903 N_("Goto line (left)"),
2904 N_("Goto line (right)")
2906 /* *INDENT-ON* */
2908 int newline;
2909 char *input;
2911 input =
2912 input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE,
2913 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
2914 if (input != NULL)
2916 const char *s = input;
2918 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2920 size_t i = 0;
2922 if (newline > 0)
2923 for (; i < dview->a[ord]->len; i++)
2925 const DIFFLN *p;
2927 p = &g_array_index (dview->a[ord], DIFFLN, i);
2928 if (p->line == newline)
2929 break;
2932 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2935 g_free (input);
2938 first_run = FALSE;
2941 /* --------------------------------------------------------------------------------------------- */
2943 static void
2944 dview_labels (WDiff * dview)
2946 Widget *d = WIDGET (dview);
2947 WButtonBar *b;
2949 b = buttonbar_find (DIALOG (d->owner));
2951 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), d->keymap, d);
2952 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), d->keymap, d);
2953 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), d->keymap, d);
2954 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), d->keymap, d);
2955 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), d->keymap, d);
2956 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), d->keymap, d);
2957 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), d->keymap, d);
2960 /* --------------------------------------------------------------------------------------------- */
2962 static gboolean
2963 dview_save (WDiff * dview)
2965 gboolean res = TRUE;
2967 if (dview->merged[DIFF_LEFT])
2969 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2970 dview->merged[DIFF_LEFT] = !res;
2972 if (dview->merged[DIFF_RIGHT])
2974 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2975 dview->merged[DIFF_RIGHT] = !res;
2977 return res;
2980 /* --------------------------------------------------------------------------------------------- */
2982 static void
2983 dview_do_save (WDiff * dview)
2985 (void) dview_save (dview);
2988 /* --------------------------------------------------------------------------------------------- */
2991 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2992 * ask user.
2994 static gboolean
2995 dview_ok_to_exit (WDiff * dview)
2997 gboolean res = TRUE;
2998 int act;
3000 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3001 return res;
3003 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3004 _("File(s) was modified. Save with exit?") :
3005 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3006 D_NORMAL, 2, _("&Yes"), _("&No"));
3008 /* Esc is No */
3009 if (mc_global.midnight_shutdown || (act == -1))
3010 act = 1;
3012 switch (act)
3014 case -1: /* Esc */
3015 res = FALSE;
3016 break;
3017 case 0: /* Yes */
3018 (void) dview_save (dview);
3019 res = TRUE;
3020 break;
3021 case 1: /* No */
3022 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3023 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3024 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3025 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3026 MC_FALLTHROUGH;
3027 default:
3028 res = TRUE;
3029 break;
3031 return res;
3034 /* --------------------------------------------------------------------------------------------- */
3036 static cb_ret_t
3037 dview_execute_cmd (WDiff * dview, long command)
3039 cb_ret_t res = MSG_HANDLED;
3041 switch (command)
3043 case CK_ShowSymbols:
3044 dview->display_symbols = !dview->display_symbols;
3045 dview->new_frame = TRUE;
3046 break;
3047 case CK_ShowNumbers:
3048 dview->display_numbers ^= calc_nwidth ((const GArray * const *) dview->a);
3049 dview->new_frame = TRUE;
3050 break;
3051 case CK_SplitFull:
3052 dview->full = !dview->full;
3053 dview->new_frame = TRUE;
3054 break;
3055 case CK_SplitEqual:
3056 if (!dview->full)
3058 dview->bias = 0;
3059 dview->new_frame = TRUE;
3061 break;
3062 case CK_SplitMore:
3063 if (!dview->full)
3065 dview_compute_split (dview, 1);
3066 dview->new_frame = TRUE;
3068 break;
3070 case CK_SplitLess:
3071 if (!dview->full)
3073 dview_compute_split (dview, -1);
3074 dview->new_frame = TRUE;
3076 break;
3077 case CK_Tab2:
3078 dview->tab_size = 2;
3079 break;
3080 case CK_Tab3:
3081 dview->tab_size = 3;
3082 break;
3083 case CK_Tab4:
3084 dview->tab_size = 4;
3085 break;
3086 case CK_Tab8:
3087 dview->tab_size = 8;
3088 break;
3089 case CK_Swap:
3090 dview->ord ^= 1;
3091 break;
3092 case CK_Redo:
3093 dview_redo (dview);
3094 break;
3095 case CK_HunkNext:
3096 dview->skip_rows = dview->search.last_accessed_num_line =
3097 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3098 break;
3099 case CK_HunkPrev:
3100 dview->skip_rows = dview->search.last_accessed_num_line =
3101 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3102 break;
3103 case CK_Goto:
3104 dview_goto_cmd (dview, DIFF_RIGHT);
3105 break;
3106 case CK_Edit:
3107 dview_edit (dview, dview->ord);
3108 break;
3109 case CK_Merge:
3110 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3111 dview_redo (dview);
3112 break;
3113 case CK_MergeOther:
3114 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3115 dview_redo (dview);
3116 break;
3117 case CK_EditOther:
3118 dview_edit (dview, dview->ord ^ 1);
3119 break;
3120 case CK_Search:
3121 dview_search_cmd (dview);
3122 break;
3123 case CK_SearchContinue:
3124 dview_continue_search_cmd (dview);
3125 break;
3126 case CK_Top:
3127 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3128 break;
3129 case CK_Bottom:
3130 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3131 break;
3132 case CK_Up:
3133 if (dview->skip_rows > 0)
3135 dview->skip_rows--;
3136 dview->search.last_accessed_num_line = dview->skip_rows;
3138 break;
3139 case CK_Down:
3140 dview->skip_rows++;
3141 dview->search.last_accessed_num_line = dview->skip_rows;
3142 break;
3143 case CK_PageDown:
3144 if (dview->height > 2)
3146 dview->skip_rows += dview->height - 2;
3147 dview->search.last_accessed_num_line = dview->skip_rows;
3149 break;
3150 case CK_PageUp:
3151 if (dview->height > 2)
3153 dview->skip_rows -= dview->height - 2;
3154 dview->search.last_accessed_num_line = dview->skip_rows;
3156 break;
3157 case CK_Left:
3158 dview->skip_cols--;
3159 break;
3160 case CK_Right:
3161 dview->skip_cols++;
3162 break;
3163 case CK_LeftQuick:
3164 dview->skip_cols -= 8;
3165 break;
3166 case CK_RightQuick:
3167 dview->skip_cols += 8;
3168 break;
3169 case CK_Home:
3170 dview->skip_cols = 0;
3171 break;
3172 case CK_Shell:
3173 toggle_subshell ();
3174 break;
3175 case CK_Quit:
3176 dview->view_quit = TRUE;
3177 break;
3178 case CK_Save:
3179 dview_do_save (dview);
3180 break;
3181 case CK_Options:
3182 dview_diff_options (dview);
3183 break;
3184 #ifdef HAVE_CHARSET
3185 case CK_SelectCodepage:
3186 dview_select_encoding (dview);
3187 break;
3188 #endif
3189 case CK_Cancel:
3190 /* don't close diffviewer due to SIGINT */
3191 break;
3192 default:
3193 res = MSG_NOT_HANDLED;
3195 return res;
3198 /* --------------------------------------------------------------------------------------------- */
3200 static cb_ret_t
3201 dview_handle_key (WDiff * dview, int key)
3203 long command;
3205 #ifdef HAVE_CHARSET
3206 key = convert_from_input_c (key);
3207 #endif
3209 command = widget_lookup_key (WIDGET (dview), key);
3210 if (command == CK_IgnoreKey)
3211 return MSG_NOT_HANDLED;
3213 return dview_execute_cmd (dview, command);
3216 /* --------------------------------------------------------------------------------------------- */
3218 static cb_ret_t
3219 dview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3221 WDiff *dview = (WDiff *) w;
3222 WDialog *h = DIALOG (w->owner);
3223 cb_ret_t i;
3225 switch (msg)
3227 case MSG_INIT:
3228 dview_labels (dview);
3229 dview_update (dview);
3230 return MSG_HANDLED;
3232 case MSG_DRAW:
3233 dview->new_frame = TRUE;
3234 dview_update (dview);
3235 return MSG_HANDLED;
3237 case MSG_KEY:
3238 i = dview_handle_key (dview, parm);
3239 if (dview->view_quit)
3240 dlg_close (h);
3241 else
3242 dview_update (dview);
3243 return i;
3245 case MSG_ACTION:
3246 i = dview_execute_cmd (dview, parm);
3247 if (dview->view_quit)
3248 dlg_close (h);
3249 else
3250 dview_update (dview);
3251 return i;
3253 case MSG_RESIZE:
3254 widget_default_callback (w, NULL, MSG_RESIZE, 0, data);
3255 dview_compute_areas (dview);
3256 return MSG_HANDLED;
3258 case MSG_DESTROY:
3259 dview_save_options (dview);
3260 dview_fini (dview);
3261 return MSG_HANDLED;
3263 default:
3264 return widget_default_callback (w, sender, msg, parm, data);
3268 /* --------------------------------------------------------------------------------------------- */
3270 static void
3271 dview_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3273 WDiff *dview = (WDiff *) w;
3275 (void) event;
3277 switch (msg)
3279 case MSG_MOUSE_SCROLL_UP:
3280 case MSG_MOUSE_SCROLL_DOWN:
3281 if (msg == MSG_MOUSE_SCROLL_UP)
3282 dview->skip_rows -= 2;
3283 else
3284 dview->skip_rows += 2;
3286 dview->search.last_accessed_num_line = dview->skip_rows;
3287 dview_update (dview);
3288 break;
3290 default:
3291 break;
3295 /* --------------------------------------------------------------------------------------------- */
3297 static cb_ret_t
3298 dview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3300 WDiff *dview;
3301 WDialog *h = DIALOG (w);
3303 switch (msg)
3305 case MSG_ACTION:
3306 /* Handle shortcuts. */
3308 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3309 * here, which is why we can pass NULL in the following call. */
3310 return dview_execute_cmd (NULL, parm);
3312 case MSG_VALIDATE:
3313 dview = (WDiff *) widget_find_by_type (CONST_WIDGET (h), dview_callback);
3314 /* don't stop the dialog before final decision */
3315 widget_set_state (w, WST_ACTIVE, TRUE);
3316 if (dview_ok_to_exit (dview))
3317 dlg_close (h);
3318 return MSG_HANDLED;
3320 default:
3321 return dlg_default_callback (w, sender, msg, parm, data);
3325 /* --------------------------------------------------------------------------------------------- */
3327 static char *
3328 dview_get_title (const WDialog * h, size_t len)
3330 const WDiff *dview;
3331 const char *modified = " (*) ";
3332 const char *notmodified = " ";
3333 size_t len1;
3334 GString *title;
3336 dview = (const WDiff *) widget_find_by_type (CONST_WIDGET (h), dview_callback);
3337 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3339 title = g_string_sized_new (len);
3340 g_string_append (title, _("Diff:"));
3341 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3342 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3343 g_string_append (title, " | ");
3344 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3345 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3347 return g_string_free (title, FALSE);
3350 /* --------------------------------------------------------------------------------------------- */
3352 static int
3353 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3355 int error;
3356 WDiff *dview;
3357 Widget *w;
3358 WDialog *dview_dlg;
3359 Widget *dw;
3360 WRect r;
3361 WGroup *g;
3363 /* Create dialog and widgets, put them on the dialog */
3364 dview_dlg =
3365 dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, NULL, dview_dialog_callback, NULL,
3366 "[Diff Viewer]", NULL);
3367 dw = WIDGET (dview_dlg);
3368 widget_want_tab (dw, TRUE);
3369 r = dw->rect;
3371 g = GROUP (dview_dlg);
3373 dview = g_new0 (WDiff, 1);
3374 w = WIDGET (dview);
3375 r.lines--;
3376 widget_init (w, &r, dview_callback, dview_mouse_callback);
3377 w->options |= WOP_SELECTABLE;
3378 w->keymap = diff_map;
3379 group_add_widget_autopos (g, w, WPOS_KEEP_ALL, NULL);
3381 w = WIDGET (buttonbar_new ());
3382 group_add_widget_autopos (g, w, w->pos_flags, NULL);
3384 dview_dlg->get_title = dview_get_title;
3386 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3387 if (error >= 0)
3388 error = redo_diff (dview);
3389 if (error >= 0)
3391 dview->ndiff = error;
3392 dview_compute_areas (dview);
3393 error = 0;
3396 if (error == 0)
3397 dlg_run (dview_dlg);
3399 if (error != 0 || widget_get_state (dw, WST_CLOSED))
3400 widget_destroy (dw);
3402 return error == 0 ? 1 : 0;
3405 /*** public functions ****************************************************************************/
3406 /* --------------------------------------------------------------------------------------------- */
3408 #define GET_FILE_AND_STAMP(n) \
3409 do \
3411 use_copy##n = 0; \
3412 real_file##n = file##n; \
3413 if (!vfs_file_is_local (file##n)) \
3415 real_file##n = mc_getlocalcopy (file##n); \
3416 if (real_file##n != NULL) \
3418 use_copy##n = 1; \
3419 if (mc_stat (real_file##n, &st##n) != 0) \
3420 use_copy##n = -1; \
3424 while (0)
3426 #define UNGET_FILE(n) \
3427 do \
3429 if (use_copy##n != 0) \
3431 gboolean changed = FALSE; \
3432 if (use_copy##n > 0) \
3434 time_t mtime; \
3435 mtime = st##n.st_mtime; \
3436 if (mc_stat (real_file##n, &st##n) == 0) \
3437 changed = (mtime != st##n.st_mtime); \
3439 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3440 vfs_path_free (real_file##n, TRUE); \
3443 while (0)
3445 gboolean
3446 dview_diff_cmd (const void *f0, const void *f1)
3448 int rv = 0;
3449 vfs_path_t *file0 = NULL;
3450 vfs_path_t *file1 = NULL;
3451 gboolean is_dir0 = FALSE;
3452 gboolean is_dir1 = FALSE;
3454 switch (mc_global.mc_run_mode)
3456 case MC_RUN_FULL:
3458 /* run from panels */
3459 const WPanel *panel0 = (const WPanel *) f0;
3460 const WPanel *panel1 = (const WPanel *) f1;
3461 const file_entry_t *fe0, *fe1;
3463 fe0 = panel_current_entry (panel0);
3464 file0 = vfs_path_append_new (panel0->cwd_vpath, fe0->fname->str, (char *) NULL);
3465 is_dir0 = S_ISDIR (fe0->st.st_mode);
3466 if (is_dir0)
3468 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3469 path_trunc (fe0->fname->str, 30));
3470 goto ret;
3473 fe1 = panel_current_entry (panel1);
3474 file1 = vfs_path_append_new (panel1->cwd_vpath, fe1->fname->str, (char *) NULL);
3475 is_dir1 = S_ISDIR (fe1->st.st_mode);
3476 if (is_dir1)
3478 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3479 path_trunc (fe1->fname->str, 30));
3480 goto ret;
3482 break;
3485 case MC_RUN_DIFFVIEWER:
3487 /* run from command line */
3488 const char *p0 = (const char *) f0;
3489 const char *p1 = (const char *) f1;
3490 struct stat st;
3492 file0 = vfs_path_from_str (p0);
3493 if (mc_stat (file0, &st) == 0)
3495 is_dir0 = S_ISDIR (st.st_mode);
3496 if (is_dir0)
3498 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3499 goto ret;
3502 else
3504 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3505 path_trunc (p0, 30), unix_error_string (errno));
3506 goto ret;
3509 file1 = vfs_path_from_str (p1);
3510 if (mc_stat (file1, &st) == 0)
3512 is_dir1 = S_ISDIR (st.st_mode);
3513 if (is_dir1)
3515 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3516 goto ret;
3519 else
3521 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3522 path_trunc (p1, 30), unix_error_string (errno));
3523 goto ret;
3525 break;
3528 default:
3529 /* this should not happened */
3530 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3531 return FALSE;
3534 if (rv == 0)
3536 rv = -1;
3537 if (file0 != NULL && file1 != NULL)
3539 int use_copy0, use_copy1;
3540 struct stat st0, st1;
3541 vfs_path_t *real_file0, *real_file1;
3543 GET_FILE_AND_STAMP (0);
3544 GET_FILE_AND_STAMP (1);
3546 if (real_file0 != NULL && real_file1 != NULL)
3547 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3548 vfs_path_as_str (file0), vfs_path_as_str (file1));
3550 UNGET_FILE (1);
3551 UNGET_FILE (0);
3555 if (rv == 0)
3556 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3558 ret:
3559 vfs_path_free (file1, TRUE);
3560 vfs_path_free (file0, TRUE);
3562 return (rv != 0);
3565 #undef GET_FILE_AND_STAMP
3566 #undef UNGET_FILE
3568 /* --------------------------------------------------------------------------------------------- */