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.
34 #include "lib/global.h"
35 #include "lib/tty/tty.h"
36 #include "lib/tty/color.h"
37 #include "lib/tty/key.h"
39 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
40 #include "lib/vfs/mc-vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
42 #include "src/cmddef.h"
43 #include "src/keybind.h"
45 #include "src/dialog.h"
47 #include "src/wtools.h"
48 #include "src/charsets.h"
49 #include "src/history.h"
50 #include "src/panel.h" /* Needed for current_panel and other_panel */
51 #include "src/layout.h" /* Needed for get_current_index and get_other_panel */
52 /* mc_open, mc_close, mc_read, mc_stat */
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 static dview_select_encoding (WDiff
* dview
)
138 if (do_select_codepage ())
140 dview_set_codeset (dview
);
149 rewrite_backup_content (const char *from_file_name
, const char *to_file_name
)
155 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
158 backup_fd
= fopen (to_file_name
, "w");
159 if (backup_fd
== NULL
)
165 fwrite ((const void *) contents
, length
, 1, backup_fd
);
173 /* buffered I/O ************************************************************* */
176 * Try to open a temporary file.
178 * \param[out] name address of a pointer to store the temporary name
180 * \return file descriptor on success, negative on error
182 * \note the name is not altered if this function fails
186 open_temp (void **name
)
189 char *diff_file_name
= NULL
;
191 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
194 message (D_ERROR
, MSG_ERROR
,
195 _(" Cannot create temporary diff file \n %s "),
196 unix_error_string (errno
));
199 *name
= diff_file_name
;
203 /* --------------------------------------------------------------------------------------------- */
206 * Alocate file structure and associate file descriptor to it.
208 * \param fd file descriptor
210 * \return file structure
220 fs
= g_try_malloc (sizeof (FBUF
));
224 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
239 /* --------------------------------------------------------------------------------------------- */
242 * Free file structure without closing the file.
244 * \param fs file structure
246 * \return 0 on success, non-zero on error
252 if (fs
->flags
& FILE_FLAG_TEMP
)
254 rv
= unlink (fs
->data
);
263 /* --------------------------------------------------------------------------------------------- */
266 * Open a binary temporary file in R/W mode.
268 * \return file structure
270 * \note the file will be deleted when closed
284 fd
= open_temp (&fs
->data
);
292 fs
->flags
= FILE_FLAG_TEMP
;
296 /* --------------------------------------------------------------------------------------------- */
299 * Open a binary file in specified mode.
301 * \param filename file name
302 * \param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
304 * \return file structure
307 f_open (const char *filename
, int flags
)
318 fd
= open (filename
, flags
);
329 /* --------------------------------------------------------------------------------------------- */
332 * Read a line of bytes from file until newline or EOF.
334 * \param buf destination buffer
335 * \param size size of buffer
336 * \param fs file structure
338 * \return number of bytes read
340 * \note does not stop on null-byte
341 * \note buf will not be null-terminated
344 f_gets (char *buf
, size_t size
, FBUF
* fs
)
353 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
363 if (j
== size
|| stop
)
369 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
376 /* --------------------------------------------------------------------------------------------- */
381 * \param fs file structure
383 * \param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
385 * \return position in file, starting from begginning
387 * \note avoids thrashing read cache when possible
390 f_seek (FBUF
* fs
, off_t off
, int whence
)
394 if (fs
->len
&& whence
!= SEEK_END
)
396 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
399 if (whence
== SEEK_CUR
)
402 off
+= rv
- fs
->len
+ fs
->pos
;
404 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
406 fs
->pos
= fs
->len
+ off
- rv
;
412 rv
= lseek (fs
->fd
, off
, whence
);
420 /* --------------------------------------------------------------------------------------------- */
423 * Seek to the beginning of file, thrashing read cache.
425 * \param fs file structure
427 * \return 0 if success, non-zero on error
432 off_t rv
= lseek (fs
->fd
, 0, SEEK_SET
);
440 /* --------------------------------------------------------------------------------------------- */
443 * Write bytes to file.
445 * \param fs file structure
446 * \param buf source buffer
447 * \param size size of buffer
449 * \return number of written bytes, -1 on error
451 * \note thrashes read cache
454 f_write (FBUF
* fs
, const char *buf
, size_t size
)
456 ssize_t rv
= write (fs
->fd
, buf
, size
);
464 /* --------------------------------------------------------------------------------------------- */
467 * Truncate file to the current position.
469 * \param fs file structure
471 * \return current file size on success, negative on error
473 * \note thrashes read cache
478 off_t off
= lseek (fs
->fd
, 0, SEEK_CUR
);
481 int rv
= ftruncate (fs
->fd
, off
);
494 /* --------------------------------------------------------------------------------------------- */
499 * \param fs file structure
501 * \return 0 on success, non-zero on error
503 * \note if this is temporary file, it is deleted
508 int rv
= close (fs
->fd
);
513 /* --------------------------------------------------------------------------------------------- */
516 * Create pipe stream to process.
518 * \param cmd shell command line
519 * \param flags open mode, either O_RDONLY or O_WRONLY
521 * \return file structure
524 p_open (const char *cmd
, int flags
)
528 const char *type
= NULL
;
530 if (flags
== O_RDONLY
)
534 if (flags
== O_WRONLY
)
550 f
= popen (cmd
, type
);
562 /* --------------------------------------------------------------------------------------------- */
567 * \param fs structure
569 * \return 0 on success, non-zero on error
574 int rv
= pclose (fs
->data
);
580 * Get one char (byte) from string
582 * \param char * str, gboolean * result
584 * \return int as character or 0 and result == FALSE if fail
588 dview_get_byte (char * str
, gboolean
* result
)
596 return (unsigned char) *str
;
601 * Get utf multibyte char from string
603 * \param char * str, int * char_width, gboolean * result
605 * \return int as utf character or 0 and result == FALSE if fail
609 dview_get_utf (char * str
, int * char_width
, gboolean
* result
)
613 gchar
*next_ch
= NULL
;
625 res
= g_utf8_get_char_validated (str
, -1);
635 /* Calculate UTF-8 char width */
636 next_ch
= g_utf8_next_char (str
);
639 width
= next_ch
- str
;
652 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
655 if (text
== NULL
|| text
[0] == '\0')
657 if (g_utf8_validate (text
, -1, NULL
))
659 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
664 char *tmpbuf
, *buffer
;
665 buffer
= tmpbuf
= g_strdup (text
);
666 while (tmpbuf
[0] != '\0')
668 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
669 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
671 tmpbuf
= g_utf8_next_char (tmpbuf
);
680 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
683 return max (length
, result
);
687 /* --------------------------------------------------------------------------------------------- */
688 /* diff parse *************************************************************** */
691 * Read decimal number from string.
693 * \param[in,out] str string to parse
694 * \param[out] n extracted number
696 * \return 0 if success, otherwise non-zero
699 scan_deci (const char **str
, int *n
)
701 const char *p
= *str
;
704 *n
= strtol (p
, &q
, 10);
713 /* --------------------------------------------------------------------------------------------- */
716 * Parse line for diff statement.
718 * \param p string to parse
719 * \param ops list of diff statements
721 * \return 0 if success, otherwise non-zero
724 scan_line (const char *p
, GArray
* ops
)
734 /* handle the following cases:
736 * NUM[,NUM]cNUM[,NUM]
738 * where NUM is a positive integer
741 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
750 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
765 else if (cmd
!= 'c' && cmd
!= 'd')
770 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
779 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
799 g_array_append_val (ops
, op
);
803 /* --------------------------------------------------------------------------------------------- */
806 * Parse diff output and extract diff statements.
808 * \param f stream to read from
809 * \param ops list of diff statements to fill
811 * \return positive number indicating number of hunks, otherwise negative
814 scan_diff (FBUF
* f
, GArray
* ops
)
819 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)))
821 if (isdigit (buf
[0]))
823 if (buf
[sz
- 1] != '\n')
828 if (scan_line (buf
, ops
) != 0)
834 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)))
842 /* --------------------------------------------------------------------------------------------- */
845 * Invoke diff and extract diff statements.
847 * \param args extra arguments to be passed to diff
848 * \param extra more arguments to be passed to diff
849 * \param file1 first file to compare
850 * \param file2 second file to compare
851 * \param ops list of diff statements to fill
853 * \return positive number indicating number of hunks, otherwise negative
856 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
859 static const char *opt
=
860 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
861 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
862 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
863 " --unchanged-group-format=''";
870 cmd
= g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args
, extra
, opt
, file1
, file2
);
874 f
= p_open (cmd
, O_RDONLY
);
880 rv
= scan_diff (f
, ops
);
883 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
889 /* --------------------------------------------------------------------------------------------- */
892 * Reparse and display file according to diff statements.
894 * \param ord 0 if displaying first file, 1 if displaying 2nd file
895 * \param filename file name to display
896 * \param ops list of diff statements
897 * \param printer printf-like function to be used for displaying
898 * \param ctx printer context
900 * \return 0 if success, otherwise non-zero
903 dff_reparse (int ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
916 f
= f_open (filename
, O_RDONLY
);
934 #define T1 a[ ord^1 ][0]
935 #define T2 a[ ord^1 ][1]
936 for (i
= 0; i
< ops
->len
; i
++)
939 op
= &g_array_index (ops
, DIFFCMD
, i
);
940 n
= op
->F1
- (op
->cmd
!= add_cmd
);
941 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)))
944 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
946 while (buf
[sz
- 1] != '\n')
948 if (!(sz
= f_gets (buf
, sizeof (buf
), f
)))
950 printer (ctx
, 0, 0, 0, 1, "\n");
953 printer (ctx
, 0, 0, 0, sz
, buf
);
962 if (op
->cmd
== add_cmd
)
964 n
= op
->T2
- op
->T1
+ 1;
967 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
971 if (op
->cmd
== del_cmd
)
973 n
= op
->F2
- op
->F1
+ 1;
974 while (n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)))
977 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
979 while (buf
[sz
- 1] != '\n')
981 if (!(sz
= f_gets (buf
, sizeof (buf
), f
)))
983 printer (ctx
, 0, 0, 0, 1, "\n");
986 printer (ctx
, 0, 0, 0, sz
, buf
);
998 n
= op
->F2
- op
->F1
+ 1;
999 while (n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)))
1002 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
1004 while (buf
[sz
- 1] != '\n')
1006 if (!(sz
= f_gets (buf
, sizeof (buf
), f
)))
1008 printer (ctx
, 0, 0, 0, 1, "\n");
1011 printer (ctx
, 0, 0, 0, sz
, buf
);
1020 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
1023 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
1033 while ((sz
= f_gets (buf
, sizeof (buf
), f
)))
1036 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1038 while (buf
[sz
- 1] != '\n')
1040 if (!(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
++);
1258 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--);
1265 /* smartscan (multiple horizontal diff) */
1266 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1269 /* --------------------------------------------------------------------------------------------- */
1270 /* read line **************************************************************** */
1273 * Check if character is inside horizontal diff limits.
1275 * \param k rank of character inside line
1276 * \param hdiff horizontal diff structure
1277 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1279 * \return TRUE if inside hdiff limits, FALSE otherwise
1282 is_inside (int k
, GArray
* hdiff
, int ord
)
1286 for (i
= 0; i
< hdiff
->len
; i
++)
1289 b
= &g_array_index (hdiff
, BRACKET
, i
);
1291 start
= (*b
)[ord
].off
;
1292 end
= start
+ (*b
)[ord
].len
;
1293 if (k
>= start
&& k
< end
)
1301 /* --------------------------------------------------------------------------------------------- */
1304 * Copy `src' to `dst' expanding tabs.
1306 * \param dst destination buffer
1307 * \param src source buffer
1308 * \param srcsize size of src buffer
1309 * \param base virtual base of this string, needed to calculate tabs
1310 * \param ts tab size
1312 * \return new virtual base
1314 * \note The procedure returns when all bytes are consumed from `src'
1317 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1320 for (i
= 0; srcsize
; i
++, src
++, dst
++, srcsize
--)
1325 int j
= TAB_SKIP (ts
, i
+ base
);
1337 /* --------------------------------------------------------------------------------------------- */
1340 * Copy `src' to `dst' expanding tabs.
1342 * \param dst destination buffer
1343 * \param dstsize size of dst buffer
1344 * \param[in,out] _src source buffer
1345 * \param srcsize size of src buffer
1346 * \param base virtual base of this string, needed to calculate tabs
1347 * \param ts tab size
1349 * \return new virtual base
1351 * \note The procedure returns when all bytes are consumed from `src'
1352 * or `dstsize' bytes are written to `dst'
1353 * \note Upon return, `src' points to the first unwritten character in source
1356 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1359 const char *src
= *_src
;
1360 for (i
= 0; i
< dstsize
&& srcsize
; i
++, src
++, dst
++, srcsize
--)
1365 int j
= TAB_SKIP (ts
, i
+ base
);
1366 if (j
> dstsize
- i
)
1382 /* --------------------------------------------------------------------------------------------- */
1385 * Read line from memory, converting tabs to spaces and padding with spaces.
1387 * \param src buffer to read from
1388 * \param srcsize size of src buffer
1389 * \param dst buffer to read to
1390 * \param dstsize size of dst buffer, excluding trailing null
1391 * \param skip number of characters to skip
1392 * \param ts tab size
1393 * \param show_cr show trailing carriage return as ^M
1395 * \return negative on error, otherwise number of bytes except padding
1398 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1406 for (i
= 0; dstsize
&& srcsize
&& *src
!= '\n'; i
++, src
++, srcsize
--)
1410 int j
= TAB_SKIP (ts
, i
+ base
);
1425 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1427 if (skip
== 0 && show_cr
)
1451 utf_ch
= dview_get_utf ((char *)src
, &w
, &res
);
1454 if (!g_unichar_isprint (utf_ch
))
1475 /* --------------------------------------------------------------------------------------------- */
1478 * Read line from memory and build attribute array.
1480 * \param src buffer to read from
1481 * \param srcsize size of src buffer
1482 * \param dst buffer to read to
1483 * \param dstsize size of dst buffer, excluding trailing null
1484 * \param skip number of characters to skip
1485 * \param ts tab size
1486 * \param show_cr show trailing carriage return as ^M
1487 * \param hdiff horizontal diff structure
1488 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1489 * \param att buffer of attributes
1491 * \return negative on error, otherwise number of bytes except padding
1494 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1495 GArray
* hdiff
, int ord
, char *att
)
1503 for (i
= 0, k
= 0; dstsize
&& srcsize
&& *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1507 int j
= TAB_SKIP (ts
, i
+ base
);
1518 *att
++ = is_inside (k
, hdiff
, ord
);
1523 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1525 if (!skip
&& show_cr
)
1530 *att
++ = is_inside (k
, hdiff
, ord
);
1532 *att
++ = is_inside (k
, hdiff
, ord
);
1538 *att
++ = is_inside (k
, hdiff
, ord
);
1552 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1555 if (!g_unichar_isprint (utf_ch
))
1561 *att
++ = is_inside (k
, hdiff
, ord
);
1578 /* --------------------------------------------------------------------------------------------- */
1581 * Read line from file, converting tabs to spaces and padding with spaces.
1583 * \param f file stream to read from
1584 * \param off offset of line inside file
1585 * \param dst buffer to read to
1586 * \param dstsize size of dst buffer, excluding trailing null
1587 * \param skip number of characters to skip
1588 * \param ts tab size
1589 * \param show_cr show trailing carriage return as ^M
1591 * \return negative on error, otherwise number of bytes except padding
1594 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1597 int old_base
= base
;
1598 const int amount
= dstsize
;
1600 size_t useful
, offset
;
1607 const char *q
= NULL
;
1608 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1609 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1611 if ((int) sizeof (tmp
) < amount
|| (int) sizeof (tmp
) <= dstsize
1612 || (int) sizeof (cvt
) < 8 * amount
)
1614 /* abnormal, but avoid buffer overflow */
1615 memset (dst
, ' ', dstsize
);
1616 dst
[dstsize
] = '\0';
1620 f_seek (f
, off
, SEEK_SET
);
1625 if (!(sz
= f_gets (tmp
, amount
, f
)))
1629 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1630 if (cvt
[base
- old_base
- 1] == '\n')
1632 q
= &cvt
[base
- old_base
- 1];
1633 base
= old_base
+ q
- cvt
+ 1;
1638 useful
= base
- skip
;
1639 offset
= skip
- old_base
;
1643 memset (dst
, ' ', dstsize
);
1644 dst
[dstsize
] = '\0';
1648 if (useful
<= dstsize
)
1652 memmove (dst
, cvt
+ offset
, useful
);
1654 if (q
== NULL
&& (sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
)))
1656 const char *ptr
= tmp
;
1657 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1667 memmove (dst
, cvt
+ offset
, dstsize
);
1669 lastch
= cvt
[offset
+ dstsize
];
1673 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1675 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1679 if (i
+ 1 < dstsize
)
1692 for (; i
< dstsize
; i
++)
1700 /* --------------------------------------------------------------------------------------------- */
1701 /* diff printers et al ****************************************************** */
1704 cc_free_elt (void *elt
)
1713 /* --------------------------------------------------------------------------------------------- */
1716 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1718 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1719 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1727 if (dsrc
== DATA_SRC_MEM
&& line
)
1729 if (sz
&& str
[sz
- 1] == '\n')
1734 p
.p
= g_strndup (str
, sz
);
1737 g_array_append_val (a
, p
);
1739 else if (dsrc
== DATA_SRC_MEM
)
1742 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1743 if (sz
&& str
[sz
- 1] == '\n')
1749 size_t new_size
= p
->u
.len
+ sz
;
1750 char *q
= g_realloc (p
->p
, new_size
);
1751 memcpy (q
+ p
->u
.len
, str
, sz
);
1756 if (dsrc
== DATA_SRC_TMP
&& (line
|| !ch
))
1758 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1759 f_write (f
, str
, sz
);
1764 /* --------------------------------------------------------------------------------------------- */
1767 redo_diff (WDiff
* dview
)
1769 FBUF
*const *f
= dview
->f
;
1779 if (dview
->opt
.quality
== 2)
1781 strcat (extra
, " -d");
1783 if (dview
->opt
.quality
== 1)
1785 strcat (extra
, " --speed-large-files");
1787 if (dview
->opt
.strip_trailing_cr
)
1789 strcat (extra
, " --strip-trailing-cr");
1791 if (dview
->opt
.ignore_tab_expansion
)
1793 strcat (extra
, " -E");
1795 if (dview
->opt
.ignore_space_change
)
1797 strcat (extra
, " -b");
1799 if (dview
->opt
.ignore_all_space
)
1801 strcat (extra
, " -w");
1803 if (dview
->opt
.ignore_case
)
1805 strcat (extra
, " -i");
1808 if (dview
->dsrc
!= DATA_SRC_MEM
)
1814 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1815 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[0], dview
->file
[1], ops
);
1818 g_array_free (ops
, TRUE
);
1822 ctx
.dsrc
= dview
->dsrc
;
1825 ctx
.a
= dview
->a
[0];
1827 rv
|= dff_reparse (0, dview
->file
[0], ops
, printer
, &ctx
);
1829 ctx
.a
= dview
->a
[1];
1831 rv
|= dff_reparse (1, dview
->file
[1], ops
, printer
, &ctx
);
1833 g_array_free (ops
, TRUE
);
1835 if (rv
!= 0 || dview
->a
[0]->len
!= dview
->a
[1]->len
)
1838 if (dview
->dsrc
== DATA_SRC_TMP
)
1844 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1846 dview
->hdiff
= g_ptr_array_new ();
1847 if (dview
->hdiff
!= NULL
)
1852 for (i
= 0; i
< dview
->a
[0]->len
; i
++)
1855 p
= &g_array_index (dview
->a
[0], DIFFLN
, i
);
1856 q
= &g_array_index (dview
->a
[1], DIFFLN
, i
);
1857 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1859 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1862 gboolean runresult
=
1863 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1867 g_array_free (h
, TRUE
);
1872 g_ptr_array_add (dview
->hdiff
, h
);
1879 /* --------------------------------------------------------------------------------------------- */
1882 destroy_hdiff (WDiff
* dview
)
1884 if (dview
->hdiff
!= NULL
)
1887 int len
= dview
->a
[0]->len
;
1888 for (i
= 0; i
< len
; i
++)
1890 GArray
*h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1893 g_array_free (h
, TRUE
);
1896 g_ptr_array_free (dview
->hdiff
, TRUE
);
1897 dview
->hdiff
= NULL
;
1900 mc_search_free (dview
->search
.handle
);
1901 dview
->search
.handle
= NULL
;
1902 g_free (dview
->search
.last_string
);
1903 dview
->search
.last_string
= NULL
;
1906 /* --------------------------------------------------------------------------------------------- */
1907 /* stuff ******************************************************************** */
1910 get_digits (unsigned int n
)
1920 /* --------------------------------------------------------------------------------------------- */
1923 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1937 p
= &g_array_index (a
, DIFFLN
, pos
);
1942 for (n
= pos
; n
> 0; n
--)
1950 *lineofs
= pos
- n
+ 1;
1958 /* --------------------------------------------------------------------------------------------- */
1961 calc_nwidth (const GArray
** const a
)
1965 get_line_numbers (a
[0], a
[0]->len
- 1, &l1
, &o1
);
1966 get_line_numbers (a
[1], a
[1]->len
- 1, &l2
, &o2
);
1971 return get_digits (l1
);
1974 /* --------------------------------------------------------------------------------------------- */
1977 find_prev_hunk (const GArray
* a
, int pos
)
1980 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1984 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1988 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1992 if (pos
> 0 && pos
< a
->len
)
1995 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1999 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
2008 /* --------------------------------------------------------------------------------------------- */
2011 find_next_hunk (const GArray
* a
, size_t pos
)
2014 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2018 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
2026 * Find start and end lines of the current hunk.
2028 * \param dview - widget WDiff
2029 * \return boolean and
2030 * start_line1 first line of current hunk (file[0])
2031 * end_line1 last line of current hunk (file[0])
2032 * start_line1 first line of current hunk (file[0])
2033 * end_line1 last line of current hunk (file[0])
2036 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
2038 const GArray
*a0
= dview
->a
[0];
2039 const GArray
*a1
= dview
->a
[1];
2049 pos
= dview
->skip_rows
;
2050 ch
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
;
2065 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2071 *start_line1
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2072 *start_line2
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2074 pos
= dview
->skip_rows
;
2075 while (pos
< a0
->len
&& ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2078 l0
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
;
2079 l1
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
;
2081 *end_line1
= max (*start_line1
, l0
);
2083 *end_line2
= max (*start_line2
, l1
);
2091 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
)
2096 f0
= fopen (dview
->file
[0], "r");
2098 while (fgets (buf
, sizeof (buf
), f0
) && line
< from1
- 1)
2101 fputs (buf
, merge_file
);
2103 while (fgets (buf
, sizeof (buf
), f0
))
2107 fputs (buf
, merge_file
);
2113 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
)
2119 f0
= fopen (dview
->file
[0], "r");
2120 f1
= fopen (dview
->file
[1], "r");
2122 while (fgets (buf
, sizeof (buf
), f0
) && line
< from1
- 1)
2125 fputs (buf
, merge_file
);
2128 while (fgets (buf
, sizeof (buf
), f1
) && line
<= to2
)
2132 fputs (buf
, merge_file
);
2134 while (fgets (buf
, sizeof (buf
), f0
))
2136 fputs (buf
, merge_file
);
2143 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
)
2149 f0
= fopen (dview
->file
[0], "r");
2150 f1
= fopen (dview
->file
[1], "r");
2152 while (fgets (buf
, sizeof (buf
), f0
) && line1
< from1
- 1)
2155 fputs (buf
, merge_file
);
2158 while (fgets (buf
, sizeof (buf
), f1
) && line2
<= to2
)
2162 fputs (buf
, merge_file
);
2164 while (fgets (buf
, sizeof (buf
), f0
))
2168 fputs (buf
, merge_file
);
2175 do_merge_hunk (WDiff
* dview
)
2178 int from1
, to1
, from2
, to2
;
2182 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2187 char *merge_file_name
= NULL
;
2191 dview
->merged
= mc_util_make_backup_if_possible (dview
->file
[0], "~~~");
2194 message (D_ERROR
, MSG_ERROR
,
2195 _(" Cannot create backup file \n %s%s \n %s "),
2196 dview
->file
[0], "~~~", unix_error_string (errno
));
2202 merge_file_fd
= mc_mkstemps (&merge_file_name
, "mcmerge", NULL
);
2203 if (merge_file_fd
== -1)
2205 message (D_ERROR
, MSG_ERROR
,
2206 _(" Cannot create temporary merge file \n %s "),
2207 unix_error_string (errno
));
2211 merge_file
= fdopen (merge_file_fd
, "w");
2216 dview_remove_hunk (dview
, merge_file
, from1
, to1
);
2219 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
);
2222 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
);
2225 fflush (merge_file
);
2226 fclose (merge_file
);
2227 res
= rewrite_backup_content (merge_file_name
, dview
->file
[0]);
2228 unlink (merge_file_name
);
2229 g_free (merge_file_name
);
2233 /* --------------------------------------------------------------------------------------------- */
2234 /* view routines and callbacks ********************************************** */
2237 dview_compute_split (WDiff
* dview
, int i
)
2240 if (dview
->bias
< 2 - dview
->half1
)
2242 dview
->bias
= 2 - dview
->half1
;
2244 if (dview
->bias
> dview
->half2
- 2)
2246 dview
->bias
= dview
->half2
- 2;
2250 /* --------------------------------------------------------------------------------------------- */
2253 dview_compute_areas (WDiff
* dview
)
2255 dview
->height
= LINES
- 2;
2256 dview
->half1
= COLS
/ 2;
2257 dview
->half2
= COLS
- dview
->half1
;
2259 dview_compute_split (dview
, 0);
2262 /* --------------------------------------------------------------------------------------------- */
2265 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2266 const char *label1
, const char *label2
, DSRC dsrc
)
2274 if (dsrc
== DATA_SRC_TMP
)
2288 if (dsrc
== DATA_SRC_ORG
)
2290 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 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2322 g_array_free (dview
->a
[0], TRUE
);
2323 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2324 g_array_free (dview
->a
[1], TRUE
);
2328 dview
->ndiff
= ndiff
;
2330 dview
->view_quit
= 0;
2333 dview
->new_frame
= 1;
2334 dview
->skip_rows
= 0;
2335 dview
->skip_cols
= 0;
2336 dview
->display_symbols
= 0;
2337 dview
->display_numbers
= 0;
2339 dview
->tab_size
= 8;
2343 dview
->search
.handle
=NULL
;
2344 dview
->search
.last_string
=NULL
;
2345 dview
->search
.last_found_line
= -1;
2346 dview
->search
.last_accessed_num_line
= 0;
2349 dview
->opt
.quality
= 0;
2350 dview
->opt
.strip_trailing_cr
= 0;
2351 dview
->opt
.ignore_tab_expansion
= 0;
2352 dview
->opt
.ignore_space_change
= 0;
2353 dview
->opt
.ignore_all_space
= 0;
2354 dview
->opt
.ignore_case
= 0;
2356 dview_compute_areas (dview
);
2361 if (dsrc
!= DATA_SRC_MEM
)
2370 /* --------------------------------------------------------------------------------------------- */
2373 dview_reread (WDiff
* dview
)
2375 int ndiff
= dview
->ndiff
;
2376 destroy_hdiff (dview
);
2378 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2379 g_array_free (dview
->a
[0], TRUE
);
2380 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2381 g_array_free (dview
->a
[1], TRUE
);
2383 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2384 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2386 ndiff
= redo_diff (dview
);
2389 dview
->ndiff
= ndiff
;
2393 /* --------------------------------------------------------------------------------------------- */
2396 dview_reinit (WDiff
* dview
)
2398 const char *quality_str
[] = {
2404 QuickWidget diffopt_widgets
[] = {
2405 QUICK_BUTTON (6, 10, 13, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2406 QUICK_BUTTON (2, 10, 13, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2407 QUICK_RADIO (3, OPTX
, 15, OPTX
,
2408 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2409 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2410 N_("strip trailing &CR"), &dview
->opt
.strip_trailing_cr
),
2411 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2412 N_("ignore all &Whitespace"), &dview
->opt
.ignore_all_space
),
2413 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2414 N_("ignore &Space change"), &dview
->opt
.ignore_space_change
),
2415 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2416 N_("ignore tab &Expansion"), &dview
->opt
.ignore_tab_expansion
),
2417 QUICK_CHECKBOX (3, OPTX
, 7, OPTY
,
2418 N_("&Ignore case"), &dview
->opt
.ignore_case
),
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 destroy_hdiff (dview
);
2446 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2447 g_array_free (dview
->a
[0], TRUE
);
2448 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2449 g_array_free (dview
->a
[1], TRUE
);
2455 /* --------------------------------------------------------------------------------------------- */
2458 dview_display_file (const WDiff
* dview
, int ord
, int r
, int c
, int height
, int width
)
2463 FBUF
*f
= dview
->f
[ord
];
2464 int skip
= dview
->skip_cols
;
2465 int display_symbols
= dview
->display_symbols
;
2466 int display_numbers
= dview
->display_numbers
;
2467 int show_cr
= dview
->show_cr
;
2468 int tab_size
= dview
->tab_size
;
2470 int nwidth
= display_numbers
;
2471 int xwidth
= display_symbols
+ display_numbers
;
2474 if (xwidth
> width
&& display_symbols
)
2477 display_symbols
= 0;
2479 if (xwidth
> width
&& display_numbers
)
2482 display_numbers
= width
;
2496 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2498 /* abnormal, but avoid buffer overflow */
2502 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2504 int ch
, next_ch
, col
;
2506 p
= (DIFFLN
*) &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2508 tty_setcolor (NORMAL_COLOR
);
2509 if (display_symbols
)
2511 tty_gotoyx (r
+ j
, c
- 2);
2512 tty_print_char (ch
);
2516 if (display_numbers
)
2518 tty_gotoyx (r
+ j
, c
- xwidth
);
2519 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2520 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2524 tty_setcolor (DFF_ADD_COLOR
);
2528 tty_setcolor (DFF_CHG_COLOR
);
2532 if (i
== (size_t) dview
->search
.last_found_line
)
2534 tty_setcolor (MARKED_SELECTED_COLOR
);
2538 if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2542 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2545 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2546 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2547 tty_gotoyx (r
+ j
, c
);
2549 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2555 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2558 if (!g_unichar_isprint (next_ch
))
2562 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2565 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2569 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2571 } else if (dview
->utf8
)
2572 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2575 next_ch
= convert_to_display_c (next_ch
);
2577 tty_print_anychar (next_ch
);
2583 else if (ch
== CHG_CH
)
2585 tty_setcolor (DFF_CHH_COLOR
);
2589 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2592 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2596 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2601 if (display_numbers
)
2603 tty_gotoyx (r
+ j
, c
- xwidth
);
2604 memset (buf
, ' ', display_numbers
);
2605 buf
[display_numbers
] = '\0';
2606 tty_print_string (buf
);
2610 tty_setcolor (DFF_DEL_COLOR
);
2614 tty_setcolor (DFF_CHD_COLOR
);
2616 memset (buf
, ' ', width
);
2619 tty_gotoyx (r
+ j
, c
);
2620 /* tty_print_nstring (buf, width); */
2622 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2628 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2631 if (!g_unichar_isprint (next_ch
))
2635 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2641 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2643 } else if (dview
->utf8
)
2644 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2647 next_ch
= convert_to_display_c (next_ch
);
2649 tty_print_anychar (next_ch
);
2655 tty_setcolor (NORMAL_COLOR
);
2657 if (width
< xwidth
- 1)
2661 memset (buf
, ' ', k
);
2663 for (; j
< height
; j
++)
2667 tty_gotoyx (r
+ j
, c
- xwidth
);
2668 /* tty_print_nstring (buf, xwidth - 1); */
2669 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2671 tty_gotoyx (r
+ j
, c
);
2672 /* tty_print_nstring (buf, width); */
2673 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2679 /* --------------------------------------------------------------------------------------------- */
2682 dview_status (const WDiff
* dview
, int ord
, int width
, int c
)
2684 int skip_rows
= dview
->skip_rows
;
2685 int skip_cols
= dview
->skip_cols
;
2689 int linenum
, lineofs
;
2691 tty_setcolor (SELECTED_COLOR
);
2694 get_line_numbers (dview
->a
[ord
], skip_rows
, &linenum
, &lineofs
);
2696 filename_width
= width
- 22;
2697 if (filename_width
< 8)
2701 if (filename_width
>= (int) sizeof (buf
))
2703 /* abnormal, but avoid buffer overflow */
2704 filename_width
= sizeof (buf
) - 1;
2706 trim (strip_home_and_password (dview
->label
[ord
]), buf
, filename_width
);
2709 tty_printf ("%-*s %6d+%-4d Col %-4d ", filename_width
, buf
, linenum
, lineofs
, skip_cols
);
2713 tty_printf ("%-*s %6d+%-4d Dif %-4d ", filename_width
, buf
, linenum
, lineofs
, dview
->ndiff
);
2717 /* --------------------------------------------------------------------------------------------- */
2720 dview_redo (WDiff
* dview
)
2722 if (dview
->display_numbers
)
2724 int old
= dview
->display_numbers
;
2725 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2726 dview
->new_frame
= (old
!= dview
->display_numbers
);
2728 dview_reread (dview
);
2731 /* --------------------------------------------------------------------------------------------- */
2734 dview_edit (WDiff
* dview
, int ord
)
2736 int linenum
, lineofs
;
2738 if (dview
->dsrc
== DATA_SRC_TMP
)
2740 error_dialog (_("Edit"), _(" Edit is disabled "));
2744 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2745 do_edit_at_line (dview
->file
[ord
], linenum
);
2747 dview_update (dview
);
2750 /* --------------------------------------------------------------------------------------------- */
2753 dview_goto_cmd (WDiff
* dview
, int ord
)
2755 static const char *title
[2] = { " Goto line (left) ", " Goto line (right) " };
2756 static char prev
[256];
2757 /* XXX some statics here, to be remembered between runs */
2762 input
= input_dialog (_(title
[ord
]), _(" Enter line: "), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2765 const char *s
= input
;
2766 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2772 for (; i
< dview
->a
[ord
]->len
; i
++)
2774 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2775 if (p
->line
== newline
)
2781 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= i
;
2782 snprintf (prev
, sizeof (prev
), "%d", newline
);
2788 /* --------------------------------------------------------------------------------------------- */
2791 dview_labels (WDiff
* dview
)
2793 Dlg_head
*h
= dview
->widget
.parent
;
2794 WButtonBar
*b
= find_buttonbar (h
);
2796 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2797 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2798 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2799 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2800 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2801 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2805 /* --------------------------------------------------------------------------------------------- */
2808 dview_event (Gpm_Event
* event
, void *x
)
2810 WDiff
*dview
= (WDiff
*) x
;
2811 int result
= MOU_NORMAL
;
2813 /* We are not interested in the release events */
2814 if (!(event
->type
& (GPM_DOWN
| GPM_DRAG
)))
2820 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
))
2822 dview
->skip_rows
-= 2;
2823 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2824 dview_update (dview
);
2827 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
))
2829 dview
->skip_rows
+= 2;
2830 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2831 dview_update (dview
);
2839 dview_save (WDiff
* dview
)
2841 gboolean res
= TRUE
;
2844 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2845 dview
->merged
= !res
;
2850 dview_do_save (WDiff
* dview
)
2852 (void) dview_save (dview
);
2856 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2860 dview_ok_to_exit (WDiff
* dview
)
2862 gboolean res
= TRUE
;
2865 switch (query_dialog
2866 (_("Quit"), _(" File was modified, Save with exit? "), D_NORMAL
, 2, _("&Yes"), _("&No")))
2873 (void) dview_save (dview
);
2877 if (mc_util_restore_from_backup_if_possible (dview
->file
[0], "~~~"))
2878 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2884 /* --------------------------------------------------------------------------------------------- */
2887 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
2889 cb_ret_t res
= MSG_HANDLED
;
2893 interactive_display (NULL
, "[Diff Viewer]");
2895 case CK_DiffDisplaySymbols
:
2896 dview
->display_symbols
^= 1;
2897 dview
->new_frame
= 1;
2899 case CK_DiffDisplayNumbers
:
2900 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
2901 dview
->new_frame
= 1;
2905 dview
->new_frame
= 1;
2911 dview
->new_frame
= 1;
2914 case CK_DiffSplitMore
:
2917 dview_compute_split (dview
, 1);
2918 dview
->new_frame
= 1;
2922 case CK_DiffSplitLess
:
2925 dview_compute_split (dview
, -1);
2926 dview
->new_frame
= 1;
2930 dview
->show_cr
^= 1;
2932 case CK_DiffSetTab2
:
2933 dview
->tab_size
= 2;
2935 case CK_DiffSetTab3
:
2936 dview
->tab_size
= 3;
2938 case CK_DiffSetTab4
:
2939 dview
->tab_size
= 4;
2941 case CK_DiffSetTab8
:
2942 dview
->tab_size
= 8;
2944 case CK_DiffSwapPanel
:
2950 case CK_DiffNextHunk
:
2951 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_next_hunk (dview
->a
[0], dview
->skip_rows
);
2953 case CK_DiffPrevHunk
:
2954 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_prev_hunk (dview
->a
[0], dview
->skip_rows
);
2957 dview_goto_cmd (dview
, TRUE
);
2961 dview->search.last_found_line = -1;
2964 case CK_DiffEditCurrent
:
2965 dview_edit (dview
, dview
->ord
);
2967 case CK_DiffMergeCurrentHunk
:
2968 do_merge_hunk (dview
);
2971 case CK_DiffEditOther
:
2972 dview_edit (dview
, dview
->ord
^ 1);
2975 dview_search_cmd (dview
);
2977 case CK_DiffContinueSearch
:
2978 dview_continue_search_cmd (dview
);
2981 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2984 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[0]->len
- 1;
2988 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2992 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2994 case CK_DiffPageDown
:
2995 dview
->skip_rows
+= dview
->height
- 2;
2996 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2999 dview
->skip_rows
-= dview
->height
- 2;
3000 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3008 case CK_DiffQuickLeft
:
3009 dview
->skip_cols
-= 8;
3011 case CK_DiffQuickRight
:
3012 dview
->skip_cols
+= 8;
3015 dview
->skip_cols
= 0;
3017 case CK_ShowCommandLine
:
3021 dview
->view_quit
= 1;
3024 dview_do_save (dview
);
3026 case CK_SelectCodepage
:
3027 dview_select_encoding (dview
);
3028 dview_reread (dview
);
3031 res
= MSG_NOT_HANDLED
;
3036 /* --------------------------------------------------------------------------------------------- */
3039 dview_handle_key (WDiff
* dview
, int key
)
3041 unsigned long command
;
3043 key
= convert_from_input_c (key
);
3045 command
= lookup_keymap_command (diff_map
, key
);
3046 if ((command
!= CK_Ignore_Key
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3050 return MSG_NOT_HANDLED
;
3053 /* --------------------------------------------------------------------------------------------- */
3056 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3058 WDiff
*dview
= (WDiff
*) w
;
3059 Dlg_head
*h
= dview
->widget
.parent
;
3065 dview_labels (dview
);
3069 dview
->new_frame
= 1;
3070 dview_update (dview
);
3074 i
= dview_handle_key (dview
, parm
);
3075 if (dview
->view_quit
)
3078 dview_update (dview
);
3081 case WIDGET_COMMAND
:
3082 i
= dview_execute_cmd (dview
, parm
);
3083 if (dview
->view_quit
)
3086 dview_update (dview
);
3090 return default_proc (msg
, parm
);
3094 /* --------------------------------------------------------------------------------------------- */
3097 dview_adjust_size (Dlg_head
* h
)
3102 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3103 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3104 bar
= find_buttonbar (h
);
3105 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3106 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3108 dview_compute_areas (dview
);
3111 /* --------------------------------------------------------------------------------------------- */
3114 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3116 WDiff
*dview
= (WDiff
*) data
;
3121 dview_adjust_size (h
);
3125 /* command from buttonbar */
3126 return send_message ((Widget
*) dview
, WIDGET_COMMAND
, parm
);
3129 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3130 if (!dview_ok_to_exit (dview
))
3135 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3139 /*** public functions ****************************************************************************/
3140 /* --------------------------------------------------------------------------------------------- */
3143 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3148 Dlg_head
*dview_dlg
;
3150 /* Create dialog and widgets, put them on the dialog */
3152 create_dlg (0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
,
3153 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3155 dview
= g_new0 (WDiff
, 1);
3157 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3158 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3160 widget_want_cursor (dview
->widget
, 0);
3162 bar
= buttonbar_new (1);
3164 add_widget (dview_dlg
, dview
);
3165 add_widget (dview_dlg
, bar
);
3167 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3169 /* Please note that if you add another widget,
3170 * you have to modify dview_adjust_size to
3175 run_dlg (dview_dlg
);
3178 destroy_dlg (dview_dlg
);
3183 /* --------------------------------------------------------------------------------------------- */
3185 #define GET_FILE_AND_STAMP(n) \
3188 real_file##n = file##n; \
3189 if (!vfs_file_is_local(file##n)) { \
3190 real_file##n = mc_getlocalcopy(file##n); \
3191 if (real_file##n != NULL) { \
3193 if (mc_stat(real_file##n, &st##n) != 0) { \
3199 #define UNGET_FILE(n) \
3201 if (use_copy##n) { \
3203 if (use_copy##n > 0) { \
3204 time_t mtime = st##n.st_mtime; \
3205 if (mc_stat(real_file##n, &st##n) == 0) { \
3206 changed = (mtime != st##n.st_mtime); \
3209 mc_ungetlocalcopy(file##n, real_file##n, changed); \
3210 g_free(real_file##n); \
3215 dview_diff_cmd (WDiff
*dview
)
3223 if ((mc_run_mode
== MC_RUN_FULL
) && (dview
== NULL
))
3225 const WPanel
*panel0
= current_panel
;
3226 const WPanel
*panel1
= other_panel
;
3227 if (get_current_index ())
3229 panel0
= other_panel
;
3230 panel1
= current_panel
;
3232 file0
= concat_dir_and_file (panel0
->cwd
, selection (panel0
)->fname
);
3233 file1
= concat_dir_and_file (panel1
->cwd
, selection (panel1
)->fname
);
3234 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3235 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3240 if (file0
!= NULL
&& !is_dir0
&& file1
!= NULL
&& !is_dir1
)
3248 GET_FILE_AND_STAMP (0);
3249 GET_FILE_AND_STAMP (1);
3250 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3252 rv
= diff_view (real_file0
, real_file1
, file0
, file1
);
3263 message (1, MSG_ERROR
, _("Need two files to compare"));
3267 /* --------------------------------------------------------------------------------------------- */
3270 dview_update (WDiff
* dview
)
3272 int height
= dview
->height
;
3276 int last
= dview
->a
[0]->len
- 1;
3278 if (dview
->skip_rows
> last
)
3280 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
3282 if (dview
->skip_rows
< 0)
3284 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3286 if (dview
->skip_cols
< 0)
3288 dview
->skip_cols
= 0;
3296 width1
= dview
->half1
+ dview
->bias
;
3297 width2
= dview
->half2
- dview
->bias
;
3304 if (dview
->new_frame
)
3306 int xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
3308 tty_setcolor (NORMAL_COLOR
);
3311 tty_draw_box (1, 0, height
, width1
, FALSE
);
3315 tty_draw_box (1, width1
, height
, width2
, FALSE
);
3321 if (xwidth
< width1
- 1)
3323 tty_gotoyx (1, xwidth
);
3324 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3325 tty_gotoyx (height
, xwidth
);
3326 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3327 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3329 if (xwidth
< width2
- 1)
3331 tty_gotoyx (1, width1
+ xwidth
);
3332 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3333 tty_gotoyx (height
, width1
+ xwidth
);
3334 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3335 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3338 dview
->new_frame
= 0;
3343 dview_status (dview
, dview
->ord
, width1
, 0);
3344 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
3348 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
3349 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
3353 /* --------------------------------------------------------------------------------------------- */