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 /*** file scope macro definitions ****************************************************************/
63 #define g_array_foreach(a, TP, cbf) \
65 size_t g_array_foreach_i;\
66 TP *g_array_foreach_var=NULL; \
67 for (g_array_foreach_i=0;g_array_foreach_i < a->len; g_array_foreach_i++) \
69 g_array_foreach_var = &g_array_index(a,TP,g_array_foreach_i); \
70 (*cbf) (g_array_foreach_var); \
74 #define FILE_READ_BUF 4096
75 #define FILE_FLAG_TEMP (1 << 0)
85 #define HDIFF_ENABLE 1
86 #define HDIFF_MINCTX 5
87 #define HDIFF_DEPTH 10
89 #define TAB_SKIP(ts, pos) ((ts) - (pos) % (ts))
91 #define FILE_DIRTY(fs) \
97 /*** file scope type declarations ****************************************************************/
99 /*** file scope variables ************************************************************************/
101 /*** file scope functions ************************************************************************/
103 /* --------------------------------------------------------------------------------------------- */
106 dview_set_codeset (WDiff
* dview
)
109 const char *encoding_id
= NULL
;
112 encoding_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
113 if (encoding_id
!= NULL
)
116 conv
= str_crt_conv_from (encoding_id
);
117 if (conv
!= INVALID_CONV
)
119 if (dview
->converter
!= str_cnv_from_term
)
120 str_close_conv (dview
->converter
);
121 dview
->converter
= conv
;
123 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
130 /* --------------------------------------------------------------------------------------------- */
133 dview_select_encoding (WDiff
* dview
)
136 if (do_select_codepage ())
137 dview_set_codeset (dview
);
145 rewrite_backup_content (const char *from_file_name
, const char *to_file_name
)
151 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
154 backup_fd
= fopen (to_file_name
, "w");
155 if (backup_fd
== NULL
)
161 fwrite ((const void *) contents
, length
, 1, backup_fd
);
169 /* buffered I/O ************************************************************* */
172 * Try to open a temporary file.
174 * \param[out] name address of a pointer to store the temporary name
176 * \return file descriptor on success, negative on error
178 * \note the name is not altered if this function fails
182 open_temp (void **name
)
185 char *diff_file_name
= NULL
;
187 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
190 message (D_ERROR
, MSG_ERROR
,
191 _(" Cannot create temporary diff file \n %s "),
192 unix_error_string (errno
));
195 *name
= diff_file_name
;
199 /* --------------------------------------------------------------------------------------------- */
202 * Alocate file structure and associate file descriptor to it.
204 * \param fd file descriptor
206 * \return file structure
216 fs
= g_try_malloc (sizeof (FBUF
));
220 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
235 /* --------------------------------------------------------------------------------------------- */
238 * Free file structure without closing the file.
240 * \param fs file structure
242 * \return 0 on success, non-zero on error
248 if (fs
->flags
& FILE_FLAG_TEMP
)
250 rv
= unlink (fs
->data
);
259 /* --------------------------------------------------------------------------------------------- */
262 * Open a binary temporary file in R/W mode.
264 * \return file structure
266 * \note the file will be deleted when closed
280 fd
= open_temp (&fs
->data
);
288 fs
->flags
= FILE_FLAG_TEMP
;
292 /* --------------------------------------------------------------------------------------------- */
295 * Open a binary file in specified mode.
297 * \param filename file name
298 * \param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
300 * \return file structure
303 f_open (const char *filename
, int flags
)
314 fd
= open (filename
, flags
);
325 /* --------------------------------------------------------------------------------------------- */
328 * Read a line of bytes from file until newline or EOF.
330 * \param buf destination buffer
331 * \param size size of buffer
332 * \param fs file structure
334 * \return number of bytes read
336 * \note does not stop on null-byte
337 * \note buf will not be null-terminated
340 f_gets (char *buf
, size_t size
, FBUF
* fs
)
349 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
359 if (j
== size
|| stop
)
365 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
372 /* --------------------------------------------------------------------------------------------- */
377 * \param fs file structure
379 * \param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
381 * \return position in file, starting from begginning
383 * \note avoids thrashing read cache when possible
386 f_seek (FBUF
* fs
, off_t off
, int whence
)
390 if (fs
->len
&& whence
!= SEEK_END
)
392 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
395 if (whence
== SEEK_CUR
)
398 off
+= rv
- fs
->len
+ fs
->pos
;
400 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
402 fs
->pos
= fs
->len
+ off
- rv
;
408 rv
= lseek (fs
->fd
, off
, whence
);
416 /* --------------------------------------------------------------------------------------------- */
419 * Seek to the beginning of file, thrashing read cache.
421 * \param fs file structure
423 * \return 0 if success, non-zero on error
428 off_t rv
= lseek (fs
->fd
, 0, SEEK_SET
);
436 /* --------------------------------------------------------------------------------------------- */
439 * Write bytes to file.
441 * \param fs file structure
442 * \param buf source buffer
443 * \param size size of buffer
445 * \return number of written bytes, -1 on error
447 * \note thrashes read cache
450 f_write (FBUF
* fs
, const char *buf
, size_t size
)
452 ssize_t rv
= write (fs
->fd
, buf
, size
);
460 /* --------------------------------------------------------------------------------------------- */
463 * Truncate file to the current position.
465 * \param fs file structure
467 * \return current file size on success, negative on error
469 * \note thrashes read cache
474 off_t off
= lseek (fs
->fd
, 0, SEEK_CUR
);
477 int rv
= ftruncate (fs
->fd
, off
);
490 /* --------------------------------------------------------------------------------------------- */
495 * \param fs file structure
497 * \return 0 on success, non-zero on error
499 * \note if this is temporary file, it is deleted
504 int rv
= close (fs
->fd
);
509 /* --------------------------------------------------------------------------------------------- */
512 * Create pipe stream to process.
514 * \param cmd shell command line
515 * \param flags open mode, either O_RDONLY or O_WRONLY
517 * \return file structure
520 p_open (const char *cmd
, int flags
)
524 const char *type
= NULL
;
526 if (flags
== O_RDONLY
)
530 if (flags
== O_WRONLY
)
546 f
= popen (cmd
, type
);
558 /* --------------------------------------------------------------------------------------------- */
563 * \param fs structure
565 * \return 0 on success, non-zero on error
570 int rv
= pclose (fs
->data
);
576 * Get one char (byte) from string
578 * \param char * str, gboolean * result
580 * \return int as character or 0 and result == FALSE if fail
584 dview_get_byte (char * str
, gboolean
* result
)
592 return (unsigned char) *str
;
597 * Get utf multibyte char from string
599 * \param char * str, int * char_width, gboolean * result
601 * \return int as utf character or 0 and result == FALSE if fail
605 dview_get_utf (char * str
, int * char_width
, gboolean
* result
)
609 gchar
*next_ch
= NULL
;
621 res
= g_utf8_get_char_validated (str
, -1);
631 /* Calculate UTF-8 char width */
632 next_ch
= g_utf8_next_char (str
);
635 width
= next_ch
- str
;
648 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
651 if (text
== NULL
|| text
[0] == '\0')
653 if (g_utf8_validate (text
, -1, NULL
))
655 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
660 char *tmpbuf
, *buffer
;
661 buffer
= tmpbuf
= g_strdup (text
);
662 while (tmpbuf
[0] != '\0')
664 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
665 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
667 tmpbuf
= g_utf8_next_char (tmpbuf
);
676 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
679 return max (length
, (size_t) result
);
683 /* --------------------------------------------------------------------------------------------- */
684 /* diff parse *************************************************************** */
687 * Read decimal number from string.
689 * \param[in,out] str string to parse
690 * \param[out] n extracted number
692 * \return 0 if success, otherwise non-zero
695 scan_deci (const char **str
, int *n
)
697 const char *p
= *str
;
700 *n
= strtol (p
, &q
, 10);
709 /* --------------------------------------------------------------------------------------------- */
712 * Parse line for diff statement.
714 * \param p string to parse
715 * \param ops list of diff statements
717 * \return 0 if success, otherwise non-zero
720 scan_line (const char *p
, GArray
* ops
)
730 /* handle the following cases:
732 * NUM[,NUM]cNUM[,NUM]
734 * where NUM is a positive integer
737 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
746 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
761 else if (cmd
!= 'c' && cmd
!= 'd')
766 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
775 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
795 g_array_append_val (ops
, op
);
799 /* --------------------------------------------------------------------------------------------- */
802 * Parse diff output and extract diff statements.
804 * \param f stream to read from
805 * \param ops list of diff statements to fill
807 * \return positive number indicating number of hunks, otherwise negative
810 scan_diff (FBUF
* f
, GArray
* ops
)
815 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)))
817 if (isdigit (buf
[0]))
819 if (buf
[sz
- 1] != '\n')
824 if (scan_line (buf
, ops
) != 0)
830 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
838 /* --------------------------------------------------------------------------------------------- */
841 * Invoke diff and extract diff statements.
843 * \param args extra arguments to be passed to diff
844 * \param extra more arguments to be passed to diff
845 * \param file1 first file to compare
846 * \param file2 second file to compare
847 * \param ops list of diff statements to fill
849 * \return positive number indicating number of hunks, otherwise negative
852 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
855 static const char *opt
=
856 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
857 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
858 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
859 " --unchanged-group-format=''";
866 cmd
= g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args
, extra
, opt
, file1
, file2
);
870 f
= p_open (cmd
, O_RDONLY
);
876 rv
= scan_diff (f
, ops
);
879 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
885 /* --------------------------------------------------------------------------------------------- */
888 * Reparse and display file according to diff statements.
890 * \param ord 0 if displaying first file, 1 if displaying 2nd file
891 * \param filename file name to display
892 * \param ops list of diff statements
893 * \param printer printf-like function to be used for displaying
894 * \param ctx printer context
896 * \return 0 if success, otherwise non-zero
899 dff_reparse (int ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
912 f
= f_open (filename
, O_RDONLY
);
930 #define T1 a[ ord^1 ][0]
931 #define T2 a[ ord^1 ][1]
932 for (i
= 0; i
< ops
->len
; i
++)
935 op
= &g_array_index (ops
, DIFFCMD
, i
);
936 n
= op
->F1
- (op
->cmd
!= add_cmd
);
937 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
940 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
942 while (buf
[sz
- 1] != '\n')
944 sz
= f_gets (buf
, sizeof (buf
), f
);
947 printer (ctx
, 0, 0, 0, 1, "\n");
950 printer (ctx
, 0, 0, 0, sz
, buf
);
959 if (op
->cmd
== add_cmd
)
961 n
= op
->T2
- op
->T1
+ 1;
964 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
968 if (op
->cmd
== del_cmd
)
970 n
= op
->F2
- op
->F1
+ 1;
971 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
974 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
976 while (buf
[sz
- 1] != '\n')
978 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
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1000 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
1002 while (buf
[sz
- 1] != '\n')
1004 sz
= f_gets (buf
, sizeof (buf
), f
);
1007 printer (ctx
, 0, 0, 0, 1, "\n");
1010 printer (ctx
, 0, 0, 0, sz
, buf
);
1019 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
1022 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
1032 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1035 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1037 while (buf
[sz
- 1] != '\n')
1039 sz
= f_gets (buf
, sizeof (buf
), f
);
1042 printer (ctx
, 0, 0, 0, 1, "\n");
1045 printer (ctx
, 0, 0, 0, sz
, buf
);
1058 /* --------------------------------------------------------------------------------------------- */
1059 /* horizontal diff ********************************************************** */
1062 * Longest common substring.
1064 * \param s first string
1065 * \param m length of first string
1066 * \param t second string
1067 * \param n length of second string
1068 * \param ret list of offsets for longest common substrings inside each string
1069 * \param min minimum length of common substrings
1071 * \return 0 if success, nonzero otherwise
1074 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1082 if (m
< min
|| n
< min
)
1084 /* XXX early culling */
1088 Lprev
= g_new0 (int, n
+ 1);
1089 Lcurr
= g_new0 (int, n
+ 1);
1091 if (Lprev
== NULL
|| Lcurr
== NULL
)
1098 for (i
= 0; i
< m
; i
++)
1103 #ifdef USE_MEMSET_IN_LCS
1104 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1106 for (j
= 0; j
< n
; j
++)
1108 #ifndef USE_MEMSET_IN_LCS
1113 int v
= Lprev
[j
] + 1;
1118 g_array_set_size (ret
, 0);
1120 if (z
== v
&& z
>= min
)
1122 int off0
= i
- z
+ 1;
1123 int off1
= j
- z
+ 1;
1125 for (k
= 0; k
< ret
->len
; k
++)
1127 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1128 if ((*p
)[0] == off0
)
1132 if ((*p
)[1] >= off1
)
1142 g_array_append_val (ret
, p2
);
1158 /* --------------------------------------------------------------------------------------------- */
1161 * Scan recursively for common substrings and build ranges.
1163 * \param s first string
1164 * \param t second string
1165 * \param bracket current limits for both of the strings
1166 * \param min minimum length of common substrings
1167 * \param hdiff list of horizontal diff ranges to fill
1168 * \param depth recursion depth
1170 * \return 0 if success, nonzero otherwise
1173 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1183 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1187 len
= lcsubstr (s
+ bracket
[0].off
, bracket
[0].len
,
1188 t
+ bracket
[1].off
, bracket
[1].len
, ret
, min
);
1192 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1195 b
[0].off
= bracket
[0].off
;
1196 b
[0].len
= (*data
)[0];
1197 b
[1].off
= bracket
[1].off
;
1198 b
[1].len
= (*data
)[1];
1199 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1202 for (k
= 0; k
< ret
->len
- 1; k
++)
1204 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1205 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1206 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1207 b
[0].len
= (*data2
)[0] - (*data
)[0] - len
;
1208 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1209 b
[1].len
= (*data2
)[1] - (*data
)[1] - len
;
1210 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1213 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1214 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1215 b
[0].len
= bracket
[0].len
- (*data
)[0] - len
;
1216 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1217 b
[1].len
= bracket
[1].len
- (*data
)[1] - len
;
1218 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1221 g_array_free (ret
, TRUE
);
1226 p
[0].off
= bracket
[0].off
;
1227 p
[0].len
= bracket
[0].len
;
1228 p
[1].off
= bracket
[1].off
;
1229 p
[1].len
= bracket
[1].len
;
1230 g_array_append_val (hdiff
, p
);
1235 /* --------------------------------------------------------------------------------------------- */
1238 * Build list of horizontal diff ranges.
1240 * \param s first string
1241 * \param m length of first string
1242 * \param t second string
1243 * \param n length of second string
1244 * \param min minimum length of common substrings
1245 * \param hdiff list of horizontal diff ranges to fill
1246 * \param depth recursion depth
1248 * \return 0 if success, nonzero otherwise
1251 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1256 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1257 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1259 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1267 /* smartscan (multiple horizontal diff) */
1268 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1271 /* --------------------------------------------------------------------------------------------- */
1272 /* read line **************************************************************** */
1275 * Check if character is inside horizontal diff limits.
1277 * \param k rank of character inside line
1278 * \param hdiff horizontal diff structure
1279 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1281 * \return TRUE if inside hdiff limits, FALSE otherwise
1284 is_inside (int k
, GArray
* hdiff
, int ord
)
1288 for (i
= 0; i
< hdiff
->len
; i
++)
1291 b
= &g_array_index (hdiff
, BRACKET
, i
);
1293 start
= (*b
)[ord
].off
;
1294 end
= start
+ (*b
)[ord
].len
;
1295 if (k
>= start
&& k
< end
)
1303 /* --------------------------------------------------------------------------------------------- */
1306 * Copy `src' to `dst' expanding tabs.
1308 * \param dst destination buffer
1309 * \param src source buffer
1310 * \param srcsize size of src buffer
1311 * \param base virtual base of this string, needed to calculate tabs
1312 * \param ts tab size
1314 * \return new virtual base
1316 * \note The procedure returns when all bytes are consumed from `src'
1319 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1322 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1327 int j
= TAB_SKIP (ts
, i
+ base
);
1339 /* --------------------------------------------------------------------------------------------- */
1342 * Copy `src' to `dst' expanding tabs.
1344 * \param dst destination buffer
1345 * \param dstsize size of dst buffer
1346 * \param[in,out] _src source buffer
1347 * \param srcsize size of src buffer
1348 * \param base virtual base of this string, needed to calculate tabs
1349 * \param ts tab size
1351 * \return new virtual base
1353 * \note The procedure returns when all bytes are consumed from `src'
1354 * or `dstsize' bytes are written to `dst'
1355 * \note Upon return, `src' points to the first unwritten character in source
1358 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1361 const char *src
= *_src
;
1362 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1367 int j
= TAB_SKIP (ts
, i
+ base
);
1368 if (j
> dstsize
- i
)
1384 /* --------------------------------------------------------------------------------------------- */
1387 * Read line from memory, converting tabs to spaces and padding with spaces.
1389 * \param src buffer to read from
1390 * \param srcsize size of src buffer
1391 * \param dst buffer to read to
1392 * \param dstsize size of dst buffer, excluding trailing null
1393 * \param skip number of characters to skip
1394 * \param ts tab size
1395 * \param show_cr show trailing carriage return as ^M
1397 * \return negative on error, otherwise number of bytes except padding
1400 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1408 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1412 int j
= TAB_SKIP (ts
, i
+ base
);
1420 else if (dstsize
!= 0)
1427 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1429 if (skip
== 0 && show_cr
)
1454 utf_ch
= dview_get_utf ((char *)src
, &w
, &res
);
1457 if (!g_unichar_isprint (utf_ch
))
1469 while (dstsize
!= 0)
1478 /* --------------------------------------------------------------------------------------------- */
1481 * Read line from memory and build attribute array.
1483 * \param src buffer to read from
1484 * \param srcsize size of src buffer
1485 * \param dst buffer to read to
1486 * \param dstsize size of dst buffer, excluding trailing null
1487 * \param skip number of characters to skip
1488 * \param ts tab size
1489 * \param show_cr show trailing carriage return as ^M
1490 * \param hdiff horizontal diff structure
1491 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1492 * \param att buffer of attributes
1494 * \return negative on error, otherwise number of bytes except padding
1497 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1498 GArray
* hdiff
, int ord
, char *att
)
1506 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1510 int j
= TAB_SKIP (ts
, i
+ base
);
1518 else if (dstsize
!= 0)
1521 *att
++ = is_inside (k
, hdiff
, ord
);
1526 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1528 if (skip
== 0 && show_cr
)
1533 *att
++ = is_inside (k
, hdiff
, ord
);
1535 *att
++ = is_inside (k
, hdiff
, ord
);
1541 *att
++ = is_inside (k
, hdiff
, ord
);
1556 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1559 if (!g_unichar_isprint (utf_ch
))
1565 *att
++ = is_inside (k
, hdiff
, ord
);
1572 while (dstsize
!= 0)
1582 /* --------------------------------------------------------------------------------------------- */
1585 * Read line from file, converting tabs to spaces and padding with spaces.
1587 * \param f file stream to read from
1588 * \param off offset of line inside file
1589 * \param dst buffer to read to
1590 * \param dstsize size of dst buffer, excluding trailing null
1591 * \param skip number of characters to skip
1592 * \param ts tab size
1593 * \param show_cr show trailing carriage return as ^M
1595 * \return negative on error, otherwise number of bytes except padding
1598 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1601 int old_base
= base
;
1602 const int amount
= dstsize
;
1604 size_t useful
, offset
;
1611 const char *q
= NULL
;
1612 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1613 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1615 if ((int) sizeof (tmp
) < amount
|| (int) sizeof (tmp
) <= dstsize
1616 || (int) sizeof (cvt
) < 8 * amount
)
1618 /* abnormal, but avoid buffer overflow */
1619 memset (dst
, ' ', dstsize
);
1620 dst
[dstsize
] = '\0';
1624 f_seek (f
, off
, SEEK_SET
);
1629 sz
= f_gets (tmp
, amount
, f
);
1633 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1634 if (cvt
[base
- old_base
- 1] == '\n')
1636 q
= &cvt
[base
- old_base
- 1];
1637 base
= old_base
+ q
- cvt
+ 1;
1642 useful
= base
- skip
;
1643 offset
= skip
- old_base
;
1647 memset (dst
, ' ', dstsize
);
1648 dst
[dstsize
] = '\0';
1652 if (useful
<= dstsize
)
1655 memmove (dst
, cvt
+ offset
, useful
);
1659 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1662 const char *ptr
= tmp
;
1663 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1672 memmove (dst
, cvt
+ offset
, dstsize
);
1674 lastch
= cvt
[offset
+ dstsize
];
1678 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1680 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1684 if (i
+ 1 < dstsize
)
1697 for (; i
< dstsize
; i
++)
1705 /* --------------------------------------------------------------------------------------------- */
1706 /* diff printers et al ****************************************************** */
1709 cc_free_elt (void *elt
)
1718 /* --------------------------------------------------------------------------------------------- */
1721 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1723 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1724 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1732 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1734 if (sz
!= 0 && str
[sz
- 1] == '\n')
1739 p
.p
= g_strndup (str
, sz
);
1742 g_array_append_val (a
, p
);
1744 else if (dsrc
== DATA_SRC_MEM
)
1747 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1748 if (sz
!= 0 && str
[sz
- 1] == '\n')
1754 size_t new_size
= p
->u
.len
+ sz
;
1755 char *q
= g_realloc (p
->p
, new_size
);
1756 memcpy (q
+ p
->u
.len
, str
, sz
);
1761 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1763 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1764 f_write (f
, str
, sz
);
1769 /* --------------------------------------------------------------------------------------------- */
1772 redo_diff (WDiff
* dview
)
1774 FBUF
*const *f
= dview
->f
;
1784 if (dview
->opt
.quality
== 2)
1786 strcat (extra
, " -d");
1788 if (dview
->opt
.quality
== 1)
1790 strcat (extra
, " --speed-large-files");
1792 if (dview
->opt
.strip_trailing_cr
)
1794 strcat (extra
, " --strip-trailing-cr");
1796 if (dview
->opt
.ignore_tab_expansion
)
1798 strcat (extra
, " -E");
1800 if (dview
->opt
.ignore_space_change
)
1802 strcat (extra
, " -b");
1804 if (dview
->opt
.ignore_all_space
)
1806 strcat (extra
, " -w");
1808 if (dview
->opt
.ignore_case
)
1810 strcat (extra
, " -i");
1813 if (dview
->dsrc
!= DATA_SRC_MEM
)
1819 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1820 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[0], dview
->file
[1], ops
);
1823 g_array_free (ops
, TRUE
);
1827 ctx
.dsrc
= dview
->dsrc
;
1830 ctx
.a
= dview
->a
[0];
1832 rv
|= dff_reparse (0, dview
->file
[0], ops
, printer
, &ctx
);
1834 ctx
.a
= dview
->a
[1];
1836 rv
|= dff_reparse (1, dview
->file
[1], ops
, printer
, &ctx
);
1838 g_array_free (ops
, TRUE
);
1840 if (rv
!= 0 || dview
->a
[0]->len
!= dview
->a
[1]->len
)
1843 if (dview
->dsrc
== DATA_SRC_TMP
)
1849 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1851 dview
->hdiff
= g_ptr_array_new ();
1852 if (dview
->hdiff
!= NULL
)
1857 for (i
= 0; i
< dview
->a
[0]->len
; i
++)
1860 p
= &g_array_index (dview
->a
[0], DIFFLN
, i
);
1861 q
= &g_array_index (dview
->a
[1], DIFFLN
, i
);
1862 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1864 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1867 gboolean runresult
=
1868 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1872 g_array_free (h
, TRUE
);
1877 g_ptr_array_add (dview
->hdiff
, h
);
1884 /* --------------------------------------------------------------------------------------------- */
1887 destroy_hdiff (WDiff
* dview
)
1889 if (dview
->hdiff
!= NULL
)
1892 int len
= dview
->a
[0]->len
;
1893 for (i
= 0; i
< len
; i
++)
1895 GArray
*h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1898 g_array_free (h
, TRUE
);
1901 g_ptr_array_free (dview
->hdiff
, TRUE
);
1902 dview
->hdiff
= NULL
;
1905 mc_search_free (dview
->search
.handle
);
1906 dview
->search
.handle
= NULL
;
1907 g_free (dview
->search
.last_string
);
1908 dview
->search
.last_string
= NULL
;
1911 /* --------------------------------------------------------------------------------------------- */
1912 /* stuff ******************************************************************** */
1915 get_digits (unsigned int n
)
1925 /* --------------------------------------------------------------------------------------------- */
1928 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1942 p
= &g_array_index (a
, DIFFLN
, pos
);
1947 for (n
= pos
; n
> 0; n
--)
1955 *lineofs
= pos
- n
+ 1;
1963 /* --------------------------------------------------------------------------------------------- */
1966 calc_nwidth (const GArray
** const a
)
1970 get_line_numbers (a
[0], a
[0]->len
- 1, &l1
, &o1
);
1971 get_line_numbers (a
[1], a
[1]->len
- 1, &l2
, &o2
);
1976 return get_digits (l1
);
1979 /* --------------------------------------------------------------------------------------------- */
1982 find_prev_hunk (const GArray
* a
, int pos
)
1985 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1989 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1993 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1997 if (pos
> 0 && (size_t) pos
< a
->len
)
2000 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
2004 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
2013 /* --------------------------------------------------------------------------------------------- */
2016 find_next_hunk (const GArray
* a
, size_t pos
)
2018 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2022 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
2030 * Find start and end lines of the current hunk.
2032 * \param dview - widget WDiff
2033 * \return boolean and
2034 * start_line1 first line of current hunk (file[0])
2035 * end_line1 last line of current hunk (file[0])
2036 * start_line1 first line of current hunk (file[0])
2037 * end_line1 last line of current hunk (file[0])
2040 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
2042 const GArray
*a0
= dview
->a
[0];
2043 const GArray
*a1
= dview
->a
[1];
2053 pos
= dview
->skip_rows
;
2054 ch
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
;
2069 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2075 *start_line1
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2076 *start_line2
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2078 pos
= dview
->skip_rows
;
2079 while (pos
< a0
->len
&& ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2082 l0
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
;
2083 l1
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
;
2085 *end_line1
= max (*start_line1
, l0
);
2087 *end_line2
= max (*start_line2
, l1
);
2095 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
)
2100 f0
= fopen (dview
->file
[0], "r");
2102 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2105 fputs (buf
, merge_file
);
2107 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2111 fputs (buf
, merge_file
);
2117 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
)
2123 f0
= fopen (dview
->file
[0], "r");
2124 f1
= fopen (dview
->file
[1], "r");
2126 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2129 fputs (buf
, merge_file
);
2132 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2136 fputs (buf
, merge_file
);
2138 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2140 fputs (buf
, merge_file
);
2147 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
)
2153 f0
= fopen (dview
->file
[0], "r");
2154 f1
= fopen (dview
->file
[1], "r");
2156 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2159 fputs (buf
, merge_file
);
2162 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2166 fputs (buf
, merge_file
);
2168 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2172 fputs (buf
, merge_file
);
2179 do_merge_hunk (WDiff
* dview
)
2181 int from1
, to1
, from2
, to2
;
2185 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2190 char *merge_file_name
= NULL
;
2194 dview
->merged
= mc_util_make_backup_if_possible (dview
->file
[0], "~~~");
2197 message (D_ERROR
, MSG_ERROR
,
2198 _(" Cannot create backup file \n %s%s \n %s "),
2199 dview
->file
[0], "~~~", unix_error_string (errno
));
2205 merge_file_fd
= mc_mkstemps (&merge_file_name
, "mcmerge", NULL
);
2206 if (merge_file_fd
== -1)
2208 message (D_ERROR
, MSG_ERROR
, _(" Cannot create temporary merge file \n %s "),
2209 unix_error_string (errno
));
2213 merge_file
= fdopen (merge_file_fd
, "w");
2218 dview_remove_hunk (dview
, merge_file
, from1
, to1
);
2221 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
);
2224 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
);
2227 fflush (merge_file
);
2228 fclose (merge_file
);
2229 res
= rewrite_backup_content (merge_file_name
, dview
->file
[0]);
2230 unlink (merge_file_name
);
2231 g_free (merge_file_name
);
2235 /* --------------------------------------------------------------------------------------------- */
2236 /* view routines and callbacks ********************************************** */
2239 dview_compute_split (WDiff
* dview
, int i
)
2242 if (dview
->bias
< 2 - dview
->half1
)
2244 dview
->bias
= 2 - dview
->half1
;
2246 if (dview
->bias
> dview
->half2
- 2)
2248 dview
->bias
= dview
->half2
- 2;
2252 /* --------------------------------------------------------------------------------------------- */
2255 dview_compute_areas (WDiff
* dview
)
2257 dview
->height
= LINES
- 2;
2258 dview
->half1
= COLS
/ 2;
2259 dview
->half2
= COLS
- dview
->half1
;
2261 dview_compute_split (dview
, 0);
2264 /* --------------------------------------------------------------------------------------------- */
2267 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2268 const char *label1
, const char *label2
, DSRC dsrc
)
2276 if (dsrc
== DATA_SRC_TMP
)
2289 else if (dsrc
== DATA_SRC_ORG
)
2291 f
[0] = f_open (file1
, O_RDONLY
);
2295 f
[1] = f_open (file2
, O_RDONLY
);
2304 dview
->file
[0] = file1
;
2305 dview
->file
[1] = file2
;
2306 dview
->label
[0] = label1
;
2307 dview
->label
[1] = label2
;
2310 dview
->hdiff
= NULL
;
2312 dview
->converter
= str_cnv_from_term
;
2313 dview_set_codeset (dview
);
2315 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2316 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2318 ndiff
= redo_diff (dview
);
2321 /* goto WIDGET_DESTROY stage: dview_fini() */
2325 dview
->ndiff
= ndiff
;
2327 dview
->view_quit
= 0;
2330 dview
->new_frame
= 1;
2331 dview
->skip_rows
= 0;
2332 dview
->skip_cols
= 0;
2333 dview
->display_symbols
= 0;
2334 dview
->display_numbers
= 0;
2336 dview
->tab_size
= 8;
2340 dview
->search
.handle
=NULL
;
2341 dview
->search
.last_string
=NULL
;
2342 dview
->search
.last_found_line
= -1;
2343 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 /* --------------------------------------------------------------------------------------------- */
2360 dview_reread (WDiff
* dview
)
2362 int ndiff
= dview
->ndiff
;
2363 destroy_hdiff (dview
);
2365 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2366 g_array_free (dview
->a
[0], TRUE
);
2367 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2368 g_array_free (dview
->a
[1], TRUE
);
2370 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2371 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2373 ndiff
= redo_diff (dview
);
2376 dview
->ndiff
= ndiff
;
2380 /* --------------------------------------------------------------------------------------------- */
2382 dview_diff_options (WDiff
* dview
)
2384 const char *quality_str
[] = {
2386 N_("&Fastest (Assume large files)"),
2387 N_("&Minimal (Find a smaller set of change)")
2390 QuickWidget diffopt_widgets
[] = {
2391 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2392 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2394 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2395 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2396 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2397 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2398 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2399 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2400 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2401 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2402 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2403 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2404 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2405 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2406 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2407 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2412 QuickDialog diffopt
= {
2414 N_("Diff Options"), "[Diff Options]",
2418 if (quick_dialog (&diffopt
) != B_CANCEL
)
2420 dview_reread (dview
);
2423 /* --------------------------------------------------------------------------------------------- */
2426 dview_fini (WDiff
* dview
)
2428 if (dview
->dsrc
!= DATA_SRC_MEM
)
2430 f_close (dview
->f
[1]);
2431 f_close (dview
->f
[0]);
2434 if (dview
->converter
!= str_cnv_from_term
)
2435 str_close_conv (dview
->converter
);
2437 destroy_hdiff (dview
);
2438 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2439 g_array_free (dview
->a
[0], TRUE
);
2440 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2441 g_array_free (dview
->a
[1], TRUE
);
2447 /* --------------------------------------------------------------------------------------------- */
2450 dview_display_file (const WDiff
* dview
, int ord
, int r
, int c
, int height
, int width
)
2455 FBUF
*f
= dview
->f
[ord
];
2456 int skip
= dview
->skip_cols
;
2457 int display_symbols
= dview
->display_symbols
;
2458 int display_numbers
= dview
->display_numbers
;
2459 int show_cr
= dview
->show_cr
;
2460 int tab_size
= dview
->tab_size
;
2462 int nwidth
= display_numbers
;
2463 int xwidth
= display_symbols
+ display_numbers
;
2466 if (xwidth
> width
&& display_symbols
)
2469 display_symbols
= 0;
2471 if (xwidth
> width
&& display_numbers
)
2474 display_numbers
= width
;
2488 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2490 /* abnormal, but avoid buffer overflow */
2494 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2496 int ch
, next_ch
, col
;
2498 p
= (DIFFLN
*) &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2500 tty_setcolor (NORMAL_COLOR
);
2501 if (display_symbols
)
2503 tty_gotoyx (r
+ j
, c
- 2);
2504 tty_print_char (ch
);
2508 if (display_numbers
)
2510 tty_gotoyx (r
+ j
, c
- xwidth
);
2511 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2512 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2516 tty_setcolor (DFF_ADD_COLOR
);
2520 tty_setcolor (DFF_CHG_COLOR
);
2524 if (i
== (size_t) dview
->search
.last_found_line
)
2526 tty_setcolor (MARKED_SELECTED_COLOR
);
2530 if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2534 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2537 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2538 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2539 tty_gotoyx (r
+ j
, c
);
2541 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2547 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2550 if (!g_unichar_isprint (next_ch
))
2554 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2557 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2561 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2563 } else if (dview
->utf8
)
2564 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2567 next_ch
= convert_to_display_c (next_ch
);
2569 tty_print_anychar (next_ch
);
2575 else if (ch
== CHG_CH
)
2577 tty_setcolor (DFF_CHH_COLOR
);
2581 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2584 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2588 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2593 if (display_numbers
)
2595 tty_gotoyx (r
+ j
, c
- xwidth
);
2596 memset (buf
, ' ', display_numbers
);
2597 buf
[display_numbers
] = '\0';
2598 tty_print_string (buf
);
2602 tty_setcolor (DFF_DEL_COLOR
);
2606 tty_setcolor (DFF_CHD_COLOR
);
2608 memset (buf
, ' ', width
);
2611 tty_gotoyx (r
+ j
, c
);
2612 /* tty_print_nstring (buf, width); */
2614 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2620 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2623 if (!g_unichar_isprint (next_ch
))
2627 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2633 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2635 } else if (dview
->utf8
)
2636 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2639 next_ch
= convert_to_display_c (next_ch
);
2641 tty_print_anychar (next_ch
);
2647 tty_setcolor (NORMAL_COLOR
);
2649 if (width
< xwidth
- 1)
2653 memset (buf
, ' ', k
);
2655 for (; j
< height
; j
++)
2659 tty_gotoyx (r
+ j
, c
- xwidth
);
2660 /* tty_print_nstring (buf, xwidth - 1); */
2661 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2663 tty_gotoyx (r
+ j
, c
);
2664 /* tty_print_nstring (buf, width); */
2665 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2671 /* --------------------------------------------------------------------------------------------- */
2674 dview_status (const WDiff
* dview
, int ord
, int width
, int c
)
2676 int skip_rows
= dview
->skip_rows
;
2677 int skip_cols
= dview
->skip_cols
;
2681 int linenum
, lineofs
;
2683 tty_setcolor (SELECTED_COLOR
);
2686 get_line_numbers (dview
->a
[ord
], skip_rows
, &linenum
, &lineofs
);
2688 filename_width
= width
- 22;
2689 if (filename_width
< 8)
2693 if (filename_width
>= (int) sizeof (buf
))
2695 /* abnormal, but avoid buffer overflow */
2696 filename_width
= sizeof (buf
) - 1;
2698 trim (strip_home_and_password (dview
->label
[ord
]), buf
, filename_width
);
2701 tty_printf ("%-*s %6d+%-4d Col %-4d ", filename_width
, buf
, linenum
, lineofs
, skip_cols
);
2705 tty_printf ("%-*s %6d+%-4d Dif %-4d ", filename_width
, buf
, linenum
, lineofs
, dview
->ndiff
);
2709 /* --------------------------------------------------------------------------------------------- */
2712 dview_redo (WDiff
* dview
)
2714 if (dview
->display_numbers
)
2716 int old
= dview
->display_numbers
;
2717 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2718 dview
->new_frame
= (old
!= dview
->display_numbers
);
2720 dview_reread (dview
);
2723 /* --------------------------------------------------------------------------------------------- */
2726 dview_edit (WDiff
* dview
, int ord
)
2728 int linenum
, lineofs
;
2730 if (dview
->dsrc
== DATA_SRC_TMP
)
2732 error_dialog (_("Edit"), _(" Edit is disabled "));
2736 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2737 do_edit_at_line (dview
->file
[ord
], linenum
);
2739 dview_update (dview
);
2742 /* --------------------------------------------------------------------------------------------- */
2745 dview_goto_cmd (WDiff
* dview
, int ord
)
2747 static const char *title
[2] = { " Goto line (left) ", " Goto line (right) " };
2748 static char prev
[256];
2749 /* XXX some statics here, to be remembered between runs */
2754 input
= input_dialog (_(title
[ord
]), _(" Enter line: "), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2757 const char *s
= input
;
2758 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2764 for (; i
< dview
->a
[ord
]->len
; i
++)
2766 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2767 if (p
->line
== newline
)
2773 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= i
;
2774 snprintf (prev
, sizeof (prev
), "%d", newline
);
2780 /* --------------------------------------------------------------------------------------------- */
2783 dview_labels (WDiff
* dview
)
2785 Dlg_head
*h
= dview
->widget
.parent
;
2786 WButtonBar
*b
= find_buttonbar (h
);
2788 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2789 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2790 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2791 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2792 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2793 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, (Widget
*) dview
);
2794 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2798 /* --------------------------------------------------------------------------------------------- */
2801 dview_event (Gpm_Event
* event
, void *x
)
2803 WDiff
*dview
= (WDiff
*) x
;
2804 int result
= MOU_NORMAL
;
2806 /* We are not interested in the release events */
2807 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2813 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2815 dview
->skip_rows
-= 2;
2816 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2817 dview_update (dview
);
2819 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2821 dview
->skip_rows
+= 2;
2822 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2823 dview_update (dview
);
2830 dview_save (WDiff
* dview
)
2832 gboolean res
= TRUE
;
2835 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2836 dview
->merged
= !res
;
2841 dview_do_save (WDiff
* dview
)
2843 (void) dview_save (dview
);
2847 dview_save_options (WDiff
* dview
)
2849 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
2850 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
2851 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
2852 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
2853 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
2855 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
2857 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
2858 dview
->opt
.strip_trailing_cr
);
2859 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
2860 dview
->opt
.ignore_all_space
);
2861 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
2862 dview
->opt
.ignore_space_change
);
2863 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
2864 dview
->opt
.ignore_tab_expansion
);
2865 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
2869 dview_load_options (WDiff
* dview
)
2871 gboolean show_numbers
, show_symbols
;
2873 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
2875 dview
->display_symbols
= 1;
2876 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
2878 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
2879 dview
->tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
2881 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
2883 dview
->opt
.strip_trailing_cr
=
2884 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
2885 dview
->opt
.ignore_all_space
=
2886 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
2887 dview
->opt
.ignore_space_change
=
2888 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
2889 dview
->opt
.ignore_tab_expansion
=
2890 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
2891 dview
->opt
.ignore_case
=
2892 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
2894 dview
->new_frame
= 1;
2898 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2902 dview_ok_to_exit (WDiff
* dview
)
2904 gboolean res
= TRUE
;
2907 switch (query_dialog
2908 (_("Quit"), _(" File was modified, Save with exit? "), D_NORMAL
, 2, _("&Yes"), _("&No")))
2915 (void) dview_save (dview
);
2919 if (mc_util_restore_from_backup_if_possible (dview
->file
[0], "~~~"))
2920 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2926 /* --------------------------------------------------------------------------------------------- */
2929 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
2931 cb_ret_t res
= MSG_HANDLED
;
2935 interactive_display (NULL
, "[Diff Viewer]");
2937 case CK_DiffDisplaySymbols
:
2938 dview
->display_symbols
^= 1;
2939 dview
->new_frame
= 1;
2941 case CK_DiffDisplayNumbers
:
2942 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
2943 dview
->new_frame
= 1;
2947 dview
->new_frame
= 1;
2953 dview
->new_frame
= 1;
2956 case CK_DiffSplitMore
:
2959 dview_compute_split (dview
, 1);
2960 dview
->new_frame
= 1;
2964 case CK_DiffSplitLess
:
2967 dview_compute_split (dview
, -1);
2968 dview
->new_frame
= 1;
2971 case CK_DiffSetTab2
:
2972 dview
->tab_size
= 2;
2974 case CK_DiffSetTab3
:
2975 dview
->tab_size
= 3;
2977 case CK_DiffSetTab4
:
2978 dview
->tab_size
= 4;
2980 case CK_DiffSetTab8
:
2981 dview
->tab_size
= 8;
2983 case CK_DiffSwapPanel
:
2989 case CK_DiffNextHunk
:
2990 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_next_hunk (dview
->a
[0], dview
->skip_rows
);
2992 case CK_DiffPrevHunk
:
2993 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_prev_hunk (dview
->a
[0], dview
->skip_rows
);
2996 dview_goto_cmd (dview
, TRUE
);
2998 case CK_DiffEditCurrent
:
2999 dview_edit (dview
, dview
->ord
);
3001 case CK_DiffMergeCurrentHunk
:
3002 do_merge_hunk (dview
);
3005 case CK_DiffEditOther
:
3006 dview_edit (dview
, dview
->ord
^ 1);
3009 dview_search_cmd (dview
);
3011 case CK_DiffContinueSearch
:
3012 dview_continue_search_cmd (dview
);
3015 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3018 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[0]->len
- 1;
3022 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3026 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3028 case CK_DiffPageDown
:
3029 dview
->skip_rows
+= dview
->height
- 2;
3030 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3033 dview
->skip_rows
-= dview
->height
- 2;
3034 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3042 case CK_DiffQuickLeft
:
3043 dview
->skip_cols
-= 8;
3045 case CK_DiffQuickRight
:
3046 dview
->skip_cols
+= 8;
3049 dview
->skip_cols
= 0;
3051 case CK_ShowCommandLine
:
3055 dview
->view_quit
= 1;
3058 dview_do_save (dview
);
3060 case CK_DiffOptions
:
3061 dview_diff_options (dview
);
3063 case CK_SelectCodepage
:
3064 dview_select_encoding (dview
);
3065 dview_reread (dview
);
3066 tty_touch_screen ();
3070 res
= MSG_NOT_HANDLED
;
3075 /* --------------------------------------------------------------------------------------------- */
3078 dview_handle_key (WDiff
* dview
, int key
)
3080 unsigned long command
;
3082 key
= convert_from_input_c (key
);
3084 command
= lookup_keymap_command (diff_map
, key
);
3085 if ((command
!= CK_Ignore_Key
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3089 return MSG_NOT_HANDLED
;
3092 /* --------------------------------------------------------------------------------------------- */
3095 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3097 WDiff
*dview
= (WDiff
*) w
;
3098 Dlg_head
*h
= dview
->widget
.parent
;
3104 dview_labels (dview
);
3105 dview_load_options (dview
);
3106 dview_update (dview
);
3110 dview
->new_frame
= 1;
3111 dview_update (dview
);
3115 i
= dview_handle_key (dview
, parm
);
3116 if (dview
->view_quit
)
3119 dview_update (dview
);
3122 case WIDGET_COMMAND
:
3123 i
= dview_execute_cmd (dview
, parm
);
3124 if (dview
->view_quit
)
3127 dview_update (dview
);
3130 case WIDGET_DESTROY
:
3131 dview_save_options (dview
);
3136 return default_proc (msg
, parm
);
3140 /* --------------------------------------------------------------------------------------------- */
3143 dview_adjust_size (Dlg_head
* h
)
3148 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3149 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3150 bar
= find_buttonbar (h
);
3151 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3152 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3154 dview_compute_areas (dview
);
3157 /* --------------------------------------------------------------------------------------------- */
3160 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3162 WDiff
*dview
= (WDiff
*) data
;
3167 dview_adjust_size (h
);
3171 /* command from buttonbar */
3172 return send_message ((Widget
*) dview
, WIDGET_COMMAND
, parm
);
3175 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3176 if (!dview_ok_to_exit (dview
))
3181 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3185 /*** public functions ****************************************************************************/
3186 /* --------------------------------------------------------------------------------------------- */
3189 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3194 Dlg_head
*dview_dlg
;
3196 /* Create dialog and widgets, put them on the dialog */
3198 create_dlg (0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
,
3199 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3201 dview
= g_new0 (WDiff
, 1);
3203 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3204 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3206 widget_want_cursor (dview
->widget
, 0);
3208 bar
= buttonbar_new (1);
3210 add_widget (dview_dlg
, dview
);
3211 add_widget (dview_dlg
, bar
);
3213 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3215 /* Please note that if you add another widget,
3216 * you have to modify dview_adjust_size to
3220 run_dlg (dview_dlg
);
3221 destroy_dlg (dview_dlg
);
3226 /* --------------------------------------------------------------------------------------------- */
3228 #define GET_FILE_AND_STAMP(n) \
3231 real_file##n = file##n; \
3232 if (!vfs_file_is_local(file##n)) { \
3233 real_file##n = mc_getlocalcopy(file##n); \
3234 if (real_file##n != NULL) { \
3236 if (mc_stat(real_file##n, &st##n) != 0) { \
3242 #define UNGET_FILE(n) \
3244 if (use_copy##n) { \
3246 if (use_copy##n > 0) { \
3247 time_t mtime = st##n.st_mtime; \
3248 if (mc_stat(real_file##n, &st##n) == 0) { \
3249 changed = (mtime != st##n.st_mtime); \
3252 mc_ungetlocalcopy(file##n, real_file##n, changed); \
3253 g_free(real_file##n); \
3258 dview_diff_cmd (void)
3266 if (mc_run_mode
== MC_RUN_FULL
)
3268 const WPanel
*panel0
= current_panel
;
3269 const WPanel
*panel1
= other_panel
;
3270 if (get_current_index ())
3272 panel0
= other_panel
;
3273 panel1
= current_panel
;
3275 file0
= concat_dir_and_file (panel0
->cwd
, selection (panel0
)->fname
);
3276 file1
= concat_dir_and_file (panel1
->cwd
, selection (panel1
)->fname
);
3277 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3278 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3284 if (file0
!= NULL
&& !is_dir0
&& file1
!= NULL
&& !is_dir1
)
3292 GET_FILE_AND_STAMP (0);
3293 GET_FILE_AND_STAMP (1);
3294 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3296 rv
= diff_view (real_file0
, real_file1
, file0
, file1
);
3307 message (1, MSG_ERROR
, _("Two files are needed to compare"));
3310 /* --------------------------------------------------------------------------------------------- */
3313 dview_update (WDiff
* dview
)
3315 int height
= dview
->height
;
3319 int last
= dview
->a
[0]->len
- 1;
3321 if (dview
->skip_rows
> last
)
3323 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
3325 if (dview
->skip_rows
< 0)
3327 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3329 if (dview
->skip_cols
< 0)
3331 dview
->skip_cols
= 0;
3339 width1
= dview
->half1
+ dview
->bias
;
3340 width2
= dview
->half2
- dview
->bias
;
3347 if (dview
->new_frame
)
3349 int xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
3351 tty_setcolor (NORMAL_COLOR
);
3354 tty_draw_box (1, 0, height
, width1
, FALSE
);
3358 tty_draw_box (1, width1
, height
, width2
, FALSE
);
3364 if (xwidth
< width1
- 1)
3366 tty_gotoyx (1, xwidth
);
3367 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3368 tty_gotoyx (height
, xwidth
);
3369 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3370 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3372 if (xwidth
< width2
- 1)
3374 tty_gotoyx (1, width1
+ xwidth
);
3375 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3376 tty_gotoyx (height
, width1
+ xwidth
);
3377 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3378 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3381 dview
->new_frame
= 0;
3386 dview_status (dview
, dview
->ord
, width1
, 0);
3387 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
3391 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
3392 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
3396 /* --------------------------------------------------------------------------------------------- */