2 Copyright (C) 2007, 2010 Free Software Foundation, Inc.
4 2007 Daniel Borca <dborca@yahoo.com>
6 2010 Slava Zanko <slavazanko@gmail.com>
7 2010 Andrew Borodin <aborodin@vmail.ru>
8 2010 Ilia Maslakov <il.smind@gmail.com>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/types.h>
35 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/tty/color.h"
38 #include "lib/tty/key.h"
40 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
41 #include "lib/vfs/mc-vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
43 #include "src/cmddef.h"
44 #include "src/keybind.h"
46 #include "src/dialog.h"
48 #include "src/wtools.h"
49 #include "src/charsets.h"
50 #include "src/history.h"
51 #include "src/panel.h" /* Needed for current_panel and other_panel */
52 #include "src/layout.h" /* Needed for get_current_index and get_other_panel */
53 #include "src/main.h" /* mc_run_mode */
54 #include "src/selcodepage.h"
59 /*** global variables ****************************************************************************/
61 const global_keymap_t
*diff_map
;
63 /*** file scope macro definitions ****************************************************************/
65 #define g_array_foreach(a, TP, cbf) \
67 size_t g_array_foreach_i;\
68 TP *g_array_foreach_var=NULL; \
69 for (g_array_foreach_i=0;g_array_foreach_i < a->len; g_array_foreach_i++) \
71 g_array_foreach_var = &g_array_index(a,TP,g_array_foreach_i); \
72 (*cbf) (g_array_foreach_var); \
76 #define FILE_READ_BUF 4096
77 #define FILE_FLAG_TEMP (1 << 0)
87 #define HDIFF_ENABLE 1
88 #define HDIFF_MINCTX 5
89 #define HDIFF_DEPTH 10
91 #define TAB_SKIP(ts, pos) ((ts) - (pos) % (ts))
93 #define FILE_DIRTY(fs) \
99 /*** file scope type declarations ****************************************************************/
101 /*** file scope variables ************************************************************************/
103 /*** file scope functions ************************************************************************/
105 /* --------------------------------------------------------------------------------------------- */
108 dview_set_codeset (WDiff
* dview
)
111 const char *encoding_id
= NULL
;
114 encoding_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
115 if (encoding_id
!= NULL
)
118 conv
= str_crt_conv_from (encoding_id
);
119 if (conv
!= INVALID_CONV
)
121 if (dview
->converter
!= str_cnv_from_term
)
122 str_close_conv (dview
->converter
);
123 dview
->converter
= conv
;
125 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
132 /* --------------------------------------------------------------------------------------------- */
135 dview_select_encoding (WDiff
* dview
)
138 if (do_select_codepage ())
139 dview_set_codeset (dview
);
147 rewrite_backup_content (const char *from_file_name
, const char *to_file_name
)
153 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
156 backup_fd
= fopen (to_file_name
, "w");
157 if (backup_fd
== NULL
)
163 fwrite ((const void *) contents
, length
, 1, backup_fd
);
171 /* buffered I/O ************************************************************* */
174 * Try to open a temporary file.
176 * \param[out] name address of a pointer to store the temporary name
178 * \return file descriptor on success, negative on error
180 * \note the name is not altered if this function fails
184 open_temp (void **name
)
187 char *diff_file_name
= NULL
;
189 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
192 message (D_ERROR
, MSG_ERROR
,
193 _(" Cannot create temporary diff file \n %s "),
194 unix_error_string (errno
));
197 *name
= diff_file_name
;
201 /* --------------------------------------------------------------------------------------------- */
204 * Alocate file structure and associate file descriptor to it.
206 * \param fd file descriptor
208 * \return file structure
218 fs
= g_try_malloc (sizeof (FBUF
));
222 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
237 /* --------------------------------------------------------------------------------------------- */
240 * Free file structure without closing the file.
242 * \param fs file structure
244 * \return 0 on success, non-zero on error
250 if (fs
->flags
& FILE_FLAG_TEMP
)
252 rv
= unlink (fs
->data
);
261 /* --------------------------------------------------------------------------------------------- */
264 * Open a binary temporary file in R/W mode.
266 * \return file structure
268 * \note the file will be deleted when closed
282 fd
= open_temp (&fs
->data
);
290 fs
->flags
= FILE_FLAG_TEMP
;
294 /* --------------------------------------------------------------------------------------------- */
297 * Open a binary file in specified mode.
299 * \param filename file name
300 * \param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
302 * \return file structure
305 f_open (const char *filename
, int flags
)
316 fd
= open (filename
, flags
);
327 /* --------------------------------------------------------------------------------------------- */
330 * Read a line of bytes from file until newline or EOF.
332 * \param buf destination buffer
333 * \param size size of buffer
334 * \param fs file structure
336 * \return number of bytes read
338 * \note does not stop on null-byte
339 * \note buf will not be null-terminated
342 f_gets (char *buf
, size_t size
, FBUF
* fs
)
351 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
361 if (j
== size
|| stop
)
367 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
374 /* --------------------------------------------------------------------------------------------- */
379 * \param fs file structure
381 * \param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
383 * \return position in file, starting from begginning
385 * \note avoids thrashing read cache when possible
388 f_seek (FBUF
* fs
, off_t off
, int whence
)
392 if (fs
->len
&& whence
!= SEEK_END
)
394 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
397 if (whence
== SEEK_CUR
)
400 off
+= rv
- fs
->len
+ fs
->pos
;
402 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
404 fs
->pos
= fs
->len
+ off
- rv
;
410 rv
= lseek (fs
->fd
, off
, whence
);
418 /* --------------------------------------------------------------------------------------------- */
421 * Seek to the beginning of file, thrashing read cache.
423 * \param fs file structure
425 * \return 0 if success, non-zero on error
430 off_t rv
= lseek (fs
->fd
, 0, SEEK_SET
);
438 /* --------------------------------------------------------------------------------------------- */
441 * Write bytes to file.
443 * \param fs file structure
444 * \param buf source buffer
445 * \param size size of buffer
447 * \return number of written bytes, -1 on error
449 * \note thrashes read cache
452 f_write (FBUF
* fs
, const char *buf
, size_t size
)
454 ssize_t rv
= write (fs
->fd
, buf
, size
);
462 /* --------------------------------------------------------------------------------------------- */
465 * Truncate file to the current position.
467 * \param fs file structure
469 * \return current file size on success, negative on error
471 * \note thrashes read cache
476 off_t off
= lseek (fs
->fd
, 0, SEEK_CUR
);
479 int rv
= ftruncate (fs
->fd
, off
);
492 /* --------------------------------------------------------------------------------------------- */
497 * \param fs file structure
499 * \return 0 on success, non-zero on error
501 * \note if this is temporary file, it is deleted
506 int rv
= close (fs
->fd
);
511 /* --------------------------------------------------------------------------------------------- */
514 * Create pipe stream to process.
516 * \param cmd shell command line
517 * \param flags open mode, either O_RDONLY or O_WRONLY
519 * \return file structure
522 p_open (const char *cmd
, int flags
)
526 const char *type
= NULL
;
528 if (flags
== O_RDONLY
)
532 if (flags
== O_WRONLY
)
548 f
= popen (cmd
, type
);
560 /* --------------------------------------------------------------------------------------------- */
565 * \param fs structure
567 * \return 0 on success, non-zero on error
572 int rv
= pclose (fs
->data
);
578 * Get one char (byte) from string
580 * \param char * str, gboolean * result
582 * \return int as character or 0 and result == FALSE if fail
586 dview_get_byte (char * str
, gboolean
* result
)
594 return (unsigned char) *str
;
599 * Get utf multibyte char from string
601 * \param char * str, int * char_width, gboolean * result
603 * \return int as utf character or 0 and result == FALSE if fail
607 dview_get_utf (char * str
, int * char_width
, gboolean
* result
)
611 gchar
*next_ch
= NULL
;
623 res
= g_utf8_get_char_validated (str
, -1);
633 /* Calculate UTF-8 char width */
634 next_ch
= g_utf8_next_char (str
);
637 width
= next_ch
- str
;
650 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
653 if (text
== NULL
|| text
[0] == '\0')
655 if (g_utf8_validate (text
, -1, NULL
))
657 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
662 char *tmpbuf
, *buffer
;
663 buffer
= tmpbuf
= g_strdup (text
);
664 while (tmpbuf
[0] != '\0')
666 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
667 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
669 tmpbuf
= g_utf8_next_char (tmpbuf
);
678 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
681 return max (length
, (size_t) result
);
685 /* --------------------------------------------------------------------------------------------- */
686 /* diff parse *************************************************************** */
689 * Read decimal number from string.
691 * \param[in,out] str string to parse
692 * \param[out] n extracted number
694 * \return 0 if success, otherwise non-zero
697 scan_deci (const char **str
, int *n
)
699 const char *p
= *str
;
702 *n
= strtol (p
, &q
, 10);
711 /* --------------------------------------------------------------------------------------------- */
714 * Parse line for diff statement.
716 * \param p string to parse
717 * \param ops list of diff statements
719 * \return 0 if success, otherwise non-zero
722 scan_line (const char *p
, GArray
* ops
)
732 /* handle the following cases:
734 * NUM[,NUM]cNUM[,NUM]
736 * where NUM is a positive integer
739 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
748 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
763 else if (cmd
!= 'c' && cmd
!= 'd')
768 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
777 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
797 g_array_append_val (ops
, op
);
801 /* --------------------------------------------------------------------------------------------- */
804 * Parse diff output and extract diff statements.
806 * \param f stream to read from
807 * \param ops list of diff statements to fill
809 * \return positive number indicating number of hunks, otherwise negative
812 scan_diff (FBUF
* f
, GArray
* ops
)
817 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)))
819 if (isdigit (buf
[0]))
821 if (buf
[sz
- 1] != '\n')
826 if (scan_line (buf
, ops
) != 0)
832 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
840 /* --------------------------------------------------------------------------------------------- */
843 * Invoke diff and extract diff statements.
845 * \param args extra arguments to be passed to diff
846 * \param extra more arguments to be passed to diff
847 * \param file1 first file to compare
848 * \param file2 second file to compare
849 * \param ops list of diff statements to fill
851 * \return positive number indicating number of hunks, otherwise negative
854 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
857 static const char *opt
=
858 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
859 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
860 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
861 " --unchanged-group-format=''";
868 cmd
= g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args
, extra
, opt
, file1
, file2
);
872 f
= p_open (cmd
, O_RDONLY
);
878 rv
= scan_diff (f
, ops
);
881 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
887 /* --------------------------------------------------------------------------------------------- */
890 * Reparse and display file according to diff statements.
892 * \param ord 0 if displaying first file, 1 if displaying 2nd file
893 * \param filename file name to display
894 * \param ops list of diff statements
895 * \param printer printf-like function to be used for displaying
896 * \param ctx printer context
898 * \return 0 if success, otherwise non-zero
901 dff_reparse (int ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
914 f
= f_open (filename
, O_RDONLY
);
932 #define T1 a[ ord^1 ][0]
933 #define T2 a[ ord^1 ][1]
934 for (i
= 0; i
< ops
->len
; i
++)
937 op
= &g_array_index (ops
, DIFFCMD
, i
);
938 n
= op
->F1
- (op
->cmd
!= add_cmd
);
939 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
942 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
944 while (buf
[sz
- 1] != '\n')
946 sz
= f_gets (buf
, sizeof (buf
), f
);
949 printer (ctx
, 0, 0, 0, 1, "\n");
952 printer (ctx
, 0, 0, 0, sz
, buf
);
961 if (op
->cmd
== add_cmd
)
963 n
= op
->T2
- op
->T1
+ 1;
966 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
970 if (op
->cmd
== del_cmd
)
972 n
= op
->F2
- op
->F1
+ 1;
973 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
976 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
978 while (buf
[sz
- 1] != '\n')
980 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
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1002 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
1004 while (buf
[sz
- 1] != '\n')
1006 sz
= f_gets (buf
, sizeof (buf
), f
);
1009 printer (ctx
, 0, 0, 0, 1, "\n");
1012 printer (ctx
, 0, 0, 0, sz
, buf
);
1021 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
1024 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
1034 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1037 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1039 while (buf
[sz
- 1] != '\n')
1041 sz
= f_gets (buf
, sizeof (buf
), f
);
1044 printer (ctx
, 0, 0, 0, 1, "\n");
1047 printer (ctx
, 0, 0, 0, sz
, buf
);
1060 /* --------------------------------------------------------------------------------------------- */
1061 /* horizontal diff ********************************************************** */
1064 * Longest common substring.
1066 * \param s first string
1067 * \param m length of first string
1068 * \param t second string
1069 * \param n length of second string
1070 * \param ret list of offsets for longest common substrings inside each string
1071 * \param min minimum length of common substrings
1073 * \return 0 if success, nonzero otherwise
1076 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1084 if (m
< min
|| n
< min
)
1086 /* XXX early culling */
1090 Lprev
= g_new0 (int, n
+ 1);
1091 Lcurr
= g_new0 (int, n
+ 1);
1093 if (Lprev
== NULL
|| Lcurr
== NULL
)
1100 for (i
= 0; i
< m
; i
++)
1105 #ifdef USE_MEMSET_IN_LCS
1106 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1108 for (j
= 0; j
< n
; j
++)
1110 #ifndef USE_MEMSET_IN_LCS
1115 int v
= Lprev
[j
] + 1;
1120 g_array_set_size (ret
, 0);
1122 if (z
== v
&& z
>= min
)
1124 int off0
= i
- z
+ 1;
1125 int off1
= j
- z
+ 1;
1127 for (k
= 0; k
< ret
->len
; k
++)
1129 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1130 if ((*p
)[0] == off0
)
1134 if ((*p
)[1] >= off1
)
1144 g_array_append_val (ret
, p2
);
1160 /* --------------------------------------------------------------------------------------------- */
1163 * Scan recursively for common substrings and build ranges.
1165 * \param s first string
1166 * \param t second string
1167 * \param bracket current limits for both of the strings
1168 * \param min minimum length of common substrings
1169 * \param hdiff list of horizontal diff ranges to fill
1170 * \param depth recursion depth
1172 * \return 0 if success, nonzero otherwise
1175 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1185 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1189 len
= lcsubstr (s
+ bracket
[0].off
, bracket
[0].len
,
1190 t
+ bracket
[1].off
, bracket
[1].len
, ret
, min
);
1194 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1197 b
[0].off
= bracket
[0].off
;
1198 b
[0].len
= (*data
)[0];
1199 b
[1].off
= bracket
[1].off
;
1200 b
[1].len
= (*data
)[1];
1201 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1204 for (k
= 0; k
< ret
->len
- 1; k
++)
1206 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1207 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1208 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1209 b
[0].len
= (*data2
)[0] - (*data
)[0] - len
;
1210 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1211 b
[1].len
= (*data2
)[1] - (*data
)[1] - len
;
1212 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1215 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1216 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1217 b
[0].len
= bracket
[0].len
- (*data
)[0] - len
;
1218 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1219 b
[1].len
= bracket
[1].len
- (*data
)[1] - len
;
1220 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1223 g_array_free (ret
, TRUE
);
1228 p
[0].off
= bracket
[0].off
;
1229 p
[0].len
= bracket
[0].len
;
1230 p
[1].off
= bracket
[1].off
;
1231 p
[1].len
= bracket
[1].len
;
1232 g_array_append_val (hdiff
, p
);
1237 /* --------------------------------------------------------------------------------------------- */
1240 * Build list of horizontal diff ranges.
1242 * \param s first string
1243 * \param m length of first string
1244 * \param t second string
1245 * \param n length of second string
1246 * \param min minimum length of common substrings
1247 * \param hdiff list of horizontal diff ranges to fill
1248 * \param depth recursion depth
1250 * \return 0 if success, nonzero otherwise
1253 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1258 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1259 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1261 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1269 /* smartscan (multiple horizontal diff) */
1270 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1273 /* --------------------------------------------------------------------------------------------- */
1274 /* read line **************************************************************** */
1277 * Check if character is inside horizontal diff limits.
1279 * \param k rank of character inside line
1280 * \param hdiff horizontal diff structure
1281 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1283 * \return TRUE if inside hdiff limits, FALSE otherwise
1286 is_inside (int k
, GArray
* hdiff
, int ord
)
1290 for (i
= 0; i
< hdiff
->len
; i
++)
1293 b
= &g_array_index (hdiff
, BRACKET
, i
);
1295 start
= (*b
)[ord
].off
;
1296 end
= start
+ (*b
)[ord
].len
;
1297 if (k
>= start
&& k
< end
)
1305 /* --------------------------------------------------------------------------------------------- */
1308 * Copy `src' to `dst' expanding tabs.
1310 * \param dst destination buffer
1311 * \param src source buffer
1312 * \param srcsize size of src buffer
1313 * \param base virtual base of this string, needed to calculate tabs
1314 * \param ts tab size
1316 * \return new virtual base
1318 * \note The procedure returns when all bytes are consumed from `src'
1321 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1324 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1329 int j
= TAB_SKIP (ts
, i
+ base
);
1341 /* --------------------------------------------------------------------------------------------- */
1344 * Copy `src' to `dst' expanding tabs.
1346 * \param dst destination buffer
1347 * \param dstsize size of dst buffer
1348 * \param[in,out] _src source buffer
1349 * \param srcsize size of src buffer
1350 * \param base virtual base of this string, needed to calculate tabs
1351 * \param ts tab size
1353 * \return new virtual base
1355 * \note The procedure returns when all bytes are consumed from `src'
1356 * or `dstsize' bytes are written to `dst'
1357 * \note Upon return, `src' points to the first unwritten character in source
1360 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1363 const char *src
= *_src
;
1364 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1369 int j
= TAB_SKIP (ts
, i
+ base
);
1370 if (j
> dstsize
- i
)
1386 /* --------------------------------------------------------------------------------------------- */
1389 * Read line from memory, converting tabs to spaces and padding with spaces.
1391 * \param src buffer to read from
1392 * \param srcsize size of src buffer
1393 * \param dst buffer to read to
1394 * \param dstsize size of dst buffer, excluding trailing null
1395 * \param skip number of characters to skip
1396 * \param ts tab size
1397 * \param show_cr show trailing carriage return as ^M
1399 * \return negative on error, otherwise number of bytes except padding
1402 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1410 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1414 int j
= TAB_SKIP (ts
, i
+ base
);
1422 else if (dstsize
!= 0)
1429 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1431 if (skip
== 0 && show_cr
)
1456 utf_ch
= dview_get_utf ((char *)src
, &w
, &res
);
1459 if (!g_unichar_isprint (utf_ch
))
1471 while (dstsize
!= 0)
1480 /* --------------------------------------------------------------------------------------------- */
1483 * Read line from memory and build attribute array.
1485 * \param src buffer to read from
1486 * \param srcsize size of src buffer
1487 * \param dst buffer to read to
1488 * \param dstsize size of dst buffer, excluding trailing null
1489 * \param skip number of characters to skip
1490 * \param ts tab size
1491 * \param show_cr show trailing carriage return as ^M
1492 * \param hdiff horizontal diff structure
1493 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1494 * \param att buffer of attributes
1496 * \return negative on error, otherwise number of bytes except padding
1499 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1500 GArray
* hdiff
, int ord
, char *att
)
1508 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1512 int j
= TAB_SKIP (ts
, i
+ base
);
1520 else if (dstsize
!= 0)
1523 *att
++ = is_inside (k
, hdiff
, ord
);
1528 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1530 if (skip
== 0 && show_cr
)
1535 *att
++ = is_inside (k
, hdiff
, ord
);
1537 *att
++ = is_inside (k
, hdiff
, ord
);
1543 *att
++ = is_inside (k
, hdiff
, ord
);
1558 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1561 if (!g_unichar_isprint (utf_ch
))
1567 *att
++ = is_inside (k
, hdiff
, ord
);
1574 while (dstsize
!= 0)
1584 /* --------------------------------------------------------------------------------------------- */
1587 * Read line from file, converting tabs to spaces and padding with spaces.
1589 * \param f file stream to read from
1590 * \param off offset of line inside file
1591 * \param dst buffer to read to
1592 * \param dstsize size of dst buffer, excluding trailing null
1593 * \param skip number of characters to skip
1594 * \param ts tab size
1595 * \param show_cr show trailing carriage return as ^M
1597 * \return negative on error, otherwise number of bytes except padding
1600 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1603 int old_base
= base
;
1604 const int amount
= dstsize
;
1606 size_t useful
, offset
;
1613 const char *q
= NULL
;
1614 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1615 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1617 if ((int) sizeof (tmp
) < amount
|| (int) sizeof (tmp
) <= dstsize
1618 || (int) sizeof (cvt
) < 8 * amount
)
1620 /* abnormal, but avoid buffer overflow */
1621 memset (dst
, ' ', dstsize
);
1622 dst
[dstsize
] = '\0';
1626 f_seek (f
, off
, SEEK_SET
);
1631 sz
= f_gets (tmp
, amount
, f
);
1635 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1636 if (cvt
[base
- old_base
- 1] == '\n')
1638 q
= &cvt
[base
- old_base
- 1];
1639 base
= old_base
+ q
- cvt
+ 1;
1644 useful
= base
- skip
;
1645 offset
= skip
- old_base
;
1649 memset (dst
, ' ', dstsize
);
1650 dst
[dstsize
] = '\0';
1654 if (useful
<= dstsize
)
1657 memmove (dst
, cvt
+ offset
, useful
);
1661 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1664 const char *ptr
= tmp
;
1665 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1674 memmove (dst
, cvt
+ offset
, dstsize
);
1676 lastch
= cvt
[offset
+ dstsize
];
1680 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1682 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1686 if (i
+ 1 < dstsize
)
1699 for (; i
< dstsize
; i
++)
1707 /* --------------------------------------------------------------------------------------------- */
1708 /* diff printers et al ****************************************************** */
1711 cc_free_elt (void *elt
)
1720 /* --------------------------------------------------------------------------------------------- */
1723 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1725 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1726 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1734 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1736 if (sz
!= 0 && str
[sz
- 1] == '\n')
1741 p
.p
= g_strndup (str
, sz
);
1744 g_array_append_val (a
, p
);
1746 else if (dsrc
== DATA_SRC_MEM
)
1749 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1750 if (sz
!= 0 && str
[sz
- 1] == '\n')
1756 size_t new_size
= p
->u
.len
+ sz
;
1757 char *q
= g_realloc (p
->p
, new_size
);
1758 memcpy (q
+ p
->u
.len
, str
, sz
);
1763 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1765 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1766 f_write (f
, str
, sz
);
1771 /* --------------------------------------------------------------------------------------------- */
1774 redo_diff (WDiff
* dview
)
1776 FBUF
*const *f
= dview
->f
;
1786 if (dview
->opt
.quality
== 2)
1788 strcat (extra
, " -d");
1790 if (dview
->opt
.quality
== 1)
1792 strcat (extra
, " --speed-large-files");
1794 if (dview
->opt
.strip_trailing_cr
)
1796 strcat (extra
, " --strip-trailing-cr");
1798 if (dview
->opt
.ignore_tab_expansion
)
1800 strcat (extra
, " -E");
1802 if (dview
->opt
.ignore_space_change
)
1804 strcat (extra
, " -b");
1806 if (dview
->opt
.ignore_all_space
)
1808 strcat (extra
, " -w");
1810 if (dview
->opt
.ignore_case
)
1812 strcat (extra
, " -i");
1815 if (dview
->dsrc
!= DATA_SRC_MEM
)
1821 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1822 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[0], dview
->file
[1], ops
);
1825 g_array_free (ops
, TRUE
);
1829 ctx
.dsrc
= dview
->dsrc
;
1832 ctx
.a
= dview
->a
[0];
1834 rv
|= dff_reparse (0, dview
->file
[0], ops
, printer
, &ctx
);
1836 ctx
.a
= dview
->a
[1];
1838 rv
|= dff_reparse (1, dview
->file
[1], ops
, printer
, &ctx
);
1840 g_array_free (ops
, TRUE
);
1842 if (rv
!= 0 || dview
->a
[0]->len
!= dview
->a
[1]->len
)
1845 if (dview
->dsrc
== DATA_SRC_TMP
)
1851 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1853 dview
->hdiff
= g_ptr_array_new ();
1854 if (dview
->hdiff
!= NULL
)
1859 for (i
= 0; i
< dview
->a
[0]->len
; i
++)
1862 p
= &g_array_index (dview
->a
[0], DIFFLN
, i
);
1863 q
= &g_array_index (dview
->a
[1], DIFFLN
, i
);
1864 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1866 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1869 gboolean runresult
=
1870 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1874 g_array_free (h
, TRUE
);
1879 g_ptr_array_add (dview
->hdiff
, h
);
1886 /* --------------------------------------------------------------------------------------------- */
1889 destroy_hdiff (WDiff
* dview
)
1891 if (dview
->hdiff
!= NULL
)
1894 int len
= dview
->a
[0]->len
;
1895 for (i
= 0; i
< len
; i
++)
1897 GArray
*h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1900 g_array_free (h
, TRUE
);
1903 g_ptr_array_free (dview
->hdiff
, TRUE
);
1904 dview
->hdiff
= NULL
;
1907 mc_search_free (dview
->search
.handle
);
1908 dview
->search
.handle
= NULL
;
1909 g_free (dview
->search
.last_string
);
1910 dview
->search
.last_string
= NULL
;
1913 /* --------------------------------------------------------------------------------------------- */
1914 /* stuff ******************************************************************** */
1917 get_digits (unsigned int n
)
1927 /* --------------------------------------------------------------------------------------------- */
1930 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1944 p
= &g_array_index (a
, DIFFLN
, pos
);
1949 for (n
= pos
; n
> 0; n
--)
1957 *lineofs
= pos
- n
+ 1;
1965 /* --------------------------------------------------------------------------------------------- */
1968 calc_nwidth (const GArray
** const a
)
1972 get_line_numbers (a
[0], a
[0]->len
- 1, &l1
, &o1
);
1973 get_line_numbers (a
[1], a
[1]->len
- 1, &l2
, &o2
);
1978 return get_digits (l1
);
1981 /* --------------------------------------------------------------------------------------------- */
1984 find_prev_hunk (const GArray
* a
, int pos
)
1987 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1991 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1995 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1999 if (pos
> 0 && (size_t) pos
< a
->len
)
2002 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
2006 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
2015 /* --------------------------------------------------------------------------------------------- */
2018 find_next_hunk (const GArray
* a
, size_t pos
)
2020 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2024 while (pos
< a
->len
&& ((DIFFLN
*) &g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
2032 * Find start and end lines of the current hunk.
2034 * \param dview - widget WDiff
2035 * \return boolean and
2036 * start_line1 first line of current hunk (file[0])
2037 * end_line1 last line of current hunk (file[0])
2038 * start_line1 first line of current hunk (file[0])
2039 * end_line1 last line of current hunk (file[0])
2042 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
2044 const GArray
*a0
= dview
->a
[0];
2045 const GArray
*a1
= dview
->a
[1];
2055 pos
= dview
->skip_rows
;
2056 ch
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
;
2071 while (pos
> 0 && ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2077 *start_line1
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2078 *start_line2
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2080 pos
= dview
->skip_rows
;
2081 while (pos
< a0
->len
&& ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2084 l0
= ((DIFFLN
*) &g_array_index (a0
, DIFFLN
, pos
))->line
;
2085 l1
= ((DIFFLN
*) &g_array_index (a1
, DIFFLN
, pos
))->line
;
2087 *end_line1
= max (*start_line1
, l0
);
2089 *end_line2
= max (*start_line2
, l1
);
2097 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
)
2102 f0
= fopen (dview
->file
[0], "r");
2104 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2107 fputs (buf
, merge_file
);
2109 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2113 fputs (buf
, merge_file
);
2119 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
)
2125 f0
= fopen (dview
->file
[0], "r");
2126 f1
= fopen (dview
->file
[1], "r");
2128 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2131 fputs (buf
, merge_file
);
2134 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2138 fputs (buf
, merge_file
);
2140 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2142 fputs (buf
, merge_file
);
2149 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
)
2155 f0
= fopen (dview
->file
[0], "r");
2156 f1
= fopen (dview
->file
[1], "r");
2158 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2161 fputs (buf
, merge_file
);
2164 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2168 fputs (buf
, merge_file
);
2170 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2174 fputs (buf
, merge_file
);
2181 do_merge_hunk (WDiff
* dview
)
2183 int from1
, to1
, from2
, to2
;
2187 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2192 char *merge_file_name
= NULL
;
2196 dview
->merged
= mc_util_make_backup_if_possible (dview
->file
[0], "~~~");
2199 message (D_ERROR
, MSG_ERROR
,
2200 _(" Cannot create backup file \n %s%s \n %s "),
2201 dview
->file
[0], "~~~", unix_error_string (errno
));
2207 merge_file_fd
= mc_mkstemps (&merge_file_name
, "mcmerge", NULL
);
2208 if (merge_file_fd
== -1)
2210 message (D_ERROR
, MSG_ERROR
, _(" Cannot create temporary merge file \n %s "),
2211 unix_error_string (errno
));
2215 merge_file
= fdopen (merge_file_fd
, "w");
2220 dview_remove_hunk (dview
, merge_file
, from1
, to1
);
2223 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
);
2226 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
);
2229 fflush (merge_file
);
2230 fclose (merge_file
);
2231 res
= rewrite_backup_content (merge_file_name
, dview
->file
[0]);
2232 unlink (merge_file_name
);
2233 g_free (merge_file_name
);
2237 /* --------------------------------------------------------------------------------------------- */
2238 /* view routines and callbacks ********************************************** */
2241 dview_compute_split (WDiff
* dview
, int i
)
2244 if (dview
->bias
< 2 - dview
->half1
)
2246 dview
->bias
= 2 - dview
->half1
;
2248 if (dview
->bias
> dview
->half2
- 2)
2250 dview
->bias
= dview
->half2
- 2;
2254 /* --------------------------------------------------------------------------------------------- */
2257 dview_compute_areas (WDiff
* dview
)
2259 dview
->height
= LINES
- 2;
2260 dview
->half1
= COLS
/ 2;
2261 dview
->half2
= COLS
- dview
->half1
;
2263 dview_compute_split (dview
, 0);
2266 /* --------------------------------------------------------------------------------------------- */
2269 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2270 const char *label1
, const char *label2
, DSRC dsrc
)
2278 if (dsrc
== DATA_SRC_TMP
)
2291 else if (dsrc
== DATA_SRC_ORG
)
2293 f
[0] = f_open (file1
, O_RDONLY
);
2297 f
[1] = f_open (file2
, O_RDONLY
);
2306 dview
->file
[0] = file1
;
2307 dview
->file
[1] = file2
;
2308 dview
->label
[0] = label1
;
2309 dview
->label
[1] = label2
;
2312 dview
->hdiff
= NULL
;
2314 dview
->converter
= str_cnv_from_term
;
2315 dview_set_codeset (dview
);
2317 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2318 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2320 ndiff
= redo_diff (dview
);
2323 /* goto WIDGET_DESTROY stage: dview_fini() */
2327 dview
->ndiff
= ndiff
;
2329 dview
->view_quit
= 0;
2332 dview
->new_frame
= 1;
2333 dview
->skip_rows
= 0;
2334 dview
->skip_cols
= 0;
2335 dview
->display_symbols
= 0;
2336 dview
->display_numbers
= 0;
2338 dview
->tab_size
= 8;
2342 dview
->search
.handle
=NULL
;
2343 dview
->search
.last_string
=NULL
;
2344 dview
->search
.last_found_line
= -1;
2345 dview
->search
.last_accessed_num_line
= 0;
2347 dview
->opt
.quality
= 0;
2348 dview
->opt
.strip_trailing_cr
= 0;
2349 dview
->opt
.ignore_tab_expansion
= 0;
2350 dview
->opt
.ignore_space_change
= 0;
2351 dview
->opt
.ignore_all_space
= 0;
2352 dview
->opt
.ignore_case
= 0;
2354 dview_compute_areas (dview
);
2359 /* --------------------------------------------------------------------------------------------- */
2362 dview_reread (WDiff
* dview
)
2364 int ndiff
= dview
->ndiff
;
2365 destroy_hdiff (dview
);
2367 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2368 g_array_free (dview
->a
[0], TRUE
);
2369 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2370 g_array_free (dview
->a
[1], TRUE
);
2372 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2373 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2375 ndiff
= redo_diff (dview
);
2378 dview
->ndiff
= ndiff
;
2382 /* --------------------------------------------------------------------------------------------- */
2384 dview_diff_options (WDiff
* dview
)
2386 const char *quality_str
[] = {
2388 N_("&Fastest (Assume large files)"),
2389 N_("&Minimal (Find a smaller set of change)")
2392 QuickWidget diffopt_widgets
[] = {
2393 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2394 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2396 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2397 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2398 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2399 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2400 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2401 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2402 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2403 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2404 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2405 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2406 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2407 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2408 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2409 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2414 QuickDialog diffopt
= {
2416 N_("Diff Options"), "[Diff Options]",
2420 if (quick_dialog (&diffopt
) != B_CANCEL
)
2422 dview_reread (dview
);
2425 /* --------------------------------------------------------------------------------------------- */
2428 dview_fini (WDiff
* dview
)
2430 if (dview
->dsrc
!= DATA_SRC_MEM
)
2432 f_close (dview
->f
[1]);
2433 f_close (dview
->f
[0]);
2436 if (dview
->converter
!= str_cnv_from_term
)
2437 str_close_conv (dview
->converter
);
2439 destroy_hdiff (dview
);
2440 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2441 g_array_free (dview
->a
[0], TRUE
);
2442 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2443 g_array_free (dview
->a
[1], TRUE
);
2449 /* --------------------------------------------------------------------------------------------- */
2452 dview_display_file (const WDiff
* dview
, int ord
, int r
, int c
, int height
, int width
)
2457 FBUF
*f
= dview
->f
[ord
];
2458 int skip
= dview
->skip_cols
;
2459 int display_symbols
= dview
->display_symbols
;
2460 int display_numbers
= dview
->display_numbers
;
2461 int show_cr
= dview
->show_cr
;
2462 int tab_size
= dview
->tab_size
;
2464 int nwidth
= display_numbers
;
2465 int xwidth
= display_symbols
+ display_numbers
;
2468 if (xwidth
> width
&& display_symbols
)
2471 display_symbols
= 0;
2473 if (xwidth
> width
&& display_numbers
)
2476 display_numbers
= width
;
2490 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2492 /* abnormal, but avoid buffer overflow */
2496 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2498 int ch
, next_ch
, col
;
2500 p
= (DIFFLN
*) &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2502 tty_setcolor (NORMAL_COLOR
);
2503 if (display_symbols
)
2505 tty_gotoyx (r
+ j
, c
- 2);
2506 tty_print_char (ch
);
2510 if (display_numbers
)
2512 tty_gotoyx (r
+ j
, c
- xwidth
);
2513 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2514 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2518 tty_setcolor (DFF_ADD_COLOR
);
2522 tty_setcolor (DFF_CHG_COLOR
);
2526 if (i
== (size_t) dview
->search
.last_found_line
)
2528 tty_setcolor (MARKED_SELECTED_COLOR
);
2532 if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2536 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2539 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2540 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2541 tty_gotoyx (r
+ j
, c
);
2543 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2549 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2552 if (!g_unichar_isprint (next_ch
))
2556 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2559 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2563 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2565 } else if (dview
->utf8
)
2566 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2569 next_ch
= convert_to_display_c (next_ch
);
2571 tty_print_anychar (next_ch
);
2577 else if (ch
== CHG_CH
)
2579 tty_setcolor (DFF_CHH_COLOR
);
2583 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2586 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2590 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2595 if (display_numbers
)
2597 tty_gotoyx (r
+ j
, c
- xwidth
);
2598 memset (buf
, ' ', display_numbers
);
2599 buf
[display_numbers
] = '\0';
2600 tty_print_string (buf
);
2604 tty_setcolor (DFF_DEL_COLOR
);
2608 tty_setcolor (DFF_CHD_COLOR
);
2610 memset (buf
, ' ', width
);
2613 tty_gotoyx (r
+ j
, c
);
2614 /* tty_print_nstring (buf, width); */
2616 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2622 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2625 if (!g_unichar_isprint (next_ch
))
2629 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2635 next_ch
= convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2637 } else if (dview
->utf8
)
2638 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2641 next_ch
= convert_to_display_c (next_ch
);
2643 tty_print_anychar (next_ch
);
2649 tty_setcolor (NORMAL_COLOR
);
2651 if (width
< xwidth
- 1)
2655 memset (buf
, ' ', k
);
2657 for (; j
< height
; j
++)
2661 tty_gotoyx (r
+ j
, c
- xwidth
);
2662 /* tty_print_nstring (buf, xwidth - 1); */
2663 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2665 tty_gotoyx (r
+ j
, c
);
2666 /* tty_print_nstring (buf, width); */
2667 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2673 /* --------------------------------------------------------------------------------------------- */
2676 dview_status (const WDiff
* dview
, int ord
, int width
, int c
)
2678 int skip_rows
= dview
->skip_rows
;
2679 int skip_cols
= dview
->skip_cols
;
2683 int linenum
, lineofs
;
2685 tty_setcolor (SELECTED_COLOR
);
2688 get_line_numbers (dview
->a
[ord
], skip_rows
, &linenum
, &lineofs
);
2690 filename_width
= width
- 22;
2691 if (filename_width
< 8)
2695 if (filename_width
>= (int) sizeof (buf
))
2697 /* abnormal, but avoid buffer overflow */
2698 filename_width
= sizeof (buf
) - 1;
2700 trim (strip_home_and_password (dview
->label
[ord
]), buf
, filename_width
);
2703 tty_printf ("%-*s %6d+%-4d Col %-4d ", filename_width
, buf
, linenum
, lineofs
, skip_cols
);
2707 tty_printf ("%-*s %6d+%-4d Dif %-4d ", filename_width
, buf
, linenum
, lineofs
, dview
->ndiff
);
2711 /* --------------------------------------------------------------------------------------------- */
2714 dview_redo (WDiff
* dview
)
2716 if (dview
->display_numbers
)
2718 int old
= dview
->display_numbers
;
2719 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2720 dview
->new_frame
= (old
!= dview
->display_numbers
);
2722 dview_reread (dview
);
2725 /* --------------------------------------------------------------------------------------------- */
2728 dview_edit (WDiff
* dview
, int ord
)
2730 int linenum
, lineofs
;
2732 if (dview
->dsrc
== DATA_SRC_TMP
)
2734 error_dialog (_("Edit"), _(" Edit is disabled "));
2738 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2739 do_edit_at_line (dview
->file
[ord
], linenum
);
2741 dview_update (dview
);
2744 /* --------------------------------------------------------------------------------------------- */
2747 dview_goto_cmd (WDiff
* dview
, int ord
)
2749 static const char *title
[2] = { " Goto line (left) ", " Goto line (right) " };
2750 static char prev
[256];
2751 /* XXX some statics here, to be remembered between runs */
2756 input
= input_dialog (_(title
[ord
]), _(" Enter line: "), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2759 const char *s
= input
;
2760 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2766 for (; i
< dview
->a
[ord
]->len
; i
++)
2768 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2769 if (p
->line
== newline
)
2775 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= i
;
2776 snprintf (prev
, sizeof (prev
), "%d", newline
);
2782 /* --------------------------------------------------------------------------------------------- */
2785 dview_labels (WDiff
* dview
)
2787 Dlg_head
*h
= dview
->widget
.parent
;
2788 WButtonBar
*b
= find_buttonbar (h
);
2790 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2791 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2792 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2793 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2794 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2795 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, (Widget
*) dview
);
2796 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2800 /* --------------------------------------------------------------------------------------------- */
2803 dview_event (Gpm_Event
* event
, void *x
)
2805 WDiff
*dview
= (WDiff
*) x
;
2806 int result
= MOU_NORMAL
;
2808 /* We are not interested in the release events */
2809 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2815 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2817 dview
->skip_rows
-= 2;
2818 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2819 dview_update (dview
);
2821 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2823 dview
->skip_rows
+= 2;
2824 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2825 dview_update (dview
);
2832 dview_save (WDiff
* dview
)
2834 gboolean res
= TRUE
;
2837 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2838 dview
->merged
= !res
;
2843 dview_do_save (WDiff
* dview
)
2845 (void) dview_save (dview
);
2849 dview_save_options (WDiff
* dview
)
2851 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
2852 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
2853 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
2854 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
2855 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
2857 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
2859 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
2860 dview
->opt
.strip_trailing_cr
);
2861 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
2862 dview
->opt
.ignore_all_space
);
2863 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
2864 dview
->opt
.ignore_space_change
);
2865 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
2866 dview
->opt
.ignore_tab_expansion
);
2867 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
2871 dview_load_options (WDiff
* dview
)
2873 gboolean show_numbers
, show_symbols
;
2875 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
2877 dview
->display_symbols
= 1;
2878 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
2880 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
2881 dview
->tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
2883 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
2885 dview
->opt
.strip_trailing_cr
=
2886 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
2887 dview
->opt
.ignore_all_space
=
2888 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
2889 dview
->opt
.ignore_space_change
=
2890 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
2891 dview
->opt
.ignore_tab_expansion
=
2892 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
2893 dview
->opt
.ignore_case
=
2894 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
2896 dview
->new_frame
= 1;
2900 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2904 dview_ok_to_exit (WDiff
* dview
)
2906 gboolean res
= TRUE
;
2909 switch (query_dialog
2910 (_("Quit"), _(" File was modified, Save with exit? "), D_NORMAL
, 2, _("&Yes"), _("&No")))
2917 (void) dview_save (dview
);
2921 if (mc_util_restore_from_backup_if_possible (dview
->file
[0], "~~~"))
2922 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2928 /* --------------------------------------------------------------------------------------------- */
2931 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
2933 cb_ret_t res
= MSG_HANDLED
;
2937 interactive_display (NULL
, "[Diff Viewer]");
2939 case CK_DiffDisplaySymbols
:
2940 dview
->display_symbols
^= 1;
2941 dview
->new_frame
= 1;
2943 case CK_DiffDisplayNumbers
:
2944 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
2945 dview
->new_frame
= 1;
2949 dview
->new_frame
= 1;
2955 dview
->new_frame
= 1;
2958 case CK_DiffSplitMore
:
2961 dview_compute_split (dview
, 1);
2962 dview
->new_frame
= 1;
2966 case CK_DiffSplitLess
:
2969 dview_compute_split (dview
, -1);
2970 dview
->new_frame
= 1;
2973 case CK_DiffSetTab2
:
2974 dview
->tab_size
= 2;
2976 case CK_DiffSetTab3
:
2977 dview
->tab_size
= 3;
2979 case CK_DiffSetTab4
:
2980 dview
->tab_size
= 4;
2982 case CK_DiffSetTab8
:
2983 dview
->tab_size
= 8;
2985 case CK_DiffSwapPanel
:
2991 case CK_DiffNextHunk
:
2992 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_next_hunk (dview
->a
[0], dview
->skip_rows
);
2994 case CK_DiffPrevHunk
:
2995 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= find_prev_hunk (dview
->a
[0], dview
->skip_rows
);
2998 dview_goto_cmd (dview
, TRUE
);
3000 case CK_DiffEditCurrent
:
3001 dview_edit (dview
, dview
->ord
);
3003 case CK_DiffMergeCurrentHunk
:
3004 do_merge_hunk (dview
);
3007 case CK_DiffEditOther
:
3008 dview_edit (dview
, dview
->ord
^ 1);
3011 dview_search_cmd (dview
);
3013 case CK_DiffContinueSearch
:
3014 dview_continue_search_cmd (dview
);
3017 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3020 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[0]->len
- 1;
3024 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3028 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3030 case CK_DiffPageDown
:
3031 dview
->skip_rows
+= dview
->height
- 2;
3032 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3035 dview
->skip_rows
-= dview
->height
- 2;
3036 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3044 case CK_DiffQuickLeft
:
3045 dview
->skip_cols
-= 8;
3047 case CK_DiffQuickRight
:
3048 dview
->skip_cols
+= 8;
3051 dview
->skip_cols
= 0;
3053 case CK_ShowCommandLine
:
3057 dview
->view_quit
= 1;
3060 dview_do_save (dview
);
3062 case CK_DiffOptions
:
3063 dview_diff_options (dview
);
3065 case CK_SelectCodepage
:
3066 dview_select_encoding (dview
);
3067 dview_reread (dview
);
3068 tty_touch_screen ();
3072 res
= MSG_NOT_HANDLED
;
3077 /* --------------------------------------------------------------------------------------------- */
3080 dview_handle_key (WDiff
* dview
, int key
)
3082 unsigned long command
;
3084 key
= convert_from_input_c (key
);
3086 command
= lookup_keymap_command (diff_map
, key
);
3087 if ((command
!= CK_Ignore_Key
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3091 return MSG_NOT_HANDLED
;
3094 /* --------------------------------------------------------------------------------------------- */
3097 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3099 WDiff
*dview
= (WDiff
*) w
;
3100 Dlg_head
*h
= dview
->widget
.parent
;
3106 dview_labels (dview
);
3107 dview_load_options (dview
);
3108 dview_update (dview
);
3112 dview
->new_frame
= 1;
3113 dview_update (dview
);
3117 i
= dview_handle_key (dview
, parm
);
3118 if (dview
->view_quit
)
3121 dview_update (dview
);
3124 case WIDGET_COMMAND
:
3125 i
= dview_execute_cmd (dview
, parm
);
3126 if (dview
->view_quit
)
3129 dview_update (dview
);
3132 case WIDGET_DESTROY
:
3133 dview_save_options (dview
);
3138 return default_proc (msg
, parm
);
3142 /* --------------------------------------------------------------------------------------------- */
3145 dview_adjust_size (Dlg_head
* h
)
3150 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3151 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3152 bar
= find_buttonbar (h
);
3153 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3154 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3156 dview_compute_areas (dview
);
3159 /* --------------------------------------------------------------------------------------------- */
3162 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3164 WDiff
*dview
= (WDiff
*) data
;
3169 dview_adjust_size (h
);
3173 /* command from buttonbar */
3174 return send_message ((Widget
*) dview
, WIDGET_COMMAND
, parm
);
3177 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3178 if (!dview_ok_to_exit (dview
))
3183 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3187 /*** public functions ****************************************************************************/
3188 /* --------------------------------------------------------------------------------------------- */
3191 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3196 Dlg_head
*dview_dlg
;
3198 /* Create dialog and widgets, put them on the dialog */
3200 create_dlg (0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
,
3201 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3203 dview
= g_new0 (WDiff
, 1);
3205 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3206 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3208 widget_want_cursor (dview
->widget
, 0);
3210 bar
= buttonbar_new (1);
3212 add_widget (dview_dlg
, dview
);
3213 add_widget (dview_dlg
, bar
);
3215 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3217 /* Please note that if you add another widget,
3218 * you have to modify dview_adjust_size to
3222 run_dlg (dview_dlg
);
3223 destroy_dlg (dview_dlg
);
3228 /* --------------------------------------------------------------------------------------------- */
3230 #define GET_FILE_AND_STAMP(n) \
3233 real_file##n = file##n; \
3234 if (!vfs_file_is_local(file##n)) { \
3235 real_file##n = mc_getlocalcopy(file##n); \
3236 if (real_file##n != NULL) { \
3238 if (mc_stat(real_file##n, &st##n) != 0) { \
3244 #define UNGET_FILE(n) \
3246 if (use_copy##n) { \
3248 if (use_copy##n > 0) { \
3249 time_t mtime = st##n.st_mtime; \
3250 if (mc_stat(real_file##n, &st##n) == 0) { \
3251 changed = (mtime != st##n.st_mtime); \
3254 mc_ungetlocalcopy(file##n, real_file##n, changed); \
3255 g_free(real_file##n); \
3260 dview_diff_cmd (void)
3268 if (mc_run_mode
== MC_RUN_FULL
)
3270 const WPanel
*panel0
= current_panel
;
3271 const WPanel
*panel1
= other_panel
;
3272 if (get_current_index ())
3274 panel0
= other_panel
;
3275 panel1
= current_panel
;
3277 file0
= concat_dir_and_file (panel0
->cwd
, selection (panel0
)->fname
);
3278 file1
= concat_dir_and_file (panel1
->cwd
, selection (panel1
)->fname
);
3279 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3280 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3286 if (file0
!= NULL
&& !is_dir0
&& file1
!= NULL
&& !is_dir1
)
3294 GET_FILE_AND_STAMP (0);
3295 GET_FILE_AND_STAMP (1);
3296 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3298 rv
= diff_view (real_file0
, real_file1
, file0
, file1
);
3309 message (1, MSG_ERROR
, _("Two files are needed to compare"));
3312 /* --------------------------------------------------------------------------------------------- */
3315 dview_update (WDiff
* dview
)
3317 int height
= dview
->height
;
3321 int last
= dview
->a
[0]->len
- 1;
3323 if (dview
->skip_rows
> last
)
3325 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
3327 if (dview
->skip_rows
< 0)
3329 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3331 if (dview
->skip_cols
< 0)
3333 dview
->skip_cols
= 0;
3341 width1
= dview
->half1
+ dview
->bias
;
3342 width2
= dview
->half2
- dview
->bias
;
3349 if (dview
->new_frame
)
3351 int xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
3353 tty_setcolor (NORMAL_COLOR
);
3356 tty_draw_box (1, 0, height
, width1
, FALSE
);
3360 tty_draw_box (1, width1
, height
, width2
, FALSE
);
3366 if (xwidth
< width1
- 1)
3368 tty_gotoyx (1, xwidth
);
3369 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3370 tty_gotoyx (height
, xwidth
);
3371 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3372 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3374 if (xwidth
< width2
- 1)
3376 tty_gotoyx (1, width1
+ xwidth
);
3377 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3378 tty_gotoyx (height
, width1
+ xwidth
);
3379 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3380 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3383 dview
->new_frame
= 0;
3388 dview_status (dview
, dview
->ord
, width1
, 0);
3389 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
3393 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
3394 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
3398 /* --------------------------------------------------------------------------------------------- */