4 Copyright (C) 2007-2016
5 Free Software Foundation, Inc.
8 Daniel Borca <dborca@yahoo.com>, 2007
9 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
10 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013, 2016
11 Ilia Maslakov <il.smind@gmail.com>, 2010
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include <sys/types.h>
38 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/color.h"
41 #include "lib/tty/key.h"
42 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
43 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
45 #include "lib/widget.h"
46 #include "lib/strutil.h"
47 #include "lib/strescape.h" /* strutils_glob_escape() */
49 #include "lib/charsets.h"
51 #include "lib/event.h" /* mc_event_raise() */
53 #include "src/filemanager/cmd.h" /* edit_file_at_line(), view_other_cmd() */
54 #include "src/filemanager/panel.h"
55 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
57 #include "src/keybind-defaults.h"
58 #include "src/setup.h"
59 #include "src/history.h"
61 #include "src/selcodepage.h"
67 /*** global variables ****************************************************************************/
69 /*** file scope macro definitions ****************************************************************/
71 #define g_array_foreach(a, TP, cbf) \
73 size_t g_array_foreach_i;\
75 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
77 TP *g_array_foreach_var; \
79 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
80 (*cbf) (g_array_foreach_var); \
84 #define FILE_READ_BUF 4096
85 #define FILE_FLAG_TEMP (1 << 0)
92 #define HDIFF_ENABLE 1
93 #define HDIFF_MINCTX 5
94 #define HDIFF_DEPTH 10
96 #define FILE_DIRTY(fs) \
104 /*** file scope type declarations ****************************************************************/
110 } action_direction_t
;
112 /*** file scope variables ************************************************************************/
114 /*** file scope functions ************************************************************************/
115 /* --------------------------------------------------------------------------------------------- */
118 TAB_SKIP (int ts
, int pos
)
120 if (ts
> 0 && ts
< 9)
121 return ts
- pos
% ts
;
126 /* --------------------------------------------------------------------------------------------- */
129 rewrite_backup_content (const vfs_path_t
* from_file_name_vpath
, const char *to_file_name
)
134 const char *from_file_name
;
136 from_file_name
= vfs_path_get_by_index (from_file_name_vpath
, -1)->path
;
137 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
140 backup_fd
= fopen (to_file_name
, "w");
141 if (backup_fd
== NULL
)
147 length
= fwrite ((const void *) contents
, length
, 1, backup_fd
);
155 /* buffered I/O ************************************************************* */
158 * Try to open a temporary file.
159 * @note the name is not altered if this function fails
161 * @param[out] name address of a pointer to store the temporary name
162 * @return file descriptor on success, negative on error
166 open_temp (void **name
)
169 vfs_path_t
*diff_file_name
= NULL
;
171 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
174 message (D_ERROR
, MSG_ERROR
,
175 _("Cannot create temporary diff file\n%s"), unix_error_string (errno
));
178 *name
= g_strdup (vfs_path_as_str (diff_file_name
));
179 vfs_path_free (diff_file_name
);
183 /* --------------------------------------------------------------------------------------------- */
186 * Alocate file structure and associate file descriptor to it.
188 * @param fd file descriptor
189 * @return file structure
200 fs
= g_try_malloc (sizeof (FBUF
));
204 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
219 /* --------------------------------------------------------------------------------------------- */
222 * Free file structure without closing the file.
224 * @param fs file structure
225 * @return 0 on success, non-zero on error
233 if (fs
->flags
& FILE_FLAG_TEMP
)
235 rv
= unlink (fs
->data
);
243 /* --------------------------------------------------------------------------------------------- */
246 * Open a binary temporary file in R/W mode.
247 * @note the file will be deleted when closed
249 * @return file structure
261 fd
= open_temp (&fs
->data
);
269 fs
->flags
= FILE_FLAG_TEMP
;
273 /* --------------------------------------------------------------------------------------------- */
276 * Open a binary file in specified mode.
278 * @param filename file name
279 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
281 * @return file structure
285 f_open (const char *filename
, int flags
)
294 fd
= open (filename
, flags
);
305 /* --------------------------------------------------------------------------------------------- */
308 * Read a line of bytes from file until newline or EOF.
309 * @note does not stop on null-byte
310 * @note buf will not be null-terminated
312 * @param buf destination buffer
313 * @param size size of buffer
314 * @param fs file structure
316 * @return number of bytes read
320 f_gets (char *buf
, size_t size
, FBUF
* fs
)
329 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
337 if (j
== size
|| stop
)
341 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
348 /* --------------------------------------------------------------------------------------------- */
352 * @note avoids thrashing read cache when possible
354 * @param fs file structure
356 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
358 * @return position in file, starting from begginning
362 f_seek (FBUF
* fs
, off_t off
, int whence
)
366 if (fs
->len
&& whence
!= SEEK_END
)
368 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
371 if (whence
== SEEK_CUR
)
374 off
+= rv
- fs
->len
+ fs
->pos
;
376 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
378 fs
->pos
= fs
->len
+ off
- rv
;
384 rv
= lseek (fs
->fd
, off
, whence
);
390 /* --------------------------------------------------------------------------------------------- */
393 * Seek to the beginning of file, thrashing read cache.
395 * @param fs file structure
397 * @return 0 if success, non-zero on error
405 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
411 /* --------------------------------------------------------------------------------------------- */
414 * Write bytes to file.
415 * @note thrashes read cache
417 * @param fs file structure
418 * @param buf source buffer
419 * @param size size of buffer
421 * @return number of written bytes, -1 on error
425 f_write (FBUF
* fs
, const char *buf
, size_t size
)
429 rv
= write (fs
->fd
, buf
, size
);
435 /* --------------------------------------------------------------------------------------------- */
438 * Truncate file to the current position.
439 * @note thrashes read cache
441 * @param fs file structure
443 * @return current file size on success, negative on error
451 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
456 rv
= ftruncate (fs
->fd
, off
);
465 /* --------------------------------------------------------------------------------------------- */
469 * @note if this is temporary file, it is deleted
471 * @param fs file structure
472 * @return 0 on success, non-zero on error
489 /* --------------------------------------------------------------------------------------------- */
492 * Create pipe stream to process.
494 * @param cmd shell command line
495 * @param flags open mode, either O_RDONLY or O_WRONLY
497 * @return file structure
501 p_open (const char *cmd
, int flags
)
505 const char *type
= NULL
;
507 if (flags
== O_RDONLY
)
509 else if (flags
== O_WRONLY
)
519 f
= popen (cmd
, type
);
531 /* --------------------------------------------------------------------------------------------- */
536 * @param fs structure
537 * @return 0 on success, non-zero on error
547 rv
= pclose (fs
->data
);
554 /* --------------------------------------------------------------------------------------------- */
557 * Get one char (byte) from string
561 * @return TRUE on success, FALSE otherwise
565 dview_get_byte (const char *str
, int *ch
)
570 *ch
= (unsigned char) (*str
);
574 /* --------------------------------------------------------------------------------------------- */
578 * Get utf multibyte char from string
582 * @param ch_length ...
583 * @return TRUE on success, FALSE otherwise
587 dview_get_utf (const char *str
, int *ch
, int *ch_length
)
592 *ch
= g_utf8_get_char_validated (str
, -1);
596 *ch
= (unsigned char) (*str
);
603 /* Calculate UTF-8 char length */
604 next_ch
= g_utf8_next_char (str
);
605 *ch_length
= next_ch
- str
;
611 /* --------------------------------------------------------------------------------------------- */
614 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
618 if (text
== NULL
|| text
[0] == '\0')
621 if (g_utf8_validate (text
, -1, NULL
))
622 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
626 char *tmpbuf
, *buffer
;
628 buffer
= tmpbuf
= g_strdup (text
);
629 while (tmpbuf
[0] != '\0')
631 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
632 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
633 tmpbuf
= g_utf8_next_char (tmpbuf
);
640 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
643 return MAX (length
, (size_t) result
);
645 #endif /*HAVE_CHARSET */
647 /* --------------------------------------------------------------------------------------------- */
649 /* diff parse *************************************************************** */
652 * Read decimal number from string.
654 * @param[in,out] str string to parse
655 * @param[out] n extracted number
656 * @return 0 if success, otherwise non-zero
660 scan_deci (const char **str
, int *n
)
662 const char *p
= *str
;
666 *n
= strtol (p
, &q
, 10);
667 if (errno
!= 0 || p
== q
)
673 /* --------------------------------------------------------------------------------------------- */
676 * Parse line for diff statement.
678 * @param p string to parse
679 * @param ops list of diff statements
680 * @return 0 if success, otherwise non-zero
684 scan_line (const char *p
, GArray
* ops
)
693 /* handle the following cases:
695 * NUM[,NUM]cNUM[,NUM]
697 * where NUM is a positive integer
700 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
708 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
720 else if (cmd
!= 'c' && cmd
!= 'd')
723 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
731 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
737 if (cmd
== 'd' && range
!= 0)
745 g_array_append_val (ops
, op
);
749 /* --------------------------------------------------------------------------------------------- */
752 * Parse diff output and extract diff statements.
754 * @param f stream to read from
755 * @param ops list of diff statements to fill
756 * @return positive number indicating number of hunks, otherwise negative
760 scan_diff (FBUF
* f
, GArray
* ops
)
765 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
767 if (isdigit (buf
[0]))
769 if (buf
[sz
- 1] != '\n')
773 if (scan_line (buf
, ops
) != 0)
779 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
786 /* --------------------------------------------------------------------------------------------- */
789 * Invoke diff and extract diff statements.
791 * @param args extra arguments to be passed to diff
792 * @param extra more arguments to be passed to diff
793 * @param file1 first file to compare
794 * @param file2 second file to compare
795 * @param ops list of diff statements to fill
797 * @return positive number indicating number of hunks, otherwise negative
801 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
804 static const char *opt
=
805 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
806 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
807 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
808 " --unchanged-group-format=''";
814 char *file1_esc
, *file2_esc
;
816 /* escape potential $ to avoid shell variable substitutions in popen() */
817 file1_esc
= strutils_shell_escape (file1
);
818 file2_esc
= strutils_shell_escape (file2
);
819 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
826 f
= p_open (cmd
, O_RDONLY
);
832 rv
= scan_diff (f
, ops
);
835 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
841 /* --------------------------------------------------------------------------------------------- */
844 * Reparse and display file according to diff statements.
846 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
847 * @param filename file name to display
848 * @param ops list of diff statements
849 * @param printer printf-like function to be used for displaying
850 * @param ctx printer context
852 * @return 0 if success, otherwise non-zero
856 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
869 f
= f_open (filename
, O_RDONLY
);
885 #define T1 a[ ord^1 ][0]
886 #define T2 a[ ord^1 ][1]
887 for (i
= 0; i
< ops
->len
; i
++)
891 op
= &g_array_index (ops
, DIFFCMD
, i
);
892 n
= op
->F1
- (op
->cmd
!= add_cmd
);
894 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
897 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
899 while (buf
[sz
- 1] != '\n')
901 sz
= f_gets (buf
, sizeof (buf
), f
);
904 printer (ctx
, 0, 0, 0, 1, "\n");
907 printer (ctx
, 0, 0, 0, sz
, buf
);
915 if (op
->cmd
== add_cmd
)
917 n
= op
->T2
- op
->T1
+ 1;
920 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
925 if (op
->cmd
== del_cmd
)
927 n
= op
->F2
- op
->F1
+ 1;
928 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
931 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
933 while (buf
[sz
- 1] != '\n')
935 sz
= f_gets (buf
, sizeof (buf
), f
);
938 printer (ctx
, 0, 0, 0, 1, "\n");
941 printer (ctx
, 0, 0, 0, sz
, buf
);
953 n
= op
->F2
- op
->F1
+ 1;
954 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
957 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
959 while (buf
[sz
- 1] != '\n')
961 sz
= f_gets (buf
, sizeof (buf
), f
);
964 printer (ctx
, 0, 0, 0, 1, "\n");
967 printer (ctx
, 0, 0, 0, sz
, buf
);
976 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
979 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
989 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
992 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
994 while (buf
[sz
- 1] != '\n')
996 sz
= f_gets (buf
, sizeof (buf
), f
);
999 printer (ctx
, 0, 0, 0, 1, "\n");
1002 printer (ctx
, 0, 0, 0, sz
, buf
);
1015 /* --------------------------------------------------------------------------------------------- */
1017 /* horizontal diff ********************************************************** */
1020 * Longest common substring.
1022 * @param s first string
1023 * @param m length of first string
1024 * @param t second string
1025 * @param n length of second string
1026 * @param ret list of offsets for longest common substrings inside each string
1027 * @param min minimum length of common substrings
1029 * @return 0 if success, nonzero otherwise
1033 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1039 if (m
< min
|| n
< min
)
1041 /* XXX early culling */
1045 Lprev
= g_try_new0 (int, n
+ 1);
1049 Lcurr
= g_try_new0 (int, n
+ 1);
1056 for (i
= 0; i
< m
; i
++)
1063 #ifdef USE_MEMSET_IN_LCS
1064 memset (Lcurr
, 0, (n
+ 1) * sizeof (*Lcurr
));
1066 for (j
= 0; j
< n
; j
++)
1068 #ifndef USE_MEMSET_IN_LCS
1080 g_array_set_size (ret
, 0);
1082 if (z
== v
&& z
>= min
)
1090 for (k
= 0; k
< ret
->len
; k
++)
1092 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1093 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1102 g_array_append_val (ret
, p2
);
1114 /* --------------------------------------------------------------------------------------------- */
1117 * Scan recursively for common substrings and build ranges.
1119 * @param s first string
1120 * @param t second string
1121 * @param bracket current limits for both of the strings
1122 * @param min minimum length of common substrings
1123 * @param hdiff list of horizontal diff ranges to fill
1124 * @param depth recursion depth
1126 * @return 0 if success, nonzero otherwise
1130 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1141 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1145 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1146 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1150 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1153 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1154 b
[DIFF_LEFT
].len
= (*data
)[0];
1155 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1156 b
[DIFF_RIGHT
].len
= (*data
)[1];
1157 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1160 for (k
= 0; k
< ret
->len
- 1; k
++)
1162 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1163 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1164 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1165 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1166 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1167 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1168 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1171 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1172 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1173 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1174 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1175 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1176 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1179 g_array_free (ret
, TRUE
);
1184 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1185 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1186 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1187 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1188 g_array_append_val (hdiff
, p
);
1193 /* --------------------------------------------------------------------------------------------- */
1196 * Build list of horizontal diff ranges.
1198 * @param s first string
1199 * @param m length of first string
1200 * @param t second string
1201 * @param n length of second string
1202 * @param min minimum length of common substrings
1203 * @param hdiff list of horizontal diff ranges to fill
1204 * @param depth recursion depth
1206 * @return 0 if success, nonzero otherwise
1210 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1215 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1216 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1218 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1221 b
[DIFF_LEFT
].off
= i
;
1222 b
[DIFF_LEFT
].len
= m
- i
;
1223 b
[DIFF_RIGHT
].off
= i
;
1224 b
[DIFF_RIGHT
].len
= n
- i
;
1226 /* smartscan (multiple horizontal diff) */
1227 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1230 /* --------------------------------------------------------------------------------------------- */
1232 /* read line **************************************************************** */
1235 * Check if character is inside horizontal diff limits.
1237 * @param k rank of character inside line
1238 * @param hdiff horizontal diff structure
1239 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1241 * @return TRUE if inside hdiff limits, FALSE otherwise
1245 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1250 for (i
= 0; i
< hdiff
->len
; i
++)
1254 b
= &g_array_index (hdiff
, BRACKET
, i
);
1255 start
= (*b
)[ord
].off
;
1256 end
= start
+ (*b
)[ord
].len
;
1257 if (k
>= start
&& k
< end
)
1263 /* --------------------------------------------------------------------------------------------- */
1266 * Copy 'src' to 'dst' expanding tabs.
1267 * @note The procedure returns when all bytes are consumed from 'src'
1269 * @param dst destination buffer
1270 * @param src source buffer
1271 * @param srcsize size of src buffer
1272 * @param base virtual base of this string, needed to calculate tabs
1273 * @param ts tab size
1275 * @return new virtual base
1279 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1283 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1290 j
= TAB_SKIP (ts
, i
+ base
);
1300 /* --------------------------------------------------------------------------------------------- */
1303 * Copy 'src' to 'dst' expanding tabs.
1305 * @param dst destination buffer
1306 * @param dstsize size of dst buffer
1307 * @param[in,out] _src source buffer
1308 * @param srcsize size of src buffer
1309 * @param base virtual base of this string, needed to calculate tabs
1310 * @param ts tab size
1312 * @return new virtual base
1314 * @note The procedure returns when all bytes are consumed from 'src'
1315 * or 'dstsize' bytes are written to 'dst'
1316 * @note Upon return, 'src' points to the first unwritten character in source
1320 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1323 const char *src
= *_src
;
1325 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1332 j
= TAB_SKIP (ts
, i
+ base
);
1333 if (j
> dstsize
- i
)
1345 /* --------------------------------------------------------------------------------------------- */
1348 * Read line from memory, converting tabs to spaces and padding with spaces.
1350 * @param src buffer to read from
1351 * @param srcsize size of src buffer
1352 * @param dst buffer to read to
1353 * @param dstsize size of dst buffer, excluding trailing null
1354 * @param skip number of characters to skip
1355 * @param ts tab size
1356 * @param show_cr show trailing carriage return as ^M
1358 * @return negative on error, otherwise number of bytes except padding
1362 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
,
1373 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1379 j
= TAB_SKIP (ts
, i
+ base
);
1385 else if (dstsize
!= 0)
1392 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1394 if (skip
== 0 && show_cr
)
1416 (void) dview_get_utf (src
, &ch
, &ch_length
);
1419 skip
+= ch_length
- 1;
1432 while (dstsize
!= 0)
1441 /* --------------------------------------------------------------------------------------------- */
1444 * Read line from memory and build attribute array.
1446 * @param src buffer to read from
1447 * @param srcsize size of src buffer
1448 * @param dst buffer to read to
1449 * @param dstsize size of dst buffer, excluding trailing null
1450 * @param skip number of characters to skip
1451 * @param ts tab size
1452 * @param show_cr show trailing carriage return as ^M
1453 * @param hdiff horizontal diff structure
1454 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1455 * @param att buffer of attributes
1457 * @return negative on error, otherwise number of bytes except padding
1461 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
,
1462 gboolean show_cr
, GArray
* hdiff
, diff_place_t ord
, char *att
)
1472 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1478 j
= TAB_SKIP (ts
, i
+ base
);
1484 else if (dstsize
!= 0)
1487 *att
++ = is_inside (k
, hdiff
, ord
);
1492 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1494 if (skip
== 0 && show_cr
)
1499 *att
++ = is_inside (k
, hdiff
, ord
);
1501 *att
++ = is_inside (k
, hdiff
, ord
);
1507 *att
++ = is_inside (k
, hdiff
, ord
);
1519 (void) dview_get_utf (src
, &ch
, &ch_length
);
1521 skip
+= ch_length
- 1;
1529 *att
++ = is_inside (k
, hdiff
, ord
);
1535 while (dstsize
!= 0)
1545 /* --------------------------------------------------------------------------------------------- */
1548 * Read line from file, converting tabs to spaces and padding with spaces.
1550 * @param f file stream to read from
1551 * @param off offset of line inside file
1552 * @param dst buffer to read to
1553 * @param dstsize size of dst buffer, excluding trailing null
1554 * @param skip number of characters to skip
1555 * @param ts tab size
1556 * @param show_cr show trailing carriage return as ^M
1558 * @return negative on error, otherwise number of bytes except padding
1562 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, gboolean show_cr
)
1565 int old_base
= base
;
1566 size_t amount
= dstsize
;
1567 size_t useful
, offset
;
1571 const char *q
= NULL
;
1572 char tmp
[BUFSIZ
]; /* XXX capacity must be >= MAX{dstsize + 1, amount} */
1573 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1575 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1577 /* abnormal, but avoid buffer overflow */
1578 memset (dst
, ' ', dstsize
);
1579 dst
[dstsize
] = '\0';
1583 f_seek (f
, off
, SEEK_SET
);
1588 sz
= f_gets (tmp
, amount
, f
);
1592 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1593 if (cvt
[base
- old_base
- 1] == '\n')
1595 q
= &cvt
[base
- old_base
- 1];
1596 base
= old_base
+ q
- cvt
+ 1;
1603 memset (dst
, ' ', dstsize
);
1604 dst
[dstsize
] = '\0';
1608 useful
= base
- skip
;
1609 offset
= skip
- old_base
;
1611 if (useful
<= dstsize
)
1614 memmove (dst
, cvt
+ offset
, useful
);
1618 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1621 const char *ptr
= tmp
;
1623 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1632 memmove (dst
, cvt
+ offset
, dstsize
);
1634 lastch
= cvt
[offset
+ dstsize
];
1638 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1640 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1644 if (i
+ 1 < dstsize
)
1658 for (; i
< dstsize
; i
++)
1664 /* --------------------------------------------------------------------------------------------- */
1665 /* diff printers et al ****************************************************** */
1668 cc_free_elt (void *elt
)
1676 /* --------------------------------------------------------------------------------------------- */
1679 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1681 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1682 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1692 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1694 if (sz
!= 0 && str
[sz
- 1] == '\n')
1697 p
.p
= g_strndup (str
, sz
);
1700 g_array_append_val (a
, p
);
1702 else if (dsrc
== DATA_SRC_MEM
)
1706 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1707 if (sz
!= 0 && str
[sz
- 1] == '\n')
1714 new_size
= p
->u
.len
+ sz
;
1715 q
= g_realloc (p
->p
, new_size
);
1716 memcpy (q
+ p
->u
.len
, str
, sz
);
1721 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1723 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1724 f_write (f
, str
, sz
);
1729 /* --------------------------------------------------------------------------------------------- */
1732 redo_diff (WDiff
* dview
)
1734 FBUF
*const *f
= dview
->f
;
1742 if (dview
->opt
.quality
== 2)
1743 strcat (extra
, " -d");
1744 if (dview
->opt
.quality
== 1)
1745 strcat (extra
, " --speed-large-files");
1746 if (dview
->opt
.strip_trailing_cr
)
1747 strcat (extra
, " --strip-trailing-cr");
1748 if (dview
->opt
.ignore_tab_expansion
)
1749 strcat (extra
, " -E");
1750 if (dview
->opt
.ignore_space_change
)
1751 strcat (extra
, " -b");
1752 if (dview
->opt
.ignore_all_space
)
1753 strcat (extra
, " -w");
1754 if (dview
->opt
.ignore_case
)
1755 strcat (extra
, " -i");
1757 if (dview
->dsrc
!= DATA_SRC_MEM
)
1759 f_reset (f
[DIFF_LEFT
]);
1760 f_reset (f
[DIFF_RIGHT
]);
1763 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1764 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1768 g_array_free (ops
, TRUE
);
1772 ctx
.dsrc
= dview
->dsrc
;
1775 ctx
.a
= dview
->a
[DIFF_LEFT
];
1776 ctx
.f
= f
[DIFF_LEFT
];
1777 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1779 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1780 ctx
.f
= f
[DIFF_RIGHT
];
1781 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1784 g_array_free (ops
, TRUE
);
1786 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1789 if (dview
->dsrc
== DATA_SRC_TMP
)
1791 f_trunc (f
[DIFF_LEFT
]);
1792 f_trunc (f
[DIFF_RIGHT
]);
1795 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1797 dview
->hdiff
= g_ptr_array_new ();
1798 if (dview
->hdiff
!= NULL
)
1802 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1808 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1809 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1810 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1812 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1818 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1822 g_array_free (h
, TRUE
);
1827 g_ptr_array_add (dview
->hdiff
, h
);
1834 /* --------------------------------------------------------------------------------------------- */
1837 destroy_hdiff (WDiff
* dview
)
1839 if (dview
->hdiff
!= NULL
)
1844 len
= dview
->a
[DIFF_LEFT
]->len
;
1846 for (i
= 0; i
< len
; i
++)
1850 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1852 g_array_free (h
, TRUE
);
1854 g_ptr_array_free (dview
->hdiff
, TRUE
);
1855 dview
->hdiff
= NULL
;
1858 mc_search_free (dview
->search
.handle
);
1859 dview
->search
.handle
= NULL
;
1860 MC_PTR_FREE (dview
->search
.last_string
);
1863 /* --------------------------------------------------------------------------------------------- */
1864 /* stuff ******************************************************************** */
1867 get_digits (unsigned int n
)
1876 /* --------------------------------------------------------------------------------------------- */
1879 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1891 p
= &g_array_index (a
, DIFFLN
, pos
);
1897 for (n
= pos
; n
> 0; n
--)
1903 *lineofs
= pos
- n
+ 1;
1911 /* --------------------------------------------------------------------------------------------- */
1914 calc_nwidth (const GArray
* const *a
)
1919 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1920 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1923 return get_digits (l1
);
1926 /* --------------------------------------------------------------------------------------------- */
1929 find_prev_hunk (const GArray
* a
, int pos
)
1932 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1934 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1936 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1938 if (pos
> 0 && (size_t) pos
< a
->len
)
1941 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1943 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1950 /* --------------------------------------------------------------------------------------------- */
1953 find_next_hunk (const GArray
* a
, size_t pos
)
1955 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1957 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1962 /* --------------------------------------------------------------------------------------------- */
1964 * Find start and end lines of the current hunk.
1966 * @param dview WDiff widget
1967 * @return boolean and
1968 * start_line1 first line of current hunk (file[0])
1969 * end_line1 last line of current hunk (file[0])
1970 * start_line1 first line of current hunk (file[0])
1971 * end_line1 last line of current hunk (file[0])
1975 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1977 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1978 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1988 pos
= dview
->skip_rows
;
1989 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2006 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2010 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2011 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2013 pos
= dview
->skip_rows
;
2014 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2018 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2019 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2021 *end_line1
= MAX (*start_line1
, l0
);
2023 *end_line2
= MAX (*start_line2
, l1
);
2030 /* --------------------------------------------------------------------------------------------- */
2032 * Remove hunk from file.
2034 * @param dview WDiff widget
2035 * @param merge_file file stream for writing data
2036 * @param from1 first line of hunk
2037 * @param to1 last line of hunk
2038 * @param merge_direction in what direction files should be merged
2042 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2043 action_direction_t merge_direction
)
2049 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2050 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2052 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2055 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2058 fputs (buf
, merge_file
);
2060 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2064 fputs (buf
, merge_file
);
2069 /* --------------------------------------------------------------------------------------------- */
2073 * @param dview WDiff widget
2074 * @param merge_file file stream for writing data
2075 * @param from1 first line of source hunk
2076 * @param from2 first line of destination hunk
2077 * @param to1 last line of source hunk
2078 * @param merge_direction in what direction files should be merged
2082 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2083 action_direction_t merge_direction
)
2090 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2092 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2093 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2097 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2098 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2102 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2105 fputs (buf
, merge_file
);
2108 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2112 fputs (buf
, merge_file
);
2114 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2115 fputs (buf
, merge_file
);
2121 /* --------------------------------------------------------------------------------------------- */
2123 * Replace hunk in file.
2125 * @param dview WDiff widget
2126 * @param merge_file file stream for writing data
2127 * @param from1 first line of source hunk
2128 * @param to1 last line of source hunk
2129 * @param from2 first line of destination hunk
2130 * @param to2 last line of destination hunk
2131 * @param merge_direction in what direction files should be merged
2135 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2136 action_direction_t merge_direction
)
2138 int line1
= 0, line2
= 0;
2143 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2145 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2146 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2150 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2151 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2154 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2157 fputs (buf
, merge_file
);
2159 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2163 fputs (buf
, merge_file
);
2165 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2169 fputs (buf
, merge_file
);
2175 /* --------------------------------------------------------------------------------------------- */
2179 * @param dview WDiff widget
2180 * @param merge_direction in what direction files should be merged
2184 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2186 int from1
, to1
, from2
, to2
;
2188 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2190 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2191 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2193 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2199 vfs_path_t
*merge_file_name_vpath
= NULL
;
2201 if (!dview
->merged
[n_merge
])
2203 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2204 if (!dview
->merged
[n_merge
])
2206 message (D_ERROR
, MSG_ERROR
,
2207 _("Cannot create backup file\n%s%s\n%s"),
2208 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2213 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "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 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2227 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2229 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2232 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2233 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2235 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2238 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2243 fflush (merge_file
);
2244 fclose (merge_file
);
2248 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2251 mc_unlink (merge_file_name_vpath
);
2252 vfs_path_free (merge_file_name_vpath
);
2256 /* --------------------------------------------------------------------------------------------- */
2257 /* view routines and callbacks ********************************************** */
2260 dview_compute_split (WDiff
* dview
, int i
)
2263 if (dview
->bias
< 2 - dview
->half1
)
2264 dview
->bias
= 2 - dview
->half1
;
2265 if (dview
->bias
> dview
->half2
- 2)
2266 dview
->bias
= dview
->half2
- 2;
2269 /* --------------------------------------------------------------------------------------------- */
2272 dview_compute_areas (WDiff
* dview
)
2274 dview
->height
= LINES
- 2;
2275 dview
->half1
= COLS
/ 2;
2276 dview
->half2
= COLS
- dview
->half1
;
2278 dview_compute_split (dview
, 0);
2281 /* --------------------------------------------------------------------------------------------- */
2284 dview_reread (WDiff
* dview
)
2288 destroy_hdiff (dview
);
2289 if (dview
->a
[DIFF_LEFT
] != NULL
)
2291 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2292 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2294 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2296 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2297 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2300 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2301 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2303 ndiff
= redo_diff (dview
);
2305 dview
->ndiff
= ndiff
;
2308 /* --------------------------------------------------------------------------------------------- */
2312 dview_set_codeset (WDiff
* dview
)
2314 const char *encoding_id
= NULL
;
2318 get_codepage_id (mc_global
.source_codepage
>=
2319 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2320 if (encoding_id
!= NULL
)
2324 conv
= str_crt_conv_from (encoding_id
);
2325 if (conv
!= INVALID_CONV
)
2327 if (dview
->converter
!= str_cnv_from_term
)
2328 str_close_conv (dview
->converter
);
2329 dview
->converter
= conv
;
2331 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
2335 /* --------------------------------------------------------------------------------------------- */
2338 dview_select_encoding (WDiff
* dview
)
2340 if (do_select_codepage ())
2341 dview_set_codeset (dview
);
2342 dview_reread (dview
);
2343 tty_touch_screen ();
2346 #endif /* HAVE_CHARSET */
2348 /* --------------------------------------------------------------------------------------------- */
2351 dview_diff_options (WDiff
* dview
)
2353 const char *quality_str
[] = {
2355 N_("&Fastest (Assume large files)"),
2356 N_("&Minimal (Find a smaller set of change)")
2359 quick_widget_t quick_widgets
[] = {
2361 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2362 QUICK_RADIO (3, (const char **) quality_str
, (int *) &dview
->opt
.quality
, NULL
),
2363 QUICK_STOP_GROUPBOX
,
2364 QUICK_START_GROUPBOX (N_("Diff extra options")),
2365 QUICK_CHECKBOX (N_("&Ignore case"), &dview
->opt
.ignore_case
, NULL
),
2366 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
, NULL
),
2367 QUICK_CHECKBOX (N_("Ignore &space change"), &dview
->opt
.ignore_space_change
, NULL
),
2368 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
, NULL
),
2369 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
,
2371 QUICK_STOP_GROUPBOX
,
2372 QUICK_BUTTONS_OK_CANCEL
,
2377 quick_dialog_t qdlg
= {
2379 N_("Diff Options"), "[Diff Options]",
2380 quick_widgets
, NULL
, NULL
2383 if (quick_dialog (&qdlg
) != B_CANCEL
)
2384 dview_reread (dview
);
2387 /* --------------------------------------------------------------------------------------------- */
2390 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2391 const char *label1
, const char *label2
, DSRC dsrc
)
2394 FBUF
*f
[DIFF_COUNT
];
2396 f
[DIFF_LEFT
] = NULL
;
2397 f
[DIFF_RIGHT
] = NULL
;
2399 if (dsrc
== DATA_SRC_TMP
)
2401 f
[DIFF_LEFT
] = f_temp ();
2402 if (f
[DIFF_LEFT
] == NULL
)
2405 f
[DIFF_RIGHT
] = f_temp ();
2406 if (f
[DIFF_RIGHT
] == NULL
)
2408 f_close (f
[DIFF_LEFT
]);
2412 else if (dsrc
== DATA_SRC_ORG
)
2414 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2415 if (f
[DIFF_LEFT
] == NULL
)
2418 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2419 if (f
[DIFF_RIGHT
] == NULL
)
2421 f_close (f
[DIFF_LEFT
]);
2427 dview
->file
[DIFF_LEFT
] = file1
;
2428 dview
->file
[DIFF_RIGHT
] = file2
;
2429 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2430 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2431 dview
->f
[DIFF_LEFT
] = f
[0];
2432 dview
->f
[DIFF_RIGHT
] = f
[1];
2433 dview
->merged
[DIFF_LEFT
] = FALSE
;
2434 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2435 dview
->hdiff
= NULL
;
2438 dview
->converter
= str_cnv_from_term
;
2439 dview_set_codeset (dview
);
2441 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2442 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2444 ndiff
= redo_diff (dview
);
2447 /* goto MSG_DESTROY stage: dview_fini() */
2448 f_close (f
[DIFF_LEFT
]);
2449 f_close (f
[DIFF_RIGHT
]);
2453 dview
->ndiff
= ndiff
;
2455 dview
->view_quit
= FALSE
;
2458 dview
->new_frame
= TRUE
;
2459 dview
->skip_rows
= 0;
2460 dview
->skip_cols
= 0;
2461 dview
->display_symbols
= 0;
2462 dview
->display_numbers
= 0;
2463 dview
->show_cr
= TRUE
;
2464 dview
->tab_size
= 8;
2465 dview
->ord
= DIFF_LEFT
;
2466 dview
->full
= FALSE
;
2468 dview
->search
.handle
= NULL
;
2469 dview
->search
.last_string
= NULL
;
2470 dview
->search
.last_found_line
= -1;
2471 dview
->search
.last_accessed_num_line
= -1;
2473 dview
->opt
.quality
= 0;
2474 dview
->opt
.strip_trailing_cr
= 0;
2475 dview
->opt
.ignore_tab_expansion
= 0;
2476 dview
->opt
.ignore_space_change
= 0;
2477 dview
->opt
.ignore_all_space
= 0;
2478 dview
->opt
.ignore_case
= 0;
2480 dview_compute_areas (dview
);
2485 /* --------------------------------------------------------------------------------------------- */
2488 dview_fini (WDiff
* dview
)
2490 if (dview
->dsrc
!= DATA_SRC_MEM
)
2492 f_close (dview
->f
[DIFF_RIGHT
]);
2493 f_close (dview
->f
[DIFF_LEFT
]);
2497 if (dview
->converter
!= str_cnv_from_term
)
2498 str_close_conv (dview
->converter
);
2501 destroy_hdiff (dview
);
2502 if (dview
->a
[DIFF_LEFT
] != NULL
)
2504 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2505 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2506 dview
->a
[DIFF_LEFT
] = NULL
;
2508 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2510 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2511 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2512 dview
->a
[DIFF_RIGHT
] = NULL
;
2515 g_free (dview
->label
[DIFF_LEFT
]);
2516 g_free (dview
->label
[DIFF_RIGHT
]);
2519 /* --------------------------------------------------------------------------------------------- */
2522 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2527 FBUF
*f
= dview
->f
[ord
];
2528 int skip
= dview
->skip_cols
;
2529 int display_symbols
= dview
->display_symbols
;
2530 int display_numbers
= dview
->display_numbers
;
2531 gboolean show_cr
= dview
->show_cr
;
2534 int nwidth
= display_numbers
;
2537 xwidth
= display_symbols
+ display_numbers
;
2538 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2539 tab_size
= dview
->tab_size
;
2543 if (xwidth
> width
&& display_symbols
)
2546 display_symbols
= 0;
2548 if (xwidth
> width
&& display_numbers
)
2551 display_numbers
= width
;
2561 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2563 /* abnormal, but avoid buffer overflow */
2567 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2569 int ch
, next_ch
= 0, col
;
2572 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2574 tty_setcolor (NORMAL_COLOR
);
2575 if (display_symbols
)
2577 tty_gotoyx (r
+ j
, c
- 2);
2578 tty_print_char (ch
);
2582 if (display_numbers
)
2584 tty_gotoyx (r
+ j
, c
- xwidth
);
2585 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2586 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2589 tty_setcolor (DFF_ADD_COLOR
);
2591 tty_setcolor (DFF_CHG_COLOR
);
2594 if (i
== (size_t) dview
->search
.last_found_line
)
2595 tty_setcolor (MARKED_SELECTED_COLOR
);
2596 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2602 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2607 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2608 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2609 tty_gotoyx (r
+ j
, c
);
2612 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2621 ch_res
= dview_get_utf (buf
+ cnt
, &next_ch
, &ch_length
);
2623 cnt
+= ch_length
- 1;
2624 if (!g_unichar_isprint (next_ch
))
2629 ch_res
= dview_get_byte (buf
+ cnt
, &next_ch
);
2633 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2635 if (mc_global
.utf8_display
)
2640 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2644 else if (dview
->utf8
)
2645 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2647 next_ch
= convert_to_display_c (next_ch
);
2649 tty_print_anychar (next_ch
);
2657 tty_setcolor (DFF_CHH_COLOR
);
2661 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2665 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2668 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2672 if (display_numbers
)
2674 tty_gotoyx (r
+ j
, c
- xwidth
);
2675 memset (buf
, ' ', display_numbers
);
2676 buf
[display_numbers
] = '\0';
2677 tty_print_string (buf
);
2680 tty_setcolor (DFF_DEL_COLOR
);
2682 tty_setcolor (DFF_CHD_COLOR
);
2683 memset (buf
, ' ', width
);
2686 tty_gotoyx (r
+ j
, c
);
2687 /* tty_print_nstring (buf, width); */
2689 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2698 ch_res
= dview_get_utf (buf
+ cnt
, &next_ch
, &ch_length
);
2700 cnt
+= ch_length
- 1;
2701 if (!g_unichar_isprint (next_ch
))
2706 ch_res
= dview_get_byte (buf
+ cnt
, &next_ch
);
2711 if (mc_global
.utf8_display
)
2716 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2719 else if (dview
->utf8
)
2720 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2722 next_ch
= convert_to_display_c (next_ch
);
2725 tty_print_anychar (next_ch
);
2730 tty_setcolor (NORMAL_COLOR
);
2732 if (width
< xwidth
- 1)
2734 memset (buf
, ' ', k
);
2736 for (; j
< height
; j
++)
2740 tty_gotoyx (r
+ j
, c
- xwidth
);
2741 /* tty_print_nstring (buf, xwidth - 1); */
2742 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2744 tty_gotoyx (r
+ j
, c
);
2745 /* tty_print_nstring (buf, width); */
2746 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2752 /* --------------------------------------------------------------------------------------------- */
2755 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2759 int linenum
, lineofs
;
2763 tty_setcolor (STATUSBAR_COLOR
);
2766 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2768 filename_width
= width
- 24;
2769 if (filename_width
< 8)
2772 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2773 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2774 vfs_path_free (vpath
);
2775 buf
= str_term_trim (path
, filename_width
);
2776 if (ord
== DIFF_LEFT
)
2777 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2778 buf
, linenum
, lineofs
, dview
->skip_cols
);
2780 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2781 buf
, linenum
, lineofs
, dview
->ndiff
);
2785 /* --------------------------------------------------------------------------------------------- */
2788 dview_redo (WDiff
* dview
)
2790 if (dview
->display_numbers
)
2794 old
= dview
->display_numbers
;
2795 dview
->display_numbers
= calc_nwidth ((const GArray
* const *) dview
->a
);
2796 dview
->new_frame
= (old
!= dview
->display_numbers
);
2798 dview_reread (dview
);
2801 /* --------------------------------------------------------------------------------------------- */
2804 dview_update (WDiff
* dview
)
2806 int height
= dview
->height
;
2811 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2813 if (dview
->skip_rows
> last
)
2814 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2815 if (dview
->skip_rows
< 0)
2816 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2817 if (dview
->skip_cols
< 0)
2818 dview
->skip_cols
= 0;
2823 width1
= dview
->half1
+ dview
->bias
;
2824 width2
= dview
->half2
- dview
->bias
;
2831 if (dview
->new_frame
)
2835 tty_setcolor (NORMAL_COLOR
);
2836 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2838 tty_draw_box (1, 0, height
, width1
, FALSE
);
2840 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2845 if (xwidth
< width1
- 1)
2847 tty_gotoyx (1, xwidth
);
2848 tty_print_alt_char (ACS_TTEE
, FALSE
);
2849 tty_gotoyx (height
, xwidth
);
2850 tty_print_alt_char (ACS_BTEE
, FALSE
);
2851 tty_draw_vline (2, xwidth
, ACS_VLINE
, height
- 2);
2853 if (xwidth
< width2
- 1)
2855 tty_gotoyx (1, width1
+ xwidth
);
2856 tty_print_alt_char (ACS_TTEE
, FALSE
);
2857 tty_gotoyx (height
, width1
+ xwidth
);
2858 tty_print_alt_char (ACS_BTEE
, FALSE
);
2859 tty_draw_vline (2, width1
+ xwidth
, ACS_VLINE
, height
- 2);
2862 dview
->new_frame
= FALSE
;
2867 dview_status (dview
, dview
->ord
, width1
, 0);
2868 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2872 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2873 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2877 /* --------------------------------------------------------------------------------------------- */
2880 dview_edit (WDiff
* dview
, diff_place_t ord
)
2884 int linenum
, lineofs
;
2886 if (dview
->dsrc
== DATA_SRC_TMP
)
2888 error_dialog (_("Edit"), _("Edit is disabled"));
2892 h
= WIDGET (dview
)->owner
;
2893 h_modal
= widget_get_state (WIDGET (h
), WST_MODAL
);
2895 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2897 /* disallow edit file in several editors */
2898 widget_set_state (WIDGET (h
), WST_MODAL
, TRUE
);
2901 vfs_path_t
*tmp_vpath
;
2903 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2904 edit_file_at_line (tmp_vpath
, use_internal_edit
!= 0, linenum
);
2905 vfs_path_free (tmp_vpath
);
2908 widget_set_state (WIDGET (h
), WST_MODAL
, h_modal
);
2910 dview_update (dview
);
2913 /* --------------------------------------------------------------------------------------------- */
2916 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2918 static gboolean first_run
= TRUE
;
2921 static const char *title
[2] = {
2922 N_("Goto line (left)"),
2923 N_("Goto line (right)")
2931 input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
,
2932 first_run
? NULL
: INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
2935 const char *s
= input
;
2937 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2943 for (; i
< dview
->a
[ord
]->len
; i
++)
2947 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2948 if (p
->line
== newline
)
2952 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2960 /* --------------------------------------------------------------------------------------------- */
2963 dview_labels (WDiff
* dview
)
2971 b
= find_buttonbar (h
);
2973 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, d
);
2974 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, d
);
2975 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, d
);
2976 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, d
);
2977 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, d
);
2978 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, d
);
2979 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, d
);
2982 /* --------------------------------------------------------------------------------------------- */
2985 dview_save (WDiff
* dview
)
2987 gboolean res
= TRUE
;
2989 if (dview
->merged
[DIFF_LEFT
])
2991 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
2992 dview
->merged
[DIFF_LEFT
] = !res
;
2994 if (dview
->merged
[DIFF_RIGHT
])
2996 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
2997 dview
->merged
[DIFF_RIGHT
] = !res
;
3002 /* --------------------------------------------------------------------------------------------- */
3005 dview_do_save (WDiff
* dview
)
3007 (void) dview_save (dview
);
3010 /* --------------------------------------------------------------------------------------------- */
3013 dview_save_options (WDiff
* dview
)
3015 mc_config_set_bool (mc_global
.main_config
, "DiffView", "show_symbols",
3016 dview
->display_symbols
!= 0);
3017 mc_config_set_bool (mc_global
.main_config
, "DiffView", "show_numbers",
3018 dview
->display_numbers
!= 0);
3019 mc_config_set_int (mc_global
.main_config
, "DiffView", "tab_size", dview
->tab_size
);
3021 mc_config_set_int (mc_global
.main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3023 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_tws",
3024 dview
->opt
.strip_trailing_cr
);
3025 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_all_space",
3026 dview
->opt
.ignore_all_space
);
3027 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_space_change",
3028 dview
->opt
.ignore_space_change
);
3029 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_tab_expansion",
3030 dview
->opt
.ignore_tab_expansion
);
3031 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_case",
3032 dview
->opt
.ignore_case
);
3035 /* --------------------------------------------------------------------------------------------- */
3038 dview_load_options (WDiff
* dview
)
3040 gboolean show_numbers
, show_symbols
;
3043 show_symbols
= mc_config_get_bool (mc_global
.main_config
, "DiffView", "show_symbols", FALSE
);
3045 dview
->display_symbols
= 1;
3046 show_numbers
= mc_config_get_bool (mc_global
.main_config
, "DiffView", "show_numbers", FALSE
);
3048 dview
->display_numbers
= calc_nwidth ((const GArray
* const *) dview
->a
);
3049 tab_size
= mc_config_get_int (mc_global
.main_config
, "DiffView", "tab_size", 8);
3050 if (tab_size
> 0 && tab_size
< 9)
3051 dview
->tab_size
= tab_size
;
3053 dview
->tab_size
= 8;
3055 dview
->opt
.quality
= mc_config_get_int (mc_global
.main_config
, "DiffView", "diff_quality", 0);
3057 dview
->opt
.strip_trailing_cr
=
3058 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3059 dview
->opt
.ignore_all_space
=
3060 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3061 dview
->opt
.ignore_space_change
=
3062 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3063 dview
->opt
.ignore_tab_expansion
=
3064 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3065 dview
->opt
.ignore_case
=
3066 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_case", FALSE
);
3068 dview
->new_frame
= TRUE
;
3071 /* --------------------------------------------------------------------------------------------- */
3074 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3078 dview_ok_to_exit (WDiff
* dview
)
3080 gboolean res
= TRUE
;
3083 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3086 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3087 _("File(s) was modified. Save with exit?") :
3088 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3089 D_NORMAL
, 2, _("&Yes"), _("&No"));
3092 if (mc_global
.midnight_shutdown
|| (act
== -1))
3101 (void) dview_save (dview
);
3105 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3106 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3107 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3108 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3117 /* --------------------------------------------------------------------------------------------- */
3120 dview_execute_cmd (WDiff
* dview
, long command
)
3122 cb_ret_t res
= MSG_HANDLED
;
3126 case CK_ShowSymbols
:
3127 dview
->display_symbols
^= 1;
3128 dview
->new_frame
= TRUE
;
3130 case CK_ShowNumbers
:
3131 dview
->display_numbers
^= calc_nwidth ((const GArray
* const *) dview
->a
);
3132 dview
->new_frame
= TRUE
;
3135 dview
->full
= !dview
->full
;
3136 dview
->new_frame
= TRUE
;
3142 dview
->new_frame
= TRUE
;
3148 dview_compute_split (dview
, 1);
3149 dview
->new_frame
= TRUE
;
3156 dview_compute_split (dview
, -1);
3157 dview
->new_frame
= TRUE
;
3161 dview
->tab_size
= 2;
3164 dview
->tab_size
= 3;
3167 dview
->tab_size
= 4;
3170 dview
->tab_size
= 8;
3179 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3180 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3183 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3184 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3187 dview_goto_cmd (dview
, DIFF_RIGHT
);
3190 dview_edit (dview
, dview
->ord
);
3193 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3197 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3201 dview_edit (dview
, dview
->ord
^ 1);
3204 dview_search_cmd (dview
);
3206 case CK_SearchContinue
:
3207 dview_continue_search_cmd (dview
);
3210 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3213 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3216 if (dview
->skip_rows
> 0)
3219 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3224 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3227 if (dview
->height
> 2)
3229 dview
->skip_rows
+= dview
->height
- 2;
3230 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3234 if (dview
->height
> 2)
3236 dview
->skip_rows
-= dview
->height
- 2;
3237 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3247 dview
->skip_cols
-= 8;
3250 dview
->skip_cols
+= 8;
3253 dview
->skip_cols
= 0;
3259 dview
->view_quit
= TRUE
;
3262 dview_do_save (dview
);
3265 dview_diff_options (dview
);
3268 case CK_SelectCodepage
:
3269 dview_select_encoding (dview
);
3273 /* don't close diffviewer due to SIGINT */
3276 res
= MSG_NOT_HANDLED
;
3281 /* --------------------------------------------------------------------------------------------- */
3284 dview_handle_key (WDiff
* dview
, int key
)
3289 key
= convert_from_input_c (key
);
3292 command
= keybind_lookup_keymap_command (diff_map
, key
);
3293 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3297 return MSG_NOT_HANDLED
;
3300 /* --------------------------------------------------------------------------------------------- */
3303 dview_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3305 WDiff
*dview
= (WDiff
*) w
;
3306 WDialog
*h
= w
->owner
;
3312 dview_labels (dview
);
3313 dview_load_options (dview
);
3314 dview_update (dview
);
3318 dview
->new_frame
= TRUE
;
3319 dview_update (dview
);
3323 i
= dview_handle_key (dview
, parm
);
3324 if (dview
->view_quit
)
3327 dview_update (dview
);
3331 i
= dview_execute_cmd (dview
, parm
);
3332 if (dview
->view_quit
)
3335 dview_update (dview
);
3339 dview_save_options (dview
);
3344 return widget_default_callback (w
, sender
, msg
, parm
, data
);
3348 /* --------------------------------------------------------------------------------------------- */
3351 dview_mouse_callback (Widget
* w
, mouse_msg_t msg
, mouse_event_t
* event
)
3353 WDiff
*dview
= (WDiff
*) w
;
3359 case MSG_MOUSE_SCROLL_UP
:
3360 case MSG_MOUSE_SCROLL_DOWN
:
3361 if (msg
== MSG_MOUSE_SCROLL_UP
)
3362 dview
->skip_rows
-= 2;
3364 dview
->skip_rows
+= 2;
3366 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3367 dview_update (dview
);
3375 /* --------------------------------------------------------------------------------------------- */
3378 dview_adjust_size (WDialog
* h
)
3383 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3384 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3385 bar
= find_buttonbar (h
);
3386 widget_set_size (WIDGET (dview
), 0, 0, LINES
- 1, COLS
);
3387 widget_set_size (WIDGET (bar
), LINES
- 1, 0, 1, COLS
);
3389 dview_compute_areas (dview
);
3392 /* --------------------------------------------------------------------------------------------- */
3395 dview_dialog_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3398 WDialog
*h
= DIALOG (w
);
3403 dview_adjust_size (h
);
3407 /* Handle shortcuts. */
3409 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3410 * here, which is why we can pass NULL in the following call. */
3411 return dview_execute_cmd (NULL
, parm
);
3414 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3415 /* don't stop the dialog before final decision */
3416 widget_set_state (w
, WST_ACTIVE
, TRUE
);
3417 if (dview_ok_to_exit (dview
))
3422 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
3426 /* --------------------------------------------------------------------------------------------- */
3429 dview_get_title (const WDialog
* h
, size_t len
)
3432 const char *modified
= " (*) ";
3433 const char *notmodified
= " ";
3437 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3438 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3440 title
= g_string_sized_new (len
);
3441 g_string_append (title
, _("Diff:"));
3442 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3443 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3444 g_string_append (title
, " | ");
3445 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3446 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3448 return g_string_free (title
, FALSE
);
3451 /* --------------------------------------------------------------------------------------------- */
3454 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3461 /* Create dialog and widgets, put them on the dialog */
3463 dlg_create (FALSE
, 0, 0, 1, 1, WPOS_FULLSCREEN
, FALSE
, NULL
, dview_dialog_callback
, NULL
,
3464 "[Diff Viewer]", NULL
);
3465 widget_want_tab (WIDGET (dview_dlg
), TRUE
);
3467 dview
= g_new0 (WDiff
, 1);
3469 widget_init (w
, 0, 0, LINES
- 1, COLS
, dview_callback
, dview_mouse_callback
);
3471 add_widget (dview_dlg
, dview
);
3472 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3474 dview_dlg
->get_title
= dview_get_title
;
3476 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3478 /* Please note that if you add another widget,
3479 * you have to modify dview_adjust_size to
3483 dlg_run (dview_dlg
);
3485 if (error
!= 0 || widget_get_state (WIDGET (dview_dlg
), WST_CLOSED
))
3486 dlg_destroy (dview_dlg
);
3488 return error
== 0 ? 1 : 0;
3491 /*** public functions ****************************************************************************/
3492 /* --------------------------------------------------------------------------------------------- */
3494 #define GET_FILE_AND_STAMP(n) \
3498 real_file##n = file##n; \
3499 if (!vfs_file_is_local (file##n)) \
3501 real_file##n = mc_getlocalcopy (file##n); \
3502 if (real_file##n != NULL) \
3505 if (mc_stat (real_file##n, &st##n) != 0) \
3512 #define UNGET_FILE(n) \
3518 if (use_copy##n > 0) \
3521 mtime = st##n.st_mtime; \
3522 if (mc_stat (real_file##n, &st##n) == 0) \
3523 changed = (mtime != st##n.st_mtime); \
3525 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3526 vfs_path_free (real_file##n); \
3532 dview_diff_cmd (const void *f0
, const void *f1
)
3535 vfs_path_t
*file0
= NULL
;
3536 vfs_path_t
*file1
= NULL
;
3537 gboolean is_dir0
= FALSE
;
3538 gboolean is_dir1
= FALSE
;
3540 switch (mc_global
.mc_run_mode
)
3544 /* run from panels */
3545 const WPanel
*panel0
= (const WPanel
*) f0
;
3546 const WPanel
*panel1
= (const WPanel
*) f1
;
3549 vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, (char *) NULL
);
3550 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3553 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3554 path_trunc (selection (panel0
)->fname
, 30));
3559 vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, (char *) NULL
);
3560 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3563 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3564 path_trunc (selection (panel1
)->fname
, 30));
3570 case MC_RUN_DIFFVIEWER
:
3572 /* run from command line */
3573 const char *p0
= (const char *) f0
;
3574 const char *p1
= (const char *) f1
;
3577 file0
= vfs_path_from_str (p0
);
3578 if (mc_stat (file0
, &st
) == 0)
3580 is_dir0
= S_ISDIR (st
.st_mode
);
3583 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3589 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3590 path_trunc (p0
, 30), unix_error_string (errno
));
3594 file1
= vfs_path_from_str (p1
);
3595 if (mc_stat (file1
, &st
) == 0)
3597 is_dir1
= S_ISDIR (st
.st_mode
);
3600 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3606 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3607 path_trunc (p1
, 30), unix_error_string (errno
));
3614 /* this should not happaned */
3615 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3622 if (file0
!= NULL
&& file1
!= NULL
)
3628 vfs_path_t
*real_file0
;
3629 vfs_path_t
*real_file1
;
3631 GET_FILE_AND_STAMP (0);
3632 GET_FILE_AND_STAMP (1);
3634 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3635 rv
= diff_view (vfs_path_as_str (real_file0
), vfs_path_as_str (real_file1
),
3636 vfs_path_as_str (file0
), vfs_path_as_str (file1
));
3644 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3647 vfs_path_free (file1
);
3648 vfs_path_free (file0
);
3653 #undef GET_FILE_AND_STAMP
3656 /* --------------------------------------------------------------------------------------------- */