2fd6f1c4803e8175702a053044d3104c1bfe3502
[midnight-commander.git] / src / diffviewer / ydiff.c
blob2fd6f1c4803e8175702a053044d3104c1bfe3502
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 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;
2241 fflush (merge_file);
2242 fclose (merge_file);
2244 int res;
2246 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2247 (void) res;
2249 mc_unlink (merge_file_name_vpath);
2250 vfs_path_free (merge_file_name_vpath);
2254 /* --------------------------------------------------------------------------------------------- */
2255 /* view routines and callbacks ********************************************** */
2257 static void
2258 dview_compute_split (WDiff * dview, int i)
2260 dview->bias += i;
2261 if (dview->bias < 2 - dview->half1)
2262 dview->bias = 2 - dview->half1;
2263 if (dview->bias > dview->half2 - 2)
2264 dview->bias = dview->half2 - 2;
2267 /* --------------------------------------------------------------------------------------------- */
2269 static void
2270 dview_compute_areas (WDiff * dview)
2272 dview->height = LINES - 2;
2273 dview->half1 = COLS / 2;
2274 dview->half2 = COLS - dview->half1;
2276 dview_compute_split (dview, 0);
2279 /* --------------------------------------------------------------------------------------------- */
2281 static void
2282 dview_reread (WDiff * dview)
2284 int ndiff;
2286 destroy_hdiff (dview);
2287 if (dview->a[DIFF_LEFT] != NULL)
2289 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2290 g_array_free (dview->a[DIFF_LEFT], TRUE);
2292 if (dview->a[DIFF_RIGHT] != NULL)
2294 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2295 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2298 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2299 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2301 ndiff = redo_diff (dview);
2302 if (ndiff >= 0)
2303 dview->ndiff = ndiff;
2306 /* --------------------------------------------------------------------------------------------- */
2308 #ifdef HAVE_CHARSET
2309 static void
2310 dview_set_codeset (WDiff * dview)
2312 const char *encoding_id = NULL;
2314 dview->utf8 = TRUE;
2315 encoding_id =
2316 get_codepage_id (mc_global.source_codepage >=
2317 0 ? mc_global.source_codepage : mc_global.display_codepage);
2318 if (encoding_id != NULL)
2320 GIConv conv;
2322 conv = str_crt_conv_from (encoding_id);
2323 if (conv != INVALID_CONV)
2325 if (dview->converter != str_cnv_from_term)
2326 str_close_conv (dview->converter);
2327 dview->converter = conv;
2329 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2333 /* --------------------------------------------------------------------------------------------- */
2335 static void
2336 dview_select_encoding (WDiff * dview)
2338 if (do_select_codepage ())
2339 dview_set_codeset (dview);
2340 dview_reread (dview);
2341 tty_touch_screen ();
2342 repaint_screen ();
2344 #endif /* HAVE_CHARSET */
2346 /* --------------------------------------------------------------------------------------------- */
2348 static void
2349 dview_diff_options (WDiff * dview)
2351 const char *quality_str[] = {
2352 N_("No&rmal"),
2353 N_("&Fastest (Assume large files)"),
2354 N_("&Minimal (Find a smaller set of change)")
2357 quick_widget_t quick_widgets[] = {
2358 /* *INDENT-OFF* */
2359 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2360 QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2361 QUICK_STOP_GROUPBOX,
2362 QUICK_START_GROUPBOX (N_("Diff extra options")),
2363 QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2364 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2365 QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2366 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2367 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2368 NULL),
2369 QUICK_STOP_GROUPBOX,
2370 QUICK_BUTTONS_OK_CANCEL,
2371 QUICK_END
2372 /* *INDENT-ON* */
2375 quick_dialog_t qdlg = {
2376 -1, -1, 56,
2377 N_("Diff Options"), "[Diff Options]",
2378 quick_widgets, NULL, NULL
2381 if (quick_dialog (&qdlg) != B_CANCEL)
2382 dview_reread (dview);
2385 /* --------------------------------------------------------------------------------------------- */
2387 static int
2388 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2389 const char *label1, const char *label2, DSRC dsrc)
2391 int ndiff;
2392 FBUF *f[DIFF_COUNT];
2394 f[DIFF_LEFT] = NULL;
2395 f[DIFF_RIGHT] = NULL;
2397 if (dsrc == DATA_SRC_TMP)
2399 f[DIFF_LEFT] = f_temp ();
2400 if (f[DIFF_LEFT] == NULL)
2401 return -1;
2403 f[DIFF_RIGHT] = f_temp ();
2404 if (f[DIFF_RIGHT] == NULL)
2406 f_close (f[DIFF_LEFT]);
2407 return -1;
2410 else if (dsrc == DATA_SRC_ORG)
2412 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2413 if (f[DIFF_LEFT] == NULL)
2414 return -1;
2416 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2417 if (f[DIFF_RIGHT] == NULL)
2419 f_close (f[DIFF_LEFT]);
2420 return -1;
2424 dview->args = args;
2425 dview->file[DIFF_LEFT] = file1;
2426 dview->file[DIFF_RIGHT] = file2;
2427 dview->label[DIFF_LEFT] = g_strdup (label1);
2428 dview->label[DIFF_RIGHT] = g_strdup (label2);
2429 dview->f[DIFF_LEFT] = f[0];
2430 dview->f[DIFF_RIGHT] = f[1];
2431 dview->merged[DIFF_LEFT] = FALSE;
2432 dview->merged[DIFF_RIGHT] = FALSE;
2433 dview->hdiff = NULL;
2434 dview->dsrc = dsrc;
2435 dview->converter = str_cnv_from_term;
2436 #ifdef HAVE_CHARSET
2437 dview_set_codeset (dview);
2438 #endif
2439 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2440 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2442 ndiff = redo_diff (dview);
2443 if (ndiff < 0)
2445 /* goto MSG_DESTROY stage: dview_fini() */
2446 f_close (f[DIFF_LEFT]);
2447 f_close (f[DIFF_RIGHT]);
2448 return -1;
2451 dview->ndiff = ndiff;
2453 dview->view_quit = 0;
2455 dview->bias = 0;
2456 dview->new_frame = 1;
2457 dview->skip_rows = 0;
2458 dview->skip_cols = 0;
2459 dview->display_symbols = 0;
2460 dview->display_numbers = 0;
2461 dview->show_cr = 1;
2462 dview->tab_size = 8;
2463 dview->ord = DIFF_LEFT;
2464 dview->full = 0;
2466 dview->search.handle = NULL;
2467 dview->search.last_string = NULL;
2468 dview->search.last_found_line = -1;
2469 dview->search.last_accessed_num_line = -1;
2471 dview->opt.quality = 0;
2472 dview->opt.strip_trailing_cr = 0;
2473 dview->opt.ignore_tab_expansion = 0;
2474 dview->opt.ignore_space_change = 0;
2475 dview->opt.ignore_all_space = 0;
2476 dview->opt.ignore_case = 0;
2478 dview_compute_areas (dview);
2480 return 0;
2483 /* --------------------------------------------------------------------------------------------- */
2485 static void
2486 dview_fini (WDiff * dview)
2488 if (dview->dsrc != DATA_SRC_MEM)
2490 f_close (dview->f[DIFF_RIGHT]);
2491 f_close (dview->f[DIFF_LEFT]);
2494 if (dview->converter != str_cnv_from_term)
2495 str_close_conv (dview->converter);
2497 destroy_hdiff (dview);
2498 if (dview->a[DIFF_LEFT] != NULL)
2500 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2501 g_array_free (dview->a[DIFF_LEFT], TRUE);
2502 dview->a[DIFF_LEFT] = NULL;
2504 if (dview->a[DIFF_RIGHT] != NULL)
2506 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2507 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2508 dview->a[DIFF_RIGHT] = NULL;
2511 g_free (dview->label[DIFF_LEFT]);
2512 g_free (dview->label[DIFF_RIGHT]);
2515 /* --------------------------------------------------------------------------------------------- */
2517 static int
2518 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2520 size_t i, k;
2521 int j;
2522 char buf[BUFSIZ];
2523 FBUF *f = dview->f[ord];
2524 int skip = dview->skip_cols;
2525 int display_symbols = dview->display_symbols;
2526 int display_numbers = dview->display_numbers;
2527 int show_cr = dview->show_cr;
2528 int tab_size = 8;
2529 const DIFFLN *p;
2530 int nwidth = display_numbers;
2531 int xwidth;
2533 xwidth = display_symbols + display_numbers;
2534 if (dview->tab_size > 0 && dview->tab_size < 9)
2535 tab_size = dview->tab_size;
2537 if (xwidth != 0)
2539 if (xwidth > width && display_symbols)
2541 xwidth--;
2542 display_symbols = 0;
2544 if (xwidth > width && display_numbers)
2546 xwidth = width;
2547 display_numbers = width;
2550 xwidth++;
2551 c += xwidth;
2552 width -= xwidth;
2553 if (width < 0)
2554 width = 0;
2557 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2559 /* abnormal, but avoid buffer overflow */
2560 return -1;
2563 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2565 int ch, next_ch, col;
2566 size_t cnt;
2568 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2569 ch = p->ch;
2570 tty_setcolor (NORMAL_COLOR);
2571 if (display_symbols)
2573 tty_gotoyx (r + j, c - 2);
2574 tty_print_char (ch);
2576 if (p->line != 0)
2578 if (display_numbers)
2580 tty_gotoyx (r + j, c - xwidth);
2581 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2582 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2584 if (ch == ADD_CH)
2585 tty_setcolor (DFF_ADD_COLOR);
2586 if (ch == CHG_CH)
2587 tty_setcolor (DFF_CHG_COLOR);
2588 if (f == NULL)
2590 if (i == (size_t) dview->search.last_found_line)
2591 tty_setcolor (MARKED_SELECTED_COLOR);
2592 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2594 char att[BUFSIZ];
2596 if (dview->utf8)
2597 k = dview_str_utf8_offset_to_pos (p->p, width);
2598 else
2599 k = width;
2601 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2602 g_ptr_array_index (dview->hdiff, i), ord, att);
2603 tty_gotoyx (r + j, c);
2604 col = 0;
2606 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2608 int w;
2609 gboolean ch_res;
2611 if (dview->utf8)
2613 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2614 if (w > 1)
2615 cnt += w - 1;
2616 if (!g_unichar_isprint (next_ch))
2617 next_ch = '.';
2619 else
2620 next_ch = dview_get_byte (buf + cnt, &ch_res);
2622 if (ch_res)
2624 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2625 #ifdef HAVE_CHARSET
2626 if (mc_global.utf8_display)
2628 if (!dview->utf8)
2630 next_ch =
2631 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2632 dview->converter);
2635 else if (dview->utf8)
2636 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2637 else
2638 next_ch = convert_to_display_c (next_ch);
2639 #endif
2640 tty_print_anychar (next_ch);
2641 col++;
2644 continue;
2647 if (ch == CHG_CH)
2648 tty_setcolor (DFF_CHH_COLOR);
2650 if (dview->utf8)
2651 k = dview_str_utf8_offset_to_pos (p->p, width);
2652 else
2653 k = width;
2654 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2656 else
2657 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2659 else
2661 if (display_numbers)
2663 tty_gotoyx (r + j, c - xwidth);
2664 memset (buf, ' ', display_numbers);
2665 buf[display_numbers] = '\0';
2666 tty_print_string (buf);
2668 if (ch == DEL_CH)
2669 tty_setcolor (DFF_DEL_COLOR);
2670 if (ch == CHG_CH)
2671 tty_setcolor (DFF_CHD_COLOR);
2672 memset (buf, ' ', width);
2673 buf[width] = '\0';
2675 tty_gotoyx (r + j, c);
2676 /* tty_print_nstring (buf, width); */
2677 col = 0;
2678 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2680 int w;
2681 gboolean ch_res;
2683 if (dview->utf8)
2685 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2686 if (w > 1)
2687 cnt += w - 1;
2688 if (!g_unichar_isprint (next_ch))
2689 next_ch = '.';
2691 else
2692 next_ch = dview_get_byte (buf + cnt, &ch_res);
2693 if (ch_res)
2695 #ifdef HAVE_CHARSET
2696 if (mc_global.utf8_display)
2698 if (!dview->utf8)
2700 next_ch =
2701 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2704 else if (dview->utf8)
2705 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2706 else
2707 next_ch = convert_to_display_c (next_ch);
2708 #endif
2710 tty_print_anychar (next_ch);
2711 col++;
2715 tty_setcolor (NORMAL_COLOR);
2716 k = width;
2717 if (width < xwidth - 1)
2718 k = xwidth - 1;
2719 memset (buf, ' ', k);
2720 buf[k] = '\0';
2721 for (; j < height; j++)
2723 if (xwidth != 0)
2725 tty_gotoyx (r + j, c - xwidth);
2726 /* tty_print_nstring (buf, xwidth - 1); */
2727 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2729 tty_gotoyx (r + j, c);
2730 /* tty_print_nstring (buf, width); */
2731 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2734 return 0;
2737 /* --------------------------------------------------------------------------------------------- */
2739 static void
2740 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2742 const char *buf;
2743 int filename_width;
2744 int linenum, lineofs;
2745 vfs_path_t *vpath;
2746 char *path;
2748 tty_setcolor (STATUSBAR_COLOR);
2750 tty_gotoyx (0, c);
2751 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2753 filename_width = width - 24;
2754 if (filename_width < 8)
2755 filename_width = 8;
2757 vpath = vfs_path_from_str (dview->label[ord]);
2758 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2759 vfs_path_free (vpath);
2760 buf = str_term_trim (path, filename_width);
2761 if (ord == DIFF_LEFT)
2762 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2763 buf, linenum, lineofs, dview->skip_cols);
2764 else
2765 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2766 buf, linenum, lineofs, dview->ndiff);
2767 g_free (path);
2770 /* --------------------------------------------------------------------------------------------- */
2772 static void
2773 dview_redo (WDiff * dview)
2775 if (dview->display_numbers)
2777 int old;
2779 old = dview->display_numbers;
2780 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2781 dview->new_frame = (old != dview->display_numbers);
2783 dview_reread (dview);
2786 /* --------------------------------------------------------------------------------------------- */
2788 static void
2789 dview_update (WDiff * dview)
2791 int height = dview->height;
2792 int width1;
2793 int width2;
2794 int last;
2796 last = dview->a[DIFF_LEFT]->len - 1;
2798 if (dview->skip_rows > last)
2799 dview->skip_rows = dview->search.last_accessed_num_line = last;
2800 if (dview->skip_rows < 0)
2801 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2802 if (dview->skip_cols < 0)
2803 dview->skip_cols = 0;
2805 if (height < 2)
2806 return;
2808 width1 = dview->half1 + dview->bias;
2809 width2 = dview->half2 - dview->bias;
2810 if (dview->full)
2812 width1 = COLS;
2813 width2 = 0;
2816 if (dview->new_frame)
2818 int xwidth;
2820 tty_setcolor (NORMAL_COLOR);
2821 xwidth = dview->display_symbols + dview->display_numbers;
2822 if (width1 > 1)
2823 tty_draw_box (1, 0, height, width1, FALSE);
2824 if (width2 > 1)
2825 tty_draw_box (1, width1, height, width2, FALSE);
2827 if (xwidth != 0)
2829 xwidth++;
2830 if (xwidth < width1 - 1)
2832 tty_gotoyx (1, xwidth);
2833 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2834 tty_gotoyx (height, xwidth);
2835 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2836 tty_draw_vline (2, xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2838 if (xwidth < width2 - 1)
2840 tty_gotoyx (1, width1 + xwidth);
2841 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2842 tty_gotoyx (height, width1 + xwidth);
2843 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2844 tty_draw_vline (2, width1 + xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2847 dview->new_frame = 0;
2850 if (width1 > 2)
2852 dview_status (dview, dview->ord, width1, 0);
2853 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2855 if (width2 > 2)
2857 dview_status (dview, dview->ord ^ 1, width2, width1);
2858 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2862 /* --------------------------------------------------------------------------------------------- */
2864 static void
2865 dview_edit (WDiff * dview, diff_place_t ord)
2867 WDialog *h;
2868 gboolean h_modal;
2869 int linenum, lineofs;
2871 if (dview->dsrc == DATA_SRC_TMP)
2873 error_dialog (_("Edit"), _("Edit is disabled"));
2874 return;
2877 h = WIDGET (dview)->owner;
2878 h_modal = h->modal;
2880 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2881 h->modal = TRUE; /* not allow edit file in several editors */
2883 vfs_path_t *tmp_vpath;
2885 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2886 do_edit_at_line (tmp_vpath, use_internal_edit, linenum);
2887 vfs_path_free (tmp_vpath);
2889 h->modal = h_modal;
2890 dview_redo (dview);
2891 dview_update (dview);
2894 /* --------------------------------------------------------------------------------------------- */
2896 static void
2897 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2899 /* *INDENT-OFF* */
2900 static const char *title[2] = {
2901 N_("Goto line (left)"),
2902 N_("Goto line (right)")
2904 /* *INDENT-ON* */
2905 static char prev[256];
2906 /* XXX some statics here, to be remembered between runs */
2908 int newline;
2909 char *input;
2911 input = input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE, prev);
2912 if (input != NULL)
2914 const char *s = input;
2916 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2918 size_t i = 0;
2920 if (newline > 0)
2922 const DIFFLN *p;
2924 for (; i < dview->a[ord]->len; i++)
2926 p = &g_array_index (dview->a[ord], DIFFLN, i);
2927 if (p->line == newline)
2928 break;
2931 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2932 g_snprintf (prev, sizeof (prev), "%d", newline);
2934 g_free (input);
2938 /* --------------------------------------------------------------------------------------------- */
2940 static void
2941 dview_labels (WDiff * dview)
2943 Widget *d;
2944 WDialog *h;
2945 WButtonBar *b;
2947 d = WIDGET (dview);
2948 h = d->owner;
2949 b = find_buttonbar (h);
2951 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, d);
2952 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, d);
2953 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, d);
2954 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, d);
2955 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, d);
2956 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, d);
2957 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, d);
2960 /* --------------------------------------------------------------------------------------------- */
2962 static int
2963 dview_event (Gpm_Event * event, void *data)
2965 WDiff *dview = (WDiff *) data;
2967 if (!mouse_global_in_widget (event, data))
2968 return MOU_UNHANDLED;
2970 /* We are not interested in release events */
2971 if ((event->type & (GPM_DOWN | GPM_DRAG)) == 0)
2972 return MOU_NORMAL;
2974 /* Wheel events */
2975 if ((event->buttons & GPM_B_UP) != 0 && (event->type & GPM_DOWN) != 0)
2977 dview->skip_rows -= 2;
2978 dview->search.last_accessed_num_line = dview->skip_rows;
2979 dview_update (dview);
2981 else if ((event->buttons & GPM_B_DOWN) != 0 && (event->type & GPM_DOWN) != 0)
2983 dview->skip_rows += 2;
2984 dview->search.last_accessed_num_line = dview->skip_rows;
2985 dview_update (dview);
2988 return MOU_NORMAL;
2991 /* --------------------------------------------------------------------------------------------- */
2993 static gboolean
2994 dview_save (WDiff * dview)
2996 gboolean res = TRUE;
2998 if (dview->merged[DIFF_LEFT])
3000 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3001 dview->merged[DIFF_LEFT] = !res;
3003 if (dview->merged[DIFF_RIGHT])
3005 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3006 dview->merged[DIFF_RIGHT] = !res;
3008 return res;
3011 /* --------------------------------------------------------------------------------------------- */
3013 static void
3014 dview_do_save (WDiff * dview)
3016 (void) dview_save (dview);
3019 /* --------------------------------------------------------------------------------------------- */
3021 static void
3022 dview_save_options (WDiff * dview)
3024 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
3025 dview->display_symbols != 0 ? TRUE : FALSE);
3026 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
3027 dview->display_numbers != 0 ? TRUE : FALSE);
3028 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
3030 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
3032 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
3033 dview->opt.strip_trailing_cr);
3034 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
3035 dview->opt.ignore_all_space);
3036 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
3037 dview->opt.ignore_space_change);
3038 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
3039 dview->opt.ignore_tab_expansion);
3040 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
3043 /* --------------------------------------------------------------------------------------------- */
3045 static void
3046 dview_load_options (WDiff * dview)
3048 gboolean show_numbers, show_symbols;
3049 int tab_size;
3051 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
3052 if (show_symbols)
3053 dview->display_symbols = 1;
3054 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
3055 if (show_numbers)
3056 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
3057 tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
3058 if (tab_size > 0 && tab_size < 9)
3059 dview->tab_size = tab_size;
3060 else
3061 dview->tab_size = 8;
3063 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
3065 dview->opt.strip_trailing_cr =
3066 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
3067 dview->opt.ignore_all_space =
3068 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
3069 dview->opt.ignore_space_change =
3070 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
3071 dview->opt.ignore_tab_expansion =
3072 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
3073 dview->opt.ignore_case =
3074 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
3076 dview->new_frame = 1;
3079 /* --------------------------------------------------------------------------------------------- */
3082 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3083 * ask user.
3085 static gboolean
3086 dview_ok_to_exit (WDiff * dview)
3088 gboolean res = TRUE;
3089 int act;
3091 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3092 return res;
3094 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3095 _("File(s) was modified. Save with exit?") :
3096 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3097 D_NORMAL, 2, _("&Yes"), _("&No"));
3099 /* Esc is No */
3100 if (mc_global.midnight_shutdown || (act == -1))
3101 act = 1;
3103 switch (act)
3105 case -1: /* Esc */
3106 res = FALSE;
3107 break;
3108 case 0: /* Yes */
3109 (void) dview_save (dview);
3110 res = TRUE;
3111 break;
3112 case 1: /* No */
3113 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3114 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3115 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3116 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3117 /* fall through */
3118 default:
3119 res = TRUE;
3120 break;
3122 return res;
3125 /* --------------------------------------------------------------------------------------------- */
3127 static cb_ret_t
3128 dview_execute_cmd (WDiff * dview, unsigned long command)
3130 cb_ret_t res = MSG_HANDLED;
3132 switch (command)
3134 case CK_ShowSymbols:
3135 dview->display_symbols ^= 1;
3136 dview->new_frame = 1;
3137 break;
3138 case CK_ShowNumbers:
3139 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
3140 dview->new_frame = 1;
3141 break;
3142 case CK_SplitFull:
3143 dview->full ^= 1;
3144 dview->new_frame = 1;
3145 break;
3146 case CK_SplitEqual:
3147 if (!dview->full)
3149 dview->bias = 0;
3150 dview->new_frame = 1;
3152 break;
3153 case CK_SplitMore:
3154 if (!dview->full)
3156 dview_compute_split (dview, 1);
3157 dview->new_frame = 1;
3159 break;
3161 case CK_SplitLess:
3162 if (!dview->full)
3164 dview_compute_split (dview, -1);
3165 dview->new_frame = 1;
3167 break;
3168 case CK_Tab2:
3169 dview->tab_size = 2;
3170 break;
3171 case CK_Tab3:
3172 dview->tab_size = 3;
3173 break;
3174 case CK_Tab4:
3175 dview->tab_size = 4;
3176 break;
3177 case CK_Tab8:
3178 dview->tab_size = 8;
3179 break;
3180 case CK_Swap:
3181 dview->ord ^= 1;
3182 break;
3183 case CK_Redo:
3184 dview_redo (dview);
3185 break;
3186 case CK_HunkNext:
3187 dview->skip_rows = dview->search.last_accessed_num_line =
3188 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3189 break;
3190 case CK_HunkPrev:
3191 dview->skip_rows = dview->search.last_accessed_num_line =
3192 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3193 break;
3194 case CK_Goto:
3195 dview_goto_cmd (dview, TRUE);
3196 break;
3197 case CK_Edit:
3198 dview_edit (dview, dview->ord);
3199 break;
3200 case CK_Merge:
3201 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3202 dview_redo (dview);
3203 break;
3204 case CK_MergeOther:
3205 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3206 dview_redo (dview);
3207 break;
3208 case CK_EditOther:
3209 dview_edit (dview, dview->ord ^ 1);
3210 break;
3211 case CK_Search:
3212 dview_search_cmd (dview);
3213 break;
3214 case CK_SearchContinue:
3215 dview_continue_search_cmd (dview);
3216 break;
3217 case CK_Top:
3218 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3219 break;
3220 case CK_Bottom:
3221 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3222 break;
3223 case CK_Up:
3224 if (dview->skip_rows > 0)
3226 dview->skip_rows--;
3227 dview->search.last_accessed_num_line = dview->skip_rows;
3229 break;
3230 case CK_Down:
3231 dview->skip_rows++;
3232 dview->search.last_accessed_num_line = dview->skip_rows;
3233 break;
3234 case CK_PageDown:
3235 if (dview->height > 2)
3237 dview->skip_rows += dview->height - 2;
3238 dview->search.last_accessed_num_line = dview->skip_rows;
3240 break;
3241 case CK_PageUp:
3242 if (dview->height > 2)
3244 dview->skip_rows -= dview->height - 2;
3245 dview->search.last_accessed_num_line = dview->skip_rows;
3247 break;
3248 case CK_Left:
3249 dview->skip_cols--;
3250 break;
3251 case CK_Right:
3252 dview->skip_cols++;
3253 break;
3254 case CK_LeftQuick:
3255 dview->skip_cols -= 8;
3256 break;
3257 case CK_RightQuick:
3258 dview->skip_cols += 8;
3259 break;
3260 case CK_Home:
3261 dview->skip_cols = 0;
3262 break;
3263 case CK_Shell:
3264 view_other_cmd ();
3265 break;
3266 case CK_Quit:
3267 dview->view_quit = 1;
3268 break;
3269 case CK_Save:
3270 dview_do_save (dview);
3271 break;
3272 case CK_Options:
3273 dview_diff_options (dview);
3274 break;
3275 #ifdef HAVE_CHARSET
3276 case CK_SelectCodepage:
3277 dview_select_encoding (dview);
3278 break;
3279 #endif
3280 case CK_Cancel:
3281 /* don't close diffviewer due to SIGINT */
3282 break;
3283 default:
3284 res = MSG_NOT_HANDLED;
3286 return res;
3289 /* --------------------------------------------------------------------------------------------- */
3291 static cb_ret_t
3292 dview_handle_key (WDiff * dview, int key)
3294 unsigned long command;
3296 #ifdef HAVE_CHARSET
3297 key = convert_from_input_c (key);
3298 #endif
3300 command = keybind_lookup_keymap_command (diff_map, key);
3301 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3302 return MSG_HANDLED;
3304 /* Key not used */
3305 return MSG_NOT_HANDLED;
3308 /* --------------------------------------------------------------------------------------------- */
3310 static cb_ret_t
3311 dview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3313 WDiff *dview = (WDiff *) w;
3314 WDialog *h = w->owner;
3315 cb_ret_t i;
3317 switch (msg)
3319 case MSG_INIT:
3320 dview_labels (dview);
3321 dview_load_options (dview);
3322 dview_update (dview);
3323 return MSG_HANDLED;
3325 case MSG_DRAW:
3326 dview->new_frame = 1;
3327 dview_update (dview);
3328 return MSG_HANDLED;
3330 case MSG_KEY:
3331 i = dview_handle_key (dview, parm);
3332 if (dview->view_quit)
3333 dlg_stop (h);
3334 else
3335 dview_update (dview);
3336 return i;
3338 case MSG_ACTION:
3339 i = dview_execute_cmd (dview, parm);
3340 if (dview->view_quit)
3341 dlg_stop (h);
3342 else
3343 dview_update (dview);
3344 return i;
3346 case MSG_DESTROY:
3347 dview_save_options (dview);
3348 dview_fini (dview);
3349 return MSG_HANDLED;
3351 default:
3352 return widget_default_callback (w, sender, msg, parm, data);
3356 /* --------------------------------------------------------------------------------------------- */
3358 static void
3359 dview_adjust_size (WDialog * h)
3361 WDiff *dview;
3362 WButtonBar *bar;
3364 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3365 dview = (WDiff *) find_widget_type (h, dview_callback);
3366 bar = find_buttonbar (h);
3367 widget_set_size (WIDGET (dview), 0, 0, LINES - 1, COLS);
3368 widget_set_size (WIDGET (bar), LINES - 1, 0, 1, COLS);
3370 dview_compute_areas (dview);
3373 /* --------------------------------------------------------------------------------------------- */
3375 static cb_ret_t
3376 dview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3378 WDiff *dview = (WDiff *) data;
3379 WDialog *h = DIALOG (w);
3381 switch (msg)
3383 case MSG_RESIZE:
3384 dview_adjust_size (h);
3385 return MSG_HANDLED;
3387 case MSG_ACTION:
3388 /* shortcut */
3389 if (sender == NULL)
3390 return dview_execute_cmd (NULL, parm);
3391 /* message from buttonbar */
3392 if (sender == WIDGET (find_buttonbar (h)))
3394 if (data != NULL)
3395 return send_message (data, NULL, MSG_ACTION, parm, NULL);
3397 dview = (WDiff *) find_widget_type (h, dview_callback);
3398 return dview_execute_cmd (dview, parm);
3400 return MSG_NOT_HANDLED;
3402 case MSG_VALIDATE:
3403 dview = (WDiff *) find_widget_type (h, dview_callback);
3404 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
3405 if (dview_ok_to_exit (dview))
3406 h->state = DLG_CLOSED;
3407 return MSG_HANDLED;
3409 default:
3410 return dlg_default_callback (w, sender, msg, parm, data);
3414 /* --------------------------------------------------------------------------------------------- */
3416 static char *
3417 dview_get_title (const WDialog * h, size_t len)
3419 const WDiff *dview;
3420 const char *modified = " (*) ";
3421 const char *notmodified = " ";
3422 size_t len1;
3423 GString *title;
3425 dview = (const WDiff *) find_widget_type (h, dview_callback);
3426 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3428 title = g_string_sized_new (len);
3429 g_string_append (title, _("Diff:"));
3430 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3431 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3432 g_string_append (title, " | ");
3433 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3434 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3436 return g_string_free (title, FALSE);
3439 /* --------------------------------------------------------------------------------------------- */
3441 static int
3442 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3444 int error;
3445 WDiff *dview;
3446 Widget *w;
3447 WDialog *dview_dlg;
3449 /* Create dialog and widgets, put them on the dialog */
3450 dview_dlg =
3451 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, dview_dialog_callback, NULL,
3452 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3454 dview = g_new0 (WDiff, 1);
3455 w = WIDGET (dview);
3456 init_widget (w, 0, 0, LINES - 1, COLS, dview_callback, dview_event);
3457 widget_want_cursor (w, FALSE);
3459 add_widget (dview_dlg, dview);
3460 add_widget (dview_dlg, buttonbar_new (TRUE));
3462 dview_dlg->get_title = dview_get_title;
3464 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3466 /* Please note that if you add another widget,
3467 * you have to modify dview_adjust_size to
3468 * be aware of it
3470 if (error == 0)
3471 run_dlg (dview_dlg);
3473 if ((error != 0) || (dview_dlg->state == DLG_CLOSED))
3474 destroy_dlg (dview_dlg);
3476 return error == 0 ? 1 : 0;
3479 /*** public functions ****************************************************************************/
3480 /* --------------------------------------------------------------------------------------------- */
3482 #define GET_FILE_AND_STAMP(n) \
3483 do \
3485 use_copy##n = 0; \
3486 real_file##n = file##n; \
3487 if (!vfs_file_is_local (file##n)) \
3489 real_file##n = mc_getlocalcopy (file##n); \
3490 if (real_file##n != NULL) \
3492 use_copy##n = 1; \
3493 if (mc_stat (real_file##n, &st##n) != 0) \
3494 use_copy##n = -1; \
3498 while (0)
3500 #define UNGET_FILE(n) \
3501 do \
3503 if (use_copy##n) \
3505 int changed = 0; \
3506 if (use_copy##n > 0) \
3508 time_t mtime; \
3509 mtime = st##n.st_mtime; \
3510 if (mc_stat (real_file##n, &st##n) == 0) \
3511 changed = (mtime != st##n.st_mtime); \
3513 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3514 vfs_path_free (real_file##n); \
3517 while (0)
3519 gboolean
3520 dview_diff_cmd (const void *f0, const void *f1)
3522 int rv = 0;
3523 vfs_path_t *file0 = NULL;
3524 vfs_path_t *file1 = NULL;
3525 gboolean is_dir0 = FALSE;
3526 gboolean is_dir1 = FALSE;
3528 switch (mc_global.mc_run_mode)
3530 case MC_RUN_FULL:
3532 /* run from panels */
3533 const WPanel *panel0 = (const WPanel *) f0;
3534 const WPanel *panel1 = (const WPanel *) f1;
3536 file0 = vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, NULL);
3537 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3538 if (is_dir0)
3540 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3541 path_trunc (selection (panel0)->fname, 30));
3542 goto ret;
3545 file1 = vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, NULL);
3546 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3547 if (is_dir1)
3549 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3550 path_trunc (selection (panel1)->fname, 30));
3551 goto ret;
3553 break;
3556 case MC_RUN_DIFFVIEWER:
3558 /* run from command line */
3559 const char *p0 = (const char *) f0;
3560 const char *p1 = (const char *) f1;
3561 struct stat st;
3563 file0 = vfs_path_from_str (p0);
3564 if (mc_stat (file0, &st) == 0)
3566 is_dir0 = S_ISDIR (st.st_mode);
3567 if (is_dir0)
3569 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3570 goto ret;
3573 else
3575 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3576 path_trunc (p0, 30), unix_error_string (errno));
3577 goto ret;
3580 file1 = vfs_path_from_str (p1);
3581 if (mc_stat (file1, &st) == 0)
3583 is_dir1 = S_ISDIR (st.st_mode);
3584 if (is_dir1)
3586 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3587 goto ret;
3590 else
3592 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3593 path_trunc (p1, 30), unix_error_string (errno));
3594 goto ret;
3596 break;
3599 default:
3600 /* this should not happaned */
3601 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3602 return FALSE;
3605 if (rv == 0)
3607 rv = -1;
3608 if (file0 != NULL && file1 != NULL)
3610 int use_copy0;
3611 int use_copy1;
3612 struct stat st0;
3613 struct stat st1;
3614 vfs_path_t *real_file0;
3615 vfs_path_t *real_file1;
3617 GET_FILE_AND_STAMP (0);
3618 GET_FILE_AND_STAMP (1);
3619 if (real_file0 != NULL && real_file1 != NULL)
3621 char *real_file0_str, *real_file1_str;
3622 char *file0_str, *file1_str;
3624 real_file0_str = vfs_path_to_str (real_file0);
3625 real_file1_str = vfs_path_to_str (real_file1);
3626 file0_str = vfs_path_to_str (file0);
3627 file1_str = vfs_path_to_str (file1);
3628 rv = diff_view (real_file0_str, real_file1_str, file0_str, file1_str);
3629 g_free (real_file0_str);
3630 g_free (real_file1_str);
3631 g_free (file0_str);
3632 g_free (file1_str);
3634 UNGET_FILE (1);
3635 UNGET_FILE (0);
3639 if (rv == 0)
3640 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3642 ret:
3643 vfs_path_free (file1);
3644 vfs_path_free (file0);
3646 return (rv != 0);
3649 #undef GET_FILE_AND_STAMP
3650 #undef UNGET_FILE
3652 /* --------------------------------------------------------------------------------------------- */