2 Copyright (C) 2007, 2010 Free Software Foundation, Inc.
4 2007 Daniel Borca <dborca@yahoo.com>
6 2010 Slava Zanko <slavazanko@gmail.com>
7 2010 Andrew Borodin <aborodin@vmail.ru>
8 2010 Ilia Maslakov <il.smind@gmail.com>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/types.h>
35 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/tty/color.h"
38 #include "lib/tty/key.h"
40 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
41 #include "lib/vfs/mc-vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
43 #include "src/cmddef.h"
44 #include "src/keybind.h"
46 #include "src/dialog.h"
48 #include "src/wtools.h"
49 #include "src/charsets.h"
50 #include "src/history.h"
51 #include "src/panel.h" /* Needed for current_panel and other_panel */
52 #include "src/layout.h" /* Needed for get_current_index and get_other_panel */
53 #include "src/main.h" /* mc_run_mode */
54 #include "src/selcodepage.h"
59 /*** global variables ****************************************************************************/
61 /*** file scope macro definitions ****************************************************************/
63 #define g_array_foreach(a, TP, cbf) \
65 size_t g_array_foreach_i;\
66 TP *g_array_foreach_var=NULL; \
67 for (g_array_foreach_i=0;g_array_foreach_i < a->len; g_array_foreach_i++) \
69 g_array_foreach_var = &g_array_index(a,TP,g_array_foreach_i); \
70 (*cbf) (g_array_foreach_var); \
74 #define FILE_READ_BUF 4096
75 #define FILE_FLAG_TEMP (1 << 0)
85 #define HDIFF_ENABLE 1
86 #define HDIFF_MINCTX 5
87 #define HDIFF_DEPTH 10
89 #define FILE_DIRTY(fs) \
96 /*** file scope type declarations ****************************************************************/
98 /*** file scope variables ************************************************************************/
100 /*** file scope functions ************************************************************************/
101 /* --------------------------------------------------------------------------------------------- */
104 TAB_SKIP (int ts
, int pos
)
106 if (ts
> 0 && ts
< 9)
107 return ts
- pos
% ts
;
112 /* --------------------------------------------------------------------------------------------- */
115 dview_set_codeset (WDiff
* dview
)
118 const char *encoding_id
= NULL
;
121 encoding_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
122 if (encoding_id
!= NULL
)
125 conv
= str_crt_conv_from (encoding_id
);
126 if (conv
!= INVALID_CONV
)
128 if (dview
->converter
!= str_cnv_from_term
)
129 str_close_conv (dview
->converter
);
130 dview
->converter
= conv
;
132 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
139 /* --------------------------------------------------------------------------------------------- */
142 dview_select_encoding (WDiff
* dview
)
145 if (do_select_codepage ())
146 dview_set_codeset (dview
);
154 rewrite_backup_content (const char *from_file_name
, const char *to_file_name
)
160 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
163 backup_fd
= fopen (to_file_name
, "w");
164 if (backup_fd
== NULL
)
170 fwrite ((const void *) contents
, length
, 1, backup_fd
);
178 /* buffered I/O ************************************************************* */
181 * Try to open a temporary file.
183 * \param[out] name address of a pointer to store the temporary name
185 * \return file descriptor on success, negative on error
187 * \note the name is not altered if this function fails
191 open_temp (void **name
)
194 char *diff_file_name
= NULL
;
196 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
199 message (D_ERROR
, MSG_ERROR
,
200 _(" Cannot create temporary diff file \n %s "), unix_error_string (errno
));
203 *name
= diff_file_name
;
207 /* --------------------------------------------------------------------------------------------- */
210 * Alocate file structure and associate file descriptor to it.
212 * \param fd file descriptor
214 * \return file structure
224 fs
= g_try_malloc (sizeof (FBUF
));
228 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
243 /* --------------------------------------------------------------------------------------------- */
246 * Free file structure without closing the file.
248 * \param fs file structure
250 * \return 0 on success, non-zero on error
256 if (fs
->flags
& FILE_FLAG_TEMP
)
258 rv
= unlink (fs
->data
);
267 /* --------------------------------------------------------------------------------------------- */
270 * Open a binary temporary file in R/W mode.
272 * \return file structure
274 * \note the file will be deleted when closed
288 fd
= open_temp (&fs
->data
);
296 fs
->flags
= FILE_FLAG_TEMP
;
300 /* --------------------------------------------------------------------------------------------- */
303 * Open a binary file in specified mode.
305 * \param filename file name
306 * \param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
308 * \return file structure
311 f_open (const char *filename
, int flags
)
322 fd
= open (filename
, flags
);
333 /* --------------------------------------------------------------------------------------------- */
336 * Read a line of bytes from file until newline or EOF.
338 * \param buf destination buffer
339 * \param size size of buffer
340 * \param fs file structure
342 * \return number of bytes read
344 * \note does not stop on null-byte
345 * \note buf will not be null-terminated
348 f_gets (char *buf
, size_t size
, FBUF
* fs
)
357 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
367 if (j
== size
|| stop
)
373 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
380 /* --------------------------------------------------------------------------------------------- */
385 * \param fs file structure
387 * \param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
389 * \return position in file, starting from begginning
391 * \note avoids thrashing read cache when possible
394 f_seek (FBUF
* fs
, off_t off
, int whence
)
398 if (fs
->len
&& whence
!= SEEK_END
)
400 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
403 if (whence
== SEEK_CUR
)
406 off
+= rv
- fs
->len
+ fs
->pos
;
408 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
410 fs
->pos
= fs
->len
+ off
- rv
;
416 rv
= lseek (fs
->fd
, off
, whence
);
424 /* --------------------------------------------------------------------------------------------- */
427 * Seek to the beginning of file, thrashing read cache.
429 * \param fs file structure
431 * \return 0 if success, non-zero on error
436 off_t rv
= lseek (fs
->fd
, 0, SEEK_SET
);
444 /* --------------------------------------------------------------------------------------------- */
447 * Write bytes to file.
449 * \param fs file structure
450 * \param buf source buffer
451 * \param size size of buffer
453 * \return number of written bytes, -1 on error
455 * \note thrashes read cache
458 f_write (FBUF
* fs
, const char *buf
, size_t size
)
460 ssize_t rv
= write (fs
->fd
, buf
, size
);
468 /* --------------------------------------------------------------------------------------------- */
471 * Truncate file to the current position.
473 * \param fs file structure
475 * \return current file size on success, negative on error
477 * \note thrashes read cache
482 off_t off
= lseek (fs
->fd
, 0, SEEK_CUR
);
485 int rv
= ftruncate (fs
->fd
, off
);
498 /* --------------------------------------------------------------------------------------------- */
503 * \param fs file structure
505 * \return 0 on success, non-zero on error
507 * \note if this is temporary file, it is deleted
512 int rv
= close (fs
->fd
);
517 /* --------------------------------------------------------------------------------------------- */
520 * Create pipe stream to process.
522 * \param cmd shell command line
523 * \param flags open mode, either O_RDONLY or O_WRONLY
525 * \return file structure
528 p_open (const char *cmd
, int flags
)
532 const char *type
= NULL
;
534 if (flags
== O_RDONLY
)
538 if (flags
== O_WRONLY
)
554 f
= popen (cmd
, type
);
566 /* --------------------------------------------------------------------------------------------- */
571 * \param fs structure
573 * \return 0 on success, non-zero on error
578 int rv
= pclose (fs
->data
);
584 * Get one char (byte) from string
586 * \param char * str, gboolean * result
588 * \return int as character or 0 and result == FALSE if fail
592 dview_get_byte (char *str
, gboolean
* result
)
600 return (unsigned char) *str
;
605 * Get utf multibyte char from string
607 * \param char * str, int * char_width, gboolean * result
609 * \return int as utf character or 0 and result == FALSE if fail
613 dview_get_utf (char *str
, int *char_width
, gboolean
* result
)
617 gchar
*next_ch
= NULL
;
629 res
= g_utf8_get_char_validated (str
, -1);
639 /* Calculate UTF-8 char width */
640 next_ch
= g_utf8_next_char (str
);
643 width
= next_ch
- str
;
656 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
659 if (text
== NULL
|| text
[0] == '\0')
661 if (g_utf8_validate (text
, -1, NULL
))
663 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
668 char *tmpbuf
, *buffer
;
669 buffer
= tmpbuf
= g_strdup (text
);
670 while (tmpbuf
[0] != '\0')
672 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
673 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
675 tmpbuf
= g_utf8_next_char (tmpbuf
);
684 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
687 return max (length
, (size_t) result
);
691 /* --------------------------------------------------------------------------------------------- */
692 /* diff parse *************************************************************** */
695 * Read decimal number from string.
697 * \param[in,out] str string to parse
698 * \param[out] n extracted number
700 * \return 0 if success, otherwise non-zero
703 scan_deci (const char **str
, int *n
)
705 const char *p
= *str
;
708 *n
= strtol (p
, &q
, 10);
717 /* --------------------------------------------------------------------------------------------- */
720 * Parse line for diff statement.
722 * \param p string to parse
723 * \param ops list of diff statements
725 * \return 0 if success, otherwise non-zero
728 scan_line (const char *p
, GArray
* ops
)
738 /* handle the following cases:
740 * NUM[,NUM]cNUM[,NUM]
742 * where NUM is a positive integer
745 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
754 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
769 else if (cmd
!= 'c' && cmd
!= 'd')
774 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
783 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
803 g_array_append_val (ops
, op
);
807 /* --------------------------------------------------------------------------------------------- */
810 * Parse diff output and extract diff statements.
812 * \param f stream to read from
813 * \param ops list of diff statements to fill
815 * \return positive number indicating number of hunks, otherwise negative
818 scan_diff (FBUF
* f
, GArray
* ops
)
823 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)))
825 if (isdigit (buf
[0]))
827 if (buf
[sz
- 1] != '\n')
832 if (scan_line (buf
, ops
) != 0)
838 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
846 /* --------------------------------------------------------------------------------------------- */
849 * Invoke diff and extract diff statements.
851 * \param args extra arguments to be passed to diff
852 * \param extra more arguments to be passed to diff
853 * \param file1 first file to compare
854 * \param file2 second file to compare
855 * \param ops list of diff statements to fill
857 * \return positive number indicating number of hunks, otherwise negative
860 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
863 static const char *opt
=
864 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
865 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
866 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
867 " --unchanged-group-format=''";
874 cmd
= g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args
, extra
, opt
, file1
, file2
);
878 f
= p_open (cmd
, O_RDONLY
);
884 rv
= scan_diff (f
, ops
);
887 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
893 /* --------------------------------------------------------------------------------------------- */
896 * Reparse and display file according to diff statements.
898 * \param ord 0 if displaying first file, 1 if displaying 2nd file
899 * \param filename file name to display
900 * \param ops list of diff statements
901 * \param printer printf-like function to be used for displaying
902 * \param ctx printer context
904 * \return 0 if success, otherwise non-zero
907 dff_reparse (int ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
920 f
= f_open (filename
, O_RDONLY
);
938 #define T1 a[ ord^1 ][0]
939 #define T2 a[ ord^1 ][1]
940 for (i
= 0; i
< ops
->len
; i
++)
943 op
= &g_array_index (ops
, DIFFCMD
, i
);
944 n
= op
->F1
- (op
->cmd
!= add_cmd
);
945 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
948 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
950 while (buf
[sz
- 1] != '\n')
952 sz
= f_gets (buf
, sizeof (buf
), f
);
955 printer (ctx
, 0, 0, 0, 1, "\n");
958 printer (ctx
, 0, 0, 0, sz
, buf
);
967 if (op
->cmd
== add_cmd
)
969 n
= op
->T2
- op
->T1
+ 1;
972 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
976 if (op
->cmd
== del_cmd
)
978 n
= op
->F2
- op
->F1
+ 1;
979 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
982 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
984 while (buf
[sz
- 1] != '\n')
986 sz
= f_gets (buf
, sizeof (buf
), f
);
989 printer (ctx
, 0, 0, 0, 1, "\n");
992 printer (ctx
, 0, 0, 0, sz
, buf
);
1004 n
= op
->F2
- op
->F1
+ 1;
1005 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1008 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
1010 while (buf
[sz
- 1] != '\n')
1012 sz
= f_gets (buf
, sizeof (buf
), f
);
1015 printer (ctx
, 0, 0, 0, 1, "\n");
1018 printer (ctx
, 0, 0, 0, sz
, buf
);
1027 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
1030 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
1040 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1043 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1045 while (buf
[sz
- 1] != '\n')
1047 sz
= f_gets (buf
, sizeof (buf
), f
);
1050 printer (ctx
, 0, 0, 0, 1, "\n");
1053 printer (ctx
, 0, 0, 0, sz
, buf
);
1066 /* --------------------------------------------------------------------------------------------- */
1067 /* horizontal diff ********************************************************** */
1070 * Longest common substring.
1072 * \param s first string
1073 * \param m length of first string
1074 * \param t second string
1075 * \param n length of second string
1076 * \param ret list of offsets for longest common substrings inside each string
1077 * \param min minimum length of common substrings
1079 * \return 0 if success, nonzero otherwise
1082 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1090 if (m
< min
|| n
< min
)
1092 /* XXX early culling */
1096 Lprev
= g_new0 (int, n
+ 1);
1097 Lcurr
= g_new0 (int, n
+ 1);
1099 if (Lprev
== NULL
|| Lcurr
== NULL
)
1106 for (i
= 0; i
< m
; i
++)
1111 #ifdef USE_MEMSET_IN_LCS
1112 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1114 for (j
= 0; j
< n
; j
++)
1116 #ifndef USE_MEMSET_IN_LCS
1121 int v
= Lprev
[j
] + 1;
1126 g_array_set_size (ret
, 0);
1128 if (z
== v
&& z
>= min
)
1130 int off0
= i
- z
+ 1;
1131 int off1
= j
- z
+ 1;
1133 for (k
= 0; k
< ret
->len
; k
++)
1135 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1136 if ((*p
)[0] == off0
)
1140 if ((*p
)[1] >= off1
)
1150 g_array_append_val (ret
, p2
);
1166 /* --------------------------------------------------------------------------------------------- */
1169 * Scan recursively for common substrings and build ranges.
1171 * \param s first string
1172 * \param t second string
1173 * \param bracket current limits for both of the strings
1174 * \param min minimum length of common substrings
1175 * \param hdiff list of horizontal diff ranges to fill
1176 * \param depth recursion depth
1178 * \return 0 if success, nonzero otherwise
1181 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1191 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1195 len
= lcsubstr (s
+ bracket
[0].off
, bracket
[0].len
,
1196 t
+ bracket
[1].off
, bracket
[1].len
, ret
, min
);
1200 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1203 b
[0].off
= bracket
[0].off
;
1204 b
[0].len
= (*data
)[0];
1205 b
[1].off
= bracket
[1].off
;
1206 b
[1].len
= (*data
)[1];
1207 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1210 for (k
= 0; k
< ret
->len
- 1; k
++)
1212 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1213 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1214 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1215 b
[0].len
= (*data2
)[0] - (*data
)[0] - len
;
1216 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1217 b
[1].len
= (*data2
)[1] - (*data
)[1] - len
;
1218 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1221 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1222 b
[0].off
= bracket
[0].off
+ (*data
)[0] + len
;
1223 b
[0].len
= bracket
[0].len
- (*data
)[0] - len
;
1224 b
[1].off
= bracket
[1].off
+ (*data
)[1] + len
;
1225 b
[1].len
= bracket
[1].len
- (*data
)[1] - len
;
1226 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1229 g_array_free (ret
, TRUE
);
1234 p
[0].off
= bracket
[0].off
;
1235 p
[0].len
= bracket
[0].len
;
1236 p
[1].off
= bracket
[1].off
;
1237 p
[1].len
= bracket
[1].len
;
1238 g_array_append_val (hdiff
, p
);
1243 /* --------------------------------------------------------------------------------------------- */
1246 * Build list of horizontal diff ranges.
1248 * \param s first string
1249 * \param m length of first string
1250 * \param t second string
1251 * \param n length of second string
1252 * \param min minimum length of common substrings
1253 * \param hdiff list of horizontal diff ranges to fill
1254 * \param depth recursion depth
1256 * \return 0 if success, nonzero otherwise
1259 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1264 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1265 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1267 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1275 /* smartscan (multiple horizontal diff) */
1276 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1279 /* --------------------------------------------------------------------------------------------- */
1280 /* read line **************************************************************** */
1283 * Check if character is inside horizontal diff limits.
1285 * \param k rank of character inside line
1286 * \param hdiff horizontal diff structure
1287 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1289 * \return TRUE if inside hdiff limits, FALSE otherwise
1292 is_inside (int k
, GArray
* hdiff
, int ord
)
1296 for (i
= 0; i
< hdiff
->len
; i
++)
1299 b
= &g_array_index (hdiff
, BRACKET
, i
);
1301 start
= (*b
)[ord
].off
;
1302 end
= start
+ (*b
)[ord
].len
;
1303 if (k
>= start
&& k
< end
)
1311 /* --------------------------------------------------------------------------------------------- */
1314 * Copy `src' to `dst' expanding tabs.
1316 * \param dst destination buffer
1317 * \param src source buffer
1318 * \param srcsize size of src buffer
1319 * \param base virtual base of this string, needed to calculate tabs
1320 * \param ts tab size
1322 * \return new virtual base
1324 * \note The procedure returns when all bytes are consumed from `src'
1327 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1330 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1335 int j
= TAB_SKIP (ts
, i
+ base
);
1347 /* --------------------------------------------------------------------------------------------- */
1350 * Copy `src' to `dst' expanding tabs.
1352 * \param dst destination buffer
1353 * \param dstsize size of dst buffer
1354 * \param[in,out] _src source buffer
1355 * \param srcsize size of src buffer
1356 * \param base virtual base of this string, needed to calculate tabs
1357 * \param ts tab size
1359 * \return new virtual base
1361 * \note The procedure returns when all bytes are consumed from `src'
1362 * or `dstsize' bytes are written to `dst'
1363 * \note Upon return, `src' points to the first unwritten character in source
1366 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1369 const char *src
= *_src
;
1370 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1375 int j
= TAB_SKIP (ts
, i
+ base
);
1376 if (j
> dstsize
- i
)
1392 /* --------------------------------------------------------------------------------------------- */
1395 * Read line from memory, converting tabs to spaces and padding with spaces.
1397 * \param src buffer to read from
1398 * \param srcsize size of src buffer
1399 * \param dst buffer to read to
1400 * \param dstsize size of dst buffer, excluding trailing null
1401 * \param skip number of characters to skip
1402 * \param ts tab size
1403 * \param show_cr show trailing carriage return as ^M
1405 * \return negative on error, otherwise number of bytes except padding
1408 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1416 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1420 int j
= TAB_SKIP (ts
, i
+ base
);
1428 else if (dstsize
!= 0)
1435 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1437 if (skip
== 0 && show_cr
)
1462 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1465 if (!g_unichar_isprint (utf_ch
))
1477 while (dstsize
!= 0)
1486 /* --------------------------------------------------------------------------------------------- */
1489 * Read line from memory and build attribute array.
1491 * \param src buffer to read from
1492 * \param srcsize size of src buffer
1493 * \param dst buffer to read to
1494 * \param dstsize size of dst buffer, excluding trailing null
1495 * \param skip number of characters to skip
1496 * \param ts tab size
1497 * \param show_cr show trailing carriage return as ^M
1498 * \param hdiff horizontal diff structure
1499 * \param ord 0 if reading from first file, 1 if reading from 2nd file
1500 * \param att buffer of attributes
1502 * \return negative on error, otherwise number of bytes except padding
1505 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1506 GArray
* hdiff
, int ord
, char *att
)
1514 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1518 int j
= TAB_SKIP (ts
, i
+ base
);
1526 else if (dstsize
!= 0)
1529 *att
++ = is_inside (k
, hdiff
, ord
);
1534 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1536 if (skip
== 0 && show_cr
)
1541 *att
++ = is_inside (k
, hdiff
, ord
);
1543 *att
++ = is_inside (k
, hdiff
, ord
);
1549 *att
++ = is_inside (k
, hdiff
, ord
);
1564 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1567 if (!g_unichar_isprint (utf_ch
))
1573 *att
++ = is_inside (k
, hdiff
, ord
);
1580 while (dstsize
!= 0)
1590 /* --------------------------------------------------------------------------------------------- */
1593 * Read line from file, converting tabs to spaces and padding with spaces.
1595 * \param f file stream to read from
1596 * \param off offset of line inside file
1597 * \param dst buffer to read to
1598 * \param dstsize size of dst buffer, excluding trailing null
1599 * \param skip number of characters to skip
1600 * \param ts tab size
1601 * \param show_cr show trailing carriage return as ^M
1603 * \return negative on error, otherwise number of bytes except padding
1606 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1609 int old_base
= base
;
1610 const int amount
= dstsize
;
1612 size_t useful
, offset
;
1619 const char *q
= NULL
;
1620 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1621 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1623 if ((int) sizeof (tmp
) < amount
|| (int) sizeof (tmp
) <= dstsize
1624 || (int) sizeof (cvt
) < 8 * amount
)
1626 /* abnormal, but avoid buffer overflow */
1627 memset (dst
, ' ', dstsize
);
1628 dst
[dstsize
] = '\0';
1632 f_seek (f
, off
, SEEK_SET
);
1637 sz
= f_gets (tmp
, amount
, f
);
1641 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1642 if (cvt
[base
- old_base
- 1] == '\n')
1644 q
= &cvt
[base
- old_base
- 1];
1645 base
= old_base
+ q
- cvt
+ 1;
1650 useful
= base
- skip
;
1651 offset
= skip
- old_base
;
1655 memset (dst
, ' ', dstsize
);
1656 dst
[dstsize
] = '\0';
1660 if (useful
<= dstsize
)
1663 memmove (dst
, cvt
+ offset
, useful
);
1667 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1670 const char *ptr
= tmp
;
1671 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1680 memmove (dst
, cvt
+ offset
, dstsize
);
1682 lastch
= cvt
[offset
+ dstsize
];
1686 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1688 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1692 if (i
+ 1 < dstsize
)
1705 for (; i
< dstsize
; i
++)
1713 /* --------------------------------------------------------------------------------------------- */
1714 /* diff printers et al ****************************************************** */
1717 cc_free_elt (void *elt
)
1726 /* --------------------------------------------------------------------------------------------- */
1729 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1731 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1732 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1740 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1742 if (sz
!= 0 && str
[sz
- 1] == '\n')
1747 p
.p
= g_strndup (str
, sz
);
1750 g_array_append_val (a
, p
);
1752 else if (dsrc
== DATA_SRC_MEM
)
1755 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1756 if (sz
!= 0 && str
[sz
- 1] == '\n')
1762 size_t new_size
= p
->u
.len
+ sz
;
1763 char *q
= g_realloc (p
->p
, new_size
);
1764 memcpy (q
+ p
->u
.len
, str
, sz
);
1769 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1771 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1772 f_write (f
, str
, sz
);
1777 /* --------------------------------------------------------------------------------------------- */
1780 redo_diff (WDiff
* dview
)
1782 FBUF
*const *f
= dview
->f
;
1792 if (dview
->opt
.quality
== 2)
1794 strcat (extra
, " -d");
1796 if (dview
->opt
.quality
== 1)
1798 strcat (extra
, " --speed-large-files");
1800 if (dview
->opt
.strip_trailing_cr
)
1802 strcat (extra
, " --strip-trailing-cr");
1804 if (dview
->opt
.ignore_tab_expansion
)
1806 strcat (extra
, " -E");
1808 if (dview
->opt
.ignore_space_change
)
1810 strcat (extra
, " -b");
1812 if (dview
->opt
.ignore_all_space
)
1814 strcat (extra
, " -w");
1816 if (dview
->opt
.ignore_case
)
1818 strcat (extra
, " -i");
1821 if (dview
->dsrc
!= DATA_SRC_MEM
)
1827 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1828 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[0], dview
->file
[1], ops
);
1831 g_array_free (ops
, TRUE
);
1835 ctx
.dsrc
= dview
->dsrc
;
1838 ctx
.a
= dview
->a
[0];
1840 rv
|= dff_reparse (0, dview
->file
[0], ops
, printer
, &ctx
);
1842 ctx
.a
= dview
->a
[1];
1844 rv
|= dff_reparse (1, dview
->file
[1], ops
, printer
, &ctx
);
1846 g_array_free (ops
, TRUE
);
1848 if (rv
!= 0 || dview
->a
[0]->len
!= dview
->a
[1]->len
)
1851 if (dview
->dsrc
== DATA_SRC_TMP
)
1857 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1859 dview
->hdiff
= g_ptr_array_new ();
1860 if (dview
->hdiff
!= NULL
)
1865 for (i
= 0; i
< dview
->a
[0]->len
; i
++)
1868 p
= &g_array_index (dview
->a
[0], DIFFLN
, i
);
1869 q
= &g_array_index (dview
->a
[1], DIFFLN
, i
);
1870 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1872 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1875 gboolean runresult
=
1876 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1880 g_array_free (h
, TRUE
);
1885 g_ptr_array_add (dview
->hdiff
, h
);
1892 /* --------------------------------------------------------------------------------------------- */
1895 destroy_hdiff (WDiff
* dview
)
1897 if (dview
->hdiff
!= NULL
)
1900 int len
= dview
->a
[0]->len
;
1901 for (i
= 0; i
< len
; i
++)
1903 GArray
*h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1906 g_array_free (h
, TRUE
);
1909 g_ptr_array_free (dview
->hdiff
, TRUE
);
1910 dview
->hdiff
= NULL
;
1913 mc_search_free (dview
->search
.handle
);
1914 dview
->search
.handle
= NULL
;
1915 g_free (dview
->search
.last_string
);
1916 dview
->search
.last_string
= NULL
;
1919 /* --------------------------------------------------------------------------------------------- */
1920 /* stuff ******************************************************************** */
1923 get_digits (unsigned int n
)
1933 /* --------------------------------------------------------------------------------------------- */
1936 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1950 p
= &g_array_index (a
, DIFFLN
, pos
);
1955 for (n
= pos
; n
> 0; n
--)
1963 *lineofs
= pos
- n
+ 1;
1971 /* --------------------------------------------------------------------------------------------- */
1974 calc_nwidth (const GArray
** const a
)
1978 get_line_numbers (a
[0], a
[0]->len
- 1, &l1
, &o1
);
1979 get_line_numbers (a
[1], a
[1]->len
- 1, &l2
, &o2
);
1984 return get_digits (l1
);
1987 /* --------------------------------------------------------------------------------------------- */
1990 find_prev_hunk (const GArray
* a
, int pos
)
1993 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1997 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
2001 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2005 if (pos
> 0 && (size_t) pos
< a
->len
)
2008 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
2012 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
2021 /* --------------------------------------------------------------------------------------------- */
2024 find_next_hunk (const GArray
* a
, size_t pos
)
2026 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2030 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
2038 * Find start and end lines of the current hunk.
2040 * \param dview - widget WDiff
2041 * \return boolean and
2042 * start_line1 first line of current hunk (file[0])
2043 * end_line1 last line of current hunk (file[0])
2044 * start_line1 first line of current hunk (file[0])
2045 * end_line1 last line of current hunk (file[0])
2048 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
2050 const GArray
*a0
= dview
->a
[0];
2051 const GArray
*a1
= dview
->a
[1];
2061 pos
= dview
->skip_rows
;
2062 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2077 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2083 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2084 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2086 pos
= dview
->skip_rows
;
2087 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2090 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2091 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2093 *end_line1
= max (*start_line1
, l0
);
2095 *end_line2
= max (*start_line2
, l1
);
2103 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
)
2108 f0
= fopen (dview
->file
[0], "r");
2110 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2113 fputs (buf
, merge_file
);
2115 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2119 fputs (buf
, merge_file
);
2125 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
)
2131 f0
= fopen (dview
->file
[0], "r");
2132 f1
= fopen (dview
->file
[1], "r");
2134 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2137 fputs (buf
, merge_file
);
2140 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2144 fputs (buf
, merge_file
);
2146 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2148 fputs (buf
, merge_file
);
2155 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
)
2161 f0
= fopen (dview
->file
[0], "r");
2162 f1
= fopen (dview
->file
[1], "r");
2164 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2167 fputs (buf
, merge_file
);
2170 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2174 fputs (buf
, merge_file
);
2176 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2180 fputs (buf
, merge_file
);
2187 do_merge_hunk (WDiff
* dview
)
2189 int from1
, to1
, from2
, to2
;
2193 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2198 char *merge_file_name
= NULL
;
2202 dview
->merged
= mc_util_make_backup_if_possible (dview
->file
[0], "~~~");
2205 message (D_ERROR
, MSG_ERROR
,
2206 _(" Cannot create backup file \n %s%s \n %s "),
2207 dview
->file
[0], "~~~", unix_error_string (errno
));
2213 merge_file_fd
= mc_mkstemps (&merge_file_name
, "mcmerge", NULL
);
2214 if (merge_file_fd
== -1)
2216 message (D_ERROR
, MSG_ERROR
, _(" Cannot create temporary merge file \n %s "),
2217 unix_error_string (errno
));
2221 merge_file
= fdopen (merge_file_fd
, "w");
2226 dview_remove_hunk (dview
, merge_file
, from1
, to1
);
2229 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
);
2232 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
);
2235 fflush (merge_file
);
2236 fclose (merge_file
);
2237 res
= rewrite_backup_content (merge_file_name
, dview
->file
[0]);
2238 unlink (merge_file_name
);
2239 g_free (merge_file_name
);
2243 /* --------------------------------------------------------------------------------------------- */
2244 /* view routines and callbacks ********************************************** */
2247 dview_compute_split (WDiff
* dview
, int i
)
2250 if (dview
->bias
< 2 - dview
->half1
)
2252 dview
->bias
= 2 - dview
->half1
;
2254 if (dview
->bias
> dview
->half2
- 2)
2256 dview
->bias
= dview
->half2
- 2;
2260 /* --------------------------------------------------------------------------------------------- */
2263 dview_compute_areas (WDiff
* dview
)
2265 dview
->height
= LINES
- 2;
2266 dview
->half1
= COLS
/ 2;
2267 dview
->half2
= COLS
- dview
->half1
;
2269 dview_compute_split (dview
, 0);
2272 /* --------------------------------------------------------------------------------------------- */
2275 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2276 const char *label1
, const char *label2
, DSRC dsrc
)
2284 if (dsrc
== DATA_SRC_TMP
)
2297 else if (dsrc
== DATA_SRC_ORG
)
2299 f
[0] = f_open (file1
, O_RDONLY
);
2303 f
[1] = f_open (file2
, O_RDONLY
);
2312 dview
->file
[0] = file1
;
2313 dview
->file
[1] = file2
;
2314 dview
->label
[0] = label1
;
2315 dview
->label
[1] = label2
;
2318 dview
->hdiff
= NULL
;
2320 dview
->converter
= str_cnv_from_term
;
2321 dview_set_codeset (dview
);
2323 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2324 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2326 ndiff
= redo_diff (dview
);
2329 /* goto WIDGET_DESTROY stage: dview_fini() */
2333 dview
->ndiff
= ndiff
;
2335 dview
->view_quit
= 0;
2338 dview
->new_frame
= 1;
2339 dview
->skip_rows
= 0;
2340 dview
->skip_cols
= 0;
2341 dview
->display_symbols
= 0;
2342 dview
->display_numbers
= 0;
2344 dview
->tab_size
= 8;
2348 dview
->search
.handle
= NULL
;
2349 dview
->search
.last_string
= NULL
;
2350 dview
->search
.last_found_line
= -1;
2351 dview
->search
.last_accessed_num_line
= 0;
2353 dview
->opt
.quality
= 0;
2354 dview
->opt
.strip_trailing_cr
= 0;
2355 dview
->opt
.ignore_tab_expansion
= 0;
2356 dview
->opt
.ignore_space_change
= 0;
2357 dview
->opt
.ignore_all_space
= 0;
2358 dview
->opt
.ignore_case
= 0;
2360 dview_compute_areas (dview
);
2365 /* --------------------------------------------------------------------------------------------- */
2368 dview_reread (WDiff
* dview
)
2370 int ndiff
= dview
->ndiff
;
2371 destroy_hdiff (dview
);
2373 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2374 g_array_free (dview
->a
[0], TRUE
);
2375 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2376 g_array_free (dview
->a
[1], TRUE
);
2378 dview
->a
[0] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2379 dview
->a
[1] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2381 ndiff
= redo_diff (dview
);
2384 dview
->ndiff
= ndiff
;
2388 /* --------------------------------------------------------------------------------------------- */
2390 dview_diff_options (WDiff
* dview
)
2392 const char *quality_str
[] = {
2394 N_("&Fastest (Assume large files)"),
2395 N_("&Minimal (Find a smaller set of change)")
2398 QuickWidget diffopt_widgets
[] = {
2399 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2400 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2402 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2403 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2404 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2405 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2406 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2407 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2408 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2409 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2410 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2411 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2412 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2413 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2414 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2415 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2420 QuickDialog diffopt
= {
2422 N_("Diff Options"), "[Diff Options]",
2426 if (quick_dialog (&diffopt
) != B_CANCEL
)
2428 dview_reread (dview
);
2432 /* --------------------------------------------------------------------------------------------- */
2435 dview_fini (WDiff
* dview
)
2437 if (dview
->dsrc
!= DATA_SRC_MEM
)
2439 f_close (dview
->f
[1]);
2440 f_close (dview
->f
[0]);
2443 if (dview
->converter
!= str_cnv_from_term
)
2444 str_close_conv (dview
->converter
);
2446 destroy_hdiff (dview
);
2447 g_array_foreach (dview
->a
[0], DIFFLN
, cc_free_elt
);
2448 g_array_free (dview
->a
[0], TRUE
);
2449 g_array_foreach (dview
->a
[1], DIFFLN
, cc_free_elt
);
2450 g_array_free (dview
->a
[1], TRUE
);
2456 /* --------------------------------------------------------------------------------------------- */
2459 dview_display_file (const WDiff
* dview
, int ord
, int r
, int c
, int height
, int width
)
2464 FBUF
*f
= dview
->f
[ord
];
2465 int skip
= dview
->skip_cols
;
2466 int display_symbols
= dview
->display_symbols
;
2467 int display_numbers
= dview
->display_numbers
;
2468 int show_cr
= dview
->show_cr
;
2471 int nwidth
= display_numbers
;
2472 int xwidth
= display_symbols
+ display_numbers
;
2473 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2474 tab_size
= dview
->tab_size
;
2478 if (xwidth
> width
&& display_symbols
)
2481 display_symbols
= 0;
2483 if (xwidth
> width
&& display_numbers
)
2486 display_numbers
= width
;
2500 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2502 /* abnormal, but avoid buffer overflow */
2506 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2508 int ch
, next_ch
, col
;
2510 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2512 tty_setcolor (NORMAL_COLOR
);
2513 if (display_symbols
)
2515 tty_gotoyx (r
+ j
, c
- 2);
2516 tty_print_char (ch
);
2520 if (display_numbers
)
2522 tty_gotoyx (r
+ j
, c
- xwidth
);
2523 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2524 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2528 tty_setcolor (DFF_ADD_COLOR
);
2532 tty_setcolor (DFF_CHG_COLOR
);
2536 if (i
== (size_t) dview
->search
.last_found_line
)
2538 tty_setcolor (MARKED_SELECTED_COLOR
);
2542 if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2546 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2549 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2550 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2551 tty_gotoyx (r
+ j
, c
);
2553 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2559 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2562 if (!g_unichar_isprint (next_ch
))
2566 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2569 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2576 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2580 else if (dview
->utf8
)
2582 convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2585 next_ch
= convert_to_display_c (next_ch
);
2587 tty_print_anychar (next_ch
);
2593 else if (ch
== CHG_CH
)
2595 tty_setcolor (DFF_CHH_COLOR
);
2599 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2602 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2606 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2611 if (display_numbers
)
2613 tty_gotoyx (r
+ j
, c
- xwidth
);
2614 memset (buf
, ' ', display_numbers
);
2615 buf
[display_numbers
] = '\0';
2616 tty_print_string (buf
);
2620 tty_setcolor (DFF_DEL_COLOR
);
2624 tty_setcolor (DFF_CHD_COLOR
);
2626 memset (buf
, ' ', width
);
2629 tty_gotoyx (r
+ j
, c
);
2630 /* tty_print_nstring (buf, width); */
2632 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2638 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2641 if (!g_unichar_isprint (next_ch
))
2645 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2654 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2657 else if (dview
->utf8
)
2658 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2661 next_ch
= convert_to_display_c (next_ch
);
2663 tty_print_anychar (next_ch
);
2669 tty_setcolor (NORMAL_COLOR
);
2671 if (width
< xwidth
- 1)
2675 memset (buf
, ' ', k
);
2677 for (; j
< height
; j
++)
2681 tty_gotoyx (r
+ j
, c
- xwidth
);
2682 /* tty_print_nstring (buf, xwidth - 1); */
2683 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2685 tty_gotoyx (r
+ j
, c
);
2686 /* tty_print_nstring (buf, width); */
2687 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2693 /* --------------------------------------------------------------------------------------------- */
2696 dview_status (const WDiff
* dview
, int ord
, int width
, int c
)
2698 int skip_rows
= dview
->skip_rows
;
2699 int skip_cols
= dview
->skip_cols
;
2703 int linenum
, lineofs
;
2705 tty_setcolor (SELECTED_COLOR
);
2708 get_line_numbers (dview
->a
[ord
], skip_rows
, &linenum
, &lineofs
);
2710 filename_width
= width
- 22;
2711 if (filename_width
< 8)
2715 if (filename_width
>= (int) sizeof (buf
))
2717 /* abnormal, but avoid buffer overflow */
2718 filename_width
= sizeof (buf
) - 1;
2720 trim (strip_home_and_password (dview
->label
[ord
]), buf
, filename_width
);
2723 tty_printf ("%-*s %6d+%-4d Col %-4d ", filename_width
, buf
, linenum
, lineofs
, skip_cols
);
2727 tty_printf ("%-*s %6d+%-4d Dif %-4d ", filename_width
, buf
, linenum
, lineofs
, dview
->ndiff
);
2731 /* --------------------------------------------------------------------------------------------- */
2734 dview_redo (WDiff
* dview
)
2736 if (dview
->display_numbers
)
2738 int old
= dview
->display_numbers
;
2739 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2740 dview
->new_frame
= (old
!= dview
->display_numbers
);
2742 dview_reread (dview
);
2745 /* --------------------------------------------------------------------------------------------- */
2748 dview_edit (WDiff
* dview
, int ord
)
2750 int linenum
, lineofs
;
2752 if (dview
->dsrc
== DATA_SRC_TMP
)
2754 error_dialog (_("Edit"), _(" Edit is disabled "));
2758 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2759 do_edit_at_line (dview
->file
[ord
], linenum
);
2761 dview_update (dview
);
2764 /* --------------------------------------------------------------------------------------------- */
2767 dview_goto_cmd (WDiff
* dview
, int ord
)
2769 static const char *title
[2] = { " Goto line (left) ", " Goto line (right) " };
2770 static char prev
[256];
2771 /* XXX some statics here, to be remembered between runs */
2776 input
= input_dialog (_(title
[ord
]), _(" Enter line: "), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2779 const char *s
= input
;
2780 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2786 for (; i
< dview
->a
[ord
]->len
; i
++)
2788 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2789 if (p
->line
== newline
)
2795 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= i
;
2796 snprintf (prev
, sizeof (prev
), "%d", newline
);
2802 /* --------------------------------------------------------------------------------------------- */
2805 dview_labels (WDiff
* dview
)
2807 Dlg_head
*h
= dview
->widget
.parent
;
2808 WButtonBar
*b
= find_buttonbar (h
);
2810 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2811 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2812 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2813 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2814 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2815 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, (Widget
*) dview
);
2816 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2820 /* --------------------------------------------------------------------------------------------- */
2823 dview_event (Gpm_Event
* event
, void *x
)
2825 WDiff
*dview
= (WDiff
*) x
;
2826 int result
= MOU_NORMAL
;
2828 /* We are not interested in the release events */
2829 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2835 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2837 dview
->skip_rows
-= 2;
2838 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2839 dview_update (dview
);
2841 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2843 dview
->skip_rows
+= 2;
2844 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2845 dview_update (dview
);
2852 dview_save (WDiff
* dview
)
2854 gboolean res
= TRUE
;
2857 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2858 dview
->merged
= !res
;
2863 dview_do_save (WDiff
* dview
)
2865 (void) dview_save (dview
);
2869 dview_save_options (WDiff
* dview
)
2871 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
2872 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
2873 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
2874 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
2875 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
2877 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
2879 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
2880 dview
->opt
.strip_trailing_cr
);
2881 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
2882 dview
->opt
.ignore_all_space
);
2883 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
2884 dview
->opt
.ignore_space_change
);
2885 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
2886 dview
->opt
.ignore_tab_expansion
);
2887 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
2891 dview_load_options (WDiff
* dview
)
2893 gboolean show_numbers
, show_symbols
;
2896 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
2898 dview
->display_symbols
= 1;
2899 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
2901 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
2902 tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
2903 if (tab_size
> 0 && tab_size
< 9)
2904 dview
->tab_size
= tab_size
;
2906 dview
->tab_size
= 8;
2908 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
2910 dview
->opt
.strip_trailing_cr
=
2911 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
2912 dview
->opt
.ignore_all_space
=
2913 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
2914 dview
->opt
.ignore_space_change
=
2915 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
2916 dview
->opt
.ignore_tab_expansion
=
2917 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
2918 dview
->opt
.ignore_case
=
2919 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
2921 dview
->new_frame
= 1;
2925 * Check if it's OK to close the diff viewer. If there are unsaved changes,
2929 dview_ok_to_exit (WDiff
* dview
)
2931 gboolean res
= TRUE
;
2934 switch (query_dialog
2935 (_("Quit"), _(" File was modified, Save with exit? "), D_NORMAL
, 2, _("&Yes"),
2943 (void) dview_save (dview
);
2947 if (mc_util_restore_from_backup_if_possible (dview
->file
[0], "~~~"))
2948 res
= mc_util_unlink_backup_if_possible (dview
->file
[0], "~~~");
2954 /* --------------------------------------------------------------------------------------------- */
2957 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
2959 cb_ret_t res
= MSG_HANDLED
;
2963 interactive_display (NULL
, "[Diff Viewer]");
2965 case CK_DiffDisplaySymbols
:
2966 dview
->display_symbols
^= 1;
2967 dview
->new_frame
= 1;
2969 case CK_DiffDisplayNumbers
:
2970 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
2971 dview
->new_frame
= 1;
2975 dview
->new_frame
= 1;
2981 dview
->new_frame
= 1;
2984 case CK_DiffSplitMore
:
2987 dview_compute_split (dview
, 1);
2988 dview
->new_frame
= 1;
2992 case CK_DiffSplitLess
:
2995 dview_compute_split (dview
, -1);
2996 dview
->new_frame
= 1;
2999 case CK_DiffSetTab2
:
3000 dview
->tab_size
= 2;
3002 case CK_DiffSetTab3
:
3003 dview
->tab_size
= 3;
3005 case CK_DiffSetTab4
:
3006 dview
->tab_size
= 4;
3008 case CK_DiffSetTab8
:
3009 dview
->tab_size
= 8;
3011 case CK_DiffSwapPanel
:
3017 case CK_DiffNextHunk
:
3018 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3019 find_next_hunk (dview
->a
[0], dview
->skip_rows
);
3021 case CK_DiffPrevHunk
:
3022 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3023 find_prev_hunk (dview
->a
[0], dview
->skip_rows
);
3026 dview_goto_cmd (dview
, TRUE
);
3028 case CK_DiffEditCurrent
:
3029 dview_edit (dview
, dview
->ord
);
3031 case CK_DiffMergeCurrentHunk
:
3032 do_merge_hunk (dview
);
3035 case CK_DiffEditOther
:
3036 dview_edit (dview
, dview
->ord
^ 1);
3039 dview_search_cmd (dview
);
3041 case CK_DiffContinueSearch
:
3042 dview_continue_search_cmd (dview
);
3045 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3048 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[0]->len
- 1;
3052 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3056 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3058 case CK_DiffPageDown
:
3059 dview
->skip_rows
+= dview
->height
- 2;
3060 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3063 dview
->skip_rows
-= dview
->height
- 2;
3064 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3072 case CK_DiffQuickLeft
:
3073 dview
->skip_cols
-= 8;
3075 case CK_DiffQuickRight
:
3076 dview
->skip_cols
+= 8;
3079 dview
->skip_cols
= 0;
3081 case CK_ShowCommandLine
:
3085 dview
->view_quit
= 1;
3088 dview_do_save (dview
);
3090 case CK_DiffOptions
:
3091 dview_diff_options (dview
);
3093 case CK_SelectCodepage
:
3094 dview_select_encoding (dview
);
3095 dview_reread (dview
);
3096 tty_touch_screen ();
3100 res
= MSG_NOT_HANDLED
;
3105 /* --------------------------------------------------------------------------------------------- */
3108 dview_handle_key (WDiff
* dview
, int key
)
3110 unsigned long command
;
3112 key
= convert_from_input_c (key
);
3114 command
= lookup_keymap_command (diff_map
, key
);
3115 if ((command
!= CK_Ignore_Key
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3119 return MSG_NOT_HANDLED
;
3122 /* --------------------------------------------------------------------------------------------- */
3125 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3127 WDiff
*dview
= (WDiff
*) w
;
3128 Dlg_head
*h
= dview
->widget
.parent
;
3134 dview_labels (dview
);
3135 dview_load_options (dview
);
3136 dview_update (dview
);
3140 dview
->new_frame
= 1;
3141 dview_update (dview
);
3145 i
= dview_handle_key (dview
, parm
);
3146 if (dview
->view_quit
)
3149 dview_update (dview
);
3152 case WIDGET_COMMAND
:
3153 i
= dview_execute_cmd (dview
, parm
);
3154 if (dview
->view_quit
)
3157 dview_update (dview
);
3160 case WIDGET_DESTROY
:
3161 dview_save_options (dview
);
3166 return default_proc (msg
, parm
);
3170 /* --------------------------------------------------------------------------------------------- */
3173 dview_adjust_size (Dlg_head
* h
)
3178 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3179 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3180 bar
= find_buttonbar (h
);
3181 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3182 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3184 dview_compute_areas (dview
);
3187 /* --------------------------------------------------------------------------------------------- */
3190 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3192 WDiff
*dview
= (WDiff
*) data
;
3197 dview_adjust_size (h
);
3201 /* command from buttonbar */
3202 return send_message ((Widget
*) dview
, WIDGET_COMMAND
, parm
);
3205 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3206 if (!dview_ok_to_exit (dview
))
3211 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3215 /*** public functions ****************************************************************************/
3216 /* --------------------------------------------------------------------------------------------- */
3219 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3224 Dlg_head
*dview_dlg
;
3226 /* Create dialog and widgets, put them on the dialog */
3228 create_dlg (0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
,
3229 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3231 dview
= g_new0 (WDiff
, 1);
3233 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3234 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3236 widget_want_cursor (dview
->widget
, 0);
3238 bar
= buttonbar_new (1);
3240 add_widget (dview_dlg
, dview
);
3241 add_widget (dview_dlg
, bar
);
3243 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3245 /* Please note that if you add another widget,
3246 * you have to modify dview_adjust_size to
3250 run_dlg (dview_dlg
);
3251 destroy_dlg (dview_dlg
);
3256 /* --------------------------------------------------------------------------------------------- */
3258 #define GET_FILE_AND_STAMP(n) \
3261 real_file##n = file##n; \
3262 if (!vfs_file_is_local(file##n)) { \
3263 real_file##n = mc_getlocalcopy(file##n); \
3264 if (real_file##n != NULL) { \
3266 if (mc_stat(real_file##n, &st##n) != 0) { \
3272 #define UNGET_FILE(n) \
3274 if (use_copy##n) { \
3276 if (use_copy##n > 0) { \
3277 time_t mtime = st##n.st_mtime; \
3278 if (mc_stat(real_file##n, &st##n) == 0) { \
3279 changed = (mtime != st##n.st_mtime); \
3282 mc_ungetlocalcopy(file##n, real_file##n, changed); \
3283 g_free(real_file##n); \
3288 dview_diff_cmd (void)
3296 if (mc_run_mode
== MC_RUN_FULL
)
3298 const WPanel
*panel0
= current_panel
;
3299 const WPanel
*panel1
= other_panel
;
3300 if (get_current_index ())
3302 panel0
= other_panel
;
3303 panel1
= current_panel
;
3305 file0
= concat_dir_and_file (panel0
->cwd
, selection (panel0
)->fname
);
3306 file1
= concat_dir_and_file (panel1
->cwd
, selection (panel1
)->fname
);
3307 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3308 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3314 if (file0
!= NULL
&& !is_dir0
&& file1
!= NULL
&& !is_dir1
)
3322 GET_FILE_AND_STAMP (0);
3323 GET_FILE_AND_STAMP (1);
3324 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3326 rv
= diff_view (real_file0
, real_file1
, file0
, file1
);
3337 message (1, MSG_ERROR
, _("Two files are needed to compare"));
3340 /* --------------------------------------------------------------------------------------------- */
3343 dview_update (WDiff
* dview
)
3345 int height
= dview
->height
;
3349 int last
= dview
->a
[0]->len
- 1;
3351 if (dview
->skip_rows
> last
)
3353 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
3355 if (dview
->skip_rows
< 0)
3357 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3359 if (dview
->skip_cols
< 0)
3361 dview
->skip_cols
= 0;
3369 width1
= dview
->half1
+ dview
->bias
;
3370 width2
= dview
->half2
- dview
->bias
;
3377 if (dview
->new_frame
)
3379 int xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
3381 tty_setcolor (NORMAL_COLOR
);
3384 tty_draw_box (1, 0, height
, width1
, FALSE
);
3388 tty_draw_box (1, width1
, height
, width2
, FALSE
);
3394 if (xwidth
< width1
- 1)
3396 tty_gotoyx (1, xwidth
);
3397 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3398 tty_gotoyx (height
, xwidth
);
3399 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3400 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3402 if (xwidth
< width2
- 1)
3404 tty_gotoyx (1, width1
+ xwidth
);
3405 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
3406 tty_gotoyx (height
, width1
+ xwidth
);
3407 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
3408 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
3411 dview
->new_frame
= 0;
3416 dview_status (dview
, dview
->ord
, width1
, 0);
3417 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
3421 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
3422 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
3426 /* --------------------------------------------------------------------------------------------- */