Ticket #3262: rename variables.
[midnight-commander.git] / src / diffviewer / ydiff.c
blobbde3b6520f5a4581064693ff4fb99b90c603e438
1 /*
2 Copyright (C) 2007-2015
3 Free Software Foundation, Inc.
5 Written by:
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
8 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013
9 Ilia Maslakov <il.smind@gmail.com>, 2010
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
37 #include "lib/global.h"
38 #include "lib/tty/tty.h"
39 #include "lib/tty/color.h"
40 #include "lib/tty/key.h"
41 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
42 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
43 #include "lib/util.h"
44 #include "lib/widget.h"
45 #include "lib/strutil.h"
46 #include "lib/strescape.h" /* strutils_glob_escape() */
47 #ifdef HAVE_CHARSET
48 #include "lib/charsets.h"
49 #endif
50 #include "lib/event.h" /* mc_event_raise() */
52 #include "src/filemanager/cmd.h" /* edit_file_at_line(), view_other_cmd() */
53 #include "src/filemanager/panel.h"
54 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
56 #include "src/keybind-defaults.h"
57 #include "src/setup.h"
58 #include "src/history.h"
59 #ifdef HAVE_CHARSET
60 #include "src/selcodepage.h"
61 #endif
63 #include "ydiff.h"
64 #include "internal.h"
66 /*** global variables ****************************************************************************/
68 /*** file scope macro definitions ****************************************************************/
70 #define g_array_foreach(a, TP, cbf) \
71 do { \
72 size_t g_array_foreach_i;\
74 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
75 { \
76 TP *g_array_foreach_var; \
78 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
79 (*cbf) (g_array_foreach_var); \
80 } \
81 } while (0)
83 #define FILE_READ_BUF 4096
84 #define FILE_FLAG_TEMP (1 << 0)
86 #define ADD_CH '+'
87 #define DEL_CH '-'
88 #define CHG_CH '*'
89 #define EQU_CH ' '
91 #define HDIFF_ENABLE 1
92 #define HDIFF_MINCTX 5
93 #define HDIFF_DEPTH 10
95 #define FILE_DIRTY(fs) \
96 do \
97 { \
98 (fs)->pos = 0; \
99 (fs)->len = 0; \
101 while (0)
103 /*** file scope type declarations ****************************************************************/
105 typedef enum
107 FROM_LEFT_TO_RIGHT,
108 FROM_RIGHT_TO_LEFT
109 } action_direction_t;
111 /*** file scope variables ************************************************************************/
113 /*** file scope functions ************************************************************************/
114 /* --------------------------------------------------------------------------------------------- */
116 static inline int
117 TAB_SKIP (int ts, int pos)
119 if (ts > 0 && ts < 9)
120 return ts - pos % ts;
121 else
122 return 8 - pos % 8;
125 /* --------------------------------------------------------------------------------------------- */
127 static gboolean
128 rewrite_backup_content (const vfs_path_t * from_file_name_vpath, const char *to_file_name)
130 FILE *backup_fd;
131 char *contents;
132 gsize length;
133 const char *from_file_name;
135 from_file_name = vfs_path_get_by_index (from_file_name_vpath, -1)->path;
136 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
137 return FALSE;
139 backup_fd = fopen (to_file_name, "w");
140 if (backup_fd == NULL)
142 g_free (contents);
143 return FALSE;
146 length = fwrite ((const void *) contents, length, 1, backup_fd);
148 fflush (backup_fd);
149 fclose (backup_fd);
150 g_free (contents);
151 return TRUE;
154 /* buffered I/O ************************************************************* */
157 * Try to open a temporary file.
158 * @note the name is not altered if this function fails
160 * @param[out] name address of a pointer to store the temporary name
161 * @return file descriptor on success, negative on error
164 static int
165 open_temp (void **name)
167 int fd;
168 vfs_path_t *diff_file_name = NULL;
170 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
171 if (fd == -1)
173 message (D_ERROR, MSG_ERROR,
174 _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
175 return -1;
177 *name = g_strdup (vfs_path_as_str (diff_file_name));
178 vfs_path_free (diff_file_name);
179 return fd;
182 /* --------------------------------------------------------------------------------------------- */
185 * Alocate file structure and associate file descriptor to it.
187 * @param fd file descriptor
188 * @return file structure
191 static FBUF *
192 f_dopen (int fd)
194 FBUF *fs;
196 if (fd < 0)
197 return NULL;
199 fs = g_try_malloc (sizeof (FBUF));
200 if (fs == NULL)
201 return NULL;
203 fs->buf = g_try_malloc (FILE_READ_BUF);
204 if (fs->buf == NULL)
206 g_free (fs);
207 return NULL;
210 fs->fd = fd;
211 FILE_DIRTY (fs);
212 fs->flags = 0;
213 fs->data = NULL;
215 return fs;
218 /* --------------------------------------------------------------------------------------------- */
221 * Free file structure without closing the file.
223 * @param fs file structure
224 * @return 0 on success, non-zero on error
227 static int
228 f_free (FBUF * fs)
230 int rv = 0;
232 if (fs->flags & FILE_FLAG_TEMP)
234 rv = unlink (fs->data);
235 g_free (fs->data);
237 g_free (fs->buf);
238 g_free (fs);
239 return rv;
242 /* --------------------------------------------------------------------------------------------- */
245 * Open a binary temporary file in R/W mode.
246 * @note the file will be deleted when closed
248 * @return file structure
250 static FBUF *
251 f_temp (void)
253 int fd;
254 FBUF *fs;
256 fs = f_dopen (0);
257 if (fs == NULL)
258 return NULL;
260 fd = open_temp (&fs->data);
261 if (fd < 0)
263 f_free (fs);
264 return NULL;
267 fs->fd = fd;
268 fs->flags = FILE_FLAG_TEMP;
269 return fs;
272 /* --------------------------------------------------------------------------------------------- */
275 * Open a binary file in specified mode.
277 * @param filename file name
278 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
280 * @return file structure
283 static FBUF *
284 f_open (const char *filename, int flags)
286 int fd;
287 FBUF *fs;
289 fs = f_dopen (0);
290 if (fs == NULL)
291 return NULL;
293 fd = open (filename, flags);
294 if (fd < 0)
296 f_free (fs);
297 return NULL;
300 fs->fd = fd;
301 return fs;
304 /* --------------------------------------------------------------------------------------------- */
307 * Read a line of bytes from file until newline or EOF.
308 * @note does not stop on null-byte
309 * @note buf will not be null-terminated
311 * @param buf destination buffer
312 * @param size size of buffer
313 * @param fs file structure
315 * @return number of bytes read
318 static size_t
319 f_gets (char *buf, size_t size, FBUF * fs)
321 size_t j = 0;
325 int i;
326 int stop = 0;
328 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
330 buf[j] = fs->buf[i];
331 if (buf[j] == '\n')
332 stop = 1;
334 fs->pos = i;
336 if (j == size || stop)
337 break;
339 fs->pos = 0;
340 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
342 while (fs->len > 0);
344 return j;
347 /* --------------------------------------------------------------------------------------------- */
350 * Seek into file.
351 * @note avoids thrashing read cache when possible
353 * @param fs file structure
354 * @param off offset
355 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
357 * @return position in file, starting from begginning
360 static off_t
361 f_seek (FBUF * fs, off_t off, int whence)
363 off_t rv;
365 if (fs->len && whence != SEEK_END)
367 rv = lseek (fs->fd, 0, SEEK_CUR);
368 if (rv != -1)
370 if (whence == SEEK_CUR)
372 whence = SEEK_SET;
373 off += rv - fs->len + fs->pos;
375 if (off - rv >= -fs->len && off - rv <= 0)
377 fs->pos = fs->len + off - rv;
378 return off;
383 rv = lseek (fs->fd, off, whence);
384 if (rv != -1)
385 FILE_DIRTY (fs);
386 return rv;
389 /* --------------------------------------------------------------------------------------------- */
392 * Seek to the beginning of file, thrashing read cache.
394 * @param fs file structure
396 * @return 0 if success, non-zero on error
399 static off_t
400 f_reset (FBUF * fs)
402 off_t rv;
404 rv = lseek (fs->fd, 0, SEEK_SET);
405 if (rv != -1)
406 FILE_DIRTY (fs);
407 return rv;
410 /* --------------------------------------------------------------------------------------------- */
413 * Write bytes to file.
414 * @note thrashes read cache
416 * @param fs file structure
417 * @param buf source buffer
418 * @param size size of buffer
420 * @return number of written bytes, -1 on error
423 static ssize_t
424 f_write (FBUF * fs, const char *buf, size_t size)
426 ssize_t rv;
428 rv = write (fs->fd, buf, size);
429 if (rv >= 0)
430 FILE_DIRTY (fs);
431 return rv;
434 /* --------------------------------------------------------------------------------------------- */
437 * Truncate file to the current position.
438 * @note thrashes read cache
440 * @param fs file structure
442 * @return current file size on success, negative on error
445 static off_t
446 f_trunc (FBUF * fs)
448 off_t off;
450 off = lseek (fs->fd, 0, SEEK_CUR);
451 if (off != -1)
453 int rv;
455 rv = ftruncate (fs->fd, off);
456 if (rv != 0)
457 off = -1;
458 else
459 FILE_DIRTY (fs);
461 return off;
464 /* --------------------------------------------------------------------------------------------- */
467 * Close file.
468 * @note if this is temporary file, it is deleted
470 * @param fs file structure
471 * @return 0 on success, non-zero on error
474 static int
475 f_close (FBUF * fs)
477 int rv = -1;
479 if (fs != NULL)
481 rv = close (fs->fd);
482 f_free (fs);
485 return rv;
488 /* --------------------------------------------------------------------------------------------- */
491 * Create pipe stream to process.
493 * @param cmd shell command line
494 * @param flags open mode, either O_RDONLY or O_WRONLY
496 * @return file structure
499 static FBUF *
500 p_open (const char *cmd, int flags)
502 FILE *f;
503 FBUF *fs;
504 const char *type = NULL;
506 if (flags == O_RDONLY)
507 type = "r";
508 else if (flags == O_WRONLY)
509 type = "w";
511 if (type == NULL)
512 return NULL;
514 fs = f_dopen (0);
515 if (fs == NULL)
516 return NULL;
518 f = popen (cmd, type);
519 if (f == NULL)
521 f_free (fs);
522 return NULL;
525 fs->fd = fileno (f);
526 fs->data = f;
527 return fs;
530 /* --------------------------------------------------------------------------------------------- */
533 * Close pipe stream.
535 * @param fs structure
536 * @return 0 on success, non-zero on error
539 static int
540 p_close (FBUF * fs)
542 int rv = -1;
544 if (fs != NULL)
546 rv = pclose (fs->data);
547 f_free (fs);
550 return rv;
553 /* --------------------------------------------------------------------------------------------- */
556 * Get one char (byte) from string
558 * @param char * str, gboolean * result
559 * @return int as character or 0 and result == FALSE if fail
562 static int
563 dview_get_byte (char *str, gboolean * result)
565 if (str == NULL)
567 *result = FALSE;
568 return 0;
570 *result = TRUE;
571 return (unsigned char) *str;
574 /* --------------------------------------------------------------------------------------------- */
577 * Get utf multibyte char from string
579 * @param char * str, int * char_length, gboolean * result
580 * @return int as utf character or 0 and result == FALSE if fail
583 static int
584 dview_get_utf (char *str, int *char_length, gboolean * result)
586 int res = -1;
587 gunichar ch;
588 gchar *next_ch = NULL;
589 int ch_len = 0;
591 *result = TRUE;
593 if (str == NULL)
595 *result = FALSE;
596 return 0;
599 res = g_utf8_get_char_validated (str, -1);
601 if (res < 0)
602 ch = *str;
603 else
605 ch = res;
606 /* Calculate UTF-8 char length */
607 next_ch = g_utf8_next_char (str);
608 ch_len = next_ch - str;
610 *char_length = ch_len;
611 return ch;
614 /* --------------------------------------------------------------------------------------------- */
616 static int
617 dview_str_utf8_offset_to_pos (const char *text, size_t length)
619 ptrdiff_t result;
621 if (text == NULL || text[0] == '\0')
622 return length;
624 if (g_utf8_validate (text, -1, NULL))
625 result = g_utf8_offset_to_pointer (text, length) - text;
626 else
628 gunichar uni;
629 char *tmpbuf, *buffer;
631 buffer = tmpbuf = g_strdup (text);
632 while (tmpbuf[0] != '\0')
634 uni = g_utf8_get_char_validated (tmpbuf, -1);
635 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
636 tmpbuf = g_utf8_next_char (tmpbuf);
637 else
639 tmpbuf[0] = '.';
640 tmpbuf++;
643 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
644 g_free (buffer);
646 return max (length, (size_t) result);
649 /* --------------------------------------------------------------------------------------------- */
651 /* diff parse *************************************************************** */
654 * Read decimal number from string.
656 * @param[in,out] str string to parse
657 * @param[out] n extracted number
658 * @return 0 if success, otherwise non-zero
661 static int
662 scan_deci (const char **str, int *n)
664 const char *p = *str;
665 char *q;
667 errno = 0;
668 *n = strtol (p, &q, 10);
669 if (errno != 0 || p == q)
670 return -1;
671 *str = q;
672 return 0;
675 /* --------------------------------------------------------------------------------------------- */
678 * Parse line for diff statement.
680 * @param p string to parse
681 * @param ops list of diff statements
682 * @return 0 if success, otherwise non-zero
685 static int
686 scan_line (const char *p, GArray * ops)
688 DIFFCMD op;
690 int f1, f2;
691 int t1, t2;
692 int cmd;
693 int range;
695 /* handle the following cases:
696 * NUMaNUM[,NUM]
697 * NUM[,NUM]cNUM[,NUM]
698 * NUM[,NUM]dNUM
699 * where NUM is a positive integer
702 if (scan_deci (&p, &f1) != 0 || f1 < 0)
703 return -1;
705 f2 = f1;
706 range = 0;
707 if (*p == ',')
709 p++;
710 if (scan_deci (&p, &f2) != 0 || f2 < f1)
711 return -1;
713 range = 1;
716 cmd = *p++;
717 if (cmd == 'a')
719 if (range != 0)
720 return -1;
722 else if (cmd != 'c' && cmd != 'd')
723 return -1;
725 if (scan_deci (&p, &t1) != 0 || t1 < 0)
726 return -1;
728 t2 = t1;
729 range = 0;
730 if (*p == ',')
732 p++;
733 if (scan_deci (&p, &t2) != 0 || t2 < t1)
734 return -1;
736 range = 1;
739 if (cmd == 'd' && range != 0)
740 return -1;
742 op.a[0][0] = f1;
743 op.a[0][1] = f2;
744 op.cmd = cmd;
745 op.a[1][0] = t1;
746 op.a[1][1] = t2;
747 g_array_append_val (ops, op);
748 return 0;
751 /* --------------------------------------------------------------------------------------------- */
754 * Parse diff output and extract diff statements.
756 * @param f stream to read from
757 * @param ops list of diff statements to fill
758 * @return positive number indicating number of hunks, otherwise negative
761 static int
762 scan_diff (FBUF * f, GArray * ops)
764 int sz;
765 char buf[BUFSIZ];
767 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
769 if (isdigit (buf[0]))
771 if (buf[sz - 1] != '\n')
772 return -1;
774 buf[sz] = '\0';
775 if (scan_line (buf, ops) != 0)
776 return -1;
778 continue;
781 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
785 return ops->len;
788 /* --------------------------------------------------------------------------------------------- */
791 * Invoke diff and extract diff statements.
793 * @param args extra arguments to be passed to diff
794 * @param extra more arguments to be passed to diff
795 * @param file1 first file to compare
796 * @param file2 second file to compare
797 * @param ops list of diff statements to fill
799 * @return positive number indicating number of hunks, otherwise negative
802 static int
803 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
804 GArray * ops)
806 static const char *opt =
807 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
808 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
809 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
810 " --unchanged-group-format=''";
812 int rv;
813 FBUF *f;
814 char *cmd;
815 int code;
816 char *file1_esc, *file2_esc;
818 /* escape potential $ to avoid shell variable substitutions in popen() */
819 file1_esc = strutils_shell_escape (file1);
820 file2_esc = strutils_shell_escape (file2);
821 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
822 g_free (file1_esc);
823 g_free (file2_esc);
825 if (cmd == NULL)
826 return -1;
828 f = p_open (cmd, O_RDONLY);
829 g_free (cmd);
831 if (f == NULL)
832 return -1;
834 rv = scan_diff (f, ops);
835 code = p_close (f);
837 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
838 rv = -1;
840 return rv;
843 /* --------------------------------------------------------------------------------------------- */
846 * Reparse and display file according to diff statements.
848 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
849 * @param filename file name to display
850 * @param ops list of diff statements
851 * @param printer printf-like function to be used for displaying
852 * @param ctx printer context
854 * @return 0 if success, otherwise non-zero
857 static int
858 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
860 size_t i;
861 FBUF *f;
862 size_t sz;
863 char buf[BUFSIZ];
864 int line = 0;
865 off_t off = 0;
866 const DIFFCMD *op;
867 diff_place_t eff;
868 int add_cmd;
869 int del_cmd;
871 f = f_open (filename, O_RDONLY);
872 if (f == NULL)
873 return -1;
875 ord &= 1;
876 eff = ord;
878 add_cmd = 'a';
879 del_cmd = 'd';
880 if (ord != 0)
882 add_cmd = 'd';
883 del_cmd = 'a';
885 #define F1 a[eff][0]
886 #define F2 a[eff][1]
887 #define T1 a[ ord^1 ][0]
888 #define T2 a[ ord^1 ][1]
889 for (i = 0; i < ops->len; i++)
891 int n;
893 op = &g_array_index (ops, DIFFCMD, i);
894 n = op->F1 - (op->cmd != add_cmd);
896 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
898 line++;
899 printer (ctx, EQU_CH, line, off, sz, buf);
900 off += sz;
901 while (buf[sz - 1] != '\n')
903 sz = f_gets (buf, sizeof (buf), f);
904 if (sz == 0)
906 printer (ctx, 0, 0, 0, 1, "\n");
907 break;
909 printer (ctx, 0, 0, 0, sz, buf);
910 off += sz;
914 if (line != n)
915 goto err;
917 if (op->cmd == add_cmd)
919 n = op->T2 - op->T1 + 1;
920 while (n != 0)
922 printer (ctx, DEL_CH, 0, 0, 1, "\n");
923 n--;
927 if (op->cmd == del_cmd)
929 n = op->F2 - op->F1 + 1;
930 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
932 line++;
933 printer (ctx, ADD_CH, line, off, sz, buf);
934 off += sz;
935 while (buf[sz - 1] != '\n')
937 sz = f_gets (buf, sizeof (buf), f);
938 if (sz == 0)
940 printer (ctx, 0, 0, 0, 1, "\n");
941 break;
943 printer (ctx, 0, 0, 0, sz, buf);
944 off += sz;
946 n--;
949 if (n != 0)
950 goto err;
953 if (op->cmd == 'c')
955 n = op->F2 - op->F1 + 1;
956 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
958 line++;
959 printer (ctx, CHG_CH, line, off, sz, buf);
960 off += sz;
961 while (buf[sz - 1] != '\n')
963 sz = f_gets (buf, sizeof (buf), f);
964 if (sz == 0)
966 printer (ctx, 0, 0, 0, 1, "\n");
967 break;
969 printer (ctx, 0, 0, 0, sz, buf);
970 off += sz;
972 n--;
975 if (n != 0)
976 goto err;
978 n = op->T2 - op->T1 - (op->F2 - op->F1);
979 while (n > 0)
981 printer (ctx, CHG_CH, 0, 0, 1, "\n");
982 n--;
986 #undef T2
987 #undef T1
988 #undef F2
989 #undef F1
991 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
993 line++;
994 printer (ctx, EQU_CH, line, off, sz, buf);
995 off += sz;
996 while (buf[sz - 1] != '\n')
998 sz = f_gets (buf, sizeof (buf), f);
999 if (sz == 0)
1001 printer (ctx, 0, 0, 0, 1, "\n");
1002 break;
1004 printer (ctx, 0, 0, 0, sz, buf);
1005 off += sz;
1009 f_close (f);
1010 return 0;
1012 err:
1013 f_close (f);
1014 return -1;
1017 /* --------------------------------------------------------------------------------------------- */
1019 /* horizontal diff ********************************************************** */
1022 * Longest common substring.
1024 * @param s first string
1025 * @param m length of first string
1026 * @param t second string
1027 * @param n length of second string
1028 * @param ret list of offsets for longest common substrings inside each string
1029 * @param min minimum length of common substrings
1031 * @return 0 if success, nonzero otherwise
1034 static int
1035 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1037 int i, j;
1038 int *Lprev, *Lcurr;
1039 int z = 0;
1041 if (m < min || n < min)
1043 /* XXX early culling */
1044 return 0;
1047 Lprev = g_try_new0 (int, n + 1);
1048 if (Lprev == NULL)
1049 return -1;
1051 Lcurr = g_try_new0 (int, n + 1);
1052 if (Lcurr == NULL)
1054 g_free (Lprev);
1055 return -1;
1058 for (i = 0; i < m; i++)
1060 int *L;
1062 L = Lprev;
1063 Lprev = Lcurr;
1064 Lcurr = L;
1065 #ifdef USE_MEMSET_IN_LCS
1066 memset (Lcurr, 0, (n + 1) * sizeof (int));
1067 #endif
1068 for (j = 0; j < n; j++)
1070 #ifndef USE_MEMSET_IN_LCS
1071 Lcurr[j + 1] = 0;
1072 #endif
1073 if (s[i] == t[j])
1075 int v;
1077 v = Lprev[j] + 1;
1078 Lcurr[j + 1] = v;
1079 if (z < v)
1081 z = v;
1082 g_array_set_size (ret, 0);
1084 if (z == v && z >= min)
1086 int off0, off1;
1087 size_t k;
1089 off0 = i - z + 1;
1090 off1 = j - z + 1;
1092 for (k = 0; k < ret->len; k++)
1094 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1095 if ((*p)[0] == off0 || (*p)[1] >= off1)
1096 break;
1098 if (k == ret->len)
1100 PAIR p2;
1102 p2[0] = off0;
1103 p2[1] = off1;
1104 g_array_append_val (ret, p2);
1111 g_free (Lcurr);
1112 g_free (Lprev);
1113 return z;
1116 /* --------------------------------------------------------------------------------------------- */
1119 * Scan recursively for common substrings and build ranges.
1121 * @param s first string
1122 * @param t second string
1123 * @param bracket current limits for both of the strings
1124 * @param min minimum length of common substrings
1125 * @param hdiff list of horizontal diff ranges to fill
1126 * @param depth recursion depth
1128 * @return 0 if success, nonzero otherwise
1131 static gboolean
1132 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1133 unsigned int depth)
1135 BRACKET p;
1137 if (depth-- != 0)
1139 GArray *ret;
1140 BRACKET b;
1141 int len;
1143 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1144 if (ret == NULL)
1145 return FALSE;
1147 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1148 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1149 if (ret->len != 0)
1151 size_t k = 0;
1152 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1153 const PAIR *data2;
1155 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1156 b[DIFF_LEFT].len = (*data)[0];
1157 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1158 b[DIFF_RIGHT].len = (*data)[1];
1159 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1160 return FALSE;
1162 for (k = 0; k < ret->len - 1; k++)
1164 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1165 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1166 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1167 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1168 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1169 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1170 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1171 return FALSE;
1173 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1174 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1175 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1176 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1177 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1178 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1179 return FALSE;
1181 g_array_free (ret, TRUE);
1182 return TRUE;
1186 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1187 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1188 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1189 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1190 g_array_append_val (hdiff, p);
1192 return TRUE;
1195 /* --------------------------------------------------------------------------------------------- */
1198 * Build list of horizontal diff ranges.
1200 * @param s first string
1201 * @param m length of first string
1202 * @param t second string
1203 * @param n length of second string
1204 * @param min minimum length of common substrings
1205 * @param hdiff list of horizontal diff ranges to fill
1206 * @param depth recursion depth
1208 * @return 0 if success, nonzero otherwise
1211 static gboolean
1212 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1214 int i;
1215 BRACKET b;
1217 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1218 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1220 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1223 b[DIFF_LEFT].off = i;
1224 b[DIFF_LEFT].len = m - i;
1225 b[DIFF_RIGHT].off = i;
1226 b[DIFF_RIGHT].len = n - i;
1228 /* smartscan (multiple horizontal diff) */
1229 return hdiff_multi (s, t, b, min, hdiff, depth);
1232 /* --------------------------------------------------------------------------------------------- */
1234 /* read line **************************************************************** */
1237 * Check if character is inside horizontal diff limits.
1239 * @param k rank of character inside line
1240 * @param hdiff horizontal diff structure
1241 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1243 * @return TRUE if inside hdiff limits, FALSE otherwise
1246 static gboolean
1247 is_inside (int k, GArray * hdiff, diff_place_t ord)
1249 size_t i;
1250 BRACKET *b;
1252 for (i = 0; i < hdiff->len; i++)
1254 int start, end;
1256 b = &g_array_index (hdiff, BRACKET, i);
1257 start = (*b)[ord].off;
1258 end = start + (*b)[ord].len;
1259 if (k >= start && k < end)
1260 return TRUE;
1262 return FALSE;
1265 /* --------------------------------------------------------------------------------------------- */
1268 * Copy 'src' to 'dst' expanding tabs.
1269 * @note The procedure returns when all bytes are consumed from 'src'
1271 * @param dst destination buffer
1272 * @param src source buffer
1273 * @param srcsize size of src buffer
1274 * @param base virtual base of this string, needed to calculate tabs
1275 * @param ts tab size
1277 * @return new virtual base
1280 static int
1281 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1283 int i;
1285 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1287 *dst = *src;
1288 if (*src == '\t')
1290 int j;
1292 j = TAB_SKIP (ts, i + base);
1293 i += j - 1;
1294 while (j-- > 0)
1295 *dst++ = ' ';
1296 dst--;
1299 return i + base;
1302 /* --------------------------------------------------------------------------------------------- */
1305 * Copy 'src' to 'dst' expanding tabs.
1307 * @param dst destination buffer
1308 * @param dstsize size of dst buffer
1309 * @param[in,out] _src source buffer
1310 * @param srcsize size of src buffer
1311 * @param base virtual base of this string, needed to calculate tabs
1312 * @param ts tab size
1314 * @return new virtual base
1316 * @note The procedure returns when all bytes are consumed from 'src'
1317 * or 'dstsize' bytes are written to 'dst'
1318 * @note Upon return, 'src' points to the first unwritten character in source
1321 static int
1322 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1324 int i;
1325 const char *src = *_src;
1327 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1329 *dst = *src;
1330 if (*src == '\t')
1332 int j;
1334 j = TAB_SKIP (ts, i + base);
1335 if (j > dstsize - i)
1336 j = dstsize - i;
1337 i += j - 1;
1338 while (j-- > 0)
1339 *dst++ = ' ';
1340 dst--;
1343 *_src = src;
1344 return i + base;
1347 /* --------------------------------------------------------------------------------------------- */
1350 * Read line from memory, converting tabs to spaces and padding with spaces.
1352 * @param src buffer to read from
1353 * @param srcsize size of src buffer
1354 * @param dst buffer to read to
1355 * @param dstsize size of dst buffer, excluding trailing null
1356 * @param skip number of characters to skip
1357 * @param ts tab size
1358 * @param show_cr show trailing carriage return as ^M
1360 * @return negative on error, otherwise number of bytes except padding
1363 static int
1364 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr)
1366 int sz = 0;
1368 if (src != NULL)
1370 int i;
1371 char *tmp = dst;
1372 const int base = 0;
1374 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1376 if (*src == '\t')
1378 int j;
1380 j = TAB_SKIP (ts, i + base);
1381 i += j - 1;
1382 while (j-- > 0)
1384 if (skip > 0)
1385 skip--;
1386 else if (dstsize != 0)
1388 dstsize--;
1389 *dst++ = ' ';
1393 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1395 if (skip == 0 && show_cr)
1397 if (dstsize > 1)
1399 dstsize -= 2;
1400 *dst++ = '^';
1401 *dst++ = 'M';
1403 else
1405 dstsize--;
1406 *dst++ = '.';
1409 break;
1411 else if (skip > 0)
1413 int utf_ch = 0;
1414 gboolean res;
1415 int ch_len;
1417 skip--;
1418 utf_ch = dview_get_utf ((char *) src, &ch_len, &res);
1419 if (ch_len > 1)
1420 skip += ch_len - 1;
1421 (void) utf_ch;
1423 else
1425 dstsize--;
1426 *dst++ = *src;
1429 sz = dst - tmp;
1431 while (dstsize != 0)
1433 dstsize--;
1434 *dst++ = ' ';
1436 *dst = '\0';
1437 return sz;
1440 /* --------------------------------------------------------------------------------------------- */
1443 * Read line from memory and build attribute array.
1445 * @param src buffer to read from
1446 * @param srcsize size of src buffer
1447 * @param dst buffer to read to
1448 * @param dstsize size of dst buffer, excluding trailing null
1449 * @param skip number of characters to skip
1450 * @param ts tab size
1451 * @param show_cr show trailing carriage return as ^M
1452 * @param hdiff horizontal diff structure
1453 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1454 * @param att buffer of attributes
1456 * @return negative on error, otherwise number of bytes except padding
1459 static int
1460 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr,
1461 GArray * hdiff, diff_place_t ord, char *att)
1463 int sz = 0;
1465 if (src != NULL)
1467 int i, k;
1468 char *tmp = dst;
1469 const int base = 0;
1471 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1473 if (*src == '\t')
1475 int j;
1477 j = TAB_SKIP (ts, i + base);
1478 i += j - 1;
1479 while (j-- > 0)
1481 if (skip != 0)
1482 skip--;
1483 else if (dstsize != 0)
1485 dstsize--;
1486 *att++ = is_inside (k, hdiff, ord);
1487 *dst++ = ' ';
1491 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1493 if (skip == 0 && show_cr)
1495 if (dstsize > 1)
1497 dstsize -= 2;
1498 *att++ = is_inside (k, hdiff, ord);
1499 *dst++ = '^';
1500 *att++ = is_inside (k, hdiff, ord);
1501 *dst++ = 'M';
1503 else
1505 dstsize--;
1506 *att++ = is_inside (k, hdiff, ord);
1507 *dst++ = '.';
1510 break;
1512 else if (skip != 0)
1514 int utf_ch = 0;
1515 gboolean res;
1516 int ch_len;
1518 skip--;
1519 utf_ch = dview_get_utf ((char *) src, &ch_len, &res);
1520 if (ch_len > 1)
1521 skip += ch_len - 1;
1522 (void) utf_ch;
1524 else
1526 dstsize--;
1527 *att++ = is_inside (k, hdiff, ord);
1528 *dst++ = *src;
1531 sz = dst - tmp;
1533 while (dstsize != 0)
1535 dstsize--;
1536 *att++ = '\0';
1537 *dst++ = ' ';
1539 *dst = '\0';
1540 return sz;
1543 /* --------------------------------------------------------------------------------------------- */
1546 * Read line from file, converting tabs to spaces and padding with spaces.
1548 * @param f file stream to read from
1549 * @param off offset of line inside file
1550 * @param dst buffer to read to
1551 * @param dstsize size of dst buffer, excluding trailing null
1552 * @param skip number of characters to skip
1553 * @param ts tab size
1554 * @param show_cr show trailing carriage return as ^M
1556 * @return negative on error, otherwise number of bytes except padding
1559 static int
1560 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, int show_cr)
1562 int base = 0;
1563 int old_base = base;
1564 size_t amount = dstsize;
1565 size_t useful, offset;
1566 size_t i;
1567 size_t sz;
1568 int lastch = '\0';
1569 const char *q = NULL;
1570 char tmp[BUFSIZ]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1571 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1573 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1575 /* abnormal, but avoid buffer overflow */
1576 memset (dst, ' ', dstsize);
1577 dst[dstsize] = '\0';
1578 return 0;
1581 f_seek (f, off, SEEK_SET);
1583 while (skip > base)
1585 old_base = base;
1586 sz = f_gets (tmp, amount, f);
1587 if (sz == 0)
1588 break;
1590 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1591 if (cvt[base - old_base - 1] == '\n')
1593 q = &cvt[base - old_base - 1];
1594 base = old_base + q - cvt + 1;
1595 break;
1599 if (base < skip)
1601 memset (dst, ' ', dstsize);
1602 dst[dstsize] = '\0';
1603 return 0;
1606 useful = base - skip;
1607 offset = skip - old_base;
1609 if (useful <= dstsize)
1611 if (useful != 0)
1612 memmove (dst, cvt + offset, useful);
1614 if (q == NULL)
1616 sz = f_gets (tmp, dstsize - useful + 1, f);
1617 if (sz != 0)
1619 const char *ptr = tmp;
1621 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1622 if (ptr < tmp + sz)
1623 lastch = *ptr;
1626 sz = useful;
1628 else
1630 memmove (dst, cvt + offset, dstsize);
1631 sz = dstsize;
1632 lastch = cvt[offset + dstsize];
1635 dst[sz] = lastch;
1636 for (i = 0; i < sz && dst[i] != '\n'; i++)
1638 if (dst[i] == '\r' && dst[i + 1] == '\n')
1640 if (show_cr)
1642 if (i + 1 < dstsize)
1644 dst[i++] = '^';
1645 dst[i++] = 'M';
1647 else
1649 dst[i++] = '*';
1652 break;
1656 for (; i < dstsize; i++)
1657 dst[i] = ' ';
1658 dst[i] = '\0';
1659 return sz;
1662 /* --------------------------------------------------------------------------------------------- */
1663 /* diff printers et al ****************************************************** */
1665 static void
1666 cc_free_elt (void *elt)
1668 DIFFLN *p = elt;
1670 if (p != NULL)
1671 g_free (p->p);
1674 /* --------------------------------------------------------------------------------------------- */
1676 static int
1677 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1679 GArray *a = ((PRINTER_CTX *) ctx)->a;
1680 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1682 if (ch != 0)
1684 DIFFLN p;
1686 p.p = NULL;
1687 p.ch = ch;
1688 p.line = line;
1689 p.u.off = off;
1690 if (dsrc == DATA_SRC_MEM && line != 0)
1692 if (sz != 0 && str[sz - 1] == '\n')
1693 sz--;
1694 if (sz > 0)
1695 p.p = g_strndup (str, sz);
1696 p.u.len = sz;
1698 g_array_append_val (a, p);
1700 else if (dsrc == DATA_SRC_MEM)
1702 DIFFLN *p;
1704 p = &g_array_index (a, DIFFLN, a->len - 1);
1705 if (sz != 0 && str[sz - 1] == '\n')
1706 sz--;
1707 if (sz != 0)
1709 size_t new_size;
1710 char *q;
1712 new_size = p->u.len + sz;
1713 q = g_realloc (p->p, new_size);
1714 memcpy (q + p->u.len, str, sz);
1715 p->p = q;
1717 p->u.len += sz;
1719 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1721 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1722 f_write (f, str, sz);
1724 return 0;
1727 /* --------------------------------------------------------------------------------------------- */
1729 static int
1730 redo_diff (WDiff * dview)
1732 FBUF *const *f = dview->f;
1733 PRINTER_CTX ctx;
1734 GArray *ops;
1735 int ndiff;
1736 int rv;
1737 char extra[256];
1739 extra[0] = '\0';
1740 if (dview->opt.quality == 2)
1741 strcat (extra, " -d");
1742 if (dview->opt.quality == 1)
1743 strcat (extra, " --speed-large-files");
1744 if (dview->opt.strip_trailing_cr)
1745 strcat (extra, " --strip-trailing-cr");
1746 if (dview->opt.ignore_tab_expansion)
1747 strcat (extra, " -E");
1748 if (dview->opt.ignore_space_change)
1749 strcat (extra, " -b");
1750 if (dview->opt.ignore_all_space)
1751 strcat (extra, " -w");
1752 if (dview->opt.ignore_case)
1753 strcat (extra, " -i");
1755 if (dview->dsrc != DATA_SRC_MEM)
1757 f_reset (f[DIFF_LEFT]);
1758 f_reset (f[DIFF_RIGHT]);
1761 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1762 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1763 if (ndiff < 0)
1765 if (ops != NULL)
1766 g_array_free (ops, TRUE);
1767 return -1;
1770 ctx.dsrc = dview->dsrc;
1772 rv = 0;
1773 ctx.a = dview->a[DIFF_LEFT];
1774 ctx.f = f[DIFF_LEFT];
1775 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1777 ctx.a = dview->a[DIFF_RIGHT];
1778 ctx.f = f[DIFF_RIGHT];
1779 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1781 if (ops != NULL)
1782 g_array_free (ops, TRUE);
1784 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1785 return -1;
1787 if (dview->dsrc == DATA_SRC_TMP)
1789 f_trunc (f[DIFF_LEFT]);
1790 f_trunc (f[DIFF_RIGHT]);
1793 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1795 dview->hdiff = g_ptr_array_new ();
1796 if (dview->hdiff != NULL)
1798 size_t i;
1800 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1802 GArray *h = NULL;
1803 const DIFFLN *p;
1804 const DIFFLN *q;
1806 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1807 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1808 if (p->line && q->line && p->ch == CHG_CH)
1810 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1811 if (h != NULL)
1813 gboolean runresult;
1815 runresult =
1816 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1817 HDIFF_DEPTH);
1818 if (!runresult)
1820 g_array_free (h, TRUE);
1821 h = NULL;
1825 g_ptr_array_add (dview->hdiff, h);
1829 return ndiff;
1832 /* --------------------------------------------------------------------------------------------- */
1834 static void
1835 destroy_hdiff (WDiff * dview)
1837 if (dview->hdiff != NULL)
1839 int i;
1840 int len;
1842 len = dview->a[DIFF_LEFT]->len;
1844 for (i = 0; i < len; i++)
1846 GArray *h;
1848 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1849 if (h != NULL)
1850 g_array_free (h, TRUE);
1852 g_ptr_array_free (dview->hdiff, TRUE);
1853 dview->hdiff = NULL;
1856 mc_search_free (dview->search.handle);
1857 dview->search.handle = NULL;
1858 MC_PTR_FREE (dview->search.last_string);
1861 /* --------------------------------------------------------------------------------------------- */
1862 /* stuff ******************************************************************** */
1864 static int
1865 get_digits (unsigned int n)
1867 int d = 1;
1869 while (n /= 10)
1870 d++;
1871 return d;
1874 /* --------------------------------------------------------------------------------------------- */
1876 static int
1877 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1879 const DIFFLN *p;
1881 *linenum = 0;
1882 *lineofs = 0;
1884 if (a->len != 0)
1886 if (pos >= a->len)
1887 pos = a->len - 1;
1889 p = &g_array_index (a, DIFFLN, pos);
1891 if (p->line == 0)
1893 int n;
1895 for (n = pos; n > 0; n--)
1897 p--;
1898 if (p->line != 0)
1899 break;
1901 *lineofs = pos - n + 1;
1904 *linenum = p->line;
1906 return 0;
1909 /* --------------------------------------------------------------------------------------------- */
1911 static int
1912 calc_nwidth (const GArray ** const a)
1914 int l1, o1;
1915 int l2, o2;
1917 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1918 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1919 if (l1 < l2)
1920 l1 = l2;
1921 return get_digits (l1);
1924 /* --------------------------------------------------------------------------------------------- */
1926 static int
1927 find_prev_hunk (const GArray * a, int pos)
1929 #if 1
1930 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1931 pos--;
1932 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1933 pos--;
1934 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1935 pos--;
1936 if (pos > 0 && (size_t) pos < a->len)
1937 pos++;
1938 #else
1939 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1940 pos--;
1941 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1942 pos--;
1943 #endif
1945 return pos;
1948 /* --------------------------------------------------------------------------------------------- */
1950 static size_t
1951 find_next_hunk (const GArray * a, size_t pos)
1953 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1954 pos++;
1955 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1956 pos++;
1957 return pos;
1960 /* --------------------------------------------------------------------------------------------- */
1962 * Find start and end lines of the current hunk.
1964 * @param dview WDiff widget
1965 * @return boolean and
1966 * start_line1 first line of current hunk (file[0])
1967 * end_line1 last line of current hunk (file[0])
1968 * start_line1 first line of current hunk (file[0])
1969 * end_line1 last line of current hunk (file[0])
1972 static int
1973 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1975 const GArray *a0 = dview->a[DIFF_LEFT];
1976 const GArray *a1 = dview->a[DIFF_RIGHT];
1977 size_t pos;
1978 int ch;
1979 int res = 0;
1981 *start_line1 = 1;
1982 *start_line2 = 1;
1983 *end_line1 = 1;
1984 *end_line2 = 1;
1986 pos = dview->skip_rows;
1987 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1988 if (ch != EQU_CH)
1990 switch (ch)
1992 case ADD_CH:
1993 res = DIFF_DEL;
1994 break;
1995 case DEL_CH:
1996 res = DIFF_ADD;
1997 break;
1998 case CHG_CH:
1999 res = DIFF_CHG;
2000 break;
2002 while (pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2003 pos--;
2004 if (pos > 0)
2006 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
2007 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
2009 pos = dview->skip_rows;
2010 while (pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2012 int l0, l1;
2014 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
2015 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
2016 if (l0 > 0)
2017 *end_line1 = max (*start_line1, l0);
2018 if (l1 > 0)
2019 *end_line2 = max (*start_line2, l1);
2020 pos++;
2023 return res;
2026 /* --------------------------------------------------------------------------------------------- */
2028 * Remove hunk from file.
2030 * @param dview WDiff widget
2031 * @param merge_file file stream for writing data
2032 * @param from1 first line of hunk
2033 * @param to1 last line of hunk
2034 * @param merge_direction in what direction files should be merged
2037 static void
2038 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
2039 action_direction_t merge_direction)
2041 int line;
2042 char buf[BUF_10K];
2043 FILE *f0;
2045 if (merge_direction == FROM_RIGHT_TO_LEFT)
2046 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2047 else
2048 f0 = fopen (dview->file[DIFF_LEFT], "r");
2050 line = 0;
2051 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2053 line++;
2054 fputs (buf, merge_file);
2056 while (fgets (buf, sizeof (buf), f0) != NULL)
2058 line++;
2059 if (line >= to1)
2060 fputs (buf, merge_file);
2062 fclose (f0);
2065 /* --------------------------------------------------------------------------------------------- */
2067 * Add hunk to file.
2069 * @param dview WDiff widget
2070 * @param merge_file file stream for writing data
2071 * @param from1 first line of source hunk
2072 * @param from2 first line of destination hunk
2073 * @param to1 last line of source hunk
2074 * @param merge_direction in what direction files should be merged
2077 static void
2078 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2079 action_direction_t merge_direction)
2081 int line;
2082 char buf[BUF_10K];
2083 FILE *f0;
2084 FILE *f1;
2086 if (merge_direction == FROM_RIGHT_TO_LEFT)
2088 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2089 f1 = fopen (dview->file[DIFF_LEFT], "r");
2091 else
2093 f0 = fopen (dview->file[DIFF_LEFT], "r");
2094 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2097 line = 0;
2098 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2100 line++;
2101 fputs (buf, merge_file);
2103 line = 0;
2104 while (fgets (buf, sizeof (buf), f1) != NULL && line <= to2)
2106 line++;
2107 if (line >= from2)
2108 fputs (buf, merge_file);
2110 while (fgets (buf, sizeof (buf), f0) != NULL)
2111 fputs (buf, merge_file);
2113 fclose (f0);
2114 fclose (f1);
2117 /* --------------------------------------------------------------------------------------------- */
2119 * Replace hunk in file.
2121 * @param dview WDiff widget
2122 * @param merge_file file stream for writing data
2123 * @param from1 first line of source hunk
2124 * @param to1 last line of source hunk
2125 * @param from2 first line of destination hunk
2126 * @param to2 last line of destination hunk
2127 * @param merge_direction in what direction files should be merged
2130 static void
2131 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2132 action_direction_t merge_direction)
2134 int line1 = 0, line2 = 0;
2135 char buf[BUF_10K];
2136 FILE *f0;
2137 FILE *f1;
2139 if (merge_direction == FROM_RIGHT_TO_LEFT)
2141 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2142 f1 = fopen (dview->file[DIFF_LEFT], "r");
2144 else
2146 f0 = fopen (dview->file[DIFF_LEFT], "r");
2147 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2150 while (fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1)
2152 line1++;
2153 fputs (buf, merge_file);
2155 while (fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2)
2157 line2++;
2158 if (line2 >= from2)
2159 fputs (buf, merge_file);
2161 while (fgets (buf, sizeof (buf), f0) != NULL)
2163 line1++;
2164 if (line1 > to1)
2165 fputs (buf, merge_file);
2167 fclose (f0);
2168 fclose (f1);
2171 /* --------------------------------------------------------------------------------------------- */
2173 * Merge hunk.
2175 * @param dview WDiff widget
2176 * @param merge_direction in what direction files should be merged
2179 static void
2180 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2182 int from1, to1, from2, to2;
2183 int hunk;
2184 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2186 if (merge_direction == FROM_RIGHT_TO_LEFT)
2187 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2188 else
2189 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2191 if (hunk > 0)
2193 int merge_file_fd;
2194 FILE *merge_file;
2195 vfs_path_t *merge_file_name_vpath = NULL;
2197 if (!dview->merged[n_merge])
2199 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2200 if (!dview->merged[n_merge])
2202 message (D_ERROR, MSG_ERROR,
2203 _("Cannot create backup file\n%s%s\n%s"),
2204 dview->file[n_merge], "~~~", unix_error_string (errno));
2205 return;
2209 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2210 if (merge_file_fd == -1)
2212 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2213 unix_error_string (errno));
2214 return;
2217 merge_file = fdopen (merge_file_fd, "w");
2219 switch (hunk)
2221 case DIFF_DEL:
2222 if (merge_direction == FROM_RIGHT_TO_LEFT)
2223 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2224 else
2225 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2226 break;
2227 case DIFF_ADD:
2228 if (merge_direction == FROM_RIGHT_TO_LEFT)
2229 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2230 else
2231 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2232 break;
2233 case DIFF_CHG:
2234 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2235 break;
2237 fflush (merge_file);
2238 fclose (merge_file);
2240 int res;
2242 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2243 (void) res;
2245 mc_unlink (merge_file_name_vpath);
2246 vfs_path_free (merge_file_name_vpath);
2250 /* --------------------------------------------------------------------------------------------- */
2251 /* view routines and callbacks ********************************************** */
2253 static void
2254 dview_compute_split (WDiff * dview, int i)
2256 dview->bias += i;
2257 if (dview->bias < 2 - dview->half1)
2258 dview->bias = 2 - dview->half1;
2259 if (dview->bias > dview->half2 - 2)
2260 dview->bias = dview->half2 - 2;
2263 /* --------------------------------------------------------------------------------------------- */
2265 static void
2266 dview_compute_areas (WDiff * dview)
2268 dview->height = LINES - 2;
2269 dview->half1 = COLS / 2;
2270 dview->half2 = COLS - dview->half1;
2272 dview_compute_split (dview, 0);
2275 /* --------------------------------------------------------------------------------------------- */
2277 static void
2278 dview_reread (WDiff * dview)
2280 int ndiff;
2282 destroy_hdiff (dview);
2283 if (dview->a[DIFF_LEFT] != NULL)
2285 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2286 g_array_free (dview->a[DIFF_LEFT], TRUE);
2288 if (dview->a[DIFF_RIGHT] != NULL)
2290 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2291 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2294 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2295 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2297 ndiff = redo_diff (dview);
2298 if (ndiff >= 0)
2299 dview->ndiff = ndiff;
2302 /* --------------------------------------------------------------------------------------------- */
2304 #ifdef HAVE_CHARSET
2305 static void
2306 dview_set_codeset (WDiff * dview)
2308 const char *encoding_id = NULL;
2310 dview->utf8 = TRUE;
2311 encoding_id =
2312 get_codepage_id (mc_global.source_codepage >=
2313 0 ? mc_global.source_codepage : mc_global.display_codepage);
2314 if (encoding_id != NULL)
2316 GIConv conv;
2318 conv = str_crt_conv_from (encoding_id);
2319 if (conv != INVALID_CONV)
2321 if (dview->converter != str_cnv_from_term)
2322 str_close_conv (dview->converter);
2323 dview->converter = conv;
2325 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2329 /* --------------------------------------------------------------------------------------------- */
2331 static void
2332 dview_select_encoding (WDiff * dview)
2334 if (do_select_codepage ())
2335 dview_set_codeset (dview);
2336 dview_reread (dview);
2337 tty_touch_screen ();
2338 repaint_screen ();
2340 #endif /* HAVE_CHARSET */
2342 /* --------------------------------------------------------------------------------------------- */
2344 static void
2345 dview_diff_options (WDiff * dview)
2347 const char *quality_str[] = {
2348 N_("No&rmal"),
2349 N_("&Fastest (Assume large files)"),
2350 N_("&Minimal (Find a smaller set of change)")
2353 quick_widget_t quick_widgets[] = {
2354 /* *INDENT-OFF* */
2355 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2356 QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2357 QUICK_STOP_GROUPBOX,
2358 QUICK_START_GROUPBOX (N_("Diff extra options")),
2359 QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2360 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2361 QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2362 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2363 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2364 NULL),
2365 QUICK_STOP_GROUPBOX,
2366 QUICK_BUTTONS_OK_CANCEL,
2367 QUICK_END
2368 /* *INDENT-ON* */
2371 quick_dialog_t qdlg = {
2372 -1, -1, 56,
2373 N_("Diff Options"), "[Diff Options]",
2374 quick_widgets, NULL, NULL
2377 if (quick_dialog (&qdlg) != B_CANCEL)
2378 dview_reread (dview);
2381 /* --------------------------------------------------------------------------------------------- */
2383 static int
2384 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2385 const char *label1, const char *label2, DSRC dsrc)
2387 int ndiff;
2388 FBUF *f[DIFF_COUNT];
2390 f[DIFF_LEFT] = NULL;
2391 f[DIFF_RIGHT] = NULL;
2393 if (dsrc == DATA_SRC_TMP)
2395 f[DIFF_LEFT] = f_temp ();
2396 if (f[DIFF_LEFT] == NULL)
2397 return -1;
2399 f[DIFF_RIGHT] = f_temp ();
2400 if (f[DIFF_RIGHT] == NULL)
2402 f_close (f[DIFF_LEFT]);
2403 return -1;
2406 else if (dsrc == DATA_SRC_ORG)
2408 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2409 if (f[DIFF_LEFT] == NULL)
2410 return -1;
2412 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2413 if (f[DIFF_RIGHT] == NULL)
2415 f_close (f[DIFF_LEFT]);
2416 return -1;
2420 dview->args = args;
2421 dview->file[DIFF_LEFT] = file1;
2422 dview->file[DIFF_RIGHT] = file2;
2423 dview->label[DIFF_LEFT] = g_strdup (label1);
2424 dview->label[DIFF_RIGHT] = g_strdup (label2);
2425 dview->f[DIFF_LEFT] = f[0];
2426 dview->f[DIFF_RIGHT] = f[1];
2427 dview->merged[DIFF_LEFT] = FALSE;
2428 dview->merged[DIFF_RIGHT] = FALSE;
2429 dview->hdiff = NULL;
2430 dview->dsrc = dsrc;
2431 dview->converter = str_cnv_from_term;
2432 #ifdef HAVE_CHARSET
2433 dview_set_codeset (dview);
2434 #endif
2435 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2436 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2438 ndiff = redo_diff (dview);
2439 if (ndiff < 0)
2441 /* goto MSG_DESTROY stage: dview_fini() */
2442 f_close (f[DIFF_LEFT]);
2443 f_close (f[DIFF_RIGHT]);
2444 return -1;
2447 dview->ndiff = ndiff;
2449 dview->view_quit = 0;
2451 dview->bias = 0;
2452 dview->new_frame = 1;
2453 dview->skip_rows = 0;
2454 dview->skip_cols = 0;
2455 dview->display_symbols = 0;
2456 dview->display_numbers = 0;
2457 dview->show_cr = 1;
2458 dview->tab_size = 8;
2459 dview->ord = DIFF_LEFT;
2460 dview->full = 0;
2462 dview->search.handle = NULL;
2463 dview->search.last_string = NULL;
2464 dview->search.last_found_line = -1;
2465 dview->search.last_accessed_num_line = -1;
2467 dview->opt.quality = 0;
2468 dview->opt.strip_trailing_cr = 0;
2469 dview->opt.ignore_tab_expansion = 0;
2470 dview->opt.ignore_space_change = 0;
2471 dview->opt.ignore_all_space = 0;
2472 dview->opt.ignore_case = 0;
2474 dview_compute_areas (dview);
2476 return 0;
2479 /* --------------------------------------------------------------------------------------------- */
2481 static void
2482 dview_fini (WDiff * dview)
2484 if (dview->dsrc != DATA_SRC_MEM)
2486 f_close (dview->f[DIFF_RIGHT]);
2487 f_close (dview->f[DIFF_LEFT]);
2490 if (dview->converter != str_cnv_from_term)
2491 str_close_conv (dview->converter);
2493 destroy_hdiff (dview);
2494 if (dview->a[DIFF_LEFT] != NULL)
2496 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2497 g_array_free (dview->a[DIFF_LEFT], TRUE);
2498 dview->a[DIFF_LEFT] = NULL;
2500 if (dview->a[DIFF_RIGHT] != NULL)
2502 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2503 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2504 dview->a[DIFF_RIGHT] = NULL;
2507 g_free (dview->label[DIFF_LEFT]);
2508 g_free (dview->label[DIFF_RIGHT]);
2511 /* --------------------------------------------------------------------------------------------- */
2513 static int
2514 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2516 size_t i, k;
2517 int j;
2518 char buf[BUFSIZ];
2519 FBUF *f = dview->f[ord];
2520 int skip = dview->skip_cols;
2521 int display_symbols = dview->display_symbols;
2522 int display_numbers = dview->display_numbers;
2523 int show_cr = dview->show_cr;
2524 int tab_size = 8;
2525 const DIFFLN *p;
2526 int nwidth = display_numbers;
2527 int xwidth;
2529 xwidth = display_symbols + display_numbers;
2530 if (dview->tab_size > 0 && dview->tab_size < 9)
2531 tab_size = dview->tab_size;
2533 if (xwidth != 0)
2535 if (xwidth > width && display_symbols)
2537 xwidth--;
2538 display_symbols = 0;
2540 if (xwidth > width && display_numbers)
2542 xwidth = width;
2543 display_numbers = width;
2546 xwidth++;
2547 c += xwidth;
2548 width -= xwidth;
2549 if (width < 0)
2550 width = 0;
2553 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2555 /* abnormal, but avoid buffer overflow */
2556 return -1;
2559 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2561 int ch, next_ch, col;
2562 size_t cnt;
2564 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2565 ch = p->ch;
2566 tty_setcolor (NORMAL_COLOR);
2567 if (display_symbols)
2569 tty_gotoyx (r + j, c - 2);
2570 tty_print_char (ch);
2572 if (p->line != 0)
2574 if (display_numbers)
2576 tty_gotoyx (r + j, c - xwidth);
2577 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2578 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2580 if (ch == ADD_CH)
2581 tty_setcolor (DFF_ADD_COLOR);
2582 if (ch == CHG_CH)
2583 tty_setcolor (DFF_CHG_COLOR);
2584 if (f == NULL)
2586 if (i == (size_t) dview->search.last_found_line)
2587 tty_setcolor (MARKED_SELECTED_COLOR);
2588 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2590 char att[BUFSIZ];
2592 if (dview->utf8)
2593 k = dview_str_utf8_offset_to_pos (p->p, width);
2594 else
2595 k = width;
2597 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2598 g_ptr_array_index (dview->hdiff, i), ord, att);
2599 tty_gotoyx (r + j, c);
2600 col = 0;
2602 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2604 gboolean ch_res;
2606 if (dview->utf8)
2608 int ch_len;
2610 next_ch = dview_get_utf (buf + cnt, &ch_len, &ch_res);
2611 if (ch_len > 1)
2612 cnt += ch_len - 1;
2613 if (!g_unichar_isprint (next_ch))
2614 next_ch = '.';
2616 else
2617 next_ch = dview_get_byte (buf + cnt, &ch_res);
2619 if (ch_res)
2621 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2622 #ifdef HAVE_CHARSET
2623 if (mc_global.utf8_display)
2625 if (!dview->utf8)
2627 next_ch =
2628 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2629 dview->converter);
2632 else if (dview->utf8)
2633 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2634 else
2635 next_ch = convert_to_display_c (next_ch);
2636 #endif
2637 tty_print_anychar (next_ch);
2638 col++;
2641 continue;
2644 if (ch == CHG_CH)
2645 tty_setcolor (DFF_CHH_COLOR);
2647 if (dview->utf8)
2648 k = dview_str_utf8_offset_to_pos (p->p, width);
2649 else
2650 k = width;
2651 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2653 else
2654 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2656 else
2658 if (display_numbers)
2660 tty_gotoyx (r + j, c - xwidth);
2661 memset (buf, ' ', display_numbers);
2662 buf[display_numbers] = '\0';
2663 tty_print_string (buf);
2665 if (ch == DEL_CH)
2666 tty_setcolor (DFF_DEL_COLOR);
2667 if (ch == CHG_CH)
2668 tty_setcolor (DFF_CHD_COLOR);
2669 memset (buf, ' ', width);
2670 buf[width] = '\0';
2672 tty_gotoyx (r + j, c);
2673 /* tty_print_nstring (buf, width); */
2674 col = 0;
2675 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2677 gboolean ch_res;
2679 if (dview->utf8)
2681 int ch_len;
2683 next_ch = dview_get_utf (buf + cnt, &ch_len, &ch_res);
2684 if (ch_len > 1)
2685 cnt += ch_len - 1;
2686 if (!g_unichar_isprint (next_ch))
2687 next_ch = '.';
2689 else
2690 next_ch = dview_get_byte (buf + cnt, &ch_res);
2691 if (ch_res)
2693 #ifdef HAVE_CHARSET
2694 if (mc_global.utf8_display)
2696 if (!dview->utf8)
2698 next_ch =
2699 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2702 else if (dview->utf8)
2703 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2704 else
2705 next_ch = convert_to_display_c (next_ch);
2706 #endif
2708 tty_print_anychar (next_ch);
2709 col++;
2713 tty_setcolor (NORMAL_COLOR);
2714 k = width;
2715 if (width < xwidth - 1)
2716 k = xwidth - 1;
2717 memset (buf, ' ', k);
2718 buf[k] = '\0';
2719 for (; j < height; j++)
2721 if (xwidth != 0)
2723 tty_gotoyx (r + j, c - xwidth);
2724 /* tty_print_nstring (buf, xwidth - 1); */
2725 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2727 tty_gotoyx (r + j, c);
2728 /* tty_print_nstring (buf, width); */
2729 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2732 return 0;
2735 /* --------------------------------------------------------------------------------------------- */
2737 static void
2738 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2740 const char *buf;
2741 int filename_width;
2742 int linenum, lineofs;
2743 vfs_path_t *vpath;
2744 char *path;
2746 tty_setcolor (STATUSBAR_COLOR);
2748 tty_gotoyx (0, c);
2749 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2751 filename_width = width - 24;
2752 if (filename_width < 8)
2753 filename_width = 8;
2755 vpath = vfs_path_from_str (dview->label[ord]);
2756 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2757 vfs_path_free (vpath);
2758 buf = str_term_trim (path, filename_width);
2759 if (ord == DIFF_LEFT)
2760 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2761 buf, linenum, lineofs, dview->skip_cols);
2762 else
2763 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2764 buf, linenum, lineofs, dview->ndiff);
2765 g_free (path);
2768 /* --------------------------------------------------------------------------------------------- */
2770 static void
2771 dview_redo (WDiff * dview)
2773 if (dview->display_numbers)
2775 int old;
2777 old = dview->display_numbers;
2778 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2779 dview->new_frame = (old != dview->display_numbers);
2781 dview_reread (dview);
2784 /* --------------------------------------------------------------------------------------------- */
2786 static void
2787 dview_update (WDiff * dview)
2789 int height = dview->height;
2790 int width1;
2791 int width2;
2792 int last;
2794 last = dview->a[DIFF_LEFT]->len - 1;
2796 if (dview->skip_rows > last)
2797 dview->skip_rows = dview->search.last_accessed_num_line = last;
2798 if (dview->skip_rows < 0)
2799 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2800 if (dview->skip_cols < 0)
2801 dview->skip_cols = 0;
2803 if (height < 2)
2804 return;
2806 width1 = dview->half1 + dview->bias;
2807 width2 = dview->half2 - dview->bias;
2808 if (dview->full)
2810 width1 = COLS;
2811 width2 = 0;
2814 if (dview->new_frame)
2816 int xwidth;
2818 tty_setcolor (NORMAL_COLOR);
2819 xwidth = dview->display_symbols + dview->display_numbers;
2820 if (width1 > 1)
2821 tty_draw_box (1, 0, height, width1, FALSE);
2822 if (width2 > 1)
2823 tty_draw_box (1, width1, height, width2, FALSE);
2825 if (xwidth != 0)
2827 xwidth++;
2828 if (xwidth < width1 - 1)
2830 tty_gotoyx (1, xwidth);
2831 tty_print_alt_char (ACS_TTEE, FALSE);
2832 tty_gotoyx (height, xwidth);
2833 tty_print_alt_char (ACS_BTEE, FALSE);
2834 tty_draw_vline (2, xwidth, ACS_VLINE, height - 2);
2836 if (xwidth < width2 - 1)
2838 tty_gotoyx (1, width1 + xwidth);
2839 tty_print_alt_char (ACS_TTEE, FALSE);
2840 tty_gotoyx (height, width1 + xwidth);
2841 tty_print_alt_char (ACS_BTEE, FALSE);
2842 tty_draw_vline (2, width1 + xwidth, ACS_VLINE, height - 2);
2845 dview->new_frame = 0;
2848 if (width1 > 2)
2850 dview_status (dview, dview->ord, width1, 0);
2851 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2853 if (width2 > 2)
2855 dview_status (dview, dview->ord ^ 1, width2, width1);
2856 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2860 /* --------------------------------------------------------------------------------------------- */
2862 static void
2863 dview_edit (WDiff * dview, diff_place_t ord)
2865 WDialog *h;
2866 gboolean h_modal;
2867 int linenum, lineofs;
2869 if (dview->dsrc == DATA_SRC_TMP)
2871 error_dialog (_("Edit"), _("Edit is disabled"));
2872 return;
2875 h = WIDGET (dview)->owner;
2876 h_modal = h->modal;
2878 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2879 h->modal = TRUE; /* not allow edit file in several editors */
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 != 0, linenum);
2885 vfs_path_free (tmp_vpath);
2887 h->modal = h_modal;
2888 dview_redo (dview);
2889 dview_update (dview);
2892 /* --------------------------------------------------------------------------------------------- */
2894 static void
2895 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2897 /* *INDENT-OFF* */
2898 static const char *title[2] = {
2899 N_("Goto line (left)"),
2900 N_("Goto line (right)")
2902 /* *INDENT-ON* */
2903 static char prev[256];
2904 /* XXX some statics here, to be remembered between runs */
2906 int newline;
2907 char *input;
2909 input =
2910 input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE, prev,
2911 INPUT_COMPLETE_NONE);
2912 if (input != NULL)
2914 const char *s = input;
2916 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2918 size_t i = 0;
2920 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;
2932 g_snprintf (prev, sizeof (prev), "%d", newline);
2934 g_free (input);
2938 /* --------------------------------------------------------------------------------------------- */
2940 static void
2941 dview_labels (WDiff * dview)
2943 Widget *d;
2944 WDialog *h;
2945 WButtonBar *b;
2947 d = WIDGET (dview);
2948 h = d->owner;
2949 b = find_buttonbar (h);
2951 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, d);
2952 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, d);
2953 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, d);
2954 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, d);
2955 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, d);
2956 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, d);
2957 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, d);
2960 /* --------------------------------------------------------------------------------------------- */
2962 static int
2963 dview_event (Gpm_Event * event, void *data)
2965 WDiff *dview = (WDiff *) data;
2967 if (!mouse_global_in_widget (event, data))
2968 return MOU_UNHANDLED;
2970 /* We are not interested in release events */
2971 if ((event->type & (GPM_DOWN | GPM_DRAG)) == 0)
2972 return MOU_NORMAL;
2974 /* Wheel events */
2975 if ((event->buttons & GPM_B_UP) != 0 && (event->type & GPM_DOWN) != 0)
2977 dview->skip_rows -= 2;
2978 dview->search.last_accessed_num_line = dview->skip_rows;
2979 dview_update (dview);
2981 else if ((event->buttons & GPM_B_DOWN) != 0 && (event->type & GPM_DOWN) != 0)
2983 dview->skip_rows += 2;
2984 dview->search.last_accessed_num_line = dview->skip_rows;
2985 dview_update (dview);
2988 return MOU_NORMAL;
2991 /* --------------------------------------------------------------------------------------------- */
2993 static gboolean
2994 dview_save (WDiff * dview)
2996 gboolean res = TRUE;
2998 if (dview->merged[DIFF_LEFT])
3000 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3001 dview->merged[DIFF_LEFT] = !res;
3003 if (dview->merged[DIFF_RIGHT])
3005 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3006 dview->merged[DIFF_RIGHT] = !res;
3008 return res;
3011 /* --------------------------------------------------------------------------------------------- */
3013 static void
3014 dview_do_save (WDiff * dview)
3016 (void) dview_save (dview);
3019 /* --------------------------------------------------------------------------------------------- */
3021 static void
3022 dview_save_options (WDiff * dview)
3024 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
3025 dview->display_symbols != 0 ? TRUE : FALSE);
3026 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
3027 dview->display_numbers != 0 ? TRUE : FALSE);
3028 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
3030 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
3032 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
3033 dview->opt.strip_trailing_cr);
3034 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
3035 dview->opt.ignore_all_space);
3036 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
3037 dview->opt.ignore_space_change);
3038 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
3039 dview->opt.ignore_tab_expansion);
3040 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
3043 /* --------------------------------------------------------------------------------------------- */
3045 static void
3046 dview_load_options (WDiff * dview)
3048 gboolean show_numbers, show_symbols;
3049 int tab_size;
3051 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
3052 if (show_symbols)
3053 dview->display_symbols = 1;
3054 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
3055 if (show_numbers)
3056 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
3057 tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
3058 if (tab_size > 0 && tab_size < 9)
3059 dview->tab_size = tab_size;
3060 else
3061 dview->tab_size = 8;
3063 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
3065 dview->opt.strip_trailing_cr =
3066 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
3067 dview->opt.ignore_all_space =
3068 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
3069 dview->opt.ignore_space_change =
3070 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
3071 dview->opt.ignore_tab_expansion =
3072 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
3073 dview->opt.ignore_case =
3074 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
3076 dview->new_frame = 1;
3079 /* --------------------------------------------------------------------------------------------- */
3082 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3083 * ask user.
3085 static gboolean
3086 dview_ok_to_exit (WDiff * dview)
3088 gboolean res = TRUE;
3089 int act;
3091 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3092 return res;
3094 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3095 _("File(s) was modified. Save with exit?") :
3096 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3097 D_NORMAL, 2, _("&Yes"), _("&No"));
3099 /* Esc is No */
3100 if (mc_global.midnight_shutdown || (act == -1))
3101 act = 1;
3103 switch (act)
3105 case -1: /* Esc */
3106 res = FALSE;
3107 break;
3108 case 0: /* Yes */
3109 (void) dview_save (dview);
3110 res = TRUE;
3111 break;
3112 case 1: /* No */
3113 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3114 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3115 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3116 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3117 /* fall through */
3118 default:
3119 res = TRUE;
3120 break;
3122 return res;
3125 /* --------------------------------------------------------------------------------------------- */
3127 static cb_ret_t
3128 dview_execute_cmd (WDiff * dview, unsigned long command)
3130 cb_ret_t res = MSG_HANDLED;
3132 switch (command)
3134 case CK_ShowSymbols:
3135 dview->display_symbols ^= 1;
3136 dview->new_frame = 1;
3137 break;
3138 case CK_ShowNumbers:
3139 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
3140 dview->new_frame = 1;
3141 break;
3142 case CK_SplitFull:
3143 dview->full ^= 1;
3144 dview->new_frame = 1;
3145 break;
3146 case CK_SplitEqual:
3147 if (!dview->full)
3149 dview->bias = 0;
3150 dview->new_frame = 1;
3152 break;
3153 case CK_SplitMore:
3154 if (!dview->full)
3156 dview_compute_split (dview, 1);
3157 dview->new_frame = 1;
3159 break;
3161 case CK_SplitLess:
3162 if (!dview->full)
3164 dview_compute_split (dview, -1);
3165 dview->new_frame = 1;
3167 break;
3168 case CK_Tab2:
3169 dview->tab_size = 2;
3170 break;
3171 case CK_Tab3:
3172 dview->tab_size = 3;
3173 break;
3174 case CK_Tab4:
3175 dview->tab_size = 4;
3176 break;
3177 case CK_Tab8:
3178 dview->tab_size = 8;
3179 break;
3180 case CK_Swap:
3181 dview->ord ^= 1;
3182 break;
3183 case CK_Redo:
3184 dview_redo (dview);
3185 break;
3186 case CK_HunkNext:
3187 dview->skip_rows = dview->search.last_accessed_num_line =
3188 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3189 break;
3190 case CK_HunkPrev:
3191 dview->skip_rows = dview->search.last_accessed_num_line =
3192 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3193 break;
3194 case CK_Goto:
3195 dview_goto_cmd (dview, TRUE);
3196 break;
3197 case CK_Edit:
3198 dview_edit (dview, dview->ord);
3199 break;
3200 case CK_Merge:
3201 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3202 dview_redo (dview);
3203 break;
3204 case CK_MergeOther:
3205 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3206 dview_redo (dview);
3207 break;
3208 case CK_EditOther:
3209 dview_edit (dview, dview->ord ^ 1);
3210 break;
3211 case CK_Search:
3212 dview_search_cmd (dview);
3213 break;
3214 case CK_SearchContinue:
3215 dview_continue_search_cmd (dview);
3216 break;
3217 case CK_Top:
3218 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3219 break;
3220 case CK_Bottom:
3221 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3222 break;
3223 case CK_Up:
3224 if (dview->skip_rows > 0)
3226 dview->skip_rows--;
3227 dview->search.last_accessed_num_line = dview->skip_rows;
3229 break;
3230 case CK_Down:
3231 dview->skip_rows++;
3232 dview->search.last_accessed_num_line = dview->skip_rows;
3233 break;
3234 case CK_PageDown:
3235 if (dview->height > 2)
3237 dview->skip_rows += dview->height - 2;
3238 dview->search.last_accessed_num_line = dview->skip_rows;
3240 break;
3241 case CK_PageUp:
3242 if (dview->height > 2)
3244 dview->skip_rows -= dview->height - 2;
3245 dview->search.last_accessed_num_line = dview->skip_rows;
3247 break;
3248 case CK_Left:
3249 dview->skip_cols--;
3250 break;
3251 case CK_Right:
3252 dview->skip_cols++;
3253 break;
3254 case CK_LeftQuick:
3255 dview->skip_cols -= 8;
3256 break;
3257 case CK_RightQuick:
3258 dview->skip_cols += 8;
3259 break;
3260 case CK_Home:
3261 dview->skip_cols = 0;
3262 break;
3263 case CK_Shell:
3264 view_other_cmd ();
3265 break;
3266 case CK_Quit:
3267 dview->view_quit = 1;
3268 break;
3269 case CK_Save:
3270 dview_do_save (dview);
3271 break;
3272 case CK_Options:
3273 dview_diff_options (dview);
3274 break;
3275 #ifdef HAVE_CHARSET
3276 case CK_SelectCodepage:
3277 dview_select_encoding (dview);
3278 break;
3279 #endif
3280 case CK_Cancel:
3281 /* don't close diffviewer due to SIGINT */
3282 break;
3283 default:
3284 res = MSG_NOT_HANDLED;
3286 return res;
3289 /* --------------------------------------------------------------------------------------------- */
3291 static cb_ret_t
3292 dview_handle_key (WDiff * dview, int key)
3294 unsigned long command;
3296 #ifdef HAVE_CHARSET
3297 key = convert_from_input_c (key);
3298 #endif
3300 command = keybind_lookup_keymap_command (diff_map, key);
3301 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3302 return MSG_HANDLED;
3304 /* Key not used */
3305 return MSG_NOT_HANDLED;
3308 /* --------------------------------------------------------------------------------------------- */
3310 static cb_ret_t
3311 dview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3313 WDiff *dview = (WDiff *) w;
3314 WDialog *h = w->owner;
3315 cb_ret_t i;
3317 switch (msg)
3319 case MSG_INIT:
3320 dview_labels (dview);
3321 dview_load_options (dview);
3322 dview_update (dview);
3323 return MSG_HANDLED;
3325 case MSG_DRAW:
3326 dview->new_frame = 1;
3327 dview_update (dview);
3328 return MSG_HANDLED;
3330 case MSG_KEY:
3331 i = dview_handle_key (dview, parm);
3332 if (dview->view_quit)
3333 dlg_stop (h);
3334 else
3335 dview_update (dview);
3336 return i;
3338 case MSG_ACTION:
3339 i = dview_execute_cmd (dview, parm);
3340 if (dview->view_quit)
3341 dlg_stop (h);
3342 else
3343 dview_update (dview);
3344 return i;
3346 case MSG_DESTROY:
3347 dview_save_options (dview);
3348 dview_fini (dview);
3349 return MSG_HANDLED;
3351 default:
3352 return widget_default_callback (w, sender, msg, parm, data);
3356 /* --------------------------------------------------------------------------------------------- */
3358 static void
3359 dview_adjust_size (WDialog * h)
3361 WDiff *dview;
3362 WButtonBar *bar;
3364 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3365 dview = (WDiff *) find_widget_type (h, dview_callback);
3366 bar = find_buttonbar (h);
3367 widget_set_size (WIDGET (dview), 0, 0, LINES - 1, COLS);
3368 widget_set_size (WIDGET (bar), LINES - 1, 0, 1, COLS);
3370 dview_compute_areas (dview);
3373 /* --------------------------------------------------------------------------------------------- */
3375 static cb_ret_t
3376 dview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3378 WDiff *dview = (WDiff *) data;
3379 WDialog *h = DIALOG (w);
3381 switch (msg)
3383 case MSG_RESIZE:
3384 dview_adjust_size (h);
3385 return MSG_HANDLED;
3387 case MSG_ACTION:
3388 /* shortcut */
3389 if (sender == NULL)
3390 return dview_execute_cmd (NULL, parm);
3391 /* message from buttonbar */
3392 if (sender == WIDGET (find_buttonbar (h)))
3394 if (data != NULL)
3395 return send_message (data, NULL, MSG_ACTION, parm, NULL);
3397 dview = (WDiff *) find_widget_type (h, dview_callback);
3398 return dview_execute_cmd (dview, parm);
3400 return MSG_NOT_HANDLED;
3402 case MSG_VALIDATE:
3403 dview = (WDiff *) find_widget_type (h, dview_callback);
3404 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
3405 if (dview_ok_to_exit (dview))
3406 h->state = DLG_CLOSED;
3407 return MSG_HANDLED;
3409 default:
3410 return dlg_default_callback (w, sender, msg, parm, data);
3414 /* --------------------------------------------------------------------------------------------- */
3416 static char *
3417 dview_get_title (const WDialog * h, size_t len)
3419 const WDiff *dview;
3420 const char *modified = " (*) ";
3421 const char *notmodified = " ";
3422 size_t len1;
3423 GString *title;
3425 dview = (const WDiff *) find_widget_type (h, dview_callback);
3426 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3428 title = g_string_sized_new (len);
3429 g_string_append (title, _("Diff:"));
3430 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3431 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3432 g_string_append (title, " | ");
3433 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3434 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3436 return g_string_free (title, FALSE);
3439 /* --------------------------------------------------------------------------------------------- */
3441 static int
3442 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3444 int error;
3445 WDiff *dview;
3446 Widget *w;
3447 WDialog *dview_dlg;
3449 /* Create dialog and widgets, put them on the dialog */
3450 dview_dlg =
3451 dlg_create (FALSE, 0, 0, LINES, COLS, NULL, dview_dialog_callback, NULL,
3452 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3454 dview = g_new0 (WDiff, 1);
3455 w = WIDGET (dview);
3456 widget_init (w, 0, 0, LINES - 1, COLS, dview_callback, dview_event);
3457 widget_want_cursor (w, FALSE);
3459 add_widget (dview_dlg, dview);
3460 add_widget (dview_dlg, buttonbar_new (TRUE));
3462 dview_dlg->get_title = dview_get_title;
3464 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3466 /* Please note that if you add another widget,
3467 * you have to modify dview_adjust_size to
3468 * be aware of it
3470 if (error == 0)
3471 dlg_run (dview_dlg);
3473 if ((error != 0) || (dview_dlg->state == DLG_CLOSED))
3474 dlg_destroy (dview_dlg);
3476 return error == 0 ? 1 : 0;
3479 /*** public functions ****************************************************************************/
3480 /* --------------------------------------------------------------------------------------------- */
3482 #define GET_FILE_AND_STAMP(n) \
3483 do \
3485 use_copy##n = 0; \
3486 real_file##n = file##n; \
3487 if (!vfs_file_is_local (file##n)) \
3489 real_file##n = mc_getlocalcopy (file##n); \
3490 if (real_file##n != NULL) \
3492 use_copy##n = 1; \
3493 if (mc_stat (real_file##n, &st##n) != 0) \
3494 use_copy##n = -1; \
3498 while (0)
3500 #define UNGET_FILE(n) \
3501 do \
3503 if (use_copy##n) \
3505 int changed = 0; \
3506 if (use_copy##n > 0) \
3508 time_t mtime; \
3509 mtime = st##n.st_mtime; \
3510 if (mc_stat (real_file##n, &st##n) == 0) \
3511 changed = (mtime != st##n.st_mtime); \
3513 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3514 vfs_path_free (real_file##n); \
3517 while (0)
3519 gboolean
3520 dview_diff_cmd (const void *f0, const void *f1)
3522 int rv = 0;
3523 vfs_path_t *file0 = NULL;
3524 vfs_path_t *file1 = NULL;
3525 gboolean is_dir0 = FALSE;
3526 gboolean is_dir1 = FALSE;
3528 switch (mc_global.mc_run_mode)
3530 case MC_RUN_FULL:
3532 /* run from panels */
3533 const WPanel *panel0 = (const WPanel *) f0;
3534 const WPanel *panel1 = (const WPanel *) f1;
3536 file0 = vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, NULL);
3537 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3538 if (is_dir0)
3540 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3541 path_trunc (selection (panel0)->fname, 30));
3542 goto ret;
3545 file1 = vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, NULL);
3546 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3547 if (is_dir1)
3549 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3550 path_trunc (selection (panel1)->fname, 30));
3551 goto ret;
3553 break;
3556 case MC_RUN_DIFFVIEWER:
3558 /* run from command line */
3559 const char *p0 = (const char *) f0;
3560 const char *p1 = (const char *) f1;
3561 struct stat st;
3563 file0 = vfs_path_from_str (p0);
3564 if (mc_stat (file0, &st) == 0)
3566 is_dir0 = S_ISDIR (st.st_mode);
3567 if (is_dir0)
3569 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3570 goto ret;
3573 else
3575 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3576 path_trunc (p0, 30), unix_error_string (errno));
3577 goto ret;
3580 file1 = vfs_path_from_str (p1);
3581 if (mc_stat (file1, &st) == 0)
3583 is_dir1 = S_ISDIR (st.st_mode);
3584 if (is_dir1)
3586 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3587 goto ret;
3590 else
3592 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3593 path_trunc (p1, 30), unix_error_string (errno));
3594 goto ret;
3596 break;
3599 default:
3600 /* this should not happaned */
3601 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3602 return FALSE;
3605 if (rv == 0)
3607 rv = -1;
3608 if (file0 != NULL && file1 != NULL)
3610 int use_copy0;
3611 int use_copy1;
3612 struct stat st0;
3613 struct stat st1;
3614 vfs_path_t *real_file0;
3615 vfs_path_t *real_file1;
3617 GET_FILE_AND_STAMP (0);
3618 GET_FILE_AND_STAMP (1);
3620 if (real_file0 != NULL && real_file1 != NULL)
3621 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3622 vfs_path_as_str (file0), vfs_path_as_str (file1));
3624 UNGET_FILE (1);
3625 UNGET_FILE (0);
3629 if (rv == 0)
3630 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3632 ret:
3633 vfs_path_free (file1);
3634 vfs_path_free (file0);
3636 return (rv != 0);
3639 #undef GET_FILE_AND_STAMP
3640 #undef UNGET_FILE
3642 /* --------------------------------------------------------------------------------------------- */