Reorganize WDialog flags.
[midnight-commander.git] / src / diffviewer / ydiff.c
blobe2c055aa2a5b57df9273826ef55c4feb7eb1451a
1 /*
2 File difference viewer
4 Copyright (C) 2007-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Daniel Borca <dborca@yahoo.com>, 2007
9 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
10 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013, 2016
11 Ilia Maslakov <il.smind@gmail.com>, 2010
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <config.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
38 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/color.h"
41 #include "lib/tty/key.h"
42 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
43 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
44 #include "lib/util.h"
45 #include "lib/widget.h"
46 #include "lib/strutil.h"
47 #include "lib/strescape.h" /* strutils_glob_escape() */
48 #ifdef HAVE_CHARSET
49 #include "lib/charsets.h"
50 #endif
51 #include "lib/event.h" /* mc_event_raise() */
53 #include "src/filemanager/cmd.h" /* edit_file_at_line(), view_other_cmd() */
54 #include "src/filemanager/panel.h"
55 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
57 #include "src/keybind-defaults.h"
58 #include "src/setup.h"
59 #include "src/history.h"
60 #ifdef HAVE_CHARSET
61 #include "src/selcodepage.h"
62 #endif
64 #include "ydiff.h"
65 #include "internal.h"
67 /*** global variables ****************************************************************************/
69 /*** file scope macro definitions ****************************************************************/
71 #define g_array_foreach(a, TP, cbf) \
72 do { \
73 size_t g_array_foreach_i;\
75 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
76 { \
77 TP *g_array_foreach_var; \
79 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
80 (*cbf) (g_array_foreach_var); \
81 } \
82 } while (0)
84 #define FILE_READ_BUF 4096
85 #define FILE_FLAG_TEMP (1 << 0)
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 = g_strdup (vfs_path_as_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 str ...
560 * @param ch ...
561 * @return TRUE on success, FALSE otherwise
564 static gboolean
565 dview_get_byte (const char *str, int *ch)
567 if (str == NULL)
568 return FALSE;
570 *ch = (unsigned char) (*str);
571 return TRUE;
574 /* --------------------------------------------------------------------------------------------- */
576 #ifdef HAVE_CHARSET
578 * Get utf multibyte char from string
580 * @param str ...
581 * @param ch ...
582 * @param ch_length ...
583 * @return TRUE on success, FALSE otherwise
586 static gboolean
587 dview_get_utf (const char *str, int *ch, int *ch_length)
589 if (str == NULL)
590 return FALSE;
592 *ch = g_utf8_get_char_validated (str, -1);
594 if (*ch < 0)
596 *ch = (unsigned char) (*str);
597 *ch_length = 1;
599 else
601 char *next_ch;
603 /* Calculate UTF-8 char length */
604 next_ch = g_utf8_next_char (str);
605 *ch_length = next_ch - str;
608 return TRUE;
611 /* --------------------------------------------------------------------------------------------- */
613 static int
614 dview_str_utf8_offset_to_pos (const char *text, size_t length)
616 ptrdiff_t result;
618 if (text == NULL || text[0] == '\0')
619 return length;
621 if (g_utf8_validate (text, -1, NULL))
622 result = g_utf8_offset_to_pointer (text, length) - text;
623 else
625 gunichar uni;
626 char *tmpbuf, *buffer;
628 buffer = tmpbuf = g_strdup (text);
629 while (tmpbuf[0] != '\0')
631 uni = g_utf8_get_char_validated (tmpbuf, -1);
632 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
633 tmpbuf = g_utf8_next_char (tmpbuf);
634 else
636 tmpbuf[0] = '.';
637 tmpbuf++;
640 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
641 g_free (buffer);
643 return MAX (length, (size_t) result);
645 #endif /*HAVE_CHARSET */
647 /* --------------------------------------------------------------------------------------------- */
649 /* diff parse *************************************************************** */
652 * Read decimal number from string.
654 * @param[in,out] str string to parse
655 * @param[out] n extracted number
656 * @return 0 if success, otherwise non-zero
659 static int
660 scan_deci (const char **str, int *n)
662 const char *p = *str;
663 char *q;
665 errno = 0;
666 *n = strtol (p, &q, 10);
667 if (errno != 0 || p == q)
668 return -1;
669 *str = q;
670 return 0;
673 /* --------------------------------------------------------------------------------------------- */
676 * Parse line for diff statement.
678 * @param p string to parse
679 * @param ops list of diff statements
680 * @return 0 if success, otherwise non-zero
683 static int
684 scan_line (const char *p, GArray * ops)
686 DIFFCMD op;
688 int f1, f2;
689 int t1, t2;
690 int cmd;
691 int range;
693 /* handle the following cases:
694 * NUMaNUM[,NUM]
695 * NUM[,NUM]cNUM[,NUM]
696 * NUM[,NUM]dNUM
697 * where NUM is a positive integer
700 if (scan_deci (&p, &f1) != 0 || f1 < 0)
701 return -1;
703 f2 = f1;
704 range = 0;
705 if (*p == ',')
707 p++;
708 if (scan_deci (&p, &f2) != 0 || f2 < f1)
709 return -1;
711 range = 1;
714 cmd = *p++;
715 if (cmd == 'a')
717 if (range != 0)
718 return -1;
720 else if (cmd != 'c' && cmd != 'd')
721 return -1;
723 if (scan_deci (&p, &t1) != 0 || t1 < 0)
724 return -1;
726 t2 = t1;
727 range = 0;
728 if (*p == ',')
730 p++;
731 if (scan_deci (&p, &t2) != 0 || t2 < t1)
732 return -1;
734 range = 1;
737 if (cmd == 'd' && range != 0)
738 return -1;
740 op.a[0][0] = f1;
741 op.a[0][1] = f2;
742 op.cmd = cmd;
743 op.a[1][0] = t1;
744 op.a[1][1] = t2;
745 g_array_append_val (ops, op);
746 return 0;
749 /* --------------------------------------------------------------------------------------------- */
752 * Parse diff output and extract diff statements.
754 * @param f stream to read from
755 * @param ops list of diff statements to fill
756 * @return positive number indicating number of hunks, otherwise negative
759 static int
760 scan_diff (FBUF * f, GArray * ops)
762 int sz;
763 char buf[BUFSIZ];
765 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
767 if (isdigit (buf[0]))
769 if (buf[sz - 1] != '\n')
770 return -1;
772 buf[sz] = '\0';
773 if (scan_line (buf, ops) != 0)
774 return -1;
776 continue;
779 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
783 return ops->len;
786 /* --------------------------------------------------------------------------------------------- */
789 * Invoke diff and extract diff statements.
791 * @param args extra arguments to be passed to diff
792 * @param extra more arguments to be passed to diff
793 * @param file1 first file to compare
794 * @param file2 second file to compare
795 * @param ops list of diff statements to fill
797 * @return positive number indicating number of hunks, otherwise negative
800 static int
801 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
802 GArray * ops)
804 static const char *opt =
805 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
806 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
807 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
808 " --unchanged-group-format=''";
810 int rv;
811 FBUF *f;
812 char *cmd;
813 int code;
814 char *file1_esc, *file2_esc;
816 /* escape potential $ to avoid shell variable substitutions in popen() */
817 file1_esc = strutils_shell_escape (file1);
818 file2_esc = strutils_shell_escape (file2);
819 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
820 g_free (file1_esc);
821 g_free (file2_esc);
823 if (cmd == NULL)
824 return -1;
826 f = p_open (cmd, O_RDONLY);
827 g_free (cmd);
829 if (f == NULL)
830 return -1;
832 rv = scan_diff (f, ops);
833 code = p_close (f);
835 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
836 rv = -1;
838 return rv;
841 /* --------------------------------------------------------------------------------------------- */
844 * Reparse and display file according to diff statements.
846 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
847 * @param filename file name to display
848 * @param ops list of diff statements
849 * @param printer printf-like function to be used for displaying
850 * @param ctx printer context
852 * @return 0 if success, otherwise non-zero
855 static int
856 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
858 size_t i;
859 FBUF *f;
860 size_t sz;
861 char buf[BUFSIZ];
862 int line = 0;
863 off_t off = 0;
864 const DIFFCMD *op;
865 diff_place_t eff;
866 int add_cmd;
867 int del_cmd;
869 f = f_open (filename, O_RDONLY);
870 if (f == NULL)
871 return -1;
873 ord &= 1;
874 eff = ord;
876 add_cmd = 'a';
877 del_cmd = 'd';
878 if (ord != 0)
880 add_cmd = 'd';
881 del_cmd = 'a';
883 #define F1 a[eff][0]
884 #define F2 a[eff][1]
885 #define T1 a[ ord^1 ][0]
886 #define T2 a[ ord^1 ][1]
887 for (i = 0; i < ops->len; i++)
889 int n;
891 op = &g_array_index (ops, DIFFCMD, i);
892 n = op->F1 - (op->cmd != add_cmd);
894 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
896 line++;
897 printer (ctx, EQU_CH, line, off, sz, buf);
898 off += sz;
899 while (buf[sz - 1] != '\n')
901 sz = f_gets (buf, sizeof (buf), f);
902 if (sz == 0)
904 printer (ctx, 0, 0, 0, 1, "\n");
905 break;
907 printer (ctx, 0, 0, 0, sz, buf);
908 off += sz;
912 if (line != n)
913 goto err;
915 if (op->cmd == add_cmd)
917 n = op->T2 - op->T1 + 1;
918 while (n != 0)
920 printer (ctx, DEL_CH, 0, 0, 1, "\n");
921 n--;
925 if (op->cmd == del_cmd)
927 n = op->F2 - op->F1 + 1;
928 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
930 line++;
931 printer (ctx, ADD_CH, line, off, sz, buf);
932 off += sz;
933 while (buf[sz - 1] != '\n')
935 sz = f_gets (buf, sizeof (buf), f);
936 if (sz == 0)
938 printer (ctx, 0, 0, 0, 1, "\n");
939 break;
941 printer (ctx, 0, 0, 0, sz, buf);
942 off += sz;
944 n--;
947 if (n != 0)
948 goto err;
951 if (op->cmd == 'c')
953 n = op->F2 - op->F1 + 1;
954 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
956 line++;
957 printer (ctx, CHG_CH, line, off, sz, buf);
958 off += sz;
959 while (buf[sz - 1] != '\n')
961 sz = f_gets (buf, sizeof (buf), f);
962 if (sz == 0)
964 printer (ctx, 0, 0, 0, 1, "\n");
965 break;
967 printer (ctx, 0, 0, 0, sz, buf);
968 off += sz;
970 n--;
973 if (n != 0)
974 goto err;
976 n = op->T2 - op->T1 - (op->F2 - op->F1);
977 while (n > 0)
979 printer (ctx, CHG_CH, 0, 0, 1, "\n");
980 n--;
984 #undef T2
985 #undef T1
986 #undef F2
987 #undef F1
989 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
991 line++;
992 printer (ctx, EQU_CH, line, off, sz, buf);
993 off += sz;
994 while (buf[sz - 1] != '\n')
996 sz = f_gets (buf, sizeof (buf), f);
997 if (sz == 0)
999 printer (ctx, 0, 0, 0, 1, "\n");
1000 break;
1002 printer (ctx, 0, 0, 0, sz, buf);
1003 off += sz;
1007 f_close (f);
1008 return 0;
1010 err:
1011 f_close (f);
1012 return -1;
1015 /* --------------------------------------------------------------------------------------------- */
1017 /* horizontal diff ********************************************************** */
1020 * Longest common substring.
1022 * @param s first string
1023 * @param m length of first string
1024 * @param t second string
1025 * @param n length of second string
1026 * @param ret list of offsets for longest common substrings inside each string
1027 * @param min minimum length of common substrings
1029 * @return 0 if success, nonzero otherwise
1032 static int
1033 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1035 int i, j;
1036 int *Lprev, *Lcurr;
1037 int z = 0;
1039 if (m < min || n < min)
1041 /* XXX early culling */
1042 return 0;
1045 Lprev = g_try_new0 (int, n + 1);
1046 if (Lprev == NULL)
1047 return -1;
1049 Lcurr = g_try_new0 (int, n + 1);
1050 if (Lcurr == NULL)
1052 g_free (Lprev);
1053 return -1;
1056 for (i = 0; i < m; i++)
1058 int *L;
1060 L = Lprev;
1061 Lprev = Lcurr;
1062 Lcurr = L;
1063 #ifdef USE_MEMSET_IN_LCS
1064 memset (Lcurr, 0, (n + 1) * sizeof (*Lcurr));
1065 #endif
1066 for (j = 0; j < n; j++)
1068 #ifndef USE_MEMSET_IN_LCS
1069 Lcurr[j + 1] = 0;
1070 #endif
1071 if (s[i] == t[j])
1073 int v;
1075 v = Lprev[j] + 1;
1076 Lcurr[j + 1] = v;
1077 if (z < v)
1079 z = v;
1080 g_array_set_size (ret, 0);
1082 if (z == v && z >= min)
1084 int off0, off1;
1085 size_t k;
1087 off0 = i - z + 1;
1088 off1 = j - z + 1;
1090 for (k = 0; k < ret->len; k++)
1092 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1093 if ((*p)[0] == off0 || (*p)[1] >= off1)
1094 break;
1096 if (k == ret->len)
1098 PAIR p2;
1100 p2[0] = off0;
1101 p2[1] = off1;
1102 g_array_append_val (ret, p2);
1109 g_free (Lcurr);
1110 g_free (Lprev);
1111 return z;
1114 /* --------------------------------------------------------------------------------------------- */
1117 * Scan recursively for common substrings and build ranges.
1119 * @param s first string
1120 * @param t second string
1121 * @param bracket current limits for both of the strings
1122 * @param min minimum length of common substrings
1123 * @param hdiff list of horizontal diff ranges to fill
1124 * @param depth recursion depth
1126 * @return 0 if success, nonzero otherwise
1129 static gboolean
1130 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1131 unsigned int depth)
1133 BRACKET p;
1135 if (depth-- != 0)
1137 GArray *ret;
1138 BRACKET b;
1139 int len;
1141 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1142 if (ret == NULL)
1143 return FALSE;
1145 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1146 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1147 if (ret->len != 0)
1149 size_t k = 0;
1150 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1151 const PAIR *data2;
1153 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1154 b[DIFF_LEFT].len = (*data)[0];
1155 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1156 b[DIFF_RIGHT].len = (*data)[1];
1157 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1158 return FALSE;
1160 for (k = 0; k < ret->len - 1; k++)
1162 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1163 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1164 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1165 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1166 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1167 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1168 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1169 return FALSE;
1171 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1172 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1173 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1174 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1175 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1176 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1177 return FALSE;
1179 g_array_free (ret, TRUE);
1180 return TRUE;
1184 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1185 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1186 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1187 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1188 g_array_append_val (hdiff, p);
1190 return TRUE;
1193 /* --------------------------------------------------------------------------------------------- */
1196 * Build list of horizontal diff ranges.
1198 * @param s first string
1199 * @param m length of first string
1200 * @param t second string
1201 * @param n length of second string
1202 * @param min minimum length of common substrings
1203 * @param hdiff list of horizontal diff ranges to fill
1204 * @param depth recursion depth
1206 * @return 0 if success, nonzero otherwise
1209 static gboolean
1210 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1212 int i;
1213 BRACKET b;
1215 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1216 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1218 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1221 b[DIFF_LEFT].off = i;
1222 b[DIFF_LEFT].len = m - i;
1223 b[DIFF_RIGHT].off = i;
1224 b[DIFF_RIGHT].len = n - i;
1226 /* smartscan (multiple horizontal diff) */
1227 return hdiff_multi (s, t, b, min, hdiff, depth);
1230 /* --------------------------------------------------------------------------------------------- */
1232 /* read line **************************************************************** */
1235 * Check if character is inside horizontal diff limits.
1237 * @param k rank of character inside line
1238 * @param hdiff horizontal diff structure
1239 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1241 * @return TRUE if inside hdiff limits, FALSE otherwise
1244 static gboolean
1245 is_inside (int k, GArray * hdiff, diff_place_t ord)
1247 size_t i;
1248 BRACKET *b;
1250 for (i = 0; i < hdiff->len; i++)
1252 int start, end;
1254 b = &g_array_index (hdiff, BRACKET, i);
1255 start = (*b)[ord].off;
1256 end = start + (*b)[ord].len;
1257 if (k >= start && k < end)
1258 return TRUE;
1260 return FALSE;
1263 /* --------------------------------------------------------------------------------------------- */
1266 * Copy 'src' to 'dst' expanding tabs.
1267 * @note The procedure returns when all bytes are consumed from 'src'
1269 * @param dst destination buffer
1270 * @param src source buffer
1271 * @param srcsize size of src buffer
1272 * @param base virtual base of this string, needed to calculate tabs
1273 * @param ts tab size
1275 * @return new virtual base
1278 static int
1279 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1281 int i;
1283 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1285 *dst = *src;
1286 if (*src == '\t')
1288 int j;
1290 j = TAB_SKIP (ts, i + base);
1291 i += j - 1;
1292 while (j-- > 0)
1293 *dst++ = ' ';
1294 dst--;
1297 return i + base;
1300 /* --------------------------------------------------------------------------------------------- */
1303 * Copy 'src' to 'dst' expanding tabs.
1305 * @param dst destination buffer
1306 * @param dstsize size of dst buffer
1307 * @param[in,out] _src source buffer
1308 * @param srcsize size of src buffer
1309 * @param base virtual base of this string, needed to calculate tabs
1310 * @param ts tab size
1312 * @return new virtual base
1314 * @note The procedure returns when all bytes are consumed from 'src'
1315 * or 'dstsize' bytes are written to 'dst'
1316 * @note Upon return, 'src' points to the first unwritten character in source
1319 static int
1320 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1322 int i;
1323 const char *src = *_src;
1325 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1327 *dst = *src;
1328 if (*src == '\t')
1330 int j;
1332 j = TAB_SKIP (ts, i + base);
1333 if (j > dstsize - i)
1334 j = dstsize - i;
1335 i += j - 1;
1336 while (j-- > 0)
1337 *dst++ = ' ';
1338 dst--;
1341 *_src = src;
1342 return i + base;
1345 /* --------------------------------------------------------------------------------------------- */
1348 * Read line from memory, converting tabs to spaces and padding with spaces.
1350 * @param src buffer to read from
1351 * @param srcsize size of src buffer
1352 * @param dst buffer to read to
1353 * @param dstsize size of dst buffer, excluding trailing null
1354 * @param skip number of characters to skip
1355 * @param ts tab size
1356 * @param show_cr show trailing carriage return as ^M
1358 * @return negative on error, otherwise number of bytes except padding
1361 static int
1362 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1363 gboolean show_cr)
1365 int sz = 0;
1367 if (src != NULL)
1369 int i;
1370 char *tmp = dst;
1371 const int base = 0;
1373 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1375 if (*src == '\t')
1377 int j;
1379 j = TAB_SKIP (ts, i + base);
1380 i += j - 1;
1381 while (j-- > 0)
1383 if (skip > 0)
1384 skip--;
1385 else if (dstsize != 0)
1387 dstsize--;
1388 *dst++ = ' ';
1392 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1394 if (skip == 0 && show_cr)
1396 if (dstsize > 1)
1398 dstsize -= 2;
1399 *dst++ = '^';
1400 *dst++ = 'M';
1402 else
1404 dstsize--;
1405 *dst++ = '.';
1408 break;
1410 else if (skip > 0)
1412 #ifdef HAVE_CHARSET
1413 int ch = 0;
1414 int ch_length = 1;
1416 (void) dview_get_utf (src, &ch, &ch_length);
1418 if (ch_length > 1)
1419 skip += ch_length - 1;
1420 #endif
1422 skip--;
1424 else
1426 dstsize--;
1427 *dst++ = *src;
1430 sz = dst - tmp;
1432 while (dstsize != 0)
1434 dstsize--;
1435 *dst++ = ' ';
1437 *dst = '\0';
1438 return sz;
1441 /* --------------------------------------------------------------------------------------------- */
1444 * Read line from memory and build attribute array.
1446 * @param src buffer to read from
1447 * @param srcsize size of src buffer
1448 * @param dst buffer to read to
1449 * @param dstsize size of dst buffer, excluding trailing null
1450 * @param skip number of characters to skip
1451 * @param ts tab size
1452 * @param show_cr show trailing carriage return as ^M
1453 * @param hdiff horizontal diff structure
1454 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1455 * @param att buffer of attributes
1457 * @return negative on error, otherwise number of bytes except padding
1460 static int
1461 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
1462 gboolean show_cr, GArray * hdiff, diff_place_t ord, char *att)
1464 int sz = 0;
1466 if (src != NULL)
1468 int i, k;
1469 char *tmp = dst;
1470 const int base = 0;
1472 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1474 if (*src == '\t')
1476 int j;
1478 j = TAB_SKIP (ts, i + base);
1479 i += j - 1;
1480 while (j-- > 0)
1482 if (skip != 0)
1483 skip--;
1484 else if (dstsize != 0)
1486 dstsize--;
1487 *att++ = is_inside (k, hdiff, ord);
1488 *dst++ = ' ';
1492 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1494 if (skip == 0 && show_cr)
1496 if (dstsize > 1)
1498 dstsize -= 2;
1499 *att++ = is_inside (k, hdiff, ord);
1500 *dst++ = '^';
1501 *att++ = is_inside (k, hdiff, ord);
1502 *dst++ = 'M';
1504 else
1506 dstsize--;
1507 *att++ = is_inside (k, hdiff, ord);
1508 *dst++ = '.';
1511 break;
1513 else if (skip != 0)
1515 #ifdef HAVE_CHARSET
1516 int ch = 0;
1517 int ch_length = 1;
1519 (void) dview_get_utf (src, &ch, &ch_length);
1520 if (ch_length > 1)
1521 skip += ch_length - 1;
1522 #endif
1524 skip--;
1526 else
1528 dstsize--;
1529 *att++ = is_inside (k, hdiff, ord);
1530 *dst++ = *src;
1533 sz = dst - tmp;
1535 while (dstsize != 0)
1537 dstsize--;
1538 *att++ = '\0';
1539 *dst++ = ' ';
1541 *dst = '\0';
1542 return sz;
1545 /* --------------------------------------------------------------------------------------------- */
1548 * Read line from file, converting tabs to spaces and padding with spaces.
1550 * @param f file stream to read from
1551 * @param off offset of line inside file
1552 * @param dst buffer to read to
1553 * @param dstsize size of dst buffer, excluding trailing null
1554 * @param skip number of characters to skip
1555 * @param ts tab size
1556 * @param show_cr show trailing carriage return as ^M
1558 * @return negative on error, otherwise number of bytes except padding
1561 static int
1562 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, gboolean show_cr)
1564 int base = 0;
1565 int old_base = base;
1566 size_t amount = dstsize;
1567 size_t useful, offset;
1568 size_t i;
1569 size_t sz;
1570 int lastch = '\0';
1571 const char *q = NULL;
1572 char tmp[BUFSIZ]; /* XXX capacity must be >= MAX{dstsize + 1, amount} */
1573 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1575 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1577 /* abnormal, but avoid buffer overflow */
1578 memset (dst, ' ', dstsize);
1579 dst[dstsize] = '\0';
1580 return 0;
1583 f_seek (f, off, SEEK_SET);
1585 while (skip > base)
1587 old_base = base;
1588 sz = f_gets (tmp, amount, f);
1589 if (sz == 0)
1590 break;
1592 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1593 if (cvt[base - old_base - 1] == '\n')
1595 q = &cvt[base - old_base - 1];
1596 base = old_base + q - cvt + 1;
1597 break;
1601 if (base < skip)
1603 memset (dst, ' ', dstsize);
1604 dst[dstsize] = '\0';
1605 return 0;
1608 useful = base - skip;
1609 offset = skip - old_base;
1611 if (useful <= dstsize)
1613 if (useful != 0)
1614 memmove (dst, cvt + offset, useful);
1616 if (q == NULL)
1618 sz = f_gets (tmp, dstsize - useful + 1, f);
1619 if (sz != 0)
1621 const char *ptr = tmp;
1623 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1624 if (ptr < tmp + sz)
1625 lastch = *ptr;
1628 sz = useful;
1630 else
1632 memmove (dst, cvt + offset, dstsize);
1633 sz = dstsize;
1634 lastch = cvt[offset + dstsize];
1637 dst[sz] = lastch;
1638 for (i = 0; i < sz && dst[i] != '\n'; i++)
1640 if (dst[i] == '\r' && dst[i + 1] == '\n')
1642 if (show_cr)
1644 if (i + 1 < dstsize)
1646 dst[i++] = '^';
1647 dst[i++] = 'M';
1649 else
1651 dst[i++] = '*';
1654 break;
1658 for (; i < dstsize; i++)
1659 dst[i] = ' ';
1660 dst[i] = '\0';
1661 return sz;
1664 /* --------------------------------------------------------------------------------------------- */
1665 /* diff printers et al ****************************************************** */
1667 static void
1668 cc_free_elt (void *elt)
1670 DIFFLN *p = elt;
1672 if (p != NULL)
1673 g_free (p->p);
1676 /* --------------------------------------------------------------------------------------------- */
1678 static int
1679 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1681 GArray *a = ((PRINTER_CTX *) ctx)->a;
1682 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1684 if (ch != 0)
1686 DIFFLN p;
1688 p.p = NULL;
1689 p.ch = ch;
1690 p.line = line;
1691 p.u.off = off;
1692 if (dsrc == DATA_SRC_MEM && line != 0)
1694 if (sz != 0 && str[sz - 1] == '\n')
1695 sz--;
1696 if (sz > 0)
1697 p.p = g_strndup (str, sz);
1698 p.u.len = sz;
1700 g_array_append_val (a, p);
1702 else if (dsrc == DATA_SRC_MEM)
1704 DIFFLN *p;
1706 p = &g_array_index (a, DIFFLN, a->len - 1);
1707 if (sz != 0 && str[sz - 1] == '\n')
1708 sz--;
1709 if (sz != 0)
1711 size_t new_size;
1712 char *q;
1714 new_size = p->u.len + sz;
1715 q = g_realloc (p->p, new_size);
1716 memcpy (q + p->u.len, str, sz);
1717 p->p = q;
1719 p->u.len += sz;
1721 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1723 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1724 f_write (f, str, sz);
1726 return 0;
1729 /* --------------------------------------------------------------------------------------------- */
1731 static int
1732 redo_diff (WDiff * dview)
1734 FBUF *const *f = dview->f;
1735 PRINTER_CTX ctx;
1736 GArray *ops;
1737 int ndiff;
1738 int rv;
1739 char extra[256];
1741 extra[0] = '\0';
1742 if (dview->opt.quality == 2)
1743 strcat (extra, " -d");
1744 if (dview->opt.quality == 1)
1745 strcat (extra, " --speed-large-files");
1746 if (dview->opt.strip_trailing_cr)
1747 strcat (extra, " --strip-trailing-cr");
1748 if (dview->opt.ignore_tab_expansion)
1749 strcat (extra, " -E");
1750 if (dview->opt.ignore_space_change)
1751 strcat (extra, " -b");
1752 if (dview->opt.ignore_all_space)
1753 strcat (extra, " -w");
1754 if (dview->opt.ignore_case)
1755 strcat (extra, " -i");
1757 if (dview->dsrc != DATA_SRC_MEM)
1759 f_reset (f[DIFF_LEFT]);
1760 f_reset (f[DIFF_RIGHT]);
1763 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1764 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1765 if (ndiff < 0)
1767 if (ops != NULL)
1768 g_array_free (ops, TRUE);
1769 return -1;
1772 ctx.dsrc = dview->dsrc;
1774 rv = 0;
1775 ctx.a = dview->a[DIFF_LEFT];
1776 ctx.f = f[DIFF_LEFT];
1777 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1779 ctx.a = dview->a[DIFF_RIGHT];
1780 ctx.f = f[DIFF_RIGHT];
1781 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1783 if (ops != NULL)
1784 g_array_free (ops, TRUE);
1786 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1787 return -1;
1789 if (dview->dsrc == DATA_SRC_TMP)
1791 f_trunc (f[DIFF_LEFT]);
1792 f_trunc (f[DIFF_RIGHT]);
1795 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1797 dview->hdiff = g_ptr_array_new ();
1798 if (dview->hdiff != NULL)
1800 size_t i;
1802 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1804 GArray *h = NULL;
1805 const DIFFLN *p;
1806 const DIFFLN *q;
1808 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1809 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1810 if (p->line && q->line && p->ch == CHG_CH)
1812 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1813 if (h != NULL)
1815 gboolean runresult;
1817 runresult =
1818 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1819 HDIFF_DEPTH);
1820 if (!runresult)
1822 g_array_free (h, TRUE);
1823 h = NULL;
1827 g_ptr_array_add (dview->hdiff, h);
1831 return ndiff;
1834 /* --------------------------------------------------------------------------------------------- */
1836 static void
1837 destroy_hdiff (WDiff * dview)
1839 if (dview->hdiff != NULL)
1841 int i;
1842 int len;
1844 len = dview->a[DIFF_LEFT]->len;
1846 for (i = 0; i < len; i++)
1848 GArray *h;
1850 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1851 if (h != NULL)
1852 g_array_free (h, TRUE);
1854 g_ptr_array_free (dview->hdiff, TRUE);
1855 dview->hdiff = NULL;
1858 mc_search_free (dview->search.handle);
1859 dview->search.handle = NULL;
1860 MC_PTR_FREE (dview->search.last_string);
1863 /* --------------------------------------------------------------------------------------------- */
1864 /* stuff ******************************************************************** */
1866 static int
1867 get_digits (unsigned int n)
1869 int d = 1;
1871 while (n /= 10)
1872 d++;
1873 return d;
1876 /* --------------------------------------------------------------------------------------------- */
1878 static int
1879 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1881 const DIFFLN *p;
1883 *linenum = 0;
1884 *lineofs = 0;
1886 if (a->len != 0)
1888 if (pos >= a->len)
1889 pos = a->len - 1;
1891 p = &g_array_index (a, DIFFLN, pos);
1893 if (p->line == 0)
1895 int n;
1897 for (n = pos; n > 0; n--)
1899 p--;
1900 if (p->line != 0)
1901 break;
1903 *lineofs = pos - n + 1;
1906 *linenum = p->line;
1908 return 0;
1911 /* --------------------------------------------------------------------------------------------- */
1913 static int
1914 calc_nwidth (const GArray * const *a)
1916 int l1, o1;
1917 int l2, o2;
1919 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1920 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1921 if (l1 < l2)
1922 l1 = l2;
1923 return get_digits (l1);
1926 /* --------------------------------------------------------------------------------------------- */
1928 static int
1929 find_prev_hunk (const GArray * a, int pos)
1931 #if 1
1932 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1933 pos--;
1934 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1935 pos--;
1936 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1937 pos--;
1938 if (pos > 0 && (size_t) pos < a->len)
1939 pos++;
1940 #else
1941 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1942 pos--;
1943 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1944 pos--;
1945 #endif
1947 return pos;
1950 /* --------------------------------------------------------------------------------------------- */
1952 static size_t
1953 find_next_hunk (const GArray * a, size_t pos)
1955 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1956 pos++;
1957 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1958 pos++;
1959 return pos;
1962 /* --------------------------------------------------------------------------------------------- */
1964 * Find start and end lines of the current hunk.
1966 * @param dview WDiff widget
1967 * @return boolean and
1968 * start_line1 first line of current hunk (file[0])
1969 * end_line1 last line of current hunk (file[0])
1970 * start_line1 first line of current hunk (file[0])
1971 * end_line1 last line of current hunk (file[0])
1974 static int
1975 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1977 const GArray *a0 = dview->a[DIFF_LEFT];
1978 const GArray *a1 = dview->a[DIFF_RIGHT];
1979 size_t pos;
1980 int ch;
1981 int res = 0;
1983 *start_line1 = 1;
1984 *start_line2 = 1;
1985 *end_line1 = 1;
1986 *end_line2 = 1;
1988 pos = dview->skip_rows;
1989 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1990 if (ch != EQU_CH)
1992 switch (ch)
1994 case ADD_CH:
1995 res = DIFF_DEL;
1996 break;
1997 case DEL_CH:
1998 res = DIFF_ADD;
1999 break;
2000 case CHG_CH:
2001 res = DIFF_CHG;
2002 break;
2003 default:
2004 break;
2006 while (pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2007 pos--;
2008 if (pos > 0)
2010 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
2011 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
2013 pos = dview->skip_rows;
2014 while (pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2016 int l0, l1;
2018 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
2019 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
2020 if (l0 > 0)
2021 *end_line1 = MAX (*start_line1, l0);
2022 if (l1 > 0)
2023 *end_line2 = MAX (*start_line2, l1);
2024 pos++;
2027 return res;
2030 /* --------------------------------------------------------------------------------------------- */
2032 * Remove hunk from file.
2034 * @param dview WDiff widget
2035 * @param merge_file file stream for writing data
2036 * @param from1 first line of hunk
2037 * @param to1 last line of hunk
2038 * @param merge_direction in what direction files should be merged
2041 static void
2042 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
2043 action_direction_t merge_direction)
2045 int line;
2046 char buf[BUF_10K];
2047 FILE *f0;
2049 if (merge_direction == FROM_RIGHT_TO_LEFT)
2050 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2051 else
2052 f0 = fopen (dview->file[DIFF_LEFT], "r");
2054 line = 0;
2055 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2057 line++;
2058 fputs (buf, merge_file);
2060 while (fgets (buf, sizeof (buf), f0) != NULL)
2062 line++;
2063 if (line >= to1)
2064 fputs (buf, merge_file);
2066 fclose (f0);
2069 /* --------------------------------------------------------------------------------------------- */
2071 * Add hunk to file.
2073 * @param dview WDiff widget
2074 * @param merge_file file stream for writing data
2075 * @param from1 first line of source hunk
2076 * @param from2 first line of destination hunk
2077 * @param to1 last line of source hunk
2078 * @param merge_direction in what direction files should be merged
2081 static void
2082 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2083 action_direction_t merge_direction)
2085 int line;
2086 char buf[BUF_10K];
2087 FILE *f0;
2088 FILE *f1;
2090 if (merge_direction == FROM_RIGHT_TO_LEFT)
2092 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2093 f1 = fopen (dview->file[DIFF_LEFT], "r");
2095 else
2097 f0 = fopen (dview->file[DIFF_LEFT], "r");
2098 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2101 line = 0;
2102 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2104 line++;
2105 fputs (buf, merge_file);
2107 line = 0;
2108 while (fgets (buf, sizeof (buf), f1) != NULL && line <= to2)
2110 line++;
2111 if (line >= from2)
2112 fputs (buf, merge_file);
2114 while (fgets (buf, sizeof (buf), f0) != NULL)
2115 fputs (buf, merge_file);
2117 fclose (f0);
2118 fclose (f1);
2121 /* --------------------------------------------------------------------------------------------- */
2123 * Replace hunk in file.
2125 * @param dview WDiff widget
2126 * @param merge_file file stream for writing data
2127 * @param from1 first line of source hunk
2128 * @param to1 last line of source hunk
2129 * @param from2 first line of destination hunk
2130 * @param to2 last line of destination hunk
2131 * @param merge_direction in what direction files should be merged
2134 static void
2135 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2136 action_direction_t merge_direction)
2138 int line1 = 0, line2 = 0;
2139 char buf[BUF_10K];
2140 FILE *f0;
2141 FILE *f1;
2143 if (merge_direction == FROM_RIGHT_TO_LEFT)
2145 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2146 f1 = fopen (dview->file[DIFF_LEFT], "r");
2148 else
2150 f0 = fopen (dview->file[DIFF_LEFT], "r");
2151 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2154 while (fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1)
2156 line1++;
2157 fputs (buf, merge_file);
2159 while (fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2)
2161 line2++;
2162 if (line2 >= from2)
2163 fputs (buf, merge_file);
2165 while (fgets (buf, sizeof (buf), f0) != NULL)
2167 line1++;
2168 if (line1 > to1)
2169 fputs (buf, merge_file);
2171 fclose (f0);
2172 fclose (f1);
2175 /* --------------------------------------------------------------------------------------------- */
2177 * Merge hunk.
2179 * @param dview WDiff widget
2180 * @param merge_direction in what direction files should be merged
2183 static void
2184 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2186 int from1, to1, from2, to2;
2187 int hunk;
2188 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2190 if (merge_direction == FROM_RIGHT_TO_LEFT)
2191 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2192 else
2193 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2195 if (hunk > 0)
2197 int merge_file_fd;
2198 FILE *merge_file;
2199 vfs_path_t *merge_file_name_vpath = NULL;
2201 if (!dview->merged[n_merge])
2203 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2204 if (!dview->merged[n_merge])
2206 message (D_ERROR, MSG_ERROR,
2207 _("Cannot create backup file\n%s%s\n%s"),
2208 dview->file[n_merge], "~~~", unix_error_string (errno));
2209 return;
2213 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2214 if (merge_file_fd == -1)
2216 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2217 unix_error_string (errno));
2218 return;
2221 merge_file = fdopen (merge_file_fd, "w");
2223 switch (hunk)
2225 case DIFF_DEL:
2226 if (merge_direction == FROM_RIGHT_TO_LEFT)
2227 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2228 else
2229 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2230 break;
2231 case DIFF_ADD:
2232 if (merge_direction == FROM_RIGHT_TO_LEFT)
2233 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2234 else
2235 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2236 break;
2237 case DIFF_CHG:
2238 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2239 break;
2240 default:
2241 break;
2243 fflush (merge_file);
2244 fclose (merge_file);
2246 int res;
2248 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2249 (void) res;
2251 mc_unlink (merge_file_name_vpath);
2252 vfs_path_free (merge_file_name_vpath);
2256 /* --------------------------------------------------------------------------------------------- */
2257 /* view routines and callbacks ********************************************** */
2259 static void
2260 dview_compute_split (WDiff * dview, int i)
2262 dview->bias += i;
2263 if (dview->bias < 2 - dview->half1)
2264 dview->bias = 2 - dview->half1;
2265 if (dview->bias > dview->half2 - 2)
2266 dview->bias = dview->half2 - 2;
2269 /* --------------------------------------------------------------------------------------------- */
2271 static void
2272 dview_compute_areas (WDiff * dview)
2274 dview->height = LINES - 2;
2275 dview->half1 = COLS / 2;
2276 dview->half2 = COLS - dview->half1;
2278 dview_compute_split (dview, 0);
2281 /* --------------------------------------------------------------------------------------------- */
2283 static void
2284 dview_reread (WDiff * dview)
2286 int ndiff;
2288 destroy_hdiff (dview);
2289 if (dview->a[DIFF_LEFT] != NULL)
2291 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2292 g_array_free (dview->a[DIFF_LEFT], TRUE);
2294 if (dview->a[DIFF_RIGHT] != NULL)
2296 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2297 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2300 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2301 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2303 ndiff = redo_diff (dview);
2304 if (ndiff >= 0)
2305 dview->ndiff = ndiff;
2308 /* --------------------------------------------------------------------------------------------- */
2310 #ifdef HAVE_CHARSET
2311 static void
2312 dview_set_codeset (WDiff * dview)
2314 const char *encoding_id = NULL;
2316 dview->utf8 = TRUE;
2317 encoding_id =
2318 get_codepage_id (mc_global.source_codepage >=
2319 0 ? mc_global.source_codepage : mc_global.display_codepage);
2320 if (encoding_id != NULL)
2322 GIConv conv;
2324 conv = str_crt_conv_from (encoding_id);
2325 if (conv != INVALID_CONV)
2327 if (dview->converter != str_cnv_from_term)
2328 str_close_conv (dview->converter);
2329 dview->converter = conv;
2331 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2335 /* --------------------------------------------------------------------------------------------- */
2337 static void
2338 dview_select_encoding (WDiff * dview)
2340 if (do_select_codepage ())
2341 dview_set_codeset (dview);
2342 dview_reread (dview);
2343 tty_touch_screen ();
2344 repaint_screen ();
2346 #endif /* HAVE_CHARSET */
2348 /* --------------------------------------------------------------------------------------------- */
2350 static void
2351 dview_diff_options (WDiff * dview)
2353 const char *quality_str[] = {
2354 N_("No&rmal"),
2355 N_("&Fastest (Assume large files)"),
2356 N_("&Minimal (Find a smaller set of change)")
2359 quick_widget_t quick_widgets[] = {
2360 /* *INDENT-OFF* */
2361 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2362 QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2363 QUICK_STOP_GROUPBOX,
2364 QUICK_START_GROUPBOX (N_("Diff extra options")),
2365 QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2366 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2367 QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2368 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2369 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2370 NULL),
2371 QUICK_STOP_GROUPBOX,
2372 QUICK_BUTTONS_OK_CANCEL,
2373 QUICK_END
2374 /* *INDENT-ON* */
2377 quick_dialog_t qdlg = {
2378 -1, -1, 56,
2379 N_("Diff Options"), "[Diff Options]",
2380 quick_widgets, NULL, NULL
2383 if (quick_dialog (&qdlg) != B_CANCEL)
2384 dview_reread (dview);
2387 /* --------------------------------------------------------------------------------------------- */
2389 static int
2390 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2391 const char *label1, const char *label2, DSRC dsrc)
2393 int ndiff;
2394 FBUF *f[DIFF_COUNT];
2396 f[DIFF_LEFT] = NULL;
2397 f[DIFF_RIGHT] = NULL;
2399 if (dsrc == DATA_SRC_TMP)
2401 f[DIFF_LEFT] = f_temp ();
2402 if (f[DIFF_LEFT] == NULL)
2403 return -1;
2405 f[DIFF_RIGHT] = f_temp ();
2406 if (f[DIFF_RIGHT] == NULL)
2408 f_close (f[DIFF_LEFT]);
2409 return -1;
2412 else if (dsrc == DATA_SRC_ORG)
2414 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2415 if (f[DIFF_LEFT] == NULL)
2416 return -1;
2418 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2419 if (f[DIFF_RIGHT] == NULL)
2421 f_close (f[DIFF_LEFT]);
2422 return -1;
2426 dview->args = args;
2427 dview->file[DIFF_LEFT] = file1;
2428 dview->file[DIFF_RIGHT] = file2;
2429 dview->label[DIFF_LEFT] = g_strdup (label1);
2430 dview->label[DIFF_RIGHT] = g_strdup (label2);
2431 dview->f[DIFF_LEFT] = f[0];
2432 dview->f[DIFF_RIGHT] = f[1];
2433 dview->merged[DIFF_LEFT] = FALSE;
2434 dview->merged[DIFF_RIGHT] = FALSE;
2435 dview->hdiff = NULL;
2436 dview->dsrc = dsrc;
2437 #ifdef HAVE_CHARSET
2438 dview->converter = str_cnv_from_term;
2439 dview_set_codeset (dview);
2440 #endif
2441 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2442 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2444 ndiff = redo_diff (dview);
2445 if (ndiff < 0)
2447 /* goto MSG_DESTROY stage: dview_fini() */
2448 f_close (f[DIFF_LEFT]);
2449 f_close (f[DIFF_RIGHT]);
2450 return -1;
2453 dview->ndiff = ndiff;
2455 dview->view_quit = FALSE;
2457 dview->bias = 0;
2458 dview->new_frame = TRUE;
2459 dview->skip_rows = 0;
2460 dview->skip_cols = 0;
2461 dview->display_symbols = 0;
2462 dview->display_numbers = 0;
2463 dview->show_cr = TRUE;
2464 dview->tab_size = 8;
2465 dview->ord = DIFF_LEFT;
2466 dview->full = FALSE;
2468 dview->search.handle = NULL;
2469 dview->search.last_string = NULL;
2470 dview->search.last_found_line = -1;
2471 dview->search.last_accessed_num_line = -1;
2473 dview->opt.quality = 0;
2474 dview->opt.strip_trailing_cr = 0;
2475 dview->opt.ignore_tab_expansion = 0;
2476 dview->opt.ignore_space_change = 0;
2477 dview->opt.ignore_all_space = 0;
2478 dview->opt.ignore_case = 0;
2480 dview_compute_areas (dview);
2482 return 0;
2485 /* --------------------------------------------------------------------------------------------- */
2487 static void
2488 dview_fini (WDiff * dview)
2490 if (dview->dsrc != DATA_SRC_MEM)
2492 f_close (dview->f[DIFF_RIGHT]);
2493 f_close (dview->f[DIFF_LEFT]);
2496 #ifdef HAVE_CHARSET
2497 if (dview->converter != str_cnv_from_term)
2498 str_close_conv (dview->converter);
2499 #endif
2501 destroy_hdiff (dview);
2502 if (dview->a[DIFF_LEFT] != NULL)
2504 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2505 g_array_free (dview->a[DIFF_LEFT], TRUE);
2506 dview->a[DIFF_LEFT] = NULL;
2508 if (dview->a[DIFF_RIGHT] != NULL)
2510 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2511 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2512 dview->a[DIFF_RIGHT] = NULL;
2515 g_free (dview->label[DIFF_LEFT]);
2516 g_free (dview->label[DIFF_RIGHT]);
2519 /* --------------------------------------------------------------------------------------------- */
2521 static int
2522 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2524 size_t i, k;
2525 int j;
2526 char buf[BUFSIZ];
2527 FBUF *f = dview->f[ord];
2528 int skip = dview->skip_cols;
2529 int display_symbols = dview->display_symbols;
2530 int display_numbers = dview->display_numbers;
2531 gboolean show_cr = dview->show_cr;
2532 int tab_size = 8;
2533 const DIFFLN *p;
2534 int nwidth = display_numbers;
2535 int xwidth;
2537 xwidth = display_symbols + display_numbers;
2538 if (dview->tab_size > 0 && dview->tab_size < 9)
2539 tab_size = dview->tab_size;
2541 if (xwidth != 0)
2543 if (xwidth > width && display_symbols)
2545 xwidth--;
2546 display_symbols = 0;
2548 if (xwidth > width && display_numbers)
2550 xwidth = width;
2551 display_numbers = width;
2554 xwidth++;
2555 c += xwidth;
2556 width -= xwidth;
2557 if (width < 0)
2558 width = 0;
2561 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2563 /* abnormal, but avoid buffer overflow */
2564 return -1;
2567 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2569 int ch, next_ch = 0, col;
2570 size_t cnt;
2572 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2573 ch = p->ch;
2574 tty_setcolor (NORMAL_COLOR);
2575 if (display_symbols)
2577 tty_gotoyx (r + j, c - 2);
2578 tty_print_char (ch);
2580 if (p->line != 0)
2582 if (display_numbers)
2584 tty_gotoyx (r + j, c - xwidth);
2585 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2586 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2588 if (ch == ADD_CH)
2589 tty_setcolor (DFF_ADD_COLOR);
2590 if (ch == CHG_CH)
2591 tty_setcolor (DFF_CHG_COLOR);
2592 if (f == NULL)
2594 if (i == (size_t) dview->search.last_found_line)
2595 tty_setcolor (MARKED_SELECTED_COLOR);
2596 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2598 char att[BUFSIZ];
2600 #ifdef HAVE_CHARSET
2601 if (dview->utf8)
2602 k = dview_str_utf8_offset_to_pos (p->p, width);
2603 else
2604 #endif
2605 k = width;
2607 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2608 g_ptr_array_index (dview->hdiff, i), ord, att);
2609 tty_gotoyx (r + j, c);
2610 col = 0;
2612 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2614 gboolean ch_res;
2616 #ifdef HAVE_CHARSET
2617 if (dview->utf8)
2619 int ch_length = 0;
2621 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2622 if (ch_length > 1)
2623 cnt += ch_length - 1;
2624 if (!g_unichar_isprint (next_ch))
2625 next_ch = '.';
2627 else
2628 #endif
2629 ch_res = dview_get_byte (buf + cnt, &next_ch);
2631 if (ch_res)
2633 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2634 #ifdef HAVE_CHARSET
2635 if (mc_global.utf8_display)
2637 if (!dview->utf8)
2639 next_ch =
2640 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2641 dview->converter);
2644 else if (dview->utf8)
2645 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2646 else
2647 next_ch = convert_to_display_c (next_ch);
2648 #endif
2649 tty_print_anychar (next_ch);
2650 col++;
2653 continue;
2656 if (ch == CHG_CH)
2657 tty_setcolor (DFF_CHH_COLOR);
2659 #ifdef HAVE_CHARSET
2660 if (dview->utf8)
2661 k = dview_str_utf8_offset_to_pos (p->p, width);
2662 else
2663 #endif
2664 k = width;
2665 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2667 else
2668 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2670 else
2672 if (display_numbers)
2674 tty_gotoyx (r + j, c - xwidth);
2675 memset (buf, ' ', display_numbers);
2676 buf[display_numbers] = '\0';
2677 tty_print_string (buf);
2679 if (ch == DEL_CH)
2680 tty_setcolor (DFF_DEL_COLOR);
2681 if (ch == CHG_CH)
2682 tty_setcolor (DFF_CHD_COLOR);
2683 memset (buf, ' ', width);
2684 buf[width] = '\0';
2686 tty_gotoyx (r + j, c);
2687 /* tty_print_nstring (buf, width); */
2688 col = 0;
2689 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2691 gboolean ch_res;
2693 #ifdef HAVE_CHARSET
2694 if (dview->utf8)
2696 int ch_length = 0;
2698 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2699 if (ch_length > 1)
2700 cnt += ch_length - 1;
2701 if (!g_unichar_isprint (next_ch))
2702 next_ch = '.';
2704 else
2705 #endif
2706 ch_res = dview_get_byte (buf + cnt, &next_ch);
2708 if (ch_res)
2710 #ifdef HAVE_CHARSET
2711 if (mc_global.utf8_display)
2713 if (!dview->utf8)
2715 next_ch =
2716 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2719 else if (dview->utf8)
2720 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2721 else
2722 next_ch = convert_to_display_c (next_ch);
2723 #endif
2725 tty_print_anychar (next_ch);
2726 col++;
2730 tty_setcolor (NORMAL_COLOR);
2731 k = width;
2732 if (width < xwidth - 1)
2733 k = xwidth - 1;
2734 memset (buf, ' ', k);
2735 buf[k] = '\0';
2736 for (; j < height; j++)
2738 if (xwidth != 0)
2740 tty_gotoyx (r + j, c - xwidth);
2741 /* tty_print_nstring (buf, xwidth - 1); */
2742 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2744 tty_gotoyx (r + j, c);
2745 /* tty_print_nstring (buf, width); */
2746 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2749 return 0;
2752 /* --------------------------------------------------------------------------------------------- */
2754 static void
2755 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2757 const char *buf;
2758 int filename_width;
2759 int linenum, lineofs;
2760 vfs_path_t *vpath;
2761 char *path;
2763 tty_setcolor (STATUSBAR_COLOR);
2765 tty_gotoyx (0, c);
2766 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2768 filename_width = width - 24;
2769 if (filename_width < 8)
2770 filename_width = 8;
2772 vpath = vfs_path_from_str (dview->label[ord]);
2773 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2774 vfs_path_free (vpath);
2775 buf = str_term_trim (path, filename_width);
2776 if (ord == DIFF_LEFT)
2777 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2778 buf, linenum, lineofs, dview->skip_cols);
2779 else
2780 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2781 buf, linenum, lineofs, dview->ndiff);
2782 g_free (path);
2785 /* --------------------------------------------------------------------------------------------- */
2787 static void
2788 dview_redo (WDiff * dview)
2790 if (dview->display_numbers)
2792 int old;
2794 old = dview->display_numbers;
2795 dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
2796 dview->new_frame = (old != dview->display_numbers);
2798 dview_reread (dview);
2801 /* --------------------------------------------------------------------------------------------- */
2803 static void
2804 dview_update (WDiff * dview)
2806 int height = dview->height;
2807 int width1;
2808 int width2;
2809 int last;
2811 last = dview->a[DIFF_LEFT]->len - 1;
2813 if (dview->skip_rows > last)
2814 dview->skip_rows = dview->search.last_accessed_num_line = last;
2815 if (dview->skip_rows < 0)
2816 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2817 if (dview->skip_cols < 0)
2818 dview->skip_cols = 0;
2820 if (height < 2)
2821 return;
2823 width1 = dview->half1 + dview->bias;
2824 width2 = dview->half2 - dview->bias;
2825 if (dview->full)
2827 width1 = COLS;
2828 width2 = 0;
2831 if (dview->new_frame)
2833 int xwidth;
2835 tty_setcolor (NORMAL_COLOR);
2836 xwidth = dview->display_symbols + dview->display_numbers;
2837 if (width1 > 1)
2838 tty_draw_box (1, 0, height, width1, FALSE);
2839 if (width2 > 1)
2840 tty_draw_box (1, width1, height, width2, FALSE);
2842 if (xwidth != 0)
2844 xwidth++;
2845 if (xwidth < width1 - 1)
2847 tty_gotoyx (1, xwidth);
2848 tty_print_alt_char (ACS_TTEE, FALSE);
2849 tty_gotoyx (height, xwidth);
2850 tty_print_alt_char (ACS_BTEE, FALSE);
2851 tty_draw_vline (2, xwidth, ACS_VLINE, height - 2);
2853 if (xwidth < width2 - 1)
2855 tty_gotoyx (1, width1 + xwidth);
2856 tty_print_alt_char (ACS_TTEE, FALSE);
2857 tty_gotoyx (height, width1 + xwidth);
2858 tty_print_alt_char (ACS_BTEE, FALSE);
2859 tty_draw_vline (2, width1 + xwidth, ACS_VLINE, height - 2);
2862 dview->new_frame = FALSE;
2865 if (width1 > 2)
2867 dview_status (dview, dview->ord, width1, 0);
2868 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2870 if (width2 > 2)
2872 dview_status (dview, dview->ord ^ 1, width2, width1);
2873 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2877 /* --------------------------------------------------------------------------------------------- */
2879 static void
2880 dview_edit (WDiff * dview, diff_place_t ord)
2882 WDialog *h;
2883 gboolean h_modal;
2884 int linenum, lineofs;
2886 if (dview->dsrc == DATA_SRC_TMP)
2888 error_dialog (_("Edit"), _("Edit is disabled"));
2889 return;
2892 h = WIDGET (dview)->owner;
2893 h_modal = widget_get_state (WIDGET (h), WST_MODAL);
2895 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2897 /* disallow edit file in several editors */
2898 widget_set_state (WIDGET (h), WST_MODAL, TRUE);
2901 vfs_path_t *tmp_vpath;
2903 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2904 edit_file_at_line (tmp_vpath, use_internal_edit != 0, linenum);
2905 vfs_path_free (tmp_vpath);
2908 widget_set_state (WIDGET (h), WST_MODAL, h_modal);
2909 dview_redo (dview);
2910 dview_update (dview);
2913 /* --------------------------------------------------------------------------------------------- */
2915 static void
2916 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2918 static gboolean first_run = TRUE;
2920 /* *INDENT-OFF* */
2921 static const char *title[2] = {
2922 N_("Goto line (left)"),
2923 N_("Goto line (right)")
2925 /* *INDENT-ON* */
2927 int newline;
2928 char *input;
2930 input =
2931 input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE,
2932 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
2933 if (input != NULL)
2935 const char *s = input;
2937 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2939 size_t i = 0;
2941 if (newline > 0)
2943 for (; i < dview->a[ord]->len; i++)
2945 const DIFFLN *p;
2947 p = &g_array_index (dview->a[ord], DIFFLN, i);
2948 if (p->line == newline)
2949 break;
2952 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2954 g_free (input);
2957 first_run = FALSE;
2960 /* --------------------------------------------------------------------------------------------- */
2962 static void
2963 dview_labels (WDiff * dview)
2965 Widget *d;
2966 WDialog *h;
2967 WButtonBar *b;
2969 d = WIDGET (dview);
2970 h = d->owner;
2971 b = find_buttonbar (h);
2973 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, d);
2974 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, d);
2975 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, d);
2976 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, d);
2977 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, d);
2978 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, d);
2979 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, d);
2982 /* --------------------------------------------------------------------------------------------- */
2984 static gboolean
2985 dview_save (WDiff * dview)
2987 gboolean res = TRUE;
2989 if (dview->merged[DIFF_LEFT])
2991 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2992 dview->merged[DIFF_LEFT] = !res;
2994 if (dview->merged[DIFF_RIGHT])
2996 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2997 dview->merged[DIFF_RIGHT] = !res;
2999 return res;
3002 /* --------------------------------------------------------------------------------------------- */
3004 static void
3005 dview_do_save (WDiff * dview)
3007 (void) dview_save (dview);
3010 /* --------------------------------------------------------------------------------------------- */
3012 static void
3013 dview_save_options (WDiff * dview)
3015 mc_config_set_bool (mc_global.main_config, "DiffView", "show_symbols",
3016 dview->display_symbols != 0);
3017 mc_config_set_bool (mc_global.main_config, "DiffView", "show_numbers",
3018 dview->display_numbers != 0);
3019 mc_config_set_int (mc_global.main_config, "DiffView", "tab_size", dview->tab_size);
3021 mc_config_set_int (mc_global.main_config, "DiffView", "diff_quality", dview->opt.quality);
3023 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_tws",
3024 dview->opt.strip_trailing_cr);
3025 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space",
3026 dview->opt.ignore_all_space);
3027 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change",
3028 dview->opt.ignore_space_change);
3029 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_tab_expansion",
3030 dview->opt.ignore_tab_expansion);
3031 mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_case",
3032 dview->opt.ignore_case);
3035 /* --------------------------------------------------------------------------------------------- */
3037 static void
3038 dview_load_options (WDiff * dview)
3040 gboolean show_numbers, show_symbols;
3041 int tab_size;
3043 show_symbols = mc_config_get_bool (mc_global.main_config, "DiffView", "show_symbols", FALSE);
3044 if (show_symbols)
3045 dview->display_symbols = 1;
3046 show_numbers = mc_config_get_bool (mc_global.main_config, "DiffView", "show_numbers", FALSE);
3047 if (show_numbers)
3048 dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
3049 tab_size = mc_config_get_int (mc_global.main_config, "DiffView", "tab_size", 8);
3050 if (tab_size > 0 && tab_size < 9)
3051 dview->tab_size = tab_size;
3052 else
3053 dview->tab_size = 8;
3055 dview->opt.quality = mc_config_get_int (mc_global.main_config, "DiffView", "diff_quality", 0);
3057 dview->opt.strip_trailing_cr =
3058 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_tws", FALSE);
3059 dview->opt.ignore_all_space =
3060 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space", FALSE);
3061 dview->opt.ignore_space_change =
3062 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change", FALSE);
3063 dview->opt.ignore_tab_expansion =
3064 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_tab_expansion", FALSE);
3065 dview->opt.ignore_case =
3066 mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_case", FALSE);
3068 dview->new_frame = TRUE;
3071 /* --------------------------------------------------------------------------------------------- */
3074 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3075 * ask user.
3077 static gboolean
3078 dview_ok_to_exit (WDiff * dview)
3080 gboolean res = TRUE;
3081 int act;
3083 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3084 return res;
3086 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3087 _("File(s) was modified. Save with exit?") :
3088 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3089 D_NORMAL, 2, _("&Yes"), _("&No"));
3091 /* Esc is No */
3092 if (mc_global.midnight_shutdown || (act == -1))
3093 act = 1;
3095 switch (act)
3097 case -1: /* Esc */
3098 res = FALSE;
3099 break;
3100 case 0: /* Yes */
3101 (void) dview_save (dview);
3102 res = TRUE;
3103 break;
3104 case 1: /* No */
3105 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3106 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3107 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3108 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3109 /* fall through */
3110 default:
3111 res = TRUE;
3112 break;
3114 return res;
3117 /* --------------------------------------------------------------------------------------------- */
3119 static cb_ret_t
3120 dview_execute_cmd (WDiff * dview, long command)
3122 cb_ret_t res = MSG_HANDLED;
3124 switch (command)
3126 case CK_ShowSymbols:
3127 dview->display_symbols ^= 1;
3128 dview->new_frame = TRUE;
3129 break;
3130 case CK_ShowNumbers:
3131 dview->display_numbers ^= calc_nwidth ((const GArray * const *) dview->a);
3132 dview->new_frame = TRUE;
3133 break;
3134 case CK_SplitFull:
3135 dview->full = !dview->full;
3136 dview->new_frame = TRUE;
3137 break;
3138 case CK_SplitEqual:
3139 if (!dview->full)
3141 dview->bias = 0;
3142 dview->new_frame = TRUE;
3144 break;
3145 case CK_SplitMore:
3146 if (!dview->full)
3148 dview_compute_split (dview, 1);
3149 dview->new_frame = TRUE;
3151 break;
3153 case CK_SplitLess:
3154 if (!dview->full)
3156 dview_compute_split (dview, -1);
3157 dview->new_frame = TRUE;
3159 break;
3160 case CK_Tab2:
3161 dview->tab_size = 2;
3162 break;
3163 case CK_Tab3:
3164 dview->tab_size = 3;
3165 break;
3166 case CK_Tab4:
3167 dview->tab_size = 4;
3168 break;
3169 case CK_Tab8:
3170 dview->tab_size = 8;
3171 break;
3172 case CK_Swap:
3173 dview->ord ^= 1;
3174 break;
3175 case CK_Redo:
3176 dview_redo (dview);
3177 break;
3178 case CK_HunkNext:
3179 dview->skip_rows = dview->search.last_accessed_num_line =
3180 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3181 break;
3182 case CK_HunkPrev:
3183 dview->skip_rows = dview->search.last_accessed_num_line =
3184 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3185 break;
3186 case CK_Goto:
3187 dview_goto_cmd (dview, DIFF_RIGHT);
3188 break;
3189 case CK_Edit:
3190 dview_edit (dview, dview->ord);
3191 break;
3192 case CK_Merge:
3193 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3194 dview_redo (dview);
3195 break;
3196 case CK_MergeOther:
3197 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3198 dview_redo (dview);
3199 break;
3200 case CK_EditOther:
3201 dview_edit (dview, dview->ord ^ 1);
3202 break;
3203 case CK_Search:
3204 dview_search_cmd (dview);
3205 break;
3206 case CK_SearchContinue:
3207 dview_continue_search_cmd (dview);
3208 break;
3209 case CK_Top:
3210 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3211 break;
3212 case CK_Bottom:
3213 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3214 break;
3215 case CK_Up:
3216 if (dview->skip_rows > 0)
3218 dview->skip_rows--;
3219 dview->search.last_accessed_num_line = dview->skip_rows;
3221 break;
3222 case CK_Down:
3223 dview->skip_rows++;
3224 dview->search.last_accessed_num_line = dview->skip_rows;
3225 break;
3226 case CK_PageDown:
3227 if (dview->height > 2)
3229 dview->skip_rows += dview->height - 2;
3230 dview->search.last_accessed_num_line = dview->skip_rows;
3232 break;
3233 case CK_PageUp:
3234 if (dview->height > 2)
3236 dview->skip_rows -= dview->height - 2;
3237 dview->search.last_accessed_num_line = dview->skip_rows;
3239 break;
3240 case CK_Left:
3241 dview->skip_cols--;
3242 break;
3243 case CK_Right:
3244 dview->skip_cols++;
3245 break;
3246 case CK_LeftQuick:
3247 dview->skip_cols -= 8;
3248 break;
3249 case CK_RightQuick:
3250 dview->skip_cols += 8;
3251 break;
3252 case CK_Home:
3253 dview->skip_cols = 0;
3254 break;
3255 case CK_Shell:
3256 view_other_cmd ();
3257 break;
3258 case CK_Quit:
3259 dview->view_quit = TRUE;
3260 break;
3261 case CK_Save:
3262 dview_do_save (dview);
3263 break;
3264 case CK_Options:
3265 dview_diff_options (dview);
3266 break;
3267 #ifdef HAVE_CHARSET
3268 case CK_SelectCodepage:
3269 dview_select_encoding (dview);
3270 break;
3271 #endif
3272 case CK_Cancel:
3273 /* don't close diffviewer due to SIGINT */
3274 break;
3275 default:
3276 res = MSG_NOT_HANDLED;
3278 return res;
3281 /* --------------------------------------------------------------------------------------------- */
3283 static cb_ret_t
3284 dview_handle_key (WDiff * dview, int key)
3286 long command;
3288 #ifdef HAVE_CHARSET
3289 key = convert_from_input_c (key);
3290 #endif
3292 command = keybind_lookup_keymap_command (diff_map, key);
3293 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3294 return MSG_HANDLED;
3296 /* Key not used */
3297 return MSG_NOT_HANDLED;
3300 /* --------------------------------------------------------------------------------------------- */
3302 static cb_ret_t
3303 dview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3305 WDiff *dview = (WDiff *) w;
3306 WDialog *h = w->owner;
3307 cb_ret_t i;
3309 switch (msg)
3311 case MSG_INIT:
3312 dview_labels (dview);
3313 dview_load_options (dview);
3314 dview_update (dview);
3315 return MSG_HANDLED;
3317 case MSG_DRAW:
3318 dview->new_frame = TRUE;
3319 dview_update (dview);
3320 return MSG_HANDLED;
3322 case MSG_KEY:
3323 i = dview_handle_key (dview, parm);
3324 if (dview->view_quit)
3325 dlg_stop (h);
3326 else
3327 dview_update (dview);
3328 return i;
3330 case MSG_ACTION:
3331 i = dview_execute_cmd (dview, parm);
3332 if (dview->view_quit)
3333 dlg_stop (h);
3334 else
3335 dview_update (dview);
3336 return i;
3338 case MSG_DESTROY:
3339 dview_save_options (dview);
3340 dview_fini (dview);
3341 return MSG_HANDLED;
3343 default:
3344 return widget_default_callback (w, sender, msg, parm, data);
3348 /* --------------------------------------------------------------------------------------------- */
3350 static void
3351 dview_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3353 WDiff *dview = (WDiff *) w;
3355 (void) event;
3357 switch (msg)
3359 case MSG_MOUSE_SCROLL_UP:
3360 case MSG_MOUSE_SCROLL_DOWN:
3361 if (msg == MSG_MOUSE_SCROLL_UP)
3362 dview->skip_rows -= 2;
3363 else
3364 dview->skip_rows += 2;
3366 dview->search.last_accessed_num_line = dview->skip_rows;
3367 dview_update (dview);
3368 break;
3370 default:
3371 break;
3375 /* --------------------------------------------------------------------------------------------- */
3377 static void
3378 dview_adjust_size (WDialog * h)
3380 WDiff *dview;
3381 WButtonBar *bar;
3383 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3384 dview = (WDiff *) find_widget_type (h, dview_callback);
3385 bar = find_buttonbar (h);
3386 widget_set_size (WIDGET (dview), 0, 0, LINES - 1, COLS);
3387 widget_set_size (WIDGET (bar), LINES - 1, 0, 1, COLS);
3389 dview_compute_areas (dview);
3392 /* --------------------------------------------------------------------------------------------- */
3394 static cb_ret_t
3395 dview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3397 WDiff *dview;
3398 WDialog *h = DIALOG (w);
3400 switch (msg)
3402 case MSG_RESIZE:
3403 dview_adjust_size (h);
3404 return MSG_HANDLED;
3406 case MSG_ACTION:
3407 /* Handle shortcuts. */
3409 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3410 * here, which is why we can pass NULL in the following call. */
3411 return dview_execute_cmd (NULL, parm);
3413 case MSG_VALIDATE:
3414 dview = (WDiff *) find_widget_type (h, dview_callback);
3415 /* don't stop the dialog before final decision */
3416 widget_set_state (w, WST_ACTIVE, TRUE);
3417 if (dview_ok_to_exit (dview))
3418 dlg_stop (h);
3419 return MSG_HANDLED;
3421 default:
3422 return dlg_default_callback (w, sender, msg, parm, data);
3426 /* --------------------------------------------------------------------------------------------- */
3428 static char *
3429 dview_get_title (const WDialog * h, size_t len)
3431 const WDiff *dview;
3432 const char *modified = " (*) ";
3433 const char *notmodified = " ";
3434 size_t len1;
3435 GString *title;
3437 dview = (const WDiff *) find_widget_type (h, dview_callback);
3438 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3440 title = g_string_sized_new (len);
3441 g_string_append (title, _("Diff:"));
3442 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3443 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3444 g_string_append (title, " | ");
3445 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3446 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3448 return g_string_free (title, FALSE);
3451 /* --------------------------------------------------------------------------------------------- */
3453 static int
3454 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3456 int error;
3457 WDiff *dview;
3458 Widget *w;
3459 WDialog *dview_dlg;
3461 /* Create dialog and widgets, put them on the dialog */
3462 dview_dlg =
3463 dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, NULL, dview_dialog_callback, NULL,
3464 "[Diff Viewer]", NULL);
3465 widget_want_tab (WIDGET (dview_dlg), TRUE);
3467 dview = g_new0 (WDiff, 1);
3468 w = WIDGET (dview);
3469 widget_init (w, 0, 0, LINES - 1, COLS, dview_callback, dview_mouse_callback);
3471 add_widget (dview_dlg, dview);
3472 add_widget (dview_dlg, buttonbar_new (TRUE));
3474 dview_dlg->get_title = dview_get_title;
3476 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3478 /* Please note that if you add another widget,
3479 * you have to modify dview_adjust_size to
3480 * be aware of it
3482 if (error == 0)
3483 dlg_run (dview_dlg);
3485 if (error != 0 || widget_get_state (WIDGET (dview_dlg), WST_CLOSED))
3486 dlg_destroy (dview_dlg);
3488 return error == 0 ? 1 : 0;
3491 /*** public functions ****************************************************************************/
3492 /* --------------------------------------------------------------------------------------------- */
3494 #define GET_FILE_AND_STAMP(n) \
3495 do \
3497 use_copy##n = 0; \
3498 real_file##n = file##n; \
3499 if (!vfs_file_is_local (file##n)) \
3501 real_file##n = mc_getlocalcopy (file##n); \
3502 if (real_file##n != NULL) \
3504 use_copy##n = 1; \
3505 if (mc_stat (real_file##n, &st##n) != 0) \
3506 use_copy##n = -1; \
3510 while (0)
3512 #define UNGET_FILE(n) \
3513 do \
3515 if (use_copy##n) \
3517 int changed = 0; \
3518 if (use_copy##n > 0) \
3520 time_t mtime; \
3521 mtime = st##n.st_mtime; \
3522 if (mc_stat (real_file##n, &st##n) == 0) \
3523 changed = (mtime != st##n.st_mtime); \
3525 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3526 vfs_path_free (real_file##n); \
3529 while (0)
3531 gboolean
3532 dview_diff_cmd (const void *f0, const void *f1)
3534 int rv = 0;
3535 vfs_path_t *file0 = NULL;
3536 vfs_path_t *file1 = NULL;
3537 gboolean is_dir0 = FALSE;
3538 gboolean is_dir1 = FALSE;
3540 switch (mc_global.mc_run_mode)
3542 case MC_RUN_FULL:
3544 /* run from panels */
3545 const WPanel *panel0 = (const WPanel *) f0;
3546 const WPanel *panel1 = (const WPanel *) f1;
3548 file0 =
3549 vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, (char *) NULL);
3550 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3551 if (is_dir0)
3553 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3554 path_trunc (selection (panel0)->fname, 30));
3555 goto ret;
3558 file1 =
3559 vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, (char *) NULL);
3560 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3561 if (is_dir1)
3563 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3564 path_trunc (selection (panel1)->fname, 30));
3565 goto ret;
3567 break;
3570 case MC_RUN_DIFFVIEWER:
3572 /* run from command line */
3573 const char *p0 = (const char *) f0;
3574 const char *p1 = (const char *) f1;
3575 struct stat st;
3577 file0 = vfs_path_from_str (p0);
3578 if (mc_stat (file0, &st) == 0)
3580 is_dir0 = S_ISDIR (st.st_mode);
3581 if (is_dir0)
3583 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3584 goto ret;
3587 else
3589 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3590 path_trunc (p0, 30), unix_error_string (errno));
3591 goto ret;
3594 file1 = vfs_path_from_str (p1);
3595 if (mc_stat (file1, &st) == 0)
3597 is_dir1 = S_ISDIR (st.st_mode);
3598 if (is_dir1)
3600 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3601 goto ret;
3604 else
3606 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3607 path_trunc (p1, 30), unix_error_string (errno));
3608 goto ret;
3610 break;
3613 default:
3614 /* this should not happaned */
3615 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3616 return FALSE;
3619 if (rv == 0)
3621 rv = -1;
3622 if (file0 != NULL && file1 != NULL)
3624 int use_copy0;
3625 int use_copy1;
3626 struct stat st0;
3627 struct stat st1;
3628 vfs_path_t *real_file0;
3629 vfs_path_t *real_file1;
3631 GET_FILE_AND_STAMP (0);
3632 GET_FILE_AND_STAMP (1);
3634 if (real_file0 != NULL && real_file1 != NULL)
3635 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3636 vfs_path_as_str (file0), vfs_path_as_str (file1));
3638 UNGET_FILE (1);
3639 UNGET_FILE (0);
3643 if (rv == 0)
3644 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3646 ret:
3647 vfs_path_free (file1);
3648 vfs_path_free (file0);
3650 return (rv != 0);
3653 #undef GET_FILE_AND_STAMP
3654 #undef UNGET_FILE
3656 /* --------------------------------------------------------------------------------------------- */