Aggressive use WIDGET macro.
[midnight-commander.git] / src / diffviewer / ydiff.c
blobff3a2c4320d937af1379c3d2c1268b7cfed74166
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 * @return 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 * @return 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 * @return 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 * @return file structure
251 static FBUF *
252 f_temp (void)
254 int fd;
255 FBUF *fs;
257 fs = f_dopen (0);
258 if (fs == NULL)
259 return NULL;
261 fd = open_temp (&fs->data);
262 if (fd < 0)
264 f_free (fs);
265 return NULL;
268 fs->fd = fd;
269 fs->flags = FILE_FLAG_TEMP;
270 return fs;
273 /* --------------------------------------------------------------------------------------------- */
276 * Open a binary file in specified mode.
278 * @param filename file name
279 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
281 * @return file structure
284 static FBUF *
285 f_open (const char *filename, int flags)
287 int fd;
288 FBUF *fs;
290 fs = f_dopen (0);
291 if (fs == NULL)
292 return NULL;
294 fd = open (filename, flags);
295 if (fd < 0)
297 f_free (fs);
298 return NULL;
301 fs->fd = fd;
302 return fs;
305 /* --------------------------------------------------------------------------------------------- */
308 * Read a line of bytes from file until newline or EOF.
309 * @note does not stop on null-byte
310 * @note buf will not be null-terminated
312 * @param buf destination buffer
313 * @param size size of buffer
314 * @param fs file structure
316 * @return number of bytes read
319 static size_t
320 f_gets (char *buf, size_t size, FBUF * fs)
322 size_t j = 0;
326 int i;
327 int stop = 0;
329 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
331 buf[j] = fs->buf[i];
332 if (buf[j] == '\n')
333 stop = 1;
335 fs->pos = i;
337 if (j == size || stop)
338 break;
340 fs->pos = 0;
341 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
343 while (fs->len > 0);
345 return j;
348 /* --------------------------------------------------------------------------------------------- */
351 * Seek into file.
352 * @note avoids thrashing read cache when possible
354 * @param fs file structure
355 * @param off offset
356 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
358 * @return position in file, starting from begginning
361 static off_t
362 f_seek (FBUF * fs, off_t off, int whence)
364 off_t rv;
366 if (fs->len && whence != SEEK_END)
368 rv = lseek (fs->fd, 0, SEEK_CUR);
369 if (rv != -1)
371 if (whence == SEEK_CUR)
373 whence = SEEK_SET;
374 off += rv - fs->len + fs->pos;
376 if (off - rv >= -fs->len && off - rv <= 0)
378 fs->pos = fs->len + off - rv;
379 return off;
384 rv = lseek (fs->fd, off, whence);
385 if (rv != -1)
386 FILE_DIRTY (fs);
387 return rv;
390 /* --------------------------------------------------------------------------------------------- */
393 * Seek to the beginning of file, thrashing read cache.
395 * @param fs file structure
397 * @return 0 if success, non-zero on error
400 static off_t
401 f_reset (FBUF * fs)
403 off_t rv;
405 rv = lseek (fs->fd, 0, SEEK_SET);
406 if (rv != -1)
407 FILE_DIRTY (fs);
408 return rv;
411 /* --------------------------------------------------------------------------------------------- */
414 * Write bytes to file.
415 * @note thrashes read cache
417 * @param fs file structure
418 * @param buf source buffer
419 * @param size size of buffer
421 * @return number of written bytes, -1 on error
424 static ssize_t
425 f_write (FBUF * fs, const char *buf, size_t size)
427 ssize_t rv;
429 rv = write (fs->fd, buf, size);
430 if (rv >= 0)
431 FILE_DIRTY (fs);
432 return rv;
435 /* --------------------------------------------------------------------------------------------- */
438 * Truncate file to the current position.
439 * @note thrashes read cache
441 * @param fs file structure
443 * @return current file size on success, negative on error
446 static off_t
447 f_trunc (FBUF * fs)
449 off_t off;
451 off = lseek (fs->fd, 0, SEEK_CUR);
452 if (off != -1)
454 int rv;
456 rv = ftruncate (fs->fd, off);
457 if (rv != 0)
458 off = -1;
459 else
460 FILE_DIRTY (fs);
462 return off;
465 /* --------------------------------------------------------------------------------------------- */
468 * Close file.
469 * @note if this is temporary file, it is deleted
471 * @param fs file structure
472 * @return 0 on success, non-zero on error
475 static int
476 f_close (FBUF * fs)
478 int rv = -1;
480 if (fs != NULL)
482 rv = close (fs->fd);
483 f_free (fs);
486 return rv;
489 /* --------------------------------------------------------------------------------------------- */
492 * Create pipe stream to process.
494 * @param cmd shell command line
495 * @param flags open mode, either O_RDONLY or O_WRONLY
497 * @return file structure
500 static FBUF *
501 p_open (const char *cmd, int flags)
503 FILE *f;
504 FBUF *fs;
505 const char *type = NULL;
507 if (flags == O_RDONLY)
508 type = "r";
509 else if (flags == O_WRONLY)
510 type = "w";
512 if (type == NULL)
513 return NULL;
515 fs = f_dopen (0);
516 if (fs == NULL)
517 return NULL;
519 f = popen (cmd, type);
520 if (f == NULL)
522 f_free (fs);
523 return NULL;
526 fs->fd = fileno (f);
527 fs->data = f;
528 return fs;
531 /* --------------------------------------------------------------------------------------------- */
534 * Close pipe stream.
536 * @param fs structure
537 * @return 0 on success, non-zero on error
540 static int
541 p_close (FBUF * fs)
543 int rv = -1;
545 if (fs != NULL)
547 rv = pclose (fs->data);
548 f_free (fs);
551 return rv;
554 /* --------------------------------------------------------------------------------------------- */
557 * Get one char (byte) from string
559 * @param char * str, gboolean * result
560 * @return int as character or 0 and result == FALSE if fail
563 static int
564 dview_get_byte (char *str, gboolean * result)
566 if (str == NULL)
568 *result = FALSE;
569 return 0;
571 *result = TRUE;
572 return (unsigned char) *str;
575 /* --------------------------------------------------------------------------------------------- */
578 * Get utf multibyte char from string
580 * @param char * str, int * char_width, gboolean * result
581 * @return int as utf character or 0 and result == FALSE if fail
584 static int
585 dview_get_utf (char *str, int *char_width, gboolean * result)
587 int res = -1;
588 gunichar ch;
589 gchar *next_ch = NULL;
590 int width = 0;
592 *result = TRUE;
594 if (str == NULL)
596 *result = FALSE;
597 return 0;
600 res = g_utf8_get_char_validated (str, -1);
602 if (res < 0)
603 ch = *str;
604 else
606 ch = res;
607 /* Calculate UTF-8 char width */
608 next_ch = g_utf8_next_char (str);
609 if (next_ch != NULL)
610 width = next_ch - str;
611 else
612 ch = 0;
614 *char_width = width;
615 return ch;
618 /* --------------------------------------------------------------------------------------------- */
620 static int
621 dview_str_utf8_offset_to_pos (const char *text, size_t length)
623 ptrdiff_t result;
625 if (text == NULL || text[0] == '\0')
626 return length;
628 if (g_utf8_validate (text, -1, NULL))
629 result = g_utf8_offset_to_pointer (text, length) - text;
630 else
632 gunichar uni;
633 char *tmpbuf, *buffer;
635 buffer = tmpbuf = g_strdup (text);
636 while (tmpbuf[0] != '\0')
638 uni = g_utf8_get_char_validated (tmpbuf, -1);
639 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
640 tmpbuf = g_utf8_next_char (tmpbuf);
641 else
643 tmpbuf[0] = '.';
644 tmpbuf++;
647 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
648 g_free (buffer);
650 return max (length, (size_t) result);
653 /* --------------------------------------------------------------------------------------------- */
655 /* diff parse *************************************************************** */
658 * Read decimal number from string.
660 * @param[in,out] str string to parse
661 * @param[out] n extracted number
662 * @return 0 if success, otherwise non-zero
665 static int
666 scan_deci (const char **str, int *n)
668 const char *p = *str;
669 char *q;
671 errno = 0;
672 *n = strtol (p, &q, 10);
673 if (errno != 0 || p == q)
674 return -1;
675 *str = q;
676 return 0;
679 /* --------------------------------------------------------------------------------------------- */
682 * Parse line for diff statement.
684 * @param p string to parse
685 * @param ops list of diff statements
686 * @return 0 if success, otherwise non-zero
689 static int
690 scan_line (const char *p, GArray * ops)
692 DIFFCMD op;
694 int f1, f2;
695 int t1, t2;
696 int cmd;
697 int range;
699 /* handle the following cases:
700 * NUMaNUM[,NUM]
701 * NUM[,NUM]cNUM[,NUM]
702 * NUM[,NUM]dNUM
703 * where NUM is a positive integer
706 if (scan_deci (&p, &f1) != 0 || f1 < 0)
707 return -1;
709 f2 = f1;
710 range = 0;
711 if (*p == ',')
713 p++;
714 if (scan_deci (&p, &f2) != 0 || f2 < f1)
715 return -1;
717 range = 1;
720 cmd = *p++;
721 if (cmd == 'a')
723 if (range != 0)
724 return -1;
726 else if (cmd != 'c' && cmd != 'd')
727 return -1;
729 if (scan_deci (&p, &t1) != 0 || t1 < 0)
730 return -1;
732 t2 = t1;
733 range = 0;
734 if (*p == ',')
736 p++;
737 if (scan_deci (&p, &t2) != 0 || t2 < t1)
738 return -1;
740 range = 1;
743 if (cmd == 'd' && range != 0)
744 return -1;
746 op.a[0][0] = f1;
747 op.a[0][1] = f2;
748 op.cmd = cmd;
749 op.a[1][0] = t1;
750 op.a[1][1] = t2;
751 g_array_append_val (ops, op);
752 return 0;
755 /* --------------------------------------------------------------------------------------------- */
758 * Parse diff output and extract diff statements.
760 * @param f stream to read from
761 * @param ops list of diff statements to fill
762 * @return positive number indicating number of hunks, otherwise negative
765 static int
766 scan_diff (FBUF * f, GArray * ops)
768 int sz;
769 char buf[BUFSIZ];
771 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
773 if (isdigit (buf[0]))
775 if (buf[sz - 1] != '\n')
776 return -1;
778 buf[sz] = '\0';
779 if (scan_line (buf, ops) != 0)
780 return -1;
782 continue;
785 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
789 return ops->len;
792 /* --------------------------------------------------------------------------------------------- */
795 * Invoke diff and extract diff statements.
797 * @param args extra arguments to be passed to diff
798 * @param extra more arguments to be passed to diff
799 * @param file1 first file to compare
800 * @param file2 second file to compare
801 * @param ops list of diff statements to fill
803 * @return positive number indicating number of hunks, otherwise negative
806 static int
807 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
808 GArray * ops)
810 static const char *opt =
811 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
812 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
813 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
814 " --unchanged-group-format=''";
816 int rv;
817 FBUF *f;
818 char *cmd;
819 int code;
820 char *file1_esc, *file2_esc;
822 /* escape potential $ to avoid shell variable substitutions in popen() */
823 file1_esc = strutils_shell_escape (file1);
824 file2_esc = strutils_shell_escape (file2);
825 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
826 g_free (file1_esc);
827 g_free (file2_esc);
829 if (cmd == NULL)
830 return -1;
832 f = p_open (cmd, O_RDONLY);
833 g_free (cmd);
835 if (f == NULL)
836 return -1;
838 rv = scan_diff (f, ops);
839 code = p_close (f);
841 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
842 rv = -1;
844 return rv;
847 /* --------------------------------------------------------------------------------------------- */
850 * Reparse and display file according to diff statements.
852 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
853 * @param filename file name to display
854 * @param ops list of diff statements
855 * @param printer printf-like function to be used for displaying
856 * @param ctx printer context
858 * @return 0 if success, otherwise non-zero
861 static int
862 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
864 size_t i;
865 FBUF *f;
866 size_t sz;
867 char buf[BUFSIZ];
868 int line = 0;
869 off_t off = 0;
870 const DIFFCMD *op;
871 diff_place_t eff;
872 int add_cmd;
873 int del_cmd;
875 f = f_open (filename, O_RDONLY);
876 if (f == NULL)
877 return -1;
879 ord &= 1;
880 eff = ord;
882 add_cmd = 'a';
883 del_cmd = 'd';
884 if (ord != 0)
886 add_cmd = 'd';
887 del_cmd = 'a';
889 #define F1 a[eff][0]
890 #define F2 a[eff][1]
891 #define T1 a[ ord^1 ][0]
892 #define T2 a[ ord^1 ][1]
893 for (i = 0; i < ops->len; i++)
895 int n;
897 op = &g_array_index (ops, DIFFCMD, i);
898 n = op->F1 - (op->cmd != add_cmd);
900 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
902 line++;
903 printer (ctx, EQU_CH, line, off, sz, buf);
904 off += sz;
905 while (buf[sz - 1] != '\n')
907 sz = f_gets (buf, sizeof (buf), f);
908 if (sz == 0)
910 printer (ctx, 0, 0, 0, 1, "\n");
911 break;
913 printer (ctx, 0, 0, 0, sz, buf);
914 off += sz;
918 if (line != n)
919 goto err;
921 if (op->cmd == add_cmd)
923 n = op->T2 - op->T1 + 1;
924 while (n != 0)
926 printer (ctx, DEL_CH, 0, 0, 1, "\n");
927 n--;
931 if (op->cmd == del_cmd)
933 n = op->F2 - op->F1 + 1;
934 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
936 line++;
937 printer (ctx, ADD_CH, line, off, sz, buf);
938 off += sz;
939 while (buf[sz - 1] != '\n')
941 sz = f_gets (buf, sizeof (buf), f);
942 if (sz == 0)
944 printer (ctx, 0, 0, 0, 1, "\n");
945 break;
947 printer (ctx, 0, 0, 0, sz, buf);
948 off += sz;
950 n--;
953 if (n != 0)
954 goto err;
957 if (op->cmd == 'c')
959 n = op->F2 - op->F1 + 1;
960 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
962 line++;
963 printer (ctx, CHG_CH, line, off, sz, buf);
964 off += sz;
965 while (buf[sz - 1] != '\n')
967 sz = f_gets (buf, sizeof (buf), f);
968 if (sz == 0)
970 printer (ctx, 0, 0, 0, 1, "\n");
971 break;
973 printer (ctx, 0, 0, 0, sz, buf);
974 off += sz;
976 n--;
979 if (n != 0)
980 goto err;
982 n = op->T2 - op->T1 - (op->F2 - op->F1);
983 while (n > 0)
985 printer (ctx, CHG_CH, 0, 0, 1, "\n");
986 n--;
990 #undef T2
991 #undef T1
992 #undef F2
993 #undef F1
995 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
997 line++;
998 printer (ctx, EQU_CH, line, off, sz, buf);
999 off += sz;
1000 while (buf[sz - 1] != '\n')
1002 sz = f_gets (buf, sizeof (buf), f);
1003 if (sz == 0)
1005 printer (ctx, 0, 0, 0, 1, "\n");
1006 break;
1008 printer (ctx, 0, 0, 0, sz, buf);
1009 off += sz;
1013 f_close (f);
1014 return 0;
1016 err:
1017 f_close (f);
1018 return -1;
1021 /* --------------------------------------------------------------------------------------------- */
1023 /* horizontal diff ********************************************************** */
1026 * Longest common substring.
1028 * @param s first string
1029 * @param m length of first string
1030 * @param t second string
1031 * @param n length of second string
1032 * @param ret list of offsets for longest common substrings inside each string
1033 * @param min minimum length of common substrings
1035 * @return 0 if success, nonzero otherwise
1038 static int
1039 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1041 int i, j;
1042 int *Lprev, *Lcurr;
1043 int z = 0;
1045 if (m < min || n < min)
1047 /* XXX early culling */
1048 return 0;
1051 Lprev = g_try_new0 (int, n + 1);
1052 if (Lprev == NULL)
1053 return -1;
1055 Lcurr = g_try_new0 (int, n + 1);
1056 if (Lcurr == NULL)
1058 g_free (Lprev);
1059 return -1;
1062 for (i = 0; i < m; i++)
1064 int *L;
1066 L = Lprev;
1067 Lprev = Lcurr;
1068 Lcurr = L;
1069 #ifdef USE_MEMSET_IN_LCS
1070 memset (Lcurr, 0, (n + 1) * sizeof (int));
1071 #endif
1072 for (j = 0; j < n; j++)
1074 #ifndef USE_MEMSET_IN_LCS
1075 Lcurr[j + 1] = 0;
1076 #endif
1077 if (s[i] == t[j])
1079 int v;
1081 v = Lprev[j] + 1;
1082 Lcurr[j + 1] = v;
1083 if (z < v)
1085 z = v;
1086 g_array_set_size (ret, 0);
1088 if (z == v && z >= min)
1090 int off0, off1;
1091 size_t k;
1093 off0 = i - z + 1;
1094 off1 = j - z + 1;
1096 for (k = 0; k < ret->len; k++)
1098 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1099 if ((*p)[0] == off0 || (*p)[1] >= off1)
1100 break;
1102 if (k == ret->len)
1104 PAIR p2;
1106 p2[0] = off0;
1107 p2[1] = off1;
1108 g_array_append_val (ret, p2);
1115 free (Lcurr);
1116 free (Lprev);
1117 return z;
1120 /* --------------------------------------------------------------------------------------------- */
1123 * Scan recursively for common substrings and build ranges.
1125 * @param s first string
1126 * @param t second string
1127 * @param bracket current limits for both of the strings
1128 * @param min minimum length of common substrings
1129 * @param hdiff list of horizontal diff ranges to fill
1130 * @param depth recursion depth
1132 * @return 0 if success, nonzero otherwise
1135 static gboolean
1136 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1137 unsigned int depth)
1139 BRACKET p;
1141 if (depth-- != 0)
1143 GArray *ret;
1144 BRACKET b;
1145 int len;
1147 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1148 if (ret == NULL)
1149 return FALSE;
1151 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1152 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1153 if (ret->len != 0)
1155 size_t k = 0;
1156 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1157 const PAIR *data2;
1159 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1160 b[DIFF_LEFT].len = (*data)[0];
1161 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1162 b[DIFF_RIGHT].len = (*data)[1];
1163 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1164 return FALSE;
1166 for (k = 0; k < ret->len - 1; k++)
1168 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1169 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1170 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1171 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1172 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1173 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1174 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1175 return FALSE;
1177 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1178 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1179 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1180 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1181 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1182 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1183 return FALSE;
1185 g_array_free (ret, TRUE);
1186 return TRUE;
1190 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1191 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1192 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1193 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1194 g_array_append_val (hdiff, p);
1196 return TRUE;
1199 /* --------------------------------------------------------------------------------------------- */
1202 * Build list of horizontal diff ranges.
1204 * @param s first string
1205 * @param m length of first string
1206 * @param t second string
1207 * @param n length of second string
1208 * @param min minimum length of common substrings
1209 * @param hdiff list of horizontal diff ranges to fill
1210 * @param depth recursion depth
1212 * @return 0 if success, nonzero otherwise
1215 static gboolean
1216 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1218 int i;
1219 BRACKET b;
1221 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1222 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1224 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1227 b[DIFF_LEFT].off = i;
1228 b[DIFF_LEFT].len = m - i;
1229 b[DIFF_RIGHT].off = i;
1230 b[DIFF_RIGHT].len = n - i;
1232 /* smartscan (multiple horizontal diff) */
1233 return hdiff_multi (s, t, b, min, hdiff, depth);
1236 /* --------------------------------------------------------------------------------------------- */
1238 /* read line **************************************************************** */
1241 * Check if character is inside horizontal diff limits.
1243 * @param k rank of character inside line
1244 * @param hdiff horizontal diff structure
1245 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1247 * @return TRUE if inside hdiff limits, FALSE otherwise
1250 static gboolean
1251 is_inside (int k, GArray * hdiff, diff_place_t ord)
1253 size_t i;
1254 BRACKET *b;
1256 for (i = 0; i < hdiff->len; i++)
1258 int start, end;
1260 b = &g_array_index (hdiff, BRACKET, i);
1261 start = (*b)[ord].off;
1262 end = start + (*b)[ord].len;
1263 if (k >= start && k < end)
1264 return TRUE;
1266 return FALSE;
1269 /* --------------------------------------------------------------------------------------------- */
1272 * Copy 'src' to 'dst' expanding tabs.
1273 * @note The procedure returns when all bytes are consumed from 'src'
1275 * @param dst destination buffer
1276 * @param src source buffer
1277 * @param srcsize size of src buffer
1278 * @param base virtual base of this string, needed to calculate tabs
1279 * @param ts tab size
1281 * @return new virtual base
1284 static int
1285 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1287 int i;
1289 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1291 *dst = *src;
1292 if (*src == '\t')
1294 int j;
1296 j = TAB_SKIP (ts, i + base);
1297 i += j - 1;
1298 while (j-- > 0)
1299 *dst++ = ' ';
1300 dst--;
1303 return i + base;
1306 /* --------------------------------------------------------------------------------------------- */
1309 * Copy 'src' to 'dst' expanding tabs.
1311 * @param dst destination buffer
1312 * @param dstsize size of dst buffer
1313 * @param[in,out] _src source buffer
1314 * @param srcsize size of src buffer
1315 * @param base virtual base of this string, needed to calculate tabs
1316 * @param ts tab size
1318 * @return new virtual base
1320 * @note The procedure returns when all bytes are consumed from 'src'
1321 * or 'dstsize' bytes are written to 'dst'
1322 * @note Upon return, 'src' points to the first unwritten character in source
1325 static int
1326 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1328 int i;
1329 const char *src = *_src;
1331 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1333 *dst = *src;
1334 if (*src == '\t')
1336 int j;
1338 j = TAB_SKIP (ts, i + base);
1339 if (j > dstsize - i)
1340 j = dstsize - i;
1341 i += j - 1;
1342 while (j-- > 0)
1343 *dst++ = ' ';
1344 dst--;
1347 *_src = src;
1348 return i + base;
1351 /* --------------------------------------------------------------------------------------------- */
1354 * Read line from memory, converting tabs to spaces and padding with spaces.
1356 * @param src buffer to read from
1357 * @param srcsize size of src buffer
1358 * @param dst buffer to read to
1359 * @param dstsize size of dst buffer, excluding trailing null
1360 * @param skip number of characters to skip
1361 * @param ts tab size
1362 * @param show_cr show trailing carriage return as ^M
1364 * @return negative on error, otherwise number of bytes except padding
1367 static int
1368 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr)
1370 int sz = 0;
1372 if (src != NULL)
1374 int i;
1375 char *tmp = dst;
1376 const int base = 0;
1378 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1380 if (*src == '\t')
1382 int j;
1384 j = TAB_SKIP (ts, i + base);
1385 i += j - 1;
1386 while (j-- > 0)
1388 if (skip > 0)
1389 skip--;
1390 else if (dstsize != 0)
1392 dstsize--;
1393 *dst++ = ' ';
1397 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1399 if (skip == 0 && show_cr)
1401 if (dstsize > 1)
1403 dstsize -= 2;
1404 *dst++ = '^';
1405 *dst++ = 'M';
1407 else
1409 dstsize--;
1410 *dst++ = '.';
1413 break;
1415 else if (skip > 0)
1417 int utf_ch = 0;
1418 gboolean res;
1419 int w;
1421 skip--;
1422 utf_ch = dview_get_utf ((char *) src, &w, &res);
1423 if (w > 1)
1424 skip += w - 1;
1425 if (!g_unichar_isprint (utf_ch))
1426 utf_ch = '.';
1428 else
1430 dstsize--;
1431 *dst++ = *src;
1434 sz = dst - tmp;
1436 while (dstsize != 0)
1438 dstsize--;
1439 *dst++ = ' ';
1441 *dst = '\0';
1442 return sz;
1445 /* --------------------------------------------------------------------------------------------- */
1448 * Read line from memory and build attribute array.
1450 * @param src buffer to read from
1451 * @param srcsize size of src buffer
1452 * @param dst buffer to read to
1453 * @param dstsize size of dst buffer, excluding trailing null
1454 * @param skip number of characters to skip
1455 * @param ts tab size
1456 * @param show_cr show trailing carriage return as ^M
1457 * @param hdiff horizontal diff structure
1458 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1459 * @param att buffer of attributes
1461 * @return negative on error, otherwise number of bytes except padding
1464 static int
1465 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr,
1466 GArray * hdiff, diff_place_t ord, char *att)
1468 int sz = 0;
1470 if (src != NULL)
1472 int i, k;
1473 char *tmp = dst;
1474 const int base = 0;
1476 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1478 if (*src == '\t')
1480 int j;
1482 j = TAB_SKIP (ts, i + base);
1483 i += j - 1;
1484 while (j-- > 0)
1486 if (skip != 0)
1487 skip--;
1488 else if (dstsize != 0)
1490 dstsize--;
1491 *att++ = is_inside (k, hdiff, ord);
1492 *dst++ = ' ';
1496 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1498 if (skip == 0 && show_cr)
1500 if (dstsize > 1)
1502 dstsize -= 2;
1503 *att++ = is_inside (k, hdiff, ord);
1504 *dst++ = '^';
1505 *att++ = is_inside (k, hdiff, ord);
1506 *dst++ = 'M';
1508 else
1510 dstsize--;
1511 *att++ = is_inside (k, hdiff, ord);
1512 *dst++ = '.';
1515 break;
1517 else if (skip != 0)
1519 int utf_ch = 0;
1520 gboolean res;
1521 int w;
1523 skip--;
1524 utf_ch = dview_get_utf ((char *) src, &w, &res);
1525 if (w > 1)
1526 skip += w - 1;
1527 if (!g_unichar_isprint (utf_ch))
1528 utf_ch = '.';
1530 else
1532 dstsize--;
1533 *att++ = is_inside (k, hdiff, ord);
1534 *dst++ = *src;
1537 sz = dst - tmp;
1539 while (dstsize != 0)
1541 dstsize--;
1542 *att++ = '\0';
1543 *dst++ = ' ';
1545 *dst = '\0';
1546 return sz;
1549 /* --------------------------------------------------------------------------------------------- */
1552 * Read line from file, converting tabs to spaces and padding with spaces.
1554 * @param f file stream to read from
1555 * @param off offset of line inside file
1556 * @param dst buffer to read to
1557 * @param dstsize size of dst buffer, excluding trailing null
1558 * @param skip number of characters to skip
1559 * @param ts tab size
1560 * @param show_cr show trailing carriage return as ^M
1562 * @return negative on error, otherwise number of bytes except padding
1565 static int
1566 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, int show_cr)
1568 int base = 0;
1569 int old_base = base;
1570 size_t amount = dstsize;
1571 size_t useful, offset;
1572 size_t i;
1573 size_t sz;
1574 int lastch = '\0';
1575 const char *q = NULL;
1576 char tmp[BUFSIZ]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1577 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1579 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1581 /* abnormal, but avoid buffer overflow */
1582 memset (dst, ' ', dstsize);
1583 dst[dstsize] = '\0';
1584 return 0;
1587 f_seek (f, off, SEEK_SET);
1589 while (skip > base)
1591 old_base = base;
1592 sz = f_gets (tmp, amount, f);
1593 if (sz == 0)
1594 break;
1596 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1597 if (cvt[base - old_base - 1] == '\n')
1599 q = &cvt[base - old_base - 1];
1600 base = old_base + q - cvt + 1;
1601 break;
1605 if (base < skip)
1607 memset (dst, ' ', dstsize);
1608 dst[dstsize] = '\0';
1609 return 0;
1612 useful = base - skip;
1613 offset = skip - old_base;
1615 if (useful <= dstsize)
1617 if (useful != 0)
1618 memmove (dst, cvt + offset, useful);
1620 if (q == NULL)
1622 sz = f_gets (tmp, dstsize - useful + 1, f);
1623 if (sz != 0)
1625 const char *ptr = tmp;
1627 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1628 if (ptr < tmp + sz)
1629 lastch = *ptr;
1632 sz = useful;
1634 else
1636 memmove (dst, cvt + offset, dstsize);
1637 sz = dstsize;
1638 lastch = cvt[offset + dstsize];
1641 dst[sz] = lastch;
1642 for (i = 0; i < sz && dst[i] != '\n'; i++)
1644 if (dst[i] == '\r' && dst[i + 1] == '\n')
1646 if (show_cr)
1648 if (i + 1 < dstsize)
1650 dst[i++] = '^';
1651 dst[i++] = 'M';
1653 else
1655 dst[i++] = '*';
1658 break;
1662 for (; i < dstsize; i++)
1663 dst[i] = ' ';
1664 dst[i] = '\0';
1665 return sz;
1668 /* --------------------------------------------------------------------------------------------- */
1669 /* diff printers et al ****************************************************** */
1671 static void
1672 cc_free_elt (void *elt)
1674 DIFFLN *p = elt;
1676 if (p != NULL)
1677 g_free (p->p);
1680 /* --------------------------------------------------------------------------------------------- */
1682 static int
1683 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1685 GArray *a = ((PRINTER_CTX *) ctx)->a;
1686 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1688 if (ch != 0)
1690 DIFFLN p;
1692 p.p = NULL;
1693 p.ch = ch;
1694 p.line = line;
1695 p.u.off = off;
1696 if (dsrc == DATA_SRC_MEM && line != 0)
1698 if (sz != 0 && str[sz - 1] == '\n')
1699 sz--;
1700 if (sz > 0)
1701 p.p = g_strndup (str, sz);
1702 p.u.len = sz;
1704 g_array_append_val (a, p);
1706 else if (dsrc == DATA_SRC_MEM)
1708 DIFFLN *p;
1710 p = &g_array_index (a, DIFFLN, a->len - 1);
1711 if (sz != 0 && str[sz - 1] == '\n')
1712 sz--;
1713 if (sz != 0)
1715 size_t new_size;
1716 char *q;
1718 new_size = p->u.len + sz;
1719 q = g_realloc (p->p, new_size);
1720 memcpy (q + p->u.len, str, sz);
1721 p->p = q;
1723 p->u.len += sz;
1725 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1727 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1728 f_write (f, str, sz);
1730 return 0;
1733 /* --------------------------------------------------------------------------------------------- */
1735 static int
1736 redo_diff (WDiff * dview)
1738 FBUF *const *f = dview->f;
1739 PRINTER_CTX ctx;
1740 GArray *ops;
1741 int ndiff;
1742 int rv;
1743 char extra[256];
1745 extra[0] = '\0';
1746 if (dview->opt.quality == 2)
1747 strcat (extra, " -d");
1748 if (dview->opt.quality == 1)
1749 strcat (extra, " --speed-large-files");
1750 if (dview->opt.strip_trailing_cr)
1751 strcat (extra, " --strip-trailing-cr");
1752 if (dview->opt.ignore_tab_expansion)
1753 strcat (extra, " -E");
1754 if (dview->opt.ignore_space_change)
1755 strcat (extra, " -b");
1756 if (dview->opt.ignore_all_space)
1757 strcat (extra, " -w");
1758 if (dview->opt.ignore_case)
1759 strcat (extra, " -i");
1761 if (dview->dsrc != DATA_SRC_MEM)
1763 f_reset (f[DIFF_LEFT]);
1764 f_reset (f[DIFF_RIGHT]);
1767 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1768 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1769 if (ndiff < 0)
1771 if (ops != NULL)
1772 g_array_free (ops, TRUE);
1773 return -1;
1776 ctx.dsrc = dview->dsrc;
1778 rv = 0;
1779 ctx.a = dview->a[DIFF_LEFT];
1780 ctx.f = f[DIFF_LEFT];
1781 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1783 ctx.a = dview->a[DIFF_RIGHT];
1784 ctx.f = f[DIFF_RIGHT];
1785 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1787 if (ops != NULL)
1788 g_array_free (ops, TRUE);
1790 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1791 return -1;
1793 if (dview->dsrc == DATA_SRC_TMP)
1795 f_trunc (f[DIFF_LEFT]);
1796 f_trunc (f[DIFF_RIGHT]);
1799 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1801 dview->hdiff = g_ptr_array_new ();
1802 if (dview->hdiff != NULL)
1804 size_t i;
1805 const DIFFLN *p;
1806 const DIFFLN *q;
1808 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1810 GArray *h = NULL;
1812 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1813 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1814 if (p->line && q->line && p->ch == CHG_CH)
1816 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1817 if (h != NULL)
1819 gboolean runresult;
1821 runresult =
1822 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1823 HDIFF_DEPTH);
1824 if (!runresult)
1826 g_array_free (h, TRUE);
1827 h = NULL;
1831 g_ptr_array_add (dview->hdiff, h);
1835 return ndiff;
1838 /* --------------------------------------------------------------------------------------------- */
1840 static void
1841 destroy_hdiff (WDiff * dview)
1843 if (dview->hdiff != NULL)
1845 int i;
1846 int len;
1848 len = dview->a[DIFF_LEFT]->len;
1850 for (i = 0; i < len; i++)
1852 GArray *h;
1854 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1855 if (h != NULL)
1856 g_array_free (h, TRUE);
1858 g_ptr_array_free (dview->hdiff, TRUE);
1859 dview->hdiff = NULL;
1862 mc_search_free (dview->search.handle);
1863 dview->search.handle = NULL;
1864 g_free (dview->search.last_string);
1865 dview->search.last_string = NULL;
1868 /* --------------------------------------------------------------------------------------------- */
1869 /* stuff ******************************************************************** */
1871 static int
1872 get_digits (unsigned int n)
1874 int d = 1;
1876 while (n /= 10)
1877 d++;
1878 return d;
1881 /* --------------------------------------------------------------------------------------------- */
1883 static int
1884 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1886 const DIFFLN *p;
1888 *linenum = 0;
1889 *lineofs = 0;
1891 if (a->len != 0)
1893 if (pos >= a->len)
1894 pos = a->len - 1;
1896 p = &g_array_index (a, DIFFLN, pos);
1898 if (p->line == 0)
1900 int n;
1902 for (n = pos; n > 0; n--)
1904 p--;
1905 if (p->line != 0)
1906 break;
1908 *lineofs = pos - n + 1;
1911 *linenum = p->line;
1913 return 0;
1916 /* --------------------------------------------------------------------------------------------- */
1918 static int
1919 calc_nwidth (const GArray ** const a)
1921 int l1, o1;
1922 int l2, o2;
1924 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1925 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1926 if (l1 < l2)
1927 l1 = l2;
1928 return get_digits (l1);
1931 /* --------------------------------------------------------------------------------------------- */
1933 static int
1934 find_prev_hunk (const GArray * a, int pos)
1936 #if 1
1937 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1938 pos--;
1939 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1940 pos--;
1941 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1942 pos--;
1943 if (pos > 0 && (size_t) pos < a->len)
1944 pos++;
1945 #else
1946 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1947 pos--;
1948 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1949 pos--;
1950 #endif
1952 return pos;
1955 /* --------------------------------------------------------------------------------------------- */
1957 static size_t
1958 find_next_hunk (const GArray * a, size_t pos)
1960 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1961 pos++;
1962 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1963 pos++;
1964 return pos;
1967 /* --------------------------------------------------------------------------------------------- */
1969 * Find start and end lines of the current hunk.
1971 * @param dview WDiff widget
1972 * @return boolean and
1973 * start_line1 first line of current hunk (file[0])
1974 * end_line1 last line of current hunk (file[0])
1975 * start_line1 first line of current hunk (file[0])
1976 * end_line1 last line of current hunk (file[0])
1979 static int
1980 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1982 const GArray *a0 = dview->a[DIFF_LEFT];
1983 const GArray *a1 = dview->a[DIFF_RIGHT];
1984 size_t pos;
1985 int ch;
1986 int res = 0;
1988 *start_line1 = 1;
1989 *start_line2 = 1;
1990 *end_line1 = 1;
1991 *end_line2 = 1;
1993 pos = dview->skip_rows;
1994 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1995 if (ch != EQU_CH)
1997 switch (ch)
1999 case ADD_CH:
2000 res = DIFF_DEL;
2001 break;
2002 case DEL_CH:
2003 res = DIFF_ADD;
2004 break;
2005 case CHG_CH:
2006 res = DIFF_CHG;
2007 break;
2009 while (pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2010 pos--;
2011 if (pos > 0)
2013 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
2014 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
2016 pos = dview->skip_rows;
2017 while (pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2019 int l0, l1;
2021 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
2022 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
2023 if (l0 > 0)
2024 *end_line1 = max (*start_line1, l0);
2025 if (l1 > 0)
2026 *end_line2 = max (*start_line2, l1);
2027 pos++;
2030 return res;
2033 /* --------------------------------------------------------------------------------------------- */
2035 * Remove hunk from file.
2037 * @param dview WDiff widget
2038 * @param merge_file file stream for writing data
2039 * @param from1 first line of hunk
2040 * @param to1 last line of hunk
2041 * @param merge_direction in what direction files should be merged
2044 static void
2045 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
2046 action_direction_t merge_direction)
2048 int line;
2049 char buf[BUF_10K];
2050 FILE *f0;
2052 if (merge_direction == FROM_RIGHT_TO_LEFT)
2053 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2054 else
2055 f0 = fopen (dview->file[DIFF_LEFT], "r");
2057 line = 0;
2058 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2060 line++;
2061 fputs (buf, merge_file);
2063 while (fgets (buf, sizeof (buf), f0) != NULL)
2065 line++;
2066 if (line >= to1)
2067 fputs (buf, merge_file);
2069 fclose (f0);
2072 /* --------------------------------------------------------------------------------------------- */
2074 * Add hunk to file.
2076 * @param dview WDiff widget
2077 * @param merge_file file stream for writing data
2078 * @param from1 first line of source hunk
2079 * @param from2 first line of destination hunk
2080 * @param to1 last line of source hunk
2081 * @param merge_direction in what direction files should be merged
2084 static void
2085 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2086 action_direction_t merge_direction)
2088 int line;
2089 char buf[BUF_10K];
2090 FILE *f0;
2091 FILE *f1;
2093 if (merge_direction == FROM_RIGHT_TO_LEFT)
2095 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2096 f1 = fopen (dview->file[DIFF_LEFT], "r");
2098 else
2100 f0 = fopen (dview->file[DIFF_LEFT], "r");
2101 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2104 line = 0;
2105 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2107 line++;
2108 fputs (buf, merge_file);
2110 line = 0;
2111 while (fgets (buf, sizeof (buf), f1) != NULL && line <= to2)
2113 line++;
2114 if (line >= from2)
2115 fputs (buf, merge_file);
2117 while (fgets (buf, sizeof (buf), f0) != NULL)
2118 fputs (buf, merge_file);
2120 fclose (f0);
2121 fclose (f1);
2124 /* --------------------------------------------------------------------------------------------- */
2126 * Replace hunk in file.
2128 * @param dview WDiff widget
2129 * @param merge_file file stream for writing data
2130 * @param from1 first line of source hunk
2131 * @param to1 last line of source hunk
2132 * @param from2 first line of destination hunk
2133 * @param to2 last line of destination hunk
2134 * @param merge_direction in what direction files should be merged
2137 static void
2138 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2139 action_direction_t merge_direction)
2141 int line1 = 0, line2 = 0;
2142 char buf[BUF_10K];
2143 FILE *f0;
2144 FILE *f1;
2146 if (merge_direction == FROM_RIGHT_TO_LEFT)
2148 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2149 f1 = fopen (dview->file[DIFF_LEFT], "r");
2151 else
2153 f0 = fopen (dview->file[DIFF_LEFT], "r");
2154 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2157 while (fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1)
2159 line1++;
2160 fputs (buf, merge_file);
2162 while (fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2)
2164 line2++;
2165 if (line2 >= from2)
2166 fputs (buf, merge_file);
2168 while (fgets (buf, sizeof (buf), f0) != NULL)
2170 line1++;
2171 if (line1 > to1)
2172 fputs (buf, merge_file);
2174 fclose (f0);
2175 fclose (f1);
2178 /* --------------------------------------------------------------------------------------------- */
2180 * Merge hunk.
2182 * @param dview WDiff widget
2183 * @param merge_direction in what direction files should be merged
2186 static void
2187 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2189 int from1, to1, from2, to2;
2190 int res;
2191 int hunk;
2192 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2194 if (merge_direction == FROM_RIGHT_TO_LEFT)
2195 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2196 else
2197 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2199 if (hunk > 0)
2201 int merge_file_fd;
2202 FILE *merge_file;
2203 vfs_path_t *merge_file_name_vpath = NULL;
2205 if (!dview->merged[n_merge])
2207 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2208 if (!dview->merged[n_merge])
2210 message (D_ERROR, MSG_ERROR,
2211 _("Cannot create backup file\n%s%s\n%s"),
2212 dview->file[n_merge], "~~~", unix_error_string (errno));
2213 return;
2217 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2218 if (merge_file_fd == -1)
2220 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2221 unix_error_string (errno));
2222 return;
2225 merge_file = fdopen (merge_file_fd, "w");
2227 switch (hunk)
2229 case DIFF_DEL:
2230 if (merge_direction == FROM_RIGHT_TO_LEFT)
2231 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2232 else
2233 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2234 break;
2235 case DIFF_ADD:
2236 if (merge_direction == FROM_RIGHT_TO_LEFT)
2237 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2238 else
2239 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2240 break;
2241 case DIFF_CHG:
2242 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2243 break;
2245 fflush (merge_file);
2246 fclose (merge_file);
2247 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2248 mc_unlink (merge_file_name_vpath);
2249 vfs_path_free (merge_file_name_vpath);
2253 /* --------------------------------------------------------------------------------------------- */
2254 /* view routines and callbacks ********************************************** */
2256 static void
2257 dview_compute_split (WDiff * dview, int i)
2259 dview->bias += i;
2260 if (dview->bias < 2 - dview->half1)
2261 dview->bias = 2 - dview->half1;
2262 if (dview->bias > dview->half2 - 2)
2263 dview->bias = dview->half2 - 2;
2266 /* --------------------------------------------------------------------------------------------- */
2268 static void
2269 dview_compute_areas (WDiff * dview)
2271 dview->height = LINES - 2;
2272 dview->half1 = COLS / 2;
2273 dview->half2 = COLS - dview->half1;
2275 dview_compute_split (dview, 0);
2278 /* --------------------------------------------------------------------------------------------- */
2280 static void
2281 dview_reread (WDiff * dview)
2283 int ndiff;
2285 destroy_hdiff (dview);
2286 if (dview->a[DIFF_LEFT] != NULL)
2288 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2289 g_array_free (dview->a[DIFF_LEFT], TRUE);
2291 if (dview->a[DIFF_RIGHT] != NULL)
2293 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2294 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2297 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2298 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2300 ndiff = redo_diff (dview);
2301 if (ndiff >= 0)
2302 dview->ndiff = ndiff;
2305 /* --------------------------------------------------------------------------------------------- */
2307 #ifdef HAVE_CHARSET
2308 static void
2309 dview_set_codeset (WDiff * dview)
2311 const char *encoding_id = NULL;
2313 dview->utf8 = TRUE;
2314 encoding_id =
2315 get_codepage_id (mc_global.source_codepage >=
2316 0 ? mc_global.source_codepage : mc_global.display_codepage);
2317 if (encoding_id != NULL)
2319 GIConv conv;
2321 conv = str_crt_conv_from (encoding_id);
2322 if (conv != INVALID_CONV)
2324 if (dview->converter != str_cnv_from_term)
2325 str_close_conv (dview->converter);
2326 dview->converter = conv;
2328 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2332 /* --------------------------------------------------------------------------------------------- */
2334 static void
2335 dview_select_encoding (WDiff * dview)
2337 if (do_select_codepage ())
2338 dview_set_codeset (dview);
2339 dview_reread (dview);
2340 tty_touch_screen ();
2341 repaint_screen ();
2343 #endif /* HAVE_CHARSET */
2345 /* --------------------------------------------------------------------------------------------- */
2347 static void
2348 dview_diff_options (WDiff * dview)
2350 const char *quality_str[] = {
2351 N_("No&rmal"),
2352 N_("&Fastest (Assume large files)"),
2353 N_("&Minimal (Find a smaller set of change)")
2356 QuickWidget diffopt_widgets[] = {
2357 QUICK_BUTTON (6, 10, 14, OPTY, N_("&Cancel"), B_CANCEL, NULL),
2358 QUICK_BUTTON (2, 10, 14, OPTY, N_("&OK"), B_ENTER, NULL),
2360 QUICK_CHECKBOX (3, OPTX, 12, OPTY,
2361 N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr),
2362 QUICK_CHECKBOX (3, OPTX, 11, OPTY,
2363 N_("Ignore all &whitespace"), &dview->opt.ignore_all_space),
2364 QUICK_CHECKBOX (3, OPTX, 10, OPTY,
2365 N_("Ignore &space change"), &dview->opt.ignore_space_change),
2366 QUICK_CHECKBOX (3, OPTX, 9, OPTY,
2367 N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion),
2368 QUICK_CHECKBOX (3, OPTX, 8, OPTY,
2369 N_("&Ignore case"), &dview->opt.ignore_case),
2370 QUICK_LABEL (3, OPTX, 7, OPTY, N_("Diff extra options")),
2371 QUICK_RADIO (3, OPTX, 3, OPTY,
2372 3, (const char **) quality_str, (int *) &dview->opt.quality),
2373 QUICK_LABEL (3, OPTX, 2, OPTY, N_("Diff algorithm")),
2375 QUICK_END
2378 QuickDialog diffopt = {
2379 OPTX, OPTY, -1, -1,
2380 N_("Diff Options"), "[Diff Options]",
2381 diffopt_widgets, NULL, NULL, FALSE
2384 if (quick_dialog (&diffopt) != B_CANCEL)
2385 dview_reread (dview);
2388 /* --------------------------------------------------------------------------------------------- */
2390 static int
2391 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2392 const char *label1, const char *label2, DSRC dsrc)
2394 int ndiff;
2395 FBUF *f[DIFF_COUNT];
2397 f[DIFF_LEFT] = NULL;
2398 f[DIFF_RIGHT] = NULL;
2400 if (dsrc == DATA_SRC_TMP)
2402 f[DIFF_LEFT] = f_temp ();
2403 if (f[DIFF_LEFT] == NULL)
2404 return -1;
2406 f[DIFF_RIGHT] = f_temp ();
2407 if (f[DIFF_RIGHT] == NULL)
2409 f_close (f[DIFF_LEFT]);
2410 return -1;
2413 else if (dsrc == DATA_SRC_ORG)
2415 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2416 if (f[DIFF_LEFT] == NULL)
2417 return -1;
2419 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2420 if (f[DIFF_RIGHT] == NULL)
2422 f_close (f[DIFF_LEFT]);
2423 return -1;
2427 dview->args = args;
2428 dview->file[DIFF_LEFT] = file1;
2429 dview->file[DIFF_RIGHT] = file2;
2430 dview->label[DIFF_LEFT] = g_strdup (label1);
2431 dview->label[DIFF_RIGHT] = g_strdup (label2);
2432 dview->f[DIFF_LEFT] = f[0];
2433 dview->f[DIFF_RIGHT] = f[1];
2434 dview->merged[DIFF_LEFT] = FALSE;
2435 dview->merged[DIFF_RIGHT] = FALSE;
2436 dview->hdiff = NULL;
2437 dview->dsrc = dsrc;
2438 dview->converter = str_cnv_from_term;
2439 #ifdef HAVE_CHARSET
2440 dview_set_codeset (dview);
2441 #endif
2442 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2443 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2445 ndiff = redo_diff (dview);
2446 if (ndiff < 0)
2448 /* goto WIDGET_DESTROY stage: dview_fini() */
2449 f_close (f[DIFF_LEFT]);
2450 f_close (f[DIFF_RIGHT]);
2451 return -1;
2454 dview->ndiff = ndiff;
2456 dview->view_quit = 0;
2458 dview->bias = 0;
2459 dview->new_frame = 1;
2460 dview->skip_rows = 0;
2461 dview->skip_cols = 0;
2462 dview->display_symbols = 0;
2463 dview->display_numbers = 0;
2464 dview->show_cr = 1;
2465 dview->tab_size = 8;
2466 dview->ord = DIFF_LEFT;
2467 dview->full = 0;
2469 dview->search.handle = NULL;
2470 dview->search.last_string = NULL;
2471 dview->search.last_found_line = -1;
2472 dview->search.last_accessed_num_line = -1;
2474 dview->opt.quality = 0;
2475 dview->opt.strip_trailing_cr = 0;
2476 dview->opt.ignore_tab_expansion = 0;
2477 dview->opt.ignore_space_change = 0;
2478 dview->opt.ignore_all_space = 0;
2479 dview->opt.ignore_case = 0;
2481 dview_compute_areas (dview);
2483 return 0;
2486 /* --------------------------------------------------------------------------------------------- */
2488 static void
2489 dview_fini (WDiff * dview)
2491 if (dview->dsrc != DATA_SRC_MEM)
2493 f_close (dview->f[DIFF_RIGHT]);
2494 f_close (dview->f[DIFF_LEFT]);
2497 if (dview->converter != str_cnv_from_term)
2498 str_close_conv (dview->converter);
2500 destroy_hdiff (dview);
2501 if (dview->a[DIFF_LEFT] != NULL)
2503 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2504 g_array_free (dview->a[DIFF_LEFT], TRUE);
2505 dview->a[DIFF_LEFT] = NULL;
2507 if (dview->a[DIFF_RIGHT] != NULL)
2509 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2510 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2511 dview->a[DIFF_RIGHT] = NULL;
2514 g_free (dview->label[DIFF_LEFT]);
2515 g_free (dview->label[DIFF_RIGHT]);
2518 /* --------------------------------------------------------------------------------------------- */
2520 static int
2521 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2523 size_t i, k;
2524 int j;
2525 char buf[BUFSIZ];
2526 FBUF *f = dview->f[ord];
2527 int skip = dview->skip_cols;
2528 int display_symbols = dview->display_symbols;
2529 int display_numbers = dview->display_numbers;
2530 int show_cr = dview->show_cr;
2531 int tab_size = 8;
2532 const DIFFLN *p;
2533 int nwidth = display_numbers;
2534 int xwidth;
2536 xwidth = display_symbols + display_numbers;
2537 if (dview->tab_size > 0 && dview->tab_size < 9)
2538 tab_size = dview->tab_size;
2540 if (xwidth != 0)
2542 if (xwidth > width && display_symbols)
2544 xwidth--;
2545 display_symbols = 0;
2547 if (xwidth > width && display_numbers)
2549 xwidth = width;
2550 display_numbers = width;
2553 xwidth++;
2554 c += xwidth;
2555 width -= xwidth;
2556 if (width < 0)
2557 width = 0;
2560 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2562 /* abnormal, but avoid buffer overflow */
2563 return -1;
2566 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2568 int ch, next_ch, col;
2569 size_t cnt;
2571 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2572 ch = p->ch;
2573 tty_setcolor (NORMAL_COLOR);
2574 if (display_symbols)
2576 tty_gotoyx (r + j, c - 2);
2577 tty_print_char (ch);
2579 if (p->line != 0)
2581 if (display_numbers)
2583 tty_gotoyx (r + j, c - xwidth);
2584 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2585 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2587 if (ch == ADD_CH)
2588 tty_setcolor (DFF_ADD_COLOR);
2589 if (ch == CHG_CH)
2590 tty_setcolor (DFF_CHG_COLOR);
2591 if (f == NULL)
2593 if (i == (size_t) dview->search.last_found_line)
2594 tty_setcolor (MARKED_SELECTED_COLOR);
2595 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2597 char att[BUFSIZ];
2599 if (dview->utf8)
2600 k = dview_str_utf8_offset_to_pos (p->p, width);
2601 else
2602 k = width;
2604 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2605 g_ptr_array_index (dview->hdiff, i), ord, att);
2606 tty_gotoyx (r + j, c);
2607 col = 0;
2609 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2611 int w;
2612 gboolean ch_res;
2614 if (dview->utf8)
2616 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2617 if (w > 1)
2618 cnt += w - 1;
2619 if (!g_unichar_isprint (next_ch))
2620 next_ch = '.';
2622 else
2623 next_ch = dview_get_byte (buf + cnt, &ch_res);
2625 if (ch_res)
2627 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2628 #ifdef HAVE_CHARSET
2629 if (mc_global.utf8_display)
2631 if (!dview->utf8)
2633 next_ch =
2634 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2635 dview->converter);
2638 else if (dview->utf8)
2639 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2640 else
2641 next_ch = convert_to_display_c (next_ch);
2642 #endif
2643 tty_print_anychar (next_ch);
2644 col++;
2647 continue;
2650 if (ch == CHG_CH)
2651 tty_setcolor (DFF_CHH_COLOR);
2653 if (dview->utf8)
2654 k = dview_str_utf8_offset_to_pos (p->p, width);
2655 else
2656 k = width;
2657 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2659 else
2660 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2662 else
2664 if (display_numbers)
2666 tty_gotoyx (r + j, c - xwidth);
2667 memset (buf, ' ', display_numbers);
2668 buf[display_numbers] = '\0';
2669 tty_print_string (buf);
2671 if (ch == DEL_CH)
2672 tty_setcolor (DFF_DEL_COLOR);
2673 if (ch == CHG_CH)
2674 tty_setcolor (DFF_CHD_COLOR);
2675 memset (buf, ' ', width);
2676 buf[width] = '\0';
2678 tty_gotoyx (r + j, c);
2679 /* tty_print_nstring (buf, width); */
2680 col = 0;
2681 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2683 int w;
2684 gboolean ch_res;
2686 if (dview->utf8)
2688 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2689 if (w > 1)
2690 cnt += w - 1;
2691 if (!g_unichar_isprint (next_ch))
2692 next_ch = '.';
2694 else
2695 next_ch = dview_get_byte (buf + cnt, &ch_res);
2696 if (ch_res)
2698 #ifdef HAVE_CHARSET
2699 if (mc_global.utf8_display)
2701 if (!dview->utf8)
2703 next_ch =
2704 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2707 else if (dview->utf8)
2708 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2709 else
2710 next_ch = convert_to_display_c (next_ch);
2711 #endif
2713 tty_print_anychar (next_ch);
2714 col++;
2718 tty_setcolor (NORMAL_COLOR);
2719 k = width;
2720 if (width < xwidth - 1)
2721 k = xwidth - 1;
2722 memset (buf, ' ', k);
2723 buf[k] = '\0';
2724 for (; j < height; j++)
2726 if (xwidth != 0)
2728 tty_gotoyx (r + j, c - xwidth);
2729 /* tty_print_nstring (buf, xwidth - 1); */
2730 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2732 tty_gotoyx (r + j, c);
2733 /* tty_print_nstring (buf, width); */
2734 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2737 return 0;
2740 /* --------------------------------------------------------------------------------------------- */
2742 static void
2743 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2745 const char *buf;
2746 int filename_width;
2747 int linenum, lineofs;
2748 vfs_path_t *vpath;
2749 char *path;
2751 tty_setcolor (STATUSBAR_COLOR);
2753 tty_gotoyx (0, c);
2754 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2756 filename_width = width - 24;
2757 if (filename_width < 8)
2758 filename_width = 8;
2760 vpath = vfs_path_from_str (dview->label[ord]);
2761 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2762 vfs_path_free (vpath);
2763 buf = str_term_trim (path, filename_width);
2764 if (ord == DIFF_LEFT)
2765 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2766 buf, linenum, lineofs, dview->skip_cols);
2767 else
2768 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2769 buf, linenum, lineofs, dview->ndiff);
2770 g_free (path);
2773 /* --------------------------------------------------------------------------------------------- */
2775 static void
2776 dview_redo (WDiff * dview)
2778 if (dview->display_numbers)
2780 int old;
2782 old = dview->display_numbers;
2783 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2784 dview->new_frame = (old != dview->display_numbers);
2786 dview_reread (dview);
2789 /* --------------------------------------------------------------------------------------------- */
2791 static void
2792 dview_update (WDiff * dview)
2794 int height = dview->height;
2795 int width1;
2796 int width2;
2797 int last;
2799 last = dview->a[DIFF_LEFT]->len - 1;
2801 if (dview->skip_rows > last)
2802 dview->skip_rows = dview->search.last_accessed_num_line = last;
2803 if (dview->skip_rows < 0)
2804 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2805 if (dview->skip_cols < 0)
2806 dview->skip_cols = 0;
2808 if (height < 2)
2809 return;
2811 width1 = dview->half1 + dview->bias;
2812 width2 = dview->half2 - dview->bias;
2813 if (dview->full)
2815 width1 = COLS;
2816 width2 = 0;
2819 if (dview->new_frame)
2821 int xwidth;
2823 tty_setcolor (NORMAL_COLOR);
2824 xwidth = dview->display_symbols + dview->display_numbers;
2825 if (width1 > 1)
2826 tty_draw_box (1, 0, height, width1, FALSE);
2827 if (width2 > 1)
2828 tty_draw_box (1, width1, height, width2, FALSE);
2830 if (xwidth != 0)
2832 xwidth++;
2833 if (xwidth < width1 - 1)
2835 tty_gotoyx (1, xwidth);
2836 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2837 tty_gotoyx (height, xwidth);
2838 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2839 tty_draw_vline (2, xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2841 if (xwidth < width2 - 1)
2843 tty_gotoyx (1, width1 + xwidth);
2844 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2845 tty_gotoyx (height, width1 + xwidth);
2846 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2847 tty_draw_vline (2, width1 + xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2850 dview->new_frame = 0;
2853 if (width1 > 2)
2855 dview_status (dview, dview->ord, width1, 0);
2856 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2858 if (width2 > 2)
2860 dview_status (dview, dview->ord ^ 1, width2, width1);
2861 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2865 /* --------------------------------------------------------------------------------------------- */
2867 static void
2868 dview_edit (WDiff * dview, diff_place_t ord)
2870 Dlg_head *h;
2871 gboolean h_modal;
2872 int linenum, lineofs;
2874 if (dview->dsrc == DATA_SRC_TMP)
2876 error_dialog (_("Edit"), _("Edit is disabled"));
2877 return;
2880 h = WIDGET (dview)->owner;
2881 h_modal = h->modal;
2883 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2884 h->modal = TRUE; /* not allow edit file in several editors */
2886 vfs_path_t *tmp_vpath;
2888 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2889 do_edit_at_line (tmp_vpath, use_internal_edit, linenum);
2890 vfs_path_free (tmp_vpath);
2892 h->modal = h_modal;
2893 dview_redo (dview);
2894 dview_update (dview);
2897 /* --------------------------------------------------------------------------------------------- */
2899 static void
2900 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2902 /* *INDENT-OFF* */
2903 static const char *title[2] = {
2904 N_("Goto line (left)"),
2905 N_("Goto line (right)")
2907 /* *INDENT-ON* */
2908 static char prev[256];
2909 /* XXX some statics here, to be remembered between runs */
2911 int newline;
2912 char *input;
2914 input = input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE, prev);
2915 if (input != NULL)
2917 const char *s = input;
2919 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2921 size_t i = 0;
2923 if (newline > 0)
2925 const DIFFLN *p;
2927 for (; i < dview->a[ord]->len; i++)
2929 p = &g_array_index (dview->a[ord], DIFFLN, i);
2930 if (p->line == newline)
2931 break;
2934 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2935 g_snprintf (prev, sizeof (prev), "%d", newline);
2937 g_free (input);
2941 /* --------------------------------------------------------------------------------------------- */
2943 static void
2944 dview_labels (WDiff * dview)
2946 Widget *d;
2947 Dlg_head *h;
2948 WButtonBar *b;
2950 d = WIDGET (dview);
2951 h = d->owner;
2952 b = find_buttonbar (h);
2954 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, d);
2955 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, d);
2956 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, d);
2957 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, d);
2958 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, d);
2959 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, d);
2960 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, d);
2963 /* --------------------------------------------------------------------------------------------- */
2965 static int
2966 dview_event (Gpm_Event * event, void *data)
2968 WDiff *dview = (WDiff *) data;
2970 if (!mouse_global_in_widget (event, data))
2971 return MOU_UNHANDLED;
2973 /* We are not interested in release events */
2974 if ((event->type & (GPM_DOWN | GPM_DRAG)) == 0)
2975 return MOU_NORMAL;
2977 /* Wheel events */
2978 if ((event->buttons & GPM_B_UP) != 0 && (event->type & GPM_DOWN) != 0)
2980 dview->skip_rows -= 2;
2981 dview->search.last_accessed_num_line = dview->skip_rows;
2982 dview_update (dview);
2984 else if ((event->buttons & GPM_B_DOWN) != 0 && (event->type & GPM_DOWN) != 0)
2986 dview->skip_rows += 2;
2987 dview->search.last_accessed_num_line = dview->skip_rows;
2988 dview_update (dview);
2991 return MOU_NORMAL;
2994 /* --------------------------------------------------------------------------------------------- */
2996 static gboolean
2997 dview_save (WDiff * dview)
2999 gboolean res = TRUE;
3001 if (dview->merged[DIFF_LEFT])
3003 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3004 dview->merged[DIFF_LEFT] = !res;
3006 if (dview->merged[DIFF_RIGHT])
3008 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3009 dview->merged[DIFF_RIGHT] = !res;
3011 return res;
3014 /* --------------------------------------------------------------------------------------------- */
3016 static void
3017 dview_do_save (WDiff * dview)
3019 (void) dview_save (dview);
3022 /* --------------------------------------------------------------------------------------------- */
3024 static void
3025 dview_save_options (WDiff * dview)
3027 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
3028 dview->display_symbols != 0 ? TRUE : FALSE);
3029 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
3030 dview->display_numbers != 0 ? TRUE : FALSE);
3031 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
3033 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
3035 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
3036 dview->opt.strip_trailing_cr);
3037 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
3038 dview->opt.ignore_all_space);
3039 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
3040 dview->opt.ignore_space_change);
3041 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
3042 dview->opt.ignore_tab_expansion);
3043 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
3046 /* --------------------------------------------------------------------------------------------- */
3048 static void
3049 dview_load_options (WDiff * dview)
3051 gboolean show_numbers, show_symbols;
3052 int tab_size;
3054 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
3055 if (show_symbols)
3056 dview->display_symbols = 1;
3057 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
3058 if (show_numbers)
3059 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
3060 tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
3061 if (tab_size > 0 && tab_size < 9)
3062 dview->tab_size = tab_size;
3063 else
3064 dview->tab_size = 8;
3066 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
3068 dview->opt.strip_trailing_cr =
3069 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
3070 dview->opt.ignore_all_space =
3071 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
3072 dview->opt.ignore_space_change =
3073 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
3074 dview->opt.ignore_tab_expansion =
3075 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
3076 dview->opt.ignore_case =
3077 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
3079 dview->new_frame = 1;
3082 /* --------------------------------------------------------------------------------------------- */
3085 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3086 * ask user.
3088 static gboolean
3089 dview_ok_to_exit (WDiff * dview)
3091 gboolean res = TRUE;
3092 int act;
3094 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3095 return res;
3097 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3098 _("File(s) was modified. Save with exit?") :
3099 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3100 D_NORMAL, 2, _("&Yes"), _("&No"));
3102 /* Esc is No */
3103 if (mc_global.midnight_shutdown || (act == -1))
3104 act = 1;
3106 switch (act)
3108 case -1: /* Esc */
3109 res = FALSE;
3110 break;
3111 case 0: /* Yes */
3112 (void) dview_save (dview);
3113 res = TRUE;
3114 break;
3115 case 1: /* No */
3116 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3117 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3118 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3119 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3120 /* fall through */
3121 default:
3122 res = TRUE;
3123 break;
3125 return res;
3128 /* --------------------------------------------------------------------------------------------- */
3130 static cb_ret_t
3131 dview_execute_cmd (WDiff * dview, unsigned long command)
3133 cb_ret_t res = MSG_HANDLED;
3135 switch (command)
3137 case CK_ShowSymbols:
3138 dview->display_symbols ^= 1;
3139 dview->new_frame = 1;
3140 break;
3141 case CK_ShowNumbers:
3142 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
3143 dview->new_frame = 1;
3144 break;
3145 case CK_SplitFull:
3146 dview->full ^= 1;
3147 dview->new_frame = 1;
3148 break;
3149 case CK_SplitEqual:
3150 if (!dview->full)
3152 dview->bias = 0;
3153 dview->new_frame = 1;
3155 break;
3156 case CK_SplitMore:
3157 if (!dview->full)
3159 dview_compute_split (dview, 1);
3160 dview->new_frame = 1;
3162 break;
3164 case CK_SplitLess:
3165 if (!dview->full)
3167 dview_compute_split (dview, -1);
3168 dview->new_frame = 1;
3170 break;
3171 case CK_Tab2:
3172 dview->tab_size = 2;
3173 break;
3174 case CK_Tab3:
3175 dview->tab_size = 3;
3176 break;
3177 case CK_Tab4:
3178 dview->tab_size = 4;
3179 break;
3180 case CK_Tab8:
3181 dview->tab_size = 8;
3182 break;
3183 case CK_Swap:
3184 dview->ord ^= 1;
3185 break;
3186 case CK_Redo:
3187 dview_redo (dview);
3188 break;
3189 case CK_HunkNext:
3190 dview->skip_rows = dview->search.last_accessed_num_line =
3191 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3192 break;
3193 case CK_HunkPrev:
3194 dview->skip_rows = dview->search.last_accessed_num_line =
3195 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3196 break;
3197 case CK_Goto:
3198 dview_goto_cmd (dview, TRUE);
3199 break;
3200 case CK_Edit:
3201 dview_edit (dview, dview->ord);
3202 break;
3203 case CK_Merge:
3204 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3205 dview_redo (dview);
3206 break;
3207 case CK_MergeOther:
3208 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3209 dview_redo (dview);
3210 break;
3211 case CK_EditOther:
3212 dview_edit (dview, dview->ord ^ 1);
3213 break;
3214 case CK_Search:
3215 dview_search_cmd (dview);
3216 break;
3217 case CK_SearchContinue:
3218 dview_continue_search_cmd (dview);
3219 break;
3220 case CK_Top:
3221 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3222 break;
3223 case CK_Bottom:
3224 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3225 break;
3226 case CK_Up:
3227 if (dview->skip_rows > 0)
3229 dview->skip_rows--;
3230 dview->search.last_accessed_num_line = dview->skip_rows;
3232 break;
3233 case CK_Down:
3234 dview->skip_rows++;
3235 dview->search.last_accessed_num_line = dview->skip_rows;
3236 break;
3237 case CK_PageDown:
3238 if (dview->height > 2)
3240 dview->skip_rows += dview->height - 2;
3241 dview->search.last_accessed_num_line = dview->skip_rows;
3243 break;
3244 case CK_PageUp:
3245 if (dview->height > 2)
3247 dview->skip_rows -= dview->height - 2;
3248 dview->search.last_accessed_num_line = dview->skip_rows;
3250 break;
3251 case CK_Left:
3252 dview->skip_cols--;
3253 break;
3254 case CK_Right:
3255 dview->skip_cols++;
3256 break;
3257 case CK_LeftQuick:
3258 dview->skip_cols -= 8;
3259 break;
3260 case CK_RightQuick:
3261 dview->skip_cols += 8;
3262 break;
3263 case CK_Home:
3264 dview->skip_cols = 0;
3265 break;
3266 case CK_Shell:
3267 view_other_cmd ();
3268 break;
3269 case CK_Quit:
3270 dview->view_quit = 1;
3271 break;
3272 case CK_Save:
3273 dview_do_save (dview);
3274 break;
3275 case CK_Options:
3276 dview_diff_options (dview);
3277 break;
3278 #ifdef HAVE_CHARSET
3279 case CK_SelectCodepage:
3280 dview_select_encoding (dview);
3281 break;
3282 #endif
3283 case CK_Cancel:
3284 /* don't close diffviewer due to SIGINT */
3285 break;
3286 default:
3287 res = MSG_NOT_HANDLED;
3289 return res;
3292 /* --------------------------------------------------------------------------------------------- */
3294 static cb_ret_t
3295 dview_handle_key (WDiff * dview, int key)
3297 unsigned long command;
3299 #ifdef HAVE_CHARSET
3300 key = convert_from_input_c (key);
3301 #endif
3303 command = keybind_lookup_keymap_command (diff_map, key);
3304 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3305 return MSG_HANDLED;
3307 /* Key not used */
3308 return MSG_NOT_HANDLED;
3311 /* --------------------------------------------------------------------------------------------- */
3313 static cb_ret_t
3314 dview_callback (Widget * w, widget_msg_t msg, int parm)
3316 WDiff *dview = (WDiff *) w;
3317 Dlg_head *h = w->owner;
3318 cb_ret_t i;
3320 switch (msg)
3322 case WIDGET_INIT:
3323 dview_labels (dview);
3324 dview_load_options (dview);
3325 dview_update (dview);
3326 return MSG_HANDLED;
3328 case WIDGET_DRAW:
3329 dview->new_frame = 1;
3330 dview_update (dview);
3331 return MSG_HANDLED;
3333 case WIDGET_KEY:
3334 i = dview_handle_key (dview, parm);
3335 if (dview->view_quit)
3336 dlg_stop (h);
3337 else
3338 dview_update (dview);
3339 return i;
3341 case WIDGET_COMMAND:
3342 i = dview_execute_cmd (dview, parm);
3343 if (dview->view_quit)
3344 dlg_stop (h);
3345 else
3346 dview_update (dview);
3347 return i;
3349 case WIDGET_DESTROY:
3350 dview_save_options (dview);
3351 dview_fini (dview);
3352 return MSG_HANDLED;
3354 default:
3355 return default_proc (msg, parm);
3359 /* --------------------------------------------------------------------------------------------- */
3361 static void
3362 dview_adjust_size (Dlg_head * h)
3364 WDiff *dview;
3365 WButtonBar *bar;
3367 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3368 dview = (WDiff *) find_widget_type (h, dview_callback);
3369 bar = find_buttonbar (h);
3370 widget_set_size (WIDGET (dview), 0, 0, LINES - 1, COLS);
3371 widget_set_size (WIDGET (bar), LINES - 1, 0, 1, COLS);
3373 dview_compute_areas (dview);
3376 /* --------------------------------------------------------------------------------------------- */
3378 static cb_ret_t
3379 dview_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
3381 WDiff *dview = (WDiff *) data;
3383 switch (msg)
3385 case DLG_RESIZE:
3386 dview_adjust_size (h);
3387 return MSG_HANDLED;
3389 case DLG_ACTION:
3390 /* shortcut */
3391 if (sender == NULL)
3392 return dview_execute_cmd (NULL, parm);
3393 /* message from buttonbar */
3394 if (sender == WIDGET (find_buttonbar (h)))
3396 if (data != NULL)
3397 return send_message (WIDGET (data), WIDGET_COMMAND, parm);
3399 dview = (WDiff *) find_widget_type (h, dview_callback);
3400 return dview_execute_cmd (dview, parm);
3402 return MSG_NOT_HANDLED;
3404 case DLG_VALIDATE:
3405 dview = (WDiff *) find_widget_type (h, dview_callback);
3406 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
3407 if (dview_ok_to_exit (dview))
3408 h->state = DLG_CLOSED;
3409 return MSG_HANDLED;
3411 default:
3412 return default_dlg_callback (h, sender, msg, parm, data);
3416 /* --------------------------------------------------------------------------------------------- */
3418 static char *
3419 dview_get_title (const Dlg_head * h, size_t len)
3421 const WDiff *dview;
3422 const char *modified = " (*) ";
3423 const char *notmodified = " ";
3424 size_t len1;
3425 GString *title;
3427 dview = (const WDiff *) find_widget_type (h, dview_callback);
3428 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3430 title = g_string_sized_new (len);
3431 g_string_append (title, _("Diff:"));
3432 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3433 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3434 g_string_append (title, " | ");
3435 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3436 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3438 return g_string_free (title, FALSE);
3441 /* --------------------------------------------------------------------------------------------- */
3443 static int
3444 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3446 int error;
3447 WDiff *dview;
3448 Widget *w;
3449 Dlg_head *dview_dlg;
3451 /* Create dialog and widgets, put them on the dialog */
3452 dview_dlg =
3453 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, dview_dialog_callback, NULL,
3454 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3456 dview = g_new0 (WDiff, 1);
3457 w = WIDGET (dview);
3458 init_widget (w, 0, 0, LINES - 1, COLS, (callback_fn) dview_callback, (mouse_h) dview_event);
3459 widget_want_cursor (w, FALSE);
3461 add_widget (dview_dlg, dview);
3462 add_widget (dview_dlg, buttonbar_new (TRUE));
3464 dview_dlg->get_title = dview_get_title;
3466 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3468 /* Please note that if you add another widget,
3469 * you have to modify dview_adjust_size to
3470 * be aware of it
3472 if (error == 0)
3473 run_dlg (dview_dlg);
3475 if ((error != 0) || (dview_dlg->state == DLG_CLOSED))
3476 destroy_dlg (dview_dlg);
3478 return error == 0 ? 1 : 0;
3481 /*** public functions ****************************************************************************/
3482 /* --------------------------------------------------------------------------------------------- */
3484 #define GET_FILE_AND_STAMP(n) \
3485 do \
3487 use_copy##n = 0; \
3488 real_file##n = file##n; \
3489 if (!vfs_file_is_local (file##n)) \
3491 real_file##n = mc_getlocalcopy (file##n); \
3492 if (real_file##n != NULL) \
3494 use_copy##n = 1; \
3495 if (mc_stat (real_file##n, &st##n) != 0) \
3496 use_copy##n = -1; \
3500 while (0)
3502 #define UNGET_FILE(n) \
3503 do \
3505 if (use_copy##n) \
3507 int changed = 0; \
3508 if (use_copy##n > 0) \
3510 time_t mtime; \
3511 mtime = st##n.st_mtime; \
3512 if (mc_stat (real_file##n, &st##n) == 0) \
3513 changed = (mtime != st##n.st_mtime); \
3515 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3516 vfs_path_free (real_file##n); \
3519 while (0)
3521 gboolean
3522 dview_diff_cmd (const void *f0, const void *f1)
3524 int rv = 0;
3525 vfs_path_t *file0 = NULL;
3526 vfs_path_t *file1 = NULL;
3527 gboolean is_dir0 = FALSE;
3528 gboolean is_dir1 = FALSE;
3530 switch (mc_global.mc_run_mode)
3532 case MC_RUN_FULL:
3534 /* run from panels */
3535 const WPanel *panel0 = (const WPanel *) f0;
3536 const WPanel *panel1 = (const WPanel *) f1;
3538 file0 = vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, NULL);
3539 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3540 if (is_dir0)
3542 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3543 path_trunc (selection (panel0)->fname, 30));
3544 goto ret;
3547 file1 = vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, NULL);
3548 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3549 if (is_dir1)
3551 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3552 path_trunc (selection (panel1)->fname, 30));
3553 goto ret;
3555 break;
3558 case MC_RUN_DIFFVIEWER:
3560 /* run from command line */
3561 const char *p0 = (const char *) f0;
3562 const char *p1 = (const char *) f1;
3563 struct stat st;
3565 file0 = vfs_path_from_str (p0);
3566 if (mc_stat (file0, &st) == 0)
3568 is_dir0 = S_ISDIR (st.st_mode);
3569 if (is_dir0)
3571 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3572 goto ret;
3575 else
3577 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3578 path_trunc (p0, 30), unix_error_string (errno));
3579 goto ret;
3582 file1 = vfs_path_from_str (p1);
3583 if (mc_stat (file1, &st) == 0)
3585 is_dir1 = S_ISDIR (st.st_mode);
3586 if (is_dir1)
3588 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3589 goto ret;
3592 else
3594 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3595 path_trunc (p1, 30), unix_error_string (errno));
3596 goto ret;
3598 break;
3601 default:
3602 /* this should not happaned */
3603 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3604 return FALSE;
3607 if (rv == 0)
3609 rv = -1;
3610 if (file0 != NULL && file1 != NULL)
3612 int use_copy0;
3613 int use_copy1;
3614 struct stat st0;
3615 struct stat st1;
3616 vfs_path_t *real_file0;
3617 vfs_path_t *real_file1;
3619 GET_FILE_AND_STAMP (0);
3620 GET_FILE_AND_STAMP (1);
3621 if (real_file0 != NULL && real_file1 != NULL)
3623 char *real_file0_str, *real_file1_str;
3624 char *file0_str, *file1_str;
3626 real_file0_str = vfs_path_to_str (real_file0);
3627 real_file1_str = vfs_path_to_str (real_file1);
3628 file0_str = vfs_path_to_str (file0);
3629 file1_str = vfs_path_to_str (file1);
3630 rv = diff_view (real_file0_str, real_file1_str, file0_str, file1_str);
3631 g_free (real_file0_str);
3632 g_free (real_file1_str);
3633 g_free (file0_str);
3634 g_free (file1_str);
3636 UNGET_FILE (1);
3637 UNGET_FILE (0);
3641 if (rv == 0)
3642 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3644 ret:
3645 vfs_path_free (file1);
3646 vfs_path_free (file0);
3648 return (rv != 0);
3651 #undef GET_FILE_AND_STAMP
3652 #undef UNGET_FILE
3654 /* --------------------------------------------------------------------------------------------- */