2 Copyright (C) 2007-2015
3 Free Software Foundation, Inc.
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
8 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013
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" /* edit_file_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;\
74 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
76 TP *g_array_foreach_var; \
78 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
79 (*cbf) (g_array_foreach_var); \
83 #define FILE_READ_BUF 4096
84 #define FILE_FLAG_TEMP (1 << 0)
91 #define HDIFF_ENABLE 1
92 #define HDIFF_MINCTX 5
93 #define HDIFF_DEPTH 10
95 #define FILE_DIRTY(fs) \
103 /*** file scope type declarations ****************************************************************/
109 } action_direction_t
;
111 /*** file scope variables ************************************************************************/
113 /*** file scope functions ************************************************************************/
114 /* --------------------------------------------------------------------------------------------- */
117 TAB_SKIP (int ts
, int pos
)
119 if (ts
> 0 && ts
< 9)
120 return ts
- pos
% ts
;
125 /* --------------------------------------------------------------------------------------------- */
128 rewrite_backup_content (const vfs_path_t
* from_file_name_vpath
, const char *to_file_name
)
133 const char *from_file_name
;
135 from_file_name
= vfs_path_get_by_index (from_file_name_vpath
, -1)->path
;
136 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
139 backup_fd
= fopen (to_file_name
, "w");
140 if (backup_fd
== NULL
)
146 length
= fwrite ((const void *) contents
, length
, 1, backup_fd
);
154 /* buffered I/O ************************************************************* */
157 * Try to open a temporary file.
158 * @note the name is not altered if this function fails
160 * @param[out] name address of a pointer to store the temporary name
161 * @return file descriptor on success, negative on error
165 open_temp (void **name
)
168 vfs_path_t
*diff_file_name
= NULL
;
170 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
173 message (D_ERROR
, MSG_ERROR
,
174 _("Cannot create temporary diff file\n%s"), unix_error_string (errno
));
177 *name
= g_strdup (vfs_path_as_str (diff_file_name
));
178 vfs_path_free (diff_file_name
);
182 /* --------------------------------------------------------------------------------------------- */
185 * Alocate file structure and associate file descriptor to it.
187 * @param fd file descriptor
188 * @return file structure
199 fs
= g_try_malloc (sizeof (FBUF
));
203 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
218 /* --------------------------------------------------------------------------------------------- */
221 * Free file structure without closing the file.
223 * @param fs file structure
224 * @return 0 on success, non-zero on error
232 if (fs
->flags
& FILE_FLAG_TEMP
)
234 rv
= unlink (fs
->data
);
242 /* --------------------------------------------------------------------------------------------- */
245 * Open a binary temporary file in R/W mode.
246 * @note the file will be deleted when closed
248 * @return file structure
260 fd
= open_temp (&fs
->data
);
268 fs
->flags
= FILE_FLAG_TEMP
;
272 /* --------------------------------------------------------------------------------------------- */
275 * Open a binary file in specified mode.
277 * @param filename file name
278 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
280 * @return file structure
284 f_open (const char *filename
, int flags
)
293 fd
= open (filename
, flags
);
304 /* --------------------------------------------------------------------------------------------- */
307 * Read a line of bytes from file until newline or EOF.
308 * @note does not stop on null-byte
309 * @note buf will not be null-terminated
311 * @param buf destination buffer
312 * @param size size of buffer
313 * @param fs file structure
315 * @return number of bytes read
319 f_gets (char *buf
, size_t size
, FBUF
* fs
)
328 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
336 if (j
== size
|| stop
)
340 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
347 /* --------------------------------------------------------------------------------------------- */
351 * @note avoids thrashing read cache when possible
353 * @param fs file structure
355 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
357 * @return position in file, starting from begginning
361 f_seek (FBUF
* fs
, off_t off
, int whence
)
365 if (fs
->len
&& whence
!= SEEK_END
)
367 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
370 if (whence
== SEEK_CUR
)
373 off
+= rv
- fs
->len
+ fs
->pos
;
375 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
377 fs
->pos
= fs
->len
+ off
- rv
;
383 rv
= lseek (fs
->fd
, off
, whence
);
389 /* --------------------------------------------------------------------------------------------- */
392 * Seek to the beginning of file, thrashing read cache.
394 * @param fs file structure
396 * @return 0 if success, non-zero on error
404 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
410 /* --------------------------------------------------------------------------------------------- */
413 * Write bytes to file.
414 * @note thrashes read cache
416 * @param fs file structure
417 * @param buf source buffer
418 * @param size size of buffer
420 * @return number of written bytes, -1 on error
424 f_write (FBUF
* fs
, const char *buf
, size_t size
)
428 rv
= write (fs
->fd
, buf
, size
);
434 /* --------------------------------------------------------------------------------------------- */
437 * Truncate file to the current position.
438 * @note thrashes read cache
440 * @param fs file structure
442 * @return current file size on success, negative on error
450 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
455 rv
= ftruncate (fs
->fd
, off
);
464 /* --------------------------------------------------------------------------------------------- */
468 * @note if this is temporary file, it is deleted
470 * @param fs file structure
471 * @return 0 on success, non-zero on error
488 /* --------------------------------------------------------------------------------------------- */
491 * Create pipe stream to process.
493 * @param cmd shell command line
494 * @param flags open mode, either O_RDONLY or O_WRONLY
496 * @return file structure
500 p_open (const char *cmd
, int flags
)
504 const char *type
= NULL
;
506 if (flags
== O_RDONLY
)
508 else if (flags
== O_WRONLY
)
518 f
= popen (cmd
, type
);
530 /* --------------------------------------------------------------------------------------------- */
535 * @param fs structure
536 * @return 0 on success, non-zero on error
546 rv
= pclose (fs
->data
);
553 /* --------------------------------------------------------------------------------------------- */
556 * Get one char (byte) from string
558 * @param char * str, gboolean * result
559 * @return int as character or 0 and result == FALSE if fail
563 dview_get_byte (char *str
, gboolean
* result
)
571 return (unsigned char) *str
;
574 /* --------------------------------------------------------------------------------------------- */
577 * Get utf multibyte char from string
579 * @param char * str, int * char_length, gboolean * result
580 * @return int as utf character or 0 and result == FALSE if fail
584 dview_get_utf (char *str
, int *char_length
, gboolean
* result
)
588 gchar
*next_ch
= NULL
;
599 res
= g_utf8_get_char_validated (str
, -1);
606 /* Calculate UTF-8 char length */
607 next_ch
= g_utf8_next_char (str
);
608 ch_len
= next_ch
- str
;
610 *char_length
= ch_len
;
614 /* --------------------------------------------------------------------------------------------- */
617 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
621 if (text
== NULL
|| text
[0] == '\0')
624 if (g_utf8_validate (text
, -1, NULL
))
625 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
629 char *tmpbuf
, *buffer
;
631 buffer
= tmpbuf
= g_strdup (text
);
632 while (tmpbuf
[0] != '\0')
634 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
635 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
636 tmpbuf
= g_utf8_next_char (tmpbuf
);
643 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
646 return max (length
, (size_t) result
);
649 /* --------------------------------------------------------------------------------------------- */
651 /* diff parse *************************************************************** */
654 * Read decimal number from string.
656 * @param[in,out] str string to parse
657 * @param[out] n extracted number
658 * @return 0 if success, otherwise non-zero
662 scan_deci (const char **str
, int *n
)
664 const char *p
= *str
;
668 *n
= strtol (p
, &q
, 10);
669 if (errno
!= 0 || p
== q
)
675 /* --------------------------------------------------------------------------------------------- */
678 * Parse line for diff statement.
680 * @param p string to parse
681 * @param ops list of diff statements
682 * @return 0 if success, otherwise non-zero
686 scan_line (const char *p
, GArray
* ops
)
695 /* handle the following cases:
697 * NUM[,NUM]cNUM[,NUM]
699 * where NUM is a positive integer
702 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
710 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
722 else if (cmd
!= 'c' && cmd
!= 'd')
725 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
733 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
739 if (cmd
== 'd' && range
!= 0)
747 g_array_append_val (ops
, op
);
751 /* --------------------------------------------------------------------------------------------- */
754 * Parse diff output and extract diff statements.
756 * @param f stream to read from
757 * @param ops list of diff statements to fill
758 * @return positive number indicating number of hunks, otherwise negative
762 scan_diff (FBUF
* f
, GArray
* ops
)
767 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
769 if (isdigit (buf
[0]))
771 if (buf
[sz
- 1] != '\n')
775 if (scan_line (buf
, ops
) != 0)
781 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
788 /* --------------------------------------------------------------------------------------------- */
791 * Invoke diff and extract diff statements.
793 * @param args extra arguments to be passed to diff
794 * @param extra more arguments to be passed to diff
795 * @param file1 first file to compare
796 * @param file2 second file to compare
797 * @param ops list of diff statements to fill
799 * @return positive number indicating number of hunks, otherwise negative
803 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
806 static const char *opt
=
807 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
808 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
809 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
810 " --unchanged-group-format=''";
816 char *file1_esc
, *file2_esc
;
818 /* escape potential $ to avoid shell variable substitutions in popen() */
819 file1_esc
= strutils_shell_escape (file1
);
820 file2_esc
= strutils_shell_escape (file2
);
821 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
828 f
= p_open (cmd
, O_RDONLY
);
834 rv
= scan_diff (f
, ops
);
837 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
843 /* --------------------------------------------------------------------------------------------- */
846 * Reparse and display file according to diff statements.
848 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
849 * @param filename file name to display
850 * @param ops list of diff statements
851 * @param printer printf-like function to be used for displaying
852 * @param ctx printer context
854 * @return 0 if success, otherwise non-zero
858 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
871 f
= f_open (filename
, O_RDONLY
);
887 #define T1 a[ ord^1 ][0]
888 #define T2 a[ ord^1 ][1]
889 for (i
= 0; i
< ops
->len
; i
++)
893 op
= &g_array_index (ops
, DIFFCMD
, i
);
894 n
= op
->F1
- (op
->cmd
!= add_cmd
);
896 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
899 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
901 while (buf
[sz
- 1] != '\n')
903 sz
= f_gets (buf
, sizeof (buf
), f
);
906 printer (ctx
, 0, 0, 0, 1, "\n");
909 printer (ctx
, 0, 0, 0, sz
, buf
);
917 if (op
->cmd
== add_cmd
)
919 n
= op
->T2
- op
->T1
+ 1;
922 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
927 if (op
->cmd
== del_cmd
)
929 n
= op
->F2
- op
->F1
+ 1;
930 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
933 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
935 while (buf
[sz
- 1] != '\n')
937 sz
= f_gets (buf
, sizeof (buf
), f
);
940 printer (ctx
, 0, 0, 0, 1, "\n");
943 printer (ctx
, 0, 0, 0, sz
, buf
);
955 n
= op
->F2
- op
->F1
+ 1;
956 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
959 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
961 while (buf
[sz
- 1] != '\n')
963 sz
= f_gets (buf
, sizeof (buf
), f
);
966 printer (ctx
, 0, 0, 0, 1, "\n");
969 printer (ctx
, 0, 0, 0, sz
, buf
);
978 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
981 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
991 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
994 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
996 while (buf
[sz
- 1] != '\n')
998 sz
= f_gets (buf
, sizeof (buf
), f
);
1001 printer (ctx
, 0, 0, 0, 1, "\n");
1004 printer (ctx
, 0, 0, 0, sz
, buf
);
1017 /* --------------------------------------------------------------------------------------------- */
1019 /* horizontal diff ********************************************************** */
1022 * Longest common substring.
1024 * @param s first string
1025 * @param m length of first string
1026 * @param t second string
1027 * @param n length of second string
1028 * @param ret list of offsets for longest common substrings inside each string
1029 * @param min minimum length of common substrings
1031 * @return 0 if success, nonzero otherwise
1035 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1041 if (m
< min
|| n
< min
)
1043 /* XXX early culling */
1047 Lprev
= g_try_new0 (int, n
+ 1);
1051 Lcurr
= g_try_new0 (int, n
+ 1);
1058 for (i
= 0; i
< m
; i
++)
1065 #ifdef USE_MEMSET_IN_LCS
1066 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1068 for (j
= 0; j
< n
; j
++)
1070 #ifndef USE_MEMSET_IN_LCS
1082 g_array_set_size (ret
, 0);
1084 if (z
== v
&& z
>= min
)
1092 for (k
= 0; k
< ret
->len
; k
++)
1094 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1095 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1104 g_array_append_val (ret
, p2
);
1116 /* --------------------------------------------------------------------------------------------- */
1119 * Scan recursively for common substrings and build ranges.
1121 * @param s first string
1122 * @param t second string
1123 * @param bracket current limits for both of the strings
1124 * @param min minimum length of common substrings
1125 * @param hdiff list of horizontal diff ranges to fill
1126 * @param depth recursion depth
1128 * @return 0 if success, nonzero otherwise
1132 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1143 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1147 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1148 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1152 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1155 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1156 b
[DIFF_LEFT
].len
= (*data
)[0];
1157 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1158 b
[DIFF_RIGHT
].len
= (*data
)[1];
1159 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1162 for (k
= 0; k
< ret
->len
- 1; k
++)
1164 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1165 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1166 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1167 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1168 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1169 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1170 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1173 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1174 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1175 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1176 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1177 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1178 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1181 g_array_free (ret
, TRUE
);
1186 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1187 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1188 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1189 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1190 g_array_append_val (hdiff
, p
);
1195 /* --------------------------------------------------------------------------------------------- */
1198 * Build list of horizontal diff ranges.
1200 * @param s first string
1201 * @param m length of first string
1202 * @param t second string
1203 * @param n length of second string
1204 * @param min minimum length of common substrings
1205 * @param hdiff list of horizontal diff ranges to fill
1206 * @param depth recursion depth
1208 * @return 0 if success, nonzero otherwise
1212 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1217 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1218 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1220 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1223 b
[DIFF_LEFT
].off
= i
;
1224 b
[DIFF_LEFT
].len
= m
- i
;
1225 b
[DIFF_RIGHT
].off
= i
;
1226 b
[DIFF_RIGHT
].len
= n
- i
;
1228 /* smartscan (multiple horizontal diff) */
1229 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1232 /* --------------------------------------------------------------------------------------------- */
1234 /* read line **************************************************************** */
1237 * Check if character is inside horizontal diff limits.
1239 * @param k rank of character inside line
1240 * @param hdiff horizontal diff structure
1241 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1243 * @return TRUE if inside hdiff limits, FALSE otherwise
1247 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1252 for (i
= 0; i
< hdiff
->len
; i
++)
1256 b
= &g_array_index (hdiff
, BRACKET
, i
);
1257 start
= (*b
)[ord
].off
;
1258 end
= start
+ (*b
)[ord
].len
;
1259 if (k
>= start
&& k
< end
)
1265 /* --------------------------------------------------------------------------------------------- */
1268 * Copy 'src' to 'dst' expanding tabs.
1269 * @note The procedure returns when all bytes are consumed from 'src'
1271 * @param dst destination buffer
1272 * @param src source buffer
1273 * @param srcsize size of src buffer
1274 * @param base virtual base of this string, needed to calculate tabs
1275 * @param ts tab size
1277 * @return new virtual base
1281 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1285 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1292 j
= TAB_SKIP (ts
, i
+ base
);
1302 /* --------------------------------------------------------------------------------------------- */
1305 * Copy 'src' to 'dst' expanding tabs.
1307 * @param dst destination buffer
1308 * @param dstsize size of dst buffer
1309 * @param[in,out] _src source buffer
1310 * @param srcsize size of src buffer
1311 * @param base virtual base of this string, needed to calculate tabs
1312 * @param ts tab size
1314 * @return new virtual base
1316 * @note The procedure returns when all bytes are consumed from 'src'
1317 * or 'dstsize' bytes are written to 'dst'
1318 * @note Upon return, 'src' points to the first unwritten character in source
1322 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1325 const char *src
= *_src
;
1327 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1334 j
= TAB_SKIP (ts
, i
+ base
);
1335 if (j
> dstsize
- i
)
1347 /* --------------------------------------------------------------------------------------------- */
1350 * Read line from memory, converting tabs to spaces and padding with spaces.
1352 * @param src buffer to read from
1353 * @param srcsize size of src buffer
1354 * @param dst buffer to read to
1355 * @param dstsize size of dst buffer, excluding trailing null
1356 * @param skip number of characters to skip
1357 * @param ts tab size
1358 * @param show_cr show trailing carriage return as ^M
1360 * @return negative on error, otherwise number of bytes except padding
1364 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1374 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1380 j
= TAB_SKIP (ts
, i
+ base
);
1386 else if (dstsize
!= 0)
1393 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1395 if (skip
== 0 && show_cr
)
1418 utf_ch
= dview_get_utf ((char *) src
, &ch_len
, &res
);
1431 while (dstsize
!= 0)
1440 /* --------------------------------------------------------------------------------------------- */
1443 * Read line from memory and build attribute array.
1445 * @param src buffer to read from
1446 * @param srcsize size of src buffer
1447 * @param dst buffer to read to
1448 * @param dstsize size of dst buffer, excluding trailing null
1449 * @param skip number of characters to skip
1450 * @param ts tab size
1451 * @param show_cr show trailing carriage return as ^M
1452 * @param hdiff horizontal diff structure
1453 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1454 * @param att buffer of attributes
1456 * @return negative on error, otherwise number of bytes except padding
1460 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1461 GArray
* hdiff
, diff_place_t ord
, char *att
)
1471 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1477 j
= TAB_SKIP (ts
, i
+ base
);
1483 else if (dstsize
!= 0)
1486 *att
++ = is_inside (k
, hdiff
, ord
);
1491 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1493 if (skip
== 0 && show_cr
)
1498 *att
++ = is_inside (k
, hdiff
, ord
);
1500 *att
++ = is_inside (k
, hdiff
, ord
);
1506 *att
++ = is_inside (k
, hdiff
, ord
);
1519 utf_ch
= dview_get_utf ((char *) src
, &ch_len
, &res
);
1527 *att
++ = is_inside (k
, hdiff
, ord
);
1533 while (dstsize
!= 0)
1543 /* --------------------------------------------------------------------------------------------- */
1546 * Read line from file, converting tabs to spaces and padding with spaces.
1548 * @param f file stream to read from
1549 * @param off offset of line inside file
1550 * @param dst buffer to read to
1551 * @param dstsize size of dst buffer, excluding trailing null
1552 * @param skip number of characters to skip
1553 * @param ts tab size
1554 * @param show_cr show trailing carriage return as ^M
1556 * @return negative on error, otherwise number of bytes except padding
1560 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1563 int old_base
= base
;
1564 size_t amount
= dstsize
;
1565 size_t useful
, offset
;
1569 const char *q
= NULL
;
1570 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1571 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1573 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1575 /* abnormal, but avoid buffer overflow */
1576 memset (dst
, ' ', dstsize
);
1577 dst
[dstsize
] = '\0';
1581 f_seek (f
, off
, SEEK_SET
);
1586 sz
= f_gets (tmp
, amount
, f
);
1590 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1591 if (cvt
[base
- old_base
- 1] == '\n')
1593 q
= &cvt
[base
- old_base
- 1];
1594 base
= old_base
+ q
- cvt
+ 1;
1601 memset (dst
, ' ', dstsize
);
1602 dst
[dstsize
] = '\0';
1606 useful
= base
- skip
;
1607 offset
= skip
- old_base
;
1609 if (useful
<= dstsize
)
1612 memmove (dst
, cvt
+ offset
, useful
);
1616 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1619 const char *ptr
= tmp
;
1621 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1630 memmove (dst
, cvt
+ offset
, dstsize
);
1632 lastch
= cvt
[offset
+ dstsize
];
1636 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1638 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1642 if (i
+ 1 < dstsize
)
1656 for (; i
< dstsize
; i
++)
1662 /* --------------------------------------------------------------------------------------------- */
1663 /* diff printers et al ****************************************************** */
1666 cc_free_elt (void *elt
)
1674 /* --------------------------------------------------------------------------------------------- */
1677 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1679 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1680 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1690 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1692 if (sz
!= 0 && str
[sz
- 1] == '\n')
1695 p
.p
= g_strndup (str
, sz
);
1698 g_array_append_val (a
, p
);
1700 else if (dsrc
== DATA_SRC_MEM
)
1704 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1705 if (sz
!= 0 && str
[sz
- 1] == '\n')
1712 new_size
= p
->u
.len
+ sz
;
1713 q
= g_realloc (p
->p
, new_size
);
1714 memcpy (q
+ p
->u
.len
, str
, sz
);
1719 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1721 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1722 f_write (f
, str
, sz
);
1727 /* --------------------------------------------------------------------------------------------- */
1730 redo_diff (WDiff
* dview
)
1732 FBUF
*const *f
= dview
->f
;
1740 if (dview
->opt
.quality
== 2)
1741 strcat (extra
, " -d");
1742 if (dview
->opt
.quality
== 1)
1743 strcat (extra
, " --speed-large-files");
1744 if (dview
->opt
.strip_trailing_cr
)
1745 strcat (extra
, " --strip-trailing-cr");
1746 if (dview
->opt
.ignore_tab_expansion
)
1747 strcat (extra
, " -E");
1748 if (dview
->opt
.ignore_space_change
)
1749 strcat (extra
, " -b");
1750 if (dview
->opt
.ignore_all_space
)
1751 strcat (extra
, " -w");
1752 if (dview
->opt
.ignore_case
)
1753 strcat (extra
, " -i");
1755 if (dview
->dsrc
!= DATA_SRC_MEM
)
1757 f_reset (f
[DIFF_LEFT
]);
1758 f_reset (f
[DIFF_RIGHT
]);
1761 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1762 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1766 g_array_free (ops
, TRUE
);
1770 ctx
.dsrc
= dview
->dsrc
;
1773 ctx
.a
= dview
->a
[DIFF_LEFT
];
1774 ctx
.f
= f
[DIFF_LEFT
];
1775 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1777 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1778 ctx
.f
= f
[DIFF_RIGHT
];
1779 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1782 g_array_free (ops
, TRUE
);
1784 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1787 if (dview
->dsrc
== DATA_SRC_TMP
)
1789 f_trunc (f
[DIFF_LEFT
]);
1790 f_trunc (f
[DIFF_RIGHT
]);
1793 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1795 dview
->hdiff
= g_ptr_array_new ();
1796 if (dview
->hdiff
!= NULL
)
1800 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1806 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1807 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1808 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1810 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1816 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1820 g_array_free (h
, TRUE
);
1825 g_ptr_array_add (dview
->hdiff
, h
);
1832 /* --------------------------------------------------------------------------------------------- */
1835 destroy_hdiff (WDiff
* dview
)
1837 if (dview
->hdiff
!= NULL
)
1842 len
= dview
->a
[DIFF_LEFT
]->len
;
1844 for (i
= 0; i
< len
; i
++)
1848 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1850 g_array_free (h
, TRUE
);
1852 g_ptr_array_free (dview
->hdiff
, TRUE
);
1853 dview
->hdiff
= NULL
;
1856 mc_search_free (dview
->search
.handle
);
1857 dview
->search
.handle
= NULL
;
1858 MC_PTR_FREE (dview
->search
.last_string
);
1861 /* --------------------------------------------------------------------------------------------- */
1862 /* stuff ******************************************************************** */
1865 get_digits (unsigned int n
)
1874 /* --------------------------------------------------------------------------------------------- */
1877 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1889 p
= &g_array_index (a
, DIFFLN
, pos
);
1895 for (n
= pos
; n
> 0; n
--)
1901 *lineofs
= pos
- n
+ 1;
1909 /* --------------------------------------------------------------------------------------------- */
1912 calc_nwidth (const GArray
** const a
)
1917 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1918 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1921 return get_digits (l1
);
1924 /* --------------------------------------------------------------------------------------------- */
1927 find_prev_hunk (const GArray
* a
, int pos
)
1930 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
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 if (pos
> 0 && (size_t) pos
< a
->len
)
1939 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1941 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1948 /* --------------------------------------------------------------------------------------------- */
1951 find_next_hunk (const GArray
* a
, size_t pos
)
1953 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1955 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1960 /* --------------------------------------------------------------------------------------------- */
1962 * Find start and end lines of the current hunk.
1964 * @param dview WDiff widget
1965 * @return boolean and
1966 * start_line1 first line of current hunk (file[0])
1967 * end_line1 last line of current hunk (file[0])
1968 * start_line1 first line of current hunk (file[0])
1969 * end_line1 last line of current hunk (file[0])
1973 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1975 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1976 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1986 pos
= dview
->skip_rows
;
1987 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2002 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2006 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2007 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2009 pos
= dview
->skip_rows
;
2010 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2014 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2015 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2017 *end_line1
= max (*start_line1
, l0
);
2019 *end_line2
= max (*start_line2
, l1
);
2026 /* --------------------------------------------------------------------------------------------- */
2028 * Remove hunk from file.
2030 * @param dview WDiff widget
2031 * @param merge_file file stream for writing data
2032 * @param from1 first line of hunk
2033 * @param to1 last line of hunk
2034 * @param merge_direction in what direction files should be merged
2038 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2039 action_direction_t merge_direction
)
2045 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2046 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2048 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2051 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2054 fputs (buf
, merge_file
);
2056 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2060 fputs (buf
, merge_file
);
2065 /* --------------------------------------------------------------------------------------------- */
2069 * @param dview WDiff widget
2070 * @param merge_file file stream for writing data
2071 * @param from1 first line of source hunk
2072 * @param from2 first line of destination hunk
2073 * @param to1 last line of source hunk
2074 * @param merge_direction in what direction files should be merged
2078 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2079 action_direction_t merge_direction
)
2086 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2088 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2089 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2093 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2094 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2098 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2101 fputs (buf
, merge_file
);
2104 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2108 fputs (buf
, merge_file
);
2110 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2111 fputs (buf
, merge_file
);
2117 /* --------------------------------------------------------------------------------------------- */
2119 * Replace hunk in file.
2121 * @param dview WDiff widget
2122 * @param merge_file file stream for writing data
2123 * @param from1 first line of source hunk
2124 * @param to1 last line of source hunk
2125 * @param from2 first line of destination hunk
2126 * @param to2 last line of destination hunk
2127 * @param merge_direction in what direction files should be merged
2131 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2132 action_direction_t merge_direction
)
2134 int line1
= 0, line2
= 0;
2139 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2141 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2142 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2146 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2147 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2150 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2153 fputs (buf
, merge_file
);
2155 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2159 fputs (buf
, merge_file
);
2161 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2165 fputs (buf
, merge_file
);
2171 /* --------------------------------------------------------------------------------------------- */
2175 * @param dview WDiff widget
2176 * @param merge_direction in what direction files should be merged
2180 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2182 int from1
, to1
, from2
, to2
;
2184 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2186 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2187 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2189 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2195 vfs_path_t
*merge_file_name_vpath
= NULL
;
2197 if (!dview
->merged
[n_merge
])
2199 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2200 if (!dview
->merged
[n_merge
])
2202 message (D_ERROR
, MSG_ERROR
,
2203 _("Cannot create backup file\n%s%s\n%s"),
2204 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2209 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2210 if (merge_file_fd
== -1)
2212 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2213 unix_error_string (errno
));
2217 merge_file
= fdopen (merge_file_fd
, "w");
2222 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2223 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2225 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2228 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2229 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2231 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2234 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2237 fflush (merge_file
);
2238 fclose (merge_file
);
2242 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2245 mc_unlink (merge_file_name_vpath
);
2246 vfs_path_free (merge_file_name_vpath
);
2250 /* --------------------------------------------------------------------------------------------- */
2251 /* view routines and callbacks ********************************************** */
2254 dview_compute_split (WDiff
* dview
, int i
)
2257 if (dview
->bias
< 2 - dview
->half1
)
2258 dview
->bias
= 2 - dview
->half1
;
2259 if (dview
->bias
> dview
->half2
- 2)
2260 dview
->bias
= dview
->half2
- 2;
2263 /* --------------------------------------------------------------------------------------------- */
2266 dview_compute_areas (WDiff
* dview
)
2268 dview
->height
= LINES
- 2;
2269 dview
->half1
= COLS
/ 2;
2270 dview
->half2
= COLS
- dview
->half1
;
2272 dview_compute_split (dview
, 0);
2275 /* --------------------------------------------------------------------------------------------- */
2278 dview_reread (WDiff
* dview
)
2282 destroy_hdiff (dview
);
2283 if (dview
->a
[DIFF_LEFT
] != NULL
)
2285 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2286 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2288 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2290 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2291 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2294 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2295 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2297 ndiff
= redo_diff (dview
);
2299 dview
->ndiff
= ndiff
;
2302 /* --------------------------------------------------------------------------------------------- */
2306 dview_set_codeset (WDiff
* dview
)
2308 const char *encoding_id
= NULL
;
2312 get_codepage_id (mc_global
.source_codepage
>=
2313 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2314 if (encoding_id
!= NULL
)
2318 conv
= str_crt_conv_from (encoding_id
);
2319 if (conv
!= INVALID_CONV
)
2321 if (dview
->converter
!= str_cnv_from_term
)
2322 str_close_conv (dview
->converter
);
2323 dview
->converter
= conv
;
2325 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
2329 /* --------------------------------------------------------------------------------------------- */
2332 dview_select_encoding (WDiff
* dview
)
2334 if (do_select_codepage ())
2335 dview_set_codeset (dview
);
2336 dview_reread (dview
);
2337 tty_touch_screen ();
2340 #endif /* HAVE_CHARSET */
2342 /* --------------------------------------------------------------------------------------------- */
2345 dview_diff_options (WDiff
* dview
)
2347 const char *quality_str
[] = {
2349 N_("&Fastest (Assume large files)"),
2350 N_("&Minimal (Find a smaller set of change)")
2353 quick_widget_t quick_widgets
[] = {
2355 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2356 QUICK_RADIO (3, (const char **) quality_str
, (int *) &dview
->opt
.quality
, NULL
),
2357 QUICK_STOP_GROUPBOX
,
2358 QUICK_START_GROUPBOX (N_("Diff extra options")),
2359 QUICK_CHECKBOX (N_("&Ignore case"), &dview
->opt
.ignore_case
, NULL
),
2360 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
, NULL
),
2361 QUICK_CHECKBOX (N_("Ignore &space change"), &dview
->opt
.ignore_space_change
, NULL
),
2362 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
, NULL
),
2363 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
,
2365 QUICK_STOP_GROUPBOX
,
2366 QUICK_BUTTONS_OK_CANCEL
,
2371 quick_dialog_t qdlg
= {
2373 N_("Diff Options"), "[Diff Options]",
2374 quick_widgets
, NULL
, NULL
2377 if (quick_dialog (&qdlg
) != B_CANCEL
)
2378 dview_reread (dview
);
2381 /* --------------------------------------------------------------------------------------------- */
2384 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2385 const char *label1
, const char *label2
, DSRC dsrc
)
2388 FBUF
*f
[DIFF_COUNT
];
2390 f
[DIFF_LEFT
] = NULL
;
2391 f
[DIFF_RIGHT
] = NULL
;
2393 if (dsrc
== DATA_SRC_TMP
)
2395 f
[DIFF_LEFT
] = f_temp ();
2396 if (f
[DIFF_LEFT
] == NULL
)
2399 f
[DIFF_RIGHT
] = f_temp ();
2400 if (f
[DIFF_RIGHT
] == NULL
)
2402 f_close (f
[DIFF_LEFT
]);
2406 else if (dsrc
== DATA_SRC_ORG
)
2408 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2409 if (f
[DIFF_LEFT
] == NULL
)
2412 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2413 if (f
[DIFF_RIGHT
] == NULL
)
2415 f_close (f
[DIFF_LEFT
]);
2421 dview
->file
[DIFF_LEFT
] = file1
;
2422 dview
->file
[DIFF_RIGHT
] = file2
;
2423 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2424 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2425 dview
->f
[DIFF_LEFT
] = f
[0];
2426 dview
->f
[DIFF_RIGHT
] = f
[1];
2427 dview
->merged
[DIFF_LEFT
] = FALSE
;
2428 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2429 dview
->hdiff
= NULL
;
2431 dview
->converter
= str_cnv_from_term
;
2433 dview_set_codeset (dview
);
2435 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2436 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2438 ndiff
= redo_diff (dview
);
2441 /* goto MSG_DESTROY stage: dview_fini() */
2442 f_close (f
[DIFF_LEFT
]);
2443 f_close (f
[DIFF_RIGHT
]);
2447 dview
->ndiff
= ndiff
;
2449 dview
->view_quit
= 0;
2452 dview
->new_frame
= 1;
2453 dview
->skip_rows
= 0;
2454 dview
->skip_cols
= 0;
2455 dview
->display_symbols
= 0;
2456 dview
->display_numbers
= 0;
2458 dview
->tab_size
= 8;
2459 dview
->ord
= DIFF_LEFT
;
2462 dview
->search
.handle
= NULL
;
2463 dview
->search
.last_string
= NULL
;
2464 dview
->search
.last_found_line
= -1;
2465 dview
->search
.last_accessed_num_line
= -1;
2467 dview
->opt
.quality
= 0;
2468 dview
->opt
.strip_trailing_cr
= 0;
2469 dview
->opt
.ignore_tab_expansion
= 0;
2470 dview
->opt
.ignore_space_change
= 0;
2471 dview
->opt
.ignore_all_space
= 0;
2472 dview
->opt
.ignore_case
= 0;
2474 dview_compute_areas (dview
);
2479 /* --------------------------------------------------------------------------------------------- */
2482 dview_fini (WDiff
* dview
)
2484 if (dview
->dsrc
!= DATA_SRC_MEM
)
2486 f_close (dview
->f
[DIFF_RIGHT
]);
2487 f_close (dview
->f
[DIFF_LEFT
]);
2490 if (dview
->converter
!= str_cnv_from_term
)
2491 str_close_conv (dview
->converter
);
2493 destroy_hdiff (dview
);
2494 if (dview
->a
[DIFF_LEFT
] != NULL
)
2496 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2497 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2498 dview
->a
[DIFF_LEFT
] = NULL
;
2500 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2502 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2503 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2504 dview
->a
[DIFF_RIGHT
] = NULL
;
2507 g_free (dview
->label
[DIFF_LEFT
]);
2508 g_free (dview
->label
[DIFF_RIGHT
]);
2511 /* --------------------------------------------------------------------------------------------- */
2514 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2519 FBUF
*f
= dview
->f
[ord
];
2520 int skip
= dview
->skip_cols
;
2521 int display_symbols
= dview
->display_symbols
;
2522 int display_numbers
= dview
->display_numbers
;
2523 int show_cr
= dview
->show_cr
;
2526 int nwidth
= display_numbers
;
2529 xwidth
= display_symbols
+ display_numbers
;
2530 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2531 tab_size
= dview
->tab_size
;
2535 if (xwidth
> width
&& display_symbols
)
2538 display_symbols
= 0;
2540 if (xwidth
> width
&& display_numbers
)
2543 display_numbers
= width
;
2553 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2555 /* abnormal, but avoid buffer overflow */
2559 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2561 int ch
, next_ch
, col
;
2564 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2566 tty_setcolor (NORMAL_COLOR
);
2567 if (display_symbols
)
2569 tty_gotoyx (r
+ j
, c
- 2);
2570 tty_print_char (ch
);
2574 if (display_numbers
)
2576 tty_gotoyx (r
+ j
, c
- xwidth
);
2577 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2578 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2581 tty_setcolor (DFF_ADD_COLOR
);
2583 tty_setcolor (DFF_CHG_COLOR
);
2586 if (i
== (size_t) dview
->search
.last_found_line
)
2587 tty_setcolor (MARKED_SELECTED_COLOR
);
2588 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2593 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2597 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2598 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2599 tty_gotoyx (r
+ j
, c
);
2602 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2610 next_ch
= dview_get_utf (buf
+ cnt
, &ch_len
, &ch_res
);
2613 if (!g_unichar_isprint (next_ch
))
2617 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2621 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2623 if (mc_global
.utf8_display
)
2628 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2632 else if (dview
->utf8
)
2633 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2635 next_ch
= convert_to_display_c (next_ch
);
2637 tty_print_anychar (next_ch
);
2645 tty_setcolor (DFF_CHH_COLOR
);
2648 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2651 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2654 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2658 if (display_numbers
)
2660 tty_gotoyx (r
+ j
, c
- xwidth
);
2661 memset (buf
, ' ', display_numbers
);
2662 buf
[display_numbers
] = '\0';
2663 tty_print_string (buf
);
2666 tty_setcolor (DFF_DEL_COLOR
);
2668 tty_setcolor (DFF_CHD_COLOR
);
2669 memset (buf
, ' ', width
);
2672 tty_gotoyx (r
+ j
, c
);
2673 /* tty_print_nstring (buf, width); */
2675 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2683 next_ch
= dview_get_utf (buf
+ cnt
, &ch_len
, &ch_res
);
2686 if (!g_unichar_isprint (next_ch
))
2690 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2694 if (mc_global
.utf8_display
)
2699 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2702 else if (dview
->utf8
)
2703 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2705 next_ch
= convert_to_display_c (next_ch
);
2708 tty_print_anychar (next_ch
);
2713 tty_setcolor (NORMAL_COLOR
);
2715 if (width
< xwidth
- 1)
2717 memset (buf
, ' ', k
);
2719 for (; j
< height
; j
++)
2723 tty_gotoyx (r
+ j
, c
- xwidth
);
2724 /* tty_print_nstring (buf, xwidth - 1); */
2725 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2727 tty_gotoyx (r
+ j
, c
);
2728 /* tty_print_nstring (buf, width); */
2729 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2735 /* --------------------------------------------------------------------------------------------- */
2738 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2742 int linenum
, lineofs
;
2746 tty_setcolor (STATUSBAR_COLOR
);
2749 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2751 filename_width
= width
- 24;
2752 if (filename_width
< 8)
2755 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2756 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2757 vfs_path_free (vpath
);
2758 buf
= str_term_trim (path
, filename_width
);
2759 if (ord
== DIFF_LEFT
)
2760 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2761 buf
, linenum
, lineofs
, dview
->skip_cols
);
2763 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2764 buf
, linenum
, lineofs
, dview
->ndiff
);
2768 /* --------------------------------------------------------------------------------------------- */
2771 dview_redo (WDiff
* dview
)
2773 if (dview
->display_numbers
)
2777 old
= dview
->display_numbers
;
2778 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2779 dview
->new_frame
= (old
!= dview
->display_numbers
);
2781 dview_reread (dview
);
2784 /* --------------------------------------------------------------------------------------------- */
2787 dview_update (WDiff
* dview
)
2789 int height
= dview
->height
;
2794 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2796 if (dview
->skip_rows
> last
)
2797 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2798 if (dview
->skip_rows
< 0)
2799 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2800 if (dview
->skip_cols
< 0)
2801 dview
->skip_cols
= 0;
2806 width1
= dview
->half1
+ dview
->bias
;
2807 width2
= dview
->half2
- dview
->bias
;
2814 if (dview
->new_frame
)
2818 tty_setcolor (NORMAL_COLOR
);
2819 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2821 tty_draw_box (1, 0, height
, width1
, FALSE
);
2823 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2828 if (xwidth
< width1
- 1)
2830 tty_gotoyx (1, xwidth
);
2831 tty_print_alt_char (ACS_TTEE
, FALSE
);
2832 tty_gotoyx (height
, xwidth
);
2833 tty_print_alt_char (ACS_BTEE
, FALSE
);
2834 tty_draw_vline (2, xwidth
, ACS_VLINE
, height
- 2);
2836 if (xwidth
< width2
- 1)
2838 tty_gotoyx (1, width1
+ xwidth
);
2839 tty_print_alt_char (ACS_TTEE
, FALSE
);
2840 tty_gotoyx (height
, width1
+ xwidth
);
2841 tty_print_alt_char (ACS_BTEE
, FALSE
);
2842 tty_draw_vline (2, width1
+ xwidth
, ACS_VLINE
, height
- 2);
2845 dview
->new_frame
= 0;
2850 dview_status (dview
, dview
->ord
, width1
, 0);
2851 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2855 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2856 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2860 /* --------------------------------------------------------------------------------------------- */
2863 dview_edit (WDiff
* dview
, diff_place_t ord
)
2867 int linenum
, lineofs
;
2869 if (dview
->dsrc
== DATA_SRC_TMP
)
2871 error_dialog (_("Edit"), _("Edit is disabled"));
2875 h
= WIDGET (dview
)->owner
;
2878 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2879 h
->modal
= TRUE
; /* not allow edit file in several editors */
2881 vfs_path_t
*tmp_vpath
;
2883 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2884 edit_file_at_line (tmp_vpath
, use_internal_edit
!= 0, linenum
);
2885 vfs_path_free (tmp_vpath
);
2889 dview_update (dview
);
2892 /* --------------------------------------------------------------------------------------------- */
2895 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2898 static const char *title
[2] = {
2899 N_("Goto line (left)"),
2900 N_("Goto line (right)")
2903 static char prev
[256];
2904 /* XXX some statics here, to be remembered between runs */
2910 input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
, prev
,
2911 INPUT_COMPLETE_NONE
);
2914 const char *s
= input
;
2916 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2922 for (; i
< dview
->a
[ord
]->len
; i
++)
2926 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2927 if (p
->line
== newline
)
2931 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2932 g_snprintf (prev
, sizeof (prev
), "%d", newline
);
2938 /* --------------------------------------------------------------------------------------------- */
2941 dview_labels (WDiff
* dview
)
2949 b
= find_buttonbar (h
);
2951 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, d
);
2952 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, d
);
2953 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, d
);
2954 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, d
);
2955 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, d
);
2956 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, d
);
2957 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, d
);
2960 /* --------------------------------------------------------------------------------------------- */
2963 dview_event (Gpm_Event
* event
, void *data
)
2965 WDiff
*dview
= (WDiff
*) data
;
2967 if (!mouse_global_in_widget (event
, data
))
2968 return MOU_UNHANDLED
;
2970 /* We are not interested in release events */
2971 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2975 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2977 dview
->skip_rows
-= 2;
2978 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2979 dview_update (dview
);
2981 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2983 dview
->skip_rows
+= 2;
2984 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2985 dview_update (dview
);
2991 /* --------------------------------------------------------------------------------------------- */
2994 dview_save (WDiff
* dview
)
2996 gboolean res
= TRUE
;
2998 if (dview
->merged
[DIFF_LEFT
])
3000 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3001 dview
->merged
[DIFF_LEFT
] = !res
;
3003 if (dview
->merged
[DIFF_RIGHT
])
3005 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3006 dview
->merged
[DIFF_RIGHT
] = !res
;
3011 /* --------------------------------------------------------------------------------------------- */
3014 dview_do_save (WDiff
* dview
)
3016 (void) dview_save (dview
);
3019 /* --------------------------------------------------------------------------------------------- */
3022 dview_save_options (WDiff
* dview
)
3024 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
3025 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
3026 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
3027 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
3028 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
3030 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3032 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
3033 dview
->opt
.strip_trailing_cr
);
3034 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
3035 dview
->opt
.ignore_all_space
);
3036 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
3037 dview
->opt
.ignore_space_change
);
3038 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
3039 dview
->opt
.ignore_tab_expansion
);
3040 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
3043 /* --------------------------------------------------------------------------------------------- */
3046 dview_load_options (WDiff
* dview
)
3048 gboolean show_numbers
, show_symbols
;
3051 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
3053 dview
->display_symbols
= 1;
3054 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
3056 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
3057 tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
3058 if (tab_size
> 0 && tab_size
< 9)
3059 dview
->tab_size
= tab_size
;
3061 dview
->tab_size
= 8;
3063 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
3065 dview
->opt
.strip_trailing_cr
=
3066 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3067 dview
->opt
.ignore_all_space
=
3068 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3069 dview
->opt
.ignore_space_change
=
3070 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3071 dview
->opt
.ignore_tab_expansion
=
3072 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3073 dview
->opt
.ignore_case
=
3074 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
3076 dview
->new_frame
= 1;
3079 /* --------------------------------------------------------------------------------------------- */
3082 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3086 dview_ok_to_exit (WDiff
* dview
)
3088 gboolean res
= TRUE
;
3091 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3094 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3095 _("File(s) was modified. Save with exit?") :
3096 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3097 D_NORMAL
, 2, _("&Yes"), _("&No"));
3100 if (mc_global
.midnight_shutdown
|| (act
== -1))
3109 (void) dview_save (dview
);
3113 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3114 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3115 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3116 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3125 /* --------------------------------------------------------------------------------------------- */
3128 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
3130 cb_ret_t res
= MSG_HANDLED
;
3134 case CK_ShowSymbols
:
3135 dview
->display_symbols
^= 1;
3136 dview
->new_frame
= 1;
3138 case CK_ShowNumbers
:
3139 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
3140 dview
->new_frame
= 1;
3144 dview
->new_frame
= 1;
3150 dview
->new_frame
= 1;
3156 dview_compute_split (dview
, 1);
3157 dview
->new_frame
= 1;
3164 dview_compute_split (dview
, -1);
3165 dview
->new_frame
= 1;
3169 dview
->tab_size
= 2;
3172 dview
->tab_size
= 3;
3175 dview
->tab_size
= 4;
3178 dview
->tab_size
= 8;
3187 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3188 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3191 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3192 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3195 dview_goto_cmd (dview
, TRUE
);
3198 dview_edit (dview
, dview
->ord
);
3201 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3205 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3209 dview_edit (dview
, dview
->ord
^ 1);
3212 dview_search_cmd (dview
);
3214 case CK_SearchContinue
:
3215 dview_continue_search_cmd (dview
);
3218 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3221 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3224 if (dview
->skip_rows
> 0)
3227 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3232 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3235 if (dview
->height
> 2)
3237 dview
->skip_rows
+= dview
->height
- 2;
3238 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3242 if (dview
->height
> 2)
3244 dview
->skip_rows
-= dview
->height
- 2;
3245 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3255 dview
->skip_cols
-= 8;
3258 dview
->skip_cols
+= 8;
3261 dview
->skip_cols
= 0;
3267 dview
->view_quit
= 1;
3270 dview_do_save (dview
);
3273 dview_diff_options (dview
);
3276 case CK_SelectCodepage
:
3277 dview_select_encoding (dview
);
3281 /* don't close diffviewer due to SIGINT */
3284 res
= MSG_NOT_HANDLED
;
3289 /* --------------------------------------------------------------------------------------------- */
3292 dview_handle_key (WDiff
* dview
, int key
)
3294 unsigned long command
;
3297 key
= convert_from_input_c (key
);
3300 command
= keybind_lookup_keymap_command (diff_map
, key
);
3301 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3305 return MSG_NOT_HANDLED
;
3308 /* --------------------------------------------------------------------------------------------- */
3311 dview_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3313 WDiff
*dview
= (WDiff
*) w
;
3314 WDialog
*h
= w
->owner
;
3320 dview_labels (dview
);
3321 dview_load_options (dview
);
3322 dview_update (dview
);
3326 dview
->new_frame
= 1;
3327 dview_update (dview
);
3331 i
= dview_handle_key (dview
, parm
);
3332 if (dview
->view_quit
)
3335 dview_update (dview
);
3339 i
= dview_execute_cmd (dview
, parm
);
3340 if (dview
->view_quit
)
3343 dview_update (dview
);
3347 dview_save_options (dview
);
3352 return widget_default_callback (w
, sender
, msg
, parm
, data
);
3356 /* --------------------------------------------------------------------------------------------- */
3359 dview_adjust_size (WDialog
* h
)
3364 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3365 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3366 bar
= find_buttonbar (h
);
3367 widget_set_size (WIDGET (dview
), 0, 0, LINES
- 1, COLS
);
3368 widget_set_size (WIDGET (bar
), LINES
- 1, 0, 1, COLS
);
3370 dview_compute_areas (dview
);
3373 /* --------------------------------------------------------------------------------------------- */
3376 dview_dialog_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3378 WDiff
*dview
= (WDiff
*) data
;
3379 WDialog
*h
= DIALOG (w
);
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 (data
, NULL
, MSG_ACTION
, parm
, NULL
);
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 dlg_default_callback (w
, sender
, msg
, parm
, data
);
3414 /* --------------------------------------------------------------------------------------------- */
3417 dview_get_title (const WDialog
* 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
)
3449 /* Create dialog and widgets, put them on the dialog */
3451 dlg_create (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3452 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3454 dview
= g_new0 (WDiff
, 1);
3456 widget_init (w
, 0, 0, LINES
- 1, COLS
, dview_callback
, dview_event
);
3457 widget_want_cursor (w
, FALSE
);
3459 add_widget (dview_dlg
, dview
);
3460 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3462 dview_dlg
->get_title
= dview_get_title
;
3464 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3466 /* Please note that if you add another widget,
3467 * you have to modify dview_adjust_size to
3471 dlg_run (dview_dlg
);
3473 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3474 dlg_destroy (dview_dlg
);
3476 return error
== 0 ? 1 : 0;
3479 /*** public functions ****************************************************************************/
3480 /* --------------------------------------------------------------------------------------------- */
3482 #define GET_FILE_AND_STAMP(n) \
3486 real_file##n = file##n; \
3487 if (!vfs_file_is_local (file##n)) \
3489 real_file##n = mc_getlocalcopy (file##n); \
3490 if (real_file##n != NULL) \
3493 if (mc_stat (real_file##n, &st##n) != 0) \
3500 #define UNGET_FILE(n) \
3506 if (use_copy##n > 0) \
3509 mtime = st##n.st_mtime; \
3510 if (mc_stat (real_file##n, &st##n) == 0) \
3511 changed = (mtime != st##n.st_mtime); \
3513 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3514 vfs_path_free (real_file##n); \
3520 dview_diff_cmd (const void *f0
, const void *f1
)
3523 vfs_path_t
*file0
= NULL
;
3524 vfs_path_t
*file1
= NULL
;
3525 gboolean is_dir0
= FALSE
;
3526 gboolean is_dir1
= FALSE
;
3528 switch (mc_global
.mc_run_mode
)
3532 /* run from panels */
3533 const WPanel
*panel0
= (const WPanel
*) f0
;
3534 const WPanel
*panel1
= (const WPanel
*) f1
;
3536 file0
= vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, NULL
);
3537 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3540 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3541 path_trunc (selection (panel0
)->fname
, 30));
3545 file1
= vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, NULL
);
3546 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3549 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3550 path_trunc (selection (panel1
)->fname
, 30));
3556 case MC_RUN_DIFFVIEWER
:
3558 /* run from command line */
3559 const char *p0
= (const char *) f0
;
3560 const char *p1
= (const char *) f1
;
3563 file0
= vfs_path_from_str (p0
);
3564 if (mc_stat (file0
, &st
) == 0)
3566 is_dir0
= S_ISDIR (st
.st_mode
);
3569 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3575 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3576 path_trunc (p0
, 30), unix_error_string (errno
));
3580 file1
= vfs_path_from_str (p1
);
3581 if (mc_stat (file1
, &st
) == 0)
3583 is_dir1
= S_ISDIR (st
.st_mode
);
3586 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3592 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3593 path_trunc (p1
, 30), unix_error_string (errno
));
3600 /* this should not happaned */
3601 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3608 if (file0
!= NULL
&& file1
!= NULL
)
3614 vfs_path_t
*real_file0
;
3615 vfs_path_t
*real_file1
;
3617 GET_FILE_AND_STAMP (0);
3618 GET_FILE_AND_STAMP (1);
3620 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3621 rv
= diff_view (vfs_path_as_str (real_file0
), vfs_path_as_str (real_file1
),
3622 vfs_path_as_str (file0
), vfs_path_as_str (file1
));
3630 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3633 vfs_path_free (file1
);
3634 vfs_path_free (file0
);
3639 #undef GET_FILE_AND_STAMP
3642 /* --------------------------------------------------------------------------------------------- */