2 Copyright (C) 2007, 2010 Free Software Foundation, Inc.
4 2007 Daniel Borca <dborca@yahoo.com>
6 2010 Slava Zanko <slavazanko@gmail.com>
7 2010 Andrew Borodin <aborodin@vmail.ru>
8 2010 Ilia Maslakov <il.smind@gmail.com>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/types.h>
35 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/tty/color.h"
38 #include "lib/tty/key.h"
40 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
41 #include "lib/vfs/mc-vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
43 #include "src/cmddef.h"
44 #include "src/keybind.h"
46 #include "src/dialog.h"
48 #include "src/wtools.h"
49 #include "src/charsets.h"
50 #include "src/history.h"
51 #include "src/panel.h" /* Needed for current_panel and other_panel */
52 #include "src/layout.h" /* Needed for get_current_index and get_other_panel */
53 #include "src/main.h" /* mc_run_mode */
54 #include "src/selcodepage.h"
59 /*** global variables ****************************************************************************/
61 const global_keymap_t
*diff_map
;
63 /*** file scope macro definitions ****************************************************************/
65 #define g_array_foreach(a, TP, cbf) \
67 size_t g_array_foreach_i;\
68 TP *g_array_foreach_var=NULL; \
69 for (g_array_foreach_i=0;g_array_foreach_i < a->len; g_array_foreach_i++) \
71 g_array_foreach_var = &g_array_index(a,TP,g_array_foreach_i); \
72 (*cbf) (g_array_foreach_var); \
76 #define FILE_READ_BUF 4096
77 #define FILE_FLAG_TEMP (1 << 0)
87 #define HDIFF_ENABLE 1
88 #define HDIFF_MINCTX 5
89 #define HDIFF_DEPTH 10
91 #define TAB_SKIP(ts, pos) ((ts) - (pos) % (ts))
93 #define FILE_DIRTY(fs) \
99 /*** file scope type declarations ****************************************************************/
101 /*** file scope variables ************************************************************************/
103 /*** file scope functions ************************************************************************/
105 /* --------------------------------------------------------------------------------------------- */
108 dview_set_codeset (WDiff
* dview
)
111 const char *encoding_id
= NULL
;
114 encoding_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
115 if (encoding_id
!= NULL
)
118 conv
= str_crt_conv_from (encoding_id
);
119 if (conv
!= INVALID_CONV
)
121 if (dview
->converter
!= str_cnv_from_term
)
122 str_close_conv (dview
->converter
);
123 dview
->converter
= conv
;
125 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
132 /* --------------------------------------------------------------------------------------------- */
135 dview_select_encoding (WDiff
* dview
)
138 if (do_select_codepage ())
139 dview_set_codeset (dview
);
147 rewrite_backup_content (const char *from_file_name
, const char *to_file_name
)
153 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
156 backup_fd
= fopen (to_file_name
, "w");
157 if (backup_fd
== NULL
)
163 fwrite ((const void *) contents
, length
, 1, backup_fd
);
171 /* buffered I/O ************************************************************* */
174 * Try to open a temporary file.
176 * \param[out] name address of a pointer to store the temporary name
178 * \return file descriptor on success, negative on error
180 * \note the name is not altered if this function fails
184 open_temp (void **name
)
187 char *diff_file_name
= NULL
;
189 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
192 message (D_ERROR
, MSG_ERROR
,
193 _(" Cannot create temporary diff file \n %s "),
194 unix_error_string (errno
));
197 *name
= diff_file_name
;
201 /* --------------------------------------------------------------------------------------------- */
204 * Alocate file structure and associate file descriptor to it.
206 * \param fd file descriptor
208 * \return file structure
218 fs
= g_try_malloc (sizeof (FBUF
));
222 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
237 /* --------------------------------------------------------------------------------------------- */
240 * Free file structure without closing the file.
242 * \param fs file structure
244 * \return 0 on success, non-zero on error
250 if (fs
->flags
& FILE_FLAG_TEMP
)
252 rv
= unlink (fs
->data
);
261 /* --------------------------------------------------------------------------------------------- */
264 * Open a binary temporary file in R/W mode.
266 * \return file structure
268 * \note the file will be deleted when closed
282 fd
= open_temp (&fs
->data
);
290 fs
->flags
= FILE_FLAG_TEMP
;
294 /* --------------------------------------------------------------------------------------------- */
297 * Open a binary file in specified mode.
299 * \param filename file name
300 * \param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
302 * \return file structure
305 f_open (const char *filename
, int flags
)
316 fd
= open (filename
, flags
);
327 /* --------------------------------------------------------------------------------------------- */
330 * Read a line of bytes from file until newline or EOF.
332 * \param buf destination buffer
333 * \param size size of buffer
334 * \param fs file structure
336 * \return number of bytes read
338 * \note does not stop on null-byte
339 * \note buf will not be null-terminated
342 f_gets (char *buf
, size_t size
, FBUF
* fs
)
351 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
361 if (j
== size
|| stop
)
367 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
374 /* --------------------------------------------------------------------------------------------- */
379 * \param fs file structure
381 * \param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
383 * \return position in file, starting from begginning
385 * \note avoids thrashing read cache when possible
388 f_seek (FBUF
* fs
, off_t off
, int whence
)
392 if (fs
->len
&& whence
!= SEEK_END
)
394 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
397 if (whence
== SEEK_CUR
)
400 off
+= rv
- fs
->len
+ fs
->pos
;
402 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
404 fs
->pos
= fs
->len
+ off
- rv
;
410 rv
= lseek (fs
->fd
, off
, whence
);
418 /* --------------------------------------------------------------------------------------------- */
421 * Seek to the beginning of file, thrashing read cache.
423 * \param fs file structure
425 * \return 0 if success, non-zero on error
430 off_t rv
= lseek (fs
->fd
, 0, SEEK_SET
);
438 /* --------------------------------------------------------------------------------------------- */
441 * Write bytes to file.
443 * \param fs file structure
444 * \param buf source buffer
445 * \param size size of buffer
447 * \return number of written bytes, -1 on error
449 * \note thrashes read cache
452 f_write (FBUF
* fs
, const char *buf
, size_t size
)
454 ssize_t rv
= write (fs
->fd
, buf
, size
);
462 /* --------------------------------------------------------------------------------------------- */
465 * Truncate file to the current position.
467 * \param fs file structure
469 * \return current file size on success, negative on error
471 * \note thrashes read cache
476 off_t off
= lseek (fs
->fd
, 0, SEEK_CUR
);
479 int rv
= ftruncate (fs
->fd
, off
);
492 /* --------------------------------------------------------------------------------------------- */
497 * \param fs file structure
499 * \return 0 on success, non-zero on error
501 * \note if this is temporary file, it is deleted
506 int rv
= close (fs
->fd
);
511 /* --------------------------------------------------------------------------------------------- */
514 * Create pipe stream to process.
516 * \param cmd shell command line
517 * \param flags open mode, either O_RDONLY or O_WRONLY
519 * \return file structure
522 p_open (const char *cmd
, int flags
)
526 const char *type
= NULL
;
528 if (flags
== O_RDONLY
)
532 if (flags
== O_WRONLY
)
548 f
= popen (cmd
, type
);
560 /* --------------------------------------------------------------------------------------------- */
565 * \param fs structure
567 * \return 0 on success, non-zero on error
572 int rv
= pclose (fs
->data
);
578 * Get one char (byte) from string
580 * \param char * str, gboolean * result
582 * \return int as character or 0 and result == FALSE if fail
586 dview_get_byte (char * str
, gboolean
* result
)
594 return (unsigned char) *str
;
599 * Get utf multibyte char from string
601 * \param char * str, int * char_width, gboolean * result
603 * \return int as utf character or 0 and result == FALSE if fail
607 dview_get_utf (char * str
, int * char_width
, gboolean
* result
)
611 gchar
*next_ch
= NULL
;
623 res
= g_utf8_get_char_validated (str
, -1);
633 /* Calculate UTF-8 char width */
634 next_ch
= g_utf8_next_char (str
);
637 width
= next_ch
- str
;
650 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
653 if (text
== NULL
|| text
[0] == '\0')
655 if (g_utf8_validate (text
, -1, NULL
))
657 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
662 char *tmpbuf
, *buffer
;
663 buffer
= tmpbuf
= g_strdup (text
);
664 while (tmpbuf
[0] != '\0')
666 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
667 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
669 tmpbuf
= g_utf8_next_char (tmpbuf
);
678 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
681 return max (length
, (size_t) result
);
685 /* --------------------------------------------------------------------------------------------- */
686 /* diff parse *************************************************************** */
689 * Read decimal number from string.
691 * \param[in,out] str string to parse
692 * \param[out] n extracted number
694 * \return 0 if success, otherwise non-zero
697 scan_deci (const char **str
, int *n
)
699 const char *p
= *str
;
702 *n
= strtol (p
, &q
, 10);
711 /* --------------------------------------------------------------------------------------------- */
714 * Parse line for diff statement.
716 * \param p string to parse
717 * \param ops list of diff statements
719 * \return 0 if success, otherwise non-zero
722 scan_line (const char *p
, GArray
* ops
)
732 /* handle the following cases:
734 * NUM[,NUM]cNUM[,NUM]
736 * where NUM is a positive integer
739 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
748 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
763 else if (cmd
!= 'c' && cmd
!= 'd')
768 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
777 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
797 g_array_append_val (ops
, op
);
801 /* --------------------------------------------------------------------------------------------- */
804 * Parse diff output and extract diff statements.
806 * \param f stream to read from
807 * \param ops list of diff statements to fill
809 * \return positive number indicating number of hunks, otherwise negative
812 scan_diff (FBUF
* f
, GArray
* ops
)
817 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)))
819 if (isdigit (buf
[0]))
821 if (buf
[sz
- 1] != '\n')
826 if (scan_line (buf
, ops
) != 0)
832 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)))
840 /* --------------------------------------------------------------------------------------------- */
843 * Invoke diff and extract diff statements.
845 * \param args extra arguments to be passed to diff
846 * \param extra more arguments to be passed to diff
847 * \param file1 first file to compare
848 * \param file2 second file to compare
849 * \param ops list of diff statements to fill
851 * \return positive number indicating number of hunks, otherwise negative
854 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
857 static const char *opt
=
858 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
859 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
860 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
861 " --unchanged-group-format=''";
868 cmd
= g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args
, extra
, opt
, file1
, file2
);
872 f
= p_open (cmd
, O_RDONLY
);
878 rv
= scan_diff (f
, ops
);
881 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
887 /* --------------------------------------------------------------------------------------------- */
890 * Reparse and display file according to diff statements.
892 * \param ord 0 if displaying first file, 1 if displaying 2nd file
893 * \param filename file name to display
894 * \param ops list of diff statements
895 * \param printer printf-like function to be used for displaying
896 * \param ctx printer context
898 * \return 0 if success, otherwise non-zero
901 dff_reparse (int ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
914 f
= f_open (filename
, O_RDONLY
);
932 #define T1 a[ ord^1 ][0]
933 #define T2 a[ ord^1 ][1]
934 for (i
= 0; i
< ops
->len
; i
++)
937 op
= &g_array_index (ops
, DIFFCMD
, i
);
938 n
= op
->F1
- (op
->cmd
!= add_cmd
);
939 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)))
942 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
944 while (buf
[sz
- 1] != '\n')
946 if (!(sz
= f_gets (buf
, sizeof (buf
), f
)))
948 printer (ctx
, 0, 0, 0, 1, "\n");
951 printer (ctx
, 0, 0, 0, sz
, buf
);
960 if (op
->cmd
== add_cmd
)
962 n
= op
->T2
- op
->T1
+ 1;
965 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
969 if (op
->cmd
== del_cmd
)
971 n
= op
->F2
- op
->F1
+ 1;
972 while (n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)))
975 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
977 while (buf
[sz
- 1] != '\n')
979 if (!(sz
= f_gets (buf
, sizeof (buf
), f
)))
981 printer (ctx
, 0, 0, 0, 1, "\n");
984 printer (ctx
, 0, 0, 0, sz
, buf
);
996 n
= op
->F2
- op
->F1
+ 1;
997 while (n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)))
1000 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
1002 while (buf
[sz
- 1] != '\n')
1004 if (!(sz
= f_gets (buf
, sizeof (buf
), f
)))
1006 printer (ctx
, 0, 0, 0, 1, "\n");
1009 printer (ctx
, 0, 0, 0, sz
, buf
);
1018 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
1021 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
1031 while ((sz
= f_gets (buf
, sizeof (buf
), f
)))
1034 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1036 while (buf
[sz
- 1] != '\n')
1038 if (!(sz
= f_gets (buf
, sizeof (buf
), f
)))
1040 printer (ctx
, 0, 0, 0, 1, "\n");
1043 printer (ctx
, 0, 0, 0, sz
, buf
);
1056 /* --------------------------------------------------------------------------------------------- */
1057 /* horizontal diff ********************************************************** */
1060 * Longest common substring.
1062 * \param s first string
1063 * \param m length of first string
1064 * \param t second string
1065 * \param n length of second string
1066 * \param ret list of offsets for longest common substrings inside each string
1067 * \param min minimum length of common substrings
1069 * \return 0 if success, nonzero otherwise
1072 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1080 if (m
< min
|| n
< min
)
1082 /* XXX early culling */
1086 Lprev
= g_new0 (int, n
+ 1);
1087 Lcurr
= g_new0 (int, n
+ 1);
1089 if (Lprev
== NULL
|| Lcurr
== NULL
)
1096 for (i
= 0; i
< m
; i
++)
1101 #ifdef USE_MEMSET_IN_LCS
1102 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1104 for (j
= 0; j
< n
; j
++)
1106 #ifndef USE_MEMSET_IN_LCS
1111 int v
= Lprev
[j
] + 1;
1116 g_array_set_size (ret
, 0);
1118 if (z
== v
&& z
>= min
)
1120 int off0
= i
- z
+ 1;
1121 int off1
= j
- z
+ 1;
1123 for (k
= 0; k
< ret
->len
; k
++)
1125 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1126 if ((*p
)[0] == off0
)
1130 if ((*p
)[1] == off1
)
1140 g_array_append_val (ret
, p2
);
1156 /* --------------------------------------------------------------------------------------------- */
1159 * Scan recursively for common substrings and build ranges.
1161 * \param s first string
1162 * \param t second string
1163 * \param bracket current limits for both of the strings
1164 * \param min minimum length of common substrings
1165 * \param hdiff list of horizontal diff ranges to fill
1166 * \param depth recursion depth
1168 * \return 0 if success, nonzero otherwise
1171 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1181 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1185 len
= lcsubstr (s
+ bracket
[0].off
, bracket
[0].len
,
1186 t
+ bracket
[1].off
, bracket
[1].len
, ret
, min
);
1190 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1193 b
[0].off
= bracket
[0].off
;
1194 b
[0].len
= (*data
)[0];
1195 b
[1].off
= bracket
[1].off
;
1196 b
[1].len
= (*data
)[1];
1197 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1200 for (k
= 0; k
< ret
->len
- 1; k
++)
1202 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1203 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1204 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1205 b
[0].len
= (*data2
)[0] - (*data
)[0] - len
;
1206 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1207 b
[1].len
= (*data2
)[1] - (*data
)[1] - len
;
1208 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1211 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1212 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1213 b
[0].len
= bracket
[0].len
- (*data
)[0] - len
;
1214 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1215 b
[1].len
= bracket
[1].len
- (*data
)[1] - len
;
1216 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1219 g_array_free (ret
, TRUE
);
1224 p
[0].off
= bracket
[0].off
;
1225 p
[0].len
= bracket
[0].len
;
1226 p
[1].off
= bracket
[1].off
;
1227 p
[1].len
= bracket
[1].len
;
1228 g_array_append_val (hdiff
, p
);
1233 /* --------------------------------------------------------------------------------------------- */
1236 * Build list of horizontal diff ranges.
1238 * \param s first string
1239 * \param m length of first string
1240 * \param t second string
1241 * \param n length of second string
1242 * \param min minimum length of common substrings
1243 * \param hdiff list of horizontal diff ranges to fill
1244 * \param depth recursion depth
1246 * \return 0 if success, nonzero otherwise
1249 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1254 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1255 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++);
1256 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--);
1263 /* smartscan (multiple horizontal diff) */
1264 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1267 /* --------------------------------------------------------------------------------------------- */
1268 /* read line **************************************************************** */
1271 * Check if character is inside horizontal diff limits.
1273 * \param k rank of character inside line
1274 * \param hdiff horizontal diff structure
1275 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1277 * \return TRUE if inside hdiff limits, FALSE otherwise
1280 is_inside (int k
, GArray
* hdiff
, int ord
)
1284 for (i
= 0; i
< hdiff
->len
; i
++)
1287 b
= &g_array_index (hdiff
, BRACKET
, i
);
1289 start
= (*b
)[ord
].off
;
1290 end
= start
+ (*b
)[ord
].len
;
1291 if (k
>= start
&& k
< end
)
1299 /* --------------------------------------------------------------------------------------------- */
1302 * Copy `src' to `dst' expanding tabs.
1304 * \param dst destination buffer
1305 * \param src source buffer
1306 * \param srcsize size of src buffer
1307 * \param base virtual base of this string, needed to calculate tabs
1308 * \param ts tab size
1310 * \return new virtual base
1312 * \note The procedure returns when all bytes are consumed from `src'
1315 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1318 for (i
= 0; srcsize
; i
++, src
++, dst
++, srcsize
--)
1323 int j
= TAB_SKIP (ts
, i
+ base
);
1335 /* --------------------------------------------------------------------------------------------- */
1338 * Copy `src' to `dst' expanding tabs.
1340 * \param dst destination buffer
1341 * \param dstsize size of dst buffer
1342 * \param[in,out] _src source buffer
1343 * \param srcsize size of src buffer
1344 * \param base virtual base of this string, needed to calculate tabs
1345 * \param ts tab size
1347 * \return new virtual base
1349 * \note The procedure returns when all bytes are consumed from `src'
1350 * or `dstsize' bytes are written to `dst'
1351 * \note Upon return, `src' points to the first unwritten character in source
1354 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1357 const char *src
= *_src
;
1358 for (i
= 0; i
< dstsize
&& srcsize
; i
++, src
++, dst
++, srcsize
--)
1363 int j
= TAB_SKIP (ts
, i
+ base
);
1364 if (j
> dstsize
- i
)
1380 /* --------------------------------------------------------------------------------------------- */
1383 * Read line from memory, converting tabs to spaces and padding with spaces.
1385 * \param src buffer to read from
1386 * \param srcsize size of src buffer
1387 * \param dst buffer to read to
1388 * \param dstsize size of dst buffer, excluding trailing null
1389 * \param skip number of characters to skip
1390 * \param ts tab size
1391 * \param show_cr show trailing carriage return as ^M
1393 * \return negative on error, otherwise number of bytes except padding
1396 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1404 for (i
= 0; dstsize
&& srcsize
&& *src
!= '\n'; i
++, src
++, srcsize
--)
1408 int j
= TAB_SKIP (ts
, i
+ base
);
1423 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1425 if (skip
== 0 && show_cr
)
1449 utf_ch
= dview_get_utf ((char *)src
, &w
, &res
);
1452 if (!g_unichar_isprint (utf_ch
))
1473 /* --------------------------------------------------------------------------------------------- */
1476 * Read line from memory and build attribute array.
1478 * \param src buffer to read from
1479 * \param srcsize size of src buffer
1480 * \param dst buffer to read to
1481 * \param dstsize size of dst buffer, excluding trailing null
1482 * \param skip number of characters to skip
1483 * \param ts tab size
1484 * \param show_cr show trailing carriage return as ^M
1485 * \param hdiff horizontal diff structure
1486 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1487 * \param att buffer of attributes
1489 * \return negative on error, otherwise number of bytes except padding
1492 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1493 GArray
* hdiff
, int ord
, char *att
)
1501 for (i
= 0, k
= 0; dstsize
&& srcsize
&& *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1505 int j
= TAB_SKIP (ts
, i
+ base
);
1516 *att
++ = is_inside (k
, hdiff
, ord
);
1521 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1523 if (!skip
&& show_cr
)
1528 *att
++ = is_inside (k
, hdiff
, ord
);
1530 *att
++ = is_inside (k
, hdiff
, ord
);
1536 *att
++ = is_inside (k
, hdiff
, ord
);
1550 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1553 if (!g_unichar_isprint (utf_ch
))
1559 *att
++ = is_inside (k
, hdiff
, ord
);
1576 /* --------------------------------------------------------------------------------------------- */
1579 * Read line from file, converting tabs to spaces and padding with spaces.
1581 * \param f file stream to read from
1582 * \param off offset of line inside file
1583 * \param dst buffer to read to
1584 * \param dstsize size of dst buffer, excluding trailing null
1585 * \param skip number of characters to skip
1586 * \param ts tab size
1587 * \param show_cr show trailing carriage return as ^M
1589 * \return negative on error, otherwise number of bytes except padding
1592 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1595 int old_base
= base
;
1596 const int amount
= dstsize
;
1598 size_t useful
, offset
;
1605 const char *q
= NULL
;
1606 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1607 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1609 if ((int) sizeof (tmp
) < amount
|| (int) sizeof (tmp
) <= dstsize
1610 || (int) sizeof (cvt
) < 8 * amount
)
1612 /* abnormal, but avoid buffer overflow */
1613 memset (dst
, ' ', dstsize
);
1614 dst
[dstsize
] = '\0';
1618 f_seek (f
, off
, SEEK_SET
);
1623 if (!(sz
= f_gets (tmp
, amount
, f
)))
1627 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1628 if (cvt
[base
- old_base
- 1] == '\n')
1630 q
= &cvt
[base
- old_base
- 1];
1631 base
= old_base
+ q
- cvt
+ 1;
1636 useful
= base
- skip
;
1637 offset
= skip
- old_base
;
1641 memset (dst
, ' ', dstsize
);
1642 dst
[dstsize
] = '\0';
1646 if (useful
<= dstsize
)
1650 memmove (dst
, cvt
+ offset
, useful
);
1652 if (q
== NULL
&& (sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
)))
1654 const char *ptr
= tmp
;
1655 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1665 memmove (dst
, cvt
+ offset
, dstsize
);
1667 lastch
= cvt
[offset
+ dstsize
];
1671 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1673 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1677 if (i
+ 1 < dstsize
)
1690 for (; i
< dstsize
; i
++)
1698 /* --------------------------------------------------------------------------------------------- */
1699 /* diff printers et al ****************************************************** */
1702 cc_free_elt (void *elt
)
1711 /* --------------------------------------------------------------------------------------------- */
1714 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1716 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1717 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1725 if (dsrc
== DATA_SRC_MEM
&& line
)
1727 if (sz
&& str
[sz
- 1] == '\n')
1732 p
.p
= g_strndup (str
, sz
);
1735 g_array_append_val (a
, p
);
1737 else if (dsrc
== DATA_SRC_MEM
)
1740 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1741 if (sz
&& str
[sz
- 1] == '\n')
1747 size_t new_size
= p
->u
.len
+ sz
;
1748 char *q
= g_realloc (p
->p
, new_size
);
1749 memcpy (q
+ p
->u
.len
, str
, sz
);
1754 if (dsrc
== DATA_SRC_TMP
&& (line
|| !ch
))
1756 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1757 f_write (f
, str
, sz
);
1762 /* --------------------------------------------------------------------------------------------- */
1765 redo_diff (WDiff
* dview
)
1767 FBUF
*const *f
= dview
->f
;
1777 if (dview
->opt
.quality
== 2)
1779 strcat (extra
, " -d");
1781 if (dview
->opt
.quality
== 1)
1783 strcat (extra
, " --speed-large-files");
1785 if (dview
->opt
.strip_trailing_cr
)
1787 strcat (extra
, " --strip-trailing-cr");
1789 if (dview
->opt
.ignore_tab_expansion
)
1791 strcat (extra
, " -E");
1793 if (dview
->opt
.ignore_space_change
)
1795 strcat (extra
, " -b");
1797 if (dview
->opt
.ignore_all_space
)
1799 strcat (extra
, " -w");
1801 if (dview
->opt
.ignore_case
)
1803 strcat (extra
, " -i");
1806 if (dview
->dsrc
!= DATA_SRC_MEM
)
1812 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1813 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[0], dview
->file
[1], ops
);
1816 g_array_free (ops
, TRUE
);
1820 ctx
.dsrc
= dview
->dsrc
;
1823 ctx
.a
= dview
->a
[0];
1825 rv
|= dff_reparse (0, dview
->file
[0], ops
, printer
, &ctx
);
1827 ctx
.a
= dview
->a
[1];
1829 rv
|= dff_reparse (1, dview
->file
[1], ops
, printer
, &ctx
);
1831 g_array_free (ops
, TRUE
);
1833 if (rv
!= 0 || dview
->a
[0]->len
!= dview
->a
[1]->len
)
1836 if (dview
->dsrc
== DATA_SRC_TMP
)
1842 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1844 dview
->hdiff
= g_ptr_array_new ();
1845 if (dview
->hdiff
!= NULL
)
1850 for (i
= 0; i
< dview
->a
[0]->len
; i
++)
1853 p
= &g_array_index (dview
->a
[0], DIFFLN
, i
);
1854 q
= &g_array_index (dview
->a
[1], DIFFLN
, i
);
1855 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1857 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1860 gboolean runresult
=
1861 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1865 g_array_free (h
, TRUE
);
1870 g_ptr_array_add (dview
->hdiff
, h
);
1877 /* --------------------------------------------------------------------------------------------- */
1880 destroy_hdiff (WDiff
* dview
)
1882 if (dview
->hdiff
!= NULL
)
1885 int len
= dview
->a
[0]->len
;
1886 for (i
= 0; i
< len
; i
++)
1888 GArray
*h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1891 g_array_free (h
, TRUE
);
1894 g_ptr_array_free (dview
->hdiff
, TRUE
);
1895 dview
->hdiff
= NULL
;
1898 mc_search_free (dview
->search
.handle
);
1899 dview
->search
.handle
= NULL
;
1900 g_free (dview
->search
.last_string
);
1901 dview
->search
.last_string
= NULL
;
1904 /* --------------------------------------------------------------------------------------------- */
1905 /* stuff ******************************************************************** */
1908 get_digits (unsigned int n
)
1918 /* --------------------------------------------------------------------------------------------- */
1921 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1935 p
= &g_array_index (a
, DIFFLN
, pos
);
1940 for (n
= pos
; n
> 0; n
--)
1948 *lineofs
= pos
- n
+ 1;
1956 /* --------------------------------------------------------------------------------------------- */
1959 calc_nwidth (const GArray
** const a
)
1963 get_line_numbers (a
[0], a
[0]->len
- 1, &l1
, &o1
);
1964 get_line_numbers (a
[1], a
[1]->len
- 1, &l2
, &o2
);
1969 return get_digits (l1
);
1972 /* --------------------------------------------------------------------------------------------- */
1975 find_prev_hunk (const GArray
* a
, int pos
)
1978 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1982 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1986 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1990 if (pos
> 0 && (size_t) pos
< a
->len
)
1993 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1997 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
2006 /* --------------------------------------------------------------------------------------------- */
2009 find_next_hunk (const GArray
* a
, size_t pos
)
2011 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2015 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
2023 * Find start and end lines of the current hunk.
2025 * \param dview - widget WDiff
2026 * \return boolean and
2027 * start_line1 first line of current hunk (file[0])
2028 * end_line1 last line of current hunk (file[0])
2029 * start_line1 first line of current hunk (file[0])
2030 * end_line1 last line of current hunk (file[0])
2033 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
2035 const GArray
*a0
= dview
->a
[0];
2036 const GArray
*a1
= dview
->a
[1];
2046 pos
= dview
->skip_rows
;
2047 ch
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
;
2062 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2068 *start_line1
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2069 *start_line2
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2071 pos
= dview
->skip_rows
;
2072 while (pos
< a0
->len
&& ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2075 l0
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
;
2076 l1
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
;
2078 *end_line1
= max (*start_line1
, l0
);
2080 *end_line2
= max (*start_line2
, l1
);
2088 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
)
2093 f0
= fopen (dview
->file
[0], "r");
2095 while (fgets (buf
, sizeof (buf
), f0
) && line
< from1
- 1)
2098 fputs (buf
, merge_file
);
2100 while (fgets (buf
, sizeof (buf
), f0
))
2104 fputs (buf
, merge_file
);
2110 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
)
2116 f0
= fopen (dview
->file
[0], "r");
2117 f1
= fopen (dview
->file
[1], "r");
2119 while (fgets (buf
, sizeof (buf
), f0
) && line
< from1
- 1)
2122 fputs (buf
, merge_file
);
2125 while (fgets (buf
, sizeof (buf
), f1
) && line
<= to2
)
2129 fputs (buf
, merge_file
);
2131 while (fgets (buf
, sizeof (buf
), f0
))
2133 fputs (buf
, merge_file
);
2140 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
)
2146 f0
= fopen (dview
->file
[0], "r");
2147 f1
= fopen (dview
->file
[1], "r");
2149 while (fgets (buf
, sizeof (buf
), f0
) && line1
< from1
- 1)
2152 fputs (buf
, merge_file
);
2155 while (fgets (buf
, sizeof (buf
), f1
) && line2
<= to2
)
2159 fputs (buf
, merge_file
);
2161 while (fgets (buf
, sizeof (buf
), f0
))
2165 fputs (buf
, merge_file
);
2172 do_merge_hunk (WDiff
* dview
)
2174 int from1
, to1
, from2
, to2
;
2178 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2183 char *merge_file_name
= NULL
;
2187 dview
->merged
= mc_util_make_backup_if_possible (dview
->file
[0], "~~~");
2190 message (D_ERROR
, MSG_ERROR
,
2191 _(" Cannot create backup file \n %s%s \n %s "),
2192 dview
->file
[0], "~~~", unix_error_string (errno
));
2198 merge_file_fd
= mc_mkstemps (&merge_file_name
, "mcmerge", NULL
);
2199 if (merge_file_fd
== -1)
2201 message (D_ERROR
, MSG_ERROR
,
2202 _(" Cannot create temporary merge file \n %s "),
2203 unix_error_string (errno
));
2207 merge_file
= fdopen (merge_file_fd
, "w");
2212 dview_remove_hunk (dview
, merge_file
, from1
, to1
);
2215 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
);
2218 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
);
2221 fflush (merge_file
);
2222 fclose (merge_file
);
2223 res
= rewrite_backup_content (merge_file_name
, dview
->file
[0]);
2224 unlink (merge_file_name
);
2225 g_free (merge_file_name
);
2229 /* --------------------------------------------------------------------------------------------- */
2230 /* view routines and callbacks ********************************************** */
2233 dview_compute_split (WDiff
* dview
, int i
)
2236 if (dview
->bias
< 2 - dview
->half1
)
2238 dview
->bias
= 2 - dview
->half1
;
2240 if (dview
->bias
> dview
->half2
- 2)
2242 dview
->bias
= dview
->half2
- 2;
2246 /* --------------------------------------------------------------------------------------------- */
2249 dview_compute_areas (WDiff
* dview
)
2251 dview
->height
= LINES
- 2;
2252 dview
->half1
= COLS
/ 2;
2253 dview
->half2
= COLS
- dview
->half1
;
2255 dview_compute_split (dview
, 0);
2258 /* --------------------------------------------------------------------------------------------- */
2261 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2262 const char *label1
, const char *label2
, DSRC dsrc
)
2270 if (dsrc
== DATA_SRC_TMP
)
2284 if (dsrc
== DATA_SRC_ORG
)
2286 f
[0] = f_open (file1
, O_RDONLY
);
2291 f
[1] = f_open (file2
, O_RDONLY
);
2300 dview
->file
[0] = file1
;
2301 dview
->file
[1] = file2
;
2302 dview
->label
[0] = label1
;
2303 dview
->label
[1] = label2
;
2306 dview
->hdiff
= NULL
;
2308 dview
->converter
= str_cnv_from_term
;
2309 dview_set_codeset (dview
);
2311 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2312 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2314 ndiff
= redo_diff (dview
);
2317 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2318 g_array_free (dview
->a
[0], TRUE
);
2319 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2320 g_array_free (dview
->a
[1], TRUE
);
2324 dview
->ndiff
= ndiff
;
2326 dview
->view_quit
= 0;
2329 dview
->new_frame
= 1;
2330 dview
->skip_rows
= 0;
2331 dview
->skip_cols
= 0;
2332 dview
->display_symbols
= 0;
2333 dview
->display_numbers
= 0;
2335 dview
->tab_size
= 8;
2339 dview
->search
.handle
=NULL
;
2340 dview
->search
.last_string
=NULL
;
2341 dview
->search
.last_found_line
= -1;
2342 dview
->search
.last_accessed_num_line
= 0;
2345 dview
->opt
.quality
= 0;
2346 dview
->opt
.strip_trailing_cr
= 0;
2347 dview
->opt
.ignore_tab_expansion
= 0;
2348 dview
->opt
.ignore_space_change
= 0;
2349 dview
->opt
.ignore_all_space
= 0;
2350 dview
->opt
.ignore_case
= 0;
2352 dview_compute_areas (dview
);
2357 if (dsrc
!= DATA_SRC_MEM
)
2366 /* --------------------------------------------------------------------------------------------- */
2369 dview_reread (WDiff
* dview
)
2371 int ndiff
= dview
->ndiff
;
2372 destroy_hdiff (dview
);
2374 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2375 g_array_free (dview
->a
[0], TRUE
);
2376 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2377 g_array_free (dview
->a
[1], TRUE
);
2379 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2380 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2382 ndiff
= redo_diff (dview
);
2385 dview
->ndiff
= ndiff
;
2389 /* --------------------------------------------------------------------------------------------- */
2392 dview_reinit (WDiff
* dview
)
2394 const char *quality_str
[] = {
2396 N_("&Fastest (Assume large files)"),
2397 N_("&Minimal (Find a smaller set of change)")
2400 QuickWidget diffopt_widgets
[] = {
2401 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2402 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2404 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2405 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2406 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2407 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2408 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2409 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2410 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2411 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2412 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2413 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2414 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2415 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2416 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2417 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2422 QuickDialog diffopt
= {
2424 N_("Diff Options"), "[Diff Options]",
2428 if (quick_dialog (&diffopt
) != B_CANCEL
)
2430 dview_reread (dview
);
2434 /* --------------------------------------------------------------------------------------------- */
2437 dview_fini (WDiff
* dview
)
2439 if (dview
->dsrc
!= DATA_SRC_MEM
)
2441 f_close (dview
->f
[1]);
2442 f_close (dview
->f
[0]);
2445 if (dview
->converter
!= str_cnv_from_term
)
2446 str_close_conv (dview
->converter
);
2448 destroy_hdiff (dview
);
2449 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2450 g_array_free (dview
->a
[0], TRUE
);
2451 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2452 g_array_free (dview
->a
[1], TRUE
);
2458 /* --------------------------------------------------------------------------------------------- */
2461 dview_display_file (const WDiff
* dview
, int ord
, int r
, int c
, int height
, int width
)
2466 FBUF
*f
= dview
->f
[ord
];
2467 int skip
= dview
->skip_cols
;
2468 int display_symbols
= dview
->display_symbols
;
2469 int display_numbers
= dview
->display_numbers
;
2470 int show_cr
= dview
->show_cr
;
2471 int tab_size
= dview
->tab_size
;
2473 int nwidth
= display_numbers
;
2474 int xwidth
= display_symbols
+ display_numbers
;
2477 if (xwidth
> width
&& display_symbols
)
2480 display_symbols
= 0;
2482 if (xwidth
> width
&& display_numbers
)
2485 display_numbers
= width
;
2499 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2501 /* abnormal, but avoid buffer overflow */
2505 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2507 int ch
, next_ch
, col
;
2509 p
= (DIFFLN
*) &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2511 tty_setcolor (NORMAL_COLOR
);
2512 if (display_symbols
)
2514 tty_gotoyx (r
+ j
, c
- 2);
2515 tty_print_char (ch
);
2519 if (display_numbers
)
2521 tty_gotoyx (r
+ j
, c
- xwidth
);
2522 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2523 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2527 tty_setcolor (DFF_ADD_COLOR
);
2531 tty_setcolor (DFF_CHG_COLOR
);
2535 if (i
== (size_t) dview
->search
.last_found_line
)
2537 tty_setcolor (MARKED_SELECTED_COLOR
);
2541 if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2545 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2548 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2549 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2550 tty_gotoyx (r
+ j
, c
);
2552 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2558 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2561 if (!g_unichar_isprint (next_ch
))
2565 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2568 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2572 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2574 } else if (dview
->utf8
)
2575 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2578 next_ch
= convert_to_display_c (next_ch
);
2580 tty_print_anychar (next_ch
);
2586 else if (ch
== CHG_CH
)
2588 tty_setcolor (DFF_CHH_COLOR
);
2592 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2595 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2599 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2604 if (display_numbers
)
2606 tty_gotoyx (r
+ j
, c
- xwidth
);
2607 memset (buf
, ' ', display_numbers
);
2608 buf
[display_numbers
] = '\0';
2609 tty_print_string (buf
);
2613 tty_setcolor (DFF_DEL_COLOR
);
2617 tty_setcolor (DFF_CHD_COLOR
);
2619 memset (buf
, ' ', width
);
2622 tty_gotoyx (r
+ j
, c
);
2623 /* tty_print_nstring (buf, width); */
2625 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2631 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2634 if (!g_unichar_isprint (next_ch
))
2638 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2644 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2646 } else if (dview
->utf8
)
2647 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2650 next_ch
= convert_to_display_c (next_ch
);
2652 tty_print_anychar (next_ch
);
2658 tty_setcolor (NORMAL_COLOR
);
2660 if (width
< xwidth
- 1)
2664 memset (buf
, ' ', k
);
2666 for (; j
< height
; j
++)
2670 tty_gotoyx (r
+ j
, c
- xwidth
);
2671 /* tty_print_nstring (buf, xwidth - 1); */
2672 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2674 tty_gotoyx (r
+ j
, c
);
2675 /* tty_print_nstring (buf, width); */
2676 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2682 /* --------------------------------------------------------------------------------------------- */
2685 dview_status (const WDiff
* dview
, int ord
, int width
, int c
)
2687 int skip_rows
= dview
->skip_rows
;
2688 int skip_cols
= dview
->skip_cols
;
2692 int linenum
, lineofs
;
2694 tty_setcolor (SELECTED_COLOR
);
2697 get_line_numbers (dview
->a
[ord
], skip_rows
, &linenum
, &lineofs
);
2699 filename_width
= width
- 22;
2700 if (filename_width
< 8)
2704 if (filename_width
>= (int) sizeof (buf
))
2706 /* abnormal, but avoid buffer overflow */
2707 filename_width
= sizeof (buf
) - 1;
2709 trim (strip_home_and_password (dview
->label
[ord
]), buf
, filename_width
);
2712 tty_printf ("%-*s %6d+%-4d Col %-4d ", filename_width
, buf
, linenum
, lineofs
, skip_cols
);
2716 tty_printf ("%-*s %6d+%-4d Dif %-4d ", filename_width
, buf
, linenum
, lineofs
, dview
->ndiff
);
2720 /* --------------------------------------------------------------------------------------------- */
2723 dview_redo (WDiff
* dview
)
2725 if (dview
->display_numbers
)
2727 int old
= dview
->display_numbers
;
2728 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2729 dview
->new_frame
= (old
!= dview
->display_numbers
);
2731 dview_reread (dview
);
2734 /* --------------------------------------------------------------------------------------------- */
2737 dview_edit (WDiff
* dview
, int ord
)
2739 int linenum
, lineofs
;
2741 if (dview
->dsrc
== DATA_SRC_TMP
)
2743 error_dialog (_("Edit"), _(" Edit is disabled "));
2747 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2748 do_edit_at_line (dview
->file
[ord
], linenum
);
2750 dview_update (dview
);
2753 /* --------------------------------------------------------------------------------------------- */
2756 dview_goto_cmd (WDiff
* dview
, int ord
)
2758 static const char *title
[2] = { " Goto line (left) ", " Goto line (right) " };
2759 static char prev
[256];
2760 /* XXX some statics here, to be remembered between runs */
2765 input
= input_dialog (_(title
[ord
]), _(" Enter line: "), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2768 const char *s
= input
;
2769 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2775 for (; i
< dview
->a
[ord
]->len
; i
++)
2777 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2778 if (p
->line
== newline
)
2784 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= i
;
2785 snprintf (prev
, sizeof (prev
), "%d", newline
);
2791 /* --------------------------------------------------------------------------------------------- */
2794 dview_labels (WDiff
* dview
)
2796 Dlg_head
*h
= dview
->widget
.parent
;
2797 WButtonBar
*b
= find_buttonbar (h
);
2799 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2800 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2801 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2802 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2803 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2804 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2808 /* --------------------------------------------------------------------------------------------- */
2811 dview_event (Gpm_Event
* event
, void *x
)
2813 WDiff
*dview
= (WDiff
*) x
;
2814 int result
= MOU_NORMAL
;
2816 /* We are not interested in the release events */
2817 if (!(event
->type
& (GPM_DOWN
| GPM_DRAG
)))
2823 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
))
2825 dview
->skip_rows
-= 2;
2826 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2827 dview_update (dview
);
2830 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
))
2832 dview
->skip_rows
+= 2;
2833 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2834 dview_update (dview
);
2842 dview_save (WDiff
* dview
)
2844 gboolean res
= TRUE
;
2847 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2848 dview
->merged
= !res
;
2853 dview_do_save (WDiff
* dview
)
2855 (void) dview_save (dview
);
2859 dview_save_options (WDiff
* dview
)
2861 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
2862 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
2863 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
2864 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
2865 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
2867 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
2869 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
2870 dview
->opt
.strip_trailing_cr
);
2871 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
2872 dview
->opt
.ignore_all_space
);
2873 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
2874 dview
->opt
.ignore_space_change
);
2875 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
2876 dview
->opt
.ignore_tab_expansion
);
2877 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
2881 dview_load_options (WDiff
* dview
)
2883 gboolean show_numbers
, show_symbols
;
2885 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
2887 dview
->display_symbols
= 1;
2888 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
2890 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
2891 dview
->tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
2893 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
2895 dview
->opt
.strip_trailing_cr
=
2896 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
2897 dview
->opt
.ignore_all_space
=
2898 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
2899 dview
->opt
.ignore_space_change
=
2900 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
2901 dview
->opt
.ignore_tab_expansion
=
2902 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
2903 dview
->opt
.ignore_case
=
2904 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
2906 dview
->new_frame
= 1;
2910 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2914 dview_ok_to_exit (WDiff
* dview
)
2916 gboolean res
= TRUE
;
2919 switch (query_dialog
2920 (_("Quit"), _(" File was modified, Save with exit? "), D_NORMAL
, 2, _("&Yes"), _("&No")))
2927 (void) dview_save (dview
);
2931 if (mc_util_restore_from_backup_if_possible (dview
->file
[0], "~~~"))
2932 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2938 /* --------------------------------------------------------------------------------------------- */
2941 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
2943 cb_ret_t res
= MSG_HANDLED
;
2947 interactive_display (NULL
, "[Diff Viewer]");
2949 case CK_DiffDisplaySymbols
:
2950 dview
->display_symbols
^= 1;
2951 dview
->new_frame
= 1;
2953 case CK_DiffDisplayNumbers
:
2954 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
2955 dview
->new_frame
= 1;
2959 dview
->new_frame
= 1;
2965 dview
->new_frame
= 1;
2968 case CK_DiffSplitMore
:
2971 dview_compute_split (dview
, 1);
2972 dview
->new_frame
= 1;
2976 case CK_DiffSplitLess
:
2979 dview_compute_split (dview
, -1);
2980 dview
->new_frame
= 1;
2983 case CK_DiffSetTab2
:
2984 dview
->tab_size
= 2;
2986 case CK_DiffSetTab3
:
2987 dview
->tab_size
= 3;
2989 case CK_DiffSetTab4
:
2990 dview
->tab_size
= 4;
2992 case CK_DiffSetTab8
:
2993 dview
->tab_size
= 8;
2995 case CK_DiffSwapPanel
:
3001 case CK_DiffNextHunk
:
3002 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_next_hunk (dview
->a
[0], dview
->skip_rows
);
3004 case CK_DiffPrevHunk
:
3005 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_prev_hunk (dview
->a
[0], dview
->skip_rows
);
3008 dview_goto_cmd (dview
, TRUE
);
3010 case CK_DiffEditCurrent
:
3011 dview_edit (dview
, dview
->ord
);
3013 case CK_DiffMergeCurrentHunk
:
3014 do_merge_hunk (dview
);
3017 case CK_DiffEditOther
:
3018 dview_edit (dview
, dview
->ord
^ 1);
3021 dview_search_cmd (dview
);
3023 case CK_DiffContinueSearch
:
3024 dview_continue_search_cmd (dview
);
3027 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3030 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[0]->len
- 1;
3034 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3038 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3040 case CK_DiffPageDown
:
3041 dview
->skip_rows
+= dview
->height
- 2;
3042 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3045 dview
->skip_rows
-= dview
->height
- 2;
3046 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3054 case CK_DiffQuickLeft
:
3055 dview
->skip_cols
-= 8;
3057 case CK_DiffQuickRight
:
3058 dview
->skip_cols
+= 8;
3061 dview
->skip_cols
= 0;
3063 case CK_ShowCommandLine
:
3067 dview
->view_quit
= 1;
3070 dview_do_save (dview
);
3072 case CK_SelectCodepage
:
3073 dview_select_encoding (dview
);
3074 dview_reread (dview
);
3075 tty_touch_screen ();
3079 res
= MSG_NOT_HANDLED
;
3084 /* --------------------------------------------------------------------------------------------- */
3087 dview_handle_key (WDiff
* dview
, int key
)
3089 unsigned long command
;
3091 key
= convert_from_input_c (key
);
3093 command
= lookup_keymap_command (diff_map
, key
);
3094 if ((command
!= CK_Ignore_Key
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3098 return MSG_NOT_HANDLED
;
3101 /* --------------------------------------------------------------------------------------------- */
3104 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3106 WDiff
*dview
= (WDiff
*) w
;
3107 Dlg_head
*h
= dview
->widget
.parent
;
3113 dview_labels (dview
);
3114 dview_load_options (dview
);
3115 dview_update (dview
);
3119 dview
->new_frame
= 1;
3120 dview_update (dview
);
3124 i
= dview_handle_key (dview
, parm
);
3125 if (dview
->view_quit
)
3128 dview_update (dview
);
3131 case WIDGET_COMMAND
:
3132 i
= dview_execute_cmd (dview
, parm
);
3133 if (dview
->view_quit
)
3136 dview_update (dview
);
3139 case WIDGET_DESTROY
:
3140 dview_save_options (dview
);
3145 return default_proc (msg
, parm
);
3149 /* --------------------------------------------------------------------------------------------- */
3152 dview_adjust_size (Dlg_head
* h
)
3157 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3158 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3159 bar
= find_buttonbar (h
);
3160 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3161 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3163 dview_compute_areas (dview
);
3166 /* --------------------------------------------------------------------------------------------- */
3169 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3171 WDiff
*dview
= (WDiff
*) data
;
3176 dview_adjust_size (h
);
3180 /* command from buttonbar */
3181 return send_message ((Widget
*) dview
, WIDGET_COMMAND
, parm
);
3184 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3185 if (!dview_ok_to_exit (dview
))
3190 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3194 /*** public functions ****************************************************************************/
3195 /* --------------------------------------------------------------------------------------------- */
3198 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3203 Dlg_head
*dview_dlg
;
3205 /* Create dialog and widgets, put them on the dialog */
3207 create_dlg (0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
,
3208 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3210 dview
= g_new0 (WDiff
, 1);
3212 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3213 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3215 widget_want_cursor (dview
->widget
, 0);
3217 bar
= buttonbar_new (1);
3219 add_widget (dview_dlg
, dview
);
3220 add_widget (dview_dlg
, bar
);
3222 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3224 /* Please note that if you add another widget,
3225 * you have to modify dview_adjust_size to
3229 run_dlg (dview_dlg
);
3230 destroy_dlg (dview_dlg
);
3235 /* --------------------------------------------------------------------------------------------- */
3237 #define GET_FILE_AND_STAMP(n) \
3240 real_file##n = file##n; \
3241 if (!vfs_file_is_local(file##n)) { \
3242 real_file##n = mc_getlocalcopy(file##n); \
3243 if (real_file##n != NULL) { \
3245 if (mc_stat(real_file##n, &st##n) != 0) { \
3251 #define UNGET_FILE(n) \
3253 if (use_copy##n) { \
3255 if (use_copy##n > 0) { \
3256 time_t mtime = st##n.st_mtime; \
3257 if (mc_stat(real_file##n, &st##n) == 0) { \
3258 changed = (mtime != st##n.st_mtime); \
3261 mc_ungetlocalcopy(file##n, real_file##n, changed); \
3262 g_free(real_file##n); \
3267 dview_diff_cmd (void)
3275 if (mc_run_mode
== MC_RUN_FULL
)
3277 const WPanel
*panel0
= current_panel
;
3278 const WPanel
*panel1
= other_panel
;
3279 if (get_current_index ())
3281 panel0
= other_panel
;
3282 panel1
= current_panel
;
3284 file0
= concat_dir_and_file (panel0
->cwd
, selection (panel0
)->fname
);
3285 file1
= concat_dir_and_file (panel1
->cwd
, selection (panel1
)->fname
);
3286 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3287 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3293 if (file0
!= NULL
&& !is_dir0
&& file1
!= NULL
&& !is_dir1
)
3301 GET_FILE_AND_STAMP (0);
3302 GET_FILE_AND_STAMP (1);
3303 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3305 rv
= diff_view (real_file0
, real_file1
, file0
, file1
);
3316 message (1, MSG_ERROR
, _("Two files are needed to compare"));
3319 /* --------------------------------------------------------------------------------------------- */
3322 dview_update (WDiff
* dview
)
3324 int height
= dview
->height
;
3328 int last
= dview
->a
[0]->len
- 1;
3330 if (dview
->skip_rows
> last
)
3332 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
3334 if (dview
->skip_rows
< 0)
3336 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3338 if (dview
->skip_cols
< 0)
3340 dview
->skip_cols
= 0;
3348 width1
= dview
->half1
+ dview
->bias
;
3349 width2
= dview
->half2
- dview
->bias
;
3356 if (dview
->new_frame
)
3358 int xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
3360 tty_setcolor (NORMAL_COLOR
);
3363 tty_draw_box (1, 0, height
, width1
, FALSE
);
3367 tty_draw_box (1, width1
, height
, width2
, FALSE
);
3373 if (xwidth
< width1
- 1)
3375 tty_gotoyx (1, xwidth
);
3376 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3377 tty_gotoyx (height
, xwidth
);
3378 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3379 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3381 if (xwidth
< width2
- 1)
3383 tty_gotoyx (1, width1
+ xwidth
);
3384 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3385 tty_gotoyx (height
, width1
+ xwidth
);
3386 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3387 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3390 dview
->new_frame
= 0;
3395 dview_status (dview
, dview
->ord
, width1
, 0);
3396 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
3400 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
3401 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
3405 /* --------------------------------------------------------------------------------------------- */