2 Copyright (C) 2007, 2010, 2011, 2012
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, 2012
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)
89 #define HDIFF_ENABLE 1
90 #define HDIFF_MINCTX 5
91 #define HDIFF_DEPTH 10
93 #define FILE_DIRTY(fs) \
101 /*** file scope type declarations ****************************************************************/
107 } action_direction_t
;
109 /*** file scope variables ************************************************************************/
111 /*** file scope functions ************************************************************************/
112 /* --------------------------------------------------------------------------------------------- */
115 TAB_SKIP (int ts
, int pos
)
117 if (ts
> 0 && ts
< 9)
118 return ts
- pos
% ts
;
123 /* --------------------------------------------------------------------------------------------- */
126 rewrite_backup_content (const vfs_path_t
* from_file_name_vpath
, const char *to_file_name
)
131 const char *from_file_name
;
133 from_file_name
= vfs_path_get_by_index (from_file_name_vpath
, -1)->path
;
134 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
137 backup_fd
= fopen (to_file_name
, "w");
138 if (backup_fd
== NULL
)
144 length
= fwrite ((const void *) contents
, length
, 1, backup_fd
);
152 /* buffered I/O ************************************************************* */
155 * Try to open a temporary file.
156 * @note the name is not altered if this function fails
158 * @param[out] name address of a pointer to store the temporary name
159 * @return file descriptor on success, negative on error
163 open_temp (void **name
)
166 vfs_path_t
*diff_file_name
= NULL
;
168 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
171 message (D_ERROR
, MSG_ERROR
,
172 _("Cannot create temporary diff file\n%s"), unix_error_string (errno
));
175 *name
= vfs_path_to_str (diff_file_name
);
176 vfs_path_free (diff_file_name
);
180 /* --------------------------------------------------------------------------------------------- */
183 * Alocate file structure and associate file descriptor to it.
185 * @param fd file descriptor
186 * @return file structure
197 fs
= g_try_malloc (sizeof (FBUF
));
201 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
216 /* --------------------------------------------------------------------------------------------- */
219 * Free file structure without closing the file.
221 * @param fs file structure
222 * @return 0 on success, non-zero on error
230 if (fs
->flags
& FILE_FLAG_TEMP
)
232 rv
= unlink (fs
->data
);
240 /* --------------------------------------------------------------------------------------------- */
243 * Open a binary temporary file in R/W mode.
244 * @note the file will be deleted when closed
246 * @return file structure
258 fd
= open_temp (&fs
->data
);
266 fs
->flags
= FILE_FLAG_TEMP
;
270 /* --------------------------------------------------------------------------------------------- */
273 * Open a binary file in specified mode.
275 * @param filename file name
276 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
278 * @return file structure
282 f_open (const char *filename
, int flags
)
291 fd
= open (filename
, flags
);
302 /* --------------------------------------------------------------------------------------------- */
305 * Read a line of bytes from file until newline or EOF.
306 * @note does not stop on null-byte
307 * @note buf will not be null-terminated
309 * @param buf destination buffer
310 * @param size size of buffer
311 * @param fs file structure
313 * @return number of bytes read
317 f_gets (char *buf
, size_t size
, FBUF
* fs
)
326 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
334 if (j
== size
|| stop
)
338 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
345 /* --------------------------------------------------------------------------------------------- */
349 * @note avoids thrashing read cache when possible
351 * @param fs file structure
353 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
355 * @return position in file, starting from begginning
359 f_seek (FBUF
* fs
, off_t off
, int whence
)
363 if (fs
->len
&& whence
!= SEEK_END
)
365 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
368 if (whence
== SEEK_CUR
)
371 off
+= rv
- fs
->len
+ fs
->pos
;
373 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
375 fs
->pos
= fs
->len
+ off
- rv
;
381 rv
= lseek (fs
->fd
, off
, whence
);
387 /* --------------------------------------------------------------------------------------------- */
390 * Seek to the beginning of file, thrashing read cache.
392 * @param fs file structure
394 * @return 0 if success, non-zero on error
402 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
408 /* --------------------------------------------------------------------------------------------- */
411 * Write bytes to file.
412 * @note thrashes read cache
414 * @param fs file structure
415 * @param buf source buffer
416 * @param size size of buffer
418 * @return number of written bytes, -1 on error
422 f_write (FBUF
* fs
, const char *buf
, size_t size
)
426 rv
= write (fs
->fd
, buf
, size
);
432 /* --------------------------------------------------------------------------------------------- */
435 * Truncate file to the current position.
436 * @note thrashes read cache
438 * @param fs file structure
440 * @return current file size on success, negative on error
448 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
453 rv
= ftruncate (fs
->fd
, off
);
462 /* --------------------------------------------------------------------------------------------- */
466 * @note if this is temporary file, it is deleted
468 * @param fs file structure
469 * @return 0 on success, non-zero on error
486 /* --------------------------------------------------------------------------------------------- */
489 * Create pipe stream to process.
491 * @param cmd shell command line
492 * @param flags open mode, either O_RDONLY or O_WRONLY
494 * @return file structure
498 p_open (const char *cmd
, int flags
)
502 const char *type
= NULL
;
504 if (flags
== O_RDONLY
)
506 else if (flags
== O_WRONLY
)
516 f
= popen (cmd
, type
);
528 /* --------------------------------------------------------------------------------------------- */
533 * @param fs structure
534 * @return 0 on success, non-zero on error
544 rv
= pclose (fs
->data
);
551 /* --------------------------------------------------------------------------------------------- */
554 * Get one char (byte) from string
556 * @param char * str, gboolean * result
557 * @return int as character or 0 and result == FALSE if fail
561 dview_get_byte (char *str
, gboolean
* result
)
569 return (unsigned char) *str
;
572 /* --------------------------------------------------------------------------------------------- */
575 * Get utf multibyte char from string
577 * @param char * str, int * char_width, gboolean * result
578 * @return int as utf character or 0 and result == FALSE if fail
582 dview_get_utf (char *str
, int *char_width
, gboolean
* result
)
586 gchar
*next_ch
= NULL
;
597 res
= g_utf8_get_char_validated (str
, -1);
604 /* Calculate UTF-8 char width */
605 next_ch
= g_utf8_next_char (str
);
607 width
= next_ch
- str
;
615 /* --------------------------------------------------------------------------------------------- */
618 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
622 if (text
== NULL
|| text
[0] == '\0')
625 if (g_utf8_validate (text
, -1, NULL
))
626 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
630 char *tmpbuf
, *buffer
;
632 buffer
= tmpbuf
= g_strdup (text
);
633 while (tmpbuf
[0] != '\0')
635 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
636 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
637 tmpbuf
= g_utf8_next_char (tmpbuf
);
644 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
647 return max (length
, (size_t) result
);
650 /* --------------------------------------------------------------------------------------------- */
652 /* diff parse *************************************************************** */
655 * Read decimal number from string.
657 * @param[in,out] str string to parse
658 * @param[out] n extracted number
659 * @return 0 if success, otherwise non-zero
663 scan_deci (const char **str
, int *n
)
665 const char *p
= *str
;
669 *n
= strtol (p
, &q
, 10);
670 if (errno
!= 0 || p
== q
)
676 /* --------------------------------------------------------------------------------------------- */
679 * Parse line for diff statement.
681 * @param p string to parse
682 * @param ops list of diff statements
683 * @return 0 if success, otherwise non-zero
687 scan_line (const char *p
, GArray
* ops
)
696 /* handle the following cases:
698 * NUM[,NUM]cNUM[,NUM]
700 * where NUM is a positive integer
703 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
711 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
723 else if (cmd
!= 'c' && cmd
!= 'd')
726 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
734 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
740 if (cmd
== 'd' && range
!= 0)
748 g_array_append_val (ops
, op
);
752 /* --------------------------------------------------------------------------------------------- */
755 * Parse diff output and extract diff statements.
757 * @param f stream to read from
758 * @param ops list of diff statements to fill
759 * @return positive number indicating number of hunks, otherwise negative
763 scan_diff (FBUF
* f
, GArray
* ops
)
768 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
770 if (isdigit (buf
[0]))
772 if (buf
[sz
- 1] != '\n')
776 if (scan_line (buf
, ops
) != 0)
782 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
789 /* --------------------------------------------------------------------------------------------- */
792 * Invoke diff and extract diff statements.
794 * @param args extra arguments to be passed to diff
795 * @param extra more arguments to be passed to diff
796 * @param file1 first file to compare
797 * @param file2 second file to compare
798 * @param ops list of diff statements to fill
800 * @return positive number indicating number of hunks, otherwise negative
804 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
807 static const char *opt
=
808 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
809 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
810 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
811 " --unchanged-group-format=''";
817 char *file1_esc
, *file2_esc
;
819 /* escape potential $ to avoid shell variable substitutions in popen() */
820 file1_esc
= strutils_shell_escape (file1
);
821 file2_esc
= strutils_shell_escape (file2
);
822 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
829 f
= p_open (cmd
, O_RDONLY
);
835 rv
= scan_diff (f
, ops
);
838 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
844 /* --------------------------------------------------------------------------------------------- */
847 * Reparse and display file according to diff statements.
849 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
850 * @param filename file name to display
851 * @param ops list of diff statements
852 * @param printer printf-like function to be used for displaying
853 * @param ctx printer context
855 * @return 0 if success, otherwise non-zero
859 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
872 f
= f_open (filename
, O_RDONLY
);
888 #define T1 a[ ord^1 ][0]
889 #define T2 a[ ord^1 ][1]
890 for (i
= 0; i
< ops
->len
; i
++)
894 op
= &g_array_index (ops
, DIFFCMD
, i
);
895 n
= op
->F1
- (op
->cmd
!= add_cmd
);
897 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
900 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
902 while (buf
[sz
- 1] != '\n')
904 sz
= f_gets (buf
, sizeof (buf
), f
);
907 printer (ctx
, 0, 0, 0, 1, "\n");
910 printer (ctx
, 0, 0, 0, sz
, buf
);
918 if (op
->cmd
== add_cmd
)
920 n
= op
->T2
- op
->T1
+ 1;
923 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
928 if (op
->cmd
== del_cmd
)
930 n
= op
->F2
- op
->F1
+ 1;
931 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
934 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
936 while (buf
[sz
- 1] != '\n')
938 sz
= f_gets (buf
, sizeof (buf
), f
);
941 printer (ctx
, 0, 0, 0, 1, "\n");
944 printer (ctx
, 0, 0, 0, sz
, buf
);
956 n
= op
->F2
- op
->F1
+ 1;
957 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
960 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
962 while (buf
[sz
- 1] != '\n')
964 sz
= f_gets (buf
, sizeof (buf
), f
);
967 printer (ctx
, 0, 0, 0, 1, "\n");
970 printer (ctx
, 0, 0, 0, sz
, buf
);
979 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
982 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
992 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
995 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
997 while (buf
[sz
- 1] != '\n')
999 sz
= f_gets (buf
, sizeof (buf
), f
);
1002 printer (ctx
, 0, 0, 0, 1, "\n");
1005 printer (ctx
, 0, 0, 0, sz
, buf
);
1018 /* --------------------------------------------------------------------------------------------- */
1020 /* horizontal diff ********************************************************** */
1023 * Longest common substring.
1025 * @param s first string
1026 * @param m length of first string
1027 * @param t second string
1028 * @param n length of second string
1029 * @param ret list of offsets for longest common substrings inside each string
1030 * @param min minimum length of common substrings
1032 * @return 0 if success, nonzero otherwise
1036 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1042 if (m
< min
|| n
< min
)
1044 /* XXX early culling */
1048 Lprev
= g_try_new0 (int, n
+ 1);
1052 Lcurr
= g_try_new0 (int, n
+ 1);
1059 for (i
= 0; i
< m
; i
++)
1066 #ifdef USE_MEMSET_IN_LCS
1067 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1069 for (j
= 0; j
< n
; j
++)
1071 #ifndef USE_MEMSET_IN_LCS
1083 g_array_set_size (ret
, 0);
1085 if (z
== v
&& z
>= min
)
1093 for (k
= 0; k
< ret
->len
; k
++)
1095 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1096 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1105 g_array_append_val (ret
, p2
);
1117 /* --------------------------------------------------------------------------------------------- */
1120 * Scan recursively for common substrings and build ranges.
1122 * @param s first string
1123 * @param t second string
1124 * @param bracket current limits for both of the strings
1125 * @param min minimum length of common substrings
1126 * @param hdiff list of horizontal diff ranges to fill
1127 * @param depth recursion depth
1129 * @return 0 if success, nonzero otherwise
1133 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1144 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1148 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1149 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1153 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1156 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1157 b
[DIFF_LEFT
].len
= (*data
)[0];
1158 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1159 b
[DIFF_RIGHT
].len
= (*data
)[1];
1160 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1163 for (k
= 0; k
< ret
->len
- 1; k
++)
1165 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1166 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1167 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1168 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1169 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1170 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1171 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1174 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1175 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1176 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1177 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1178 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1179 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1182 g_array_free (ret
, TRUE
);
1187 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1188 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1189 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1190 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1191 g_array_append_val (hdiff
, p
);
1196 /* --------------------------------------------------------------------------------------------- */
1199 * Build list of horizontal diff ranges.
1201 * @param s first string
1202 * @param m length of first string
1203 * @param t second string
1204 * @param n length of second string
1205 * @param min minimum length of common substrings
1206 * @param hdiff list of horizontal diff ranges to fill
1207 * @param depth recursion depth
1209 * @return 0 if success, nonzero otherwise
1213 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1218 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1219 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1221 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1224 b
[DIFF_LEFT
].off
= i
;
1225 b
[DIFF_LEFT
].len
= m
- i
;
1226 b
[DIFF_RIGHT
].off
= i
;
1227 b
[DIFF_RIGHT
].len
= n
- i
;
1229 /* smartscan (multiple horizontal diff) */
1230 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1233 /* --------------------------------------------------------------------------------------------- */
1235 /* read line **************************************************************** */
1238 * Check if character is inside horizontal diff limits.
1240 * @param k rank of character inside line
1241 * @param hdiff horizontal diff structure
1242 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1244 * @return TRUE if inside hdiff limits, FALSE otherwise
1248 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1253 for (i
= 0; i
< hdiff
->len
; i
++)
1257 b
= &g_array_index (hdiff
, BRACKET
, i
);
1258 start
= (*b
)[ord
].off
;
1259 end
= start
+ (*b
)[ord
].len
;
1260 if (k
>= start
&& k
< end
)
1266 /* --------------------------------------------------------------------------------------------- */
1269 * Copy 'src' to 'dst' expanding tabs.
1270 * @note The procedure returns when all bytes are consumed from 'src'
1272 * @param dst destination buffer
1273 * @param src source buffer
1274 * @param srcsize size of src buffer
1275 * @param base virtual base of this string, needed to calculate tabs
1276 * @param ts tab size
1278 * @return new virtual base
1282 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1286 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1293 j
= TAB_SKIP (ts
, i
+ base
);
1303 /* --------------------------------------------------------------------------------------------- */
1306 * Copy 'src' to 'dst' expanding tabs.
1308 * @param dst destination buffer
1309 * @param dstsize size of dst buffer
1310 * @param[in,out] _src source buffer
1311 * @param srcsize size of src buffer
1312 * @param base virtual base of this string, needed to calculate tabs
1313 * @param ts tab size
1315 * @return new virtual base
1317 * @note The procedure returns when all bytes are consumed from 'src'
1318 * or 'dstsize' bytes are written to 'dst'
1319 * @note Upon return, 'src' points to the first unwritten character in source
1323 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1326 const char *src
= *_src
;
1328 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1335 j
= TAB_SKIP (ts
, i
+ base
);
1336 if (j
> dstsize
- i
)
1348 /* --------------------------------------------------------------------------------------------- */
1351 * Read line from memory, converting tabs to spaces and padding with spaces.
1353 * @param src buffer to read from
1354 * @param srcsize size of src buffer
1355 * @param dst buffer to read to
1356 * @param dstsize size of dst buffer, excluding trailing null
1357 * @param skip number of characters to skip
1358 * @param ts tab size
1359 * @param show_cr show trailing carriage return as ^M
1361 * @return negative on error, otherwise number of bytes except padding
1365 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1375 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1381 j
= TAB_SKIP (ts
, i
+ base
);
1387 else if (dstsize
!= 0)
1394 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1396 if (skip
== 0 && show_cr
)
1419 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1422 if (!g_unichar_isprint (utf_ch
))
1433 while (dstsize
!= 0)
1442 /* --------------------------------------------------------------------------------------------- */
1445 * Read line from memory and build attribute array.
1447 * @param src buffer to read from
1448 * @param srcsize size of src buffer
1449 * @param dst buffer to read to
1450 * @param dstsize size of dst buffer, excluding trailing null
1451 * @param skip number of characters to skip
1452 * @param ts tab size
1453 * @param show_cr show trailing carriage return as ^M
1454 * @param hdiff horizontal diff structure
1455 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1456 * @param att buffer of attributes
1458 * @return negative on error, otherwise number of bytes except padding
1462 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1463 GArray
* hdiff
, diff_place_t ord
, char *att
)
1473 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1479 j
= TAB_SKIP (ts
, i
+ base
);
1485 else if (dstsize
!= 0)
1488 *att
++ = is_inside (k
, hdiff
, ord
);
1493 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1495 if (skip
== 0 && show_cr
)
1500 *att
++ = is_inside (k
, hdiff
, ord
);
1502 *att
++ = is_inside (k
, hdiff
, ord
);
1508 *att
++ = is_inside (k
, hdiff
, ord
);
1521 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1524 if (!g_unichar_isprint (utf_ch
))
1530 *att
++ = is_inside (k
, hdiff
, ord
);
1536 while (dstsize
!= 0)
1546 /* --------------------------------------------------------------------------------------------- */
1549 * Read line from file, converting tabs to spaces and padding with spaces.
1551 * @param f file stream to read from
1552 * @param off offset of line inside file
1553 * @param dst buffer to read to
1554 * @param dstsize size of dst buffer, excluding trailing null
1555 * @param skip number of characters to skip
1556 * @param ts tab size
1557 * @param show_cr show trailing carriage return as ^M
1559 * @return negative on error, otherwise number of bytes except padding
1563 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1566 int old_base
= base
;
1567 size_t amount
= dstsize
;
1568 size_t useful
, offset
;
1572 const char *q
= NULL
;
1573 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1574 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1576 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1578 /* abnormal, but avoid buffer overflow */
1579 memset (dst
, ' ', dstsize
);
1580 dst
[dstsize
] = '\0';
1584 f_seek (f
, off
, SEEK_SET
);
1589 sz
= f_gets (tmp
, amount
, f
);
1593 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1594 if (cvt
[base
- old_base
- 1] == '\n')
1596 q
= &cvt
[base
- old_base
- 1];
1597 base
= old_base
+ q
- cvt
+ 1;
1604 memset (dst
, ' ', dstsize
);
1605 dst
[dstsize
] = '\0';
1609 useful
= base
- skip
;
1610 offset
= skip
- old_base
;
1612 if (useful
<= dstsize
)
1615 memmove (dst
, cvt
+ offset
, useful
);
1619 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1622 const char *ptr
= tmp
;
1624 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1633 memmove (dst
, cvt
+ offset
, dstsize
);
1635 lastch
= cvt
[offset
+ dstsize
];
1639 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1641 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1645 if (i
+ 1 < dstsize
)
1659 for (; i
< dstsize
; i
++)
1665 /* --------------------------------------------------------------------------------------------- */
1666 /* diff printers et al ****************************************************** */
1669 cc_free_elt (void *elt
)
1677 /* --------------------------------------------------------------------------------------------- */
1680 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1682 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1683 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1693 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1695 if (sz
!= 0 && str
[sz
- 1] == '\n')
1698 p
.p
= g_strndup (str
, sz
);
1701 g_array_append_val (a
, p
);
1703 else if (dsrc
== DATA_SRC_MEM
)
1707 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1708 if (sz
!= 0 && str
[sz
- 1] == '\n')
1715 new_size
= p
->u
.len
+ sz
;
1716 q
= g_realloc (p
->p
, new_size
);
1717 memcpy (q
+ p
->u
.len
, str
, sz
);
1722 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1724 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1725 f_write (f
, str
, sz
);
1730 /* --------------------------------------------------------------------------------------------- */
1733 redo_diff (WDiff
* dview
)
1735 FBUF
*const *f
= dview
->f
;
1743 if (dview
->opt
.quality
== 2)
1744 strcat (extra
, " -d");
1745 if (dview
->opt
.quality
== 1)
1746 strcat (extra
, " --speed-large-files");
1747 if (dview
->opt
.strip_trailing_cr
)
1748 strcat (extra
, " --strip-trailing-cr");
1749 if (dview
->opt
.ignore_tab_expansion
)
1750 strcat (extra
, " -E");
1751 if (dview
->opt
.ignore_space_change
)
1752 strcat (extra
, " -b");
1753 if (dview
->opt
.ignore_all_space
)
1754 strcat (extra
, " -w");
1755 if (dview
->opt
.ignore_case
)
1756 strcat (extra
, " -i");
1758 if (dview
->dsrc
!= DATA_SRC_MEM
)
1760 f_reset (f
[DIFF_LEFT
]);
1761 f_reset (f
[DIFF_RIGHT
]);
1764 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1765 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1769 g_array_free (ops
, TRUE
);
1773 ctx
.dsrc
= dview
->dsrc
;
1776 ctx
.a
= dview
->a
[DIFF_LEFT
];
1777 ctx
.f
= f
[DIFF_LEFT
];
1778 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1780 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1781 ctx
.f
= f
[DIFF_RIGHT
];
1782 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1785 g_array_free (ops
, TRUE
);
1787 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1790 if (dview
->dsrc
== DATA_SRC_TMP
)
1792 f_trunc (f
[DIFF_LEFT
]);
1793 f_trunc (f
[DIFF_RIGHT
]);
1796 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1798 dview
->hdiff
= g_ptr_array_new ();
1799 if (dview
->hdiff
!= NULL
)
1805 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1809 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1810 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1811 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1813 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1819 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1823 g_array_free (h
, TRUE
);
1828 g_ptr_array_add (dview
->hdiff
, h
);
1835 /* --------------------------------------------------------------------------------------------- */
1838 destroy_hdiff (WDiff
* dview
)
1840 if (dview
->hdiff
!= NULL
)
1845 len
= dview
->a
[DIFF_LEFT
]->len
;
1847 for (i
= 0; i
< len
; i
++)
1851 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1853 g_array_free (h
, TRUE
);
1855 g_ptr_array_free (dview
->hdiff
, TRUE
);
1856 dview
->hdiff
= NULL
;
1859 mc_search_free (dview
->search
.handle
);
1860 dview
->search
.handle
= NULL
;
1861 g_free (dview
->search
.last_string
);
1862 dview
->search
.last_string
= NULL
;
1865 /* --------------------------------------------------------------------------------------------- */
1866 /* stuff ******************************************************************** */
1869 get_digits (unsigned int n
)
1878 /* --------------------------------------------------------------------------------------------- */
1881 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1893 p
= &g_array_index (a
, DIFFLN
, pos
);
1899 for (n
= pos
; n
> 0; n
--)
1905 *lineofs
= pos
- n
+ 1;
1913 /* --------------------------------------------------------------------------------------------- */
1916 calc_nwidth (const GArray
** const a
)
1921 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1922 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1925 return get_digits (l1
);
1928 /* --------------------------------------------------------------------------------------------- */
1931 find_prev_hunk (const GArray
* a
, int pos
)
1934 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1936 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1938 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1940 if (pos
> 0 && (size_t) pos
< a
->len
)
1943 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1945 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1952 /* --------------------------------------------------------------------------------------------- */
1955 find_next_hunk (const GArray
* a
, size_t pos
)
1957 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1959 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1964 /* --------------------------------------------------------------------------------------------- */
1966 * Find start and end lines of the current hunk.
1968 * @param dview WDiff widget
1969 * @return boolean and
1970 * start_line1 first line of current hunk (file[0])
1971 * end_line1 last line of current hunk (file[0])
1972 * start_line1 first line of current hunk (file[0])
1973 * end_line1 last line of current hunk (file[0])
1977 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1979 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1980 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1990 pos
= dview
->skip_rows
;
1991 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2006 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2010 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2011 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2013 pos
= dview
->skip_rows
;
2014 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2018 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2019 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2021 *end_line1
= max (*start_line1
, l0
);
2023 *end_line2
= max (*start_line2
, l1
);
2030 /* --------------------------------------------------------------------------------------------- */
2032 * Remove hunk from file.
2034 * @param dview WDiff widget
2035 * @param merge_file file stream for writing data
2036 * @param from1 first line of hunk
2037 * @param to1 last line of hunk
2038 * @param merge_direction in what direction files should be merged
2042 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2043 action_direction_t merge_direction
)
2049 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2050 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2052 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2055 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2058 fputs (buf
, merge_file
);
2060 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2064 fputs (buf
, merge_file
);
2069 /* --------------------------------------------------------------------------------------------- */
2073 * @param dview WDiff widget
2074 * @param merge_file file stream for writing data
2075 * @param from1 first line of source hunk
2076 * @param from2 first line of destination hunk
2077 * @param to1 last line of source hunk
2078 * @param merge_direction in what direction files should be merged
2082 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2083 action_direction_t merge_direction
)
2090 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2092 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2093 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2097 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2098 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2102 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2105 fputs (buf
, merge_file
);
2108 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2112 fputs (buf
, merge_file
);
2114 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2115 fputs (buf
, merge_file
);
2121 /* --------------------------------------------------------------------------------------------- */
2123 * Replace hunk in file.
2125 * @param dview WDiff widget
2126 * @param merge_file file stream for writing data
2127 * @param from1 first line of source hunk
2128 * @param to1 last line of source hunk
2129 * @param from2 first line of destination hunk
2130 * @param to2 last line of destination hunk
2131 * @param merge_direction in what direction files should be merged
2135 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2136 action_direction_t merge_direction
)
2138 int line1
= 0, line2
= 0;
2143 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2145 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2146 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2150 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2151 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2154 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2157 fputs (buf
, merge_file
);
2159 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2163 fputs (buf
, merge_file
);
2165 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2169 fputs (buf
, merge_file
);
2175 /* --------------------------------------------------------------------------------------------- */
2179 * @param dview WDiff widget
2180 * @param merge_direction in what direction files should be merged
2184 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2186 int from1
, to1
, from2
, to2
;
2188 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2190 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2191 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2193 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2199 vfs_path_t
*merge_file_name_vpath
= NULL
;
2201 if (!dview
->merged
[n_merge
])
2203 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2204 if (!dview
->merged
[n_merge
])
2206 message (D_ERROR
, MSG_ERROR
,
2207 _("Cannot create backup file\n%s%s\n%s"),
2208 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2213 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2214 if (merge_file_fd
== -1)
2216 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2217 unix_error_string (errno
));
2221 merge_file
= fdopen (merge_file_fd
, "w");
2226 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2227 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2229 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2232 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2233 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2235 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2238 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2241 fflush (merge_file
);
2242 fclose (merge_file
);
2246 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2249 mc_unlink (merge_file_name_vpath
);
2250 vfs_path_free (merge_file_name_vpath
);
2254 /* --------------------------------------------------------------------------------------------- */
2255 /* view routines and callbacks ********************************************** */
2258 dview_compute_split (WDiff
* dview
, int i
)
2261 if (dview
->bias
< 2 - dview
->half1
)
2262 dview
->bias
= 2 - dview
->half1
;
2263 if (dview
->bias
> dview
->half2
- 2)
2264 dview
->bias
= dview
->half2
- 2;
2267 /* --------------------------------------------------------------------------------------------- */
2270 dview_compute_areas (WDiff
* dview
)
2272 dview
->height
= LINES
- 2;
2273 dview
->half1
= COLS
/ 2;
2274 dview
->half2
= COLS
- dview
->half1
;
2276 dview_compute_split (dview
, 0);
2279 /* --------------------------------------------------------------------------------------------- */
2282 dview_reread (WDiff
* dview
)
2286 destroy_hdiff (dview
);
2287 if (dview
->a
[DIFF_LEFT
] != NULL
)
2289 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2290 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2292 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2294 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2295 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2298 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2299 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2301 ndiff
= redo_diff (dview
);
2303 dview
->ndiff
= ndiff
;
2306 /* --------------------------------------------------------------------------------------------- */
2310 dview_set_codeset (WDiff
* dview
)
2312 const char *encoding_id
= NULL
;
2316 get_codepage_id (mc_global
.source_codepage
>=
2317 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2318 if (encoding_id
!= NULL
)
2322 conv
= str_crt_conv_from (encoding_id
);
2323 if (conv
!= INVALID_CONV
)
2325 if (dview
->converter
!= str_cnv_from_term
)
2326 str_close_conv (dview
->converter
);
2327 dview
->converter
= conv
;
2329 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
2333 /* --------------------------------------------------------------------------------------------- */
2336 dview_select_encoding (WDiff
* dview
)
2338 if (do_select_codepage ())
2339 dview_set_codeset (dview
);
2340 dview_reread (dview
);
2341 tty_touch_screen ();
2344 #endif /* HAVE_CHARSET */
2346 /* --------------------------------------------------------------------------------------------- */
2349 dview_diff_options (WDiff
* dview
)
2351 const char *quality_str
[] = {
2353 N_("&Fastest (Assume large files)"),
2354 N_("&Minimal (Find a smaller set of change)")
2357 quick_widget_t quick_widgets
[] = {
2359 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2360 QUICK_RADIO (3, (const char **) quality_str
, (int *) &dview
->opt
.quality
, NULL
),
2361 QUICK_STOP_GROUPBOX
,
2362 QUICK_START_GROUPBOX (N_("Diff extra options")),
2363 QUICK_CHECKBOX (N_("&Ignore case"), &dview
->opt
.ignore_case
, NULL
),
2364 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
, NULL
),
2365 QUICK_CHECKBOX (N_("Ignore &space change"), &dview
->opt
.ignore_space_change
, NULL
),
2366 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
, NULL
),
2367 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
,
2369 QUICK_STOP_GROUPBOX
,
2370 QUICK_BUTTONS_OK_CANCEL
,
2375 quick_dialog_t qdlg
= {
2377 N_("Diff Options"), "[Diff Options]",
2378 quick_widgets
, NULL
, NULL
2381 if (quick_dialog (&qdlg
) != B_CANCEL
)
2382 dview_reread (dview
);
2385 /* --------------------------------------------------------------------------------------------- */
2388 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2389 const char *label1
, const char *label2
, DSRC dsrc
)
2392 FBUF
*f
[DIFF_COUNT
];
2394 f
[DIFF_LEFT
] = NULL
;
2395 f
[DIFF_RIGHT
] = NULL
;
2397 if (dsrc
== DATA_SRC_TMP
)
2399 f
[DIFF_LEFT
] = f_temp ();
2400 if (f
[DIFF_LEFT
] == NULL
)
2403 f
[DIFF_RIGHT
] = f_temp ();
2404 if (f
[DIFF_RIGHT
] == NULL
)
2406 f_close (f
[DIFF_LEFT
]);
2410 else if (dsrc
== DATA_SRC_ORG
)
2412 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2413 if (f
[DIFF_LEFT
] == NULL
)
2416 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2417 if (f
[DIFF_RIGHT
] == NULL
)
2419 f_close (f
[DIFF_LEFT
]);
2425 dview
->file
[DIFF_LEFT
] = file1
;
2426 dview
->file
[DIFF_RIGHT
] = file2
;
2427 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2428 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2429 dview
->f
[DIFF_LEFT
] = f
[0];
2430 dview
->f
[DIFF_RIGHT
] = f
[1];
2431 dview
->merged
[DIFF_LEFT
] = FALSE
;
2432 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2433 dview
->hdiff
= NULL
;
2435 dview
->converter
= str_cnv_from_term
;
2437 dview_set_codeset (dview
);
2439 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2440 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2442 ndiff
= redo_diff (dview
);
2445 /* goto MSG_DESTROY stage: dview_fini() */
2446 f_close (f
[DIFF_LEFT
]);
2447 f_close (f
[DIFF_RIGHT
]);
2451 dview
->ndiff
= ndiff
;
2453 dview
->view_quit
= 0;
2456 dview
->new_frame
= 1;
2457 dview
->skip_rows
= 0;
2458 dview
->skip_cols
= 0;
2459 dview
->display_symbols
= 0;
2460 dview
->display_numbers
= 0;
2462 dview
->tab_size
= 8;
2463 dview
->ord
= DIFF_LEFT
;
2466 dview
->search
.handle
= NULL
;
2467 dview
->search
.last_string
= NULL
;
2468 dview
->search
.last_found_line
= -1;
2469 dview
->search
.last_accessed_num_line
= -1;
2471 dview
->opt
.quality
= 0;
2472 dview
->opt
.strip_trailing_cr
= 0;
2473 dview
->opt
.ignore_tab_expansion
= 0;
2474 dview
->opt
.ignore_space_change
= 0;
2475 dview
->opt
.ignore_all_space
= 0;
2476 dview
->opt
.ignore_case
= 0;
2478 dview_compute_areas (dview
);
2483 /* --------------------------------------------------------------------------------------------- */
2486 dview_fini (WDiff
* dview
)
2488 if (dview
->dsrc
!= DATA_SRC_MEM
)
2490 f_close (dview
->f
[DIFF_RIGHT
]);
2491 f_close (dview
->f
[DIFF_LEFT
]);
2494 if (dview
->converter
!= str_cnv_from_term
)
2495 str_close_conv (dview
->converter
);
2497 destroy_hdiff (dview
);
2498 if (dview
->a
[DIFF_LEFT
] != NULL
)
2500 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2501 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2502 dview
->a
[DIFF_LEFT
] = NULL
;
2504 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2506 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2507 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2508 dview
->a
[DIFF_RIGHT
] = NULL
;
2511 g_free (dview
->label
[DIFF_LEFT
]);
2512 g_free (dview
->label
[DIFF_RIGHT
]);
2515 /* --------------------------------------------------------------------------------------------- */
2518 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2523 FBUF
*f
= dview
->f
[ord
];
2524 int skip
= dview
->skip_cols
;
2525 int display_symbols
= dview
->display_symbols
;
2526 int display_numbers
= dview
->display_numbers
;
2527 int show_cr
= dview
->show_cr
;
2530 int nwidth
= display_numbers
;
2533 xwidth
= display_symbols
+ display_numbers
;
2534 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2535 tab_size
= dview
->tab_size
;
2539 if (xwidth
> width
&& display_symbols
)
2542 display_symbols
= 0;
2544 if (xwidth
> width
&& display_numbers
)
2547 display_numbers
= width
;
2557 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2559 /* abnormal, but avoid buffer overflow */
2563 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2565 int ch
, next_ch
, col
;
2568 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2570 tty_setcolor (NORMAL_COLOR
);
2571 if (display_symbols
)
2573 tty_gotoyx (r
+ j
, c
- 2);
2574 tty_print_char (ch
);
2578 if (display_numbers
)
2580 tty_gotoyx (r
+ j
, c
- xwidth
);
2581 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2582 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2585 tty_setcolor (DFF_ADD_COLOR
);
2587 tty_setcolor (DFF_CHG_COLOR
);
2590 if (i
== (size_t) dview
->search
.last_found_line
)
2591 tty_setcolor (MARKED_SELECTED_COLOR
);
2592 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2597 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2601 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2602 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2603 tty_gotoyx (r
+ j
, c
);
2606 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2613 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2616 if (!g_unichar_isprint (next_ch
))
2620 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2624 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2626 if (mc_global
.utf8_display
)
2631 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2635 else if (dview
->utf8
)
2636 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2638 next_ch
= convert_to_display_c (next_ch
);
2640 tty_print_anychar (next_ch
);
2648 tty_setcolor (DFF_CHH_COLOR
);
2651 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2654 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2657 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2661 if (display_numbers
)
2663 tty_gotoyx (r
+ j
, c
- xwidth
);
2664 memset (buf
, ' ', display_numbers
);
2665 buf
[display_numbers
] = '\0';
2666 tty_print_string (buf
);
2669 tty_setcolor (DFF_DEL_COLOR
);
2671 tty_setcolor (DFF_CHD_COLOR
);
2672 memset (buf
, ' ', width
);
2675 tty_gotoyx (r
+ j
, c
);
2676 /* tty_print_nstring (buf, width); */
2678 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2685 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2688 if (!g_unichar_isprint (next_ch
))
2692 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2696 if (mc_global
.utf8_display
)
2701 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2704 else if (dview
->utf8
)
2705 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2707 next_ch
= convert_to_display_c (next_ch
);
2710 tty_print_anychar (next_ch
);
2715 tty_setcolor (NORMAL_COLOR
);
2717 if (width
< xwidth
- 1)
2719 memset (buf
, ' ', k
);
2721 for (; j
< height
; j
++)
2725 tty_gotoyx (r
+ j
, c
- xwidth
);
2726 /* tty_print_nstring (buf, xwidth - 1); */
2727 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2729 tty_gotoyx (r
+ j
, c
);
2730 /* tty_print_nstring (buf, width); */
2731 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2737 /* --------------------------------------------------------------------------------------------- */
2740 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2744 int linenum
, lineofs
;
2748 tty_setcolor (STATUSBAR_COLOR
);
2751 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2753 filename_width
= width
- 24;
2754 if (filename_width
< 8)
2757 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2758 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2759 vfs_path_free (vpath
);
2760 buf
= str_term_trim (path
, filename_width
);
2761 if (ord
== DIFF_LEFT
)
2762 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2763 buf
, linenum
, lineofs
, dview
->skip_cols
);
2765 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2766 buf
, linenum
, lineofs
, dview
->ndiff
);
2770 /* --------------------------------------------------------------------------------------------- */
2773 dview_redo (WDiff
* dview
)
2775 if (dview
->display_numbers
)
2779 old
= dview
->display_numbers
;
2780 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2781 dview
->new_frame
= (old
!= dview
->display_numbers
);
2783 dview_reread (dview
);
2786 /* --------------------------------------------------------------------------------------------- */
2789 dview_update (WDiff
* dview
)
2791 int height
= dview
->height
;
2796 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2798 if (dview
->skip_rows
> last
)
2799 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2800 if (dview
->skip_rows
< 0)
2801 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2802 if (dview
->skip_cols
< 0)
2803 dview
->skip_cols
= 0;
2808 width1
= dview
->half1
+ dview
->bias
;
2809 width2
= dview
->half2
- dview
->bias
;
2816 if (dview
->new_frame
)
2820 tty_setcolor (NORMAL_COLOR
);
2821 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2823 tty_draw_box (1, 0, height
, width1
, FALSE
);
2825 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2830 if (xwidth
< width1
- 1)
2832 tty_gotoyx (1, xwidth
);
2833 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2834 tty_gotoyx (height
, xwidth
);
2835 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2836 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2838 if (xwidth
< width2
- 1)
2840 tty_gotoyx (1, width1
+ xwidth
);
2841 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2842 tty_gotoyx (height
, width1
+ xwidth
);
2843 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2844 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2847 dview
->new_frame
= 0;
2852 dview_status (dview
, dview
->ord
, width1
, 0);
2853 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2857 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2858 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2862 /* --------------------------------------------------------------------------------------------- */
2865 dview_edit (WDiff
* dview
, diff_place_t ord
)
2869 int linenum
, lineofs
;
2871 if (dview
->dsrc
== DATA_SRC_TMP
)
2873 error_dialog (_("Edit"), _("Edit is disabled"));
2877 h
= WIDGET (dview
)->owner
;
2880 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2881 h
->modal
= TRUE
; /* not allow edit file in several editors */
2883 vfs_path_t
*tmp_vpath
;
2885 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2886 do_edit_at_line (tmp_vpath
, use_internal_edit
, linenum
);
2887 vfs_path_free (tmp_vpath
);
2891 dview_update (dview
);
2894 /* --------------------------------------------------------------------------------------------- */
2897 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2900 static const char *title
[2] = {
2901 N_("Goto line (left)"),
2902 N_("Goto line (right)")
2905 static char prev
[256];
2906 /* XXX some statics here, to be remembered between runs */
2911 input
= input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2914 const char *s
= input
;
2916 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2924 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 create_dlg (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3452 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3454 dview
= g_new0 (WDiff
, 1);
3456 init_widget (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 run_dlg (dview_dlg
);
3473 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3474 destroy_dlg (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);
3619 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3621 char *real_file0_str
, *real_file1_str
;
3622 char *file0_str
, *file1_str
;
3624 real_file0_str
= vfs_path_to_str (real_file0
);
3625 real_file1_str
= vfs_path_to_str (real_file1
);
3626 file0_str
= vfs_path_to_str (file0
);
3627 file1_str
= vfs_path_to_str (file1
);
3628 rv
= diff_view (real_file0_str
, real_file1_str
, file0_str
, file1_str
);
3629 g_free (real_file0_str
);
3630 g_free (real_file1_str
);
3640 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3643 vfs_path_free (file1
);
3644 vfs_path_free (file0
);
3649 #undef GET_FILE_AND_STAMP
3652 /* --------------------------------------------------------------------------------------------- */