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
;
2189 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2191 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2192 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2194 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2200 vfs_path_t
*merge_file_name_vpath
= NULL
;
2202 if (!dview
->merged
[n_merge
])
2204 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2205 if (!dview
->merged
[n_merge
])
2207 message (D_ERROR
, MSG_ERROR
,
2208 _("Cannot create backup file\n%s%s\n%s"),
2209 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2214 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2215 if (merge_file_fd
== -1)
2217 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2218 unix_error_string (errno
));
2222 merge_file
= fdopen (merge_file_fd
, "w");
2227 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2228 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2230 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2233 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2234 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2236 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2239 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2242 fflush (merge_file
);
2243 fclose (merge_file
);
2244 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 QUICK2_START_GROUPBOX (N_("Diff algorithm")),
2356 QUICK2_RADIO (3, (const char **) quality_str
, (int *) &dview
->opt
.quality
, NULL
),
2357 QUICK2_STOP_GROUPBOX
,
2358 QUICK2_START_GROUPBOX (N_("Diff extra options")),
2359 QUICK2_CHECKBOX (N_("&Ignore case"), &dview
->opt
.ignore_case
, NULL
),
2360 QUICK2_CHECKBOX (N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
, NULL
),
2361 QUICK2_CHECKBOX (N_("Ignore &space change"), &dview
->opt
.ignore_space_change
, NULL
),
2362 QUICK2_CHECKBOX (N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
, NULL
),
2363 QUICK2_CHECKBOX (N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
,
2365 QUICK2_STOP_GROUPBOX
,
2366 QUICK2_START_BUTTONS (TRUE
, TRUE
),
2367 QUICK2_BUTTON (N_("&OK"), B_ENTER
, NULL
, NULL
),
2368 QUICK2_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
2373 quick_dialog_t qdlg
= {
2375 N_("Diff Options"), "[Diff Options]",
2376 quick_widgets
, NULL
, NULL
2379 if (quick2_dialog (&qdlg
) != B_CANCEL
)
2380 dview_reread (dview
);
2383 /* --------------------------------------------------------------------------------------------- */
2386 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2387 const char *label1
, const char *label2
, DSRC dsrc
)
2390 FBUF
*f
[DIFF_COUNT
];
2392 f
[DIFF_LEFT
] = NULL
;
2393 f
[DIFF_RIGHT
] = NULL
;
2395 if (dsrc
== DATA_SRC_TMP
)
2397 f
[DIFF_LEFT
] = f_temp ();
2398 if (f
[DIFF_LEFT
] == NULL
)
2401 f
[DIFF_RIGHT
] = f_temp ();
2402 if (f
[DIFF_RIGHT
] == NULL
)
2404 f_close (f
[DIFF_LEFT
]);
2408 else if (dsrc
== DATA_SRC_ORG
)
2410 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2411 if (f
[DIFF_LEFT
] == NULL
)
2414 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2415 if (f
[DIFF_RIGHT
] == NULL
)
2417 f_close (f
[DIFF_LEFT
]);
2423 dview
->file
[DIFF_LEFT
] = file1
;
2424 dview
->file
[DIFF_RIGHT
] = file2
;
2425 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2426 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2427 dview
->f
[DIFF_LEFT
] = f
[0];
2428 dview
->f
[DIFF_RIGHT
] = f
[1];
2429 dview
->merged
[DIFF_LEFT
] = FALSE
;
2430 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2431 dview
->hdiff
= NULL
;
2433 dview
->converter
= str_cnv_from_term
;
2435 dview_set_codeset (dview
);
2437 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2438 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2440 ndiff
= redo_diff (dview
);
2443 /* goto WIDGET_DESTROY stage: dview_fini() */
2444 f_close (f
[DIFF_LEFT
]);
2445 f_close (f
[DIFF_RIGHT
]);
2449 dview
->ndiff
= ndiff
;
2451 dview
->view_quit
= 0;
2454 dview
->new_frame
= 1;
2455 dview
->skip_rows
= 0;
2456 dview
->skip_cols
= 0;
2457 dview
->display_symbols
= 0;
2458 dview
->display_numbers
= 0;
2460 dview
->tab_size
= 8;
2461 dview
->ord
= DIFF_LEFT
;
2464 dview
->search
.handle
= NULL
;
2465 dview
->search
.last_string
= NULL
;
2466 dview
->search
.last_found_line
= -1;
2467 dview
->search
.last_accessed_num_line
= -1;
2469 dview
->opt
.quality
= 0;
2470 dview
->opt
.strip_trailing_cr
= 0;
2471 dview
->opt
.ignore_tab_expansion
= 0;
2472 dview
->opt
.ignore_space_change
= 0;
2473 dview
->opt
.ignore_all_space
= 0;
2474 dview
->opt
.ignore_case
= 0;
2476 dview_compute_areas (dview
);
2481 /* --------------------------------------------------------------------------------------------- */
2484 dview_fini (WDiff
* dview
)
2486 if (dview
->dsrc
!= DATA_SRC_MEM
)
2488 f_close (dview
->f
[DIFF_RIGHT
]);
2489 f_close (dview
->f
[DIFF_LEFT
]);
2492 if (dview
->converter
!= str_cnv_from_term
)
2493 str_close_conv (dview
->converter
);
2495 destroy_hdiff (dview
);
2496 if (dview
->a
[DIFF_LEFT
] != NULL
)
2498 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2499 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2500 dview
->a
[DIFF_LEFT
] = NULL
;
2502 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2504 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2505 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2506 dview
->a
[DIFF_RIGHT
] = NULL
;
2509 g_free (dview
->label
[DIFF_LEFT
]);
2510 g_free (dview
->label
[DIFF_RIGHT
]);
2513 /* --------------------------------------------------------------------------------------------- */
2516 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2521 FBUF
*f
= dview
->f
[ord
];
2522 int skip
= dview
->skip_cols
;
2523 int display_symbols
= dview
->display_symbols
;
2524 int display_numbers
= dview
->display_numbers
;
2525 int show_cr
= dview
->show_cr
;
2528 int nwidth
= display_numbers
;
2531 xwidth
= display_symbols
+ display_numbers
;
2532 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2533 tab_size
= dview
->tab_size
;
2537 if (xwidth
> width
&& display_symbols
)
2540 display_symbols
= 0;
2542 if (xwidth
> width
&& display_numbers
)
2545 display_numbers
= width
;
2555 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2557 /* abnormal, but avoid buffer overflow */
2561 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2563 int ch
, next_ch
, col
;
2566 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2568 tty_setcolor (NORMAL_COLOR
);
2569 if (display_symbols
)
2571 tty_gotoyx (r
+ j
, c
- 2);
2572 tty_print_char (ch
);
2576 if (display_numbers
)
2578 tty_gotoyx (r
+ j
, c
- xwidth
);
2579 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2580 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2583 tty_setcolor (DFF_ADD_COLOR
);
2585 tty_setcolor (DFF_CHG_COLOR
);
2588 if (i
== (size_t) dview
->search
.last_found_line
)
2589 tty_setcolor (MARKED_SELECTED_COLOR
);
2590 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2595 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2599 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2600 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2601 tty_gotoyx (r
+ j
, c
);
2604 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2611 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2614 if (!g_unichar_isprint (next_ch
))
2618 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2622 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2624 if (mc_global
.utf8_display
)
2629 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2633 else if (dview
->utf8
)
2634 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2636 next_ch
= convert_to_display_c (next_ch
);
2638 tty_print_anychar (next_ch
);
2646 tty_setcolor (DFF_CHH_COLOR
);
2649 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2652 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2655 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2659 if (display_numbers
)
2661 tty_gotoyx (r
+ j
, c
- xwidth
);
2662 memset (buf
, ' ', display_numbers
);
2663 buf
[display_numbers
] = '\0';
2664 tty_print_string (buf
);
2667 tty_setcolor (DFF_DEL_COLOR
);
2669 tty_setcolor (DFF_CHD_COLOR
);
2670 memset (buf
, ' ', width
);
2673 tty_gotoyx (r
+ j
, c
);
2674 /* tty_print_nstring (buf, width); */
2676 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2683 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &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 (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2832 tty_gotoyx (height
, xwidth
);
2833 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2834 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2836 if (xwidth
< width2
- 1)
2838 tty_gotoyx (1, width1
+ xwidth
);
2839 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2840 tty_gotoyx (height
, width1
+ xwidth
);
2841 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2842 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], 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 do_edit_at_line (tmp_vpath
, use_internal_edit
, 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 */
2909 input
= input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2912 const char *s
= input
;
2914 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2922 for (; i
< dview
->a
[ord
]->len
; i
++)
2924 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2925 if (p
->line
== newline
)
2929 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2930 g_snprintf (prev
, sizeof (prev
), "%d", newline
);
2936 /* --------------------------------------------------------------------------------------------- */
2939 dview_labels (WDiff
* dview
)
2947 b
= find_buttonbar (h
);
2949 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, d
);
2950 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, d
);
2951 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, d
);
2952 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, d
);
2953 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, d
);
2954 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, d
);
2955 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, d
);
2958 /* --------------------------------------------------------------------------------------------- */
2961 dview_event (Gpm_Event
* event
, void *data
)
2963 WDiff
*dview
= (WDiff
*) data
;
2965 if (!mouse_global_in_widget (event
, data
))
2966 return MOU_UNHANDLED
;
2968 /* We are not interested in release events */
2969 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2973 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2975 dview
->skip_rows
-= 2;
2976 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2977 dview_update (dview
);
2979 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2981 dview
->skip_rows
+= 2;
2982 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2983 dview_update (dview
);
2989 /* --------------------------------------------------------------------------------------------- */
2992 dview_save (WDiff
* dview
)
2994 gboolean res
= TRUE
;
2996 if (dview
->merged
[DIFF_LEFT
])
2998 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
2999 dview
->merged
[DIFF_LEFT
] = !res
;
3001 if (dview
->merged
[DIFF_RIGHT
])
3003 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3004 dview
->merged
[DIFF_RIGHT
] = !res
;
3009 /* --------------------------------------------------------------------------------------------- */
3012 dview_do_save (WDiff
* dview
)
3014 (void) dview_save (dview
);
3017 /* --------------------------------------------------------------------------------------------- */
3020 dview_save_options (WDiff
* dview
)
3022 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
3023 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
3024 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
3025 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
3026 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
3028 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3030 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
3031 dview
->opt
.strip_trailing_cr
);
3032 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
3033 dview
->opt
.ignore_all_space
);
3034 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
3035 dview
->opt
.ignore_space_change
);
3036 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
3037 dview
->opt
.ignore_tab_expansion
);
3038 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
3041 /* --------------------------------------------------------------------------------------------- */
3044 dview_load_options (WDiff
* dview
)
3046 gboolean show_numbers
, show_symbols
;
3049 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
3051 dview
->display_symbols
= 1;
3052 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
3054 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
3055 tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
3056 if (tab_size
> 0 && tab_size
< 9)
3057 dview
->tab_size
= tab_size
;
3059 dview
->tab_size
= 8;
3061 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
3063 dview
->opt
.strip_trailing_cr
=
3064 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3065 dview
->opt
.ignore_all_space
=
3066 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3067 dview
->opt
.ignore_space_change
=
3068 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3069 dview
->opt
.ignore_tab_expansion
=
3070 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3071 dview
->opt
.ignore_case
=
3072 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
3074 dview
->new_frame
= 1;
3077 /* --------------------------------------------------------------------------------------------- */
3080 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3084 dview_ok_to_exit (WDiff
* dview
)
3086 gboolean res
= TRUE
;
3089 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3092 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3093 _("File(s) was modified. Save with exit?") :
3094 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3095 D_NORMAL
, 2, _("&Yes"), _("&No"));
3098 if (mc_global
.midnight_shutdown
|| (act
== -1))
3107 (void) dview_save (dview
);
3111 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3112 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3113 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3114 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3123 /* --------------------------------------------------------------------------------------------- */
3126 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
3128 cb_ret_t res
= MSG_HANDLED
;
3132 case CK_ShowSymbols
:
3133 dview
->display_symbols
^= 1;
3134 dview
->new_frame
= 1;
3136 case CK_ShowNumbers
:
3137 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
3138 dview
->new_frame
= 1;
3142 dview
->new_frame
= 1;
3148 dview
->new_frame
= 1;
3154 dview_compute_split (dview
, 1);
3155 dview
->new_frame
= 1;
3162 dview_compute_split (dview
, -1);
3163 dview
->new_frame
= 1;
3167 dview
->tab_size
= 2;
3170 dview
->tab_size
= 3;
3173 dview
->tab_size
= 4;
3176 dview
->tab_size
= 8;
3185 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3186 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3189 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3190 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3193 dview_goto_cmd (dview
, TRUE
);
3196 dview_edit (dview
, dview
->ord
);
3199 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3203 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3207 dview_edit (dview
, dview
->ord
^ 1);
3210 dview_search_cmd (dview
);
3212 case CK_SearchContinue
:
3213 dview_continue_search_cmd (dview
);
3216 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3219 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3222 if (dview
->skip_rows
> 0)
3225 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3230 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3233 if (dview
->height
> 2)
3235 dview
->skip_rows
+= dview
->height
- 2;
3236 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3240 if (dview
->height
> 2)
3242 dview
->skip_rows
-= dview
->height
- 2;
3243 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3253 dview
->skip_cols
-= 8;
3256 dview
->skip_cols
+= 8;
3259 dview
->skip_cols
= 0;
3265 dview
->view_quit
= 1;
3268 dview_do_save (dview
);
3271 dview_diff_options (dview
);
3274 case CK_SelectCodepage
:
3275 dview_select_encoding (dview
);
3279 /* don't close diffviewer due to SIGINT */
3282 res
= MSG_NOT_HANDLED
;
3287 /* --------------------------------------------------------------------------------------------- */
3290 dview_handle_key (WDiff
* dview
, int key
)
3292 unsigned long command
;
3295 key
= convert_from_input_c (key
);
3298 command
= keybind_lookup_keymap_command (diff_map
, key
);
3299 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3303 return MSG_NOT_HANDLED
;
3306 /* --------------------------------------------------------------------------------------------- */
3309 dview_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3311 WDiff
*dview
= (WDiff
*) w
;
3312 Dlg_head
*h
= w
->owner
;
3318 dview_labels (dview
);
3319 dview_load_options (dview
);
3320 dview_update (dview
);
3324 dview
->new_frame
= 1;
3325 dview_update (dview
);
3329 i
= dview_handle_key (dview
, parm
);
3330 if (dview
->view_quit
)
3333 dview_update (dview
);
3336 case WIDGET_COMMAND
:
3337 i
= dview_execute_cmd (dview
, parm
);
3338 if (dview
->view_quit
)
3341 dview_update (dview
);
3344 case WIDGET_DESTROY
:
3345 dview_save_options (dview
);
3350 return default_widget_callback (sender
, msg
, parm
, data
);
3354 /* --------------------------------------------------------------------------------------------- */
3357 dview_adjust_size (Dlg_head
* h
)
3362 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3363 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3364 bar
= find_buttonbar (h
);
3365 widget_set_size (WIDGET (dview
), 0, 0, LINES
- 1, COLS
);
3366 widget_set_size (WIDGET (bar
), LINES
- 1, 0, 1, COLS
);
3368 dview_compute_areas (dview
);
3371 /* --------------------------------------------------------------------------------------------- */
3374 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3376 WDiff
*dview
= (WDiff
*) data
;
3381 dview_adjust_size (h
);
3387 return dview_execute_cmd (NULL
, parm
);
3388 /* message from buttonbar */
3389 if (sender
== WIDGET (find_buttonbar (h
)))
3392 return send_message (WIDGET (data
), NULL
, WIDGET_COMMAND
, parm
, NULL
);
3394 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3395 return dview_execute_cmd (dview
, parm
);
3397 return MSG_NOT_HANDLED
;
3400 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3401 h
->state
= DLG_ACTIVE
; /* don't stop the dialog before final decision */
3402 if (dview_ok_to_exit (dview
))
3403 h
->state
= DLG_CLOSED
;
3407 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3411 /* --------------------------------------------------------------------------------------------- */
3414 dview_get_title (const Dlg_head
* h
, size_t len
)
3417 const char *modified
= " (*) ";
3418 const char *notmodified
= " ";
3422 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3423 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3425 title
= g_string_sized_new (len
);
3426 g_string_append (title
, _("Diff:"));
3427 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3428 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3429 g_string_append (title
, " | ");
3430 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3431 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3433 return g_string_free (title
, FALSE
);
3436 /* --------------------------------------------------------------------------------------------- */
3439 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3444 Dlg_head
*dview_dlg
;
3446 /* Create dialog and widgets, put them on the dialog */
3448 create_dlg (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3449 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3451 dview
= g_new0 (WDiff
, 1);
3453 init_widget (w
, 0, 0, LINES
- 1, COLS
, dview_callback
, dview_event
);
3454 widget_want_cursor (w
, FALSE
);
3456 add_widget (dview_dlg
, dview
);
3457 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3459 dview_dlg
->get_title
= dview_get_title
;
3461 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3463 /* Please note that if you add another widget,
3464 * you have to modify dview_adjust_size to
3468 run_dlg (dview_dlg
);
3470 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3471 destroy_dlg (dview_dlg
);
3473 return error
== 0 ? 1 : 0;
3476 /*** public functions ****************************************************************************/
3477 /* --------------------------------------------------------------------------------------------- */
3479 #define GET_FILE_AND_STAMP(n) \
3483 real_file##n = file##n; \
3484 if (!vfs_file_is_local (file##n)) \
3486 real_file##n = mc_getlocalcopy (file##n); \
3487 if (real_file##n != NULL) \
3490 if (mc_stat (real_file##n, &st##n) != 0) \
3497 #define UNGET_FILE(n) \
3503 if (use_copy##n > 0) \
3506 mtime = st##n.st_mtime; \
3507 if (mc_stat (real_file##n, &st##n) == 0) \
3508 changed = (mtime != st##n.st_mtime); \
3510 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3511 vfs_path_free (real_file##n); \
3517 dview_diff_cmd (const void *f0
, const void *f1
)
3520 vfs_path_t
*file0
= NULL
;
3521 vfs_path_t
*file1
= NULL
;
3522 gboolean is_dir0
= FALSE
;
3523 gboolean is_dir1
= FALSE
;
3525 switch (mc_global
.mc_run_mode
)
3529 /* run from panels */
3530 const WPanel
*panel0
= (const WPanel
*) f0
;
3531 const WPanel
*panel1
= (const WPanel
*) f1
;
3533 file0
= vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, NULL
);
3534 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3537 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3538 path_trunc (selection (panel0
)->fname
, 30));
3542 file1
= vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, NULL
);
3543 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3546 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3547 path_trunc (selection (panel1
)->fname
, 30));
3553 case MC_RUN_DIFFVIEWER
:
3555 /* run from command line */
3556 const char *p0
= (const char *) f0
;
3557 const char *p1
= (const char *) f1
;
3560 file0
= vfs_path_from_str (p0
);
3561 if (mc_stat (file0
, &st
) == 0)
3563 is_dir0
= S_ISDIR (st
.st_mode
);
3566 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3572 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3573 path_trunc (p0
, 30), unix_error_string (errno
));
3577 file1
= vfs_path_from_str (p1
);
3578 if (mc_stat (file1
, &st
) == 0)
3580 is_dir1
= S_ISDIR (st
.st_mode
);
3583 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3589 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3590 path_trunc (p1
, 30), unix_error_string (errno
));
3597 /* this should not happaned */
3598 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3605 if (file0
!= NULL
&& file1
!= NULL
)
3611 vfs_path_t
*real_file0
;
3612 vfs_path_t
*real_file1
;
3614 GET_FILE_AND_STAMP (0);
3615 GET_FILE_AND_STAMP (1);
3616 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3618 char *real_file0_str
, *real_file1_str
;
3619 char *file0_str
, *file1_str
;
3621 real_file0_str
= vfs_path_to_str (real_file0
);
3622 real_file1_str
= vfs_path_to_str (real_file1
);
3623 file0_str
= vfs_path_to_str (file0
);
3624 file1_str
= vfs_path_to_str (file1
);
3625 rv
= diff_view (real_file0_str
, real_file1_str
, file0_str
, file1_str
);
3626 g_free (real_file0_str
);
3627 g_free (real_file1_str
);
3637 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3640 vfs_path_free (file1
);
3641 vfs_path_free (file0
);
3646 #undef GET_FILE_AND_STAMP
3649 /* --------------------------------------------------------------------------------------------- */