2 Copyright (C) 2007, 2010, 2011
3 The Free Software Foundation, Inc.
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010
8 Andrew Borodin <aborodin@vmail.ru>, 2010
9 Ilia Maslakov <il.smind@gmail.com>, 2010
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include <sys/types.h>
37 #include "lib/global.h"
38 #include "lib/tty/tty.h"
39 #include "lib/tty/color.h"
40 #include "lib/tty/key.h"
41 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
42 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
44 #include "lib/widget.h"
45 #include "lib/strutil.h"
46 #include "lib/strescape.h" /* strutils_glob_escape() */
48 #include "lib/charsets.h"
50 #include "lib/event.h" /* mc_event_raise() */
52 #include "src/filemanager/cmd.h" /* do_edit_at_line(), view_other_cmd() */
53 #include "src/filemanager/panel.h"
54 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
56 #include "src/keybind-defaults.h"
57 #include "src/setup.h"
58 #include "src/history.h"
60 #include "src/selcodepage.h"
66 /*** global variables ****************************************************************************/
68 /*** file scope macro definitions ****************************************************************/
70 #define g_array_foreach(a, TP, cbf) \
72 size_t g_array_foreach_i;\
73 TP *g_array_foreach_var = NULL; \
74 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
76 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
77 (*cbf) (g_array_foreach_var); \
81 #define FILE_READ_BUF 4096
82 #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
= vfs_path_to_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
559 * @param char * str, gboolean * result
560 * @return int as character or 0 and result == FALSE if fail
564 dview_get_byte (char *str
, gboolean
* result
)
572 return (unsigned char) *str
;
575 /* --------------------------------------------------------------------------------------------- */
578 * Get utf multibyte char from string
580 * @param char * str, int * char_width, gboolean * result
581 * @return int as utf character or 0 and result == FALSE if fail
585 dview_get_utf (char *str
, int *char_width
, gboolean
* result
)
589 gchar
*next_ch
= NULL
;
600 res
= g_utf8_get_char_validated (str
, -1);
607 /* Calculate UTF-8 char width */
608 next_ch
= g_utf8_next_char (str
);
610 width
= next_ch
- str
;
618 /* --------------------------------------------------------------------------------------------- */
621 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
625 if (text
== NULL
|| text
[0] == '\0')
628 if (g_utf8_validate (text
, -1, NULL
))
629 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
633 char *tmpbuf
, *buffer
;
635 buffer
= tmpbuf
= g_strdup (text
);
636 while (tmpbuf
[0] != '\0')
638 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
639 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
640 tmpbuf
= g_utf8_next_char (tmpbuf
);
647 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
650 return max (length
, (size_t) result
);
653 /* --------------------------------------------------------------------------------------------- */
655 /* diff parse *************************************************************** */
658 * Read decimal number from string.
660 * @param[in,out] str string to parse
661 * @param[out] n extracted number
662 * @return 0 if success, otherwise non-zero
666 scan_deci (const char **str
, int *n
)
668 const char *p
= *str
;
672 *n
= strtol (p
, &q
, 10);
673 if (errno
!= 0 || p
== q
)
679 /* --------------------------------------------------------------------------------------------- */
682 * Parse line for diff statement.
684 * @param p string to parse
685 * @param ops list of diff statements
686 * @return 0 if success, otherwise non-zero
690 scan_line (const char *p
, GArray
* ops
)
699 /* handle the following cases:
701 * NUM[,NUM]cNUM[,NUM]
703 * where NUM is a positive integer
706 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
714 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
726 else if (cmd
!= 'c' && cmd
!= 'd')
729 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
737 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
743 if (cmd
== 'd' && range
!= 0)
751 g_array_append_val (ops
, op
);
755 /* --------------------------------------------------------------------------------------------- */
758 * Parse diff output and extract diff statements.
760 * @param f stream to read from
761 * @param ops list of diff statements to fill
762 * @return positive number indicating number of hunks, otherwise negative
766 scan_diff (FBUF
* f
, GArray
* ops
)
771 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
773 if (isdigit (buf
[0]))
775 if (buf
[sz
- 1] != '\n')
779 if (scan_line (buf
, ops
) != 0)
785 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
792 /* --------------------------------------------------------------------------------------------- */
795 * Invoke diff and extract diff statements.
797 * @param args extra arguments to be passed to diff
798 * @param extra more arguments to be passed to diff
799 * @param file1 first file to compare
800 * @param file2 second file to compare
801 * @param ops list of diff statements to fill
803 * @return positive number indicating number of hunks, otherwise negative
807 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
810 static const char *opt
=
811 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
812 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
813 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
814 " --unchanged-group-format=''";
820 char *file1_esc
, *file2_esc
;
822 /* escape potential $ to avoid shell variable substitutions in popen() */
823 file1_esc
= strutils_shell_escape (file1
);
824 file2_esc
= strutils_shell_escape (file2
);
825 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
832 f
= p_open (cmd
, O_RDONLY
);
838 rv
= scan_diff (f
, ops
);
841 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
847 /* --------------------------------------------------------------------------------------------- */
850 * Reparse and display file according to diff statements.
852 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
853 * @param filename file name to display
854 * @param ops list of diff statements
855 * @param printer printf-like function to be used for displaying
856 * @param ctx printer context
858 * @return 0 if success, otherwise non-zero
862 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
875 f
= f_open (filename
, O_RDONLY
);
891 #define T1 a[ ord^1 ][0]
892 #define T2 a[ ord^1 ][1]
893 for (i
= 0; i
< ops
->len
; i
++)
897 op
= &g_array_index (ops
, DIFFCMD
, i
);
898 n
= op
->F1
- (op
->cmd
!= add_cmd
);
900 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
903 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
905 while (buf
[sz
- 1] != '\n')
907 sz
= f_gets (buf
, sizeof (buf
), f
);
910 printer (ctx
, 0, 0, 0, 1, "\n");
913 printer (ctx
, 0, 0, 0, sz
, buf
);
921 if (op
->cmd
== add_cmd
)
923 n
= op
->T2
- op
->T1
+ 1;
926 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
931 if (op
->cmd
== del_cmd
)
933 n
= op
->F2
- op
->F1
+ 1;
934 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
937 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
939 while (buf
[sz
- 1] != '\n')
941 sz
= f_gets (buf
, sizeof (buf
), f
);
944 printer (ctx
, 0, 0, 0, 1, "\n");
947 printer (ctx
, 0, 0, 0, sz
, buf
);
959 n
= op
->F2
- op
->F1
+ 1;
960 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
963 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
965 while (buf
[sz
- 1] != '\n')
967 sz
= f_gets (buf
, sizeof (buf
), f
);
970 printer (ctx
, 0, 0, 0, 1, "\n");
973 printer (ctx
, 0, 0, 0, sz
, buf
);
982 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
985 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
995 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
998 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1000 while (buf
[sz
- 1] != '\n')
1002 sz
= f_gets (buf
, sizeof (buf
), f
);
1005 printer (ctx
, 0, 0, 0, 1, "\n");
1008 printer (ctx
, 0, 0, 0, sz
, buf
);
1021 /* --------------------------------------------------------------------------------------------- */
1023 /* horizontal diff ********************************************************** */
1026 * Longest common substring.
1028 * @param s first string
1029 * @param m length of first string
1030 * @param t second string
1031 * @param n length of second string
1032 * @param ret list of offsets for longest common substrings inside each string
1033 * @param min minimum length of common substrings
1035 * @return 0 if success, nonzero otherwise
1039 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1045 if (m
< min
|| n
< min
)
1047 /* XXX early culling */
1051 Lprev
= g_try_new0 (int, n
+ 1);
1055 Lcurr
= g_try_new0 (int, n
+ 1);
1062 for (i
= 0; i
< m
; i
++)
1069 #ifdef USE_MEMSET_IN_LCS
1070 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1072 for (j
= 0; j
< n
; j
++)
1074 #ifndef USE_MEMSET_IN_LCS
1086 g_array_set_size (ret
, 0);
1088 if (z
== v
&& z
>= min
)
1096 for (k
= 0; k
< ret
->len
; k
++)
1098 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1099 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1108 g_array_append_val (ret
, p2
);
1120 /* --------------------------------------------------------------------------------------------- */
1123 * Scan recursively for common substrings and build ranges.
1125 * @param s first string
1126 * @param t second string
1127 * @param bracket current limits for both of the strings
1128 * @param min minimum length of common substrings
1129 * @param hdiff list of horizontal diff ranges to fill
1130 * @param depth recursion depth
1132 * @return 0 if success, nonzero otherwise
1136 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1147 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1151 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1152 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1156 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1159 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1160 b
[DIFF_LEFT
].len
= (*data
)[0];
1161 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1162 b
[DIFF_RIGHT
].len
= (*data
)[1];
1163 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1166 for (k
= 0; k
< ret
->len
- 1; k
++)
1168 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1169 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1170 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1171 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1172 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1173 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1174 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1177 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1178 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1179 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1180 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1181 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1182 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1185 g_array_free (ret
, TRUE
);
1190 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1191 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1192 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1193 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1194 g_array_append_val (hdiff
, p
);
1199 /* --------------------------------------------------------------------------------------------- */
1202 * Build list of horizontal diff ranges.
1204 * @param s first string
1205 * @param m length of first string
1206 * @param t second string
1207 * @param n length of second string
1208 * @param min minimum length of common substrings
1209 * @param hdiff list of horizontal diff ranges to fill
1210 * @param depth recursion depth
1212 * @return 0 if success, nonzero otherwise
1216 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1221 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1222 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1224 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1227 b
[DIFF_LEFT
].off
= i
;
1228 b
[DIFF_LEFT
].len
= m
- i
;
1229 b
[DIFF_RIGHT
].off
= i
;
1230 b
[DIFF_RIGHT
].len
= n
- i
;
1232 /* smartscan (multiple horizontal diff) */
1233 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1236 /* --------------------------------------------------------------------------------------------- */
1238 /* read line **************************************************************** */
1241 * Check if character is inside horizontal diff limits.
1243 * @param k rank of character inside line
1244 * @param hdiff horizontal diff structure
1245 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1247 * @return TRUE if inside hdiff limits, FALSE otherwise
1251 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1256 for (i
= 0; i
< hdiff
->len
; i
++)
1260 b
= &g_array_index (hdiff
, BRACKET
, i
);
1261 start
= (*b
)[ord
].off
;
1262 end
= start
+ (*b
)[ord
].len
;
1263 if (k
>= start
&& k
< end
)
1269 /* --------------------------------------------------------------------------------------------- */
1272 * Copy 'src' to 'dst' expanding tabs.
1273 * @note The procedure returns when all bytes are consumed from 'src'
1275 * @param dst destination buffer
1276 * @param src source buffer
1277 * @param srcsize size of src buffer
1278 * @param base virtual base of this string, needed to calculate tabs
1279 * @param ts tab size
1281 * @return new virtual base
1285 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1289 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1296 j
= TAB_SKIP (ts
, i
+ base
);
1306 /* --------------------------------------------------------------------------------------------- */
1309 * Copy 'src' to 'dst' expanding tabs.
1311 * @param dst destination buffer
1312 * @param dstsize size of dst buffer
1313 * @param[in,out] _src source buffer
1314 * @param srcsize size of src buffer
1315 * @param base virtual base of this string, needed to calculate tabs
1316 * @param ts tab size
1318 * @return new virtual base
1320 * @note The procedure returns when all bytes are consumed from 'src'
1321 * or 'dstsize' bytes are written to 'dst'
1322 * @note Upon return, 'src' points to the first unwritten character in source
1326 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1329 const char *src
= *_src
;
1331 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1338 j
= TAB_SKIP (ts
, i
+ base
);
1339 if (j
> dstsize
- i
)
1351 /* --------------------------------------------------------------------------------------------- */
1354 * Read line from memory, converting tabs to spaces and padding with spaces.
1356 * @param src buffer to read from
1357 * @param srcsize size of src buffer
1358 * @param dst buffer to read to
1359 * @param dstsize size of dst buffer, excluding trailing null
1360 * @param skip number of characters to skip
1361 * @param ts tab size
1362 * @param show_cr show trailing carriage return as ^M
1364 * @return negative on error, otherwise number of bytes except padding
1368 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1378 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1384 j
= TAB_SKIP (ts
, i
+ base
);
1390 else if (dstsize
!= 0)
1397 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1399 if (skip
== 0 && show_cr
)
1422 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1425 if (!g_unichar_isprint (utf_ch
))
1436 while (dstsize
!= 0)
1445 /* --------------------------------------------------------------------------------------------- */
1448 * Read line from memory and build attribute array.
1450 * @param src buffer to read from
1451 * @param srcsize size of src buffer
1452 * @param dst buffer to read to
1453 * @param dstsize size of dst buffer, excluding trailing null
1454 * @param skip number of characters to skip
1455 * @param ts tab size
1456 * @param show_cr show trailing carriage return as ^M
1457 * @param hdiff horizontal diff structure
1458 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1459 * @param att buffer of attributes
1461 * @return negative on error, otherwise number of bytes except padding
1465 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1466 GArray
* hdiff
, diff_place_t ord
, char *att
)
1476 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1482 j
= TAB_SKIP (ts
, i
+ base
);
1488 else if (dstsize
!= 0)
1491 *att
++ = is_inside (k
, hdiff
, ord
);
1496 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1498 if (skip
== 0 && show_cr
)
1503 *att
++ = is_inside (k
, hdiff
, ord
);
1505 *att
++ = is_inside (k
, hdiff
, ord
);
1511 *att
++ = is_inside (k
, hdiff
, ord
);
1524 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1527 if (!g_unichar_isprint (utf_ch
))
1533 *att
++ = is_inside (k
, hdiff
, ord
);
1539 while (dstsize
!= 0)
1549 /* --------------------------------------------------------------------------------------------- */
1552 * Read line from file, converting tabs to spaces and padding with spaces.
1554 * @param f file stream to read from
1555 * @param off offset of line inside file
1556 * @param dst buffer to read to
1557 * @param dstsize size of dst buffer, excluding trailing null
1558 * @param skip number of characters to skip
1559 * @param ts tab size
1560 * @param show_cr show trailing carriage return as ^M
1562 * @return negative on error, otherwise number of bytes except padding
1566 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1569 int old_base
= base
;
1570 size_t amount
= dstsize
;
1571 size_t useful
, offset
;
1575 const char *q
= NULL
;
1576 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1577 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1579 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1581 /* abnormal, but avoid buffer overflow */
1582 memset (dst
, ' ', dstsize
);
1583 dst
[dstsize
] = '\0';
1587 f_seek (f
, off
, SEEK_SET
);
1592 sz
= f_gets (tmp
, amount
, f
);
1596 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1597 if (cvt
[base
- old_base
- 1] == '\n')
1599 q
= &cvt
[base
- old_base
- 1];
1600 base
= old_base
+ q
- cvt
+ 1;
1607 memset (dst
, ' ', dstsize
);
1608 dst
[dstsize
] = '\0';
1612 useful
= base
- skip
;
1613 offset
= skip
- old_base
;
1615 if (useful
<= dstsize
)
1618 memmove (dst
, cvt
+ offset
, useful
);
1622 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1625 const char *ptr
= tmp
;
1627 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1636 memmove (dst
, cvt
+ offset
, dstsize
);
1638 lastch
= cvt
[offset
+ dstsize
];
1642 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1644 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1648 if (i
+ 1 < dstsize
)
1662 for (; i
< dstsize
; i
++)
1668 /* --------------------------------------------------------------------------------------------- */
1669 /* diff printers et al ****************************************************** */
1672 cc_free_elt (void *elt
)
1680 /* --------------------------------------------------------------------------------------------- */
1683 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1685 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1686 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1696 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1698 if (sz
!= 0 && str
[sz
- 1] == '\n')
1701 p
.p
= g_strndup (str
, sz
);
1704 g_array_append_val (a
, p
);
1706 else if (dsrc
== DATA_SRC_MEM
)
1710 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1711 if (sz
!= 0 && str
[sz
- 1] == '\n')
1718 new_size
= p
->u
.len
+ sz
;
1719 q
= g_realloc (p
->p
, new_size
);
1720 memcpy (q
+ p
->u
.len
, str
, sz
);
1725 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1727 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1728 f_write (f
, str
, sz
);
1733 /* --------------------------------------------------------------------------------------------- */
1736 redo_diff (WDiff
* dview
)
1738 FBUF
*const *f
= dview
->f
;
1746 if (dview
->opt
.quality
== 2)
1747 strcat (extra
, " -d");
1748 if (dview
->opt
.quality
== 1)
1749 strcat (extra
, " --speed-large-files");
1750 if (dview
->opt
.strip_trailing_cr
)
1751 strcat (extra
, " --strip-trailing-cr");
1752 if (dview
->opt
.ignore_tab_expansion
)
1753 strcat (extra
, " -E");
1754 if (dview
->opt
.ignore_space_change
)
1755 strcat (extra
, " -b");
1756 if (dview
->opt
.ignore_all_space
)
1757 strcat (extra
, " -w");
1758 if (dview
->opt
.ignore_case
)
1759 strcat (extra
, " -i");
1761 if (dview
->dsrc
!= DATA_SRC_MEM
)
1763 f_reset (f
[DIFF_LEFT
]);
1764 f_reset (f
[DIFF_RIGHT
]);
1767 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1768 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1772 g_array_free (ops
, TRUE
);
1776 ctx
.dsrc
= dview
->dsrc
;
1779 ctx
.a
= dview
->a
[DIFF_LEFT
];
1780 ctx
.f
= f
[DIFF_LEFT
];
1781 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1783 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1784 ctx
.f
= f
[DIFF_RIGHT
];
1785 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1788 g_array_free (ops
, TRUE
);
1790 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1793 if (dview
->dsrc
== DATA_SRC_TMP
)
1795 f_trunc (f
[DIFF_LEFT
]);
1796 f_trunc (f
[DIFF_RIGHT
]);
1799 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1801 dview
->hdiff
= g_ptr_array_new ();
1802 if (dview
->hdiff
!= NULL
)
1808 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1812 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1813 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1814 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1816 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1822 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1826 g_array_free (h
, TRUE
);
1831 g_ptr_array_add (dview
->hdiff
, h
);
1838 /* --------------------------------------------------------------------------------------------- */
1841 destroy_hdiff (WDiff
* dview
)
1843 if (dview
->hdiff
!= NULL
)
1848 len
= dview
->a
[DIFF_LEFT
]->len
;
1850 for (i
= 0; i
< len
; i
++)
1854 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1856 g_array_free (h
, TRUE
);
1858 g_ptr_array_free (dview
->hdiff
, TRUE
);
1859 dview
->hdiff
= NULL
;
1862 mc_search_free (dview
->search
.handle
);
1863 dview
->search
.handle
= NULL
;
1864 g_free (dview
->search
.last_string
);
1865 dview
->search
.last_string
= NULL
;
1868 /* --------------------------------------------------------------------------------------------- */
1869 /* stuff ******************************************************************** */
1872 get_digits (unsigned int n
)
1881 /* --------------------------------------------------------------------------------------------- */
1884 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1896 p
= &g_array_index (a
, DIFFLN
, pos
);
1902 for (n
= pos
; n
> 0; n
--)
1908 *lineofs
= pos
- n
+ 1;
1916 /* --------------------------------------------------------------------------------------------- */
1919 calc_nwidth (const GArray
** const a
)
1924 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1925 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1928 return get_digits (l1
);
1931 /* --------------------------------------------------------------------------------------------- */
1934 find_prev_hunk (const GArray
* a
, int pos
)
1937 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1939 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1941 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1943 if (pos
> 0 && (size_t) pos
< a
->len
)
1946 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1948 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1955 /* --------------------------------------------------------------------------------------------- */
1958 find_next_hunk (const GArray
* a
, size_t pos
)
1960 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1962 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1967 /* --------------------------------------------------------------------------------------------- */
1969 * Find start and end lines of the current hunk.
1971 * @param dview WDiff widget
1972 * @return boolean and
1973 * start_line1 first line of current hunk (file[0])
1974 * end_line1 last line of current hunk (file[0])
1975 * start_line1 first line of current hunk (file[0])
1976 * end_line1 last line of current hunk (file[0])
1980 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1982 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1983 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1993 pos
= dview
->skip_rows
;
1994 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2009 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2013 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2014 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2016 pos
= dview
->skip_rows
;
2017 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2021 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2022 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2024 *end_line1
= max (*start_line1
, l0
);
2026 *end_line2
= max (*start_line2
, l1
);
2033 /* --------------------------------------------------------------------------------------------- */
2035 * Remove hunk from file.
2037 * @param dview WDiff widget
2038 * @param merge_file file stream for writing data
2039 * @param from1 first line of hunk
2040 * @param to1 last line of hunk
2041 * @param merge_direction in what direction files should be merged
2045 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2046 action_direction_t merge_direction
)
2052 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2053 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2055 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2058 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2061 fputs (buf
, merge_file
);
2063 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2067 fputs (buf
, merge_file
);
2072 /* --------------------------------------------------------------------------------------------- */
2076 * @param dview WDiff widget
2077 * @param merge_file file stream for writing data
2078 * @param from1 first line of source hunk
2079 * @param from2 first line of destination hunk
2080 * @param to1 last line of source hunk
2081 * @param merge_direction in what direction files should be merged
2085 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2086 action_direction_t merge_direction
)
2093 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2095 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2096 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2100 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2101 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2105 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2108 fputs (buf
, merge_file
);
2111 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2115 fputs (buf
, merge_file
);
2117 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2118 fputs (buf
, merge_file
);
2124 /* --------------------------------------------------------------------------------------------- */
2126 * Replace hunk in file.
2128 * @param dview WDiff widget
2129 * @param merge_file file stream for writing data
2130 * @param from1 first line of source hunk
2131 * @param to1 last line of source hunk
2132 * @param from2 first line of destination hunk
2133 * @param to2 last line of destination hunk
2134 * @param merge_direction in what direction files should be merged
2138 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2139 action_direction_t merge_direction
)
2141 int line1
= 0, line2
= 0;
2146 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2148 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2149 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2153 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2154 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2157 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2160 fputs (buf
, merge_file
);
2162 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2166 fputs (buf
, merge_file
);
2168 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2172 fputs (buf
, merge_file
);
2178 /* --------------------------------------------------------------------------------------------- */
2182 * @param dview WDiff widget
2183 * @param merge_direction in what direction files should be merged
2187 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2189 int from1
, to1
, from2
, to2
;
2192 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2194 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2195 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2197 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2203 vfs_path_t
*merge_file_name_vpath
= NULL
;
2205 if (!dview
->merged
[n_merge
])
2207 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2208 if (!dview
->merged
[n_merge
])
2210 message (D_ERROR
, MSG_ERROR
,
2211 _("Cannot create backup file\n%s%s\n%s"),
2212 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2217 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2218 if (merge_file_fd
== -1)
2220 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2221 unix_error_string (errno
));
2225 merge_file
= fdopen (merge_file_fd
, "w");
2230 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2231 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2233 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2236 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2237 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2239 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2242 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2245 fflush (merge_file
);
2246 fclose (merge_file
);
2247 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2248 mc_unlink (merge_file_name_vpath
);
2249 vfs_path_free (merge_file_name_vpath
);
2253 /* --------------------------------------------------------------------------------------------- */
2254 /* view routines and callbacks ********************************************** */
2257 dview_compute_split (WDiff
* dview
, int i
)
2260 if (dview
->bias
< 2 - dview
->half1
)
2261 dview
->bias
= 2 - dview
->half1
;
2262 if (dview
->bias
> dview
->half2
- 2)
2263 dview
->bias
= dview
->half2
- 2;
2266 /* --------------------------------------------------------------------------------------------- */
2269 dview_compute_areas (WDiff
* dview
)
2271 dview
->height
= LINES
- 2;
2272 dview
->half1
= COLS
/ 2;
2273 dview
->half2
= COLS
- dview
->half1
;
2275 dview_compute_split (dview
, 0);
2278 /* --------------------------------------------------------------------------------------------- */
2281 dview_reread (WDiff
* dview
)
2285 destroy_hdiff (dview
);
2286 if (dview
->a
[DIFF_LEFT
] != NULL
)
2288 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2289 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2291 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2293 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2294 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2297 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2298 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2300 ndiff
= redo_diff (dview
);
2302 dview
->ndiff
= ndiff
;
2305 /* --------------------------------------------------------------------------------------------- */
2309 dview_set_codeset (WDiff
* dview
)
2311 const char *encoding_id
= NULL
;
2315 get_codepage_id (mc_global
.source_codepage
>=
2316 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2317 if (encoding_id
!= NULL
)
2321 conv
= str_crt_conv_from (encoding_id
);
2322 if (conv
!= INVALID_CONV
)
2324 if (dview
->converter
!= str_cnv_from_term
)
2325 str_close_conv (dview
->converter
);
2326 dview
->converter
= conv
;
2328 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
2332 /* --------------------------------------------------------------------------------------------- */
2335 dview_select_encoding (WDiff
* dview
)
2337 if (do_select_codepage ())
2338 dview_set_codeset (dview
);
2339 dview_reread (dview
);
2340 tty_touch_screen ();
2343 #endif /* HAVE_CHARSET */
2345 /* --------------------------------------------------------------------------------------------- */
2348 dview_diff_options (WDiff
* dview
)
2350 const char *quality_str
[] = {
2352 N_("&Fastest (Assume large files)"),
2353 N_("&Minimal (Find a smaller set of change)")
2356 QuickWidget diffopt_widgets
[] = {
2357 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2358 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2360 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2361 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2362 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2363 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2364 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2365 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2366 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2367 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2368 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2369 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2370 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2371 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2372 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2373 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2378 QuickDialog diffopt
= {
2380 N_("Diff Options"), "[Diff Options]",
2381 diffopt_widgets
, NULL
, NULL
, FALSE
2384 if (quick_dialog (&diffopt
) != B_CANCEL
)
2385 dview_reread (dview
);
2388 /* --------------------------------------------------------------------------------------------- */
2391 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2392 const char *label1
, const char *label2
, DSRC dsrc
)
2395 FBUF
*f
[DIFF_COUNT
];
2397 f
[DIFF_LEFT
] = NULL
;
2398 f
[DIFF_RIGHT
] = NULL
;
2400 if (dsrc
== DATA_SRC_TMP
)
2402 f
[DIFF_LEFT
] = f_temp ();
2403 if (f
[DIFF_LEFT
] == NULL
)
2406 f
[DIFF_RIGHT
] = f_temp ();
2407 if (f
[DIFF_RIGHT
] == NULL
)
2409 f_close (f
[DIFF_LEFT
]);
2413 else if (dsrc
== DATA_SRC_ORG
)
2415 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2416 if (f
[DIFF_LEFT
] == NULL
)
2419 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2420 if (f
[DIFF_RIGHT
] == NULL
)
2422 f_close (f
[DIFF_LEFT
]);
2428 dview
->file
[DIFF_LEFT
] = file1
;
2429 dview
->file
[DIFF_RIGHT
] = file2
;
2430 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2431 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2432 dview
->f
[DIFF_LEFT
] = f
[0];
2433 dview
->f
[DIFF_RIGHT
] = f
[1];
2434 dview
->merged
[DIFF_LEFT
] = FALSE
;
2435 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2436 dview
->hdiff
= NULL
;
2438 dview
->converter
= str_cnv_from_term
;
2440 dview_set_codeset (dview
);
2442 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2443 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2445 ndiff
= redo_diff (dview
);
2448 /* goto WIDGET_DESTROY stage: dview_fini() */
2449 f_close (f
[DIFF_LEFT
]);
2450 f_close (f
[DIFF_RIGHT
]);
2454 dview
->ndiff
= ndiff
;
2456 dview
->view_quit
= 0;
2459 dview
->new_frame
= 1;
2460 dview
->skip_rows
= 0;
2461 dview
->skip_cols
= 0;
2462 dview
->display_symbols
= 0;
2463 dview
->display_numbers
= 0;
2465 dview
->tab_size
= 8;
2466 dview
->ord
= DIFF_LEFT
;
2469 dview
->search
.handle
= NULL
;
2470 dview
->search
.last_string
= NULL
;
2471 dview
->search
.last_found_line
= -1;
2472 dview
->search
.last_accessed_num_line
= -1;
2474 dview
->opt
.quality
= 0;
2475 dview
->opt
.strip_trailing_cr
= 0;
2476 dview
->opt
.ignore_tab_expansion
= 0;
2477 dview
->opt
.ignore_space_change
= 0;
2478 dview
->opt
.ignore_all_space
= 0;
2479 dview
->opt
.ignore_case
= 0;
2481 dview_compute_areas (dview
);
2486 /* --------------------------------------------------------------------------------------------- */
2489 dview_fini (WDiff
* dview
)
2491 if (dview
->dsrc
!= DATA_SRC_MEM
)
2493 f_close (dview
->f
[DIFF_RIGHT
]);
2494 f_close (dview
->f
[DIFF_LEFT
]);
2497 if (dview
->converter
!= str_cnv_from_term
)
2498 str_close_conv (dview
->converter
);
2500 destroy_hdiff (dview
);
2501 if (dview
->a
[DIFF_LEFT
] != NULL
)
2503 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2504 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2505 dview
->a
[DIFF_LEFT
] = NULL
;
2507 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2509 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2510 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2511 dview
->a
[DIFF_RIGHT
] = NULL
;
2514 g_free (dview
->label
[DIFF_LEFT
]);
2515 g_free (dview
->label
[DIFF_RIGHT
]);
2518 /* --------------------------------------------------------------------------------------------- */
2521 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2526 FBUF
*f
= dview
->f
[ord
];
2527 int skip
= dview
->skip_cols
;
2528 int display_symbols
= dview
->display_symbols
;
2529 int display_numbers
= dview
->display_numbers
;
2530 int show_cr
= dview
->show_cr
;
2533 int nwidth
= display_numbers
;
2536 xwidth
= display_symbols
+ display_numbers
;
2537 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2538 tab_size
= dview
->tab_size
;
2542 if (xwidth
> width
&& display_symbols
)
2545 display_symbols
= 0;
2547 if (xwidth
> width
&& display_numbers
)
2550 display_numbers
= width
;
2560 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2562 /* abnormal, but avoid buffer overflow */
2566 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2568 int ch
, next_ch
, col
;
2571 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2573 tty_setcolor (NORMAL_COLOR
);
2574 if (display_symbols
)
2576 tty_gotoyx (r
+ j
, c
- 2);
2577 tty_print_char (ch
);
2581 if (display_numbers
)
2583 tty_gotoyx (r
+ j
, c
- xwidth
);
2584 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2585 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2588 tty_setcolor (DFF_ADD_COLOR
);
2590 tty_setcolor (DFF_CHG_COLOR
);
2593 if (i
== (size_t) dview
->search
.last_found_line
)
2594 tty_setcolor (MARKED_SELECTED_COLOR
);
2595 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2600 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2604 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2605 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2606 tty_gotoyx (r
+ j
, c
);
2609 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2616 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2619 if (!g_unichar_isprint (next_ch
))
2623 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2627 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2629 if (mc_global
.utf8_display
)
2634 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2638 else if (dview
->utf8
)
2639 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2641 next_ch
= convert_to_display_c (next_ch
);
2643 tty_print_anychar (next_ch
);
2651 tty_setcolor (DFF_CHH_COLOR
);
2654 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2657 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2660 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2664 if (display_numbers
)
2666 tty_gotoyx (r
+ j
, c
- xwidth
);
2667 memset (buf
, ' ', display_numbers
);
2668 buf
[display_numbers
] = '\0';
2669 tty_print_string (buf
);
2672 tty_setcolor (DFF_DEL_COLOR
);
2674 tty_setcolor (DFF_CHD_COLOR
);
2675 memset (buf
, ' ', width
);
2678 tty_gotoyx (r
+ j
, c
);
2679 /* tty_print_nstring (buf, width); */
2681 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2688 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2691 if (!g_unichar_isprint (next_ch
))
2695 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2699 if (mc_global
.utf8_display
)
2704 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2707 else if (dview
->utf8
)
2708 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2710 next_ch
= convert_to_display_c (next_ch
);
2713 tty_print_anychar (next_ch
);
2718 tty_setcolor (NORMAL_COLOR
);
2720 if (width
< xwidth
- 1)
2722 memset (buf
, ' ', k
);
2724 for (; j
< height
; j
++)
2728 tty_gotoyx (r
+ j
, c
- xwidth
);
2729 /* tty_print_nstring (buf, xwidth - 1); */
2730 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2732 tty_gotoyx (r
+ j
, c
);
2733 /* tty_print_nstring (buf, width); */
2734 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2740 /* --------------------------------------------------------------------------------------------- */
2743 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2747 int linenum
, lineofs
;
2751 tty_setcolor (STATUSBAR_COLOR
);
2754 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2756 filename_width
= width
- 24;
2757 if (filename_width
< 8)
2760 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2761 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2762 vfs_path_free (vpath
);
2763 buf
= str_term_trim (path
, filename_width
);
2764 if (ord
== DIFF_LEFT
)
2765 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2766 buf
, linenum
, lineofs
, dview
->skip_cols
);
2768 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2769 buf
, linenum
, lineofs
, dview
->ndiff
);
2773 /* --------------------------------------------------------------------------------------------- */
2776 dview_redo (WDiff
* dview
)
2778 if (dview
->display_numbers
)
2782 old
= dview
->display_numbers
;
2783 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2784 dview
->new_frame
= (old
!= dview
->display_numbers
);
2786 dview_reread (dview
);
2789 /* --------------------------------------------------------------------------------------------- */
2792 dview_update (WDiff
* dview
)
2794 int height
= dview
->height
;
2799 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2801 if (dview
->skip_rows
> last
)
2802 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2803 if (dview
->skip_rows
< 0)
2804 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2805 if (dview
->skip_cols
< 0)
2806 dview
->skip_cols
= 0;
2811 width1
= dview
->half1
+ dview
->bias
;
2812 width2
= dview
->half2
- dview
->bias
;
2819 if (dview
->new_frame
)
2823 tty_setcolor (NORMAL_COLOR
);
2824 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2826 tty_draw_box (1, 0, height
, width1
, FALSE
);
2828 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2833 if (xwidth
< width1
- 1)
2835 tty_gotoyx (1, xwidth
);
2836 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2837 tty_gotoyx (height
, xwidth
);
2838 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2839 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2841 if (xwidth
< width2
- 1)
2843 tty_gotoyx (1, width1
+ xwidth
);
2844 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2845 tty_gotoyx (height
, width1
+ xwidth
);
2846 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2847 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2850 dview
->new_frame
= 0;
2855 dview_status (dview
, dview
->ord
, width1
, 0);
2856 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2860 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2861 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2865 /* --------------------------------------------------------------------------------------------- */
2868 dview_edit (WDiff
* dview
, diff_place_t ord
)
2872 int linenum
, lineofs
;
2874 if (dview
->dsrc
== DATA_SRC_TMP
)
2876 error_dialog (_("Edit"), _("Edit is disabled"));
2880 h
= ((Widget
*) dview
)->owner
;
2883 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2884 h
->modal
= TRUE
; /* not allow edit file in several editors */
2886 vfs_path_t
*tmp_vpath
;
2888 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2889 do_edit_at_line (tmp_vpath
, use_internal_edit
, linenum
);
2890 vfs_path_free (tmp_vpath
);
2894 dview_update (dview
);
2897 /* --------------------------------------------------------------------------------------------- */
2900 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2903 static const char *title
[2] = {
2904 N_("Goto line (left)"),
2905 N_("Goto line (right)")
2908 static char prev
[256];
2909 /* XXX some statics here, to be remembered between runs */
2914 input
= input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2917 const char *s
= input
;
2919 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2927 for (; i
< dview
->a
[ord
]->len
; i
++)
2929 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2930 if (p
->line
== newline
)
2934 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2935 g_snprintf (prev
, sizeof (prev
), "%d", newline
);
2941 /* --------------------------------------------------------------------------------------------- */
2944 dview_labels (WDiff
* dview
)
2949 h
= dview
->widget
.owner
;
2950 b
= find_buttonbar (h
);
2952 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2953 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2954 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2955 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2956 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2957 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, (Widget
*) dview
);
2958 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2961 /* --------------------------------------------------------------------------------------------- */
2964 dview_event (Gpm_Event
* event
, void *data
)
2966 WDiff
*dview
= (WDiff
*) data
;
2968 if (!mouse_global_in_widget (event
, data
))
2969 return MOU_UNHANDLED
;
2971 /* We are not interested in release events */
2972 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2976 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2978 dview
->skip_rows
-= 2;
2979 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2980 dview_update (dview
);
2982 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2984 dview
->skip_rows
+= 2;
2985 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2986 dview_update (dview
);
2992 /* --------------------------------------------------------------------------------------------- */
2995 dview_save (WDiff
* dview
)
2997 gboolean res
= TRUE
;
2999 if (dview
->merged
[DIFF_LEFT
])
3001 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3002 dview
->merged
[DIFF_LEFT
] = !res
;
3004 if (dview
->merged
[DIFF_RIGHT
])
3006 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3007 dview
->merged
[DIFF_RIGHT
] = !res
;
3012 /* --------------------------------------------------------------------------------------------- */
3015 dview_do_save (WDiff
* dview
)
3017 (void) dview_save (dview
);
3020 /* --------------------------------------------------------------------------------------------- */
3023 dview_save_options (WDiff
* dview
)
3025 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
3026 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
3027 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
3028 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
3029 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
3031 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3033 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
3034 dview
->opt
.strip_trailing_cr
);
3035 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
3036 dview
->opt
.ignore_all_space
);
3037 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
3038 dview
->opt
.ignore_space_change
);
3039 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
3040 dview
->opt
.ignore_tab_expansion
);
3041 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
3044 /* --------------------------------------------------------------------------------------------- */
3047 dview_load_options (WDiff
* dview
)
3049 gboolean show_numbers
, show_symbols
;
3052 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
3054 dview
->display_symbols
= 1;
3055 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
3057 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
3058 tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
3059 if (tab_size
> 0 && tab_size
< 9)
3060 dview
->tab_size
= tab_size
;
3062 dview
->tab_size
= 8;
3064 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
3066 dview
->opt
.strip_trailing_cr
=
3067 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3068 dview
->opt
.ignore_all_space
=
3069 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3070 dview
->opt
.ignore_space_change
=
3071 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3072 dview
->opt
.ignore_tab_expansion
=
3073 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3074 dview
->opt
.ignore_case
=
3075 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
3077 dview
->new_frame
= 1;
3080 /* --------------------------------------------------------------------------------------------- */
3083 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3087 dview_ok_to_exit (WDiff
* dview
)
3089 gboolean res
= TRUE
;
3092 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3095 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3096 _("File(s) was modified. Save with exit?") :
3097 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3098 D_NORMAL
, 2, _("&Yes"), _("&No"));
3101 if (mc_global
.midnight_shutdown
|| (act
== -1))
3110 (void) dview_save (dview
);
3114 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3115 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3116 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3117 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3126 /* --------------------------------------------------------------------------------------------- */
3129 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
3131 cb_ret_t res
= MSG_HANDLED
;
3135 case CK_ShowSymbols
:
3136 dview
->display_symbols
^= 1;
3137 dview
->new_frame
= 1;
3139 case CK_ShowNumbers
:
3140 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
3141 dview
->new_frame
= 1;
3145 dview
->new_frame
= 1;
3151 dview
->new_frame
= 1;
3157 dview_compute_split (dview
, 1);
3158 dview
->new_frame
= 1;
3165 dview_compute_split (dview
, -1);
3166 dview
->new_frame
= 1;
3170 dview
->tab_size
= 2;
3173 dview
->tab_size
= 3;
3176 dview
->tab_size
= 4;
3179 dview
->tab_size
= 8;
3188 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3189 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3192 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3193 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3196 dview_goto_cmd (dview
, TRUE
);
3199 dview_edit (dview
, dview
->ord
);
3202 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3206 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3210 dview_edit (dview
, dview
->ord
^ 1);
3213 dview_search_cmd (dview
);
3215 case CK_SearchContinue
:
3216 dview_continue_search_cmd (dview
);
3219 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3222 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3225 if (dview
->skip_rows
> 0)
3228 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3233 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3236 if (dview
->height
> 2)
3238 dview
->skip_rows
+= dview
->height
- 2;
3239 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3243 if (dview
->height
> 2)
3245 dview
->skip_rows
-= dview
->height
- 2;
3246 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3256 dview
->skip_cols
-= 8;
3259 dview
->skip_cols
+= 8;
3262 dview
->skip_cols
= 0;
3268 dview
->view_quit
= 1;
3271 dview_do_save (dview
);
3274 dview_diff_options (dview
);
3277 case CK_SelectCodepage
:
3278 dview_select_encoding (dview
);
3282 /* don't close diffviewer due to SIGINT */
3285 res
= MSG_NOT_HANDLED
;
3290 /* --------------------------------------------------------------------------------------------- */
3293 dview_handle_key (WDiff
* dview
, int key
)
3295 unsigned long command
;
3298 key
= convert_from_input_c (key
);
3301 command
= keybind_lookup_keymap_command (diff_map
, key
);
3302 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3306 return MSG_NOT_HANDLED
;
3309 /* --------------------------------------------------------------------------------------------- */
3312 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3314 WDiff
*dview
= (WDiff
*) w
;
3315 Dlg_head
*h
= dview
->widget
.owner
;
3321 dview_labels (dview
);
3322 dview_load_options (dview
);
3323 dview_update (dview
);
3327 dview
->new_frame
= 1;
3328 dview_update (dview
);
3332 i
= dview_handle_key (dview
, parm
);
3333 if (dview
->view_quit
)
3336 dview_update (dview
);
3339 case WIDGET_COMMAND
:
3340 i
= dview_execute_cmd (dview
, parm
);
3341 if (dview
->view_quit
)
3344 dview_update (dview
);
3347 case WIDGET_DESTROY
:
3348 dview_save_options (dview
);
3353 return default_proc (msg
, parm
);
3357 /* --------------------------------------------------------------------------------------------- */
3360 dview_adjust_size (Dlg_head
* h
)
3365 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3366 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3367 bar
= find_buttonbar (h
);
3368 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3369 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3371 dview_compute_areas (dview
);
3374 /* --------------------------------------------------------------------------------------------- */
3377 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3379 WDiff
*dview
= (WDiff
*) data
;
3384 dview_adjust_size (h
);
3390 return dview_execute_cmd (NULL
, parm
);
3391 /* message from buttonbar */
3392 if (sender
== (Widget
*) find_buttonbar (h
))
3395 return send_message ((Widget
*) data
, WIDGET_COMMAND
, parm
);
3397 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3398 return dview_execute_cmd (dview
, parm
);
3400 return MSG_NOT_HANDLED
;
3403 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3404 h
->state
= DLG_ACTIVE
; /* don't stop the dialog before final decision */
3405 if (dview_ok_to_exit (dview
))
3406 h
->state
= DLG_CLOSED
;
3410 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3414 /* --------------------------------------------------------------------------------------------- */
3417 dview_get_title (const Dlg_head
* h
, size_t len
)
3420 const char *modified
= " (*) ";
3421 const char *notmodified
= " ";
3425 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3426 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3428 title
= g_string_sized_new (len
);
3429 g_string_append (title
, _("Diff:"));
3430 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3431 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3432 g_string_append (title
, " | ");
3433 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3434 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3436 return g_string_free (title
, FALSE
);
3439 /* --------------------------------------------------------------------------------------------- */
3442 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3446 Dlg_head
*dview_dlg
;
3448 /* Create dialog and widgets, put them on the dialog */
3450 create_dlg (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3451 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3453 dview
= g_new0 (WDiff
, 1);
3455 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3456 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3458 widget_want_cursor (dview
->widget
, 0);
3460 add_widget (dview_dlg
, dview
);
3461 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3463 dview_dlg
->get_title
= dview_get_title
;
3465 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3467 /* Please note that if you add another widget,
3468 * you have to modify dview_adjust_size to
3472 run_dlg (dview_dlg
);
3474 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3475 destroy_dlg (dview_dlg
);
3477 return error
== 0 ? 1 : 0;
3480 /*** public functions ****************************************************************************/
3481 /* --------------------------------------------------------------------------------------------- */
3483 #define GET_FILE_AND_STAMP(n) \
3487 real_file##n = file##n; \
3488 if (!vfs_file_is_local (file##n)) \
3490 real_file##n = mc_getlocalcopy (file##n); \
3491 if (real_file##n != NULL) \
3494 if (mc_stat (real_file##n, &st##n) != 0) \
3501 #define UNGET_FILE(n) \
3507 if (use_copy##n > 0) \
3510 mtime = st##n.st_mtime; \
3511 if (mc_stat (real_file##n, &st##n) == 0) \
3512 changed = (mtime != st##n.st_mtime); \
3514 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3515 vfs_path_free (real_file##n); \
3521 dview_diff_cmd (const void *f0
, const void *f1
)
3524 vfs_path_t
*file0
= NULL
;
3525 vfs_path_t
*file1
= NULL
;
3526 gboolean is_dir0
= FALSE
;
3527 gboolean is_dir1
= FALSE
;
3529 switch (mc_global
.mc_run_mode
)
3533 /* run from panels */
3534 const WPanel
*panel0
= (const WPanel
*) f0
;
3535 const WPanel
*panel1
= (const WPanel
*) f1
;
3537 file0
= vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, NULL
);
3538 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3541 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3542 path_trunc (selection (panel0
)->fname
, 30));
3546 file1
= vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, NULL
);
3547 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3550 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3551 path_trunc (selection (panel1
)->fname
, 30));
3557 case MC_RUN_DIFFVIEWER
:
3559 /* run from command line */
3560 const char *p0
= (const char *) f0
;
3561 const char *p1
= (const char *) f1
;
3564 file0
= vfs_path_from_str (p0
);
3565 if (mc_stat (file0
, &st
) == 0)
3567 is_dir0
= S_ISDIR (st
.st_mode
);
3570 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3576 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3577 path_trunc (p0
, 30), unix_error_string (errno
));
3581 file1
= vfs_path_from_str (p1
);
3582 if (mc_stat (file1
, &st
) == 0)
3584 is_dir1
= S_ISDIR (st
.st_mode
);
3587 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3593 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3594 path_trunc (p1
, 30), unix_error_string (errno
));
3601 /* this should not happaned */
3602 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3609 if (file0
!= NULL
&& file1
!= NULL
)
3615 vfs_path_t
*real_file0
;
3616 vfs_path_t
*real_file1
;
3618 GET_FILE_AND_STAMP (0);
3619 GET_FILE_AND_STAMP (1);
3620 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3622 char *real_file0_str
, *real_file1_str
;
3623 char *file0_str
, *file1_str
;
3625 real_file0_str
= vfs_path_to_str (real_file0
);
3626 real_file1_str
= vfs_path_to_str (real_file1
);
3627 file0_str
= vfs_path_to_str (file0
);
3628 file1_str
= vfs_path_to_str (file1
);
3629 rv
= diff_view (real_file0_str
, real_file1_str
, file0_str
, file1_str
);
3630 g_free (real_file0_str
);
3631 g_free (real_file1_str
);
3641 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3644 vfs_path_free (file1
);
3645 vfs_path_free (file0
);
3650 #undef GET_FILE_AND_STAMP
3653 /* --------------------------------------------------------------------------------------------- */