2 Copyright (C) 2007-2016
3 Free Software Foundation, Inc.
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
8 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013, 2016
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/>.
33 #include <sys/types.h>
36 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/tty/color.h"
39 #include "lib/tty/key.h"
40 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
41 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
43 #include "lib/widget.h"
44 #include "lib/strutil.h"
45 #include "lib/strescape.h" /* strutils_glob_escape() */
47 #include "lib/charsets.h"
49 #include "lib/event.h" /* mc_event_raise() */
51 #include "src/filemanager/cmd.h" /* edit_file_at_line(), view_other_cmd() */
52 #include "src/filemanager/panel.h"
53 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
55 #include "src/keybind-defaults.h"
56 #include "src/setup.h"
57 #include "src/history.h"
59 #include "src/selcodepage.h"
65 /*** global variables ****************************************************************************/
67 /*** file scope macro definitions ****************************************************************/
69 #define g_array_foreach(a, TP, cbf) \
71 size_t g_array_foreach_i;\
73 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
75 TP *g_array_foreach_var; \
77 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
78 (*cbf) (g_array_foreach_var); \
82 #define FILE_READ_BUF 4096
83 #define FILE_FLAG_TEMP (1 << 0)
90 #define HDIFF_ENABLE 1
91 #define HDIFF_MINCTX 5
92 #define HDIFF_DEPTH 10
94 #define FILE_DIRTY(fs) \
102 /*** file scope type declarations ****************************************************************/
108 } action_direction_t
;
110 /*** file scope variables ************************************************************************/
112 /*** file scope functions ************************************************************************/
113 /* --------------------------------------------------------------------------------------------- */
116 TAB_SKIP (int ts
, int pos
)
118 if (ts
> 0 && ts
< 9)
119 return ts
- pos
% ts
;
124 /* --------------------------------------------------------------------------------------------- */
127 rewrite_backup_content (const vfs_path_t
* from_file_name_vpath
, const char *to_file_name
)
132 const char *from_file_name
;
134 from_file_name
= vfs_path_get_by_index (from_file_name_vpath
, -1)->path
;
135 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
138 backup_fd
= fopen (to_file_name
, "w");
139 if (backup_fd
== NULL
)
145 length
= fwrite ((const void *) contents
, length
, 1, backup_fd
);
153 /* buffered I/O ************************************************************* */
156 * Try to open a temporary file.
157 * @note the name is not altered if this function fails
159 * @param[out] name address of a pointer to store the temporary name
160 * @return file descriptor on success, negative on error
164 open_temp (void **name
)
167 vfs_path_t
*diff_file_name
= NULL
;
169 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
172 message (D_ERROR
, MSG_ERROR
,
173 _("Cannot create temporary diff file\n%s"), unix_error_string (errno
));
176 *name
= g_strdup (vfs_path_as_str (diff_file_name
));
177 vfs_path_free (diff_file_name
);
181 /* --------------------------------------------------------------------------------------------- */
184 * Alocate file structure and associate file descriptor to it.
186 * @param fd file descriptor
187 * @return file structure
198 fs
= g_try_malloc (sizeof (FBUF
));
202 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
217 /* --------------------------------------------------------------------------------------------- */
220 * Free file structure without closing the file.
222 * @param fs file structure
223 * @return 0 on success, non-zero on error
231 if (fs
->flags
& FILE_FLAG_TEMP
)
233 rv
= unlink (fs
->data
);
241 /* --------------------------------------------------------------------------------------------- */
244 * Open a binary temporary file in R/W mode.
245 * @note the file will be deleted when closed
247 * @return file structure
259 fd
= open_temp (&fs
->data
);
267 fs
->flags
= FILE_FLAG_TEMP
;
271 /* --------------------------------------------------------------------------------------------- */
274 * Open a binary file in specified mode.
276 * @param filename file name
277 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
279 * @return file structure
283 f_open (const char *filename
, int flags
)
292 fd
= open (filename
, flags
);
303 /* --------------------------------------------------------------------------------------------- */
306 * Read a line of bytes from file until newline or EOF.
307 * @note does not stop on null-byte
308 * @note buf will not be null-terminated
310 * @param buf destination buffer
311 * @param size size of buffer
312 * @param fs file structure
314 * @return number of bytes read
318 f_gets (char *buf
, size_t size
, FBUF
* fs
)
327 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
335 if (j
== size
|| stop
)
339 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
346 /* --------------------------------------------------------------------------------------------- */
350 * @note avoids thrashing read cache when possible
352 * @param fs file structure
354 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
356 * @return position in file, starting from begginning
360 f_seek (FBUF
* fs
, off_t off
, int whence
)
364 if (fs
->len
&& whence
!= SEEK_END
)
366 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
369 if (whence
== SEEK_CUR
)
372 off
+= rv
- fs
->len
+ fs
->pos
;
374 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
376 fs
->pos
= fs
->len
+ off
- rv
;
382 rv
= lseek (fs
->fd
, off
, whence
);
388 /* --------------------------------------------------------------------------------------------- */
391 * Seek to the beginning of file, thrashing read cache.
393 * @param fs file structure
395 * @return 0 if success, non-zero on error
403 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
409 /* --------------------------------------------------------------------------------------------- */
412 * Write bytes to file.
413 * @note thrashes read cache
415 * @param fs file structure
416 * @param buf source buffer
417 * @param size size of buffer
419 * @return number of written bytes, -1 on error
423 f_write (FBUF
* fs
, const char *buf
, size_t size
)
427 rv
= write (fs
->fd
, buf
, size
);
433 /* --------------------------------------------------------------------------------------------- */
436 * Truncate file to the current position.
437 * @note thrashes read cache
439 * @param fs file structure
441 * @return current file size on success, negative on error
449 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
454 rv
= ftruncate (fs
->fd
, off
);
463 /* --------------------------------------------------------------------------------------------- */
467 * @note if this is temporary file, it is deleted
469 * @param fs file structure
470 * @return 0 on success, non-zero on error
487 /* --------------------------------------------------------------------------------------------- */
490 * Create pipe stream to process.
492 * @param cmd shell command line
493 * @param flags open mode, either O_RDONLY or O_WRONLY
495 * @return file structure
499 p_open (const char *cmd
, int flags
)
503 const char *type
= NULL
;
505 if (flags
== O_RDONLY
)
507 else if (flags
== O_WRONLY
)
517 f
= popen (cmd
, type
);
529 /* --------------------------------------------------------------------------------------------- */
534 * @param fs structure
535 * @return 0 on success, non-zero on error
545 rv
= pclose (fs
->data
);
552 /* --------------------------------------------------------------------------------------------- */
555 * Get one char (byte) from string
557 * @param char * str, gboolean * result
558 * @return int as character or 0 and result == FALSE if fail
562 dview_get_byte (char *str
, gboolean
* result
)
570 return (unsigned char) *str
;
573 /* --------------------------------------------------------------------------------------------- */
577 * Get utf multibyte char from string
579 * @param char * str, int * char_length, gboolean * result
580 * @return int as utf character or 0 and result == FALSE if fail
584 dview_get_utf (char *str
, int *char_length
, gboolean
* result
)
598 res
= g_utf8_get_char_validated (str
, -1);
607 /* Calculate UTF-8 char length */
608 next_ch
= g_utf8_next_char (str
);
609 ch_len
= next_ch
- str
;
611 *char_length
= ch_len
;
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
);
649 #endif /*HAVE_CHARSET */
651 /* --------------------------------------------------------------------------------------------- */
653 /* diff parse *************************************************************** */
656 * Read decimal number from string.
658 * @param[in,out] str string to parse
659 * @param[out] n extracted number
660 * @return 0 if success, otherwise non-zero
664 scan_deci (const char **str
, int *n
)
666 const char *p
= *str
;
670 *n
= strtol (p
, &q
, 10);
671 if (errno
!= 0 || p
== q
)
677 /* --------------------------------------------------------------------------------------------- */
680 * Parse line for diff statement.
682 * @param p string to parse
683 * @param ops list of diff statements
684 * @return 0 if success, otherwise non-zero
688 scan_line (const char *p
, GArray
* ops
)
697 /* handle the following cases:
699 * NUM[,NUM]cNUM[,NUM]
701 * where NUM is a positive integer
704 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
712 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
724 else if (cmd
!= 'c' && cmd
!= 'd')
727 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
735 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
741 if (cmd
== 'd' && range
!= 0)
749 g_array_append_val (ops
, op
);
753 /* --------------------------------------------------------------------------------------------- */
756 * Parse diff output and extract diff statements.
758 * @param f stream to read from
759 * @param ops list of diff statements to fill
760 * @return positive number indicating number of hunks, otherwise negative
764 scan_diff (FBUF
* f
, GArray
* ops
)
769 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
771 if (isdigit (buf
[0]))
773 if (buf
[sz
- 1] != '\n')
777 if (scan_line (buf
, ops
) != 0)
783 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
790 /* --------------------------------------------------------------------------------------------- */
793 * Invoke diff and extract diff statements.
795 * @param args extra arguments to be passed to diff
796 * @param extra more arguments to be passed to diff
797 * @param file1 first file to compare
798 * @param file2 second file to compare
799 * @param ops list of diff statements to fill
801 * @return positive number indicating number of hunks, otherwise negative
805 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
808 static const char *opt
=
809 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
810 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
811 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
812 " --unchanged-group-format=''";
818 char *file1_esc
, *file2_esc
;
820 /* escape potential $ to avoid shell variable substitutions in popen() */
821 file1_esc
= strutils_shell_escape (file1
);
822 file2_esc
= strutils_shell_escape (file2
);
823 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
830 f
= p_open (cmd
, O_RDONLY
);
836 rv
= scan_diff (f
, ops
);
839 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
845 /* --------------------------------------------------------------------------------------------- */
848 * Reparse and display file according to diff statements.
850 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
851 * @param filename file name to display
852 * @param ops list of diff statements
853 * @param printer printf-like function to be used for displaying
854 * @param ctx printer context
856 * @return 0 if success, otherwise non-zero
860 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
873 f
= f_open (filename
, O_RDONLY
);
889 #define T1 a[ ord^1 ][0]
890 #define T2 a[ ord^1 ][1]
891 for (i
= 0; i
< ops
->len
; i
++)
895 op
= &g_array_index (ops
, DIFFCMD
, i
);
896 n
= op
->F1
- (op
->cmd
!= add_cmd
);
898 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
901 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
903 while (buf
[sz
- 1] != '\n')
905 sz
= f_gets (buf
, sizeof (buf
), f
);
908 printer (ctx
, 0, 0, 0, 1, "\n");
911 printer (ctx
, 0, 0, 0, sz
, buf
);
919 if (op
->cmd
== add_cmd
)
921 n
= op
->T2
- op
->T1
+ 1;
924 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
929 if (op
->cmd
== del_cmd
)
931 n
= op
->F2
- op
->F1
+ 1;
932 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
935 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
937 while (buf
[sz
- 1] != '\n')
939 sz
= f_gets (buf
, sizeof (buf
), f
);
942 printer (ctx
, 0, 0, 0, 1, "\n");
945 printer (ctx
, 0, 0, 0, sz
, buf
);
957 n
= op
->F2
- op
->F1
+ 1;
958 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
961 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
963 while (buf
[sz
- 1] != '\n')
965 sz
= f_gets (buf
, sizeof (buf
), f
);
968 printer (ctx
, 0, 0, 0, 1, "\n");
971 printer (ctx
, 0, 0, 0, sz
, buf
);
980 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
983 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
993 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
996 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
998 while (buf
[sz
- 1] != '\n')
1000 sz
= f_gets (buf
, sizeof (buf
), f
);
1003 printer (ctx
, 0, 0, 0, 1, "\n");
1006 printer (ctx
, 0, 0, 0, sz
, buf
);
1019 /* --------------------------------------------------------------------------------------------- */
1021 /* horizontal diff ********************************************************** */
1024 * Longest common substring.
1026 * @param s first string
1027 * @param m length of first string
1028 * @param t second string
1029 * @param n length of second string
1030 * @param ret list of offsets for longest common substrings inside each string
1031 * @param min minimum length of common substrings
1033 * @return 0 if success, nonzero otherwise
1037 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1043 if (m
< min
|| n
< min
)
1045 /* XXX early culling */
1049 Lprev
= g_try_new0 (int, n
+ 1);
1053 Lcurr
= g_try_new0 (int, n
+ 1);
1060 for (i
= 0; i
< m
; i
++)
1067 #ifdef USE_MEMSET_IN_LCS
1068 memset (Lcurr
, 0, (n
+ 1) * sizeof (*Lcurr
));
1070 for (j
= 0; j
< n
; j
++)
1072 #ifndef USE_MEMSET_IN_LCS
1084 g_array_set_size (ret
, 0);
1086 if (z
== v
&& z
>= min
)
1094 for (k
= 0; k
< ret
->len
; k
++)
1096 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1097 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1106 g_array_append_val (ret
, p2
);
1118 /* --------------------------------------------------------------------------------------------- */
1121 * Scan recursively for common substrings and build ranges.
1123 * @param s first string
1124 * @param t second string
1125 * @param bracket current limits for both of the strings
1126 * @param min minimum length of common substrings
1127 * @param hdiff list of horizontal diff ranges to fill
1128 * @param depth recursion depth
1130 * @return 0 if success, nonzero otherwise
1134 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1145 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1149 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1150 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1154 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1157 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1158 b
[DIFF_LEFT
].len
= (*data
)[0];
1159 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1160 b
[DIFF_RIGHT
].len
= (*data
)[1];
1161 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1164 for (k
= 0; k
< ret
->len
- 1; k
++)
1166 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1167 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1168 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1169 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1170 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1171 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1172 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1175 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1176 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1177 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1178 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1179 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1180 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1183 g_array_free (ret
, TRUE
);
1188 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1189 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1190 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1191 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1192 g_array_append_val (hdiff
, p
);
1197 /* --------------------------------------------------------------------------------------------- */
1200 * Build list of horizontal diff ranges.
1202 * @param s first string
1203 * @param m length of first string
1204 * @param t second string
1205 * @param n length of second string
1206 * @param min minimum length of common substrings
1207 * @param hdiff list of horizontal diff ranges to fill
1208 * @param depth recursion depth
1210 * @return 0 if success, nonzero otherwise
1214 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1219 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1220 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1222 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1225 b
[DIFF_LEFT
].off
= i
;
1226 b
[DIFF_LEFT
].len
= m
- i
;
1227 b
[DIFF_RIGHT
].off
= i
;
1228 b
[DIFF_RIGHT
].len
= n
- i
;
1230 /* smartscan (multiple horizontal diff) */
1231 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1234 /* --------------------------------------------------------------------------------------------- */
1236 /* read line **************************************************************** */
1239 * Check if character is inside horizontal diff limits.
1241 * @param k rank of character inside line
1242 * @param hdiff horizontal diff structure
1243 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1245 * @return TRUE if inside hdiff limits, FALSE otherwise
1249 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1254 for (i
= 0; i
< hdiff
->len
; i
++)
1258 b
= &g_array_index (hdiff
, BRACKET
, i
);
1259 start
= (*b
)[ord
].off
;
1260 end
= start
+ (*b
)[ord
].len
;
1261 if (k
>= start
&& k
< end
)
1267 /* --------------------------------------------------------------------------------------------- */
1270 * Copy 'src' to 'dst' expanding tabs.
1271 * @note The procedure returns when all bytes are consumed from 'src'
1273 * @param dst destination buffer
1274 * @param src source buffer
1275 * @param srcsize size of src buffer
1276 * @param base virtual base of this string, needed to calculate tabs
1277 * @param ts tab size
1279 * @return new virtual base
1283 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1287 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1294 j
= TAB_SKIP (ts
, i
+ base
);
1304 /* --------------------------------------------------------------------------------------------- */
1307 * Copy 'src' to 'dst' expanding tabs.
1309 * @param dst destination buffer
1310 * @param dstsize size of dst buffer
1311 * @param[in,out] _src source buffer
1312 * @param srcsize size of src buffer
1313 * @param base virtual base of this string, needed to calculate tabs
1314 * @param ts tab size
1316 * @return new virtual base
1318 * @note The procedure returns when all bytes are consumed from 'src'
1319 * or 'dstsize' bytes are written to 'dst'
1320 * @note Upon return, 'src' points to the first unwritten character in source
1324 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1327 const char *src
= *_src
;
1329 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1336 j
= TAB_SKIP (ts
, i
+ base
);
1337 if (j
> dstsize
- i
)
1349 /* --------------------------------------------------------------------------------------------- */
1352 * Read line from memory, converting tabs to spaces and padding with spaces.
1354 * @param src buffer to read from
1355 * @param srcsize size of src buffer
1356 * @param dst buffer to read to
1357 * @param dstsize size of dst buffer, excluding trailing null
1358 * @param skip number of characters to skip
1359 * @param ts tab size
1360 * @param show_cr show trailing carriage return as ^M
1362 * @return negative on error, otherwise number of bytes except padding
1366 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
,
1377 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1383 j
= TAB_SKIP (ts
, i
+ base
);
1389 else if (dstsize
!= 0)
1396 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1398 if (skip
== 0 && show_cr
)
1420 (void) dview_get_utf ((char *) src
, &ch_len
, &res
);
1436 while (dstsize
!= 0)
1445 /* --------------------------------------------------------------------------------------------- */
1448 * Read line from memory and build attribute array.
1450 * @param src buffer to read from
1451 * @param srcsize size of src buffer
1452 * @param dst buffer to read to
1453 * @param dstsize size of dst buffer, excluding trailing null
1454 * @param skip number of characters to skip
1455 * @param ts tab size
1456 * @param show_cr show trailing carriage return as ^M
1457 * @param hdiff horizontal diff structure
1458 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1459 * @param att buffer of attributes
1461 * @return negative on error, otherwise number of bytes except padding
1465 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
,
1466 gboolean show_cr
, GArray
* hdiff
, diff_place_t ord
, char *att
)
1476 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1482 j
= TAB_SKIP (ts
, i
+ base
);
1488 else if (dstsize
!= 0)
1491 *att
++ = is_inside (k
, hdiff
, ord
);
1496 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1498 if (skip
== 0 && show_cr
)
1503 *att
++ = is_inside (k
, hdiff
, ord
);
1505 *att
++ = is_inside (k
, hdiff
, ord
);
1511 *att
++ = is_inside (k
, hdiff
, ord
);
1523 (void) dview_get_utf ((char *) src
, &ch_len
, &res
);
1533 *att
++ = is_inside (k
, hdiff
, ord
);
1539 while (dstsize
!= 0)
1549 /* --------------------------------------------------------------------------------------------- */
1552 * Read line from file, converting tabs to spaces and padding with spaces.
1554 * @param f file stream to read from
1555 * @param off offset of line inside file
1556 * @param dst buffer to read to
1557 * @param dstsize size of dst buffer, excluding trailing null
1558 * @param skip number of characters to skip
1559 * @param ts tab size
1560 * @param show_cr show trailing carriage return as ^M
1562 * @return negative on error, otherwise number of bytes except padding
1566 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, gboolean show_cr
)
1569 int old_base
= base
;
1570 size_t amount
= dstsize
;
1571 size_t useful
, offset
;
1575 const char *q
= NULL
;
1576 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1577 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1579 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1581 /* abnormal, but avoid buffer overflow */
1582 memset (dst
, ' ', dstsize
);
1583 dst
[dstsize
] = '\0';
1587 f_seek (f
, off
, SEEK_SET
);
1592 sz
= f_gets (tmp
, amount
, f
);
1596 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1597 if (cvt
[base
- old_base
- 1] == '\n')
1599 q
= &cvt
[base
- old_base
- 1];
1600 base
= old_base
+ q
- cvt
+ 1;
1607 memset (dst
, ' ', dstsize
);
1608 dst
[dstsize
] = '\0';
1612 useful
= base
- skip
;
1613 offset
= skip
- old_base
;
1615 if (useful
<= dstsize
)
1618 memmove (dst
, cvt
+ offset
, useful
);
1622 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1625 const char *ptr
= tmp
;
1627 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1636 memmove (dst
, cvt
+ offset
, dstsize
);
1638 lastch
= cvt
[offset
+ dstsize
];
1642 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1644 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1648 if (i
+ 1 < dstsize
)
1662 for (; i
< dstsize
; i
++)
1668 /* --------------------------------------------------------------------------------------------- */
1669 /* diff printers et al ****************************************************** */
1672 cc_free_elt (void *elt
)
1680 /* --------------------------------------------------------------------------------------------- */
1683 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1685 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1686 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1696 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1698 if (sz
!= 0 && str
[sz
- 1] == '\n')
1701 p
.p
= g_strndup (str
, sz
);
1704 g_array_append_val (a
, p
);
1706 else if (dsrc
== DATA_SRC_MEM
)
1710 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1711 if (sz
!= 0 && str
[sz
- 1] == '\n')
1718 new_size
= p
->u
.len
+ sz
;
1719 q
= g_realloc (p
->p
, new_size
);
1720 memcpy (q
+ p
->u
.len
, str
, sz
);
1725 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1727 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1728 f_write (f
, str
, sz
);
1733 /* --------------------------------------------------------------------------------------------- */
1736 redo_diff (WDiff
* dview
)
1738 FBUF
*const *f
= dview
->f
;
1746 if (dview
->opt
.quality
== 2)
1747 strcat (extra
, " -d");
1748 if (dview
->opt
.quality
== 1)
1749 strcat (extra
, " --speed-large-files");
1750 if (dview
->opt
.strip_trailing_cr
)
1751 strcat (extra
, " --strip-trailing-cr");
1752 if (dview
->opt
.ignore_tab_expansion
)
1753 strcat (extra
, " -E");
1754 if (dview
->opt
.ignore_space_change
)
1755 strcat (extra
, " -b");
1756 if (dview
->opt
.ignore_all_space
)
1757 strcat (extra
, " -w");
1758 if (dview
->opt
.ignore_case
)
1759 strcat (extra
, " -i");
1761 if (dview
->dsrc
!= DATA_SRC_MEM
)
1763 f_reset (f
[DIFF_LEFT
]);
1764 f_reset (f
[DIFF_RIGHT
]);
1767 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1768 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1772 g_array_free (ops
, TRUE
);
1776 ctx
.dsrc
= dview
->dsrc
;
1779 ctx
.a
= dview
->a
[DIFF_LEFT
];
1780 ctx
.f
= f
[DIFF_LEFT
];
1781 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1783 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1784 ctx
.f
= f
[DIFF_RIGHT
];
1785 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1788 g_array_free (ops
, TRUE
);
1790 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1793 if (dview
->dsrc
== DATA_SRC_TMP
)
1795 f_trunc (f
[DIFF_LEFT
]);
1796 f_trunc (f
[DIFF_RIGHT
]);
1799 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1801 dview
->hdiff
= g_ptr_array_new ();
1802 if (dview
->hdiff
!= NULL
)
1806 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1812 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1813 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1814 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1816 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1822 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1826 g_array_free (h
, TRUE
);
1831 g_ptr_array_add (dview
->hdiff
, h
);
1838 /* --------------------------------------------------------------------------------------------- */
1841 destroy_hdiff (WDiff
* dview
)
1843 if (dview
->hdiff
!= NULL
)
1848 len
= dview
->a
[DIFF_LEFT
]->len
;
1850 for (i
= 0; i
< len
; i
++)
1854 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1856 g_array_free (h
, TRUE
);
1858 g_ptr_array_free (dview
->hdiff
, TRUE
);
1859 dview
->hdiff
= NULL
;
1862 mc_search_free (dview
->search
.handle
);
1863 dview
->search
.handle
= NULL
;
1864 MC_PTR_FREE (dview
->search
.last_string
);
1867 /* --------------------------------------------------------------------------------------------- */
1868 /* stuff ******************************************************************** */
1871 get_digits (unsigned int n
)
1880 /* --------------------------------------------------------------------------------------------- */
1883 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1895 p
= &g_array_index (a
, DIFFLN
, pos
);
1901 for (n
= pos
; n
> 0; n
--)
1907 *lineofs
= pos
- n
+ 1;
1915 /* --------------------------------------------------------------------------------------------- */
1918 calc_nwidth (const GArray
** const a
)
1923 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1924 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1927 return get_digits (l1
);
1930 /* --------------------------------------------------------------------------------------------- */
1933 find_prev_hunk (const GArray
* a
, int pos
)
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 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1942 if (pos
> 0 && (size_t) pos
< a
->len
)
1945 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1947 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1954 /* --------------------------------------------------------------------------------------------- */
1957 find_next_hunk (const GArray
* a
, size_t pos
)
1959 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1961 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1966 /* --------------------------------------------------------------------------------------------- */
1968 * Find start and end lines of the current hunk.
1970 * @param dview WDiff widget
1971 * @return boolean and
1972 * start_line1 first line of current hunk (file[0])
1973 * end_line1 last line of current hunk (file[0])
1974 * start_line1 first line of current hunk (file[0])
1975 * end_line1 last line of current hunk (file[0])
1979 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1981 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1982 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1992 pos
= dview
->skip_rows
;
1993 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2010 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2014 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2015 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2017 pos
= dview
->skip_rows
;
2018 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2022 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2023 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2025 *end_line1
= max (*start_line1
, l0
);
2027 *end_line2
= max (*start_line2
, l1
);
2034 /* --------------------------------------------------------------------------------------------- */
2036 * Remove hunk from file.
2038 * @param dview WDiff widget
2039 * @param merge_file file stream for writing data
2040 * @param from1 first line of hunk
2041 * @param to1 last line of hunk
2042 * @param merge_direction in what direction files should be merged
2046 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2047 action_direction_t merge_direction
)
2053 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2054 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2056 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2059 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2062 fputs (buf
, merge_file
);
2064 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2068 fputs (buf
, merge_file
);
2073 /* --------------------------------------------------------------------------------------------- */
2077 * @param dview WDiff widget
2078 * @param merge_file file stream for writing data
2079 * @param from1 first line of source hunk
2080 * @param from2 first line of destination hunk
2081 * @param to1 last line of source hunk
2082 * @param merge_direction in what direction files should be merged
2086 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2087 action_direction_t merge_direction
)
2094 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2096 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2097 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2101 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2102 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2106 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2109 fputs (buf
, merge_file
);
2112 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2116 fputs (buf
, merge_file
);
2118 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2119 fputs (buf
, merge_file
);
2125 /* --------------------------------------------------------------------------------------------- */
2127 * Replace hunk in file.
2129 * @param dview WDiff widget
2130 * @param merge_file file stream for writing data
2131 * @param from1 first line of source hunk
2132 * @param to1 last line of source hunk
2133 * @param from2 first line of destination hunk
2134 * @param to2 last line of destination hunk
2135 * @param merge_direction in what direction files should be merged
2139 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2140 action_direction_t merge_direction
)
2142 int line1
= 0, line2
= 0;
2147 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2149 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2150 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2154 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2155 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2158 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2161 fputs (buf
, merge_file
);
2163 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2167 fputs (buf
, merge_file
);
2169 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2173 fputs (buf
, merge_file
);
2179 /* --------------------------------------------------------------------------------------------- */
2183 * @param dview WDiff widget
2184 * @param merge_direction in what direction files should be merged
2188 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2190 int from1
, to1
, from2
, to2
;
2192 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2194 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2195 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2197 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2203 vfs_path_t
*merge_file_name_vpath
= NULL
;
2205 if (!dview
->merged
[n_merge
])
2207 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2208 if (!dview
->merged
[n_merge
])
2210 message (D_ERROR
, MSG_ERROR
,
2211 _("Cannot create backup file\n%s%s\n%s"),
2212 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2217 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2218 if (merge_file_fd
== -1)
2220 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2221 unix_error_string (errno
));
2225 merge_file
= fdopen (merge_file_fd
, "w");
2230 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2231 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2233 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2236 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2237 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2239 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2242 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2247 fflush (merge_file
);
2248 fclose (merge_file
);
2252 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2255 mc_unlink (merge_file_name_vpath
);
2256 vfs_path_free (merge_file_name_vpath
);
2260 /* --------------------------------------------------------------------------------------------- */
2261 /* view routines and callbacks ********************************************** */
2264 dview_compute_split (WDiff
* dview
, int i
)
2267 if (dview
->bias
< 2 - dview
->half1
)
2268 dview
->bias
= 2 - dview
->half1
;
2269 if (dview
->bias
> dview
->half2
- 2)
2270 dview
->bias
= dview
->half2
- 2;
2273 /* --------------------------------------------------------------------------------------------- */
2276 dview_compute_areas (WDiff
* dview
)
2278 dview
->height
= LINES
- 2;
2279 dview
->half1
= COLS
/ 2;
2280 dview
->half2
= COLS
- dview
->half1
;
2282 dview_compute_split (dview
, 0);
2285 /* --------------------------------------------------------------------------------------------- */
2288 dview_reread (WDiff
* dview
)
2292 destroy_hdiff (dview
);
2293 if (dview
->a
[DIFF_LEFT
] != NULL
)
2295 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2296 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2298 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2300 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2301 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2304 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2305 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2307 ndiff
= redo_diff (dview
);
2309 dview
->ndiff
= ndiff
;
2312 /* --------------------------------------------------------------------------------------------- */
2316 dview_set_codeset (WDiff
* dview
)
2318 const char *encoding_id
= NULL
;
2322 get_codepage_id (mc_global
.source_codepage
>=
2323 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2324 if (encoding_id
!= NULL
)
2328 conv
= str_crt_conv_from (encoding_id
);
2329 if (conv
!= INVALID_CONV
)
2331 if (dview
->converter
!= str_cnv_from_term
)
2332 str_close_conv (dview
->converter
);
2333 dview
->converter
= conv
;
2335 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
2339 /* --------------------------------------------------------------------------------------------- */
2342 dview_select_encoding (WDiff
* dview
)
2344 if (do_select_codepage ())
2345 dview_set_codeset (dview
);
2346 dview_reread (dview
);
2347 tty_touch_screen ();
2350 #endif /* HAVE_CHARSET */
2352 /* --------------------------------------------------------------------------------------------- */
2355 dview_diff_options (WDiff
* dview
)
2357 const char *quality_str
[] = {
2359 N_("&Fastest (Assume large files)"),
2360 N_("&Minimal (Find a smaller set of change)")
2363 quick_widget_t quick_widgets
[] = {
2365 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2366 QUICK_RADIO (3, (const char **) quality_str
, (int *) &dview
->opt
.quality
, NULL
),
2367 QUICK_STOP_GROUPBOX
,
2368 QUICK_START_GROUPBOX (N_("Diff extra options")),
2369 QUICK_CHECKBOX (N_("&Ignore case"), &dview
->opt
.ignore_case
, NULL
),
2370 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
, NULL
),
2371 QUICK_CHECKBOX (N_("Ignore &space change"), &dview
->opt
.ignore_space_change
, NULL
),
2372 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
, NULL
),
2373 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
,
2375 QUICK_STOP_GROUPBOX
,
2376 QUICK_BUTTONS_OK_CANCEL
,
2381 quick_dialog_t qdlg
= {
2383 N_("Diff Options"), "[Diff Options]",
2384 quick_widgets
, NULL
, NULL
2387 if (quick_dialog (&qdlg
) != B_CANCEL
)
2388 dview_reread (dview
);
2391 /* --------------------------------------------------------------------------------------------- */
2394 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2395 const char *label1
, const char *label2
, DSRC dsrc
)
2398 FBUF
*f
[DIFF_COUNT
];
2400 f
[DIFF_LEFT
] = NULL
;
2401 f
[DIFF_RIGHT
] = NULL
;
2403 if (dsrc
== DATA_SRC_TMP
)
2405 f
[DIFF_LEFT
] = f_temp ();
2406 if (f
[DIFF_LEFT
] == NULL
)
2409 f
[DIFF_RIGHT
] = f_temp ();
2410 if (f
[DIFF_RIGHT
] == NULL
)
2412 f_close (f
[DIFF_LEFT
]);
2416 else if (dsrc
== DATA_SRC_ORG
)
2418 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2419 if (f
[DIFF_LEFT
] == NULL
)
2422 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2423 if (f
[DIFF_RIGHT
] == NULL
)
2425 f_close (f
[DIFF_LEFT
]);
2431 dview
->file
[DIFF_LEFT
] = file1
;
2432 dview
->file
[DIFF_RIGHT
] = file2
;
2433 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2434 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2435 dview
->f
[DIFF_LEFT
] = f
[0];
2436 dview
->f
[DIFF_RIGHT
] = f
[1];
2437 dview
->merged
[DIFF_LEFT
] = FALSE
;
2438 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2439 dview
->hdiff
= NULL
;
2442 dview
->converter
= str_cnv_from_term
;
2443 dview_set_codeset (dview
);
2445 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2446 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2448 ndiff
= redo_diff (dview
);
2451 /* goto MSG_DESTROY stage: dview_fini() */
2452 f_close (f
[DIFF_LEFT
]);
2453 f_close (f
[DIFF_RIGHT
]);
2457 dview
->ndiff
= ndiff
;
2459 dview
->view_quit
= FALSE
;
2462 dview
->new_frame
= TRUE
;
2463 dview
->skip_rows
= 0;
2464 dview
->skip_cols
= 0;
2465 dview
->display_symbols
= 0;
2466 dview
->display_numbers
= 0;
2467 dview
->show_cr
= TRUE
;
2468 dview
->tab_size
= 8;
2469 dview
->ord
= DIFF_LEFT
;
2470 dview
->full
= FALSE
;
2472 dview
->search
.handle
= NULL
;
2473 dview
->search
.last_string
= NULL
;
2474 dview
->search
.last_found_line
= -1;
2475 dview
->search
.last_accessed_num_line
= -1;
2477 dview
->opt
.quality
= 0;
2478 dview
->opt
.strip_trailing_cr
= 0;
2479 dview
->opt
.ignore_tab_expansion
= 0;
2480 dview
->opt
.ignore_space_change
= 0;
2481 dview
->opt
.ignore_all_space
= 0;
2482 dview
->opt
.ignore_case
= 0;
2484 dview_compute_areas (dview
);
2489 /* --------------------------------------------------------------------------------------------- */
2492 dview_fini (WDiff
* dview
)
2494 if (dview
->dsrc
!= DATA_SRC_MEM
)
2496 f_close (dview
->f
[DIFF_RIGHT
]);
2497 f_close (dview
->f
[DIFF_LEFT
]);
2501 if (dview
->converter
!= str_cnv_from_term
)
2502 str_close_conv (dview
->converter
);
2505 destroy_hdiff (dview
);
2506 if (dview
->a
[DIFF_LEFT
] != NULL
)
2508 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2509 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2510 dview
->a
[DIFF_LEFT
] = NULL
;
2512 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2514 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2515 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2516 dview
->a
[DIFF_RIGHT
] = NULL
;
2519 g_free (dview
->label
[DIFF_LEFT
]);
2520 g_free (dview
->label
[DIFF_RIGHT
]);
2523 /* --------------------------------------------------------------------------------------------- */
2526 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2531 FBUF
*f
= dview
->f
[ord
];
2532 int skip
= dview
->skip_cols
;
2533 int display_symbols
= dview
->display_symbols
;
2534 int display_numbers
= dview
->display_numbers
;
2535 gboolean show_cr
= dview
->show_cr
;
2538 int nwidth
= display_numbers
;
2541 xwidth
= display_symbols
+ display_numbers
;
2542 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2543 tab_size
= dview
->tab_size
;
2547 if (xwidth
> width
&& display_symbols
)
2550 display_symbols
= 0;
2552 if (xwidth
> width
&& display_numbers
)
2555 display_numbers
= width
;
2565 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2567 /* abnormal, but avoid buffer overflow */
2571 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2573 int ch
, next_ch
, col
;
2576 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2578 tty_setcolor (NORMAL_COLOR
);
2579 if (display_symbols
)
2581 tty_gotoyx (r
+ j
, c
- 2);
2582 tty_print_char (ch
);
2586 if (display_numbers
)
2588 tty_gotoyx (r
+ j
, c
- xwidth
);
2589 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2590 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2593 tty_setcolor (DFF_ADD_COLOR
);
2595 tty_setcolor (DFF_CHG_COLOR
);
2598 if (i
== (size_t) dview
->search
.last_found_line
)
2599 tty_setcolor (MARKED_SELECTED_COLOR
);
2600 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2606 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2611 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2612 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2613 tty_gotoyx (r
+ j
, c
);
2616 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2625 next_ch
= dview_get_utf (buf
+ cnt
, &ch_len
, &ch_res
);
2628 if (!g_unichar_isprint (next_ch
))
2633 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2637 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2639 if (mc_global
.utf8_display
)
2644 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2648 else if (dview
->utf8
)
2649 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2651 next_ch
= convert_to_display_c (next_ch
);
2653 tty_print_anychar (next_ch
);
2661 tty_setcolor (DFF_CHH_COLOR
);
2665 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2669 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2672 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2676 if (display_numbers
)
2678 tty_gotoyx (r
+ j
, c
- xwidth
);
2679 memset (buf
, ' ', display_numbers
);
2680 buf
[display_numbers
] = '\0';
2681 tty_print_string (buf
);
2684 tty_setcolor (DFF_DEL_COLOR
);
2686 tty_setcolor (DFF_CHD_COLOR
);
2687 memset (buf
, ' ', width
);
2690 tty_gotoyx (r
+ j
, c
);
2691 /* tty_print_nstring (buf, width); */
2693 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2702 next_ch
= dview_get_utf (buf
+ cnt
, &ch_len
, &ch_res
);
2705 if (!g_unichar_isprint (next_ch
))
2710 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2714 if (mc_global
.utf8_display
)
2719 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2722 else if (dview
->utf8
)
2723 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2725 next_ch
= convert_to_display_c (next_ch
);
2728 tty_print_anychar (next_ch
);
2733 tty_setcolor (NORMAL_COLOR
);
2735 if (width
< xwidth
- 1)
2737 memset (buf
, ' ', k
);
2739 for (; j
< height
; j
++)
2743 tty_gotoyx (r
+ j
, c
- xwidth
);
2744 /* tty_print_nstring (buf, xwidth - 1); */
2745 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2747 tty_gotoyx (r
+ j
, c
);
2748 /* tty_print_nstring (buf, width); */
2749 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2755 /* --------------------------------------------------------------------------------------------- */
2758 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2762 int linenum
, lineofs
;
2766 tty_setcolor (STATUSBAR_COLOR
);
2769 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2771 filename_width
= width
- 24;
2772 if (filename_width
< 8)
2775 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2776 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2777 vfs_path_free (vpath
);
2778 buf
= str_term_trim (path
, filename_width
);
2779 if (ord
== DIFF_LEFT
)
2780 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2781 buf
, linenum
, lineofs
, dview
->skip_cols
);
2783 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2784 buf
, linenum
, lineofs
, dview
->ndiff
);
2788 /* --------------------------------------------------------------------------------------------- */
2791 dview_redo (WDiff
* dview
)
2793 if (dview
->display_numbers
)
2797 old
= dview
->display_numbers
;
2798 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2799 dview
->new_frame
= (old
!= dview
->display_numbers
);
2801 dview_reread (dview
);
2804 /* --------------------------------------------------------------------------------------------- */
2807 dview_update (WDiff
* dview
)
2809 int height
= dview
->height
;
2814 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2816 if (dview
->skip_rows
> last
)
2817 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2818 if (dview
->skip_rows
< 0)
2819 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2820 if (dview
->skip_cols
< 0)
2821 dview
->skip_cols
= 0;
2826 width1
= dview
->half1
+ dview
->bias
;
2827 width2
= dview
->half2
- dview
->bias
;
2834 if (dview
->new_frame
)
2838 tty_setcolor (NORMAL_COLOR
);
2839 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2841 tty_draw_box (1, 0, height
, width1
, FALSE
);
2843 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2848 if (xwidth
< width1
- 1)
2850 tty_gotoyx (1, xwidth
);
2851 tty_print_alt_char (ACS_TTEE
, FALSE
);
2852 tty_gotoyx (height
, xwidth
);
2853 tty_print_alt_char (ACS_BTEE
, FALSE
);
2854 tty_draw_vline (2, xwidth
, ACS_VLINE
, height
- 2);
2856 if (xwidth
< width2
- 1)
2858 tty_gotoyx (1, width1
+ xwidth
);
2859 tty_print_alt_char (ACS_TTEE
, FALSE
);
2860 tty_gotoyx (height
, width1
+ xwidth
);
2861 tty_print_alt_char (ACS_BTEE
, FALSE
);
2862 tty_draw_vline (2, width1
+ xwidth
, ACS_VLINE
, height
- 2);
2865 dview
->new_frame
= FALSE
;
2870 dview_status (dview
, dview
->ord
, width1
, 0);
2871 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2875 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2876 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2880 /* --------------------------------------------------------------------------------------------- */
2883 dview_edit (WDiff
* dview
, diff_place_t ord
)
2887 int linenum
, lineofs
;
2889 if (dview
->dsrc
== DATA_SRC_TMP
)
2891 error_dialog (_("Edit"), _("Edit is disabled"));
2895 h
= WIDGET (dview
)->owner
;
2898 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2899 h
->modal
= TRUE
; /* not allow edit file in several editors */
2901 vfs_path_t
*tmp_vpath
;
2903 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2904 edit_file_at_line (tmp_vpath
, use_internal_edit
!= 0, linenum
);
2905 vfs_path_free (tmp_vpath
);
2909 dview_update (dview
);
2912 /* --------------------------------------------------------------------------------------------- */
2915 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2917 static gboolean first_run
= TRUE
;
2920 static const char *title
[2] = {
2921 N_("Goto line (left)"),
2922 N_("Goto line (right)")
2930 input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
,
2931 first_run
? NULL
: INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
2934 const char *s
= input
;
2936 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2942 for (; i
< dview
->a
[ord
]->len
; i
++)
2946 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2947 if (p
->line
== newline
)
2951 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2959 /* --------------------------------------------------------------------------------------------- */
2962 dview_labels (WDiff
* dview
)
2970 b
= find_buttonbar (h
);
2972 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, d
);
2973 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, d
);
2974 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, d
);
2975 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, d
);
2976 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, d
);
2977 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, d
);
2978 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, d
);
2981 /* --------------------------------------------------------------------------------------------- */
2984 dview_save (WDiff
* dview
)
2986 gboolean res
= TRUE
;
2988 if (dview
->merged
[DIFF_LEFT
])
2990 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
2991 dview
->merged
[DIFF_LEFT
] = !res
;
2993 if (dview
->merged
[DIFF_RIGHT
])
2995 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
2996 dview
->merged
[DIFF_RIGHT
] = !res
;
3001 /* --------------------------------------------------------------------------------------------- */
3004 dview_do_save (WDiff
* dview
)
3006 (void) dview_save (dview
);
3009 /* --------------------------------------------------------------------------------------------- */
3012 dview_save_options (WDiff
* dview
)
3014 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
3015 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
3016 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
3017 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
3018 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
3020 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3022 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
3023 dview
->opt
.strip_trailing_cr
);
3024 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
3025 dview
->opt
.ignore_all_space
);
3026 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
3027 dview
->opt
.ignore_space_change
);
3028 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
3029 dview
->opt
.ignore_tab_expansion
);
3030 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
3033 /* --------------------------------------------------------------------------------------------- */
3036 dview_load_options (WDiff
* dview
)
3038 gboolean show_numbers
, show_symbols
;
3041 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
3043 dview
->display_symbols
= 1;
3044 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
3046 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
3047 tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
3048 if (tab_size
> 0 && tab_size
< 9)
3049 dview
->tab_size
= tab_size
;
3051 dview
->tab_size
= 8;
3053 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
3055 dview
->opt
.strip_trailing_cr
=
3056 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3057 dview
->opt
.ignore_all_space
=
3058 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3059 dview
->opt
.ignore_space_change
=
3060 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3061 dview
->opt
.ignore_tab_expansion
=
3062 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3063 dview
->opt
.ignore_case
=
3064 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
3066 dview
->new_frame
= TRUE
;
3069 /* --------------------------------------------------------------------------------------------- */
3072 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3076 dview_ok_to_exit (WDiff
* dview
)
3078 gboolean res
= TRUE
;
3081 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3084 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3085 _("File(s) was modified. Save with exit?") :
3086 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3087 D_NORMAL
, 2, _("&Yes"), _("&No"));
3090 if (mc_global
.midnight_shutdown
|| (act
== -1))
3099 (void) dview_save (dview
);
3103 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3104 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3105 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3106 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3115 /* --------------------------------------------------------------------------------------------- */
3118 dview_execute_cmd (WDiff
* dview
, long command
)
3120 cb_ret_t res
= MSG_HANDLED
;
3124 case CK_ShowSymbols
:
3125 dview
->display_symbols
^= 1;
3126 dview
->new_frame
= TRUE
;
3128 case CK_ShowNumbers
:
3129 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
3130 dview
->new_frame
= TRUE
;
3133 dview
->full
= !dview
->full
;
3134 dview
->new_frame
= TRUE
;
3140 dview
->new_frame
= TRUE
;
3146 dview_compute_split (dview
, 1);
3147 dview
->new_frame
= TRUE
;
3154 dview_compute_split (dview
, -1);
3155 dview
->new_frame
= TRUE
;
3159 dview
->tab_size
= 2;
3162 dview
->tab_size
= 3;
3165 dview
->tab_size
= 4;
3168 dview
->tab_size
= 8;
3177 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3178 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3181 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3182 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3185 dview_goto_cmd (dview
, DIFF_RIGHT
);
3188 dview_edit (dview
, dview
->ord
);
3191 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3195 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3199 dview_edit (dview
, dview
->ord
^ 1);
3202 dview_search_cmd (dview
);
3204 case CK_SearchContinue
:
3205 dview_continue_search_cmd (dview
);
3208 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3211 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3214 if (dview
->skip_rows
> 0)
3217 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3222 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3225 if (dview
->height
> 2)
3227 dview
->skip_rows
+= dview
->height
- 2;
3228 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3232 if (dview
->height
> 2)
3234 dview
->skip_rows
-= dview
->height
- 2;
3235 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3245 dview
->skip_cols
-= 8;
3248 dview
->skip_cols
+= 8;
3251 dview
->skip_cols
= 0;
3257 dview
->view_quit
= TRUE
;
3260 dview_do_save (dview
);
3263 dview_diff_options (dview
);
3266 case CK_SelectCodepage
:
3267 dview_select_encoding (dview
);
3271 /* don't close diffviewer due to SIGINT */
3274 res
= MSG_NOT_HANDLED
;
3279 /* --------------------------------------------------------------------------------------------- */
3282 dview_handle_key (WDiff
* dview
, int key
)
3287 key
= convert_from_input_c (key
);
3290 command
= keybind_lookup_keymap_command (diff_map
, key
);
3291 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3295 return MSG_NOT_HANDLED
;
3298 /* --------------------------------------------------------------------------------------------- */
3301 dview_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3303 WDiff
*dview
= (WDiff
*) w
;
3304 WDialog
*h
= w
->owner
;
3310 dview_labels (dview
);
3311 dview_load_options (dview
);
3312 dview_update (dview
);
3316 dview
->new_frame
= TRUE
;
3317 dview_update (dview
);
3321 i
= dview_handle_key (dview
, parm
);
3322 if (dview
->view_quit
)
3325 dview_update (dview
);
3329 i
= dview_execute_cmd (dview
, parm
);
3330 if (dview
->view_quit
)
3333 dview_update (dview
);
3337 dview_save_options (dview
);
3342 return widget_default_callback (w
, sender
, msg
, parm
, data
);
3346 /* --------------------------------------------------------------------------------------------- */
3349 dview_mouse_callback (Widget
* w
, mouse_msg_t msg
, mouse_event_t
* event
)
3351 WDiff
*dview
= (WDiff
*) w
;
3357 case MSG_MOUSE_SCROLL_UP
:
3358 case MSG_MOUSE_SCROLL_DOWN
:
3359 if (msg
== MSG_MOUSE_SCROLL_UP
)
3360 dview
->skip_rows
-= 2;
3362 dview
->skip_rows
+= 2;
3364 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3365 dview_update (dview
);
3373 /* --------------------------------------------------------------------------------------------- */
3376 dview_adjust_size (WDialog
* h
)
3381 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3382 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3383 bar
= find_buttonbar (h
);
3384 widget_set_size (WIDGET (dview
), 0, 0, LINES
- 1, COLS
);
3385 widget_set_size (WIDGET (bar
), LINES
- 1, 0, 1, COLS
);
3387 dview_compute_areas (dview
);
3390 /* --------------------------------------------------------------------------------------------- */
3393 dview_dialog_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3395 WDiff
*dview
= (WDiff
*) data
;
3396 WDialog
*h
= DIALOG (w
);
3401 dview_adjust_size (h
);
3405 /* Handle shortcuts. */
3407 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3408 * here, which is why we can pass NULL in the following call. */
3409 return dview_execute_cmd (NULL
, parm
);
3412 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3413 h
->state
= DLG_ACTIVE
; /* don't stop the dialog before final decision */
3414 if (dview_ok_to_exit (dview
))
3415 h
->state
= DLG_CLOSED
;
3419 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
3423 /* --------------------------------------------------------------------------------------------- */
3426 dview_get_title (const WDialog
* h
, size_t len
)
3429 const char *modified
= " (*) ";
3430 const char *notmodified
= " ";
3434 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3435 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3437 title
= g_string_sized_new (len
);
3438 g_string_append (title
, _("Diff:"));
3439 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3440 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3441 g_string_append (title
, " | ");
3442 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3443 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3445 return g_string_free (title
, FALSE
);
3448 /* --------------------------------------------------------------------------------------------- */
3451 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3458 /* Create dialog and widgets, put them on the dialog */
3460 dlg_create (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3461 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3463 dview
= g_new0 (WDiff
, 1);
3465 widget_init (w
, 0, 0, LINES
- 1, COLS
, dview_callback
, dview_mouse_callback
);
3466 widget_want_cursor (w
, FALSE
);
3468 add_widget (dview_dlg
, dview
);
3469 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3471 dview_dlg
->get_title
= dview_get_title
;
3473 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3475 /* Please note that if you add another widget,
3476 * you have to modify dview_adjust_size to
3480 dlg_run (dview_dlg
);
3482 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3483 dlg_destroy (dview_dlg
);
3485 return error
== 0 ? 1 : 0;
3488 /*** public functions ****************************************************************************/
3489 /* --------------------------------------------------------------------------------------------- */
3491 #define GET_FILE_AND_STAMP(n) \
3495 real_file##n = file##n; \
3496 if (!vfs_file_is_local (file##n)) \
3498 real_file##n = mc_getlocalcopy (file##n); \
3499 if (real_file##n != NULL) \
3502 if (mc_stat (real_file##n, &st##n) != 0) \
3509 #define UNGET_FILE(n) \
3515 if (use_copy##n > 0) \
3518 mtime = st##n.st_mtime; \
3519 if (mc_stat (real_file##n, &st##n) == 0) \
3520 changed = (mtime != st##n.st_mtime); \
3522 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3523 vfs_path_free (real_file##n); \
3529 dview_diff_cmd (const void *f0
, const void *f1
)
3532 vfs_path_t
*file0
= NULL
;
3533 vfs_path_t
*file1
= NULL
;
3534 gboolean is_dir0
= FALSE
;
3535 gboolean is_dir1
= FALSE
;
3537 switch (mc_global
.mc_run_mode
)
3541 /* run from panels */
3542 const WPanel
*panel0
= (const WPanel
*) f0
;
3543 const WPanel
*panel1
= (const WPanel
*) f1
;
3545 file0
= vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, NULL
);
3546 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3549 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3550 path_trunc (selection (panel0
)->fname
, 30));
3554 file1
= vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, NULL
);
3555 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3558 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3559 path_trunc (selection (panel1
)->fname
, 30));
3565 case MC_RUN_DIFFVIEWER
:
3567 /* run from command line */
3568 const char *p0
= (const char *) f0
;
3569 const char *p1
= (const char *) f1
;
3572 file0
= vfs_path_from_str (p0
);
3573 if (mc_stat (file0
, &st
) == 0)
3575 is_dir0
= S_ISDIR (st
.st_mode
);
3578 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3584 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3585 path_trunc (p0
, 30), unix_error_string (errno
));
3589 file1
= vfs_path_from_str (p1
);
3590 if (mc_stat (file1
, &st
) == 0)
3592 is_dir1
= S_ISDIR (st
.st_mode
);
3595 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3601 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3602 path_trunc (p1
, 30), unix_error_string (errno
));
3609 /* this should not happaned */
3610 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3617 if (file0
!= NULL
&& file1
!= NULL
)
3623 vfs_path_t
*real_file0
;
3624 vfs_path_t
*real_file1
;
3626 GET_FILE_AND_STAMP (0);
3627 GET_FILE_AND_STAMP (1);
3629 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3630 rv
= diff_view (vfs_path_as_str (real_file0
), vfs_path_as_str (real_file1
),
3631 vfs_path_as_str (file0
), vfs_path_as_str (file1
));
3639 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3642 vfs_path_free (file1
);
3643 vfs_path_free (file0
);
3648 #undef GET_FILE_AND_STAMP
3651 /* --------------------------------------------------------------------------------------------- */