mcdiffviewer: use new quick dialog engine.
[midnight-commander.git] / src / diffviewer / ydiff.c
blobc9df77f05c2dc7215e2429ef76e8c7ca52e62aad
1 /*
2 Copyright (C) 2007, 2010, 2011, 2012
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, 2012
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 ADD_CH '+'
85 #define DEL_CH '-'
86 #define CHG_CH '*'
87 #define EQU_CH ' '
89 #define HDIFF_ENABLE 1
90 #define HDIFF_MINCTX 5
91 #define HDIFF_DEPTH 10
93 #define FILE_DIRTY(fs) \
94 do \
95 { \
96 (fs)->pos = 0; \
97 (fs)->len = 0; \
98 } \
99 while (0)
101 /*** file scope type declarations ****************************************************************/
103 typedef enum
105 FROM_LEFT_TO_RIGHT,
106 FROM_RIGHT_TO_LEFT
107 } action_direction_t;
109 /*** file scope variables ************************************************************************/
111 /*** file scope functions ************************************************************************/
112 /* --------------------------------------------------------------------------------------------- */
114 static inline int
115 TAB_SKIP (int ts, int pos)
117 if (ts > 0 && ts < 9)
118 return ts - pos % ts;
119 else
120 return 8 - pos % 8;
123 /* --------------------------------------------------------------------------------------------- */
125 static gboolean
126 rewrite_backup_content (const vfs_path_t * from_file_name_vpath, const char *to_file_name)
128 FILE *backup_fd;
129 char *contents;
130 gsize length;
131 const char *from_file_name;
133 from_file_name = vfs_path_get_by_index (from_file_name_vpath, -1)->path;
134 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
135 return FALSE;
137 backup_fd = fopen (to_file_name, "w");
138 if (backup_fd == NULL)
140 g_free (contents);
141 return FALSE;
144 length = fwrite ((const void *) contents, length, 1, backup_fd);
146 fflush (backup_fd);
147 fclose (backup_fd);
148 g_free (contents);
149 return TRUE;
152 /* buffered I/O ************************************************************* */
155 * Try to open a temporary file.
156 * @note the name is not altered if this function fails
158 * @param[out] name address of a pointer to store the temporary name
159 * @return file descriptor on success, negative on error
162 static int
163 open_temp (void **name)
165 int fd;
166 vfs_path_t *diff_file_name = NULL;
168 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
169 if (fd == -1)
171 message (D_ERROR, MSG_ERROR,
172 _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
173 return -1;
175 *name = vfs_path_to_str (diff_file_name);
176 vfs_path_free (diff_file_name);
177 return fd;
180 /* --------------------------------------------------------------------------------------------- */
183 * Alocate file structure and associate file descriptor to it.
185 * @param fd file descriptor
186 * @return file structure
189 static FBUF *
190 f_dopen (int fd)
192 FBUF *fs;
194 if (fd < 0)
195 return NULL;
197 fs = g_try_malloc (sizeof (FBUF));
198 if (fs == NULL)
199 return NULL;
201 fs->buf = g_try_malloc (FILE_READ_BUF);
202 if (fs->buf == NULL)
204 g_free (fs);
205 return NULL;
208 fs->fd = fd;
209 FILE_DIRTY (fs);
210 fs->flags = 0;
211 fs->data = NULL;
213 return fs;
216 /* --------------------------------------------------------------------------------------------- */
219 * Free file structure without closing the file.
221 * @param fs file structure
222 * @return 0 on success, non-zero on error
225 static int
226 f_free (FBUF * fs)
228 int rv = 0;
230 if (fs->flags & FILE_FLAG_TEMP)
232 rv = unlink (fs->data);
233 g_free (fs->data);
235 g_free (fs->buf);
236 g_free (fs);
237 return rv;
240 /* --------------------------------------------------------------------------------------------- */
243 * Open a binary temporary file in R/W mode.
244 * @note the file will be deleted when closed
246 * @return file structure
248 static FBUF *
249 f_temp (void)
251 int fd;
252 FBUF *fs;
254 fs = f_dopen (0);
255 if (fs == NULL)
256 return NULL;
258 fd = open_temp (&fs->data);
259 if (fd < 0)
261 f_free (fs);
262 return NULL;
265 fs->fd = fd;
266 fs->flags = FILE_FLAG_TEMP;
267 return fs;
270 /* --------------------------------------------------------------------------------------------- */
273 * Open a binary file in specified mode.
275 * @param filename file name
276 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
278 * @return file structure
281 static FBUF *
282 f_open (const char *filename, int flags)
284 int fd;
285 FBUF *fs;
287 fs = f_dopen (0);
288 if (fs == NULL)
289 return NULL;
291 fd = open (filename, flags);
292 if (fd < 0)
294 f_free (fs);
295 return NULL;
298 fs->fd = fd;
299 return fs;
302 /* --------------------------------------------------------------------------------------------- */
305 * Read a line of bytes from file until newline or EOF.
306 * @note does not stop on null-byte
307 * @note buf will not be null-terminated
309 * @param buf destination buffer
310 * @param size size of buffer
311 * @param fs file structure
313 * @return number of bytes read
316 static size_t
317 f_gets (char *buf, size_t size, FBUF * fs)
319 size_t j = 0;
323 int i;
324 int stop = 0;
326 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
328 buf[j] = fs->buf[i];
329 if (buf[j] == '\n')
330 stop = 1;
332 fs->pos = i;
334 if (j == size || stop)
335 break;
337 fs->pos = 0;
338 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
340 while (fs->len > 0);
342 return j;
345 /* --------------------------------------------------------------------------------------------- */
348 * Seek into file.
349 * @note avoids thrashing read cache when possible
351 * @param fs file structure
352 * @param off offset
353 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
355 * @return position in file, starting from begginning
358 static off_t
359 f_seek (FBUF * fs, off_t off, int whence)
361 off_t rv;
363 if (fs->len && whence != SEEK_END)
365 rv = lseek (fs->fd, 0, SEEK_CUR);
366 if (rv != -1)
368 if (whence == SEEK_CUR)
370 whence = SEEK_SET;
371 off += rv - fs->len + fs->pos;
373 if (off - rv >= -fs->len && off - rv <= 0)
375 fs->pos = fs->len + off - rv;
376 return off;
381 rv = lseek (fs->fd, off, whence);
382 if (rv != -1)
383 FILE_DIRTY (fs);
384 return rv;
387 /* --------------------------------------------------------------------------------------------- */
390 * Seek to the beginning of file, thrashing read cache.
392 * @param fs file structure
394 * @return 0 if success, non-zero on error
397 static off_t
398 f_reset (FBUF * fs)
400 off_t rv;
402 rv = lseek (fs->fd, 0, SEEK_SET);
403 if (rv != -1)
404 FILE_DIRTY (fs);
405 return rv;
408 /* --------------------------------------------------------------------------------------------- */
411 * Write bytes to file.
412 * @note thrashes read cache
414 * @param fs file structure
415 * @param buf source buffer
416 * @param size size of buffer
418 * @return number of written bytes, -1 on error
421 static ssize_t
422 f_write (FBUF * fs, const char *buf, size_t size)
424 ssize_t rv;
426 rv = write (fs->fd, buf, size);
427 if (rv >= 0)
428 FILE_DIRTY (fs);
429 return rv;
432 /* --------------------------------------------------------------------------------------------- */
435 * Truncate file to the current position.
436 * @note thrashes read cache
438 * @param fs file structure
440 * @return current file size on success, negative on error
443 static off_t
444 f_trunc (FBUF * fs)
446 off_t off;
448 off = lseek (fs->fd, 0, SEEK_CUR);
449 if (off != -1)
451 int rv;
453 rv = ftruncate (fs->fd, off);
454 if (rv != 0)
455 off = -1;
456 else
457 FILE_DIRTY (fs);
459 return off;
462 /* --------------------------------------------------------------------------------------------- */
465 * Close file.
466 * @note if this is temporary file, it is deleted
468 * @param fs file structure
469 * @return 0 on success, non-zero on error
472 static int
473 f_close (FBUF * fs)
475 int rv = -1;
477 if (fs != NULL)
479 rv = close (fs->fd);
480 f_free (fs);
483 return rv;
486 /* --------------------------------------------------------------------------------------------- */
489 * Create pipe stream to process.
491 * @param cmd shell command line
492 * @param flags open mode, either O_RDONLY or O_WRONLY
494 * @return file structure
497 static FBUF *
498 p_open (const char *cmd, int flags)
500 FILE *f;
501 FBUF *fs;
502 const char *type = NULL;
504 if (flags == O_RDONLY)
505 type = "r";
506 else if (flags == O_WRONLY)
507 type = "w";
509 if (type == NULL)
510 return NULL;
512 fs = f_dopen (0);
513 if (fs == NULL)
514 return NULL;
516 f = popen (cmd, type);
517 if (f == NULL)
519 f_free (fs);
520 return NULL;
523 fs->fd = fileno (f);
524 fs->data = f;
525 return fs;
528 /* --------------------------------------------------------------------------------------------- */
531 * Close pipe stream.
533 * @param fs structure
534 * @return 0 on success, non-zero on error
537 static int
538 p_close (FBUF * fs)
540 int rv = -1;
542 if (fs != NULL)
544 rv = pclose (fs->data);
545 f_free (fs);
548 return rv;
551 /* --------------------------------------------------------------------------------------------- */
554 * Get one char (byte) from string
556 * @param char * str, gboolean * result
557 * @return int as character or 0 and result == FALSE if fail
560 static int
561 dview_get_byte (char *str, gboolean * result)
563 if (str == NULL)
565 *result = FALSE;
566 return 0;
568 *result = TRUE;
569 return (unsigned char) *str;
572 /* --------------------------------------------------------------------------------------------- */
575 * Get utf multibyte char from string
577 * @param char * str, int * char_width, gboolean * result
578 * @return int as utf character or 0 and result == FALSE if fail
581 static int
582 dview_get_utf (char *str, int *char_width, gboolean * result)
584 int res = -1;
585 gunichar ch;
586 gchar *next_ch = NULL;
587 int width = 0;
589 *result = TRUE;
591 if (str == NULL)
593 *result = FALSE;
594 return 0;
597 res = g_utf8_get_char_validated (str, -1);
599 if (res < 0)
600 ch = *str;
601 else
603 ch = res;
604 /* Calculate UTF-8 char width */
605 next_ch = g_utf8_next_char (str);
606 if (next_ch != NULL)
607 width = next_ch - str;
608 else
609 ch = 0;
611 *char_width = width;
612 return ch;
615 /* --------------------------------------------------------------------------------------------- */
617 static int
618 dview_str_utf8_offset_to_pos (const char *text, size_t length)
620 ptrdiff_t result;
622 if (text == NULL || text[0] == '\0')
623 return length;
625 if (g_utf8_validate (text, -1, NULL))
626 result = g_utf8_offset_to_pointer (text, length) - text;
627 else
629 gunichar uni;
630 char *tmpbuf, *buffer;
632 buffer = tmpbuf = g_strdup (text);
633 while (tmpbuf[0] != '\0')
635 uni = g_utf8_get_char_validated (tmpbuf, -1);
636 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
637 tmpbuf = g_utf8_next_char (tmpbuf);
638 else
640 tmpbuf[0] = '.';
641 tmpbuf++;
644 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
645 g_free (buffer);
647 return max (length, (size_t) result);
650 /* --------------------------------------------------------------------------------------------- */
652 /* diff parse *************************************************************** */
655 * Read decimal number from string.
657 * @param[in,out] str string to parse
658 * @param[out] n extracted number
659 * @return 0 if success, otherwise non-zero
662 static int
663 scan_deci (const char **str, int *n)
665 const char *p = *str;
666 char *q;
668 errno = 0;
669 *n = strtol (p, &q, 10);
670 if (errno != 0 || p == q)
671 return -1;
672 *str = q;
673 return 0;
676 /* --------------------------------------------------------------------------------------------- */
679 * Parse line for diff statement.
681 * @param p string to parse
682 * @param ops list of diff statements
683 * @return 0 if success, otherwise non-zero
686 static int
687 scan_line (const char *p, GArray * ops)
689 DIFFCMD op;
691 int f1, f2;
692 int t1, t2;
693 int cmd;
694 int range;
696 /* handle the following cases:
697 * NUMaNUM[,NUM]
698 * NUM[,NUM]cNUM[,NUM]
699 * NUM[,NUM]dNUM
700 * where NUM is a positive integer
703 if (scan_deci (&p, &f1) != 0 || f1 < 0)
704 return -1;
706 f2 = f1;
707 range = 0;
708 if (*p == ',')
710 p++;
711 if (scan_deci (&p, &f2) != 0 || f2 < f1)
712 return -1;
714 range = 1;
717 cmd = *p++;
718 if (cmd == 'a')
720 if (range != 0)
721 return -1;
723 else if (cmd != 'c' && cmd != 'd')
724 return -1;
726 if (scan_deci (&p, &t1) != 0 || t1 < 0)
727 return -1;
729 t2 = t1;
730 range = 0;
731 if (*p == ',')
733 p++;
734 if (scan_deci (&p, &t2) != 0 || t2 < t1)
735 return -1;
737 range = 1;
740 if (cmd == 'd' && range != 0)
741 return -1;
743 op.a[0][0] = f1;
744 op.a[0][1] = f2;
745 op.cmd = cmd;
746 op.a[1][0] = t1;
747 op.a[1][1] = t2;
748 g_array_append_val (ops, op);
749 return 0;
752 /* --------------------------------------------------------------------------------------------- */
755 * Parse diff output and extract diff statements.
757 * @param f stream to read from
758 * @param ops list of diff statements to fill
759 * @return positive number indicating number of hunks, otherwise negative
762 static int
763 scan_diff (FBUF * f, GArray * ops)
765 int sz;
766 char buf[BUFSIZ];
768 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
770 if (isdigit (buf[0]))
772 if (buf[sz - 1] != '\n')
773 return -1;
775 buf[sz] = '\0';
776 if (scan_line (buf, ops) != 0)
777 return -1;
779 continue;
782 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
786 return ops->len;
789 /* --------------------------------------------------------------------------------------------- */
792 * Invoke diff and extract diff statements.
794 * @param args extra arguments to be passed to diff
795 * @param extra more arguments to be passed to diff
796 * @param file1 first file to compare
797 * @param file2 second file to compare
798 * @param ops list of diff statements to fill
800 * @return positive number indicating number of hunks, otherwise negative
803 static int
804 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
805 GArray * ops)
807 static const char *opt =
808 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
809 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
810 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
811 " --unchanged-group-format=''";
813 int rv;
814 FBUF *f;
815 char *cmd;
816 int code;
817 char *file1_esc, *file2_esc;
819 /* escape potential $ to avoid shell variable substitutions in popen() */
820 file1_esc = strutils_shell_escape (file1);
821 file2_esc = strutils_shell_escape (file2);
822 cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
823 g_free (file1_esc);
824 g_free (file2_esc);
826 if (cmd == NULL)
827 return -1;
829 f = p_open (cmd, O_RDONLY);
830 g_free (cmd);
832 if (f == NULL)
833 return -1;
835 rv = scan_diff (f, ops);
836 code = p_close (f);
838 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
839 rv = -1;
841 return rv;
844 /* --------------------------------------------------------------------------------------------- */
847 * Reparse and display file according to diff statements.
849 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
850 * @param filename file name to display
851 * @param ops list of diff statements
852 * @param printer printf-like function to be used for displaying
853 * @param ctx printer context
855 * @return 0 if success, otherwise non-zero
858 static int
859 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
861 size_t i;
862 FBUF *f;
863 size_t sz;
864 char buf[BUFSIZ];
865 int line = 0;
866 off_t off = 0;
867 const DIFFCMD *op;
868 diff_place_t eff;
869 int add_cmd;
870 int del_cmd;
872 f = f_open (filename, O_RDONLY);
873 if (f == NULL)
874 return -1;
876 ord &= 1;
877 eff = ord;
879 add_cmd = 'a';
880 del_cmd = 'd';
881 if (ord != 0)
883 add_cmd = 'd';
884 del_cmd = 'a';
886 #define F1 a[eff][0]
887 #define F2 a[eff][1]
888 #define T1 a[ ord^1 ][0]
889 #define T2 a[ ord^1 ][1]
890 for (i = 0; i < ops->len; i++)
892 int n;
894 op = &g_array_index (ops, DIFFCMD, i);
895 n = op->F1 - (op->cmd != add_cmd);
897 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
899 line++;
900 printer (ctx, EQU_CH, line, off, sz, buf);
901 off += sz;
902 while (buf[sz - 1] != '\n')
904 sz = f_gets (buf, sizeof (buf), f);
905 if (sz == 0)
907 printer (ctx, 0, 0, 0, 1, "\n");
908 break;
910 printer (ctx, 0, 0, 0, sz, buf);
911 off += sz;
915 if (line != n)
916 goto err;
918 if (op->cmd == add_cmd)
920 n = op->T2 - op->T1 + 1;
921 while (n != 0)
923 printer (ctx, DEL_CH, 0, 0, 1, "\n");
924 n--;
928 if (op->cmd == del_cmd)
930 n = op->F2 - op->F1 + 1;
931 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
933 line++;
934 printer (ctx, ADD_CH, line, off, sz, buf);
935 off += sz;
936 while (buf[sz - 1] != '\n')
938 sz = f_gets (buf, sizeof (buf), f);
939 if (sz == 0)
941 printer (ctx, 0, 0, 0, 1, "\n");
942 break;
944 printer (ctx, 0, 0, 0, sz, buf);
945 off += sz;
947 n--;
950 if (n != 0)
951 goto err;
954 if (op->cmd == 'c')
956 n = op->F2 - op->F1 + 1;
957 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
959 line++;
960 printer (ctx, CHG_CH, line, off, sz, buf);
961 off += sz;
962 while (buf[sz - 1] != '\n')
964 sz = f_gets (buf, sizeof (buf), f);
965 if (sz == 0)
967 printer (ctx, 0, 0, 0, 1, "\n");
968 break;
970 printer (ctx, 0, 0, 0, sz, buf);
971 off += sz;
973 n--;
976 if (n != 0)
977 goto err;
979 n = op->T2 - op->T1 - (op->F2 - op->F1);
980 while (n > 0)
982 printer (ctx, CHG_CH, 0, 0, 1, "\n");
983 n--;
987 #undef T2
988 #undef T1
989 #undef F2
990 #undef F1
992 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
994 line++;
995 printer (ctx, EQU_CH, line, off, sz, buf);
996 off += sz;
997 while (buf[sz - 1] != '\n')
999 sz = f_gets (buf, sizeof (buf), f);
1000 if (sz == 0)
1002 printer (ctx, 0, 0, 0, 1, "\n");
1003 break;
1005 printer (ctx, 0, 0, 0, sz, buf);
1006 off += sz;
1010 f_close (f);
1011 return 0;
1013 err:
1014 f_close (f);
1015 return -1;
1018 /* --------------------------------------------------------------------------------------------- */
1020 /* horizontal diff ********************************************************** */
1023 * Longest common substring.
1025 * @param s first string
1026 * @param m length of first string
1027 * @param t second string
1028 * @param n length of second string
1029 * @param ret list of offsets for longest common substrings inside each string
1030 * @param min minimum length of common substrings
1032 * @return 0 if success, nonzero otherwise
1035 static int
1036 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1038 int i, j;
1039 int *Lprev, *Lcurr;
1040 int z = 0;
1042 if (m < min || n < min)
1044 /* XXX early culling */
1045 return 0;
1048 Lprev = g_try_new0 (int, n + 1);
1049 if (Lprev == NULL)
1050 return -1;
1052 Lcurr = g_try_new0 (int, n + 1);
1053 if (Lcurr == NULL)
1055 g_free (Lprev);
1056 return -1;
1059 for (i = 0; i < m; i++)
1061 int *L;
1063 L = Lprev;
1064 Lprev = Lcurr;
1065 Lcurr = L;
1066 #ifdef USE_MEMSET_IN_LCS
1067 memset (Lcurr, 0, (n + 1) * sizeof (int));
1068 #endif
1069 for (j = 0; j < n; j++)
1071 #ifndef USE_MEMSET_IN_LCS
1072 Lcurr[j + 1] = 0;
1073 #endif
1074 if (s[i] == t[j])
1076 int v;
1078 v = Lprev[j] + 1;
1079 Lcurr[j + 1] = v;
1080 if (z < v)
1082 z = v;
1083 g_array_set_size (ret, 0);
1085 if (z == v && z >= min)
1087 int off0, off1;
1088 size_t k;
1090 off0 = i - z + 1;
1091 off1 = j - z + 1;
1093 for (k = 0; k < ret->len; k++)
1095 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1096 if ((*p)[0] == off0 || (*p)[1] >= off1)
1097 break;
1099 if (k == ret->len)
1101 PAIR p2;
1103 p2[0] = off0;
1104 p2[1] = off1;
1105 g_array_append_val (ret, p2);
1112 free (Lcurr);
1113 free (Lprev);
1114 return z;
1117 /* --------------------------------------------------------------------------------------------- */
1120 * Scan recursively for common substrings and build ranges.
1122 * @param s first string
1123 * @param t second string
1124 * @param bracket current limits for both of the strings
1125 * @param min minimum length of common substrings
1126 * @param hdiff list of horizontal diff ranges to fill
1127 * @param depth recursion depth
1129 * @return 0 if success, nonzero otherwise
1132 static gboolean
1133 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1134 unsigned int depth)
1136 BRACKET p;
1138 if (depth-- != 0)
1140 GArray *ret;
1141 BRACKET b;
1142 int len;
1144 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1145 if (ret == NULL)
1146 return FALSE;
1148 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1149 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1150 if (ret->len != 0)
1152 size_t k = 0;
1153 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1154 const PAIR *data2;
1156 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1157 b[DIFF_LEFT].len = (*data)[0];
1158 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1159 b[DIFF_RIGHT].len = (*data)[1];
1160 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1161 return FALSE;
1163 for (k = 0; k < ret->len - 1; k++)
1165 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1166 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1167 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1168 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1169 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1170 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1171 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1172 return FALSE;
1174 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1175 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1176 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1177 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1178 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1179 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1180 return FALSE;
1182 g_array_free (ret, TRUE);
1183 return TRUE;
1187 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1188 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1189 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1190 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1191 g_array_append_val (hdiff, p);
1193 return TRUE;
1196 /* --------------------------------------------------------------------------------------------- */
1199 * Build list of horizontal diff ranges.
1201 * @param s first string
1202 * @param m length of first string
1203 * @param t second string
1204 * @param n length of second string
1205 * @param min minimum length of common substrings
1206 * @param hdiff list of horizontal diff ranges to fill
1207 * @param depth recursion depth
1209 * @return 0 if success, nonzero otherwise
1212 static gboolean
1213 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1215 int i;
1216 BRACKET b;
1218 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1219 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1221 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1224 b[DIFF_LEFT].off = i;
1225 b[DIFF_LEFT].len = m - i;
1226 b[DIFF_RIGHT].off = i;
1227 b[DIFF_RIGHT].len = n - i;
1229 /* smartscan (multiple horizontal diff) */
1230 return hdiff_multi (s, t, b, min, hdiff, depth);
1233 /* --------------------------------------------------------------------------------------------- */
1235 /* read line **************************************************************** */
1238 * Check if character is inside horizontal diff limits.
1240 * @param k rank of character inside line
1241 * @param hdiff horizontal diff structure
1242 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1244 * @return TRUE if inside hdiff limits, FALSE otherwise
1247 static gboolean
1248 is_inside (int k, GArray * hdiff, diff_place_t ord)
1250 size_t i;
1251 BRACKET *b;
1253 for (i = 0; i < hdiff->len; i++)
1255 int start, end;
1257 b = &g_array_index (hdiff, BRACKET, i);
1258 start = (*b)[ord].off;
1259 end = start + (*b)[ord].len;
1260 if (k >= start && k < end)
1261 return TRUE;
1263 return FALSE;
1266 /* --------------------------------------------------------------------------------------------- */
1269 * Copy 'src' to 'dst' expanding tabs.
1270 * @note The procedure returns when all bytes are consumed from 'src'
1272 * @param dst destination buffer
1273 * @param src source buffer
1274 * @param srcsize size of src buffer
1275 * @param base virtual base of this string, needed to calculate tabs
1276 * @param ts tab size
1278 * @return new virtual base
1281 static int
1282 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1284 int i;
1286 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1288 *dst = *src;
1289 if (*src == '\t')
1291 int j;
1293 j = TAB_SKIP (ts, i + base);
1294 i += j - 1;
1295 while (j-- > 0)
1296 *dst++ = ' ';
1297 dst--;
1300 return i + base;
1303 /* --------------------------------------------------------------------------------------------- */
1306 * Copy 'src' to 'dst' expanding tabs.
1308 * @param dst destination buffer
1309 * @param dstsize size of dst buffer
1310 * @param[in,out] _src source buffer
1311 * @param srcsize size of src buffer
1312 * @param base virtual base of this string, needed to calculate tabs
1313 * @param ts tab size
1315 * @return new virtual base
1317 * @note The procedure returns when all bytes are consumed from 'src'
1318 * or 'dstsize' bytes are written to 'dst'
1319 * @note Upon return, 'src' points to the first unwritten character in source
1322 static int
1323 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1325 int i;
1326 const char *src = *_src;
1328 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1330 *dst = *src;
1331 if (*src == '\t')
1333 int j;
1335 j = TAB_SKIP (ts, i + base);
1336 if (j > dstsize - i)
1337 j = dstsize - i;
1338 i += j - 1;
1339 while (j-- > 0)
1340 *dst++ = ' ';
1341 dst--;
1344 *_src = src;
1345 return i + base;
1348 /* --------------------------------------------------------------------------------------------- */
1351 * Read line from memory, converting tabs to spaces and padding with spaces.
1353 * @param src buffer to read from
1354 * @param srcsize size of src buffer
1355 * @param dst buffer to read to
1356 * @param dstsize size of dst buffer, excluding trailing null
1357 * @param skip number of characters to skip
1358 * @param ts tab size
1359 * @param show_cr show trailing carriage return as ^M
1361 * @return negative on error, otherwise number of bytes except padding
1364 static int
1365 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr)
1367 int sz = 0;
1369 if (src != NULL)
1371 int i;
1372 char *tmp = dst;
1373 const int base = 0;
1375 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1377 if (*src == '\t')
1379 int j;
1381 j = TAB_SKIP (ts, i + base);
1382 i += j - 1;
1383 while (j-- > 0)
1385 if (skip > 0)
1386 skip--;
1387 else if (dstsize != 0)
1389 dstsize--;
1390 *dst++ = ' ';
1394 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1396 if (skip == 0 && show_cr)
1398 if (dstsize > 1)
1400 dstsize -= 2;
1401 *dst++ = '^';
1402 *dst++ = 'M';
1404 else
1406 dstsize--;
1407 *dst++ = '.';
1410 break;
1412 else if (skip > 0)
1414 int utf_ch = 0;
1415 gboolean res;
1416 int w;
1418 skip--;
1419 utf_ch = dview_get_utf ((char *) src, &w, &res);
1420 if (w > 1)
1421 skip += w - 1;
1422 if (!g_unichar_isprint (utf_ch))
1423 utf_ch = '.';
1425 else
1427 dstsize--;
1428 *dst++ = *src;
1431 sz = dst - tmp;
1433 while (dstsize != 0)
1435 dstsize--;
1436 *dst++ = ' ';
1438 *dst = '\0';
1439 return sz;
1442 /* --------------------------------------------------------------------------------------------- */
1445 * Read line from memory and build attribute array.
1447 * @param src buffer to read from
1448 * @param srcsize size of src buffer
1449 * @param dst buffer to read to
1450 * @param dstsize size of dst buffer, excluding trailing null
1451 * @param skip number of characters to skip
1452 * @param ts tab size
1453 * @param show_cr show trailing carriage return as ^M
1454 * @param hdiff horizontal diff structure
1455 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1456 * @param att buffer of attributes
1458 * @return negative on error, otherwise number of bytes except padding
1461 static int
1462 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr,
1463 GArray * hdiff, diff_place_t ord, char *att)
1465 int sz = 0;
1467 if (src != NULL)
1469 int i, k;
1470 char *tmp = dst;
1471 const int base = 0;
1473 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1475 if (*src == '\t')
1477 int j;
1479 j = TAB_SKIP (ts, i + base);
1480 i += j - 1;
1481 while (j-- > 0)
1483 if (skip != 0)
1484 skip--;
1485 else if (dstsize != 0)
1487 dstsize--;
1488 *att++ = is_inside (k, hdiff, ord);
1489 *dst++ = ' ';
1493 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1495 if (skip == 0 && show_cr)
1497 if (dstsize > 1)
1499 dstsize -= 2;
1500 *att++ = is_inside (k, hdiff, ord);
1501 *dst++ = '^';
1502 *att++ = is_inside (k, hdiff, ord);
1503 *dst++ = 'M';
1505 else
1507 dstsize--;
1508 *att++ = is_inside (k, hdiff, ord);
1509 *dst++ = '.';
1512 break;
1514 else if (skip != 0)
1516 int utf_ch = 0;
1517 gboolean res;
1518 int w;
1520 skip--;
1521 utf_ch = dview_get_utf ((char *) src, &w, &res);
1522 if (w > 1)
1523 skip += w - 1;
1524 if (!g_unichar_isprint (utf_ch))
1525 utf_ch = '.';
1527 else
1529 dstsize--;
1530 *att++ = is_inside (k, hdiff, ord);
1531 *dst++ = *src;
1534 sz = dst - tmp;
1536 while (dstsize != 0)
1538 dstsize--;
1539 *att++ = '\0';
1540 *dst++ = ' ';
1542 *dst = '\0';
1543 return sz;
1546 /* --------------------------------------------------------------------------------------------- */
1549 * Read line from file, converting tabs to spaces and padding with spaces.
1551 * @param f file stream to read from
1552 * @param off offset of line inside file
1553 * @param dst buffer to read to
1554 * @param dstsize size of dst buffer, excluding trailing null
1555 * @param skip number of characters to skip
1556 * @param ts tab size
1557 * @param show_cr show trailing carriage return as ^M
1559 * @return negative on error, otherwise number of bytes except padding
1562 static int
1563 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, int show_cr)
1565 int base = 0;
1566 int old_base = base;
1567 size_t amount = dstsize;
1568 size_t useful, offset;
1569 size_t i;
1570 size_t sz;
1571 int lastch = '\0';
1572 const char *q = NULL;
1573 char tmp[BUFSIZ]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1574 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1576 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1578 /* abnormal, but avoid buffer overflow */
1579 memset (dst, ' ', dstsize);
1580 dst[dstsize] = '\0';
1581 return 0;
1584 f_seek (f, off, SEEK_SET);
1586 while (skip > base)
1588 old_base = base;
1589 sz = f_gets (tmp, amount, f);
1590 if (sz == 0)
1591 break;
1593 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1594 if (cvt[base - old_base - 1] == '\n')
1596 q = &cvt[base - old_base - 1];
1597 base = old_base + q - cvt + 1;
1598 break;
1602 if (base < skip)
1604 memset (dst, ' ', dstsize);
1605 dst[dstsize] = '\0';
1606 return 0;
1609 useful = base - skip;
1610 offset = skip - old_base;
1612 if (useful <= dstsize)
1614 if (useful != 0)
1615 memmove (dst, cvt + offset, useful);
1617 if (q == NULL)
1619 sz = f_gets (tmp, dstsize - useful + 1, f);
1620 if (sz != 0)
1622 const char *ptr = tmp;
1624 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1625 if (ptr < tmp + sz)
1626 lastch = *ptr;
1629 sz = useful;
1631 else
1633 memmove (dst, cvt + offset, dstsize);
1634 sz = dstsize;
1635 lastch = cvt[offset + dstsize];
1638 dst[sz] = lastch;
1639 for (i = 0; i < sz && dst[i] != '\n'; i++)
1641 if (dst[i] == '\r' && dst[i + 1] == '\n')
1643 if (show_cr)
1645 if (i + 1 < dstsize)
1647 dst[i++] = '^';
1648 dst[i++] = 'M';
1650 else
1652 dst[i++] = '*';
1655 break;
1659 for (; i < dstsize; i++)
1660 dst[i] = ' ';
1661 dst[i] = '\0';
1662 return sz;
1665 /* --------------------------------------------------------------------------------------------- */
1666 /* diff printers et al ****************************************************** */
1668 static void
1669 cc_free_elt (void *elt)
1671 DIFFLN *p = elt;
1673 if (p != NULL)
1674 g_free (p->p);
1677 /* --------------------------------------------------------------------------------------------- */
1679 static int
1680 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1682 GArray *a = ((PRINTER_CTX *) ctx)->a;
1683 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1685 if (ch != 0)
1687 DIFFLN p;
1689 p.p = NULL;
1690 p.ch = ch;
1691 p.line = line;
1692 p.u.off = off;
1693 if (dsrc == DATA_SRC_MEM && line != 0)
1695 if (sz != 0 && str[sz - 1] == '\n')
1696 sz--;
1697 if (sz > 0)
1698 p.p = g_strndup (str, sz);
1699 p.u.len = sz;
1701 g_array_append_val (a, p);
1703 else if (dsrc == DATA_SRC_MEM)
1705 DIFFLN *p;
1707 p = &g_array_index (a, DIFFLN, a->len - 1);
1708 if (sz != 0 && str[sz - 1] == '\n')
1709 sz--;
1710 if (sz != 0)
1712 size_t new_size;
1713 char *q;
1715 new_size = p->u.len + sz;
1716 q = g_realloc (p->p, new_size);
1717 memcpy (q + p->u.len, str, sz);
1718 p->p = q;
1720 p->u.len += sz;
1722 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1724 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1725 f_write (f, str, sz);
1727 return 0;
1730 /* --------------------------------------------------------------------------------------------- */
1732 static int
1733 redo_diff (WDiff * dview)
1735 FBUF *const *f = dview->f;
1736 PRINTER_CTX ctx;
1737 GArray *ops;
1738 int ndiff;
1739 int rv;
1740 char extra[256];
1742 extra[0] = '\0';
1743 if (dview->opt.quality == 2)
1744 strcat (extra, " -d");
1745 if (dview->opt.quality == 1)
1746 strcat (extra, " --speed-large-files");
1747 if (dview->opt.strip_trailing_cr)
1748 strcat (extra, " --strip-trailing-cr");
1749 if (dview->opt.ignore_tab_expansion)
1750 strcat (extra, " -E");
1751 if (dview->opt.ignore_space_change)
1752 strcat (extra, " -b");
1753 if (dview->opt.ignore_all_space)
1754 strcat (extra, " -w");
1755 if (dview->opt.ignore_case)
1756 strcat (extra, " -i");
1758 if (dview->dsrc != DATA_SRC_MEM)
1760 f_reset (f[DIFF_LEFT]);
1761 f_reset (f[DIFF_RIGHT]);
1764 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1765 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1766 if (ndiff < 0)
1768 if (ops != NULL)
1769 g_array_free (ops, TRUE);
1770 return -1;
1773 ctx.dsrc = dview->dsrc;
1775 rv = 0;
1776 ctx.a = dview->a[DIFF_LEFT];
1777 ctx.f = f[DIFF_LEFT];
1778 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1780 ctx.a = dview->a[DIFF_RIGHT];
1781 ctx.f = f[DIFF_RIGHT];
1782 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1784 if (ops != NULL)
1785 g_array_free (ops, TRUE);
1787 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1788 return -1;
1790 if (dview->dsrc == DATA_SRC_TMP)
1792 f_trunc (f[DIFF_LEFT]);
1793 f_trunc (f[DIFF_RIGHT]);
1796 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1798 dview->hdiff = g_ptr_array_new ();
1799 if (dview->hdiff != NULL)
1801 size_t i;
1802 const DIFFLN *p;
1803 const DIFFLN *q;
1805 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1807 GArray *h = NULL;
1809 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1810 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1811 if (p->line && q->line && p->ch == CHG_CH)
1813 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1814 if (h != NULL)
1816 gboolean runresult;
1818 runresult =
1819 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1820 HDIFF_DEPTH);
1821 if (!runresult)
1823 g_array_free (h, TRUE);
1824 h = NULL;
1828 g_ptr_array_add (dview->hdiff, h);
1832 return ndiff;
1835 /* --------------------------------------------------------------------------------------------- */
1837 static void
1838 destroy_hdiff (WDiff * dview)
1840 if (dview->hdiff != NULL)
1842 int i;
1843 int len;
1845 len = dview->a[DIFF_LEFT]->len;
1847 for (i = 0; i < len; i++)
1849 GArray *h;
1851 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1852 if (h != NULL)
1853 g_array_free (h, TRUE);
1855 g_ptr_array_free (dview->hdiff, TRUE);
1856 dview->hdiff = NULL;
1859 mc_search_free (dview->search.handle);
1860 dview->search.handle = NULL;
1861 g_free (dview->search.last_string);
1862 dview->search.last_string = NULL;
1865 /* --------------------------------------------------------------------------------------------- */
1866 /* stuff ******************************************************************** */
1868 static int
1869 get_digits (unsigned int n)
1871 int d = 1;
1873 while (n /= 10)
1874 d++;
1875 return d;
1878 /* --------------------------------------------------------------------------------------------- */
1880 static int
1881 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1883 const DIFFLN *p;
1885 *linenum = 0;
1886 *lineofs = 0;
1888 if (a->len != 0)
1890 if (pos >= a->len)
1891 pos = a->len - 1;
1893 p = &g_array_index (a, DIFFLN, pos);
1895 if (p->line == 0)
1897 int n;
1899 for (n = pos; n > 0; n--)
1901 p--;
1902 if (p->line != 0)
1903 break;
1905 *lineofs = pos - n + 1;
1908 *linenum = p->line;
1910 return 0;
1913 /* --------------------------------------------------------------------------------------------- */
1915 static int
1916 calc_nwidth (const GArray ** const a)
1918 int l1, o1;
1919 int l2, o2;
1921 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1922 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1923 if (l1 < l2)
1924 l1 = l2;
1925 return get_digits (l1);
1928 /* --------------------------------------------------------------------------------------------- */
1930 static int
1931 find_prev_hunk (const GArray * a, int pos)
1933 #if 1
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 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1939 pos--;
1940 if (pos > 0 && (size_t) pos < a->len)
1941 pos++;
1942 #else
1943 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1944 pos--;
1945 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1946 pos--;
1947 #endif
1949 return pos;
1952 /* --------------------------------------------------------------------------------------------- */
1954 static size_t
1955 find_next_hunk (const GArray * a, size_t pos)
1957 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1958 pos++;
1959 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1960 pos++;
1961 return pos;
1964 /* --------------------------------------------------------------------------------------------- */
1966 * Find start and end lines of the current hunk.
1968 * @param dview WDiff widget
1969 * @return boolean and
1970 * start_line1 first line of current hunk (file[0])
1971 * end_line1 last line of current hunk (file[0])
1972 * start_line1 first line of current hunk (file[0])
1973 * end_line1 last line of current hunk (file[0])
1976 static int
1977 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1979 const GArray *a0 = dview->a[DIFF_LEFT];
1980 const GArray *a1 = dview->a[DIFF_RIGHT];
1981 size_t pos;
1982 int ch;
1983 int res = 0;
1985 *start_line1 = 1;
1986 *start_line2 = 1;
1987 *end_line1 = 1;
1988 *end_line2 = 1;
1990 pos = dview->skip_rows;
1991 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1992 if (ch != EQU_CH)
1994 switch (ch)
1996 case ADD_CH:
1997 res = DIFF_DEL;
1998 break;
1999 case DEL_CH:
2000 res = DIFF_ADD;
2001 break;
2002 case CHG_CH:
2003 res = DIFF_CHG;
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 res;
2188 int hunk;
2189 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2191 if (merge_direction == FROM_RIGHT_TO_LEFT)
2192 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2193 else
2194 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2196 if (hunk > 0)
2198 int merge_file_fd;
2199 FILE *merge_file;
2200 vfs_path_t *merge_file_name_vpath = NULL;
2202 if (!dview->merged[n_merge])
2204 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2205 if (!dview->merged[n_merge])
2207 message (D_ERROR, MSG_ERROR,
2208 _("Cannot create backup file\n%s%s\n%s"),
2209 dview->file[n_merge], "~~~", unix_error_string (errno));
2210 return;
2214 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2215 if (merge_file_fd == -1)
2217 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2218 unix_error_string (errno));
2219 return;
2222 merge_file = fdopen (merge_file_fd, "w");
2224 switch (hunk)
2226 case DIFF_DEL:
2227 if (merge_direction == FROM_RIGHT_TO_LEFT)
2228 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2229 else
2230 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2231 break;
2232 case DIFF_ADD:
2233 if (merge_direction == FROM_RIGHT_TO_LEFT)
2234 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2235 else
2236 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2237 break;
2238 case DIFF_CHG:
2239 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2240 break;
2242 fflush (merge_file);
2243 fclose (merge_file);
2244 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2245 mc_unlink (merge_file_name_vpath);
2246 vfs_path_free (merge_file_name_vpath);
2250 /* --------------------------------------------------------------------------------------------- */
2251 /* view routines and callbacks ********************************************** */
2253 static void
2254 dview_compute_split (WDiff * dview, int i)
2256 dview->bias += i;
2257 if (dview->bias < 2 - dview->half1)
2258 dview->bias = 2 - dview->half1;
2259 if (dview->bias > dview->half2 - 2)
2260 dview->bias = dview->half2 - 2;
2263 /* --------------------------------------------------------------------------------------------- */
2265 static void
2266 dview_compute_areas (WDiff * dview)
2268 dview->height = LINES - 2;
2269 dview->half1 = COLS / 2;
2270 dview->half2 = COLS - dview->half1;
2272 dview_compute_split (dview, 0);
2275 /* --------------------------------------------------------------------------------------------- */
2277 static void
2278 dview_reread (WDiff * dview)
2280 int ndiff;
2282 destroy_hdiff (dview);
2283 if (dview->a[DIFF_LEFT] != NULL)
2285 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2286 g_array_free (dview->a[DIFF_LEFT], TRUE);
2288 if (dview->a[DIFF_RIGHT] != NULL)
2290 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2291 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2294 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2295 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2297 ndiff = redo_diff (dview);
2298 if (ndiff >= 0)
2299 dview->ndiff = ndiff;
2302 /* --------------------------------------------------------------------------------------------- */
2304 #ifdef HAVE_CHARSET
2305 static void
2306 dview_set_codeset (WDiff * dview)
2308 const char *encoding_id = NULL;
2310 dview->utf8 = TRUE;
2311 encoding_id =
2312 get_codepage_id (mc_global.source_codepage >=
2313 0 ? mc_global.source_codepage : mc_global.display_codepage);
2314 if (encoding_id != NULL)
2316 GIConv conv;
2318 conv = str_crt_conv_from (encoding_id);
2319 if (conv != INVALID_CONV)
2321 if (dview->converter != str_cnv_from_term)
2322 str_close_conv (dview->converter);
2323 dview->converter = conv;
2325 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2329 /* --------------------------------------------------------------------------------------------- */
2331 static void
2332 dview_select_encoding (WDiff * dview)
2334 if (do_select_codepage ())
2335 dview_set_codeset (dview);
2336 dview_reread (dview);
2337 tty_touch_screen ();
2338 repaint_screen ();
2340 #endif /* HAVE_CHARSET */
2342 /* --------------------------------------------------------------------------------------------- */
2344 static void
2345 dview_diff_options (WDiff * dview)
2347 const char *quality_str[] = {
2348 N_("No&rmal"),
2349 N_("&Fastest (Assume large files)"),
2350 N_("&Minimal (Find a smaller set of change)")
2353 quick_widget_t quick_widgets[] = {
2354 /* *INDENT-OFF* */
2355 QUICK2_START_GROUPBOX (N_("Diff algorithm")),
2356 QUICK2_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2357 QUICK2_STOP_GROUPBOX,
2358 QUICK2_START_GROUPBOX (N_("Diff extra options")),
2359 QUICK2_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2360 QUICK2_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2361 QUICK2_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2362 QUICK2_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2363 QUICK2_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2364 NULL),
2365 QUICK2_STOP_GROUPBOX,
2366 QUICK2_START_BUTTONS (TRUE, TRUE),
2367 QUICK2_BUTTON (N_("&OK"), B_ENTER, NULL, NULL),
2368 QUICK2_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
2369 QUICK2_END
2370 /* *INDENT-ON* */
2373 quick_dialog_t qdlg = {
2374 -1, -1, 56,
2375 N_("Diff Options"), "[Diff Options]",
2376 quick_widgets, NULL, NULL
2379 if (quick2_dialog (&qdlg) != B_CANCEL)
2380 dview_reread (dview);
2383 /* --------------------------------------------------------------------------------------------- */
2385 static int
2386 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2387 const char *label1, const char *label2, DSRC dsrc)
2389 int ndiff;
2390 FBUF *f[DIFF_COUNT];
2392 f[DIFF_LEFT] = NULL;
2393 f[DIFF_RIGHT] = NULL;
2395 if (dsrc == DATA_SRC_TMP)
2397 f[DIFF_LEFT] = f_temp ();
2398 if (f[DIFF_LEFT] == NULL)
2399 return -1;
2401 f[DIFF_RIGHT] = f_temp ();
2402 if (f[DIFF_RIGHT] == NULL)
2404 f_close (f[DIFF_LEFT]);
2405 return -1;
2408 else if (dsrc == DATA_SRC_ORG)
2410 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2411 if (f[DIFF_LEFT] == NULL)
2412 return -1;
2414 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2415 if (f[DIFF_RIGHT] == NULL)
2417 f_close (f[DIFF_LEFT]);
2418 return -1;
2422 dview->args = args;
2423 dview->file[DIFF_LEFT] = file1;
2424 dview->file[DIFF_RIGHT] = file2;
2425 dview->label[DIFF_LEFT] = g_strdup (label1);
2426 dview->label[DIFF_RIGHT] = g_strdup (label2);
2427 dview->f[DIFF_LEFT] = f[0];
2428 dview->f[DIFF_RIGHT] = f[1];
2429 dview->merged[DIFF_LEFT] = FALSE;
2430 dview->merged[DIFF_RIGHT] = FALSE;
2431 dview->hdiff = NULL;
2432 dview->dsrc = dsrc;
2433 dview->converter = str_cnv_from_term;
2434 #ifdef HAVE_CHARSET
2435 dview_set_codeset (dview);
2436 #endif
2437 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2438 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2440 ndiff = redo_diff (dview);
2441 if (ndiff < 0)
2443 /* goto WIDGET_DESTROY stage: dview_fini() */
2444 f_close (f[DIFF_LEFT]);
2445 f_close (f[DIFF_RIGHT]);
2446 return -1;
2449 dview->ndiff = ndiff;
2451 dview->view_quit = 0;
2453 dview->bias = 0;
2454 dview->new_frame = 1;
2455 dview->skip_rows = 0;
2456 dview->skip_cols = 0;
2457 dview->display_symbols = 0;
2458 dview->display_numbers = 0;
2459 dview->show_cr = 1;
2460 dview->tab_size = 8;
2461 dview->ord = DIFF_LEFT;
2462 dview->full = 0;
2464 dview->search.handle = NULL;
2465 dview->search.last_string = NULL;
2466 dview->search.last_found_line = -1;
2467 dview->search.last_accessed_num_line = -1;
2469 dview->opt.quality = 0;
2470 dview->opt.strip_trailing_cr = 0;
2471 dview->opt.ignore_tab_expansion = 0;
2472 dview->opt.ignore_space_change = 0;
2473 dview->opt.ignore_all_space = 0;
2474 dview->opt.ignore_case = 0;
2476 dview_compute_areas (dview);
2478 return 0;
2481 /* --------------------------------------------------------------------------------------------- */
2483 static void
2484 dview_fini (WDiff * dview)
2486 if (dview->dsrc != DATA_SRC_MEM)
2488 f_close (dview->f[DIFF_RIGHT]);
2489 f_close (dview->f[DIFF_LEFT]);
2492 if (dview->converter != str_cnv_from_term)
2493 str_close_conv (dview->converter);
2495 destroy_hdiff (dview);
2496 if (dview->a[DIFF_LEFT] != NULL)
2498 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2499 g_array_free (dview->a[DIFF_LEFT], TRUE);
2500 dview->a[DIFF_LEFT] = NULL;
2502 if (dview->a[DIFF_RIGHT] != NULL)
2504 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2505 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2506 dview->a[DIFF_RIGHT] = NULL;
2509 g_free (dview->label[DIFF_LEFT]);
2510 g_free (dview->label[DIFF_RIGHT]);
2513 /* --------------------------------------------------------------------------------------------- */
2515 static int
2516 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2518 size_t i, k;
2519 int j;
2520 char buf[BUFSIZ];
2521 FBUF *f = dview->f[ord];
2522 int skip = dview->skip_cols;
2523 int display_symbols = dview->display_symbols;
2524 int display_numbers = dview->display_numbers;
2525 int show_cr = dview->show_cr;
2526 int tab_size = 8;
2527 const DIFFLN *p;
2528 int nwidth = display_numbers;
2529 int xwidth;
2531 xwidth = display_symbols + display_numbers;
2532 if (dview->tab_size > 0 && dview->tab_size < 9)
2533 tab_size = dview->tab_size;
2535 if (xwidth != 0)
2537 if (xwidth > width && display_symbols)
2539 xwidth--;
2540 display_symbols = 0;
2542 if (xwidth > width && display_numbers)
2544 xwidth = width;
2545 display_numbers = width;
2548 xwidth++;
2549 c += xwidth;
2550 width -= xwidth;
2551 if (width < 0)
2552 width = 0;
2555 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2557 /* abnormal, but avoid buffer overflow */
2558 return -1;
2561 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2563 int ch, next_ch, col;
2564 size_t cnt;
2566 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2567 ch = p->ch;
2568 tty_setcolor (NORMAL_COLOR);
2569 if (display_symbols)
2571 tty_gotoyx (r + j, c - 2);
2572 tty_print_char (ch);
2574 if (p->line != 0)
2576 if (display_numbers)
2578 tty_gotoyx (r + j, c - xwidth);
2579 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2580 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2582 if (ch == ADD_CH)
2583 tty_setcolor (DFF_ADD_COLOR);
2584 if (ch == CHG_CH)
2585 tty_setcolor (DFF_CHG_COLOR);
2586 if (f == NULL)
2588 if (i == (size_t) dview->search.last_found_line)
2589 tty_setcolor (MARKED_SELECTED_COLOR);
2590 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2592 char att[BUFSIZ];
2594 if (dview->utf8)
2595 k = dview_str_utf8_offset_to_pos (p->p, width);
2596 else
2597 k = width;
2599 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2600 g_ptr_array_index (dview->hdiff, i), ord, att);
2601 tty_gotoyx (r + j, c);
2602 col = 0;
2604 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2606 int w;
2607 gboolean ch_res;
2609 if (dview->utf8)
2611 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2612 if (w > 1)
2613 cnt += w - 1;
2614 if (!g_unichar_isprint (next_ch))
2615 next_ch = '.';
2617 else
2618 next_ch = dview_get_byte (buf + cnt, &ch_res);
2620 if (ch_res)
2622 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2623 #ifdef HAVE_CHARSET
2624 if (mc_global.utf8_display)
2626 if (!dview->utf8)
2628 next_ch =
2629 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2630 dview->converter);
2633 else if (dview->utf8)
2634 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2635 else
2636 next_ch = convert_to_display_c (next_ch);
2637 #endif
2638 tty_print_anychar (next_ch);
2639 col++;
2642 continue;
2645 if (ch == CHG_CH)
2646 tty_setcolor (DFF_CHH_COLOR);
2648 if (dview->utf8)
2649 k = dview_str_utf8_offset_to_pos (p->p, width);
2650 else
2651 k = width;
2652 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2654 else
2655 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2657 else
2659 if (display_numbers)
2661 tty_gotoyx (r + j, c - xwidth);
2662 memset (buf, ' ', display_numbers);
2663 buf[display_numbers] = '\0';
2664 tty_print_string (buf);
2666 if (ch == DEL_CH)
2667 tty_setcolor (DFF_DEL_COLOR);
2668 if (ch == CHG_CH)
2669 tty_setcolor (DFF_CHD_COLOR);
2670 memset (buf, ' ', width);
2671 buf[width] = '\0';
2673 tty_gotoyx (r + j, c);
2674 /* tty_print_nstring (buf, width); */
2675 col = 0;
2676 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2678 int w;
2679 gboolean ch_res;
2681 if (dview->utf8)
2683 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2684 if (w > 1)
2685 cnt += w - 1;
2686 if (!g_unichar_isprint (next_ch))
2687 next_ch = '.';
2689 else
2690 next_ch = dview_get_byte (buf + cnt, &ch_res);
2691 if (ch_res)
2693 #ifdef HAVE_CHARSET
2694 if (mc_global.utf8_display)
2696 if (!dview->utf8)
2698 next_ch =
2699 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2702 else if (dview->utf8)
2703 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2704 else
2705 next_ch = convert_to_display_c (next_ch);
2706 #endif
2708 tty_print_anychar (next_ch);
2709 col++;
2713 tty_setcolor (NORMAL_COLOR);
2714 k = width;
2715 if (width < xwidth - 1)
2716 k = xwidth - 1;
2717 memset (buf, ' ', k);
2718 buf[k] = '\0';
2719 for (; j < height; j++)
2721 if (xwidth != 0)
2723 tty_gotoyx (r + j, c - xwidth);
2724 /* tty_print_nstring (buf, xwidth - 1); */
2725 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2727 tty_gotoyx (r + j, c);
2728 /* tty_print_nstring (buf, width); */
2729 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2732 return 0;
2735 /* --------------------------------------------------------------------------------------------- */
2737 static void
2738 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2740 const char *buf;
2741 int filename_width;
2742 int linenum, lineofs;
2743 vfs_path_t *vpath;
2744 char *path;
2746 tty_setcolor (STATUSBAR_COLOR);
2748 tty_gotoyx (0, c);
2749 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2751 filename_width = width - 24;
2752 if (filename_width < 8)
2753 filename_width = 8;
2755 vpath = vfs_path_from_str (dview->label[ord]);
2756 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2757 vfs_path_free (vpath);
2758 buf = str_term_trim (path, filename_width);
2759 if (ord == DIFF_LEFT)
2760 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2761 buf, linenum, lineofs, dview->skip_cols);
2762 else
2763 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2764 buf, linenum, lineofs, dview->ndiff);
2765 g_free (path);
2768 /* --------------------------------------------------------------------------------------------- */
2770 static void
2771 dview_redo (WDiff * dview)
2773 if (dview->display_numbers)
2775 int old;
2777 old = dview->display_numbers;
2778 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2779 dview->new_frame = (old != dview->display_numbers);
2781 dview_reread (dview);
2784 /* --------------------------------------------------------------------------------------------- */
2786 static void
2787 dview_update (WDiff * dview)
2789 int height = dview->height;
2790 int width1;
2791 int width2;
2792 int last;
2794 last = dview->a[DIFF_LEFT]->len - 1;
2796 if (dview->skip_rows > last)
2797 dview->skip_rows = dview->search.last_accessed_num_line = last;
2798 if (dview->skip_rows < 0)
2799 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2800 if (dview->skip_cols < 0)
2801 dview->skip_cols = 0;
2803 if (height < 2)
2804 return;
2806 width1 = dview->half1 + dview->bias;
2807 width2 = dview->half2 - dview->bias;
2808 if (dview->full)
2810 width1 = COLS;
2811 width2 = 0;
2814 if (dview->new_frame)
2816 int xwidth;
2818 tty_setcolor (NORMAL_COLOR);
2819 xwidth = dview->display_symbols + dview->display_numbers;
2820 if (width1 > 1)
2821 tty_draw_box (1, 0, height, width1, FALSE);
2822 if (width2 > 1)
2823 tty_draw_box (1, width1, height, width2, FALSE);
2825 if (xwidth != 0)
2827 xwidth++;
2828 if (xwidth < width1 - 1)
2830 tty_gotoyx (1, xwidth);
2831 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2832 tty_gotoyx (height, xwidth);
2833 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2834 tty_draw_vline (2, xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2836 if (xwidth < width2 - 1)
2838 tty_gotoyx (1, width1 + xwidth);
2839 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2840 tty_gotoyx (height, width1 + xwidth);
2841 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2842 tty_draw_vline (2, width1 + xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2845 dview->new_frame = 0;
2848 if (width1 > 2)
2850 dview_status (dview, dview->ord, width1, 0);
2851 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2853 if (width2 > 2)
2855 dview_status (dview, dview->ord ^ 1, width2, width1);
2856 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2860 /* --------------------------------------------------------------------------------------------- */
2862 static void
2863 dview_edit (WDiff * dview, diff_place_t ord)
2865 Dlg_head *h;
2866 gboolean h_modal;
2867 int linenum, lineofs;
2869 if (dview->dsrc == DATA_SRC_TMP)
2871 error_dialog (_("Edit"), _("Edit is disabled"));
2872 return;
2875 h = WIDGET (dview)->owner;
2876 h_modal = h->modal;
2878 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2879 h->modal = TRUE; /* not allow edit file in several editors */
2881 vfs_path_t *tmp_vpath;
2883 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2884 do_edit_at_line (tmp_vpath, use_internal_edit, linenum);
2885 vfs_path_free (tmp_vpath);
2887 h->modal = h_modal;
2888 dview_redo (dview);
2889 dview_update (dview);
2892 /* --------------------------------------------------------------------------------------------- */
2894 static void
2895 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2897 /* *INDENT-OFF* */
2898 static const char *title[2] = {
2899 N_("Goto line (left)"),
2900 N_("Goto line (right)")
2902 /* *INDENT-ON* */
2903 static char prev[256];
2904 /* XXX some statics here, to be remembered between runs */
2906 int newline;
2907 char *input;
2909 input = input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE, prev);
2910 if (input != NULL)
2912 const char *s = input;
2914 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2916 size_t i = 0;
2918 if (newline > 0)
2920 const DIFFLN *p;
2922 for (; i < dview->a[ord]->len; i++)
2924 p = &g_array_index (dview->a[ord], DIFFLN, i);
2925 if (p->line == newline)
2926 break;
2929 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2930 g_snprintf (prev, sizeof (prev), "%d", newline);
2932 g_free (input);
2936 /* --------------------------------------------------------------------------------------------- */
2938 static void
2939 dview_labels (WDiff * dview)
2941 Widget *d;
2942 Dlg_head *h;
2943 WButtonBar *b;
2945 d = WIDGET (dview);
2946 h = d->owner;
2947 b = find_buttonbar (h);
2949 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, d);
2950 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, d);
2951 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, d);
2952 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, d);
2953 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, d);
2954 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, d);
2955 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, d);
2958 /* --------------------------------------------------------------------------------------------- */
2960 static int
2961 dview_event (Gpm_Event * event, void *data)
2963 WDiff *dview = (WDiff *) data;
2965 if (!mouse_global_in_widget (event, data))
2966 return MOU_UNHANDLED;
2968 /* We are not interested in release events */
2969 if ((event->type & (GPM_DOWN | GPM_DRAG)) == 0)
2970 return MOU_NORMAL;
2972 /* Wheel events */
2973 if ((event->buttons & GPM_B_UP) != 0 && (event->type & GPM_DOWN) != 0)
2975 dview->skip_rows -= 2;
2976 dview->search.last_accessed_num_line = dview->skip_rows;
2977 dview_update (dview);
2979 else if ((event->buttons & GPM_B_DOWN) != 0 && (event->type & GPM_DOWN) != 0)
2981 dview->skip_rows += 2;
2982 dview->search.last_accessed_num_line = dview->skip_rows;
2983 dview_update (dview);
2986 return MOU_NORMAL;
2989 /* --------------------------------------------------------------------------------------------- */
2991 static gboolean
2992 dview_save (WDiff * dview)
2994 gboolean res = TRUE;
2996 if (dview->merged[DIFF_LEFT])
2998 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2999 dview->merged[DIFF_LEFT] = !res;
3001 if (dview->merged[DIFF_RIGHT])
3003 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3004 dview->merged[DIFF_RIGHT] = !res;
3006 return res;
3009 /* --------------------------------------------------------------------------------------------- */
3011 static void
3012 dview_do_save (WDiff * dview)
3014 (void) dview_save (dview);
3017 /* --------------------------------------------------------------------------------------------- */
3019 static void
3020 dview_save_options (WDiff * dview)
3022 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
3023 dview->display_symbols != 0 ? TRUE : FALSE);
3024 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
3025 dview->display_numbers != 0 ? TRUE : FALSE);
3026 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
3028 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
3030 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
3031 dview->opt.strip_trailing_cr);
3032 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
3033 dview->opt.ignore_all_space);
3034 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
3035 dview->opt.ignore_space_change);
3036 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
3037 dview->opt.ignore_tab_expansion);
3038 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
3041 /* --------------------------------------------------------------------------------------------- */
3043 static void
3044 dview_load_options (WDiff * dview)
3046 gboolean show_numbers, show_symbols;
3047 int tab_size;
3049 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
3050 if (show_symbols)
3051 dview->display_symbols = 1;
3052 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
3053 if (show_numbers)
3054 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
3055 tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
3056 if (tab_size > 0 && tab_size < 9)
3057 dview->tab_size = tab_size;
3058 else
3059 dview->tab_size = 8;
3061 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
3063 dview->opt.strip_trailing_cr =
3064 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
3065 dview->opt.ignore_all_space =
3066 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
3067 dview->opt.ignore_space_change =
3068 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
3069 dview->opt.ignore_tab_expansion =
3070 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
3071 dview->opt.ignore_case =
3072 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
3074 dview->new_frame = 1;
3077 /* --------------------------------------------------------------------------------------------- */
3080 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3081 * ask user.
3083 static gboolean
3084 dview_ok_to_exit (WDiff * dview)
3086 gboolean res = TRUE;
3087 int act;
3089 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3090 return res;
3092 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3093 _("File(s) was modified. Save with exit?") :
3094 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3095 D_NORMAL, 2, _("&Yes"), _("&No"));
3097 /* Esc is No */
3098 if (mc_global.midnight_shutdown || (act == -1))
3099 act = 1;
3101 switch (act)
3103 case -1: /* Esc */
3104 res = FALSE;
3105 break;
3106 case 0: /* Yes */
3107 (void) dview_save (dview);
3108 res = TRUE;
3109 break;
3110 case 1: /* No */
3111 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3112 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3113 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3114 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3115 /* fall through */
3116 default:
3117 res = TRUE;
3118 break;
3120 return res;
3123 /* --------------------------------------------------------------------------------------------- */
3125 static cb_ret_t
3126 dview_execute_cmd (WDiff * dview, unsigned long command)
3128 cb_ret_t res = MSG_HANDLED;
3130 switch (command)
3132 case CK_ShowSymbols:
3133 dview->display_symbols ^= 1;
3134 dview->new_frame = 1;
3135 break;
3136 case CK_ShowNumbers:
3137 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
3138 dview->new_frame = 1;
3139 break;
3140 case CK_SplitFull:
3141 dview->full ^= 1;
3142 dview->new_frame = 1;
3143 break;
3144 case CK_SplitEqual:
3145 if (!dview->full)
3147 dview->bias = 0;
3148 dview->new_frame = 1;
3150 break;
3151 case CK_SplitMore:
3152 if (!dview->full)
3154 dview_compute_split (dview, 1);
3155 dview->new_frame = 1;
3157 break;
3159 case CK_SplitLess:
3160 if (!dview->full)
3162 dview_compute_split (dview, -1);
3163 dview->new_frame = 1;
3165 break;
3166 case CK_Tab2:
3167 dview->tab_size = 2;
3168 break;
3169 case CK_Tab3:
3170 dview->tab_size = 3;
3171 break;
3172 case CK_Tab4:
3173 dview->tab_size = 4;
3174 break;
3175 case CK_Tab8:
3176 dview->tab_size = 8;
3177 break;
3178 case CK_Swap:
3179 dview->ord ^= 1;
3180 break;
3181 case CK_Redo:
3182 dview_redo (dview);
3183 break;
3184 case CK_HunkNext:
3185 dview->skip_rows = dview->search.last_accessed_num_line =
3186 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3187 break;
3188 case CK_HunkPrev:
3189 dview->skip_rows = dview->search.last_accessed_num_line =
3190 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3191 break;
3192 case CK_Goto:
3193 dview_goto_cmd (dview, TRUE);
3194 break;
3195 case CK_Edit:
3196 dview_edit (dview, dview->ord);
3197 break;
3198 case CK_Merge:
3199 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3200 dview_redo (dview);
3201 break;
3202 case CK_MergeOther:
3203 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3204 dview_redo (dview);
3205 break;
3206 case CK_EditOther:
3207 dview_edit (dview, dview->ord ^ 1);
3208 break;
3209 case CK_Search:
3210 dview_search_cmd (dview);
3211 break;
3212 case CK_SearchContinue:
3213 dview_continue_search_cmd (dview);
3214 break;
3215 case CK_Top:
3216 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3217 break;
3218 case CK_Bottom:
3219 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3220 break;
3221 case CK_Up:
3222 if (dview->skip_rows > 0)
3224 dview->skip_rows--;
3225 dview->search.last_accessed_num_line = dview->skip_rows;
3227 break;
3228 case CK_Down:
3229 dview->skip_rows++;
3230 dview->search.last_accessed_num_line = dview->skip_rows;
3231 break;
3232 case CK_PageDown:
3233 if (dview->height > 2)
3235 dview->skip_rows += dview->height - 2;
3236 dview->search.last_accessed_num_line = dview->skip_rows;
3238 break;
3239 case CK_PageUp:
3240 if (dview->height > 2)
3242 dview->skip_rows -= dview->height - 2;
3243 dview->search.last_accessed_num_line = dview->skip_rows;
3245 break;
3246 case CK_Left:
3247 dview->skip_cols--;
3248 break;
3249 case CK_Right:
3250 dview->skip_cols++;
3251 break;
3252 case CK_LeftQuick:
3253 dview->skip_cols -= 8;
3254 break;
3255 case CK_RightQuick:
3256 dview->skip_cols += 8;
3257 break;
3258 case CK_Home:
3259 dview->skip_cols = 0;
3260 break;
3261 case CK_Shell:
3262 view_other_cmd ();
3263 break;
3264 case CK_Quit:
3265 dview->view_quit = 1;
3266 break;
3267 case CK_Save:
3268 dview_do_save (dview);
3269 break;
3270 case CK_Options:
3271 dview_diff_options (dview);
3272 break;
3273 #ifdef HAVE_CHARSET
3274 case CK_SelectCodepage:
3275 dview_select_encoding (dview);
3276 break;
3277 #endif
3278 case CK_Cancel:
3279 /* don't close diffviewer due to SIGINT */
3280 break;
3281 default:
3282 res = MSG_NOT_HANDLED;
3284 return res;
3287 /* --------------------------------------------------------------------------------------------- */
3289 static cb_ret_t
3290 dview_handle_key (WDiff * dview, int key)
3292 unsigned long command;
3294 #ifdef HAVE_CHARSET
3295 key = convert_from_input_c (key);
3296 #endif
3298 command = keybind_lookup_keymap_command (diff_map, key);
3299 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3300 return MSG_HANDLED;
3302 /* Key not used */
3303 return MSG_NOT_HANDLED;
3306 /* --------------------------------------------------------------------------------------------- */
3308 static cb_ret_t
3309 dview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3311 WDiff *dview = (WDiff *) w;
3312 Dlg_head *h = w->owner;
3313 cb_ret_t i;
3315 switch (msg)
3317 case WIDGET_INIT:
3318 dview_labels (dview);
3319 dview_load_options (dview);
3320 dview_update (dview);
3321 return MSG_HANDLED;
3323 case WIDGET_DRAW:
3324 dview->new_frame = 1;
3325 dview_update (dview);
3326 return MSG_HANDLED;
3328 case WIDGET_KEY:
3329 i = dview_handle_key (dview, parm);
3330 if (dview->view_quit)
3331 dlg_stop (h);
3332 else
3333 dview_update (dview);
3334 return i;
3336 case WIDGET_COMMAND:
3337 i = dview_execute_cmd (dview, parm);
3338 if (dview->view_quit)
3339 dlg_stop (h);
3340 else
3341 dview_update (dview);
3342 return i;
3344 case WIDGET_DESTROY:
3345 dview_save_options (dview);
3346 dview_fini (dview);
3347 return MSG_HANDLED;
3349 default:
3350 return default_widget_callback (sender, msg, parm, data);
3354 /* --------------------------------------------------------------------------------------------- */
3356 static void
3357 dview_adjust_size (Dlg_head * h)
3359 WDiff *dview;
3360 WButtonBar *bar;
3362 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3363 dview = (WDiff *) find_widget_type (h, dview_callback);
3364 bar = find_buttonbar (h);
3365 widget_set_size (WIDGET (dview), 0, 0, LINES - 1, COLS);
3366 widget_set_size (WIDGET (bar), LINES - 1, 0, 1, COLS);
3368 dview_compute_areas (dview);
3371 /* --------------------------------------------------------------------------------------------- */
3373 static cb_ret_t
3374 dview_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
3376 WDiff *dview = (WDiff *) data;
3378 switch (msg)
3380 case DLG_RESIZE:
3381 dview_adjust_size (h);
3382 return MSG_HANDLED;
3384 case DLG_ACTION:
3385 /* shortcut */
3386 if (sender == NULL)
3387 return dview_execute_cmd (NULL, parm);
3388 /* message from buttonbar */
3389 if (sender == WIDGET (find_buttonbar (h)))
3391 if (data != NULL)
3392 return send_message (WIDGET (data), NULL, WIDGET_COMMAND, parm, NULL);
3394 dview = (WDiff *) find_widget_type (h, dview_callback);
3395 return dview_execute_cmd (dview, parm);
3397 return MSG_NOT_HANDLED;
3399 case DLG_VALIDATE:
3400 dview = (WDiff *) find_widget_type (h, dview_callback);
3401 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
3402 if (dview_ok_to_exit (dview))
3403 h->state = DLG_CLOSED;
3404 return MSG_HANDLED;
3406 default:
3407 return default_dlg_callback (h, sender, msg, parm, data);
3411 /* --------------------------------------------------------------------------------------------- */
3413 static char *
3414 dview_get_title (const Dlg_head * h, size_t len)
3416 const WDiff *dview;
3417 const char *modified = " (*) ";
3418 const char *notmodified = " ";
3419 size_t len1;
3420 GString *title;
3422 dview = (const WDiff *) find_widget_type (h, dview_callback);
3423 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3425 title = g_string_sized_new (len);
3426 g_string_append (title, _("Diff:"));
3427 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3428 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3429 g_string_append (title, " | ");
3430 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3431 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3433 return g_string_free (title, FALSE);
3436 /* --------------------------------------------------------------------------------------------- */
3438 static int
3439 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3441 int error;
3442 WDiff *dview;
3443 Widget *w;
3444 Dlg_head *dview_dlg;
3446 /* Create dialog and widgets, put them on the dialog */
3447 dview_dlg =
3448 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, dview_dialog_callback, NULL,
3449 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3451 dview = g_new0 (WDiff, 1);
3452 w = WIDGET (dview);
3453 init_widget (w, 0, 0, LINES - 1, COLS, dview_callback, dview_event);
3454 widget_want_cursor (w, FALSE);
3456 add_widget (dview_dlg, dview);
3457 add_widget (dview_dlg, buttonbar_new (TRUE));
3459 dview_dlg->get_title = dview_get_title;
3461 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3463 /* Please note that if you add another widget,
3464 * you have to modify dview_adjust_size to
3465 * be aware of it
3467 if (error == 0)
3468 run_dlg (dview_dlg);
3470 if ((error != 0) || (dview_dlg->state == DLG_CLOSED))
3471 destroy_dlg (dview_dlg);
3473 return error == 0 ? 1 : 0;
3476 /*** public functions ****************************************************************************/
3477 /* --------------------------------------------------------------------------------------------- */
3479 #define GET_FILE_AND_STAMP(n) \
3480 do \
3482 use_copy##n = 0; \
3483 real_file##n = file##n; \
3484 if (!vfs_file_is_local (file##n)) \
3486 real_file##n = mc_getlocalcopy (file##n); \
3487 if (real_file##n != NULL) \
3489 use_copy##n = 1; \
3490 if (mc_stat (real_file##n, &st##n) != 0) \
3491 use_copy##n = -1; \
3495 while (0)
3497 #define UNGET_FILE(n) \
3498 do \
3500 if (use_copy##n) \
3502 int changed = 0; \
3503 if (use_copy##n > 0) \
3505 time_t mtime; \
3506 mtime = st##n.st_mtime; \
3507 if (mc_stat (real_file##n, &st##n) == 0) \
3508 changed = (mtime != st##n.st_mtime); \
3510 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3511 vfs_path_free (real_file##n); \
3514 while (0)
3516 gboolean
3517 dview_diff_cmd (const void *f0, const void *f1)
3519 int rv = 0;
3520 vfs_path_t *file0 = NULL;
3521 vfs_path_t *file1 = NULL;
3522 gboolean is_dir0 = FALSE;
3523 gboolean is_dir1 = FALSE;
3525 switch (mc_global.mc_run_mode)
3527 case MC_RUN_FULL:
3529 /* run from panels */
3530 const WPanel *panel0 = (const WPanel *) f0;
3531 const WPanel *panel1 = (const WPanel *) f1;
3533 file0 = vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, NULL);
3534 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3535 if (is_dir0)
3537 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3538 path_trunc (selection (panel0)->fname, 30));
3539 goto ret;
3542 file1 = vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, NULL);
3543 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3544 if (is_dir1)
3546 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3547 path_trunc (selection (panel1)->fname, 30));
3548 goto ret;
3550 break;
3553 case MC_RUN_DIFFVIEWER:
3555 /* run from command line */
3556 const char *p0 = (const char *) f0;
3557 const char *p1 = (const char *) f1;
3558 struct stat st;
3560 file0 = vfs_path_from_str (p0);
3561 if (mc_stat (file0, &st) == 0)
3563 is_dir0 = S_ISDIR (st.st_mode);
3564 if (is_dir0)
3566 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3567 goto ret;
3570 else
3572 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3573 path_trunc (p0, 30), unix_error_string (errno));
3574 goto ret;
3577 file1 = vfs_path_from_str (p1);
3578 if (mc_stat (file1, &st) == 0)
3580 is_dir1 = S_ISDIR (st.st_mode);
3581 if (is_dir1)
3583 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3584 goto ret;
3587 else
3589 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3590 path_trunc (p1, 30), unix_error_string (errno));
3591 goto ret;
3593 break;
3596 default:
3597 /* this should not happaned */
3598 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3599 return FALSE;
3602 if (rv == 0)
3604 rv = -1;
3605 if (file0 != NULL && file1 != NULL)
3607 int use_copy0;
3608 int use_copy1;
3609 struct stat st0;
3610 struct stat st1;
3611 vfs_path_t *real_file0;
3612 vfs_path_t *real_file1;
3614 GET_FILE_AND_STAMP (0);
3615 GET_FILE_AND_STAMP (1);
3616 if (real_file0 != NULL && real_file1 != NULL)
3618 char *real_file0_str, *real_file1_str;
3619 char *file0_str, *file1_str;
3621 real_file0_str = vfs_path_to_str (real_file0);
3622 real_file1_str = vfs_path_to_str (real_file1);
3623 file0_str = vfs_path_to_str (file0);
3624 file1_str = vfs_path_to_str (file1);
3625 rv = diff_view (real_file0_str, real_file1_str, file0_str, file1_str);
3626 g_free (real_file0_str);
3627 g_free (real_file1_str);
3628 g_free (file0_str);
3629 g_free (file1_str);
3631 UNGET_FILE (1);
3632 UNGET_FILE (0);
3636 if (rv == 0)
3637 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3639 ret:
3640 vfs_path_free (file1);
3641 vfs_path_free (file0);
3643 return (rv != 0);
3646 #undef GET_FILE_AND_STAMP
3647 #undef UNGET_FILE
3649 /* --------------------------------------------------------------------------------------------- */