mcdiffview: code cleanup and cosmetics.
[midnight-commander.git] / src / diffviewer / ydiff.c
blobac7b2574f5212ca4847ed3d94ab1aba05fb3d74d
1 /*
2 Copyright (C) 2007, 2010, 2011
3 The Free Software Foundation, Inc.
5 Written by:
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010
8 Andrew Borodin <aborodin@vmail.ru>, 2010
9 Ilia Maslakov <il.smind@gmail.com>, 2010
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
37 #include "lib/global.h"
38 #include "lib/tty/tty.h"
39 #include "lib/tty/color.h"
40 #include "lib/tty/key.h"
41 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
42 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
43 #include "lib/util.h"
44 #include "lib/widget.h"
45 #include "lib/strutil.h"
46 #ifdef HAVE_CHARSET
47 #include "lib/charsets.h"
48 #endif
49 #include "lib/event.h" /* mc_event_raise() */
51 #include "src/filemanager/cmd.h" /* do_edit_at_line(), view_other_cmd() */
52 #include "src/filemanager/panel.h"
53 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
55 #include "src/keybind-defaults.h"
56 #include "src/history.h"
57 #ifdef HAVE_CHARSET
58 #include "src/selcodepage.h"
59 #endif
61 #include "ydiff.h"
62 #include "internal.h"
64 /*** global variables ****************************************************************************/
66 /*** file scope macro definitions ****************************************************************/
68 #define g_array_foreach(a, TP, cbf) \
69 do { \
70 size_t g_array_foreach_i;\
71 TP *g_array_foreach_var = NULL; \
72 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
73 { \
74 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
75 (*cbf) (g_array_foreach_var); \
76 } \
77 } while (0)
79 #define FILE_READ_BUF 4096
80 #define FILE_FLAG_TEMP (1 << 0)
82 #define OPTX 56
83 #define OPTY 17
85 #define ADD_CH '+'
86 #define DEL_CH '-'
87 #define CHG_CH '*'
88 #define EQU_CH ' '
90 #define HDIFF_ENABLE 1
91 #define HDIFF_MINCTX 5
92 #define HDIFF_DEPTH 10
94 #define FILE_DIRTY(fs) \
95 do \
96 { \
97 (fs)->pos = 0; \
98 (fs)->len = 0; \
99 } \
100 while (0)
102 /*** file scope type declarations ****************************************************************/
104 typedef enum
106 FROM_LEFT_TO_RIGHT,
107 FROM_RIGHT_TO_LEFT
108 } action_direction_t;
110 /*** file scope variables ************************************************************************/
112 /*** file scope functions ************************************************************************/
113 /* --------------------------------------------------------------------------------------------- */
115 static inline int
116 TAB_SKIP (int ts, int pos)
118 if (ts > 0 && ts < 9)
119 return ts - pos % ts;
120 else
121 return 8 - pos % 8;
124 /* --------------------------------------------------------------------------------------------- */
126 static gboolean
127 rewrite_backup_content (const vfs_path_t * from_file_name_vpath, const char *to_file_name)
129 FILE *backup_fd;
130 char *contents;
131 gsize length;
132 const char *from_file_name;
134 from_file_name = vfs_path_get_by_index (from_file_name_vpath, -1)->path;
135 if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
136 return FALSE;
138 backup_fd = fopen (to_file_name, "w");
139 if (backup_fd == NULL)
141 g_free (contents);
142 return FALSE;
145 length = fwrite ((const void *) contents, length, 1, backup_fd);
147 fflush (backup_fd);
148 fclose (backup_fd);
149 g_free (contents);
150 return TRUE;
153 /* buffered I/O ************************************************************* */
156 * Try to open a temporary file.
157 * @note the name is not altered if this function fails
159 * @param[out] name address of a pointer to store the temporary name
160 * @returns file descriptor on success, negative on error
163 static int
164 open_temp (void **name)
166 int fd;
167 vfs_path_t *diff_file_name = NULL;
169 fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
170 if (fd == -1)
172 message (D_ERROR, MSG_ERROR,
173 _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
174 return -1;
176 *name = vfs_path_to_str (diff_file_name);
177 vfs_path_free (diff_file_name);
178 return fd;
181 /* --------------------------------------------------------------------------------------------- */
184 * Alocate file structure and associate file descriptor to it.
186 * @param fd file descriptor
187 * @returns file structure
190 static FBUF *
191 f_dopen (int fd)
193 FBUF *fs;
195 if (fd < 0)
196 return NULL;
198 fs = g_try_malloc (sizeof (FBUF));
199 if (fs == NULL)
200 return NULL;
202 fs->buf = g_try_malloc (FILE_READ_BUF);
203 if (fs->buf == NULL)
205 g_free (fs);
206 return NULL;
209 fs->fd = fd;
210 FILE_DIRTY (fs);
211 fs->flags = 0;
212 fs->data = NULL;
214 return fs;
217 /* --------------------------------------------------------------------------------------------- */
220 * Free file structure without closing the file.
222 * @param fs file structure
223 * @returns 0 on success, non-zero on error
226 static int
227 f_free (FBUF * fs)
229 int rv = 0;
231 if (fs->flags & FILE_FLAG_TEMP)
233 rv = unlink (fs->data);
234 g_free (fs->data);
236 g_free (fs->buf);
237 g_free (fs);
238 return rv;
241 /* --------------------------------------------------------------------------------------------- */
244 * Open a binary temporary file in R/W mode.
245 * @note the file will be deleted when closed
247 * @returns file structure
250 static FBUF *
251 f_temp (void)
253 int fd;
254 FBUF *fs;
256 fs = f_dopen (0);
257 if (fs == NULL)
258 return NULL;
260 fd = open_temp (&fs->data);
261 if (fd < 0)
263 f_free (fs);
264 return NULL;
267 fs->fd = fd;
268 fs->flags = FILE_FLAG_TEMP;
269 return fs;
272 /* --------------------------------------------------------------------------------------------- */
275 * Open a binary file in specified mode.
277 * @param filename file name
278 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
280 * @returns file structure
283 static FBUF *
284 f_open (const char *filename, int flags)
286 int fd;
287 FBUF *fs;
289 fs = f_dopen (0);
290 if (fs == NULL)
291 return NULL;
293 fd = open (filename, flags);
294 if (fd < 0)
296 f_free (fs);
297 return NULL;
300 fs->fd = fd;
301 return fs;
304 /* --------------------------------------------------------------------------------------------- */
307 * Read a line of bytes from file until newline or EOF.
308 * @note does not stop on null-byte
309 * @note buf will not be null-terminated
311 * @param buf destination buffer
312 * @param size size of buffer
313 * @param fs file structure
315 * @returns number of bytes read
319 static size_t
320 f_gets (char *buf, size_t size, FBUF * fs)
322 size_t j = 0;
326 int i;
327 int stop = 0;
329 for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
331 buf[j] = fs->buf[i];
332 if (buf[j] == '\n')
333 stop = 1;
335 fs->pos = i;
337 if (j == size || stop)
338 break;
340 fs->pos = 0;
341 fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
343 while (fs->len > 0);
345 return j;
348 /* --------------------------------------------------------------------------------------------- */
351 * Seek into file.
352 * @note avoids thrashing read cache when possible
354 * @param fs file structure
355 * @param off offset
356 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
358 * @returns position in file, starting from begginning
362 static off_t
363 f_seek (FBUF * fs, off_t off, int whence)
365 off_t rv;
367 if (fs->len && whence != SEEK_END)
369 rv = lseek (fs->fd, 0, SEEK_CUR);
370 if (rv != -1)
372 if (whence == SEEK_CUR)
374 whence = SEEK_SET;
375 off += rv - fs->len + fs->pos;
377 if (off - rv >= -fs->len && off - rv <= 0)
379 fs->pos = fs->len + off - rv;
380 return off;
385 rv = lseek (fs->fd, off, whence);
386 if (rv != -1)
387 FILE_DIRTY (fs);
388 return rv;
391 /* --------------------------------------------------------------------------------------------- */
394 * Seek to the beginning of file, thrashing read cache.
396 * @param fs file structure
398 * @returns 0 if success, non-zero on error
401 static off_t
402 f_reset (FBUF * fs)
404 off_t rv;
406 rv = lseek (fs->fd, 0, SEEK_SET);
407 if (rv != -1)
408 FILE_DIRTY (fs);
409 return rv;
412 /* --------------------------------------------------------------------------------------------- */
415 * Write bytes to file.
416 * @note thrashes read cache
418 * @param fs file structure
419 * @param buf source buffer
420 * @param size size of buffer
422 * @returns number of written bytes, -1 on error
426 static ssize_t
427 f_write (FBUF * fs, const char *buf, size_t size)
429 ssize_t rv;
431 rv = write (fs->fd, buf, size);
432 if (rv >= 0)
433 FILE_DIRTY (fs);
434 return rv;
437 /* --------------------------------------------------------------------------------------------- */
440 * Truncate file to the current position.
441 * @note thrashes read cache
443 * @param fs file structure
445 * @returns current file size on success, negative on error
449 static off_t
450 f_trunc (FBUF * fs)
452 off_t off;
454 off = lseek (fs->fd, 0, SEEK_CUR);
455 if (off != -1)
457 int rv;
459 rv = ftruncate (fs->fd, off);
460 if (rv != 0)
461 off = -1;
462 else
463 FILE_DIRTY (fs);
465 return off;
468 /* --------------------------------------------------------------------------------------------- */
471 * Close file.
472 * @note if this is temporary file, it is deleted
474 * @param fs file structure
475 * @returns 0 on success, non-zero on error
479 static int
480 f_close (FBUF * fs)
482 int rv;
484 rv = close (fs->fd);
485 f_free (fs);
486 return rv;
489 /* --------------------------------------------------------------------------------------------- */
492 * Create pipe stream to process.
494 * @param cmd shell command line
495 * @param flags open mode, either O_RDONLY or O_WRONLY
497 * @returns file structure
500 static FBUF *
501 p_open (const char *cmd, int flags)
503 FILE *f;
504 FBUF *fs;
505 const char *type = NULL;
507 if (flags == O_RDONLY)
508 type = "r";
509 else if (flags == O_WRONLY)
510 type = "w";
512 if (type == NULL)
513 return NULL;
515 fs = f_dopen (0);
516 if (fs == NULL)
517 return NULL;
519 f = popen (cmd, type);
520 if (f == NULL)
522 f_free (fs);
523 return NULL;
526 fs->fd = fileno (f);
527 fs->data = f;
528 return fs;
531 /* --------------------------------------------------------------------------------------------- */
534 * Close pipe stream.
536 * @param fs structure
537 * @returns 0 on success, non-zero on error
540 static int
541 p_close (FBUF * fs)
543 int rv;
545 rv = pclose (fs->data);
546 f_free (fs);
547 return rv;
550 /* --------------------------------------------------------------------------------------------- */
552 * Get one char (byte) from string
554 * @param char * str, gboolean * result
555 * @returns int as character or 0 and result == FALSE if fail
558 static int
559 dview_get_byte (char *str, gboolean * result)
561 if (str == NULL)
563 *result = FALSE;
564 return 0;
566 *result = TRUE;
567 return (unsigned char) *str;
570 /* --------------------------------------------------------------------------------------------- */
573 * Get utf multibyte char from string
575 * @param char * str, int * char_width, gboolean * result
576 * @returns int as utf character or 0 and result == FALSE if fail
580 static int
581 dview_get_utf (char *str, int *char_width, gboolean * result)
583 int res = -1;
584 gunichar ch;
585 gchar *next_ch = NULL;
586 int width = 0;
588 *result = TRUE;
590 if (str == NULL)
592 *result = FALSE;
593 return 0;
596 res = g_utf8_get_char_validated (str, -1);
598 if (res < 0)
599 ch = *str;
600 else
602 ch = res;
603 /* Calculate UTF-8 char width */
604 next_ch = g_utf8_next_char (str);
605 if (next_ch != NULL)
606 width = next_ch - str;
607 else
608 ch = 0;
610 *char_width = width;
611 return ch;
614 /* --------------------------------------------------------------------------------------------- */
616 static int
617 dview_str_utf8_offset_to_pos (const char *text, size_t length)
619 ptrdiff_t result;
621 if (text == NULL || text[0] == '\0')
622 return length;
624 if (g_utf8_validate (text, -1, NULL))
625 result = g_utf8_offset_to_pointer (text, length) - text;
626 else
628 gunichar uni;
629 char *tmpbuf, *buffer;
631 buffer = tmpbuf = g_strdup (text);
632 while (tmpbuf[0] != '\0')
634 uni = g_utf8_get_char_validated (tmpbuf, -1);
635 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
636 tmpbuf = g_utf8_next_char (tmpbuf);
637 else
639 tmpbuf[0] = '.';
640 tmpbuf++;
643 result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
644 g_free (buffer);
646 return max (length, (size_t) result);
649 /* --------------------------------------------------------------------------------------------- */
651 /* diff parse *************************************************************** */
654 * Read decimal number from string.
656 * @param[in,out] str string to parse
657 * @param[out] n extracted number
658 * @returns 0 if success, otherwise non-zero
661 static int
662 scan_deci (const char **str, int *n)
664 const char *p = *str;
665 char *q;
667 errno = 0;
668 *n = strtol (p, &q, 10);
669 if (errno != 0 || p == q)
670 return -1;
671 *str = q;
672 return 0;
675 /* --------------------------------------------------------------------------------------------- */
678 * Parse line for diff statement.
680 * @param p string to parse
681 * @param ops list of diff statements
682 * @returns 0 if success, otherwise non-zero
685 static int
686 scan_line (const char *p, GArray * ops)
688 DIFFCMD op;
690 int f1, f2;
691 int t1, t2;
692 int cmd;
693 int range;
695 /* handle the following cases:
696 * NUMaNUM[,NUM]
697 * NUM[,NUM]cNUM[,NUM]
698 * NUM[,NUM]dNUM
699 * where NUM is a positive integer
702 if (scan_deci (&p, &f1) != 0 || f1 < 0)
703 return -1;
705 f2 = f1;
706 range = 0;
707 if (*p == ',')
709 p++;
710 if (scan_deci (&p, &f2) != 0 || f2 < f1)
711 return -1;
713 range = 1;
716 cmd = *p++;
717 if (cmd == 'a')
719 if (range != 0)
720 return -1;
722 else if (cmd != 'c' && cmd != 'd')
723 return -1;
725 if (scan_deci (&p, &t1) != 0 || t1 < 0)
726 return -1;
728 t2 = t1;
729 range = 0;
730 if (*p == ',')
732 p++;
733 if (scan_deci (&p, &t2) != 0 || t2 < t1)
734 return -1;
736 range = 1;
739 if (cmd == 'd' && range != 0)
740 return -1;
742 op.a[0][0] = f1;
743 op.a[0][1] = f2;
744 op.cmd = cmd;
745 op.a[1][0] = t1;
746 op.a[1][1] = t2;
747 g_array_append_val (ops, op);
748 return 0;
751 /* --------------------------------------------------------------------------------------------- */
754 * Parse diff output and extract diff statements.
756 * @param f stream to read from
757 * @param ops list of diff statements to fill
758 * @returns positive number indicating number of hunks, otherwise negative
761 static int
762 scan_diff (FBUF * f, GArray * ops)
764 int sz;
765 char buf[BUFSIZ];
767 while ((sz = f_gets (buf, sizeof (buf) - 1, f)) != 0)
769 if (isdigit (buf[0]))
771 if (buf[sz - 1] != '\n')
772 return -1;
774 buf[sz] = '\0';
775 if (scan_line (buf, ops) != 0)
776 return -1;
778 continue;
781 while (buf[sz - 1] != '\n' && (sz = f_gets (buf, sizeof (buf), f)) != 0)
785 return ops->len;
788 /* --------------------------------------------------------------------------------------------- */
791 * Invoke diff and extract diff statements.
793 * @param args extra arguments to be passed to diff
794 * @param extra more arguments to be passed to diff
795 * @param file1 first file to compare
796 * @param file2 second file to compare
797 * @param ops list of diff statements to fill
799 * @returns positive number indicating number of hunks, otherwise negative
802 static int
803 dff_execute (const char *args, const char *extra, const char *file1, const char *file2,
804 GArray * ops)
806 static const char *opt =
807 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
808 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
809 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
810 " --unchanged-group-format=''";
812 int rv;
813 FBUF *f;
814 char *cmd;
815 int code;
817 cmd = g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args, extra, opt, file1, file2);
818 if (cmd == NULL)
819 return -1;
821 f = p_open (cmd, O_RDONLY);
822 g_free (cmd);
824 if (f == NULL)
825 return -1;
827 rv = scan_diff (f, ops);
828 code = p_close (f);
830 if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
831 rv = -1;
833 return rv;
836 /* --------------------------------------------------------------------------------------------- */
839 * Reparse and display file according to diff statements.
841 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
842 * @param filename file name to display
843 * @param ops list of diff statements
844 * @param printer printf-like function to be used for displaying
845 * @param ctx printer context
847 * @returns 0 if success, otherwise non-zero
850 static int
851 dff_reparse (diff_place_t ord, const char *filename, const GArray * ops, DFUNC printer, void *ctx)
853 size_t i;
854 FBUF *f;
855 size_t sz;
856 char buf[BUFSIZ];
857 int line = 0;
858 off_t off = 0;
859 const DIFFCMD *op;
860 diff_place_t eff;
861 int add_cmd;
862 int del_cmd;
864 f = f_open (filename, O_RDONLY);
865 if (f == NULL)
866 return -1;
868 ord &= 1;
869 eff = ord;
871 add_cmd = 'a';
872 del_cmd = 'd';
873 if (ord != 0)
875 add_cmd = 'd';
876 del_cmd = 'a';
878 #define F1 a[eff][0]
879 #define F2 a[eff][1]
880 #define T1 a[ ord^1 ][0]
881 #define T2 a[ ord^1 ][1]
882 for (i = 0; i < ops->len; i++)
884 int n;
886 op = &g_array_index (ops, DIFFCMD, i);
887 n = op->F1 - (op->cmd != add_cmd);
889 while (line < n && (sz = f_gets (buf, sizeof (buf), f)) != 0)
891 line++;
892 printer (ctx, EQU_CH, line, off, sz, buf);
893 off += sz;
894 while (buf[sz - 1] != '\n')
896 sz = f_gets (buf, sizeof (buf), f);
897 if (sz == 0)
899 printer (ctx, 0, 0, 0, 1, "\n");
900 break;
902 printer (ctx, 0, 0, 0, sz, buf);
903 off += sz;
907 if (line != n)
908 goto err;
910 if (op->cmd == add_cmd)
912 n = op->T2 - op->T1 + 1;
913 while (n != 0)
915 printer (ctx, DEL_CH, 0, 0, 1, "\n");
916 n--;
920 if (op->cmd == del_cmd)
922 n = op->F2 - op->F1 + 1;
923 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
925 line++;
926 printer (ctx, ADD_CH, line, off, sz, buf);
927 off += sz;
928 while (buf[sz - 1] != '\n')
930 sz = f_gets (buf, sizeof (buf), f);
931 if (sz == 0)
933 printer (ctx, 0, 0, 0, 1, "\n");
934 break;
936 printer (ctx, 0, 0, 0, sz, buf);
937 off += sz;
939 n--;
942 if (n != 0)
943 goto err;
946 if (op->cmd == 'c')
948 n = op->F2 - op->F1 + 1;
949 while (n != 0 && (sz = f_gets (buf, sizeof (buf), f)) != 0)
951 line++;
952 printer (ctx, CHG_CH, line, off, sz, buf);
953 off += sz;
954 while (buf[sz - 1] != '\n')
956 sz = f_gets (buf, sizeof (buf), f);
957 if (sz == 0)
959 printer (ctx, 0, 0, 0, 1, "\n");
960 break;
962 printer (ctx, 0, 0, 0, sz, buf);
963 off += sz;
965 n--;
968 if (n != 0)
969 goto err;
971 n = op->T2 - op->T1 - (op->F2 - op->F1);
972 while (n > 0)
974 printer (ctx, CHG_CH, 0, 0, 1, "\n");
975 n--;
979 #undef T2
980 #undef T1
981 #undef F2
982 #undef F1
984 while ((sz = f_gets (buf, sizeof (buf), f)) != 0)
986 line++;
987 printer (ctx, EQU_CH, line, off, sz, buf);
988 off += sz;
989 while (buf[sz - 1] != '\n')
991 sz = f_gets (buf, sizeof (buf), f);
992 if (sz == 0)
994 printer (ctx, 0, 0, 0, 1, "\n");
995 break;
997 printer (ctx, 0, 0, 0, sz, buf);
998 off += sz;
1002 f_close (f);
1003 return 0;
1005 err:
1006 f_close (f);
1007 return -1;
1010 /* --------------------------------------------------------------------------------------------- */
1012 /* horizontal diff ********************************************************** */
1015 * Longest common substring.
1017 * @param s first string
1018 * @param m length of first string
1019 * @param t second string
1020 * @param n length of second string
1021 * @param ret list of offsets for longest common substrings inside each string
1022 * @param min minimum length of common substrings
1024 * @returns 0 if success, nonzero otherwise
1027 static int
1028 lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
1030 int i, j;
1031 int *Lprev, *Lcurr;
1032 int z = 0;
1034 if (m < min || n < min)
1036 /* XXX early culling */
1037 return 0;
1040 Lprev = g_try_new0 (int, n + 1);
1041 if (Lprev == NULL)
1042 return -1;
1044 Lcurr = g_try_new0 (int, n + 1);
1045 if (Lcurr == NULL)
1047 g_free (Lprev);
1048 return -1;
1051 for (i = 0; i < m; i++)
1053 int *L;
1055 L = Lprev;
1056 Lprev = Lcurr;
1057 Lcurr = L;
1058 #ifdef USE_MEMSET_IN_LCS
1059 memset (Lcurr, 0, (n + 1) * sizeof (int));
1060 #endif
1061 for (j = 0; j < n; j++)
1063 #ifndef USE_MEMSET_IN_LCS
1064 Lcurr[j + 1] = 0;
1065 #endif
1066 if (s[i] == t[j])
1068 int v;
1070 v = Lprev[j] + 1;
1071 Lcurr[j + 1] = v;
1072 if (z < v)
1074 z = v;
1075 g_array_set_size (ret, 0);
1077 if (z == v && z >= min)
1079 int off0, off1;
1080 size_t k;
1082 off0 = i - z + 1;
1083 off1 = j - z + 1;
1085 for (k = 0; k < ret->len; k++)
1087 PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1088 if ((*p)[0] == off0 || (*p)[1] >= off1)
1089 break;
1091 if (k == ret->len)
1093 PAIR p2;
1095 p2[0] = off0;
1096 p2[1] = off1;
1097 g_array_append_val (ret, p2);
1104 free (Lcurr);
1105 free (Lprev);
1106 return z;
1109 /* --------------------------------------------------------------------------------------------- */
1112 * Scan recursively for common substrings and build ranges.
1114 * @param s first string
1115 * @param t second string
1116 * @param bracket current limits for both of the strings
1117 * @param min minimum length of common substrings
1118 * @param hdiff list of horizontal diff ranges to fill
1119 * @param depth recursion depth
1121 * @returns 0 if success, nonzero otherwise
1124 static gboolean
1125 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray * hdiff,
1126 unsigned int depth)
1128 BRACKET p;
1130 if (depth-- != 0)
1132 GArray *ret;
1133 BRACKET b;
1134 int len;
1136 ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1137 if (ret == NULL)
1138 return FALSE;
1140 len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1141 t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1142 if (ret->len != 0)
1144 size_t k = 0;
1145 const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1146 const PAIR *data2;
1148 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1149 b[DIFF_LEFT].len = (*data)[0];
1150 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1151 b[DIFF_RIGHT].len = (*data)[1];
1152 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1153 return FALSE;
1155 for (k = 0; k < ret->len - 1; k++)
1157 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1158 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1159 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1160 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1161 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1162 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1163 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1164 return FALSE;
1166 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1167 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1168 b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1169 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1170 b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1171 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1172 return FALSE;
1174 g_array_free (ret, TRUE);
1175 return TRUE;
1179 p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1180 p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1181 p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1182 p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1183 g_array_append_val (hdiff, p);
1185 return TRUE;
1188 /* --------------------------------------------------------------------------------------------- */
1191 * Build list of horizontal diff ranges.
1193 * @param s first string
1194 * @param m length of first string
1195 * @param t second string
1196 * @param n length of second string
1197 * @param min minimum length of common substrings
1198 * @param hdiff list of horizontal diff ranges to fill
1199 * @param depth recursion depth
1201 * @returns 0 if success, nonzero otherwise
1204 static gboolean
1205 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray * hdiff, unsigned int depth)
1207 int i;
1208 BRACKET b;
1210 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1211 for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1213 for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1216 b[DIFF_LEFT].off = i;
1217 b[DIFF_LEFT].len = m - i;
1218 b[DIFF_RIGHT].off = i;
1219 b[DIFF_RIGHT].len = n - i;
1221 /* smartscan (multiple horizontal diff) */
1222 return hdiff_multi (s, t, b, min, hdiff, depth);
1225 /* --------------------------------------------------------------------------------------------- */
1227 /* read line **************************************************************** */
1230 * Check if character is inside horizontal diff limits.
1232 * @param k rank of character inside line
1233 * @param hdiff horizontal diff structure
1234 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1236 * @returns TRUE if inside hdiff limits, FALSE otherwise
1239 static gboolean
1240 is_inside (int k, GArray * hdiff, diff_place_t ord)
1242 size_t i;
1243 BRACKET *b;
1245 for (i = 0; i < hdiff->len; i++)
1247 int start, end;
1249 b = &g_array_index (hdiff, BRACKET, i);
1250 start = (*b)[ord].off;
1251 end = start + (*b)[ord].len;
1252 if (k >= start && k < end)
1253 return TRUE;
1255 return FALSE;
1258 /* --------------------------------------------------------------------------------------------- */
1261 * Copy 'src' to 'dst' expanding tabs.
1262 * @note The procedure returns when all bytes are consumed from 'src'
1264 * @param dst destination buffer
1265 * @param src source buffer
1266 * @param srcsize size of src buffer
1267 * @param base virtual base of this string, needed to calculate tabs
1268 * @param ts tab size
1270 * @returns new virtual base
1273 static int
1274 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
1276 int i;
1278 for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1280 *dst = *src;
1281 if (*src == '\t')
1283 int j;
1285 j = TAB_SKIP (ts, i + base);
1286 i += j - 1;
1287 while (j-- > 0)
1288 *dst++ = ' ';
1289 dst--;
1292 return i + base;
1295 /* --------------------------------------------------------------------------------------------- */
1298 * Copy 'src' to 'dst' expanding tabs.
1300 * @param dst destination buffer
1301 * @param dstsize size of dst buffer
1302 * @param[in,out] _src source buffer
1303 * @param srcsize size of src buffer
1304 * @param base virtual base of this string, needed to calculate tabs
1305 * @param ts tab size
1307 * @returns new virtual base
1309 * @note The procedure returns when all bytes are consumed from 'src'
1310 * or 'dstsize' bytes are written to 'dst'
1311 * @note Upon return, 'src' points to the first unwritten character in source
1314 static int
1315 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
1317 int i;
1318 const char *src = *_src;
1320 for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1322 *dst = *src;
1323 if (*src == '\t')
1325 int j;
1327 j = TAB_SKIP (ts, i + base);
1328 if (j > dstsize - i)
1329 j = dstsize - i;
1330 i += j - 1;
1331 while (j-- > 0)
1332 *dst++ = ' ';
1333 dst--;
1336 *_src = src;
1337 return i + base;
1340 /* --------------------------------------------------------------------------------------------- */
1343 * Read line from memory, converting tabs to spaces and padding with spaces.
1345 * @param src buffer to read from
1346 * @param srcsize size of src buffer
1347 * @param dst buffer to read to
1348 * @param dstsize size of dst buffer, excluding trailing null
1349 * @param skip number of characters to skip
1350 * @param ts tab size
1351 * @param show_cr show trailing carriage return as ^M
1353 * @returns negative on error, otherwise number of bytes except padding
1356 static int
1357 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr)
1359 int sz = 0;
1361 if (src != NULL)
1363 int i;
1364 char *tmp = dst;
1365 const int base = 0;
1367 for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1369 if (*src == '\t')
1371 int j;
1373 j = TAB_SKIP (ts, i + base);
1374 i += j - 1;
1375 while (j-- > 0)
1377 if (skip > 0)
1378 skip--;
1379 else if (dstsize != 0)
1381 dstsize--;
1382 *dst++ = ' ';
1386 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1388 if (skip == 0 && show_cr)
1390 if (dstsize > 1)
1392 dstsize -= 2;
1393 *dst++ = '^';
1394 *dst++ = 'M';
1396 else
1398 dstsize--;
1399 *dst++ = '.';
1402 break;
1404 else if (skip > 0)
1406 int utf_ch = 0;
1407 gboolean res;
1408 int w;
1410 skip--;
1411 utf_ch = dview_get_utf ((char *) src, &w, &res);
1412 if (w > 1)
1413 skip += w - 1;
1414 if (!g_unichar_isprint (utf_ch))
1415 utf_ch = '.';
1417 else
1419 dstsize--;
1420 *dst++ = *src;
1423 sz = dst - tmp;
1425 while (dstsize != 0)
1427 dstsize--;
1428 *dst++ = ' ';
1430 *dst = '\0';
1431 return sz;
1434 /* --------------------------------------------------------------------------------------------- */
1437 * Read line from memory and build attribute array.
1439 * @param src buffer to read from
1440 * @param srcsize size of src buffer
1441 * @param dst buffer to read to
1442 * @param dstsize size of dst buffer, excluding trailing null
1443 * @param skip number of characters to skip
1444 * @param ts tab size
1445 * @param show_cr show trailing carriage return as ^M
1446 * @param hdiff horizontal diff structure
1447 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1448 * @param att buffer of attributes
1450 * @returns negative on error, otherwise number of bytes except padding
1453 static int
1454 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts, int show_cr,
1455 GArray * hdiff, diff_place_t ord, char *att)
1457 int sz = 0;
1459 if (src != NULL)
1461 int i, k;
1462 char *tmp = dst;
1463 const int base = 0;
1465 for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1467 if (*src == '\t')
1469 int j;
1471 j = TAB_SKIP (ts, i + base);
1472 i += j - 1;
1473 while (j-- > 0)
1475 if (skip != 0)
1476 skip--;
1477 else if (dstsize != 0)
1479 dstsize--;
1480 *att++ = is_inside (k, hdiff, ord);
1481 *dst++ = ' ';
1485 else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1487 if (skip == 0 && show_cr)
1489 if (dstsize > 1)
1491 dstsize -= 2;
1492 *att++ = is_inside (k, hdiff, ord);
1493 *dst++ = '^';
1494 *att++ = is_inside (k, hdiff, ord);
1495 *dst++ = 'M';
1497 else
1499 dstsize--;
1500 *att++ = is_inside (k, hdiff, ord);
1501 *dst++ = '.';
1504 break;
1506 else if (skip != 0)
1508 int utf_ch = 0;
1509 gboolean res;
1510 int w;
1512 skip--;
1513 utf_ch = dview_get_utf ((char *) src, &w, &res);
1514 if (w > 1)
1515 skip += w - 1;
1516 if (!g_unichar_isprint (utf_ch))
1517 utf_ch = '.';
1519 else
1521 dstsize--;
1522 *att++ = is_inside (k, hdiff, ord);
1523 *dst++ = *src;
1526 sz = dst - tmp;
1528 while (dstsize != 0)
1530 dstsize--;
1531 *att++ = '\0';
1532 *dst++ = ' ';
1534 *dst = '\0';
1535 return sz;
1538 /* --------------------------------------------------------------------------------------------- */
1541 * Read line from file, converting tabs to spaces and padding with spaces.
1543 * @param f file stream to read from
1544 * @param off offset of line inside file
1545 * @param dst buffer to read to
1546 * @param dstsize size of dst buffer, excluding trailing null
1547 * @param skip number of characters to skip
1548 * @param ts tab size
1549 * @param show_cr show trailing carriage return as ^M
1551 * @returns negative on error, otherwise number of bytes except padding
1554 static int
1555 cvt_fget (FBUF * f, off_t off, char *dst, size_t dstsize, int skip, int ts, int show_cr)
1557 int base = 0;
1558 int old_base = base;
1559 size_t amount = dstsize;
1560 size_t useful, offset;
1561 size_t i;
1562 size_t sz;
1563 int lastch = '\0';
1564 const char *q = NULL;
1565 char tmp[BUFSIZ]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1566 char cvt[BUFSIZ]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1568 if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1570 /* abnormal, but avoid buffer overflow */
1571 memset (dst, ' ', dstsize);
1572 dst[dstsize] = '\0';
1573 return 0;
1576 f_seek (f, off, SEEK_SET);
1578 while (skip > base)
1580 old_base = base;
1581 sz = f_gets (tmp, amount, f);
1582 if (sz == 0)
1583 break;
1585 base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1586 if (cvt[base - old_base - 1] == '\n')
1588 q = &cvt[base - old_base - 1];
1589 base = old_base + q - cvt + 1;
1590 break;
1594 if (base < skip)
1596 memset (dst, ' ', dstsize);
1597 dst[dstsize] = '\0';
1598 return 0;
1601 useful = base - skip;
1602 offset = skip - old_base;
1604 if (useful <= dstsize)
1606 if (useful != 0)
1607 memmove (dst, cvt + offset, useful);
1609 if (q == NULL)
1611 sz = f_gets (tmp, dstsize - useful + 1, f);
1612 if (sz != 0)
1614 const char *ptr = tmp;
1616 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1617 if (ptr < tmp + sz)
1618 lastch = *ptr;
1621 sz = useful;
1623 else
1625 memmove (dst, cvt + offset, dstsize);
1626 sz = dstsize;
1627 lastch = cvt[offset + dstsize];
1630 dst[sz] = lastch;
1631 for (i = 0; i < sz && dst[i] != '\n'; i++)
1633 if (dst[i] == '\r' && dst[i + 1] == '\n')
1635 if (show_cr)
1637 if (i + 1 < dstsize)
1639 dst[i++] = '^';
1640 dst[i++] = 'M';
1642 else
1644 dst[i++] = '*';
1647 break;
1651 for (; i < dstsize; i++)
1652 dst[i] = ' ';
1653 dst[i] = '\0';
1654 return sz;
1657 /* --------------------------------------------------------------------------------------------- */
1658 /* diff printers et al ****************************************************** */
1660 static void
1661 cc_free_elt (void *elt)
1663 DIFFLN *p = elt;
1665 if (p != NULL)
1666 g_free (p->p);
1669 /* --------------------------------------------------------------------------------------------- */
1671 static int
1672 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
1674 GArray *a = ((PRINTER_CTX *) ctx)->a;
1675 DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1677 if (ch != 0)
1679 DIFFLN p;
1681 p.p = NULL;
1682 p.ch = ch;
1683 p.line = line;
1684 p.u.off = off;
1685 if (dsrc == DATA_SRC_MEM && line != 0)
1687 if (sz != 0 && str[sz - 1] == '\n')
1688 sz--;
1689 if (sz > 0)
1690 p.p = g_strndup (str, sz);
1691 p.u.len = sz;
1693 g_array_append_val (a, p);
1695 else if (dsrc == DATA_SRC_MEM)
1697 DIFFLN *p;
1699 p = &g_array_index (a, DIFFLN, a->len - 1);
1700 if (sz != 0 && str[sz - 1] == '\n')
1701 sz--;
1702 if (sz != 0)
1704 size_t new_size;
1705 char *q;
1707 new_size = p->u.len + sz;
1708 q = g_realloc (p->p, new_size);
1709 memcpy (q + p->u.len, str, sz);
1710 p->p = q;
1712 p->u.len += sz;
1714 if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1716 FBUF *f = ((PRINTER_CTX *) ctx)->f;
1717 f_write (f, str, sz);
1719 return 0;
1722 /* --------------------------------------------------------------------------------------------- */
1724 static int
1725 redo_diff (WDiff * dview)
1727 FBUF *const *f = dview->f;
1728 PRINTER_CTX ctx;
1729 GArray *ops;
1730 int ndiff;
1731 int rv;
1732 char extra[256];
1734 extra[0] = '\0';
1735 if (dview->opt.quality == 2)
1736 strcat (extra, " -d");
1737 if (dview->opt.quality == 1)
1738 strcat (extra, " --speed-large-files");
1739 if (dview->opt.strip_trailing_cr)
1740 strcat (extra, " --strip-trailing-cr");
1741 if (dview->opt.ignore_tab_expansion)
1742 strcat (extra, " -E");
1743 if (dview->opt.ignore_space_change)
1744 strcat (extra, " -b");
1745 if (dview->opt.ignore_all_space)
1746 strcat (extra, " -w");
1747 if (dview->opt.ignore_case)
1748 strcat (extra, " -i");
1750 if (dview->dsrc != DATA_SRC_MEM)
1752 f_reset (f[DIFF_LEFT]);
1753 f_reset (f[DIFF_RIGHT]);
1756 ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1757 ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1758 if (ndiff < 0)
1760 if (ops != NULL)
1761 g_array_free (ops, TRUE);
1762 return -1;
1765 ctx.dsrc = dview->dsrc;
1767 rv = 0;
1768 ctx.a = dview->a[DIFF_LEFT];
1769 ctx.f = f[DIFF_LEFT];
1770 rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1772 ctx.a = dview->a[DIFF_RIGHT];
1773 ctx.f = f[DIFF_RIGHT];
1774 rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1776 if (ops != NULL)
1777 g_array_free (ops, TRUE);
1779 if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1780 return -1;
1782 if (dview->dsrc == DATA_SRC_TMP)
1784 f_trunc (f[DIFF_LEFT]);
1785 f_trunc (f[DIFF_RIGHT]);
1788 if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1790 dview->hdiff = g_ptr_array_new ();
1791 if (dview->hdiff != NULL)
1793 size_t i;
1794 const DIFFLN *p;
1795 const DIFFLN *q;
1797 for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1799 GArray *h = NULL;
1801 p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1802 q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1803 if (p->line && q->line && p->ch == CHG_CH)
1805 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1806 if (h != NULL)
1808 gboolean runresult;
1810 runresult =
1811 hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h,
1812 HDIFF_DEPTH);
1813 if (!runresult)
1815 g_array_free (h, TRUE);
1816 h = NULL;
1820 g_ptr_array_add (dview->hdiff, h);
1824 return ndiff;
1827 /* --------------------------------------------------------------------------------------------- */
1829 static void
1830 destroy_hdiff (WDiff * dview)
1832 if (dview->hdiff != NULL)
1834 int i;
1835 int len;
1837 len = dview->a[DIFF_LEFT]->len;
1839 for (i = 0; i < len; i++)
1841 GArray *h;
1843 h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1844 if (h != NULL)
1845 g_array_free (h, TRUE);
1847 g_ptr_array_free (dview->hdiff, TRUE);
1848 dview->hdiff = NULL;
1851 mc_search_free (dview->search.handle);
1852 dview->search.handle = NULL;
1853 g_free (dview->search.last_string);
1854 dview->search.last_string = NULL;
1857 /* --------------------------------------------------------------------------------------------- */
1858 /* stuff ******************************************************************** */
1860 static int
1861 get_digits (unsigned int n)
1863 int d = 1;
1865 while (n /= 10)
1866 d++;
1867 return d;
1870 /* --------------------------------------------------------------------------------------------- */
1872 static int
1873 get_line_numbers (const GArray * a, size_t pos, int *linenum, int *lineofs)
1875 const DIFFLN *p;
1877 *linenum = 0;
1878 *lineofs = 0;
1880 if (a->len != 0)
1882 if (pos >= a->len)
1883 pos = a->len - 1;
1885 p = &g_array_index (a, DIFFLN, pos);
1887 if (p->line == 0)
1889 int n;
1891 for (n = pos; n > 0; n--)
1893 p--;
1894 if (p->line != 0)
1895 break;
1897 *lineofs = pos - n + 1;
1900 *linenum = p->line;
1902 return 0;
1905 /* --------------------------------------------------------------------------------------------- */
1907 static int
1908 calc_nwidth (const GArray ** const a)
1910 int l1, o1;
1911 int l2, o2;
1913 get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1914 get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1915 if (l1 < l2)
1916 l1 = l2;
1917 return get_digits (l1);
1920 /* --------------------------------------------------------------------------------------------- */
1922 static int
1923 find_prev_hunk (const GArray * a, int pos)
1925 #if 1
1926 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1927 pos--;
1928 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1929 pos--;
1930 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1931 pos--;
1932 if (pos > 0 && (size_t) pos < a->len)
1933 pos++;
1934 #else
1935 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH)
1936 pos--;
1937 while (pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH)
1938 pos--;
1939 #endif
1941 return pos;
1944 /* --------------------------------------------------------------------------------------------- */
1946 static size_t
1947 find_next_hunk (const GArray * a, size_t pos)
1949 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH)
1950 pos++;
1951 while (pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH)
1952 pos++;
1953 return pos;
1956 /* --------------------------------------------------------------------------------------------- */
1958 * Find start and end lines of the current hunk.
1960 * @param dview WDiff widget
1961 * @returns boolean and
1962 * start_line1 first line of current hunk (file[0])
1963 * end_line1 last line of current hunk (file[0])
1964 * start_line1 first line of current hunk (file[0])
1965 * end_line1 last line of current hunk (file[0])
1968 static int
1969 get_current_hunk (WDiff * dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
1971 const GArray *a0 = dview->a[DIFF_LEFT];
1972 const GArray *a1 = dview->a[DIFF_RIGHT];
1973 size_t pos;
1974 int ch;
1975 int res = 0;
1977 *start_line1 = 1;
1978 *start_line2 = 1;
1979 *end_line1 = 1;
1980 *end_line2 = 1;
1982 pos = dview->skip_rows;
1983 ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1984 if (ch != EQU_CH)
1986 switch (ch)
1988 case ADD_CH:
1989 res = DIFF_DEL;
1990 break;
1991 case DEL_CH:
1992 res = DIFF_ADD;
1993 break;
1994 case CHG_CH:
1995 res = DIFF_CHG;
1996 break;
1998 while (pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
1999 pos--;
2000 if (pos > 0)
2002 *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
2003 *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
2005 pos = dview->skip_rows;
2006 while (pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH)
2008 int l0, l1;
2010 l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
2011 l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
2012 if (l0 > 0)
2013 *end_line1 = max (*start_line1, l0);
2014 if (l1 > 0)
2015 *end_line2 = max (*start_line2, l1);
2016 pos++;
2019 return res;
2022 /* --------------------------------------------------------------------------------------------- */
2024 * Remove hunk from file.
2026 * @param dview WDiff widget
2027 * @param merge_file file stream for writing data
2028 * @param from1 first line of hunk
2029 * @param to1 last line of hunk
2030 * @param merge_direction in what direction files should be merged
2033 static void
2034 dview_remove_hunk (WDiff * dview, FILE * merge_file, int from1, int to1,
2035 action_direction_t merge_direction)
2037 int line;
2038 char buf[BUF_10K];
2039 FILE *f0;
2041 if (merge_direction == FROM_RIGHT_TO_LEFT)
2042 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2043 else
2044 f0 = fopen (dview->file[DIFF_LEFT], "r");
2046 line = 0;
2047 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2049 line++;
2050 fputs (buf, merge_file);
2052 while (fgets (buf, sizeof (buf), f0) != NULL)
2054 line++;
2055 if (line >= to1)
2056 fputs (buf, merge_file);
2058 fclose (f0);
2061 /* --------------------------------------------------------------------------------------------- */
2063 * Add hunk to file.
2065 * @param dview WDiff widget
2066 * @param merge_file file stream for writing data
2067 * @param from1 first line of source hunk
2068 * @param from2 first line of destination hunk
2069 * @param to1 last line of source hunk
2070 * @param merge_direction in what direction files should be merged
2073 static void
2074 dview_add_hunk (WDiff * dview, FILE * merge_file, int from1, int from2, int to2,
2075 action_direction_t merge_direction)
2077 int line;
2078 char buf[BUF_10K];
2079 FILE *f0;
2080 FILE *f1;
2082 if (merge_direction == FROM_RIGHT_TO_LEFT)
2084 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2085 f1 = fopen (dview->file[DIFF_LEFT], "r");
2087 else
2089 f0 = fopen (dview->file[DIFF_LEFT], "r");
2090 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2093 line = 0;
2094 while (fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1)
2096 line++;
2097 fputs (buf, merge_file);
2099 line = 0;
2100 while (fgets (buf, sizeof (buf), f1) != NULL && line <= to2)
2102 line++;
2103 if (line >= from2)
2104 fputs (buf, merge_file);
2106 while (fgets (buf, sizeof (buf), f0) != NULL)
2107 fputs (buf, merge_file);
2109 fclose (f0);
2110 fclose (f1);
2113 /* --------------------------------------------------------------------------------------------- */
2115 * Replace hunk in file.
2117 * @param dview WDiff widget
2118 * @param merge_file file stream for writing data
2119 * @param from1 first line of source hunk
2120 * @param to1 last line of source hunk
2121 * @param from2 first line of destination hunk
2122 * @param to2 last line of destination hunk
2123 * @param merge_direction in what direction files should be merged
2126 static void
2127 dview_replace_hunk (WDiff * dview, FILE * merge_file, int from1, int to1, int from2, int to2,
2128 action_direction_t merge_direction)
2130 int line1 = 0, line2 = 0;
2131 char buf[BUF_10K];
2132 FILE *f0;
2133 FILE *f1;
2135 if (merge_direction == FROM_RIGHT_TO_LEFT)
2137 f0 = fopen (dview->file[DIFF_RIGHT], "r");
2138 f1 = fopen (dview->file[DIFF_LEFT], "r");
2140 else
2142 f0 = fopen (dview->file[DIFF_LEFT], "r");
2143 f1 = fopen (dview->file[DIFF_RIGHT], "r");
2146 while (fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1)
2148 line1++;
2149 fputs (buf, merge_file);
2151 while (fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2)
2153 line2++;
2154 if (line2 >= from2)
2155 fputs (buf, merge_file);
2157 while (fgets (buf, sizeof (buf), f0) != NULL)
2159 line1++;
2160 if (line1 > to1)
2161 fputs (buf, merge_file);
2163 fclose (f0);
2164 fclose (f1);
2167 /* --------------------------------------------------------------------------------------------- */
2169 * Merge hunk.
2171 * @param dview WDiff widget
2172 * @param merge_direction in what direction files should be merged
2175 static void
2176 do_merge_hunk (WDiff * dview, action_direction_t merge_direction)
2178 int from1, to1, from2, to2;
2179 int res;
2180 int hunk;
2181 diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2183 if (merge_direction == FROM_RIGHT_TO_LEFT)
2184 hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2185 else
2186 hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2188 if (hunk > 0)
2190 int merge_file_fd;
2191 FILE *merge_file;
2192 vfs_path_t *merge_file_name_vpath = NULL;
2194 if (!dview->merged[n_merge])
2196 dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2197 if (!dview->merged[n_merge])
2199 message (D_ERROR, MSG_ERROR,
2200 _("Cannot create backup file\n%s%s\n%s"),
2201 dview->file[n_merge], "~~~", unix_error_string (errno));
2202 return;
2206 merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2207 if (merge_file_fd == -1)
2209 message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2210 unix_error_string (errno));
2211 return;
2214 merge_file = fdopen (merge_file_fd, "w");
2216 switch (hunk)
2218 case DIFF_DEL:
2219 if (merge_direction == FROM_RIGHT_TO_LEFT)
2220 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2221 else
2222 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2223 break;
2224 case DIFF_ADD:
2225 if (merge_direction == FROM_RIGHT_TO_LEFT)
2226 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2227 else
2228 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2229 break;
2230 case DIFF_CHG:
2231 dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2232 break;
2234 fflush (merge_file);
2235 fclose (merge_file);
2236 res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2237 mc_unlink (merge_file_name_vpath);
2238 vfs_path_free (merge_file_name_vpath);
2242 /* --------------------------------------------------------------------------------------------- */
2243 /* view routines and callbacks ********************************************** */
2245 static void
2246 dview_compute_split (WDiff * dview, int i)
2248 dview->bias += i;
2249 if (dview->bias < 2 - dview->half1)
2250 dview->bias = 2 - dview->half1;
2251 if (dview->bias > dview->half2 - 2)
2252 dview->bias = dview->half2 - 2;
2255 /* --------------------------------------------------------------------------------------------- */
2257 static void
2258 dview_compute_areas (WDiff * dview)
2260 dview->height = LINES - 2;
2261 dview->half1 = COLS / 2;
2262 dview->half2 = COLS - dview->half1;
2264 dview_compute_split (dview, 0);
2267 /* --------------------------------------------------------------------------------------------- */
2269 static void
2270 dview_reread (WDiff * dview)
2272 int ndiff;
2274 destroy_hdiff (dview);
2275 if (dview->a[DIFF_LEFT] != NULL)
2277 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2278 g_array_free (dview->a[DIFF_LEFT], TRUE);
2280 if (dview->a[DIFF_RIGHT] != NULL)
2282 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2283 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2286 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2287 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2289 ndiff = redo_diff (dview);
2290 if (ndiff >= 0)
2291 dview->ndiff = ndiff;
2294 /* --------------------------------------------------------------------------------------------- */
2296 #ifdef HAVE_CHARSET
2297 static void
2298 dview_set_codeset (WDiff * dview)
2300 const char *encoding_id = NULL;
2302 dview->utf8 = TRUE;
2303 encoding_id =
2304 get_codepage_id (mc_global.source_codepage >=
2305 0 ? mc_global.source_codepage : mc_global.display_codepage);
2306 if (encoding_id != NULL)
2308 GIConv conv;
2310 conv = str_crt_conv_from (encoding_id);
2311 if (conv != INVALID_CONV)
2313 if (dview->converter != str_cnv_from_term)
2314 str_close_conv (dview->converter);
2315 dview->converter = conv;
2317 dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2321 /* --------------------------------------------------------------------------------------------- */
2323 static void
2324 dview_select_encoding (WDiff * dview)
2326 if (do_select_codepage ())
2327 dview_set_codeset (dview);
2328 dview_reread (dview);
2329 tty_touch_screen ();
2330 repaint_screen ();
2332 #endif /* HAVE_CHARSET */
2334 /* --------------------------------------------------------------------------------------------- */
2336 static void
2337 dview_diff_options (WDiff * dview)
2339 const char *quality_str[] = {
2340 N_("No&rmal"),
2341 N_("&Fastest (Assume large files)"),
2342 N_("&Minimal (Find a smaller set of change)")
2345 QuickWidget diffopt_widgets[] = {
2346 QUICK_BUTTON (6, 10, 14, OPTY, N_("&Cancel"), B_CANCEL, NULL),
2347 QUICK_BUTTON (2, 10, 14, OPTY, N_("&OK"), B_ENTER, NULL),
2349 QUICK_CHECKBOX (3, OPTX, 12, OPTY,
2350 N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr),
2351 QUICK_CHECKBOX (3, OPTX, 11, OPTY,
2352 N_("Ignore all &whitespace"), &dview->opt.ignore_all_space),
2353 QUICK_CHECKBOX (3, OPTX, 10, OPTY,
2354 N_("Ignore &space change"), &dview->opt.ignore_space_change),
2355 QUICK_CHECKBOX (3, OPTX, 9, OPTY,
2356 N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion),
2357 QUICK_CHECKBOX (3, OPTX, 8, OPTY,
2358 N_("&Ignore case"), &dview->opt.ignore_case),
2359 QUICK_LABEL (3, OPTX, 7, OPTY, N_("Diff extra options")),
2360 QUICK_RADIO (3, OPTX, 3, OPTY,
2361 3, (const char **) quality_str, (int *) &dview->opt.quality),
2362 QUICK_LABEL (3, OPTX, 2, OPTY, N_("Diff algorithm")),
2364 QUICK_END
2367 QuickDialog diffopt = {
2368 OPTX, OPTY, -1, -1,
2369 N_("Diff Options"), "[Diff Options]",
2370 diffopt_widgets, NULL, NULL, FALSE
2373 if (quick_dialog (&diffopt) != B_CANCEL)
2374 dview_reread (dview);
2377 /* --------------------------------------------------------------------------------------------- */
2379 static int
2380 dview_init (WDiff * dview, const char *args, const char *file1, const char *file2,
2381 const char *label1, const char *label2, DSRC dsrc)
2383 int ndiff;
2384 FBUF *f[DIFF_COUNT];
2386 f[DIFF_LEFT] = NULL;
2387 f[DIFF_RIGHT] = NULL;
2389 if (dsrc == DATA_SRC_TMP)
2391 f[DIFF_LEFT] = f_temp ();
2392 if (f[DIFF_LEFT] == NULL)
2393 return -1;
2395 f[DIFF_RIGHT] = f_temp ();
2396 if (f[DIFF_RIGHT] == NULL)
2398 f_close (f[DIFF_LEFT]);
2399 return -1;
2402 else if (dsrc == DATA_SRC_ORG)
2404 f[DIFF_LEFT] = f_open (file1, O_RDONLY);
2405 if (f[DIFF_LEFT] == NULL)
2406 return -1;
2408 f[DIFF_RIGHT] = f_open (file2, O_RDONLY);
2409 if (f[DIFF_RIGHT] == NULL)
2411 f_close (f[DIFF_LEFT]);
2412 return -1;
2416 dview->args = args;
2417 dview->file[DIFF_LEFT] = file1;
2418 dview->file[DIFF_RIGHT] = file2;
2419 dview->label[DIFF_LEFT] = g_strdup (label1);
2420 dview->label[DIFF_RIGHT] = g_strdup (label2);
2421 dview->f[DIFF_LEFT] = f[0];
2422 dview->f[DIFF_RIGHT] = f[1];
2423 dview->merged[DIFF_LEFT] = FALSE;
2424 dview->merged[DIFF_RIGHT] = FALSE;
2425 dview->hdiff = NULL;
2426 dview->dsrc = dsrc;
2427 dview->converter = str_cnv_from_term;
2428 #ifdef HAVE_CHARSET
2429 dview_set_codeset (dview);
2430 #endif
2431 dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2432 dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2434 ndiff = redo_diff (dview);
2435 if (ndiff < 0)
2437 /* goto WIDGET_DESTROY stage: dview_fini() */
2438 f_close (f[DIFF_LEFT]);
2439 f_close (f[DIFF_RIGHT]);
2440 return -1;
2443 dview->ndiff = ndiff;
2445 dview->view_quit = 0;
2447 dview->bias = 0;
2448 dview->new_frame = 1;
2449 dview->skip_rows = 0;
2450 dview->skip_cols = 0;
2451 dview->display_symbols = 0;
2452 dview->display_numbers = 0;
2453 dview->show_cr = 1;
2454 dview->tab_size = 8;
2455 dview->ord = DIFF_LEFT;
2456 dview->full = 0;
2458 dview->search.handle = NULL;
2459 dview->search.last_string = NULL;
2460 dview->search.last_found_line = -1;
2461 dview->search.last_accessed_num_line = -1;
2463 dview->opt.quality = 0;
2464 dview->opt.strip_trailing_cr = 0;
2465 dview->opt.ignore_tab_expansion = 0;
2466 dview->opt.ignore_space_change = 0;
2467 dview->opt.ignore_all_space = 0;
2468 dview->opt.ignore_case = 0;
2470 dview_compute_areas (dview);
2472 return 0;
2475 /* --------------------------------------------------------------------------------------------- */
2477 static void
2478 dview_fini (WDiff * dview)
2480 if (dview->dsrc != DATA_SRC_MEM)
2482 f_close (dview->f[DIFF_RIGHT]);
2483 f_close (dview->f[DIFF_LEFT]);
2486 if (dview->converter != str_cnv_from_term)
2487 str_close_conv (dview->converter);
2489 destroy_hdiff (dview);
2490 if (dview->a[DIFF_LEFT] != NULL)
2492 g_array_foreach (dview->a[DIFF_LEFT], DIFFLN, cc_free_elt);
2493 g_array_free (dview->a[DIFF_LEFT], TRUE);
2494 dview->a[DIFF_LEFT] = NULL;
2496 if (dview->a[DIFF_RIGHT] != NULL)
2498 g_array_foreach (dview->a[DIFF_RIGHT], DIFFLN, cc_free_elt);
2499 g_array_free (dview->a[DIFF_RIGHT], TRUE);
2500 dview->a[DIFF_RIGHT] = NULL;
2503 g_free (dview->label[DIFF_LEFT]);
2504 g_free (dview->label[DIFF_RIGHT]);
2507 /* --------------------------------------------------------------------------------------------- */
2509 static int
2510 dview_display_file (const WDiff * dview, diff_place_t ord, int r, int c, int height, int width)
2512 size_t i, k;
2513 int j;
2514 char buf[BUFSIZ];
2515 FBUF *f = dview->f[ord];
2516 int skip = dview->skip_cols;
2517 int display_symbols = dview->display_symbols;
2518 int display_numbers = dview->display_numbers;
2519 int show_cr = dview->show_cr;
2520 int tab_size = 8;
2521 const DIFFLN *p;
2522 int nwidth = display_numbers;
2523 int xwidth;
2525 xwidth = display_symbols + display_numbers;
2526 if (dview->tab_size > 0 && dview->tab_size < 9)
2527 tab_size = dview->tab_size;
2529 if (xwidth != 0)
2531 if (xwidth > width && display_symbols)
2533 xwidth--;
2534 display_symbols = 0;
2536 if (xwidth > width && display_numbers)
2538 xwidth = width;
2539 display_numbers = width;
2542 xwidth++;
2543 c += xwidth;
2544 width -= xwidth;
2545 if (width < 0)
2546 width = 0;
2549 if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2551 /* abnormal, but avoid buffer overflow */
2552 return -1;
2555 for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2557 int ch, next_ch, col;
2558 size_t cnt;
2560 p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2561 ch = p->ch;
2562 tty_setcolor (NORMAL_COLOR);
2563 if (display_symbols)
2565 tty_gotoyx (r + j, c - 2);
2566 tty_print_char (ch);
2568 if (p->line != 0)
2570 if (display_numbers)
2572 tty_gotoyx (r + j, c - xwidth);
2573 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2574 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2576 if (ch == ADD_CH)
2577 tty_setcolor (DFF_ADD_COLOR);
2578 if (ch == CHG_CH)
2579 tty_setcolor (DFF_CHG_COLOR);
2580 if (f == NULL)
2582 if (i == (size_t) dview->search.last_found_line)
2583 tty_setcolor (MARKED_SELECTED_COLOR);
2584 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2586 char att[BUFSIZ];
2588 if (dview->utf8)
2589 k = dview_str_utf8_offset_to_pos (p->p, width);
2590 else
2591 k = width;
2593 cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2594 g_ptr_array_index (dview->hdiff, i), ord, att);
2595 tty_gotoyx (r + j, c);
2596 col = 0;
2598 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2600 int w;
2601 gboolean ch_res;
2603 if (dview->utf8)
2605 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2606 if (w > 1)
2607 cnt += w - 1;
2608 if (!g_unichar_isprint (next_ch))
2609 next_ch = '.';
2611 else
2612 next_ch = dview_get_byte (buf + cnt, &ch_res);
2614 if (ch_res)
2616 tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2617 #ifdef HAVE_CHARSET
2618 if (mc_global.utf8_display)
2620 if (!dview->utf8)
2622 next_ch =
2623 convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2624 dview->converter);
2627 else if (dview->utf8)
2628 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2629 else
2630 next_ch = convert_to_display_c (next_ch);
2631 #endif
2632 tty_print_anychar (next_ch);
2633 col++;
2636 continue;
2639 if (ch == CHG_CH)
2640 tty_setcolor (DFF_CHH_COLOR);
2642 if (dview->utf8)
2643 k = dview_str_utf8_offset_to_pos (p->p, width);
2644 else
2645 k = width;
2646 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2648 else
2649 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2651 else
2653 if (display_numbers)
2655 tty_gotoyx (r + j, c - xwidth);
2656 memset (buf, ' ', display_numbers);
2657 buf[display_numbers] = '\0';
2658 tty_print_string (buf);
2660 if (ch == DEL_CH)
2661 tty_setcolor (DFF_DEL_COLOR);
2662 if (ch == CHG_CH)
2663 tty_setcolor (DFF_CHD_COLOR);
2664 memset (buf, ' ', width);
2665 buf[width] = '\0';
2667 tty_gotoyx (r + j, c);
2668 /* tty_print_nstring (buf, width); */
2669 col = 0;
2670 for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2672 int w;
2673 gboolean ch_res;
2675 if (dview->utf8)
2677 next_ch = dview_get_utf (buf + cnt, &w, &ch_res);
2678 if (w > 1)
2679 cnt += w - 1;
2680 if (!g_unichar_isprint (next_ch))
2681 next_ch = '.';
2683 else
2684 next_ch = dview_get_byte (buf + cnt, &ch_res);
2685 if (ch_res)
2687 #ifdef HAVE_CHARSET
2688 if (mc_global.utf8_display)
2690 if (!dview->utf8)
2692 next_ch =
2693 convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2696 else if (dview->utf8)
2697 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2698 else
2699 next_ch = convert_to_display_c (next_ch);
2700 #endif
2702 tty_print_anychar (next_ch);
2703 col++;
2707 tty_setcolor (NORMAL_COLOR);
2708 k = width;
2709 if (width < xwidth - 1)
2710 k = xwidth - 1;
2711 memset (buf, ' ', k);
2712 buf[k] = '\0';
2713 for (; j < height; j++)
2715 if (xwidth != 0)
2717 tty_gotoyx (r + j, c - xwidth);
2718 /* tty_print_nstring (buf, xwidth - 1); */
2719 tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2721 tty_gotoyx (r + j, c);
2722 /* tty_print_nstring (buf, width); */
2723 tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2726 return 0;
2729 /* --------------------------------------------------------------------------------------------- */
2731 static void
2732 dview_status (const WDiff * dview, diff_place_t ord, int width, int c)
2734 const char *buf;
2735 int filename_width;
2736 int linenum, lineofs;
2737 vfs_path_t *vpath;
2738 char *path;
2740 tty_setcolor (STATUSBAR_COLOR);
2742 tty_gotoyx (0, c);
2743 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2745 filename_width = width - 24;
2746 if (filename_width < 8)
2747 filename_width = 8;
2749 vpath = vfs_path_from_str (dview->label[ord]);
2750 path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2751 vfs_path_free (vpath);
2752 buf = str_term_trim (path, filename_width);
2753 if (ord == DIFF_LEFT)
2754 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2755 buf, linenum, lineofs, dview->skip_cols);
2756 else
2757 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : " ", filename_width,
2758 buf, linenum, lineofs, dview->ndiff);
2759 g_free (path);
2762 /* --------------------------------------------------------------------------------------------- */
2764 static void
2765 dview_redo (WDiff * dview)
2767 if (dview->display_numbers)
2769 int old;
2771 old = dview->display_numbers;
2772 dview->display_numbers = calc_nwidth ((const GArray **) dview->a);
2773 dview->new_frame = (old != dview->display_numbers);
2775 dview_reread (dview);
2778 /* --------------------------------------------------------------------------------------------- */
2780 static void
2781 dview_update (WDiff * dview)
2783 int height = dview->height;
2784 int width1;
2785 int width2;
2786 int last;
2788 last = dview->a[DIFF_LEFT]->len - 1;
2790 if (dview->skip_rows > last)
2791 dview->skip_rows = dview->search.last_accessed_num_line = last;
2792 if (dview->skip_rows < 0)
2793 dview->skip_rows = dview->search.last_accessed_num_line = 0;
2794 if (dview->skip_cols < 0)
2795 dview->skip_cols = 0;
2797 if (height < 2)
2798 return;
2800 width1 = dview->half1 + dview->bias;
2801 width2 = dview->half2 - dview->bias;
2802 if (dview->full)
2804 width1 = COLS;
2805 width2 = 0;
2808 if (dview->new_frame)
2810 int xwidth;
2812 tty_setcolor (NORMAL_COLOR);
2813 xwidth = dview->display_symbols + dview->display_numbers;
2814 if (width1 > 1)
2815 tty_draw_box (1, 0, height, width1, FALSE);
2816 if (width2 > 1)
2817 tty_draw_box (1, width1, height, width2, FALSE);
2819 if (xwidth != 0)
2821 xwidth++;
2822 if (xwidth < width1 - 1)
2824 tty_gotoyx (1, xwidth);
2825 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2826 tty_gotoyx (height, xwidth);
2827 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2828 tty_draw_vline (2, xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2830 if (xwidth < width2 - 1)
2832 tty_gotoyx (1, width1 + xwidth);
2833 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DTOPMIDDLE], FALSE);
2834 tty_gotoyx (height, width1 + xwidth);
2835 tty_print_alt_char (mc_tty_frm[MC_TTY_FRM_DBOTTOMMIDDLE], FALSE);
2836 tty_draw_vline (2, width1 + xwidth, mc_tty_frm[MC_TTY_FRM_VERT], height - 2);
2839 dview->new_frame = 0;
2842 if (width1 > 2)
2844 dview_status (dview, dview->ord, width1, 0);
2845 dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2847 if (width2 > 2)
2849 dview_status (dview, dview->ord ^ 1, width2, width1);
2850 dview_display_file (dview, dview->ord ^ 1, 2, width1 + 1, height - 2, width2 - 2);
2854 /* --------------------------------------------------------------------------------------------- */
2856 static void
2857 dview_edit (WDiff * dview, diff_place_t ord)
2859 Dlg_head *h;
2860 gboolean h_modal;
2861 int linenum, lineofs;
2863 if (dview->dsrc == DATA_SRC_TMP)
2865 error_dialog (_("Edit"), _("Edit is disabled"));
2866 return;
2869 h = ((Widget *) dview)->owner;
2870 h_modal = h->modal;
2872 get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2873 h->modal = TRUE; /* not allow edit file in several editors */
2875 vfs_path_t *tmp_vpath;
2877 tmp_vpath = vfs_path_from_str (dview->file[ord]);
2878 do_edit_at_line (tmp_vpath, use_internal_edit, linenum);
2879 vfs_path_free (tmp_vpath);
2881 h->modal = h_modal;
2882 dview_redo (dview);
2883 dview_update (dview);
2886 /* --------------------------------------------------------------------------------------------- */
2888 static void
2889 dview_goto_cmd (WDiff * dview, diff_place_t ord)
2891 /* *INDENT-OFF* */
2892 static const char *title[2] = {
2893 N_("Goto line (left)"),
2894 N_("Goto line (right)")
2896 /* *INDENT-ON* */
2897 static char prev[256];
2898 /* XXX some statics here, to be remembered between runs */
2900 int newline;
2901 char *input;
2903 input = input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE, prev);
2904 if (input != NULL)
2906 const char *s = input;
2908 if (scan_deci (&s, &newline) == 0 && *s == '\0')
2910 size_t i = 0;
2912 if (newline > 0)
2914 const DIFFLN *p;
2916 for (; i < dview->a[ord]->len; i++)
2918 p = &g_array_index (dview->a[ord], DIFFLN, i);
2919 if (p->line == newline)
2920 break;
2923 dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2924 g_snprintf (prev, sizeof (prev), "%d", newline);
2926 g_free (input);
2930 /* --------------------------------------------------------------------------------------------- */
2932 static void
2933 dview_labels (WDiff * dview)
2935 Dlg_head *h;
2936 WButtonBar *b;
2938 h = dview->widget.owner;
2939 b = find_buttonbar (h);
2941 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), diff_map, (Widget *) dview);
2942 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), diff_map, (Widget *) dview);
2943 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), diff_map, (Widget *) dview);
2944 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), diff_map, (Widget *) dview);
2945 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), diff_map, (Widget *) dview);
2946 buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), diff_map, (Widget *) dview);
2947 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), diff_map, (Widget *) dview);
2950 /* --------------------------------------------------------------------------------------------- */
2952 static int
2953 dview_event (Gpm_Event * event, void *data)
2955 WDiff *dview = (WDiff *) data;
2957 if (!mouse_global_in_widget (event, data))
2958 return MOU_UNHANDLED;
2960 /* We are not interested in release events */
2961 if ((event->type & (GPM_DOWN | GPM_DRAG)) == 0)
2962 return MOU_NORMAL;
2964 /* Wheel events */
2965 if ((event->buttons & GPM_B_UP) != 0 && (event->type & GPM_DOWN) != 0)
2967 dview->skip_rows -= 2;
2968 dview->search.last_accessed_num_line = dview->skip_rows;
2969 dview_update (dview);
2971 else if ((event->buttons & GPM_B_DOWN) != 0 && (event->type & GPM_DOWN) != 0)
2973 dview->skip_rows += 2;
2974 dview->search.last_accessed_num_line = dview->skip_rows;
2975 dview_update (dview);
2978 return MOU_NORMAL;
2981 /* --------------------------------------------------------------------------------------------- */
2983 static gboolean
2984 dview_save (WDiff * dview)
2986 gboolean res = TRUE;
2988 if (dview->merged[DIFF_LEFT])
2990 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2991 dview->merged[DIFF_LEFT] = !res;
2993 if (dview->merged[DIFF_RIGHT])
2995 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2996 dview->merged[DIFF_RIGHT] = !res;
2998 return res;
3001 /* --------------------------------------------------------------------------------------------- */
3003 static void
3004 dview_do_save (WDiff * dview)
3006 (void) dview_save (dview);
3009 /* --------------------------------------------------------------------------------------------- */
3011 static void
3012 dview_save_options (WDiff * dview)
3014 mc_config_set_bool (mc_main_config, "DiffView", "show_symbols",
3015 dview->display_symbols != 0 ? TRUE : FALSE);
3016 mc_config_set_bool (mc_main_config, "DiffView", "show_numbers",
3017 dview->display_numbers != 0 ? TRUE : FALSE);
3018 mc_config_set_int (mc_main_config, "DiffView", "tab_size", dview->tab_size);
3020 mc_config_set_int (mc_main_config, "DiffView", "diff_quality", dview->opt.quality);
3022 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_tws",
3023 dview->opt.strip_trailing_cr);
3024 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_all_space",
3025 dview->opt.ignore_all_space);
3026 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_space_change",
3027 dview->opt.ignore_space_change);
3028 mc_config_set_bool (mc_main_config, "DiffView", "diff_tab_expansion",
3029 dview->opt.ignore_tab_expansion);
3030 mc_config_set_bool (mc_main_config, "DiffView", "diff_ignore_case", dview->opt.ignore_case);
3033 /* --------------------------------------------------------------------------------------------- */
3035 static void
3036 dview_load_options (WDiff * dview)
3038 gboolean show_numbers, show_symbols;
3039 int tab_size;
3041 show_symbols = mc_config_get_bool (mc_main_config, "DiffView", "show_symbols", FALSE);
3042 if (show_symbols)
3043 dview->display_symbols = 1;
3044 show_numbers = mc_config_get_bool (mc_main_config, "DiffView", "show_numbers", FALSE);
3045 if (show_numbers)
3046 dview->display_numbers = calc_nwidth ((const GArray ** const) dview->a);
3047 tab_size = mc_config_get_int (mc_main_config, "DiffView", "tab_size", 8);
3048 if (tab_size > 0 && tab_size < 9)
3049 dview->tab_size = tab_size;
3050 else
3051 dview->tab_size = 8;
3053 dview->opt.quality = mc_config_get_int (mc_main_config, "DiffView", "diff_quality", 0);
3055 dview->opt.strip_trailing_cr =
3056 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_tws", FALSE);
3057 dview->opt.ignore_all_space =
3058 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_all_space", FALSE);
3059 dview->opt.ignore_space_change =
3060 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_space_change", FALSE);
3061 dview->opt.ignore_tab_expansion =
3062 mc_config_get_bool (mc_main_config, "DiffView", "diff_tab_expansion", FALSE);
3063 dview->opt.ignore_case =
3064 mc_config_get_bool (mc_main_config, "DiffView", "diff_ignore_case", FALSE);
3066 dview->new_frame = 1;
3069 /* --------------------------------------------------------------------------------------------- */
3072 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3073 * ask user.
3075 static gboolean
3076 dview_ok_to_exit (WDiff * dview)
3078 gboolean res = TRUE;
3079 int act;
3081 if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3082 return res;
3084 act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3085 _("File(s) was modified. Save with exit?") :
3086 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3087 D_NORMAL, 2, _("&Yes"), _("&No"));
3089 /* Esc is No */
3090 if (mc_global.midnight_shutdown || (act == -1))
3091 act = 1;
3093 switch (act)
3095 case -1: /* Esc */
3096 res = FALSE;
3097 break;
3098 case 0: /* Yes */
3099 (void) dview_save (dview);
3100 res = TRUE;
3101 break;
3102 case 1: /* No */
3103 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3104 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3105 if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3106 res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3107 /* fall through */
3108 default:
3109 res = TRUE;
3110 break;
3112 return res;
3115 /* --------------------------------------------------------------------------------------------- */
3117 static cb_ret_t
3118 dview_execute_cmd (WDiff * dview, unsigned long command)
3120 cb_ret_t res = MSG_HANDLED;
3122 switch (command)
3124 case CK_ShowSymbols:
3125 dview->display_symbols ^= 1;
3126 dview->new_frame = 1;
3127 break;
3128 case CK_ShowNumbers:
3129 dview->display_numbers ^= calc_nwidth ((const GArray ** const) dview->a);
3130 dview->new_frame = 1;
3131 break;
3132 case CK_SplitFull:
3133 dview->full ^= 1;
3134 dview->new_frame = 1;
3135 break;
3136 case CK_SplitEqual:
3137 if (!dview->full)
3139 dview->bias = 0;
3140 dview->new_frame = 1;
3142 break;
3143 case CK_SplitMore:
3144 if (!dview->full)
3146 dview_compute_split (dview, 1);
3147 dview->new_frame = 1;
3149 break;
3151 case CK_SplitLess:
3152 if (!dview->full)
3154 dview_compute_split (dview, -1);
3155 dview->new_frame = 1;
3157 break;
3158 case CK_Tab2:
3159 dview->tab_size = 2;
3160 break;
3161 case CK_Tab3:
3162 dview->tab_size = 3;
3163 break;
3164 case CK_Tab4:
3165 dview->tab_size = 4;
3166 break;
3167 case CK_Tab8:
3168 dview->tab_size = 8;
3169 break;
3170 case CK_Swap:
3171 dview->ord ^= 1;
3172 break;
3173 case CK_Redo:
3174 dview_redo (dview);
3175 break;
3176 case CK_HunkNext:
3177 dview->skip_rows = dview->search.last_accessed_num_line =
3178 find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3179 break;
3180 case CK_HunkPrev:
3181 dview->skip_rows = dview->search.last_accessed_num_line =
3182 find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3183 break;
3184 case CK_Goto:
3185 dview_goto_cmd (dview, TRUE);
3186 break;
3187 case CK_Edit:
3188 dview_edit (dview, dview->ord);
3189 break;
3190 case CK_Merge:
3191 do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3192 dview_redo (dview);
3193 break;
3194 case CK_MergeOther:
3195 do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3196 dview_redo (dview);
3197 break;
3198 case CK_EditOther:
3199 dview_edit (dview, dview->ord ^ 1);
3200 break;
3201 case CK_Search:
3202 dview_search_cmd (dview);
3203 break;
3204 case CK_SearchContinue:
3205 dview_continue_search_cmd (dview);
3206 break;
3207 case CK_Top:
3208 dview->skip_rows = dview->search.last_accessed_num_line = 0;
3209 break;
3210 case CK_Bottom:
3211 dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3212 break;
3213 case CK_Up:
3214 if (dview->skip_rows > 0)
3216 dview->skip_rows--;
3217 dview->search.last_accessed_num_line = dview->skip_rows;
3219 break;
3220 case CK_Down:
3221 dview->skip_rows++;
3222 dview->search.last_accessed_num_line = dview->skip_rows;
3223 break;
3224 case CK_PageDown:
3225 if (dview->height > 2)
3227 dview->skip_rows += dview->height - 2;
3228 dview->search.last_accessed_num_line = dview->skip_rows;
3230 break;
3231 case CK_PageUp:
3232 if (dview->height > 2)
3234 dview->skip_rows -= dview->height - 2;
3235 dview->search.last_accessed_num_line = dview->skip_rows;
3237 break;
3238 case CK_Left:
3239 dview->skip_cols--;
3240 break;
3241 case CK_Right:
3242 dview->skip_cols++;
3243 break;
3244 case CK_LeftQuick:
3245 dview->skip_cols -= 8;
3246 break;
3247 case CK_RightQuick:
3248 dview->skip_cols += 8;
3249 break;
3250 case CK_Home:
3251 dview->skip_cols = 0;
3252 break;
3253 case CK_Shell:
3254 view_other_cmd ();
3255 break;
3256 case CK_Quit:
3257 dview->view_quit = 1;
3258 break;
3259 case CK_Save:
3260 dview_do_save (dview);
3261 break;
3262 case CK_Options:
3263 dview_diff_options (dview);
3264 break;
3265 #ifdef HAVE_CHARSET
3266 case CK_SelectCodepage:
3267 dview_select_encoding (dview);
3268 break;
3269 #endif
3270 case CK_Cancel:
3271 /* don't close diffviewer due to SIGINT */
3272 break;
3273 default:
3274 res = MSG_NOT_HANDLED;
3276 return res;
3279 /* --------------------------------------------------------------------------------------------- */
3281 static cb_ret_t
3282 dview_handle_key (WDiff * dview, int key)
3284 unsigned long command;
3286 #ifdef HAVE_CHARSET
3287 key = convert_from_input_c (key);
3288 #endif
3290 command = keybind_lookup_keymap_command (diff_map, key);
3291 if ((command != CK_IgnoreKey) && (dview_execute_cmd (dview, command) == MSG_HANDLED))
3292 return MSG_HANDLED;
3294 /* Key not used */
3295 return MSG_NOT_HANDLED;
3298 /* --------------------------------------------------------------------------------------------- */
3300 static cb_ret_t
3301 dview_callback (Widget * w, widget_msg_t msg, int parm)
3303 WDiff *dview = (WDiff *) w;
3304 Dlg_head *h = dview->widget.owner;
3305 cb_ret_t i;
3307 switch (msg)
3309 case WIDGET_INIT:
3310 dview_labels (dview);
3311 dview_load_options (dview);
3312 dview_update (dview);
3313 return MSG_HANDLED;
3315 case WIDGET_DRAW:
3316 dview->new_frame = 1;
3317 dview_update (dview);
3318 return MSG_HANDLED;
3320 case WIDGET_KEY:
3321 i = dview_handle_key (dview, parm);
3322 if (dview->view_quit)
3323 dlg_stop (h);
3324 else
3325 dview_update (dview);
3326 return i;
3328 case WIDGET_COMMAND:
3329 i = dview_execute_cmd (dview, parm);
3330 if (dview->view_quit)
3331 dlg_stop (h);
3332 else
3333 dview_update (dview);
3334 return i;
3336 case WIDGET_DESTROY:
3337 dview_save_options (dview);
3338 dview_fini (dview);
3339 return MSG_HANDLED;
3341 default:
3342 return default_proc (msg, parm);
3346 /* --------------------------------------------------------------------------------------------- */
3348 static void
3349 dview_adjust_size (Dlg_head * h)
3351 WDiff *dview;
3352 WButtonBar *bar;
3354 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3355 dview = (WDiff *) find_widget_type (h, dview_callback);
3356 bar = find_buttonbar (h);
3357 widget_set_size (&dview->widget, 0, 0, LINES - 1, COLS);
3358 widget_set_size ((Widget *) bar, LINES - 1, 0, 1, COLS);
3360 dview_compute_areas (dview);
3363 /* --------------------------------------------------------------------------------------------- */
3365 static cb_ret_t
3366 dview_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
3368 WDiff *dview = (WDiff *) data;
3370 switch (msg)
3372 case DLG_RESIZE:
3373 dview_adjust_size (h);
3374 return MSG_HANDLED;
3376 case DLG_ACTION:
3377 /* shortcut */
3378 if (sender == NULL)
3379 return dview_execute_cmd (NULL, parm);
3380 /* message from buttonbar */
3381 if (sender == (Widget *) find_buttonbar (h))
3383 if (data != NULL)
3384 return send_message ((Widget *) data, WIDGET_COMMAND, parm);
3386 dview = (WDiff *) find_widget_type (h, dview_callback);
3387 return dview_execute_cmd (dview, parm);
3389 return MSG_NOT_HANDLED;
3391 case DLG_VALIDATE:
3392 dview = (WDiff *) find_widget_type (h, dview_callback);
3393 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
3394 if (dview_ok_to_exit (dview))
3395 h->state = DLG_CLOSED;
3396 return MSG_HANDLED;
3398 default:
3399 return default_dlg_callback (h, sender, msg, parm, data);
3403 /* --------------------------------------------------------------------------------------------- */
3405 static char *
3406 dview_get_title (const Dlg_head * h, size_t len)
3408 const WDiff *dview;
3409 const char *modified = " (*) ";
3410 const char *notmodified = " ";
3411 size_t len1;
3412 GString *title;
3414 dview = (const WDiff *) find_widget_type (h, dview_callback);
3415 len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3417 title = g_string_sized_new (len);
3418 g_string_append (title, _("Diff:"));
3419 g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3420 g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3421 g_string_append (title, " | ");
3422 g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3423 g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3425 return g_string_free (title, FALSE);
3428 /* --------------------------------------------------------------------------------------------- */
3430 static int
3431 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
3433 int error;
3434 WDiff *dview;
3435 Dlg_head *dview_dlg;
3437 /* Create dialog and widgets, put them on the dialog */
3438 dview_dlg =
3439 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, dview_dialog_callback, NULL,
3440 "[Diff Viewer]", NULL, DLG_WANT_TAB);
3442 dview = g_new0 (WDiff, 1);
3444 init_widget (&dview->widget, 0, 0, LINES - 1, COLS,
3445 (callback_fn) dview_callback, (mouse_h) dview_event);
3447 widget_want_cursor (dview->widget, 0);
3449 add_widget (dview_dlg, dview);
3450 add_widget (dview_dlg, buttonbar_new (TRUE));
3452 dview_dlg->get_title = dview_get_title;
3454 error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM); /* XXX binary diff? */
3456 /* Please note that if you add another widget,
3457 * you have to modify dview_adjust_size to
3458 * be aware of it
3460 if (error == 0)
3461 run_dlg (dview_dlg);
3463 if ((error != 0) || (dview_dlg->state == DLG_CLOSED))
3464 destroy_dlg (dview_dlg);
3466 return error == 0 ? 1 : 0;
3469 /*** public functions ****************************************************************************/
3470 /* --------------------------------------------------------------------------------------------- */
3472 #define GET_FILE_AND_STAMP(n) \
3473 do \
3475 use_copy##n = 0; \
3476 real_file##n = file##n; \
3477 if (!vfs_file_is_local (file##n)) \
3479 real_file##n = mc_getlocalcopy (file##n); \
3480 if (real_file##n != NULL) \
3482 use_copy##n = 1; \
3483 if (mc_stat (real_file##n, &st##n) != 0) \
3484 use_copy##n = -1; \
3488 while (0)
3490 #define UNGET_FILE(n) \
3491 do \
3493 if (use_copy##n) \
3495 int changed = 0; \
3496 if (use_copy##n > 0) \
3498 time_t mtime; \
3499 mtime = st##n.st_mtime; \
3500 if (mc_stat (real_file##n, &st##n) == 0) \
3501 changed = (mtime != st##n.st_mtime); \
3503 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3504 vfs_path_free (real_file##n); \
3507 while (0)
3509 gboolean
3510 dview_diff_cmd (const void *f0, const void *f1)
3512 int rv = 0;
3513 vfs_path_t *file0 = NULL;
3514 vfs_path_t *file1 = NULL;
3515 gboolean is_dir0 = FALSE;
3516 gboolean is_dir1 = FALSE;
3518 switch (mc_global.mc_run_mode)
3520 case MC_RUN_FULL:
3522 /* run from panels */
3523 const WPanel *panel0 = (const WPanel *) f0;
3524 const WPanel *panel1 = (const WPanel *) f1;
3526 file0 = vfs_path_append_new (panel0->cwd_vpath, selection (panel0)->fname, NULL);
3527 is_dir0 = S_ISDIR (selection (panel0)->st.st_mode);
3528 if (is_dir0)
3530 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3531 path_trunc (selection (panel0)->fname, 30));
3532 goto ret;
3535 file1 = vfs_path_append_new (panel1->cwd_vpath, selection (panel1)->fname, NULL);
3536 is_dir1 = S_ISDIR (selection (panel1)->st.st_mode);
3537 if (is_dir1)
3539 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3540 path_trunc (selection (panel1)->fname, 30));
3541 goto ret;
3543 break;
3546 case MC_RUN_DIFFVIEWER:
3548 /* run from command line */
3549 const char *p0 = (const char *) f0;
3550 const char *p1 = (const char *) f1;
3551 struct stat st;
3553 file0 = vfs_path_from_str (p0);
3554 if (mc_stat (file0, &st) == 0)
3556 is_dir0 = S_ISDIR (st.st_mode);
3557 if (is_dir0)
3559 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3560 goto ret;
3563 else
3565 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3566 path_trunc (p0, 30), unix_error_string (errno));
3567 goto ret;
3570 file1 = vfs_path_from_str (p1);
3571 if (mc_stat (file1, &st) == 0)
3573 is_dir1 = S_ISDIR (st.st_mode);
3574 if (is_dir1)
3576 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3577 goto ret;
3580 else
3582 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3583 path_trunc (p1, 30), unix_error_string (errno));
3584 goto ret;
3586 break;
3589 default:
3590 /* this should not happaned */
3591 message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3592 return FALSE;
3595 if (rv == 0)
3597 rv = -1;
3598 if (file0 != NULL && file1 != NULL)
3600 int use_copy0;
3601 int use_copy1;
3602 struct stat st0;
3603 struct stat st1;
3604 vfs_path_t *real_file0;
3605 vfs_path_t *real_file1;
3607 GET_FILE_AND_STAMP (0);
3608 GET_FILE_AND_STAMP (1);
3609 if (real_file0 != NULL && real_file1 != NULL)
3611 char *real_file0_str, *real_file1_str;
3612 char *file0_str, *file1_str;
3614 real_file0_str = vfs_path_to_str (real_file0);
3615 real_file1_str = vfs_path_to_str (real_file1);
3616 file0_str = vfs_path_to_str (file0);
3617 file1_str = vfs_path_to_str (file1);
3618 rv = diff_view (real_file0_str, real_file1_str, file0_str, file1_str);
3619 g_free (real_file0_str);
3620 g_free (real_file1_str);
3621 g_free (file0_str);
3622 g_free (file1_str);
3624 UNGET_FILE (1);
3625 UNGET_FILE (0);
3629 if (rv == 0)
3630 message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3632 ret:
3633 vfs_path_free (file1);
3634 vfs_path_free (file0);
3636 return (rv != 0);
3639 #undef GET_FILE_AND_STAMP
3640 #undef UNGET_FILE
3642 /* --------------------------------------------------------------------------------------------- */