68add475f9e5aa6c547fc6c129c625449633fd9b
[midnight-commander.git] / src / diffviewer / ydiff.c
blob68add475f9e5aa6c547fc6c129c625449633fd9b
1 /*
2 Copyright (C) 2007, 2010, 2011
3 The Free Software Foundation, Inc.
5 Written by:
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010
8 Andrew Borodin <aborodin@vmail.ru>, 2010
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" /* do_edit_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;\
73 TP *g_array_foreach_var = NULL; \
74 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
75 { \
76 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
77 (*cbf) (g_array_foreach_var); \
78 } \
79 } while (0)
81 #define FILE_READ_BUF 4096
82 #define FILE_FLAG_TEMP (1 << 0)
84 #define OPTX 56
85 #define OPTY 17
87 #define ADD_CH '+'
88 #define DEL_CH '-'
89 #define CHG_CH '*'
90 #define EQU_CH ' '
92 #define HDIFF_ENABLE 1
93 #define HDIFF_MINCTX 5
94 #define HDIFF_DEPTH 10
96 #define FILE_DIRTY(fs) \
97 do \
98 { \
99 (fs)->pos = 0; \
100 (fs)->len = 0; \
102 while (0)
104 /*** file scope type declarations ****************************************************************/
106 typedef enum
108 FROM_LEFT_TO_RIGHT,
109 FROM_RIGHT_TO_LEFT
110 } action_direction_t;
112 /*** file scope variables ************************************************************************/
114 /*** file scope functions ************************************************************************/
115 /* --------------------------------------------------------------------------------------------- */
117 static inline int
118 TAB_SKIP (int ts, int pos)
120 if (ts > 0 && ts < 9)
121 return ts - pos % ts;
122 else
123 return 8 - pos % 8;
126 /* --------------------------------------------------------------------------------------------- */
128 static gboolean
129 rewrite_backup_content (const vfs_path_t * from_file_name_vpath, const char *to_file_name)
131 FILE *backup_fd;
132 char *contents;
133 gsize length;
134 const char *from_file_name;
136 from_file_name = vfs_path_get_by_index (from_file_name_vpath, -1)->path;
137 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
138 return FALSE;
140 backup_fd = fopen (to_file_name, "w");
141 if (backup_fd == NULL)
143 g_free (contents);
144 return FALSE;
147 length = fwrite ((const void *) contents, length, 1, backup_fd);
149 fflush (backup_fd);
150 fclose (backup_fd);
151 g_free (contents);
152 return TRUE;
155 /* buffered I/O ************************************************************* */
158 * Try to open a temporary file.
159 * @note the name is not altered if this function fails
161 * @param[out] name address of a pointer to store the temporary name
162 * @returns file descriptor on success, negative on error
165 static int
166 open_temp (void **name)
168 int fd;
169 vfs_path_t *diff_file_name = NULL;
171 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
172 if (fd == -1)
174 message (D_ERROR, MSG_ERROR,
175 _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
176 return -1;
178 *name = vfs_path_to_str (diff_file_name);
179 vfs_path_free (diff_file_name);
180 return fd;
183 /* --------------------------------------------------------------------------------------------- */
186 * Alocate file structure and associate file descriptor to it.
188 * @param fd file descriptor
189 * @returns file structure
192 static FBUF *
193 f_dopen (int fd)
195 FBUF *fs;
197 if (fd < 0)
198 return NULL;
200 fs = g_try_malloc (sizeof (FBUF));
201 if (fs == NULL)
202 return NULL;
204 fs->buf = g_try_malloc (FILE_READ_BUF);
205 if (fs->buf == NULL)
207 g_free (fs);
208 return NULL;
211 fs->fd = fd;
212 FILE_DIRTY (fs);
213 fs->flags = 0;
214 fs->data = NULL;
216 return fs;
219 /* --------------------------------------------------------------------------------------------- */
222 * Free file structure without closing the file.
224 * @param fs file structure
225 * @returns 0 on success, non-zero on error
228 static int
229 f_free (FBUF * fs)
231 int rv = 0;
233 if (fs->flags & FILE_FLAG_TEMP)
235 rv = unlink (fs->data);
236 g_free (fs->data);
238 g_free (fs->buf);
239 g_free (fs);
240 return rv;
243 /* --------------------------------------------------------------------------------------------- */
246 * Open a binary temporary file in R/W mode.
247 * @note the file will be deleted when closed
249 * @returns file structure
252 static FBUF *
253 f_temp (void)
255 int fd;
256 FBUF *fs;
258 fs = f_dopen (0);
259 if (fs == NULL)
260 return NULL;
262 fd = open_temp (&fs->data);
263 if (fd < 0)
265 f_free (fs);
266 return NULL;
269 fs->fd = fd;
270 fs->flags = FILE_FLAG_TEMP;
271 return fs;
274 /* --------------------------------------------------------------------------------------------- */
277 * Open a binary file in specified mode.
279 * @param filename file name
280 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
282 * @returns file structure
285 static FBUF *
286 f_open (const char *filename, int flags)
288 int fd;
289 FBUF *fs;
291 fs = f_dopen (0);
292 if (fs == NULL)
293 return NULL;
295 fd = open (filename, flags);
296 if (fd < 0)
298 f_free (fs);
299 return NULL;
302 fs->fd = fd;
303 return fs;
306 /* --------------------------------------------------------------------------------------------- */
309 * Read a line of bytes from file until newline or EOF.
310 * @note does not stop on null-byte
311 * @note buf will not be null-terminated
313 * @param buf destination buffer
314 * @param size size of buffer
315 * @param fs file structure
317 * @returns number of bytes read
321 static size_t
322 f_gets (char *buf, size_t size, FBUF * fs)
324 size_t j = 0;
328 int i;
329 int stop = 0;
331 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
333 buf[j] = fs->buf[i];
334 if (buf[j] == '\n')
335 stop = 1;
337 fs->pos = i;
339 if (j == size || stop)
340 break;
342 fs->pos = 0;
343 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
345 while (fs->len > 0);
347 return j;
350 /* --------------------------------------------------------------------------------------------- */
353 * Seek into file.
354 * @note avoids thrashing read cache when possible
356 * @param fs file structure
357 * @param off offset
358 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
360 * @returns position in file, starting from begginning
364 static off_t
365 f_seek (FBUF * fs, off_t off, int whence)
367 off_t rv;
369 if (fs->len && whence != SEEK_END)
371 rv = lseek (fs->fd, 0, SEEK_CUR);
372 if (rv != -1)
374 if (whence == SEEK_CUR)
376 whence = SEEK_SET;
377 off += rv - fs->len + fs->pos;
379 if (off - rv >= -fs->len && off - rv <= 0)
381 fs->pos = fs->len + off - rv;
382 return off;
387 rv = lseek (fs->fd, off, whence);
388 if (rv != -1)
389 FILE_DIRTY (fs);
390 return rv;
393 /* --------------------------------------------------------------------------------------------- */
396 * Seek to the beginning of file, thrashing read cache.
398 * @param fs file structure
400 * @returns 0 if success, non-zero on error
403 static off_t
404 f_reset (FBUF * fs)
406 off_t rv;
408 rv = lseek (fs->fd, 0, SEEK_SET);
409 if (rv != -1)
410 FILE_DIRTY (fs);
411 return rv;
414 /* --------------------------------------------------------------------------------------------- */
417 * Write bytes to file.
418 * @note thrashes read cache
420 * @param fs file structure
421 * @param buf source buffer
422 * @param size size of buffer
424 * @returns number of written bytes, -1 on error
428 static ssize_t
429 f_write (FBUF * fs, const char *buf, size_t size)
431 ssize_t rv;
433 rv = write (fs->fd, buf, size);
434 if (rv >= 0)
435 FILE_DIRTY (fs);
436 return rv;
439 /* --------------------------------------------------------------------------------------------- */
442 * Truncate file to the current position.
443 * @note thrashes read cache
445 * @param fs file structure
447 * @returns current file size on success, negative on error
451 static off_t
452 f_trunc (FBUF * fs)
454 off_t off;
456 off = lseek (fs->fd, 0, SEEK_CUR);
457 if (off != -1)
459 int rv;
461 rv = ftruncate (fs->fd, off);
462 if (rv != 0)
463 off = -1;
464 else
465 FILE_DIRTY (fs);
467 return off;
470 /* --------------------------------------------------------------------------------------------- */
473 * Close file.
474 * @note if this is temporary file, it is deleted
476 * @param fs file structure
477 * @returns 0 on success, non-zero on error
481 static int
482 f_close (FBUF * fs)
484 int rv = -1;
486 if (fs != NULL)
488 rv = close (fs->fd);
489 f_free (fs);
492 return rv;
495 /* --------------------------------------------------------------------------------------------- */
498 * Create pipe stream to process.
500 * @param cmd shell command line
501 * @param flags open mode, either O_RDONLY or O_WRONLY
503 * @returns file structure
506 static FBUF *
507 p_open (const char *cmd, int flags)
509 FILE *f;
510 FBUF *fs;
511 const char *type = NULL;
513 if (flags == O_RDONLY)
514 type = "r";
515 else if (flags == O_WRONLY)
516 type = "w";
518 if (type == NULL)
519 return NULL;
521 fs = f_dopen (0);
522 if (fs == NULL)
523 return NULL;
525 f = popen (cmd, type);
526 if (f == NULL)
528 f_free (fs);
529 return NULL;
532 fs->fd = fileno (f);
533 fs->data = f;
534 return fs;
537 /* --------------------------------------------------------------------------------------------- */
540 * Close pipe stream.
542 * @param fs structure
543 * @returns 0 on success, non-zero on error
546 static int
547 p_close (FBUF * fs)
549 int rv = -1;
551 if (fs != NULL)
553 rv = pclose (fs->data);
554 f_free (fs);
557 return rv;
560 /* --------------------------------------------------------------------------------------------- */
563 * Get one char (byte) from string
565 * @param char * str, gboolean * result
566 * @returns int as character or 0 and result == FALSE if fail
569 static int
570 dview_get_byte (char *str, gboolean * result)
572 if (str == NULL)
574 *result = FALSE;
575 return 0;
577 *result = TRUE;
578 return (unsigned char) *str;
581 /* --------------------------------------------------------------------------------------------- */
584 * Get utf multibyte char from string
586 * @param char * str, int * char_width, gboolean * result
587 * @returns int as utf character or 0 and result == FALSE if fail
591 static int
592 dview_get_utf (char *str, int *char_width, gboolean * result)
594 int res = -1;
595 gunichar ch;
596 gchar *next_ch = NULL;
597 int width = 0;
599 *result = TRUE;
601 if (str == NULL)
603 *result = FALSE;
604 return 0;
607 res = g_utf8_get_char_validated (str, -1);
609 if (res < 0)
610 ch = *str;
611 else
613 ch = res;
614 /* Calculate UTF-8 char width */
615 next_ch = g_utf8_next_char (str);
616 if (next_ch != NULL)
617 width = next_ch - str;
618 else
619 ch = 0;
621 *char_width = width;
622 return ch;
625 /* --------------------------------------------------------------------------------------------- */
627 static int
628 dview_str_utf8_offset_to_pos (const char *text, size_t length)
630 ptrdiff_t result;
632 if (text == NULL || text[0] == '\0')
633 return length;
635 if (g_utf8_validate (text, -1, NULL))
636 result = g_utf8_offset_to_pointer (text, length) - text;
637 else
639 gunichar uni;
640 char *tmpbuf, *buffer;
642 buffer = tmpbuf = g_strdup (text);
643 while (tmpbuf[0] != '\0')
645 uni = g_utf8_get_char_validated (tmpbuf, -1);
646 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
647 tmpbuf = g_utf8_next_char (tmpbuf);
648 else
650 tmpbuf[0] = '.';
651 tmpbuf++;
654 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
655 g_free (buffer);
657 return max (length, (size_t) result);
660 /* --------------------------------------------------------------------------------------------- */
662 /* diff parse *************************************************************** */
665 * Read decimal number from string.
667 * @param[in,out] str string to parse
668 * @param[out] n extracted number
669 * @returns 0 if success, otherwise non-zero
672 static int
673 scan_deci (const char **str, int *n)
675 const char *p = *str;
676 char *q;
678 errno = 0;
679 *n = strtol (p, &q, 10);
680 if (errno != 0 || p == q)
681 return -1;
682 *str = q;
683 return 0;
686 /* --------------------------------------------------------------------------------------------- */
689 * Parse line for diff statement.
691 * @param p string to parse
692 * @param ops list of diff statements
693 * @returns 0 if success, otherwise non-zero
696 static int
697 scan_line (const char *p, GArray * ops)
699 DIFFCMD op;
701 int f1, f2;
702 int t1, t2;
703 int cmd;
704 int range;
706 /* handle the following cases:
707 * NUMaNUM[,NUM]
708 * NUM[,NUM]cNUM[,NUM]
709 * NUM[,NUM]dNUM
710 * where NUM is a positive integer
713 if (scan_deci (&p, &f1) != 0 || f1 < 0)
714 return -1;
716 f2 = f1;
717 range = 0;
718 if (*p == ',')
720 p++;
721 if (scan_deci (&p, &f2) != 0 || f2 < f1)
722 return -1;
724 range = 1;
727 cmd = *p++;
728 if (cmd == 'a')
730 if (range != 0)
731 return -1;
733 else if (cmd != 'c' && cmd != 'd')
734 return -1;
736 if (scan_deci (&p, &t1) != 0 || t1 < 0)
737 return -1;
739 t2 = t1;
740 range = 0;
741 if (*p == ',')
743 p++;
744 if (scan_deci (&p, &t2) != 0 || t2 < t1)
745 return -1;
747 range = 1;
750 if (cmd == 'd' && range != 0)
751 return -1;
753 op.a[0][0] = f1;
754 op.a[0][1] = f2;
755 op.cmd = cmd;
756 op.a[1][0] = t1;
757 op.a[1][1] = t2;
758 g_array_append_val (ops, op);
759 return 0;
762 /* --------------------------------------------------------------------------------------------- */
765 * Parse diff output and extract diff statements.
767 * @param f stream to read from
768 * @param ops list of diff statements to fill
769 * @returns positive number indicating number of hunks, otherwise negative
772 static int
773 scan_diff (FBUF * f, GArray * ops)
775 int sz;
776 char buf[BUFSIZ];
778 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
780 if (isdigit (buf[0]))
782 if (buf[sz - 1] != '\n')
783 return -1;
785 buf[sz] = '\0';
786 if (scan_line (buf, ops) != 0)
787 return -1;
789 continue;
792 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
796 return ops->len;
799 /* --------------------------------------------------------------------------------------------- */
802 * Invoke diff and extract diff statements.
804 * @param args extra arguments to be passed to diff
805 * @param extra more arguments to be passed to diff
806 * @param file1 first file to compare
807 * @param file2 second file to compare
808 * @param ops list of diff statements to fill
810 * @returns positive number indicating number of hunks, otherwise negative
813 static int
814 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
815 GArray * ops)
817 static const char *opt =
818 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
819 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
820 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
821 " --unchanged-group-format=''";
823 int rv;
824 FBUF *f;
825 char *cmd;
826 int code;
827 char *file1_esc, *file2_esc;
829 /* escape potential $ to avoid shell variable substitutions in popen() */
830 file1_esc = strutils_shell_escape (file1);
831 file2_esc = strutils_shell_escape (file2);
832 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
833 g_free (file1_esc);
834 g_free (file2_esc);
836 if (cmd == NULL)
837 return -1;
839 f = p_open (cmd, O_RDONLY);
840 g_free (cmd);
842 if (f == NULL)
843 return -1;
845 rv = scan_diff (f, ops);
846 code = p_close (f);
848 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
849 rv = -1;
851 return rv;
854 /* --------------------------------------------------------------------------------------------- */
857 * Reparse and display file according to diff statements.
859 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
860 * @param filename file name to display
861 * @param ops list of diff statements
862 * @param printer printf-like function to be used for displaying
863 * @param ctx printer context
865 * @returns 0 if success, otherwise non-zero
868 static int
869 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
871 size_t i;
872 FBUF *f;
873 size_t sz;
874 char buf[BUFSIZ];
875 int line = 0;
876 off_t off = 0;
877 const DIFFCMD *op;
878 diff_place_t eff;
879 int add_cmd;
880 int del_cmd;
882 f = f_open (filename, O_RDONLY);
883 if (f == NULL)
884 return -1;
886 ord &= 1;
887 eff = ord;
889 add_cmd = 'a';
890 del_cmd = 'd';
891 if (ord != 0)
893 add_cmd = 'd';
894 del_cmd = 'a';
896 #define F1 a[eff][0]
897 #define F2 a[eff][1]
898 #define T1 a[ ord^1 ][0]
899 #define T2 a[ ord^1 ][1]
900 for (i = 0; i < ops->len; i++)
902 int n;
904 op = &g_array_index (ops, DIFFCMD, i);
905 n = op->F1 - (op->cmd != add_cmd);
907 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
909 line++;
910 printer (ctx, EQU_CH, line, off, sz, buf);
911 off += sz;
912 while (buf[sz - 1] != '\n')
914 sz = f_gets (buf, sizeof (buf), f);
915 if (sz == 0)
917 printer (ctx, 0, 0, 0, 1, "\n");
918 break;
920 printer (ctx, 0, 0, 0, sz, buf);
921 off += sz;
925 if (line != n)
926 goto err;
928 if (op->cmd == add_cmd)
930 n = op->T2 - op->T1 + 1;
931 while (n != 0)
933 printer (ctx, DEL_CH, 0, 0, 1, "\n");
934 n--;
938 if (op->cmd == del_cmd)
940 n = op->F2 - op->F1 + 1;
941 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
943 line++;
944 printer (ctx, ADD_CH, line, off, sz, buf);
945 off += sz;
946 while (buf[sz - 1] != '\n')
948 sz = f_gets (buf, sizeof (buf), f);
949 if (sz == 0)
951 printer (ctx, 0, 0, 0, 1, "\n");
952 break;
954 printer (ctx, 0, 0, 0, sz, buf);
955 off += sz;
957 n--;
960 if (n != 0)
961 goto err;
964 if (op->cmd == 'c')
966 n = op->F2 - op->F1 + 1;
967 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
969 line++;
970 printer (ctx, CHG_CH, line, off, sz, buf);
971 off += sz;
972 while (buf[sz - 1] != '\n')
974 sz = f_gets (buf, sizeof (buf), f);
975 if (sz == 0)
977 printer (ctx, 0, 0, 0, 1, "\n");
978 break;
980 printer (ctx, 0, 0, 0, sz, buf);
981 off += sz;
983 n--;
986 if (n != 0)
987 goto err;
989 n = op->T2 - op->T1 - (op->F2 - op->F1);
990 while (n > 0)
992 printer (ctx, CHG_CH, 0, 0, 1, "\n");
993 n--;
997 #undef T2
998 #undef T1
999 #undef F2
1000 #undef F1
1002 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
1004 line++;
1005 printer (ctx, EQU_CH, line, off, sz, buf);
1006 off += sz;
1007 while (buf[sz - 1] != '\n')
1009 sz = f_gets (buf, sizeof (buf), f);
1010 if (sz == 0)
1012 printer (ctx, 0, 0, 0, 1, "\n");
1013 break;
1015 printer (ctx, 0, 0, 0, sz, buf);
1016 off += sz;
1020 f_close (f);
1021 return 0;
1023 err:
1024 f_close (f);
1025 return -1;
1028 /* --------------------------------------------------------------------------------------------- */
1030 /* horizontal diff ********************************************************** */
1033 * Longest common substring.
1035 * @param s first string
1036 * @param m length of first string
1037 * @param t second string
1038 * @param n length of second string
1039 * @param ret list of offsets for longest common substrings inside each string
1040 * @param min minimum length of common substrings
1042 * @returns 0 if success, nonzero otherwise
1045 static int
1046 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1048 int i, j;
1049 int *Lprev, *Lcurr;
1050 int z = 0;
1052 if (m < min || n < min)
1054 /* XXX early culling */
1055 return 0;
1058 Lprev = g_try_new0 (int, n + 1);
1059 if (Lprev == NULL)
1060 return -1;
1062 Lcurr = g_try_new0 (int, n + 1);
1063 if (Lcurr == NULL)
1065 g_free (Lprev);
1066 return -1;
1069 for (i = 0; i < m; i++)
1071 int *L;
1073 L = Lprev;
1074 Lprev = Lcurr;
1075 Lcurr = L;
1076 #ifdef USE_MEMSET_IN_LCS
1077 memset (Lcurr, 0, (n + 1) * sizeof (int));
1078 #endif
1079 for (j = 0; j < n; j++)
1081 #ifndef USE_MEMSET_IN_LCS
1082 Lcurr[j + 1] = 0;
1083 #endif
1084 if (s[i] == t[j])
1086 int v;
1088 v = Lprev[j] + 1;
1089 Lcurr[j + 1] = v;
1090 if (z < v)
1092 z = v;
1093 g_array_set_size (ret, 0);
1095 if (z == v && z >= min)
1097 int off0, off1;
1098 size_t k;
1100 off0 = i - z + 1;
1101 off1 = j - z + 1;
1103 for (k = 0; k < ret->len; k++)
1105 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1106 if ((*p)[0] == off0 || (*p)[1] >= off1)
1107 break;
1109 if (k == ret->len)
1111 PAIR p2;
1113 p2[0] = off0;
1114 p2[1] = off1;
1115 g_array_append_val (ret, p2);
1122 free (Lcurr);
1123 free (Lprev);
1124 return z;
1127 /* --------------------------------------------------------------------------------------------- */
1130 * Scan recursively for common substrings and build ranges.
1132 * @param s first string
1133 * @param t second string
1134 * @param bracket current limits for both of the strings
1135 * @param min minimum length of common substrings
1136 * @param hdiff list of horizontal diff ranges to fill
1137 * @param depth recursion depth
1139 * @returns 0 if success, nonzero otherwise
1142 static gboolean
1143 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1144 unsigned int depth)
1146 BRACKET p;
1148 if (depth-- != 0)
1150 GArray *ret;
1151 BRACKET b;
1152 int len;
1154 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1155 if (ret == NULL)
1156 return FALSE;
1158 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1159 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1160 if (ret->len != 0)
1162 size_t k = 0;
1163 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1164 const PAIR *data2;
1166 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1167 b[DIFF_LEFT].len = (*data)[0];
1168 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1169 b[DIFF_RIGHT].len = (*data)[1];
1170 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1171 return FALSE;
1173 for (k = 0; k < ret->len - 1; k++)
1175 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1176 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1177 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1178 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1179 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1180 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1181 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1182 return FALSE;
1184 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1185 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1186 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1187 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1188 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1189 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1190 return FALSE;
1192 g_array_free (ret, TRUE);
1193 return TRUE;
1197 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1198 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1199 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1200 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1201 g_array_append_val (hdiff, p);
1203 return TRUE;
1206 /* --------------------------------------------------------------------------------------------- */
1209 * Build list of horizontal diff ranges.
1211 * @param s first string
1212 * @param m length of first string
1213 * @param t second string
1214 * @param n length of second string
1215 * @param min minimum length of common substrings
1216 * @param hdiff list of horizontal diff ranges to fill
1217 * @param depth recursion depth
1219 * @returns 0 if success, nonzero otherwise
1222 static gboolean
1223 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1225 int i;
1226 BRACKET b;
1228 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1229 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1231 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1234 b[DIFF_LEFT].off = i;
1235 b[DIFF_LEFT].len = m - i;
1236 b[DIFF_RIGHT].off = i;
1237 b[DIFF_RIGHT].len = n - i;
1239 /* smartscan (multiple horizontal diff) */
1240 return hdiff_multi (s, t, b, min, hdiff, depth);
1243 /* --------------------------------------------------------------------------------------------- */
1245 /* read line **************************************************************** */
1248 * Check if character is inside horizontal diff limits.
1250 * @param k rank of character inside line
1251 * @param hdiff horizontal diff structure
1252 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1254 * @returns TRUE if inside hdiff limits, FALSE otherwise
1257 static gboolean
1258 is_inside (int k, GArray * hdiff, diff_place_t ord)
1260 size_t i;
1261 BRACKET *b;
1263 for (i = 0; i < hdiff->len; i++)
1265 int start, end;
1267 b = &g_array_index (hdiff, BRACKET, i);
1268 start = (*b)[ord].off;
1269 end = start + (*b)[ord].len;
1270 if (k >= start && k < end)
1271 return TRUE;
1273 return FALSE;
1276 /* --------------------------------------------------------------------------------------------- */
1279 * Copy 'src' to 'dst' expanding tabs.
1280 * @note The procedure returns when all bytes are consumed from 'src'
1282 * @param dst destination buffer
1283 * @param src source buffer
1284 * @param srcsize size of src buffer
1285 * @param base virtual base of this string, needed to calculate tabs
1286 * @param ts tab size
1288 * @returns new virtual base
1291 static int
1292 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1294 int i;
1296 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1298 *dst = *src;
1299 if (*src == '\t')
1301 int j;
1303 j = TAB_SKIP (ts, i + base);
1304 i += j - 1;
1305 while (j-- > 0)
1306 *dst++ = ' ';
1307 dst--;
1310 return i + base;
1313 /* --------------------------------------------------------------------------------------------- */
1316 * Copy 'src' to 'dst' expanding tabs.
1318 * @param dst destination buffer
1319 * @param dstsize size of dst buffer
1320 * @param[in,out] _src source buffer
1321 * @param srcsize size of src buffer
1322 * @param base virtual base of this string, needed to calculate tabs
1323 * @param ts tab size
1325 * @returns new virtual base
1327 * @note The procedure returns when all bytes are consumed from 'src'
1328 * or 'dstsize' bytes are written to 'dst'
1329 * @note Upon return, 'src' points to the first unwritten character in source
1332 static int
1333 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1335 int i;
1336 const char *src = *_src;
1338 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1340 *dst = *src;
1341 if (*src == '\t')
1343 int j;
1345 j = TAB_SKIP (ts, i + base);
1346 if (j > dstsize - i)
1347 j = dstsize - i;
1348 i += j - 1;
1349 while (j-- > 0)
1350 *dst++ = ' ';
1351 dst--;
1354 *_src = src;
1355 return i + base;
1358 /* --------------------------------------------------------------------------------------------- */
1361 * Read line from memory, converting tabs to spaces and padding with spaces.
1363 * @param src buffer to read from
1364 * @param srcsize size of src buffer
1365 * @param dst buffer to read to
1366 * @param dstsize size of dst buffer, excluding trailing null
1367 * @param skip number of characters to skip
1368 * @param ts tab size
1369 * @param show_cr show trailing carriage return as ^M
1371 * @returns negative on error, otherwise number of bytes except padding
1374 static int
1375 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr)
1377 int sz = 0;
1379 if (src != NULL)
1381 int i;
1382 char *tmp = dst;
1383 const int base = 0;
1385 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1387 if (*src == '\t')
1389 int j;
1391 j = TAB_SKIP (ts, i + base);
1392 i += j - 1;
1393 while (j-- > 0)
1395 if (skip > 0)
1396 skip--;
1397 else if (dstsize != 0)
1399 dstsize--;
1400 *dst++ = ' ';
1404 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1406 if (skip == 0 && show_cr)
1408 if (dstsize > 1)
1410 dstsize -= 2;
1411 *dst++ = '^';
1412 *dst++ = 'M';
1414 else
1416 dstsize--;
1417 *dst++ = '.';
1420 break;
1422 else if (skip > 0)
1424 int utf_ch = 0;
1425 gboolean res;
1426 int w;
1428 skip--;
1429 utf_ch = dview_get_utf ((char *) src, &w, &res);
1430 if (w > 1)
1431 skip += w - 1;
1432 if (!g_unichar_isprint (utf_ch))
1433 utf_ch = '.';
1435 else
1437 dstsize--;
1438 *dst++ = *src;
1441 sz = dst - tmp;
1443 while (dstsize != 0)
1445 dstsize--;
1446 *dst++ = ' ';
1448 *dst = '\0';
1449 return sz;
1452 /* --------------------------------------------------------------------------------------------- */
1455 * Read line from memory and build attribute array.
1457 * @param src buffer to read from
1458 * @param srcsize size of src buffer
1459 * @param dst buffer to read to
1460 * @param dstsize size of dst buffer, excluding trailing null
1461 * @param skip number of characters to skip
1462 * @param ts tab size
1463 * @param show_cr show trailing carriage return as ^M
1464 * @param hdiff horizontal diff structure
1465 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1466 * @param att buffer of attributes
1468 * @returns negative on error, otherwise number of bytes except padding
1471 static int
1472 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr,
1473 GArray * hdiff, diff_place_t ord, char *att)
1475 int sz = 0;
1477 if (src != NULL)
1479 int i, k;
1480 char *tmp = dst;
1481 const int base = 0;
1483 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1485 if (*src == '\t')
1487 int j;
1489 j = TAB_SKIP (ts, i + base);
1490 i += j - 1;
1491 while (j-- > 0)
1493 if (skip != 0)
1494 skip--;
1495 else if (dstsize != 0)
1497 dstsize--;
1498 *att++ = is_inside (k, hdiff, ord);
1499 *dst++ = ' ';
1503 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1505 if (skip == 0 && show_cr)
1507 if (dstsize > 1)
1509 dstsize -= 2;
1510 *att++ = is_inside (k, hdiff, ord);
1511 *dst++ = '^';
1512 *att++ = is_inside (k, hdiff, ord);
1513 *dst++ = 'M';
1515 else
1517 dstsize--;
1518 *att++ = is_inside (k, hdiff, ord);
1519 *dst++ = '.';
1522 break;
1524 else if (skip != 0)
1526 int utf_ch = 0;
1527 gboolean res;
1528 int w;
1530 skip--;
1531 utf_ch = dview_get_utf ((char *) src, &w, &res);
1532 if (w > 1)
1533 skip += w - 1;
1534 if (!g_unichar_isprint (utf_ch))
1535 utf_ch = '.';
1537 else
1539 dstsize--;
1540 *att++ = is_inside (k, hdiff, ord);
1541 *dst++ = *src;
1544 sz = dst - tmp;
1546 while (dstsize != 0)
1548 dstsize--;
1549 *att++ = '\0';
1550 *dst++ = ' ';
1552 *dst = '\0';
1553 return sz;
1556 /* --------------------------------------------------------------------------------------------- */
1559 * Read line from file, converting tabs to spaces and padding with spaces.
1561 * @param f file stream to read from
1562 * @param off offset of line inside file
1563 * @param dst buffer to read to
1564 * @param dstsize size of dst buffer, excluding trailing null
1565 * @param skip number of characters to skip
1566 * @param ts tab size
1567 * @param show_cr show trailing carriage return as ^M
1569 * @returns negative on error, otherwise number of bytes except padding
1572 static int
1573 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, int show_cr)
1575 int base = 0;
1576 int old_base = base;
1577 size_t amount = dstsize;
1578 size_t useful, offset;
1579 size_t i;
1580 size_t sz;
1581 int lastch = '\0';
1582 const char *q = NULL;
1583 char tmp[BUFSIZ]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1584 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1586 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1588 /* abnormal, but avoid buffer overflow */
1589 memset (dst, ' ', dstsize);
1590 dst[dstsize] = '\0';
1591 return 0;
1594 f_seek (f, off, SEEK_SET);
1596 while (skip > base)
1598 old_base = base;
1599 sz = f_gets (tmp, amount, f);
1600 if (sz == 0)
1601 break;
1603 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1604 if (cvt[base - old_base - 1] == '\n')
1606 q = &cvt[base - old_base - 1];
1607 base = old_base + q - cvt + 1;
1608 break;
1612 if (base < skip)
1614 memset (dst, ' ', dstsize);
1615 dst[dstsize] = '\0';
1616 return 0;
1619 useful = base - skip;
1620 offset = skip - old_base;
1622 if (useful <= dstsize)
1624 if (useful != 0)
1625 memmove (dst, cvt + offset, useful);
1627 if (q == NULL)
1629 sz = f_gets (tmp, dstsize - useful + 1, f);
1630 if (sz != 0)
1632 const char *ptr = tmp;
1634 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1635 if (ptr < tmp + sz)
1636 lastch = *ptr;
1639 sz = useful;
1641 else
1643 memmove (dst, cvt + offset, dstsize);
1644 sz = dstsize;
1645 lastch = cvt[offset + dstsize];
1648 dst[sz] = lastch;
1649 for (i = 0; i < sz && dst[i] != '\n'; i++)
1651 if (dst[i] == '\r' && dst[i + 1] == '\n')
1653 if (show_cr)
1655 if (i + 1 < dstsize)
1657 dst[i++] = '^';
1658 dst[i++] = 'M';
1660 else
1662 dst[i++] = '*';
1665 break;
1669 for (; i < dstsize; i++)
1670 dst[i] = ' ';
1671 dst[i] = '\0';
1672 return sz;
1675 /* --------------------------------------------------------------------------------------------- */
1676 /* diff printers et al ****************************************************** */
1678 static void
1679 cc_free_elt (void *elt)
1681 DIFFLN *p = elt;
1683 if (p != NULL)
1684 g_free (p->p);
1687 /* --------------------------------------------------------------------------------------------- */
1689 static int
1690 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1692 GArray *a = ((PRINTER_CTX *) ctx)->a;
1693 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1695 if (ch != 0)
1697 DIFFLN p;
1699 p.p = NULL;
1700 p.ch = ch;
1701 p.line = line;
1702 p.u.off = off;
1703 if (dsrc == DATA_SRC_MEM && line != 0)
1705 if (sz != 0 && str[sz - 1] == '\n')
1706 sz--;
1707 if (sz > 0)
1708 p.p = g_strndup (str, sz);
1709 p.u.len = sz;
1711 g_array_append_val (a, p);
1713 else if (dsrc == DATA_SRC_MEM)
1715 DIFFLN *p;
1717 p = &g_array_index (a, DIFFLN, a->len - 1);
1718 if (sz != 0 && str[sz - 1] == '\n')
1719 sz--;
1720 if (sz != 0)
1722 size_t new_size;
1723 char *q;
1725 new_size = p->u.len + sz;
1726 q = g_realloc (p->p, new_size);
1727 memcpy (q + p->u.len, str, sz);
1728 p->p = q;
1730 p->u.len += sz;
1732 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1734 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1735 f_write (f, str, sz);
1737 return 0;
1740 /* --------------------------------------------------------------------------------------------- */
1742 static int
1743 redo_diff (WDiff * dview)
1745 FBUF *const *f = dview->f;
1746 PRINTER_CTX ctx;
1747 GArray *ops;
1748 int ndiff;
1749 int rv;
1750 char extra[256];
1752 extra[0] = '\0';
1753 if (dview->opt.quality == 2)
1754 strcat (extra, " -d");
1755 if (dview->opt.quality == 1)
1756 strcat (extra, " --speed-large-files");
1757 if (dview->opt.strip_trailing_cr)
1758 strcat (extra, " --strip-trailing-cr");
1759 if (dview->opt.ignore_tab_expansion)
1760 strcat (extra, " -E");
1761 if (dview->opt.ignore_space_change)
1762 strcat (extra, " -b");
1763 if (dview->opt.ignore_all_space)
1764 strcat (extra, " -w");
1765 if (dview->opt.ignore_case)
1766 strcat (extra, " -i");
1768 if (dview->dsrc != DATA_SRC_MEM)
1770 f_reset (f[DIFF_LEFT]);
1771 f_reset (f[DIFF_RIGHT]);
1774 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1775 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1776 if (ndiff < 0)
1778 if (ops != NULL)
1779 g_array_free (ops, TRUE);
1780 return -1;
1783 ctx.dsrc = dview->dsrc;
1785 rv = 0;
1786 ctx.a = dview->a[DIFF_LEFT];
1787 ctx.f = f[DIFF_LEFT];
1788 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1790 ctx.a = dview->a[DIFF_RIGHT];
1791 ctx.f = f[DIFF_RIGHT];
1792 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1794 if (ops != NULL)
1795 g_array_free (ops, TRUE);
1797 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1798 return -1;
1800 if (dview->dsrc == DATA_SRC_TMP)
1802 f_trunc (f[DIFF_LEFT]);
1803 f_trunc (f[DIFF_RIGHT]);
1806 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1808 dview->hdiff = g_ptr_array_new ();
1809 if (dview->hdiff != NULL)
1811 size_t i;
1812 const DIFFLN *p;
1813 const DIFFLN *q;
1815 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1817 GArray *h = NULL;
1819 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1820 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1821 if (p->line && q->line && p->ch == CHG_CH)
1823 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1824 if (h != NULL)
1826 gboolean runresult;
1828 runresult =
1829 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1830 HDIFF_DEPTH);
1831 if (!runresult)
1833 g_array_free (h, TRUE);
1834 h = NULL;
1838 g_ptr_array_add (dview->hdiff, h);
1842 return ndiff;
1845 /* --------------------------------------------------------------------------------------------- */
1847 static void
1848 destroy_hdiff (WDiff * dview)
1850 if (dview->hdiff != NULL)
1852 int i;
1853 int len;
1855 len = dview->a[DIFF_LEFT]->len;
1857 for (i = 0; i < len; i++)
1859 GArray *h;
1861 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1862 if (h != NULL)
1863 g_array_free (h, TRUE);
1865 g_ptr_array_free (dview->hdiff, TRUE);
1866 dview->hdiff = NULL;
1869 mc_search_free (dview->search.handle);
1870 dview->search.handle = NULL;
1871 g_free (dview->search.last_string);
1872 dview->search.last_string = NULL;
1875 /* --------------------------------------------------------------------------------------------- */
1876 /* stuff ******************************************************************** */
1878 static int
1879 get_digits (unsigned int n)
1881 int d = 1;
1883 while (n /= 10)
1884 d++;
1885 return d;
1888 /* --------------------------------------------------------------------------------------------- */
1890 static int
1891 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1893 const DIFFLN *p;
1895 *linenum = 0;
1896 *lineofs = 0;
1898 if (a->len != 0)
1900 if (pos >= a->len)
1901 pos = a->len - 1;
1903 p = &g_array_index (a, DIFFLN, pos);
1905 if (p->line == 0)
1907 int n;
1909 for (n = pos; n > 0; n--)
1911 p--;
1912 if (p->line != 0)
1913 break;
1915 *lineofs = pos - n + 1;
1918 *linenum = p->line;
1920 return 0;
1923 /* --------------------------------------------------------------------------------------------- */
1925 static int
1926 calc_nwidth (const GArray ** const a)
1928 int l1, o1;
1929 int l2, o2;
1931 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1932 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1933 if (l1 < l2)
1934 l1 = l2;
1935 return get_digits (l1);
1938 /* --------------------------------------------------------------------------------------------- */
1940 static int
1941 find_prev_hunk (const GArray * a, int pos)
1943 #if 1
1944 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1945 pos--;
1946 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1947 pos--;
1948 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1949 pos--;
1950 if (pos > 0 && (size_t) pos < a->len)
1951 pos++;
1952 #else
1953 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1954 pos--;
1955 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1956 pos--;
1957 #endif
1959 return pos;
1962 /* --------------------------------------------------------------------------------------------- */
1964 static size_t
1965 find_next_hunk (const GArray * a, size_t pos)
1967 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1968 pos++;
1969 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1970 pos++;
1971 return pos;
1974 /* --------------------------------------------------------------------------------------------- */
1976 * Find start and end lines of the current hunk.
1978 * @param dview WDiff widget
1979 * @returns boolean and
1980 * start_line1 first line of current hunk (file[0])
1981 * end_line1 last line of current hunk (file[0])
1982 * start_line1 first line of current hunk (file[0])
1983 * end_line1 last line of current hunk (file[0])
1986 static int
1987 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1989 const GArray *a0 = dview->a[DIFF_LEFT];
1990 const GArray *a1 = dview->a[DIFF_RIGHT];
1991 size_t pos;
1992 int ch;
1993 int res = 0;
1995 *start_line1 = 1;
1996 *start_line2 = 1;
1997 *end_line1 = 1;
1998 *end_line2 = 1;
2000 pos = dview->skip_rows;
2001 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
2002 if (ch != EQU_CH)
2004 switch (ch)
2006 case ADD_CH:
2007 res = DIFF_DEL;
2008 break;
2009 case DEL_CH:
2010 res = DIFF_ADD;
2011 break;
2012 case CHG_CH:
2013 res = DIFF_CHG;
2014 break;
2016 while (pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2017 pos--;
2018 if (pos > 0)
2020 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
2021 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
2023 pos = dview->skip_rows;
2024 while (pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2026 int l0, l1;
2028 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
2029 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
2030 if (l0 > 0)
2031 *end_line1 = max (*start_line1, l0);
2032 if (l1 > 0)
2033 *end_line2 = max (*start_line2, l1);
2034 pos++;
2037 return res;
2040 /* --------------------------------------------------------------------------------------------- */
2042 * Remove hunk from file.
2044 * @param dview WDiff widget
2045 * @param merge_file file stream for writing data
2046 * @param from1 first line of hunk
2047 * @param to1 last line of hunk
2048 * @param merge_direction in what direction files should be merged
2051 static void
2052 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
2053 action_direction_t merge_direction)
2055 int line;
2056 char buf[BUF_10K];
2057 FILE *f0;
2059 if (merge_direction == FROM_RIGHT_TO_LEFT)
2060 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2061 else
2062 f0 = fopen (dview->file[DIFF_LEFT], "r");
2064 line = 0;
2065 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2067 line++;
2068 fputs (buf, merge_file);
2070 while (fgets (buf, sizeof (buf), f0) != NULL)
2072 line++;
2073 if (line >= to1)
2074 fputs (buf, merge_file);
2076 fclose (f0);
2079 /* --------------------------------------------------------------------------------------------- */
2081 * Add hunk to file.
2083 * @param dview WDiff widget
2084 * @param merge_file file stream for writing data
2085 * @param from1 first line of source hunk
2086 * @param from2 first line of destination hunk
2087 * @param to1 last line of source hunk
2088 * @param merge_direction in what direction files should be merged
2091 static void
2092 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2093 action_direction_t merge_direction)
2095 int line;
2096 char buf[BUF_10K];
2097 FILE *f0;
2098 FILE *f1;
2100 if (merge_direction == FROM_RIGHT_TO_LEFT)
2102 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2103 f1 = fopen (dview->file[DIFF_LEFT], "r");
2105 else
2107 f0 = fopen (dview->file[DIFF_LEFT], "r");
2108 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2111 line = 0;
2112 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2114 line++;
2115 fputs (buf, merge_file);
2117 line = 0;
2118 while (fgets (buf, sizeof (buf), f1) != NULL && line <= to2)
2120 line++;
2121 if (line >= from2)
2122 fputs (buf, merge_file);
2124 while (fgets (buf, sizeof (buf), f0) != NULL)
2125 fputs (buf, merge_file);
2127 fclose (f0);
2128 fclose (f1);
2131 /* --------------------------------------------------------------------------------------------- */
2133 * Replace hunk in file.
2135 * @param dview WDiff widget
2136 * @param merge_file file stream for writing data
2137 * @param from1 first line of source hunk
2138 * @param to1 last line of source hunk
2139 * @param from2 first line of destination hunk
2140 * @param to2 last line of destination hunk
2141 * @param merge_direction in what direction files should be merged
2144 static void
2145 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2146 action_direction_t merge_direction)
2148 int line1 = 0, line2 = 0;
2149 char buf[BUF_10K];
2150 FILE *f0;
2151 FILE *f1;
2153 if (merge_direction == FROM_RIGHT_TO_LEFT)
2155 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2156 f1 = fopen (dview->file[DIFF_LEFT], "r");
2158 else
2160 f0 = fopen (dview->file[DIFF_LEFT], "r");
2161 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2164 while (fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1)
2166 line1++;
2167 fputs (buf, merge_file);
2169 while (fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2)
2171 line2++;
2172 if (line2 >= from2)
2173 fputs (buf, merge_file);
2175 while (fgets (buf, sizeof (buf), f0) != NULL)
2177 line1++;
2178 if (line1 > to1)
2179 fputs (buf, merge_file);
2181 fclose (f0);
2182 fclose (f1);
2185 /* --------------------------------------------------------------------------------------------- */
2187 * Merge hunk.
2189 * @param dview WDiff widget
2190 * @param merge_direction in what direction files should be merged
2193 static void
2194 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2196 int from1, to1, from2, to2;
2197 int res;
2198 int hunk;
2199 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2201 if (merge_direction == FROM_RIGHT_TO_LEFT)
2202 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2203 else
2204 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2206 if (hunk > 0)
2208 int merge_file_fd;
2209 FILE *merge_file;
2210 vfs_path_t *merge_file_name_vpath = NULL;
2212 if (!dview->merged[n_merge])
2214 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2215 if (!dview->merged[n_merge])
2217 message (D_ERROR, MSG_ERROR,
2218 _("Cannot create backup file\n%s%s\n%s"),
2219 dview->file[n_merge], "~~~", unix_error_string (errno));
2220 return;
2224 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2225 if (merge_file_fd == -1)
2227 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2228 unix_error_string (errno));
2229 return;
2232 merge_file = fdopen (merge_file_fd, "w");
2234 switch (hunk)
2236 case DIFF_DEL:
2237 if (merge_direction == FROM_RIGHT_TO_LEFT)
2238 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2239 else
2240 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2241 break;
2242 case DIFF_ADD:
2243 if (merge_direction == FROM_RIGHT_TO_LEFT)
2244 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2245 else
2246 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2247 break;
2248 case DIFF_CHG:
2249 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2250 break;
2252 fflush (merge_file);
2253 fclose (merge_file);
2254 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2255 mc_unlink (merge_file_name_vpath);
2256 vfs_path_free (merge_file_name_vpath);
2260 /* --------------------------------------------------------------------------------------------- */
2261 /* view routines and callbacks ********************************************** */
2263 static void
2264 dview_compute_split (WDiff * dview, int i)
2266 dview->bias += i;
2267 if (dview->bias < 2 - dview->half1)
2268 dview->bias = 2 - dview->half1;
2269 if (dview->bias > dview->half2 - 2)
2270 dview->bias = dview->half2 - 2;
2273 /* --------------------------------------------------------------------------------------------- */
2275 static void
2276 dview_compute_areas (WDiff * dview)
2278 dview->height = LINES - 2;
2279 dview->half1 = COLS / 2;
2280 dview->half2 = COLS - dview->half1;
2282 dview_compute_split (dview, 0);
2285 /* --------------------------------------------------------------------------------------------- */
2287 static void
2288 dview_reread (WDiff * dview)
2290 int ndiff;
2292 destroy_hdiff (dview);
2293 if (dview->a[DIFF_LEFT] != NULL)
2295 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2296 g_array_free (dview->a[DIFF_LEFT], TRUE);
2298 if (dview->a[DIFF_RIGHT] != NULL)
2300 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2301 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2304 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2305 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2307 ndiff = redo_diff (dview);
2308 if (ndiff >= 0)
2309 dview->ndiff = ndiff;
2312 /* --------------------------------------------------------------------------------------------- */
2314 #ifdef HAVE_CHARSET
2315 static void
2316 dview_set_codeset (WDiff * dview)
2318 const char *encoding_id = NULL;
2320 dview->utf8 = TRUE;
2321 encoding_id =
2322 get_codepage_id (mc_global.source_codepage >=
2323 0 ? mc_global.source_codepage : mc_global.display_codepage);
2324 if (encoding_id != NULL)
2326 GIConv conv;
2328 conv = str_crt_conv_from (encoding_id);
2329 if (conv != INVALID_CONV)
2331 if (dview->converter != str_cnv_from_term)
2332 str_close_conv (dview->converter);
2333 dview->converter = conv;
2335 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2339 /* --------------------------------------------------------------------------------------------- */
2341 static void
2342 dview_select_encoding (WDiff * dview)
2344 if (do_select_codepage ())
2345 dview_set_codeset (dview);
2346 dview_reread (dview);
2347 tty_touch_screen ();
2348 repaint_screen ();
2350 #endif /* HAVE_CHARSET */
2352 /* --------------------------------------------------------------------------------------------- */
2354 static void
2355 dview_diff_options (WDiff * dview)
2357 const char *quality_str[] = {
2358 N_("No&rmal"),
2359 N_("&Fastest (Assume large files)"),
2360 N_("&Minimal (Find a smaller set of change)")
2363 QuickWidget diffopt_widgets[] = {
2364 QUICK_BUTTON (6, 10, 14, OPTY, N_("&Cancel"), B_CANCEL, NULL),
2365 QUICK_BUTTON (2, 10, 14, OPTY, N_("&OK"), B_ENTER, NULL),
2367 QUICK_CHECKBOX (3, OPTX, 12, OPTY,
2368 N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr),
2369 QUICK_CHECKBOX (3, OPTX, 11, OPTY,
2370 N_("Ignore all &whitespace"), &dview->opt.ignore_all_space),
2371 QUICK_CHECKBOX (3, OPTX, 10, OPTY,
2372 N_("Ignore &space change"), &dview->opt.ignore_space_change),
2373 QUICK_CHECKBOX (3, OPTX, 9, OPTY,
2374 N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion),
2375 QUICK_CHECKBOX (3, OPTX, 8, OPTY,
2376 N_("&Ignore case"), &dview->opt.ignore_case),
2377 QUICK_LABEL (3, OPTX, 7, OPTY, N_("Diff extra options")),
2378 QUICK_RADIO (3, OPTX, 3, OPTY,
2379 3, (const char **) quality_str, (int *) &dview->opt.quality),
2380 QUICK_LABEL (3, OPTX, 2, OPTY, N_("Diff algorithm")),
2382 QUICK_END
2385 QuickDialog diffopt = {
2386 OPTX, OPTY, -1, -1,
2387 N_("Diff Options"), "[Diff Options]",
2388 diffopt_widgets, NULL, NULL, FALSE
2391 if (quick_dialog (&diffopt) != B_CANCEL)
2392 dview_reread (dview);
2395 /* --------------------------------------------------------------------------------------------- */
2397 static int
2398 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2399 const char *label1, const char *label2, DSRC dsrc)
2401 int ndiff;
2402 FBUF *f[DIFF_COUNT];
2404 f[DIFF_LEFT] = NULL;
2405 f[DIFF_RIGHT] = NULL;
2407 if (dsrc == DATA_SRC_TMP)
2409 f[DIFF_LEFT] = f_temp ();
2410 if (f[DIFF_LEFT] == NULL)
2411 return -1;
2413 f[DIFF_RIGHT] = f_temp ();
2414 if (f[DIFF_RIGHT] == NULL)
2416 f_close (f[DIFF_LEFT]);
2417 return -1;
2420 else if (dsrc == DATA_SRC_ORG)
2422 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2423 if (f[DIFF_LEFT] == NULL)
2424 return -1;
2426 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2427 if (f[DIFF_RIGHT] == NULL)
2429 f_close (f[DIFF_LEFT]);
2430 return -1;
2434 dview->args = args;
2435 dview->file[DIFF_LEFT] = file1;
2436 dview->file[DIFF_RIGHT] = file2;
2437 dview->label[DIFF_LEFT] = g_strdup (label1);
2438 dview->label[DIFF_RIGHT] = g_strdup (label2);
2439 dview->f[DIFF_LEFT] = f[0];
2440 dview->f[DIFF_RIGHT] = f[1];
2441 dview->merged[DIFF_LEFT] = FALSE;
2442 dview->merged[DIFF_RIGHT] = FALSE;
2443 dview->hdiff = NULL;
2444 dview->dsrc = dsrc;
2445 dview->converter = str_cnv_from_term;
2446 #ifdef HAVE_CHARSET
2447 dview_set_codeset (dview);
2448 #endif
2449 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2450 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2452 ndiff = redo_diff (dview);
2453 if (ndiff < 0)
2455 /* goto WIDGET_DESTROY stage: dview_fini() */
2456 f_close (f[DIFF_LEFT]);
2457 f_close (f[DIFF_RIGHT]);
2458 return -1;
2461 dview->ndiff = ndiff;
2463 dview->view_quit = 0;
2465 dview->bias = 0;
2466 dview->new_frame = 1;
2467 dview->skip_rows = 0;
2468 dview->skip_cols = 0;
2469 dview->display_symbols = 0;
2470 dview->display_numbers = 0;
2471 dview->show_cr = 1;
2472 dview->tab_size = 8;
2473 dview->ord = DIFF_LEFT;
2474 dview->full = 0;
2476 dview->search.handle = NULL;
2477 dview->search.last_string = NULL;
2478 dview->search.last_found_line = -1;
2479 dview->search.last_accessed_num_line = -1;
2481 dview->opt.quality = 0;
2482 dview->opt.strip_trailing_cr = 0;
2483 dview->opt.ignore_tab_expansion = 0;
2484 dview->opt.ignore_space_change = 0;
2485 dview->opt.ignore_all_space = 0;
2486 dview->opt.ignore_case = 0;
2488 dview_compute_areas (dview);
2490 return 0;
2493 /* --------------------------------------------------------------------------------------------- */
2495 static void
2496 dview_fini (WDiff * dview)
2498 if (dview->dsrc != DATA_SRC_MEM)
2500 f_close (dview->f[DIFF_RIGHT]);
2501 f_close (dview->f[DIFF_LEFT]);
2504 if (dview->converter != str_cnv_from_term)
2505 str_close_conv (dview->converter);
2507 destroy_hdiff (dview);
2508 if (dview->a[DIFF_LEFT] != NULL)
2510 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2511 g_array_free (dview->a[DIFF_LEFT], TRUE);
2512 dview->a[DIFF_LEFT] = NULL;
2514 if (dview->a[DIFF_RIGHT] != NULL)
2516 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2517 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2518 dview->a[DIFF_RIGHT] = NULL;
2521 g_free (dview->label[DIFF_LEFT]);
2522 g_free (dview->label[DIFF_RIGHT]);
2525 /* --------------------------------------------------------------------------------------------- */
2527 static int
2528 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2530 size_t i, k;
2531 int j;
2532 char buf[BUFSIZ];
2533 FBUF *f = dview->f[ord];
2534 int skip = dview->skip_cols;
2535 int display_symbols = dview->display_symbols;
2536 int display_numbers = dview->display_numbers;
2537 int show_cr = dview->show_cr;
2538 int tab_size = 8;
2539 const DIFFLN *p;
2540 int nwidth = display_numbers;
2541 int xwidth;
2543 xwidth = display_symbols + display_numbers;
2544 if (dview->tab_size > 0 && dview->tab_size < 9)
2545 tab_size = dview->tab_size;
2547 if (xwidth != 0)
2549 if (xwidth > width && display_symbols)
2551 xwidth--;
2552 display_symbols = 0;
2554 if (xwidth > width && display_numbers)
2556 xwidth = width;
2557 display_numbers = width;
2560 xwidth++;
2561 c += xwidth;
2562 width -= xwidth;
2563 if (width < 0)
2564 width = 0;
2567 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2569 /* abnormal, but avoid buffer overflow */
2570 return -1;
2573 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2575 int ch, next_ch, col;
2576 size_t cnt;
2578 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2579 ch = p->ch;
2580 tty_setcolor (NORMAL_COLOR);
2581 if (display_symbols)
2583 tty_gotoyx (r + j, c - 2);
2584 tty_print_char (ch);
2586 if (p->line != 0)
2588 if (display_numbers)
2590 tty_gotoyx (r + j, c - xwidth);
2591 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2592 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2594 if (ch == ADD_CH)
2595 tty_setcolor (DFF_ADD_COLOR);
2596 if (ch == CHG_CH)
2597 tty_setcolor (DFF_CHG_COLOR);
2598 if (f == NULL)
2600 if (i == (size_t) dview->search.last_found_line)
2601 tty_setcolor (MARKED_SELECTED_COLOR);
2602 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2604 char att[BUFSIZ];
2606 if (dview->utf8)
2607 k = dview_str_utf8_offset_to_pos (p->p, width);
2608 else
2609 k = width;
2611 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2612 g_ptr_array_index (dview->hdiff, i), ord, att);
2613 tty_gotoyx (r + j, c);
2614 col = 0;
2616 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2618 int w;
2619 gboolean ch_res;
2621 if (dview->utf8)
2623 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2624 if (w > 1)
2625 cnt += w - 1;
2626 if (!g_unichar_isprint (next_ch))
2627 next_ch = '.';
2629 else
2630 next_ch = dview_get_byte (buf + cnt, &ch_res);
2632 if (ch_res)
2634 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2635 #ifdef HAVE_CHARSET
2636 if (mc_global.utf8_display)
2638 if (!dview->utf8)
2640 next_ch =
2641 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2642 dview->converter);
2645 else if (dview->utf8)
2646 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2647 else
2648 next_ch = convert_to_display_c (next_ch);
2649 #endif
2650 tty_print_anychar (next_ch);
2651 col++;
2654 continue;
2657 if (ch == CHG_CH)
2658 tty_setcolor (DFF_CHH_COLOR);
2660 if (dview->utf8)
2661 k = dview_str_utf8_offset_to_pos (p->p, width);
2662 else
2663 k = width;
2664 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2666 else
2667 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2669 else
2671 if (display_numbers)
2673 tty_gotoyx (r + j, c - xwidth);
2674 memset (buf, ' ', display_numbers);
2675 buf[display_numbers] = '\0';
2676 tty_print_string (buf);
2678 if (ch == DEL_CH)
2679 tty_setcolor (DFF_DEL_COLOR);
2680 if (ch == CHG_CH)
2681 tty_setcolor (DFF_CHD_COLOR);
2682 memset (buf, ' ', width);
2683 buf[width] = '\0';
2685 tty_gotoyx (r + j, c);
2686 /* tty_print_nstring (buf, width); */
2687 col = 0;
2688 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2690 int w;
2691 gboolean ch_res;
2693 if (dview->utf8)
2695 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2696 if (w > 1)
2697 cnt += w - 1;
2698 if (!g_unichar_isprint (next_ch))
2699 next_ch = '.';
2701 else
2702 next_ch = dview_get_byte (buf + cnt, &ch_res);
2703 if (ch_res)
2705 #ifdef HAVE_CHARSET
2706 if (mc_global.utf8_display)
2708 if (!dview->utf8)
2710 next_ch =
2711 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2714 else if (dview->utf8)
2715 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2716 else
2717 next_ch = convert_to_display_c (next_ch);
2718 #endif
2720 tty_print_anychar (next_ch);
2721 col++;
2725 tty_setcolor (NORMAL_COLOR);
2726 k = width;
2727 if (width < xwidth - 1)
2728 k = xwidth - 1;
2729 memset (buf, ' ', k);
2730 buf[k] = '\0';
2731 for (; j < height; j++)
2733 if (xwidth != 0)
2735 tty_gotoyx (r + j, c - xwidth);
2736 /* tty_print_nstring (buf, xwidth - 1); */
2737 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2739 tty_gotoyx (r + j, c);
2740 /* tty_print_nstring (buf, width); */
2741 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2744 return 0;
2747 /* --------------------------------------------------------------------------------------------- */
2749 static void
2750 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2752 const char *buf;
2753 int filename_width;
2754 int linenum, lineofs;
2755 vfs_path_t *vpath;
2756 char *path;
2758 tty_setcolor (STATUSBAR_COLOR);
2760 tty_gotoyx (0, c);
2761 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2763 filename_width = width - 24;
2764 if (filename_width < 8)
2765 filename_width = 8;
2767 vpath = vfs_path_from_str (dview->label[ord]);
2768 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2769 vfs_path_free (vpath);
2770 buf = str_term_trim (path, filename_width);
2771 if (ord == DIFF_LEFT)
2772 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2773 buf, linenum, lineofs, dview->skip_cols);
2774 else
2775 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2776 buf, linenum, lineofs, dview->ndiff);
2777 g_free (path);
2780 /* --------------------------------------------------------------------------------------------- */
2782 static void
2783 dview_redo (WDiff * dview)
2785 if (dview->display_numbers)
2787 int old;
2789 old = dview->display_numbers;
2790 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2791 dview->new_frame = (old != dview->display_numbers);
2793 dview_reread (dview);
2796 /* --------------------------------------------------------------------------------------------- */
2798 static void
2799 dview_update (WDiff * dview)
2801 int height = dview->height;
2802 int width1;
2803 int width2;
2804 int last;
2806 last = dview->a[DIFF_LEFT]->len - 1;
2808 if (dview->skip_rows > last)
2809 dview->skip_rows = dview->search.last_accessed_num_line = last;
2810 if (dview->skip_rows < 0)
2811 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2812 if (dview->skip_cols < 0)
2813 dview->skip_cols = 0;
2815 if (height < 2)
2816 return;
2818 width1 = dview->half1 + dview->bias;
2819 width2 = dview->half2 - dview->bias;
2820 if (dview->full)
2822 width1 = COLS;
2823 width2 = 0;
2826 if (dview->new_frame)
2828 int xwidth;
2830 tty_setcolor (NORMAL_COLOR);
2831 xwidth = dview->display_symbols + dview->display_numbers;
2832 if (width1 > 1)
2833 tty_draw_box (1, 0, height, width1, FALSE);
2834 if (width2 > 1)
2835 tty_draw_box (1, width1, height, width2, FALSE);
2837 if (xwidth != 0)
2839 xwidth++;
2840 if (xwidth < width1 - 1)
2842 tty_gotoyx (1, xwidth);
2843 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2844 tty_gotoyx (height, xwidth);
2845 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2846 tty_draw_vline (2, xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2848 if (xwidth < width2 - 1)
2850 tty_gotoyx (1, width1 + xwidth);
2851 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2852 tty_gotoyx (height, width1 + xwidth);
2853 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2854 tty_draw_vline (2, width1 + xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2857 dview->new_frame = 0;
2860 if (width1 > 2)
2862 dview_status (dview, dview->ord, width1, 0);
2863 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2865 if (width2 > 2)
2867 dview_status (dview, dview->ord ^ 1, width2, width1);
2868 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2872 /* --------------------------------------------------------------------------------------------- */
2874 static void
2875 dview_edit (WDiff * dview, diff_place_t ord)
2877 Dlg_head *h;
2878 gboolean h_modal;
2879 int linenum, lineofs;
2881 if (dview->dsrc == DATA_SRC_TMP)
2883 error_dialog (_("Edit"), _("Edit is disabled"));
2884 return;
2887 h = ((Widget *) dview)->owner;
2888 h_modal = h->modal;
2890 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2891 h->modal = TRUE; /* not allow edit file in several editors */
2893 vfs_path_t *tmp_vpath;
2895 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2896 do_edit_at_line (tmp_vpath, use_internal_edit, linenum);
2897 vfs_path_free (tmp_vpath);
2899 h->modal = h_modal;
2900 dview_redo (dview);
2901 dview_update (dview);
2904 /* --------------------------------------------------------------------------------------------- */
2906 static void
2907 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2909 /* *INDENT-OFF* */
2910 static const char *title[2] = {
2911 N_("Goto line (left)"),
2912 N_("Goto line (right)")
2914 /* *INDENT-ON* */
2915 static char prev[256];
2916 /* XXX some statics here, to be remembered between runs */
2918 int newline;
2919 char *input;
2921 input = input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE, prev);
2922 if (input != NULL)
2924 const char *s = input;
2926 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2928 size_t i = 0;
2930 if (newline > 0)
2932 const DIFFLN *p;
2934 for (; i < dview->a[ord]->len; i++)
2936 p = &g_array_index (dview->a[ord], DIFFLN, i);
2937 if (p->line == newline)
2938 break;
2941 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2942 g_snprintf (prev, sizeof (prev), "%d", newline);
2944 g_free (input);
2948 /* --------------------------------------------------------------------------------------------- */
2950 static void
2951 dview_labels (WDiff * dview)
2953 Dlg_head *h;
2954 WButtonBar *b;
2956 h = dview->widget.owner;
2957 b = find_buttonbar (h);
2959 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, (Widget *) dview);
2960 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, (Widget *) dview);
2961 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, (Widget *) dview);
2962 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, (Widget *) dview);
2963 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, (Widget *) dview);
2964 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, (Widget *) dview);
2965 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, (Widget *) dview);
2968 /* --------------------------------------------------------------------------------------------- */
2970 static int
2971 dview_event (Gpm_Event * event, void *data)
2973 WDiff *dview = (WDiff *) data;
2975 if (!mouse_global_in_widget (event, data))
2976 return MOU_UNHANDLED;
2978 /* We are not interested in release events */
2979 if ((event->type & (GPM_DOWN | GPM_DRAG)) == 0)
2980 return MOU_NORMAL;
2982 /* Wheel events */
2983 if ((event->buttons & GPM_B_UP) != 0 && (event->type & GPM_DOWN) != 0)
2985 dview->skip_rows -= 2;
2986 dview->search.last_accessed_num_line = dview->skip_rows;
2987 dview_update (dview);
2989 else if ((event->buttons & GPM_B_DOWN) != 0 && (event->type & GPM_DOWN) != 0)
2991 dview->skip_rows += 2;
2992 dview->search.last_accessed_num_line = dview->skip_rows;
2993 dview_update (dview);
2996 return MOU_NORMAL;
2999 /* --------------------------------------------------------------------------------------------- */
3001 static gboolean
3002 dview_save (WDiff * dview)
3004 gboolean res = TRUE;
3006 if (dview->merged[DIFF_LEFT])
3008 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3009 dview->merged[DIFF_LEFT] = !res;
3011 if (dview->merged[DIFF_RIGHT])
3013 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3014 dview->merged[DIFF_RIGHT] = !res;
3016 return res;
3019 /* --------------------------------------------------------------------------------------------- */
3021 static void
3022 dview_do_save (WDiff * dview)
3024 (void) dview_save (dview);
3027 /* --------------------------------------------------------------------------------------------- */
3029 static void
3030 dview_save_options (WDiff * dview)
3032 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
3033 dview->display_symbols != 0 ? TRUE : FALSE);
3034 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
3035 dview->display_numbers != 0 ? TRUE : FALSE);
3036 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
3038 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
3040 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
3041 dview->opt.strip_trailing_cr);
3042 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
3043 dview->opt.ignore_all_space);
3044 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
3045 dview->opt.ignore_space_change);
3046 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
3047 dview->opt.ignore_tab_expansion);
3048 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
3051 /* --------------------------------------------------------------------------------------------- */
3053 static void
3054 dview_load_options (WDiff * dview)
3056 gboolean show_numbers, show_symbols;
3057 int tab_size;
3059 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
3060 if (show_symbols)
3061 dview->display_symbols = 1;
3062 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
3063 if (show_numbers)
3064 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
3065 tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
3066 if (tab_size > 0 && tab_size < 9)
3067 dview->tab_size = tab_size;
3068 else
3069 dview->tab_size = 8;
3071 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
3073 dview->opt.strip_trailing_cr =
3074 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
3075 dview->opt.ignore_all_space =
3076 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
3077 dview->opt.ignore_space_change =
3078 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
3079 dview->opt.ignore_tab_expansion =
3080 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
3081 dview->opt.ignore_case =
3082 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
3084 dview->new_frame = 1;
3087 /* --------------------------------------------------------------------------------------------- */
3090 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3091 * ask user.
3093 static gboolean
3094 dview_ok_to_exit (WDiff * dview)
3096 gboolean res = TRUE;
3097 int act;
3099 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3100 return res;
3102 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3103 _("File(s) was modified. Save with exit?") :
3104 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3105 D_NORMAL, 2, _("&Yes"), _("&No"));
3107 /* Esc is No */
3108 if (mc_global.midnight_shutdown || (act == -1))
3109 act = 1;
3111 switch (act)
3113 case -1: /* Esc */
3114 res = FALSE;
3115 break;
3116 case 0: /* Yes */
3117 (void) dview_save (dview);
3118 res = TRUE;
3119 break;
3120 case 1: /* No */
3121 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3122 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3123 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3124 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3125 /* fall through */
3126 default:
3127 res = TRUE;
3128 break;
3130 return res;
3133 /* --------------------------------------------------------------------------------------------- */
3135 static cb_ret_t
3136 dview_execute_cmd (WDiff * dview, unsigned long command)
3138 cb_ret_t res = MSG_HANDLED;
3140 switch (command)
3142 case CK_ShowSymbols:
3143 dview->display_symbols ^= 1;
3144 dview->new_frame = 1;
3145 break;
3146 case CK_ShowNumbers:
3147 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
3148 dview->new_frame = 1;
3149 break;
3150 case CK_SplitFull:
3151 dview->full ^= 1;
3152 dview->new_frame = 1;
3153 break;
3154 case CK_SplitEqual:
3155 if (!dview->full)
3157 dview->bias = 0;
3158 dview->new_frame = 1;
3160 break;
3161 case CK_SplitMore:
3162 if (!dview->full)
3164 dview_compute_split (dview, 1);
3165 dview->new_frame = 1;
3167 break;
3169 case CK_SplitLess:
3170 if (!dview->full)
3172 dview_compute_split (dview, -1);
3173 dview->new_frame = 1;
3175 break;
3176 case CK_Tab2:
3177 dview->tab_size = 2;
3178 break;
3179 case CK_Tab3:
3180 dview->tab_size = 3;
3181 break;
3182 case CK_Tab4:
3183 dview->tab_size = 4;
3184 break;
3185 case CK_Tab8:
3186 dview->tab_size = 8;
3187 break;
3188 case CK_Swap:
3189 dview->ord ^= 1;
3190 break;
3191 case CK_Redo:
3192 dview_redo (dview);
3193 break;
3194 case CK_HunkNext:
3195 dview->skip_rows = dview->search.last_accessed_num_line =
3196 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3197 break;
3198 case CK_HunkPrev:
3199 dview->skip_rows = dview->search.last_accessed_num_line =
3200 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3201 break;
3202 case CK_Goto:
3203 dview_goto_cmd (dview, TRUE);
3204 break;
3205 case CK_Edit:
3206 dview_edit (dview, dview->ord);
3207 break;
3208 case CK_Merge:
3209 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3210 dview_redo (dview);
3211 break;
3212 case CK_MergeOther:
3213 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3214 dview_redo (dview);
3215 break;
3216 case CK_EditOther:
3217 dview_edit (dview, dview->ord ^ 1);
3218 break;
3219 case CK_Search:
3220 dview_search_cmd (dview);
3221 break;
3222 case CK_SearchContinue:
3223 dview_continue_search_cmd (dview);
3224 break;
3225 case CK_Top:
3226 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3227 break;
3228 case CK_Bottom:
3229 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3230 break;
3231 case CK_Up:
3232 if (dview->skip_rows > 0)
3234 dview->skip_rows--;
3235 dview->search.last_accessed_num_line = dview->skip_rows;
3237 break;
3238 case CK_Down:
3239 dview->skip_rows++;
3240 dview->search.last_accessed_num_line = dview->skip_rows;
3241 break;
3242 case CK_PageDown:
3243 if (dview->height > 2)
3245 dview->skip_rows += dview->height - 2;
3246 dview->search.last_accessed_num_line = dview->skip_rows;
3248 break;
3249 case CK_PageUp:
3250 if (dview->height > 2)
3252 dview->skip_rows -= dview->height - 2;
3253 dview->search.last_accessed_num_line = dview->skip_rows;
3255 break;
3256 case CK_Left:
3257 dview->skip_cols--;
3258 break;
3259 case CK_Right:
3260 dview->skip_cols++;
3261 break;
3262 case CK_LeftQuick:
3263 dview->skip_cols -= 8;
3264 break;
3265 case CK_RightQuick:
3266 dview->skip_cols += 8;
3267 break;
3268 case CK_Home:
3269 dview->skip_cols = 0;
3270 break;
3271 case CK_Shell:
3272 view_other_cmd ();
3273 break;
3274 case CK_Quit:
3275 dview->view_quit = 1;
3276 break;
3277 case CK_Save:
3278 dview_do_save (dview);
3279 break;
3280 case CK_Options:
3281 dview_diff_options (dview);
3282 break;
3283 #ifdef HAVE_CHARSET
3284 case CK_SelectCodepage:
3285 dview_select_encoding (dview);
3286 break;
3287 #endif
3288 case CK_Cancel:
3289 /* don't close diffviewer due to SIGINT */
3290 break;
3291 default:
3292 res = MSG_NOT_HANDLED;
3294 return res;
3297 /* --------------------------------------------------------------------------------------------- */
3299 static cb_ret_t
3300 dview_handle_key (WDiff * dview, int key)
3302 unsigned long command;
3304 #ifdef HAVE_CHARSET
3305 key = convert_from_input_c (key);
3306 #endif
3308 command = keybind_lookup_keymap_command (diff_map, key);
3309 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3310 return MSG_HANDLED;
3312 /* Key not used */
3313 return MSG_NOT_HANDLED;
3316 /* --------------------------------------------------------------------------------------------- */
3318 static cb_ret_t
3319 dview_callback (Widget * w, widget_msg_t msg, int parm)
3321 WDiff *dview = (WDiff *) w;
3322 Dlg_head *h = dview->widget.owner;
3323 cb_ret_t i;
3325 switch (msg)
3327 case WIDGET_INIT:
3328 dview_labels (dview);
3329 dview_load_options (dview);
3330 dview_update (dview);
3331 return MSG_HANDLED;
3333 case WIDGET_DRAW:
3334 dview->new_frame = 1;
3335 dview_update (dview);
3336 return MSG_HANDLED;
3338 case WIDGET_KEY:
3339 i = dview_handle_key (dview, parm);
3340 if (dview->view_quit)
3341 dlg_stop (h);
3342 else
3343 dview_update (dview);
3344 return i;
3346 case WIDGET_COMMAND:
3347 i = dview_execute_cmd (dview, parm);
3348 if (dview->view_quit)
3349 dlg_stop (h);
3350 else
3351 dview_update (dview);
3352 return i;
3354 case WIDGET_DESTROY:
3355 dview_save_options (dview);
3356 dview_fini (dview);
3357 return MSG_HANDLED;
3359 default:
3360 return default_proc (msg, parm);
3364 /* --------------------------------------------------------------------------------------------- */
3366 static void
3367 dview_adjust_size (Dlg_head * h)
3369 WDiff *dview;
3370 WButtonBar *bar;
3372 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3373 dview = (WDiff *) find_widget_type (h, dview_callback);
3374 bar = find_buttonbar (h);
3375 widget_set_size (&dview->widget, 0, 0, LINES - 1, COLS);
3376 widget_set_size ((Widget *) bar, LINES - 1, 0, 1, COLS);
3378 dview_compute_areas (dview);
3381 /* --------------------------------------------------------------------------------------------- */
3383 static cb_ret_t
3384 dview_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
3386 WDiff *dview = (WDiff *) data;
3388 switch (msg)
3390 case DLG_RESIZE:
3391 dview_adjust_size (h);
3392 return MSG_HANDLED;
3394 case DLG_ACTION:
3395 /* shortcut */
3396 if (sender == NULL)
3397 return dview_execute_cmd (NULL, parm);
3398 /* message from buttonbar */
3399 if (sender == (Widget *) find_buttonbar (h))
3401 if (data != NULL)
3402 return send_message ((Widget *) data, WIDGET_COMMAND, parm);
3404 dview = (WDiff *) find_widget_type (h, dview_callback);
3405 return dview_execute_cmd (dview, parm);
3407 return MSG_NOT_HANDLED;
3409 case DLG_VALIDATE:
3410 dview = (WDiff *) find_widget_type (h, dview_callback);
3411 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
3412 if (dview_ok_to_exit (dview))
3413 h->state = DLG_CLOSED;
3414 return MSG_HANDLED;
3416 default:
3417 return default_dlg_callback (h, sender, msg, parm, data);
3421 /* --------------------------------------------------------------------------------------------- */
3423 static char *
3424 dview_get_title (const Dlg_head * h, size_t len)
3426 const WDiff *dview;
3427 const char *modified = " (*) ";
3428 const char *notmodified = " ";
3429 size_t len1;
3430 GString *title;
3432 dview = (const WDiff *) find_widget_type (h, dview_callback);
3433 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3435 title = g_string_sized_new (len);
3436 g_string_append (title, _("Diff:"));
3437 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3438 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3439 g_string_append (title, " | ");
3440 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3441 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3443 return g_string_free (title, FALSE);
3446 /* --------------------------------------------------------------------------------------------- */
3448 static int
3449 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3451 int error;
3452 WDiff *dview;
3453 Dlg_head *dview_dlg;
3455 /* Create dialog and widgets, put them on the dialog */
3456 dview_dlg =
3457 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, dview_dialog_callback, NULL,
3458 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3460 dview = g_new0 (WDiff, 1);
3462 init_widget (&dview->widget, 0, 0, LINES - 1, COLS,
3463 (callback_fn) dview_callback, (mouse_h) dview_event);
3465 widget_want_cursor (dview->widget, 0);
3467 add_widget (dview_dlg, dview);
3468 add_widget (dview_dlg, buttonbar_new (TRUE));
3470 dview_dlg->get_title = dview_get_title;
3472 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3474 /* Please note that if you add another widget,
3475 * you have to modify dview_adjust_size to
3476 * be aware of it
3478 if (error == 0)
3479 run_dlg (dview_dlg);
3481 if ((error != 0) || (dview_dlg->state == DLG_CLOSED))
3482 destroy_dlg (dview_dlg);
3484 return error == 0 ? 1 : 0;
3487 /*** public functions ****************************************************************************/
3488 /* --------------------------------------------------------------------------------------------- */
3490 #define GET_FILE_AND_STAMP(n) \
3491 do \
3493 use_copy##n = 0; \
3494 real_file##n = file##n; \
3495 if (!vfs_file_is_local (file##n)) \
3497 real_file##n = mc_getlocalcopy (file##n); \
3498 if (real_file##n != NULL) \
3500 use_copy##n = 1; \
3501 if (mc_stat (real_file##n, &st##n) != 0) \
3502 use_copy##n = -1; \
3506 while (0)
3508 #define UNGET_FILE(n) \
3509 do \
3511 if (use_copy##n) \
3513 int changed = 0; \
3514 if (use_copy##n > 0) \
3516 time_t mtime; \
3517 mtime = st##n.st_mtime; \
3518 if (mc_stat (real_file##n, &st##n) == 0) \
3519 changed = (mtime != st##n.st_mtime); \
3521 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3522 vfs_path_free (real_file##n); \
3525 while (0)
3527 gboolean
3528 dview_diff_cmd (const void *f0, const void *f1)
3530 int rv = 0;
3531 vfs_path_t *file0 = NULL;
3532 vfs_path_t *file1 = NULL;
3533 gboolean is_dir0 = FALSE;
3534 gboolean is_dir1 = FALSE;
3536 switch (mc_global.mc_run_mode)
3538 case MC_RUN_FULL:
3540 /* run from panels */
3541 const WPanel *panel0 = (const WPanel *) f0;
3542 const WPanel *panel1 = (const WPanel *) f1;
3544 file0 = vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, NULL);
3545 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3546 if (is_dir0)
3548 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3549 path_trunc (selection (panel0)->fname, 30));
3550 goto ret;
3553 file1 = vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, NULL);
3554 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3555 if (is_dir1)
3557 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3558 path_trunc (selection (panel1)->fname, 30));
3559 goto ret;
3561 break;
3564 case MC_RUN_DIFFVIEWER:
3566 /* run from command line */
3567 const char *p0 = (const char *) f0;
3568 const char *p1 = (const char *) f1;
3569 struct stat st;
3571 file0 = vfs_path_from_str (p0);
3572 if (mc_stat (file0, &st) == 0)
3574 is_dir0 = S_ISDIR (st.st_mode);
3575 if (is_dir0)
3577 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3578 goto ret;
3581 else
3583 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3584 path_trunc (p0, 30), unix_error_string (errno));
3585 goto ret;
3588 file1 = vfs_path_from_str (p1);
3589 if (mc_stat (file1, &st) == 0)
3591 is_dir1 = S_ISDIR (st.st_mode);
3592 if (is_dir1)
3594 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3595 goto ret;
3598 else
3600 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3601 path_trunc (p1, 30), unix_error_string (errno));
3602 goto ret;
3604 break;
3607 default:
3608 /* this should not happaned */
3609 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3610 return FALSE;
3613 if (rv == 0)
3615 rv = -1;
3616 if (file0 != NULL && file1 != NULL)
3618 int use_copy0;
3619 int use_copy1;
3620 struct stat st0;
3621 struct stat st1;
3622 vfs_path_t *real_file0;
3623 vfs_path_t *real_file1;
3625 GET_FILE_AND_STAMP (0);
3626 GET_FILE_AND_STAMP (1);
3627 if (real_file0 != NULL && real_file1 != NULL)
3629 char *real_file0_str, *real_file1_str;
3630 char *file0_str, *file1_str;
3632 real_file0_str = vfs_path_to_str (real_file0);
3633 real_file1_str = vfs_path_to_str (real_file1);
3634 file0_str = vfs_path_to_str (file0);
3635 file1_str = vfs_path_to_str (file1);
3636 rv = diff_view (real_file0_str, real_file1_str, file0_str, file1_str);
3637 g_free (real_file0_str);
3638 g_free (real_file1_str);
3639 g_free (file0_str);
3640 g_free (file1_str);
3642 UNGET_FILE (1);
3643 UNGET_FILE (0);
3647 if (rv == 0)
3648 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3650 ret:
3651 vfs_path_free (file1);
3652 vfs_path_free (file0);
3654 return (rv != 0);
3657 #undef GET_FILE_AND_STAMP
3658 #undef UNGET_FILE
3660 /* --------------------------------------------------------------------------------------------- */